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 e9424729 2018-08-04 stsp struct tog_blame blame;
443 6c4c42e0 2019-06-24 stsp int matched_line;
444 11b20872 2019-11-08 stsp struct tog_colors colors;
445 ad80ab7b 2018-08-04 stsp };
446 ad80ab7b 2018-08-04 stsp
447 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
448 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 ad80ab7b 2018-08-04 stsp int selected;
453 ad80ab7b 2018-08-04 stsp };
454 ad80ab7b 2018-08-04 stsp
455 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
456 ad80ab7b 2018-08-04 stsp
457 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
458 ad80ab7b 2018-08-04 stsp char *tree_label;
459 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
460 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
466 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
468 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
469 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
470 6458efa5 2020-11-24 stsp struct tog_colors colors;
471 6458efa5 2020-11-24 stsp };
472 6458efa5 2020-11-24 stsp
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
474 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
475 6458efa5 2020-11-24 stsp struct got_reference *ref;
476 6458efa5 2020-11-24 stsp int idx;
477 6458efa5 2020-11-24 stsp };
478 6458efa5 2020-11-24 stsp
479 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
480 6458efa5 2020-11-24 stsp
481 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
482 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
483 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
486 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 6458efa5 2020-11-24 stsp struct got_repository *repo;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
489 bddb1296 2019-11-08 stsp struct tog_colors colors;
490 7cbe629d 2018-08-04 stsp };
491 7cbe629d 2018-08-04 stsp
492 669b5ffa 2018-10-07 stsp /*
493 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
494 669b5ffa 2018-10-07 stsp *
495 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
496 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
497 669b5ffa 2018-10-07 stsp * there is enough screen estate.
498 669b5ffa 2018-10-07 stsp *
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
501 669b5ffa 2018-10-07 stsp *
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
504 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
505 669b5ffa 2018-10-07 stsp *
506 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
507 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
508 669b5ffa 2018-10-07 stsp */
509 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
510 669b5ffa 2018-10-07 stsp
511 cc3c9aac 2018-08-01 stsp struct tog_view {
512 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
513 26ed57b2 2018-05-19 stsp WINDOW *window;
514 26ed57b2 2018-05-19 stsp PANEL *panel;
515 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
517 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
518 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
519 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
521 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
522 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
523 9970f7fc 2020-12-03 stsp int dying;
524 669b5ffa 2018-10-07 stsp struct tog_view *parent;
525 669b5ffa 2018-10-07 stsp struct tog_view *child;
526 5dc9f4bc 2018-08-04 stsp
527 e78dc838 2020-12-04 stsp /*
528 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
529 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
530 e78dc838 2020-12-04 stsp * between parent and child.
531 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
532 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
533 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
534 e78dc838 2020-12-04 stsp * situations.
535 e78dc838 2020-12-04 stsp */
536 e78dc838 2020-12-04 stsp int focus_child;
537 e78dc838 2020-12-04 stsp
538 9b058f45 2022-06-30 mark enum tog_view_mode mode;
539 5dc9f4bc 2018-08-04 stsp /* type-specific state */
540 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
541 5dc9f4bc 2018-08-04 stsp union {
542 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
543 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
544 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
545 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
546 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
547 5dc9f4bc 2018-08-04 stsp } state;
548 e5a0f69f 2018-08-18 stsp
549 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
550 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
551 e78dc838 2020-12-04 stsp struct tog_view *, int);
552 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
553 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
555 60493ae3 2019-06-20 stsp
556 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
558 c0c4acc8 2021-01-24 stsp int search_started;
559 60493ae3 2019-06-20 stsp int searching;
560 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
562 60493ae3 2019-06-20 stsp int search_next_done;
563 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
565 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
566 1803e47f 2019-06-22 stsp regex_t regex;
567 41605754 2020-11-12 stsp regmatch_t regmatch;
568 cc3c9aac 2018-08-01 stsp };
569 cd0acaa7 2018-05-20 stsp
570 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
571 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
572 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
573 78756c87 2020-11-24 stsp struct got_repository *);
574 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
576 e78dc838 2020-12-04 stsp struct tog_view *, int);
577 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
579 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp
582 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
583 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
584 78756c87 2020-11-24 stsp const char *, const char *, int);
585 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
587 e78dc838 2020-12-04 stsp struct tog_view *, int);
588 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
590 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp
593 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
594 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
595 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
596 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
600 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp
603 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
604 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
605 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
607 e78dc838 2020-12-04 stsp struct tog_view *, int);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
609 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
611 6458efa5 2020-11-24 stsp
612 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
613 6458efa5 2020-11-24 stsp struct got_repository *);
614 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
615 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
616 e78dc838 2020-12-04 stsp struct tog_view *, int);
617 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
620 25791caa 2018-10-24 stsp
621 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
622 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
623 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
624 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
625 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
626 25791caa 2018-10-24 stsp
627 25791caa 2018-10-24 stsp static void
628 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
629 25791caa 2018-10-24 stsp {
630 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
631 83baff54 2019-08-12 stsp }
632 83baff54 2019-08-12 stsp
633 83baff54 2019-08-12 stsp static void
634 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
635 83baff54 2019-08-12 stsp {
636 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
637 25791caa 2018-10-24 stsp }
638 26ed57b2 2018-05-19 stsp
639 61266923 2020-01-14 stsp static void
640 61266923 2020-01-14 stsp tog_sigcont(int signo)
641 61266923 2020-01-14 stsp {
642 61266923 2020-01-14 stsp tog_sigcont_received = 1;
643 61266923 2020-01-14 stsp }
644 61266923 2020-01-14 stsp
645 2497f032 2022-05-31 stsp static void
646 2497f032 2022-05-31 stsp tog_sigint(int signo)
647 2497f032 2022-05-31 stsp {
648 2497f032 2022-05-31 stsp tog_sigint_received = 1;
649 2497f032 2022-05-31 stsp }
650 2497f032 2022-05-31 stsp
651 2497f032 2022-05-31 stsp static void
652 2497f032 2022-05-31 stsp tog_sigterm(int signo)
653 2497f032 2022-05-31 stsp {
654 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
655 2497f032 2022-05-31 stsp }
656 2497f032 2022-05-31 stsp
657 2497f032 2022-05-31 stsp static int
658 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
659 2497f032 2022-05-31 stsp {
660 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
661 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
662 2497f032 2022-05-31 stsp }
663 2497f032 2022-05-31 stsp
664 e5a0f69f 2018-08-18 stsp static const struct got_error *
665 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
666 ea5e7bb5 2018-08-01 stsp {
667 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
668 e5a0f69f 2018-08-18 stsp
669 669b5ffa 2018-10-07 stsp if (view->child) {
670 5629093a 2022-07-11 stsp child_err = view_close(view->child);
671 669b5ffa 2018-10-07 stsp view->child = NULL;
672 669b5ffa 2018-10-07 stsp }
673 e5a0f69f 2018-08-18 stsp if (view->close)
674 e5a0f69f 2018-08-18 stsp err = view->close(view);
675 ea5e7bb5 2018-08-01 stsp if (view->panel)
676 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
677 ea5e7bb5 2018-08-01 stsp if (view->window)
678 ea5e7bb5 2018-08-01 stsp delwin(view->window);
679 ea5e7bb5 2018-08-01 stsp free(view);
680 5629093a 2022-07-11 stsp return err ? err : child_err;
681 ea5e7bb5 2018-08-01 stsp }
682 ea5e7bb5 2018-08-01 stsp
683 ea5e7bb5 2018-08-01 stsp static struct tog_view *
684 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
685 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
686 ea5e7bb5 2018-08-01 stsp {
687 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
688 ea5e7bb5 2018-08-01 stsp
689 ea5e7bb5 2018-08-01 stsp if (view == NULL)
690 ea5e7bb5 2018-08-01 stsp return NULL;
691 ea5e7bb5 2018-08-01 stsp
692 d6b05b5b 2018-08-04 stsp view->type = type;
693 f7d12f7e 2018-08-01 stsp view->lines = LINES;
694 f7d12f7e 2018-08-01 stsp view->cols = COLS;
695 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
696 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
697 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
698 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
699 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
700 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
701 96a765a8 2018-08-04 stsp view_close(view);
702 ea5e7bb5 2018-08-01 stsp return NULL;
703 ea5e7bb5 2018-08-01 stsp }
704 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
705 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
706 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp }
710 ea5e7bb5 2018-08-01 stsp
711 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
712 ea5e7bb5 2018-08-01 stsp return view;
713 cdf1ee82 2018-08-01 stsp }
714 cdf1ee82 2018-08-01 stsp
715 0cf4efb1 2018-09-29 stsp static int
716 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
717 0cf4efb1 2018-09-29 stsp {
718 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
719 0cf4efb1 2018-09-29 stsp return 0;
720 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
721 5c60c32a 2018-10-18 stsp }
722 5c60c32a 2018-10-18 stsp
723 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
724 9b058f45 2022-06-30 mark static int
725 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
726 9b058f45 2022-06-30 mark {
727 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
728 9b058f45 2022-06-30 mark }
729 9b058f45 2022-06-30 mark
730 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
731 5c60c32a 2018-10-18 stsp
732 5c60c32a 2018-10-18 stsp static const struct got_error *
733 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
734 5c60c32a 2018-10-18 stsp {
735 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
736 5c60c32a 2018-10-18 stsp
737 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
738 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
739 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
740 3c1dfe12 2022-07-08 mark else
741 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
742 9b058f45 2022-06-30 mark view->begin_x = 0;
743 571ccd73 2022-07-19 mark } else if (!view->resized) {
744 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
745 3c1dfe12 2022-07-08 mark view->cols > 119)
746 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
747 3c1dfe12 2022-07-08 mark else
748 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
749 9b058f45 2022-06-30 mark view->begin_y = 0;
750 9b058f45 2022-06-30 mark }
751 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
752 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
753 5c60c32a 2018-10-18 stsp view->lines = LINES;
754 5c60c32a 2018-10-18 stsp view->cols = COLS;
755 5c60c32a 2018-10-18 stsp err = view_resize(view);
756 5c60c32a 2018-10-18 stsp if (err)
757 5c60c32a 2018-10-18 stsp return err;
758 5c60c32a 2018-10-18 stsp
759 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
760 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
761 9b058f45 2022-06-30 mark
762 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
763 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
764 5c60c32a 2018-10-18 stsp
765 5c60c32a 2018-10-18 stsp return NULL;
766 5c60c32a 2018-10-18 stsp }
767 5c60c32a 2018-10-18 stsp
768 5c60c32a 2018-10-18 stsp static const struct got_error *
769 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
770 5c60c32a 2018-10-18 stsp {
771 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
772 5c60c32a 2018-10-18 stsp
773 5c60c32a 2018-10-18 stsp view->begin_x = 0;
774 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
775 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
776 5c60c32a 2018-10-18 stsp view->ncols = COLS;
777 5c60c32a 2018-10-18 stsp view->lines = LINES;
778 5c60c32a 2018-10-18 stsp view->cols = COLS;
779 5c60c32a 2018-10-18 stsp err = view_resize(view);
780 5c60c32a 2018-10-18 stsp if (err)
781 5c60c32a 2018-10-18 stsp return err;
782 5c60c32a 2018-10-18 stsp
783 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
784 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
785 5c60c32a 2018-10-18 stsp
786 5c60c32a 2018-10-18 stsp return NULL;
787 0cf4efb1 2018-09-29 stsp }
788 0cf4efb1 2018-09-29 stsp
789 5c60c32a 2018-10-18 stsp static int
790 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
791 5c60c32a 2018-10-18 stsp {
792 5c60c32a 2018-10-18 stsp return view->parent == NULL;
793 5c60c32a 2018-10-18 stsp }
794 5c60c32a 2018-10-18 stsp
795 6131ff18 2022-06-20 mark static int
796 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
797 6131ff18 2022-06-20 mark {
798 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
799 24b9cfdc 2022-06-30 stsp }
800 24b9cfdc 2022-06-30 stsp
801 24b9cfdc 2022-06-30 stsp static int
802 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
803 24b9cfdc 2022-06-30 stsp {
804 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
805 6131ff18 2022-06-20 mark }
806 6131ff18 2022-06-20 mark
807 49b24ee5 2022-07-03 mark static int
808 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
809 49b24ee5 2022-07-03 mark {
810 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
811 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
812 49b24ee5 2022-07-03 mark }
813 49b24ee5 2022-07-03 mark
814 9b058f45 2022-06-30 mark static void
815 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
816 9b058f45 2022-06-30 mark {
817 9b058f45 2022-06-30 mark PANEL *panel;
818 9b058f45 2022-06-30 mark const struct tog_view *view_above;
819 6131ff18 2022-06-20 mark
820 9b058f45 2022-06-30 mark if (view->parent)
821 9b058f45 2022-06-30 mark return view_border(view->parent);
822 9b058f45 2022-06-30 mark
823 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
824 9b058f45 2022-06-30 mark if (panel == NULL)
825 9b058f45 2022-06-30 mark return;
826 9b058f45 2022-06-30 mark
827 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
828 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
829 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
830 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
831 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
832 9b058f45 2022-06-30 mark else
833 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
834 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
835 9b058f45 2022-06-30 mark }
836 9b058f45 2022-06-30 mark
837 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
838 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
839 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
840 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
841 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
842 9b058f45 2022-06-30 mark
843 4d8c2215 2018-08-19 stsp static const struct got_error *
844 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
845 f7d12f7e 2018-08-01 stsp {
846 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
847 9b058f45 2022-06-30 mark int dif, nlines, ncols;
848 f7d12f7e 2018-08-01 stsp
849 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
850 9b058f45 2022-06-30 mark
851 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
852 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
853 0cf4efb1 2018-09-29 stsp else
854 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
855 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
856 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
857 0cf4efb1 2018-09-29 stsp else
858 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
859 6d0fee91 2018-08-01 stsp
860 4dd27a72 2022-06-29 stsp if (view->child) {
861 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
862 9b058f45 2022-06-30 mark
863 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
864 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
865 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
866 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
867 0dbbbe90 2022-06-17 op ncols = COLS;
868 0dbbbe90 2022-06-17 op
869 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
870 5c60c32a 2018-10-18 stsp if (view->child->focussed)
871 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
872 5c60c32a 2018-10-18 stsp else
873 5c60c32a 2018-10-18 stsp show_panel(view->panel);
874 5c60c32a 2018-10-18 stsp } else {
875 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
876 0dbbbe90 2022-06-17 op
877 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
878 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
879 9b058f45 2022-06-30 mark }
880 9b058f45 2022-06-30 mark /*
881 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
882 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
883 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
884 9b058f45 2022-06-30 mark */
885 9b058f45 2022-06-30 mark if (hs) {
886 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
887 9b058f45 2022-06-30 mark if (err)
888 9b058f45 2022-06-30 mark return err;
889 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
890 9b058f45 2022-06-30 mark err = offset_selection_down(view);
891 9b058f45 2022-06-30 mark if (err)
892 9b058f45 2022-06-30 mark return err;
893 9b058f45 2022-06-30 mark }
894 9b058f45 2022-06-30 mark view_border(view);
895 9b058f45 2022-06-30 mark update_panels();
896 9b058f45 2022-06-30 mark doupdate();
897 9b058f45 2022-06-30 mark show_panel(view->child->panel);
898 9b058f45 2022-06-30 mark nlines = view->nlines;
899 9b058f45 2022-06-30 mark }
900 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
901 0dbbbe90 2022-06-17 op ncols = COLS;
902 669b5ffa 2018-10-07 stsp
903 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
904 571ccd73 2022-07-19 mark err = view->resize(view, dif);
905 571ccd73 2022-07-19 mark if (err)
906 571ccd73 2022-07-19 mark return err;
907 571ccd73 2022-07-19 mark }
908 571ccd73 2022-07-19 mark
909 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
910 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
911 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
912 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
913 0dbbbe90 2022-06-17 op wclear(view->window);
914 0dbbbe90 2022-06-17 op
915 0dbbbe90 2022-06-17 op view->nlines = nlines;
916 0dbbbe90 2022-06-17 op view->ncols = ncols;
917 0dbbbe90 2022-06-17 op view->lines = LINES;
918 0dbbbe90 2022-06-17 op view->cols = COLS;
919 0dbbbe90 2022-06-17 op
920 5c60c32a 2018-10-18 stsp return NULL;
921 571ccd73 2022-07-19 mark }
922 571ccd73 2022-07-19 mark
923 571ccd73 2022-07-19 mark static const struct got_error *
924 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
925 571ccd73 2022-07-19 mark {
926 571ccd73 2022-07-19 mark struct tog_log_view_state *s = &view->state.log;
927 571ccd73 2022-07-19 mark const struct got_error *err = NULL;
928 571ccd73 2022-07-19 mark int n = s->selected_entry->idx + view->lines - s->selected;
929 571ccd73 2022-07-19 mark
930 571ccd73 2022-07-19 mark /*
931 571ccd73 2022-07-19 mark * Request commits to account for the increased
932 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
933 571ccd73 2022-07-19 mark */
934 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
935 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
936 571ccd73 2022-07-19 mark err = request_log_commits(view);
937 571ccd73 2022-07-19 mark }
938 571ccd73 2022-07-19 mark
939 571ccd73 2022-07-19 mark return err;
940 d9a7ab53 2022-07-11 mark }
941 d9a7ab53 2022-07-11 mark
942 d9a7ab53 2022-07-11 mark static void
943 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
944 d9a7ab53 2022-07-11 mark {
945 d9a7ab53 2022-07-11 mark if (n == 0)
946 d9a7ab53 2022-07-11 mark return;
947 d9a7ab53 2022-07-11 mark
948 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
949 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
950 d9a7ab53 2022-07-11 mark view->parent->offset += n;
951 d9a7ab53 2022-07-11 mark else
952 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
953 d9a7ab53 2022-07-11 mark } else if (view->offset) {
954 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
955 d9a7ab53 2022-07-11 mark view->offset -= n;
956 d9a7ab53 2022-07-11 mark else
957 d9a7ab53 2022-07-11 mark view->offset = 0;
958 d9a7ab53 2022-07-11 mark }
959 3c1dfe12 2022-07-08 mark }
960 3c1dfe12 2022-07-08 mark
961 3c1dfe12 2022-07-08 mark static const struct got_error *
962 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
963 3c1dfe12 2022-07-08 mark {
964 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
965 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
966 3c1dfe12 2022-07-08 mark
967 3c1dfe12 2022-07-08 mark if (view->parent)
968 3c1dfe12 2022-07-08 mark v = view->parent;
969 3c1dfe12 2022-07-08 mark else
970 3c1dfe12 2022-07-08 mark v = view;
971 3c1dfe12 2022-07-08 mark
972 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
973 3c1dfe12 2022-07-08 mark return NULL;
974 3c1dfe12 2022-07-08 mark
975 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
976 3c1dfe12 2022-07-08 mark
977 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
978 27187d45 2022-07-11 mark int y = v->child->begin_y;
979 27187d45 2022-07-11 mark
980 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
981 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
982 3c1dfe12 2022-07-08 mark if (view->parent)
983 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
984 3c1dfe12 2022-07-08 mark else
985 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
986 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
987 3c1dfe12 2022-07-08 mark view->count = 0;
988 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
989 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
990 3c1dfe12 2022-07-08 mark view->count = 0;
991 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
992 3c1dfe12 2022-07-08 mark }
993 3c1dfe12 2022-07-08 mark v->ncols = COLS;
994 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
995 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
996 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
997 3c1dfe12 2022-07-08 mark if (err)
998 3c1dfe12 2022-07-08 mark return err;
999 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1000 2525dccb 2022-07-13 mark if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1001 27187d45 2022-07-11 mark v->child->nscrolled = y - v->child->begin_y;
1002 2525dccb 2022-07-13 mark else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1003 2525dccb 2022-07-13 mark v->nscrolled = v->child->begin_y - y;
1004 3c1dfe12 2022-07-08 mark } else {
1005 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1006 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1007 3c1dfe12 2022-07-08 mark if (view->parent)
1008 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1009 3c1dfe12 2022-07-08 mark else
1010 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1011 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1012 3c1dfe12 2022-07-08 mark view->count = 0;
1013 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1014 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1015 3c1dfe12 2022-07-08 mark view->count = 0;
1016 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1017 3c1dfe12 2022-07-08 mark }
1018 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1019 3c1dfe12 2022-07-08 mark }
1020 3c1dfe12 2022-07-08 mark
1021 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1022 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1023 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1024 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1025 3c1dfe12 2022-07-08 mark
1026 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1027 3c1dfe12 2022-07-08 mark if (err)
1028 3c1dfe12 2022-07-08 mark return err;
1029 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1030 3c1dfe12 2022-07-08 mark if (err)
1031 3c1dfe12 2022-07-08 mark return err;
1032 3c1dfe12 2022-07-08 mark
1033 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1034 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1035 3c1dfe12 2022-07-08 mark if (err)
1036 3c1dfe12 2022-07-08 mark return err;
1037 3c1dfe12 2022-07-08 mark }
1038 3c1dfe12 2022-07-08 mark
1039 2525dccb 2022-07-13 mark if (v->nscrolled)
1040 3c1dfe12 2022-07-08 mark err = request_log_commits(v);
1041 2525dccb 2022-07-13 mark else if (v->child->nscrolled)
1042 3c1dfe12 2022-07-08 mark err = request_log_commits(v->child);
1043 3c1dfe12 2022-07-08 mark
1044 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1045 3c1dfe12 2022-07-08 mark
1046 3c1dfe12 2022-07-08 mark return err;
1047 3c1dfe12 2022-07-08 mark }
1048 3c1dfe12 2022-07-08 mark
1049 3c1dfe12 2022-07-08 mark static void
1050 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1051 3c1dfe12 2022-07-08 mark {
1052 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1053 3c1dfe12 2022-07-08 mark
1054 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1055 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1056 669b5ffa 2018-10-07 stsp }
1057 669b5ffa 2018-10-07 stsp
1058 669b5ffa 2018-10-07 stsp static const struct got_error *
1059 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1060 669b5ffa 2018-10-07 stsp {
1061 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1062 669b5ffa 2018-10-07 stsp
1063 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1064 669b5ffa 2018-10-07 stsp return NULL;
1065 669b5ffa 2018-10-07 stsp
1066 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1067 669b5ffa 2018-10-07 stsp view->child = NULL;
1068 669b5ffa 2018-10-07 stsp return err;
1069 669b5ffa 2018-10-07 stsp }
1070 669b5ffa 2018-10-07 stsp
1071 0dbbbe90 2022-06-17 op static const struct got_error *
1072 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1073 669b5ffa 2018-10-07 stsp {
1074 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1075 3c1dfe12 2022-07-08 mark
1076 669b5ffa 2018-10-07 stsp view->child = child;
1077 669b5ffa 2018-10-07 stsp child->parent = view;
1078 0dbbbe90 2022-06-17 op
1079 3c1dfe12 2022-07-08 mark err = view_resize(view);
1080 3c1dfe12 2022-07-08 mark if (err)
1081 3c1dfe12 2022-07-08 mark return err;
1082 3c1dfe12 2022-07-08 mark
1083 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1084 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1085 3c1dfe12 2022-07-08 mark
1086 3c1dfe12 2022-07-08 mark return err;
1087 bfddd0d9 2018-09-29 stsp }
1088 bfddd0d9 2018-09-29 stsp
1089 34bc9ec9 2019-02-22 stsp static void
1090 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1091 25791caa 2018-10-24 stsp {
1092 25791caa 2018-10-24 stsp int cols, lines;
1093 25791caa 2018-10-24 stsp struct winsize size;
1094 25791caa 2018-10-24 stsp
1095 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1096 25791caa 2018-10-24 stsp cols = 80; /* Default */
1097 25791caa 2018-10-24 stsp lines = 24;
1098 25791caa 2018-10-24 stsp } else {
1099 25791caa 2018-10-24 stsp cols = size.ws_col;
1100 25791caa 2018-10-24 stsp lines = size.ws_row;
1101 25791caa 2018-10-24 stsp }
1102 25791caa 2018-10-24 stsp resize_term(lines, cols);
1103 2b49a8ae 2019-06-22 stsp }
1104 2b49a8ae 2019-06-22 stsp
1105 2b49a8ae 2019-06-22 stsp static const struct got_error *
1106 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1107 2b49a8ae 2019-06-22 stsp {
1108 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1109 9b058f45 2022-06-30 mark struct tog_view *v = view;
1110 2b49a8ae 2019-06-22 stsp char pattern[1024];
1111 2b49a8ae 2019-06-22 stsp int ret;
1112 c0c4acc8 2021-01-24 stsp
1113 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1114 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1115 c0c4acc8 2021-01-24 stsp view->searching = 0;
1116 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1117 c0c4acc8 2021-01-24 stsp }
1118 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1119 2b49a8ae 2019-06-22 stsp
1120 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1121 2b49a8ae 2019-06-22 stsp return NULL;
1122 2b49a8ae 2019-06-22 stsp
1123 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1124 9b058f45 2022-06-30 mark v = view->child;
1125 2b49a8ae 2019-06-22 stsp
1126 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1127 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1128 9b058f45 2022-06-30 mark
1129 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1130 2b49a8ae 2019-06-22 stsp nocbreak();
1131 2b49a8ae 2019-06-22 stsp echo();
1132 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1133 9b058f45 2022-06-30 mark wrefresh(v->window);
1134 2b49a8ae 2019-06-22 stsp cbreak();
1135 2b49a8ae 2019-06-22 stsp noecho();
1136 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1137 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1138 2b49a8ae 2019-06-22 stsp return NULL;
1139 2b49a8ae 2019-06-22 stsp
1140 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1141 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1142 7c32bd05 2019-06-22 stsp if (err) {
1143 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1144 7c32bd05 2019-06-22 stsp return err;
1145 7c32bd05 2019-06-22 stsp }
1146 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1147 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1148 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1149 2b49a8ae 2019-06-22 stsp view->search_next(view);
1150 2b49a8ae 2019-06-22 stsp }
1151 2b49a8ae 2019-06-22 stsp
1152 2b49a8ae 2019-06-22 stsp return NULL;
1153 d2366e29 2022-07-07 mark }
1154 d2366e29 2022-07-07 mark
1155 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1156 d2366e29 2022-07-07 mark static const struct got_error *
1157 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1158 d2366e29 2022-07-07 mark {
1159 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1160 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1161 d2366e29 2022-07-07 mark
1162 d2366e29 2022-07-07 mark if (view->parent)
1163 d2366e29 2022-07-07 mark v = view->parent;
1164 d2366e29 2022-07-07 mark else
1165 d2366e29 2022-07-07 mark v = view;
1166 d2366e29 2022-07-07 mark
1167 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1168 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1169 7532ccda 2022-07-11 mark else
1170 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1171 d2366e29 2022-07-07 mark
1172 7532ccda 2022-07-11 mark if (!v->child)
1173 7532ccda 2022-07-11 mark return NULL;
1174 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1175 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1176 7532ccda 2022-07-11 mark
1177 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1178 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1179 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1180 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1181 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1182 d2366e29 2022-07-07 mark
1183 7532ccda 2022-07-11 mark
1184 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1185 d2366e29 2022-07-07 mark v->ncols = COLS;
1186 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1187 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1188 d2366e29 2022-07-07 mark
1189 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1190 d2366e29 2022-07-07 mark if (err)
1191 d2366e29 2022-07-07 mark return err;
1192 d2366e29 2022-07-07 mark }
1193 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1194 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1195 d2366e29 2022-07-07 mark v->focus_child = 1;
1196 d2366e29 2022-07-07 mark
1197 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1198 d2366e29 2022-07-07 mark if (err)
1199 d2366e29 2022-07-07 mark return err;
1200 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1201 d2366e29 2022-07-07 mark if (err)
1202 d2366e29 2022-07-07 mark return err;
1203 d2366e29 2022-07-07 mark
1204 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1205 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1206 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1207 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1208 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1209 7532ccda 2022-07-11 mark } else {
1210 7532ccda 2022-07-11 mark offset_selection_up(v);
1211 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1212 d2366e29 2022-07-07 mark }
1213 27187d45 2022-07-11 mark if (v->type == TOG_VIEW_LOG && v->nscrolled)
1214 7532ccda 2022-07-11 mark err = request_log_commits(v);
1215 27187d45 2022-07-11 mark else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1216 7532ccda 2022-07-11 mark err = request_log_commits(v->child);
1217 d2366e29 2022-07-07 mark
1218 d2366e29 2022-07-07 mark return err;
1219 0cf4efb1 2018-09-29 stsp }
1220 6d0fee91 2018-08-01 stsp
1221 640cd7ff 2022-06-22 mark /*
1222 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1223 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1224 640cd7ff 2022-06-22 mark */
1225 640cd7ff 2022-06-22 mark static int
1226 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1227 640cd7ff 2022-06-22 mark {
1228 9b058f45 2022-06-30 mark struct tog_view *v = view;
1229 9b058f45 2022-06-30 mark int x, n = 0;
1230 640cd7ff 2022-06-22 mark
1231 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1232 9b058f45 2022-06-30 mark v = view->child;
1233 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1234 9b058f45 2022-06-30 mark v = view->parent;
1235 9b058f45 2022-06-30 mark
1236 640cd7ff 2022-06-22 mark view->count = 0;
1237 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1238 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1239 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1240 9b058f45 2022-06-30 mark waddch(v->window, ':');
1241 640cd7ff 2022-06-22 mark
1242 640cd7ff 2022-06-22 mark do {
1243 9b058f45 2022-06-30 mark x = getcurx(v->window);
1244 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1245 9b058f45 2022-06-30 mark waddch(v->window, c);
1246 9b058f45 2022-06-30 mark wrefresh(v->window);
1247 9b058f45 2022-06-30 mark }
1248 9b058f45 2022-06-30 mark
1249 640cd7ff 2022-06-22 mark /*
1250 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1251 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1252 640cd7ff 2022-06-22 mark */
1253 640cd7ff 2022-06-22 mark if (n >= 9999999)
1254 640cd7ff 2022-06-22 mark n = 9999999;
1255 640cd7ff 2022-06-22 mark else
1256 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1257 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1258 640cd7ff 2022-06-22 mark
1259 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1260 640cd7ff 2022-06-22 mark view->count = n;
1261 640cd7ff 2022-06-22 mark
1262 640cd7ff 2022-06-22 mark return c;
1263 640cd7ff 2022-06-22 mark }
1264 640cd7ff 2022-06-22 mark
1265 0cf4efb1 2018-09-29 stsp static const struct got_error *
1266 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1267 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1268 e5a0f69f 2018-08-18 stsp {
1269 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1270 669b5ffa 2018-10-07 stsp struct tog_view *v;
1271 1a76625f 2018-10-22 stsp int ch, errcode;
1272 e5a0f69f 2018-08-18 stsp
1273 e5a0f69f 2018-08-18 stsp *new = NULL;
1274 8f4ed634 2020-03-26 stsp
1275 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1276 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1277 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1278 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1279 640cd7ff 2022-06-22 mark view->count = 0;
1280 640cd7ff 2022-06-22 mark }
1281 e5a0f69f 2018-08-18 stsp
1282 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1283 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1284 82954512 2020-02-03 stsp if (errcode)
1285 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1286 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1287 3da8ef85 2021-09-21 stsp sched_yield();
1288 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1289 82954512 2020-02-03 stsp if (errcode)
1290 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1291 82954512 2020-02-03 stsp "pthread_mutex_lock");
1292 60493ae3 2019-06-20 stsp view->search_next(view);
1293 60493ae3 2019-06-20 stsp return NULL;
1294 60493ae3 2019-06-20 stsp }
1295 60493ae3 2019-06-20 stsp
1296 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1297 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1298 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1299 1a76625f 2018-10-22 stsp if (errcode)
1300 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1301 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1302 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1303 a6d37fac 2022-07-03 mark cbreak();
1304 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1305 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1306 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1307 a6d37fac 2022-07-03 mark view->count = 0;
1308 a6d37fac 2022-07-03 mark else
1309 a6d37fac 2022-07-03 mark ch = view->ch;
1310 a6d37fac 2022-07-03 mark } else {
1311 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1312 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1313 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1314 640cd7ff 2022-06-22 mark }
1315 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1316 1a76625f 2018-10-22 stsp if (errcode)
1317 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1318 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1319 25791caa 2018-10-24 stsp
1320 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1321 25791caa 2018-10-24 stsp tog_resizeterm();
1322 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1323 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1324 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1325 25791caa 2018-10-24 stsp err = view_resize(v);
1326 25791caa 2018-10-24 stsp if (err)
1327 25791caa 2018-10-24 stsp return err;
1328 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1329 25791caa 2018-10-24 stsp if (err)
1330 25791caa 2018-10-24 stsp return err;
1331 cdfcfb03 2020-12-06 stsp if (v->child) {
1332 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1333 cdfcfb03 2020-12-06 stsp if (err)
1334 cdfcfb03 2020-12-06 stsp return err;
1335 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1336 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1337 cdfcfb03 2020-12-06 stsp if (err)
1338 cdfcfb03 2020-12-06 stsp return err;
1339 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1340 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1341 3c1dfe12 2022-07-08 mark if (err)
1342 3c1dfe12 2022-07-08 mark return err;
1343 3c1dfe12 2022-07-08 mark }
1344 cdfcfb03 2020-12-06 stsp }
1345 25791caa 2018-10-24 stsp }
1346 25791caa 2018-10-24 stsp }
1347 25791caa 2018-10-24 stsp
1348 e5a0f69f 2018-08-18 stsp switch (ch) {
1349 1e37a5c2 2019-05-12 jcs case '\t':
1350 640cd7ff 2022-06-22 mark view->count = 0;
1351 1e37a5c2 2019-05-12 jcs if (view->child) {
1352 e78dc838 2020-12-04 stsp view->focussed = 0;
1353 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1354 e78dc838 2020-12-04 stsp view->focus_child = 1;
1355 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1356 e78dc838 2020-12-04 stsp view->focussed = 0;
1357 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1358 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1359 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1360 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1361 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1362 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1363 9b058f45 2022-06-30 mark if (err)
1364 9b058f45 2022-06-30 mark return err;
1365 9b058f45 2022-06-30 mark }
1366 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1367 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1368 9b058f45 2022-06-30 mark if (err)
1369 9b058f45 2022-06-30 mark return err;
1370 9b058f45 2022-06-30 mark }
1371 1e37a5c2 2019-05-12 jcs }
1372 1e37a5c2 2019-05-12 jcs break;
1373 1e37a5c2 2019-05-12 jcs case 'q':
1374 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1375 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1376 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1377 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1378 9b058f45 2022-06-30 mark if (err)
1379 9b058f45 2022-06-30 mark break;
1380 9b058f45 2022-06-30 mark }
1381 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1382 9b058f45 2022-06-30 mark }
1383 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1384 9970f7fc 2020-12-03 stsp view->dying = 1;
1385 1e37a5c2 2019-05-12 jcs break;
1386 1e37a5c2 2019-05-12 jcs case 'Q':
1387 1e37a5c2 2019-05-12 jcs *done = 1;
1388 1e37a5c2 2019-05-12 jcs break;
1389 61417565 2022-06-20 mark case 'F':
1390 640cd7ff 2022-06-22 mark view->count = 0;
1391 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1392 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1393 1e37a5c2 2019-05-12 jcs break;
1394 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1395 e78dc838 2020-12-04 stsp view->focussed = 0;
1396 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1397 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1398 3c1dfe12 2022-07-08 mark } else {
1399 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1400 3c1dfe12 2022-07-08 mark if (!err)
1401 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1402 3c1dfe12 2022-07-08 mark }
1403 1e37a5c2 2019-05-12 jcs if (err)
1404 1e37a5c2 2019-05-12 jcs break;
1405 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1406 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1407 1e37a5c2 2019-05-12 jcs } else {
1408 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1409 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1410 e78dc838 2020-12-04 stsp view->focussed = 1;
1411 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1412 1e37a5c2 2019-05-12 jcs } else {
1413 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1414 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1415 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1416 3c1dfe12 2022-07-08 mark if (!err)
1417 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1418 669b5ffa 2018-10-07 stsp }
1419 1e37a5c2 2019-05-12 jcs if (err)
1420 1e37a5c2 2019-05-12 jcs break;
1421 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1422 1e37a5c2 2019-05-12 jcs }
1423 9b058f45 2022-06-30 mark if (err)
1424 9b058f45 2022-06-30 mark break;
1425 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1426 9b058f45 2022-06-30 mark err = request_log_commits(view);
1427 9b058f45 2022-06-30 mark if (err)
1428 9b058f45 2022-06-30 mark break;
1429 9b058f45 2022-06-30 mark }
1430 9b058f45 2022-06-30 mark if (view->parent)
1431 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1432 9b058f45 2022-06-30 mark if (!err)
1433 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1434 1e37a5c2 2019-05-12 jcs break;
1435 d2366e29 2022-07-07 mark case 'S':
1436 3c1dfe12 2022-07-08 mark view->count = 0;
1437 d2366e29 2022-07-07 mark err = switch_split(view);
1438 3c1dfe12 2022-07-08 mark break;
1439 3c1dfe12 2022-07-08 mark case '-':
1440 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1441 d2366e29 2022-07-07 mark break;
1442 3c1dfe12 2022-07-08 mark case '+':
1443 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1444 3c1dfe12 2022-07-08 mark break;
1445 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1446 60493ae3 2019-06-20 stsp break;
1447 60493ae3 2019-06-20 stsp case '/':
1448 640cd7ff 2022-06-22 mark view->count = 0;
1449 60493ae3 2019-06-20 stsp if (view->search_start)
1450 2b49a8ae 2019-06-22 stsp view_search_start(view);
1451 60493ae3 2019-06-20 stsp else
1452 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1453 1e37a5c2 2019-05-12 jcs break;
1454 b1bf1435 2019-06-21 stsp case 'N':
1455 60493ae3 2019-06-20 stsp case 'n':
1456 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1457 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1458 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1459 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1460 60493ae3 2019-06-20 stsp view->search_next(view);
1461 60493ae3 2019-06-20 stsp } else
1462 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1463 917d79a7 2022-07-01 stsp break;
1464 917d79a7 2022-07-01 stsp case 'A':
1465 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1466 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1467 917d79a7 2022-07-01 stsp else
1468 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1469 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1470 917d79a7 2022-07-01 stsp if (v->reset) {
1471 917d79a7 2022-07-01 stsp err = v->reset(v);
1472 917d79a7 2022-07-01 stsp if (err)
1473 917d79a7 2022-07-01 stsp return err;
1474 917d79a7 2022-07-01 stsp }
1475 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1476 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1477 917d79a7 2022-07-01 stsp if (err)
1478 917d79a7 2022-07-01 stsp return err;
1479 917d79a7 2022-07-01 stsp }
1480 917d79a7 2022-07-01 stsp }
1481 60493ae3 2019-06-20 stsp break;
1482 1e37a5c2 2019-05-12 jcs default:
1483 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1484 1e37a5c2 2019-05-12 jcs break;
1485 e5a0f69f 2018-08-18 stsp }
1486 e5a0f69f 2018-08-18 stsp
1487 e5a0f69f 2018-08-18 stsp return err;
1488 e5a0f69f 2018-08-18 stsp }
1489 e5a0f69f 2018-08-18 stsp
1490 336075a4 2022-06-25 op static int
1491 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1492 a3404814 2018-09-02 stsp {
1493 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1494 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1495 669b5ffa 2018-10-07 stsp return 0;
1496 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1497 669b5ffa 2018-10-07 stsp return 0;
1498 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1499 a3404814 2018-09-02 stsp return 0;
1500 a3404814 2018-09-02 stsp
1501 669b5ffa 2018-10-07 stsp return view->focussed;
1502 a3404814 2018-09-02 stsp }
1503 a3404814 2018-09-02 stsp
1504 bcbd79e2 2018-08-19 stsp static const struct got_error *
1505 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1506 e5a0f69f 2018-08-18 stsp {
1507 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1508 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1509 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1510 d2366e29 2022-07-07 mark char *mode;
1511 fd823528 2018-10-22 stsp int fast_refresh = 10;
1512 1a76625f 2018-10-22 stsp int done = 0, errcode;
1513 e5a0f69f 2018-08-18 stsp
1514 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1515 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1516 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1517 d2366e29 2022-07-07 mark else
1518 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1519 d2366e29 2022-07-07 mark
1520 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1521 1a76625f 2018-10-22 stsp if (errcode)
1522 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1523 1a76625f 2018-10-22 stsp
1524 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1525 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1526 e5a0f69f 2018-08-18 stsp
1527 1004088d 2018-09-29 stsp view->focussed = 1;
1528 878940b7 2018-09-29 stsp err = view->show(view);
1529 0cf4efb1 2018-09-29 stsp if (err)
1530 0cf4efb1 2018-09-29 stsp return err;
1531 0cf4efb1 2018-09-29 stsp update_panels();
1532 0cf4efb1 2018-09-29 stsp doupdate();
1533 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1534 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1535 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1536 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1537 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1538 fd823528 2018-10-22 stsp
1539 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1540 e5a0f69f 2018-08-18 stsp if (err)
1541 e5a0f69f 2018-08-18 stsp break;
1542 9970f7fc 2020-12-03 stsp if (view->dying) {
1543 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1544 669b5ffa 2018-10-07 stsp
1545 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1546 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1547 9970f7fc 2020-12-03 stsp entry);
1548 e78dc838 2020-12-04 stsp else if (view->parent)
1549 669b5ffa 2018-10-07 stsp prev = view->parent;
1550 669b5ffa 2018-10-07 stsp
1551 e78dc838 2020-12-04 stsp if (view->parent) {
1552 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1553 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1554 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1555 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1556 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1557 0dbbbe90 2022-06-17 op if (err)
1558 0dbbbe90 2022-06-17 op break;
1559 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1560 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1561 e78dc838 2020-12-04 stsp } else
1562 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1563 669b5ffa 2018-10-07 stsp
1564 9970f7fc 2020-12-03 stsp err = view_close(view);
1565 fb59748f 2020-12-05 stsp if (err)
1566 e5a0f69f 2018-08-18 stsp goto done;
1567 669b5ffa 2018-10-07 stsp
1568 e78dc838 2020-12-04 stsp view = NULL;
1569 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1570 e78dc838 2020-12-04 stsp if (v->focussed)
1571 e78dc838 2020-12-04 stsp break;
1572 0cf4efb1 2018-09-29 stsp }
1573 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1574 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1575 e78dc838 2020-12-04 stsp if (prev)
1576 e78dc838 2020-12-04 stsp view = prev;
1577 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1578 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1579 e78dc838 2020-12-04 stsp tog_view_list_head);
1580 e78dc838 2020-12-04 stsp }
1581 e78dc838 2020-12-04 stsp if (view) {
1582 e78dc838 2020-12-04 stsp if (view->focus_child) {
1583 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1584 e78dc838 2020-12-04 stsp view = view->child;
1585 e78dc838 2020-12-04 stsp } else
1586 e78dc838 2020-12-04 stsp view->focussed = 1;
1587 e78dc838 2020-12-04 stsp }
1588 e78dc838 2020-12-04 stsp }
1589 e5a0f69f 2018-08-18 stsp }
1590 bcbd79e2 2018-08-19 stsp if (new_view) {
1591 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1592 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1593 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1594 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1595 86c66b02 2018-10-18 stsp continue;
1596 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1597 86c66b02 2018-10-18 stsp err = view_close(v);
1598 86c66b02 2018-10-18 stsp if (err)
1599 86c66b02 2018-10-18 stsp goto done;
1600 86c66b02 2018-10-18 stsp break;
1601 86c66b02 2018-10-18 stsp }
1602 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1603 fed7eaa8 2018-10-24 stsp view = new_view;
1604 0ae84acc 2022-06-15 tracey }
1605 669b5ffa 2018-10-07 stsp if (view) {
1606 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1607 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1608 e78dc838 2020-12-04 stsp view = view->child;
1609 e78dc838 2020-12-04 stsp } else {
1610 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1611 e78dc838 2020-12-04 stsp view = view->parent;
1612 1a76625f 2018-10-22 stsp }
1613 e78dc838 2020-12-04 stsp show_panel(view->panel);
1614 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1615 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1616 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1617 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1618 669b5ffa 2018-10-07 stsp if (err)
1619 1a76625f 2018-10-22 stsp goto done;
1620 669b5ffa 2018-10-07 stsp }
1621 669b5ffa 2018-10-07 stsp err = view->show(view);
1622 0cf4efb1 2018-09-29 stsp if (err)
1623 1a76625f 2018-10-22 stsp goto done;
1624 669b5ffa 2018-10-07 stsp if (view->child) {
1625 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1626 669b5ffa 2018-10-07 stsp if (err)
1627 1a76625f 2018-10-22 stsp goto done;
1628 669b5ffa 2018-10-07 stsp }
1629 1a76625f 2018-10-22 stsp update_panels();
1630 1a76625f 2018-10-22 stsp doupdate();
1631 0cf4efb1 2018-09-29 stsp }
1632 e5a0f69f 2018-08-18 stsp }
1633 e5a0f69f 2018-08-18 stsp done:
1634 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1635 5629093a 2022-07-11 stsp const struct got_error *close_err;
1636 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1637 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1638 5629093a 2022-07-11 stsp close_err = view_close(view);
1639 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1640 5629093a 2022-07-11 stsp err = close_err;
1641 e5a0f69f 2018-08-18 stsp }
1642 1a76625f 2018-10-22 stsp
1643 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1644 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1645 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1646 1a76625f 2018-10-22 stsp
1647 e5a0f69f 2018-08-18 stsp return err;
1648 ea5e7bb5 2018-08-01 stsp }
1649 ea5e7bb5 2018-08-01 stsp
1650 4ed7e80c 2018-05-20 stsp __dead static void
1651 9f7d7167 2018-04-29 stsp usage_log(void)
1652 9f7d7167 2018-04-29 stsp {
1653 80ddbec8 2018-04-29 stsp endwin();
1654 c70c5802 2018-08-01 stsp fprintf(stderr,
1655 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1656 9f7d7167 2018-04-29 stsp getprogname());
1657 9f7d7167 2018-04-29 stsp exit(1);
1658 80ddbec8 2018-04-29 stsp }
1659 80ddbec8 2018-04-29 stsp
1660 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1661 80ddbec8 2018-04-29 stsp static const struct got_error *
1662 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1663 963b370f 2018-05-20 stsp {
1664 00dfcb92 2018-06-11 stsp char *vis = NULL;
1665 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1666 963b370f 2018-05-20 stsp
1667 963b370f 2018-05-20 stsp *ws = NULL;
1668 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1669 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1670 00dfcb92 2018-06-11 stsp int vislen;
1671 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1672 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1673 00dfcb92 2018-06-11 stsp
1674 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1675 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1676 00dfcb92 2018-06-11 stsp if (err)
1677 00dfcb92 2018-06-11 stsp return err;
1678 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1679 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1680 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1681 a7f50699 2018-06-11 stsp goto done;
1682 a7f50699 2018-06-11 stsp }
1683 00dfcb92 2018-06-11 stsp }
1684 963b370f 2018-05-20 stsp
1685 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1686 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1687 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1688 a7f50699 2018-06-11 stsp goto done;
1689 a7f50699 2018-06-11 stsp }
1690 963b370f 2018-05-20 stsp
1691 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1693 a7f50699 2018-06-11 stsp done:
1694 00dfcb92 2018-06-11 stsp free(vis);
1695 963b370f 2018-05-20 stsp if (err) {
1696 963b370f 2018-05-20 stsp free(*ws);
1697 963b370f 2018-05-20 stsp *ws = NULL;
1698 963b370f 2018-05-20 stsp *wlen = 0;
1699 963b370f 2018-05-20 stsp }
1700 963b370f 2018-05-20 stsp return err;
1701 145b6838 2022-06-16 stsp }
1702 145b6838 2022-06-16 stsp
1703 145b6838 2022-06-16 stsp static const struct got_error *
1704 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1705 145b6838 2022-06-16 stsp {
1706 145b6838 2022-06-16 stsp char *dst;
1707 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1708 145b6838 2022-06-16 stsp
1709 145b6838 2022-06-16 stsp *ptr = NULL;
1710 145b6838 2022-06-16 stsp n = len = strlen(src);
1711 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1712 145b6838 2022-06-16 stsp if (dst == NULL)
1713 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1714 145b6838 2022-06-16 stsp
1715 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1716 145b6838 2022-06-16 stsp const char c = src[idx];
1717 145b6838 2022-06-16 stsp
1718 145b6838 2022-06-16 stsp if (c == '\t') {
1719 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1720 367ddf28 2022-06-16 mark char *p;
1721 367ddf28 2022-06-16 mark
1722 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1723 6e1c41ad 2022-06-16 mark if (p == NULL) {
1724 6e1c41ad 2022-06-16 mark free(dst);
1725 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1726 6e1c41ad 2022-06-16 mark
1727 6e1c41ad 2022-06-16 mark }
1728 6e1c41ad 2022-06-16 mark dst = p;
1729 145b6838 2022-06-16 stsp n += nb;
1730 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1731 145b6838 2022-06-16 stsp sz += nb;
1732 145b6838 2022-06-16 stsp } else
1733 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1734 145b6838 2022-06-16 stsp ++idx;
1735 145b6838 2022-06-16 stsp }
1736 145b6838 2022-06-16 stsp
1737 145b6838 2022-06-16 stsp dst[sz] = '\0';
1738 145b6838 2022-06-16 stsp *ptr = dst;
1739 145b6838 2022-06-16 stsp return NULL;
1740 963b370f 2018-05-20 stsp }
1741 963b370f 2018-05-20 stsp
1742 4e4a9ac8 2022-06-17 op /*
1743 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1744 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1745 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1746 4e4a9ac8 2022-06-17 op * *rcol.
1747 ccda2f4d 2022-06-16 stsp */
1748 4e4a9ac8 2022-06-17 op static int
1749 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1750 4e4a9ac8 2022-06-17 op {
1751 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1752 ccda2f4d 2022-06-16 stsp
1753 4e4a9ac8 2022-06-17 op if (n == 0) {
1754 4e4a9ac8 2022-06-17 op *rcol = cols;
1755 4e4a9ac8 2022-06-17 op return off;
1756 4e4a9ac8 2022-06-17 op }
1757 ccda2f4d 2022-06-16 stsp
1758 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1759 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1760 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1761 4e4a9ac8 2022-06-17 op else
1762 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1763 ccda2f4d 2022-06-16 stsp
1764 4e4a9ac8 2022-06-17 op if (width == -1) {
1765 4e4a9ac8 2022-06-17 op width = 1;
1766 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1767 ccda2f4d 2022-06-16 stsp }
1768 ccda2f4d 2022-06-16 stsp
1769 4e4a9ac8 2022-06-17 op if (cols + width > n)
1770 4e4a9ac8 2022-06-17 op break;
1771 4e4a9ac8 2022-06-17 op cols += width;
1772 ccda2f4d 2022-06-16 stsp }
1773 ccda2f4d 2022-06-16 stsp
1774 4e4a9ac8 2022-06-17 op *rcol = cols;
1775 4e4a9ac8 2022-06-17 op return i;
1776 ccda2f4d 2022-06-16 stsp }
1777 ccda2f4d 2022-06-16 stsp
1778 ccda2f4d 2022-06-16 stsp /*
1779 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1780 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1781 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1782 ccda2f4d 2022-06-16 stsp */
1783 ccda2f4d 2022-06-16 stsp static const struct got_error *
1784 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1785 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1786 ccda2f4d 2022-06-16 stsp {
1787 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1788 4e4a9ac8 2022-06-17 op int cols;
1789 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1790 145b6838 2022-06-16 stsp char *exstr = NULL;
1791 963b370f 2018-05-20 stsp size_t wlen;
1792 4e4a9ac8 2022-06-17 op int i, scrollx;
1793 963b370f 2018-05-20 stsp
1794 963b370f 2018-05-20 stsp *wlinep = NULL;
1795 b700b5d6 2018-07-10 stsp *widthp = 0;
1796 963b370f 2018-05-20 stsp
1797 145b6838 2022-06-16 stsp if (expand) {
1798 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1799 145b6838 2022-06-16 stsp if (err)
1800 145b6838 2022-06-16 stsp return err;
1801 145b6838 2022-06-16 stsp }
1802 145b6838 2022-06-16 stsp
1803 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1804 145b6838 2022-06-16 stsp free(exstr);
1805 963b370f 2018-05-20 stsp if (err)
1806 963b370f 2018-05-20 stsp return err;
1807 963b370f 2018-05-20 stsp
1808 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1809 ccda2f4d 2022-06-16 stsp
1810 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1811 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1812 3f670bfb 2020-12-10 stsp wlen--;
1813 3f670bfb 2020-12-10 stsp }
1814 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1815 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1816 3f670bfb 2020-12-10 stsp wlen--;
1817 3f670bfb 2020-12-10 stsp }
1818 3f670bfb 2020-12-10 stsp
1819 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1820 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1821 27a741e5 2019-09-11 stsp
1822 b700b5d6 2018-07-10 stsp if (widthp)
1823 b700b5d6 2018-07-10 stsp *widthp = cols;
1824 ccda2f4d 2022-06-16 stsp if (scrollxp)
1825 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1826 963b370f 2018-05-20 stsp if (err)
1827 963b370f 2018-05-20 stsp free(wline);
1828 963b370f 2018-05-20 stsp else
1829 963b370f 2018-05-20 stsp *wlinep = wline;
1830 963b370f 2018-05-20 stsp return err;
1831 963b370f 2018-05-20 stsp }
1832 963b370f 2018-05-20 stsp
1833 8b473291 2019-02-21 stsp static const struct got_error*
1834 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1835 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1836 8b473291 2019-02-21 stsp {
1837 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1838 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1839 8b473291 2019-02-21 stsp char *s;
1840 8b473291 2019-02-21 stsp const char *name;
1841 8b473291 2019-02-21 stsp
1842 8b473291 2019-02-21 stsp *refs_str = NULL;
1843 8b473291 2019-02-21 stsp
1844 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1845 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1846 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1847 52b5abe1 2019-08-13 stsp int cmp;
1848 52b5abe1 2019-08-13 stsp
1849 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1850 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1851 8b473291 2019-02-21 stsp continue;
1852 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1853 8b473291 2019-02-21 stsp name += 5;
1854 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1855 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1856 7143d404 2019-03-12 stsp continue;
1857 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1858 8b473291 2019-02-21 stsp name += 6;
1859 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1860 8b473291 2019-02-21 stsp name += 8;
1861 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1862 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1863 79cc719f 2020-04-24 stsp continue;
1864 79cc719f 2020-04-24 stsp }
1865 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1866 48cae60d 2020-09-22 stsp if (err)
1867 48cae60d 2020-09-22 stsp break;
1868 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1869 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1870 5d844a1e 2019-08-13 stsp if (err) {
1871 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1872 48cae60d 2020-09-22 stsp free(ref_id);
1873 5d844a1e 2019-08-13 stsp break;
1874 48cae60d 2020-09-22 stsp }
1875 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1876 5d844a1e 2019-08-13 stsp err = NULL;
1877 5d844a1e 2019-08-13 stsp tag = NULL;
1878 5d844a1e 2019-08-13 stsp }
1879 52b5abe1 2019-08-13 stsp }
1880 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1881 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1882 48cae60d 2020-09-22 stsp free(ref_id);
1883 52b5abe1 2019-08-13 stsp if (tag)
1884 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1885 52b5abe1 2019-08-13 stsp if (cmp != 0)
1886 52b5abe1 2019-08-13 stsp continue;
1887 8b473291 2019-02-21 stsp s = *refs_str;
1888 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1889 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1890 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1891 8b473291 2019-02-21 stsp free(s);
1892 8b473291 2019-02-21 stsp *refs_str = NULL;
1893 8b473291 2019-02-21 stsp break;
1894 8b473291 2019-02-21 stsp }
1895 8b473291 2019-02-21 stsp free(s);
1896 8b473291 2019-02-21 stsp }
1897 8b473291 2019-02-21 stsp
1898 8b473291 2019-02-21 stsp return err;
1899 8b473291 2019-02-21 stsp }
1900 8b473291 2019-02-21 stsp
1901 963b370f 2018-05-20 stsp static const struct got_error *
1902 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1903 27a741e5 2019-09-11 stsp int col_tab_align)
1904 5813d178 2019-03-09 stsp {
1905 e6b8b890 2020-12-29 naddy char *smallerthan;
1906 5813d178 2019-03-09 stsp
1907 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1908 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1909 5813d178 2019-03-09 stsp author = smallerthan + 1;
1910 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1911 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1912 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1913 5813d178 2019-03-09 stsp }
1914 5813d178 2019-03-09 stsp
1915 5813d178 2019-03-09 stsp static const struct got_error *
1916 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1917 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1918 8fdc79fe 2020-12-01 naddy int author_display_cols)
1919 80ddbec8 2018-04-29 stsp {
1920 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1921 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1922 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1923 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1924 5813d178 2019-03-09 stsp char *author = NULL;
1925 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1926 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1927 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1928 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1929 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1930 ccb26ccd 2018-11-05 stsp struct tm tm;
1931 45d799e2 2018-12-23 stsp time_t committer_time;
1932 11b20872 2019-11-08 stsp struct tog_color *tc;
1933 80ddbec8 2018-04-29 stsp
1934 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1935 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1936 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1937 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1938 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1939 b39d25c7 2018-07-10 stsp
1940 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1941 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1942 b39d25c7 2018-07-10 stsp else
1943 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1944 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1945 11b20872 2019-11-08 stsp if (tc)
1946 11b20872 2019-11-08 stsp wattr_on(view->window,
1947 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1948 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1949 11b20872 2019-11-08 stsp if (tc)
1950 11b20872 2019-11-08 stsp wattr_off(view->window,
1951 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1952 27a741e5 2019-09-11 stsp col = limit;
1953 b39d25c7 2018-07-10 stsp if (col > avail)
1954 b39d25c7 2018-07-10 stsp goto done;
1955 6570a66d 2019-11-08 stsp
1956 6570a66d 2019-11-08 stsp if (avail >= 120) {
1957 6570a66d 2019-11-08 stsp char *id_str;
1958 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1959 6570a66d 2019-11-08 stsp if (err)
1960 6570a66d 2019-11-08 stsp goto done;
1961 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1962 11b20872 2019-11-08 stsp if (tc)
1963 11b20872 2019-11-08 stsp wattr_on(view->window,
1964 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1965 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1966 11b20872 2019-11-08 stsp if (tc)
1967 11b20872 2019-11-08 stsp wattr_off(view->window,
1968 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1969 6570a66d 2019-11-08 stsp free(id_str);
1970 6570a66d 2019-11-08 stsp col += 9;
1971 6570a66d 2019-11-08 stsp if (col > avail)
1972 6570a66d 2019-11-08 stsp goto done;
1973 6570a66d 2019-11-08 stsp }
1974 b39d25c7 2018-07-10 stsp
1975 10aab77f 2022-07-19 op if (s->use_committer)
1976 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
1977 10aab77f 2022-07-19 op else
1978 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
1979 5813d178 2019-03-09 stsp if (author == NULL) {
1980 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1981 80ddbec8 2018-04-29 stsp goto done;
1982 80ddbec8 2018-04-29 stsp }
1983 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1984 bb737323 2018-05-20 stsp if (err)
1985 bb737323 2018-05-20 stsp goto done;
1986 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1987 11b20872 2019-11-08 stsp if (tc)
1988 11b20872 2019-11-08 stsp wattr_on(view->window,
1989 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1990 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1991 11b20872 2019-11-08 stsp if (tc)
1992 11b20872 2019-11-08 stsp wattr_off(view->window,
1993 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1994 bb737323 2018-05-20 stsp col += author_width;
1995 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1996 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1997 bb737323 2018-05-20 stsp col++;
1998 bb737323 2018-05-20 stsp author_width++;
1999 bb737323 2018-05-20 stsp }
2000 9c2eaf34 2018-05-20 stsp if (col > avail)
2001 9c2eaf34 2018-05-20 stsp goto done;
2002 80ddbec8 2018-04-29 stsp
2003 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2004 5943eee2 2019-08-13 stsp if (err)
2005 6d9fbc00 2018-04-29 stsp goto done;
2006 bb737323 2018-05-20 stsp logmsg = logmsg0;
2007 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2008 bb737323 2018-05-20 stsp logmsg++;
2009 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2010 bb737323 2018-05-20 stsp if (newline)
2011 bb737323 2018-05-20 stsp *newline = '\0';
2012 ccda2f4d 2022-06-16 stsp limit = avail - col;
2013 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2014 4d1f6af3 2022-06-17 op limit--; /* for the border */
2015 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2016 ccda2f4d 2022-06-16 stsp limit, col, 1);
2017 29688b02 2022-06-16 stsp if (err)
2018 29688b02 2022-06-16 stsp goto done;
2019 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2020 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2021 27a741e5 2019-09-11 stsp while (col < avail) {
2022 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2023 bb737323 2018-05-20 stsp col++;
2024 881b2d3e 2018-04-30 stsp }
2025 80ddbec8 2018-04-29 stsp done:
2026 80ddbec8 2018-04-29 stsp free(logmsg0);
2027 bb737323 2018-05-20 stsp free(wlogmsg);
2028 5813d178 2019-03-09 stsp free(author);
2029 bb737323 2018-05-20 stsp free(wauthor);
2030 80ddbec8 2018-04-29 stsp free(line);
2031 80ddbec8 2018-04-29 stsp return err;
2032 80ddbec8 2018-04-29 stsp }
2033 26ed57b2 2018-05-19 stsp
2034 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2035 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2036 899d86c2 2018-05-10 stsp struct got_object_id *id)
2037 80ddbec8 2018-04-29 stsp {
2038 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2039 80ddbec8 2018-04-29 stsp
2040 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2041 80ddbec8 2018-04-29 stsp if (entry == NULL)
2042 899d86c2 2018-05-10 stsp return NULL;
2043 99db9666 2018-05-07 stsp
2044 899d86c2 2018-05-10 stsp entry->id = id;
2045 99db9666 2018-05-07 stsp entry->commit = commit;
2046 899d86c2 2018-05-10 stsp return entry;
2047 99db9666 2018-05-07 stsp }
2048 80ddbec8 2018-04-29 stsp
2049 99db9666 2018-05-07 stsp static void
2050 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2051 99db9666 2018-05-07 stsp {
2052 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2053 99db9666 2018-05-07 stsp
2054 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2055 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2056 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2057 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2058 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2059 99db9666 2018-05-07 stsp free(entry);
2060 99db9666 2018-05-07 stsp }
2061 99db9666 2018-05-07 stsp
2062 99db9666 2018-05-07 stsp static void
2063 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2064 99db9666 2018-05-07 stsp {
2065 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2066 99db9666 2018-05-07 stsp pop_commit(commits);
2067 c4972b91 2018-05-07 stsp }
2068 c4972b91 2018-05-07 stsp
2069 c4972b91 2018-05-07 stsp static const struct got_error *
2070 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2071 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2072 13add988 2019-10-15 stsp {
2073 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2074 13add988 2019-10-15 stsp regmatch_t regmatch;
2075 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2076 13add988 2019-10-15 stsp
2077 13add988 2019-10-15 stsp *have_match = 0;
2078 13add988 2019-10-15 stsp
2079 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2080 13add988 2019-10-15 stsp if (err)
2081 13add988 2019-10-15 stsp return err;
2082 13add988 2019-10-15 stsp
2083 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2084 13add988 2019-10-15 stsp if (err)
2085 13add988 2019-10-15 stsp goto done;
2086 13add988 2019-10-15 stsp
2087 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2088 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2089 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2090 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2091 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2092 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2093 13add988 2019-10-15 stsp *have_match = 1;
2094 13add988 2019-10-15 stsp done:
2095 13add988 2019-10-15 stsp free(id_str);
2096 13add988 2019-10-15 stsp free(logmsg);
2097 13add988 2019-10-15 stsp return err;
2098 13add988 2019-10-15 stsp }
2099 13add988 2019-10-15 stsp
2100 13add988 2019-10-15 stsp static const struct got_error *
2101 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2102 c4972b91 2018-05-07 stsp {
2103 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2104 9ba79e04 2018-06-11 stsp
2105 1a76625f 2018-10-22 stsp /*
2106 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2107 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2108 1a76625f 2018-10-22 stsp * while updating the display.
2109 1a76625f 2018-10-22 stsp */
2110 4e0d2870 2020-12-07 naddy do {
2111 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2112 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2113 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2114 1a76625f 2018-10-22 stsp int errcode;
2115 899d86c2 2018-05-10 stsp
2116 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2117 4e0d2870 2020-12-07 naddy NULL, NULL);
2118 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2119 ecb28ae0 2018-07-16 stsp break;
2120 899d86c2 2018-05-10 stsp
2121 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2122 9ba79e04 2018-06-11 stsp if (err)
2123 9ba79e04 2018-06-11 stsp break;
2124 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2125 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2126 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2127 9ba79e04 2018-06-11 stsp break;
2128 9ba79e04 2018-06-11 stsp }
2129 93e45b7c 2018-09-24 stsp
2130 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2131 1a76625f 2018-10-22 stsp if (errcode) {
2132 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2133 13add988 2019-10-15 stsp "pthread_mutex_lock");
2134 1a76625f 2018-10-22 stsp break;
2135 1a76625f 2018-10-22 stsp }
2136 1a76625f 2018-10-22 stsp
2137 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2138 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2139 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2140 1a76625f 2018-10-22 stsp
2141 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2142 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2143 7c1452c1 2020-03-26 stsp int have_match;
2144 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2145 7c1452c1 2020-03-26 stsp if (err)
2146 7c1452c1 2020-03-26 stsp break;
2147 7c1452c1 2020-03-26 stsp if (have_match)
2148 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2149 13add988 2019-10-15 stsp }
2150 13add988 2019-10-15 stsp
2151 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2152 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2153 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2154 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2155 7c1452c1 2020-03-26 stsp if (err)
2156 13add988 2019-10-15 stsp break;
2157 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2158 899d86c2 2018-05-10 stsp
2159 9ba79e04 2018-06-11 stsp return err;
2160 0553a4e3 2018-04-30 stsp }
2161 0553a4e3 2018-04-30 stsp
2162 2b779855 2020-12-05 naddy static void
2163 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2164 2b779855 2020-12-05 naddy {
2165 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2166 2b779855 2020-12-05 naddy int ncommits = 0;
2167 2b779855 2020-12-05 naddy
2168 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2169 2b779855 2020-12-05 naddy while (entry) {
2170 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2171 2b779855 2020-12-05 naddy s->selected_entry = entry;
2172 2b779855 2020-12-05 naddy break;
2173 2b779855 2020-12-05 naddy }
2174 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2175 2b779855 2020-12-05 naddy ncommits++;
2176 2b779855 2020-12-05 naddy }
2177 2b779855 2020-12-05 naddy }
2178 2b779855 2020-12-05 naddy
2179 0553a4e3 2018-04-30 stsp static const struct got_error *
2180 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2181 0553a4e3 2018-04-30 stsp {
2182 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2183 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2184 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2185 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2186 60493ae3 2019-06-20 stsp int width;
2187 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2188 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2189 8b473291 2019-02-21 stsp char *refs_str = NULL;
2190 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2191 11b20872 2019-11-08 stsp struct tog_color *tc;
2192 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2193 0553a4e3 2018-04-30 stsp
2194 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2195 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2196 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2197 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2198 1a76625f 2018-10-22 stsp if (err)
2199 ecb28ae0 2018-07-16 stsp return err;
2200 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2201 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2202 d2075bf3 2020-12-25 stsp if (refs) {
2203 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2204 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2205 d2075bf3 2020-12-25 stsp if (err)
2206 d2075bf3 2020-12-25 stsp goto done;
2207 d2075bf3 2020-12-25 stsp }
2208 867c6645 2018-07-10 stsp }
2209 359bfafd 2019-02-22 stsp
2210 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2211 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2212 1a76625f 2018-10-22 stsp
2213 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2214 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2217 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2218 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2219 8f4ed634 2020-03-26 stsp goto done;
2220 8f4ed634 2020-03-26 stsp }
2221 8f4ed634 2020-03-26 stsp } else {
2222 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2223 f9686aa5 2020-03-27 stsp
2224 f9686aa5 2020-03-27 stsp if (view->searching) {
2225 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2226 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2227 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2228 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2229 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2230 f9686aa5 2020-03-27 stsp search_str = "searching...";
2231 f9686aa5 2020-03-27 stsp }
2232 f9686aa5 2020-03-27 stsp
2233 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2234 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2235 f9686aa5 2020-03-27 stsp search_str ? search_str :
2236 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2237 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2238 8f4ed634 2020-03-26 stsp goto done;
2239 8f4ed634 2020-03-26 stsp }
2240 8b473291 2019-02-21 stsp }
2241 1a76625f 2018-10-22 stsp
2242 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2243 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2244 87411fa9 2022-06-16 stsp "........................................",
2245 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2247 1a76625f 2018-10-22 stsp header = NULL;
2248 1a76625f 2018-10-22 stsp goto done;
2249 1a76625f 2018-10-22 stsp }
2250 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2251 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2252 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2253 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2254 1a76625f 2018-10-22 stsp header = NULL;
2255 1a76625f 2018-10-22 stsp goto done;
2256 ecb28ae0 2018-07-16 stsp }
2257 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2258 1a76625f 2018-10-22 stsp if (err)
2259 1a76625f 2018-10-22 stsp goto done;
2260 867c6645 2018-07-10 stsp
2261 2814baeb 2018-08-01 stsp werase(view->window);
2262 867c6645 2018-07-10 stsp
2263 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2264 a3404814 2018-09-02 stsp wstandout(view->window);
2265 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2266 11b20872 2019-11-08 stsp if (tc)
2267 11b20872 2019-11-08 stsp wattr_on(view->window,
2268 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2269 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2270 11b20872 2019-11-08 stsp if (tc)
2271 11b20872 2019-11-08 stsp wattr_off(view->window,
2272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2273 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2274 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2275 1a76625f 2018-10-22 stsp width++;
2276 1a76625f 2018-10-22 stsp }
2277 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2278 a3404814 2018-09-02 stsp wstandend(view->window);
2279 ecb28ae0 2018-07-16 stsp free(wline);
2280 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2281 1a76625f 2018-10-22 stsp goto done;
2282 0553a4e3 2018-04-30 stsp
2283 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2284 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2285 5813d178 2019-03-09 stsp ncommits = 0;
2286 145b6838 2022-06-16 stsp view->maxx = 0;
2287 5813d178 2019-03-09 stsp while (entry) {
2288 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2289 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2290 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2291 5813d178 2019-03-09 stsp int width;
2292 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2293 5813d178 2019-03-09 stsp break;
2294 10aab77f 2022-07-19 op if (s->use_committer)
2295 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2296 10aab77f 2022-07-19 op else
2297 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2298 5813d178 2019-03-09 stsp if (author == NULL) {
2299 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2300 5813d178 2019-03-09 stsp goto done;
2301 5813d178 2019-03-09 stsp }
2302 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2303 27a741e5 2019-09-11 stsp date_display_cols);
2304 5813d178 2019-03-09 stsp if (author_cols < width)
2305 5813d178 2019-03-09 stsp author_cols = width;
2306 5813d178 2019-03-09 stsp free(wauthor);
2307 5813d178 2019-03-09 stsp free(author);
2308 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2309 145b6838 2022-06-16 stsp if (err)
2310 145b6838 2022-06-16 stsp goto done;
2311 145b6838 2022-06-16 stsp msg = msg0;
2312 145b6838 2022-06-16 stsp while (*msg == '\n')
2313 145b6838 2022-06-16 stsp ++msg;
2314 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2315 29688b02 2022-06-16 stsp *eol = '\0';
2316 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2317 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2318 29688b02 2022-06-16 stsp if (err)
2319 29688b02 2022-06-16 stsp goto done;
2320 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2321 145b6838 2022-06-16 stsp free(msg0);
2322 29688b02 2022-06-16 stsp free(wmsg);
2323 7ca04879 2019-10-19 stsp ncommits++;
2324 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2325 5813d178 2019-03-09 stsp }
2326 5813d178 2019-03-09 stsp
2327 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2328 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2329 867c6645 2018-07-10 stsp ncommits = 0;
2330 899d86c2 2018-05-10 stsp while (entry) {
2331 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2332 899d86c2 2018-05-10 stsp break;
2333 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2334 2814baeb 2018-08-01 stsp wstandout(view->window);
2335 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2336 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2337 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2338 2814baeb 2018-08-01 stsp wstandend(view->window);
2339 0553a4e3 2018-04-30 stsp if (err)
2340 60493ae3 2019-06-20 stsp goto done;
2341 0553a4e3 2018-04-30 stsp ncommits++;
2342 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2343 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2344 80ddbec8 2018-04-29 stsp }
2345 80ddbec8 2018-04-29 stsp
2346 9b058f45 2022-06-30 mark view_border(view);
2347 1a76625f 2018-10-22 stsp done:
2348 1a76625f 2018-10-22 stsp free(id_str);
2349 8b473291 2019-02-21 stsp free(refs_str);
2350 1a76625f 2018-10-22 stsp free(ncommits_str);
2351 1a76625f 2018-10-22 stsp free(header);
2352 80ddbec8 2018-04-29 stsp return err;
2353 9f7d7167 2018-04-29 stsp }
2354 07b55e75 2018-05-10 stsp
2355 07b55e75 2018-05-10 stsp static void
2356 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2357 07b55e75 2018-05-10 stsp {
2358 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2359 07b55e75 2018-05-10 stsp int nscrolled = 0;
2360 07b55e75 2018-05-10 stsp
2361 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2362 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2363 07b55e75 2018-05-10 stsp return;
2364 9f7d7167 2018-04-29 stsp
2365 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2366 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2367 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2368 07b55e75 2018-05-10 stsp if (entry) {
2369 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2370 07b55e75 2018-05-10 stsp nscrolled++;
2371 07b55e75 2018-05-10 stsp }
2372 07b55e75 2018-05-10 stsp }
2373 aa075928 2018-05-10 stsp }
2374 aa075928 2018-05-10 stsp
2375 aa075928 2018-05-10 stsp static const struct got_error *
2376 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2377 aa075928 2018-05-10 stsp {
2378 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2379 5e224a3e 2019-02-22 stsp int errcode;
2380 8a42fca8 2019-02-22 stsp
2381 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2382 aa075928 2018-05-10 stsp
2383 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2384 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2385 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2386 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2387 7aafa0d1 2019-02-22 stsp if (errcode)
2388 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2389 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2390 7c1452c1 2020-03-26 stsp
2391 7c1452c1 2020-03-26 stsp /*
2392 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2393 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2394 7c1452c1 2020-03-26 stsp */
2395 7c1452c1 2020-03-26 stsp if (!wait)
2396 7c1452c1 2020-03-26 stsp break;
2397 7c1452c1 2020-03-26 stsp
2398 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2399 ffe38506 2020-12-01 naddy show_log_view(view);
2400 7c1452c1 2020-03-26 stsp update_panels();
2401 7c1452c1 2020-03-26 stsp doupdate();
2402 7c1452c1 2020-03-26 stsp
2403 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2404 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2405 82954512 2020-02-03 stsp if (errcode)
2406 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2407 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2408 82954512 2020-02-03 stsp
2409 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2410 ffe38506 2020-12-01 naddy show_log_view(view);
2411 7c1452c1 2020-03-26 stsp update_panels();
2412 7c1452c1 2020-03-26 stsp doupdate();
2413 5e224a3e 2019-02-22 stsp }
2414 5e224a3e 2019-02-22 stsp
2415 5e224a3e 2019-02-22 stsp return NULL;
2416 5e224a3e 2019-02-22 stsp }
2417 5e224a3e 2019-02-22 stsp
2418 5e224a3e 2019-02-22 stsp static const struct got_error *
2419 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2420 9b058f45 2022-06-30 mark {
2421 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2422 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2423 2525dccb 2022-07-13 mark
2424 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2425 2525dccb 2022-07-13 mark return NULL;
2426 9b058f45 2022-06-30 mark
2427 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2428 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2429 9b058f45 2022-06-30 mark view->nscrolled = 0;
2430 9b058f45 2022-06-30 mark
2431 9b058f45 2022-06-30 mark return err;
2432 9b058f45 2022-06-30 mark }
2433 9b058f45 2022-06-30 mark
2434 9b058f45 2022-06-30 mark static const struct got_error *
2435 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2436 5e224a3e 2019-02-22 stsp {
2437 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2438 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2439 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2440 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2441 5e224a3e 2019-02-22 stsp
2442 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2443 5e224a3e 2019-02-22 stsp return NULL;
2444 5e224a3e 2019-02-22 stsp
2445 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2446 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2447 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2448 08ebd0a9 2019-02-22 stsp /*
2449 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2450 08ebd0a9 2019-02-22 stsp */
2451 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2452 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2453 5e224a3e 2019-02-22 stsp if (err)
2454 5e224a3e 2019-02-22 stsp return err;
2455 7aafa0d1 2019-02-22 stsp }
2456 b295e71b 2019-02-22 stsp
2457 7aafa0d1 2019-02-22 stsp do {
2458 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2459 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2460 88048b54 2019-02-21 stsp break;
2461 88048b54 2019-02-21 stsp
2462 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2463 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2464 aa075928 2018-05-10 stsp
2465 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2466 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2467 dd0a52c1 2018-05-20 stsp break;
2468 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2469 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2470 aa075928 2018-05-10 stsp
2471 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2472 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2473 9b058f45 2022-06-30 mark else
2474 9b058f45 2022-06-30 mark view->nscrolled = 0;
2475 9b058f45 2022-06-30 mark
2476 dd0a52c1 2018-05-20 stsp return err;
2477 07b55e75 2018-05-10 stsp }
2478 4a7f7875 2018-05-10 stsp
2479 cd0acaa7 2018-05-20 stsp static const struct got_error *
2480 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2481 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2482 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2483 cd0acaa7 2018-05-20 stsp {
2484 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2485 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2486 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2487 cd0acaa7 2018-05-20 stsp
2488 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2489 15a94983 2018-12-23 stsp if (diff_view == NULL)
2490 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2491 ea5e7bb5 2018-08-01 stsp
2492 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2493 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2494 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2495 e5a0f69f 2018-08-18 stsp if (err == NULL)
2496 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2497 cd0acaa7 2018-05-20 stsp return err;
2498 4a7f7875 2018-05-10 stsp }
2499 4a7f7875 2018-05-10 stsp
2500 80ddbec8 2018-04-29 stsp static const struct got_error *
2501 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2502 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2503 9343a5fb 2018-06-23 stsp {
2504 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2505 941e9f74 2019-05-21 stsp
2506 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2507 941e9f74 2019-05-21 stsp if (parent == NULL)
2508 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2509 941e9f74 2019-05-21 stsp
2510 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2511 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2512 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2513 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2514 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2515 941e9f74 2019-05-21 stsp s->tree = subtree;
2516 941e9f74 2019-05-21 stsp s->selected = 0;
2517 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2518 941e9f74 2019-05-21 stsp return NULL;
2519 941e9f74 2019-05-21 stsp }
2520 941e9f74 2019-05-21 stsp
2521 941e9f74 2019-05-21 stsp static const struct got_error *
2522 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2523 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2524 941e9f74 2019-05-21 stsp {
2525 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2526 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2527 941e9f74 2019-05-21 stsp const char *p;
2528 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2529 9343a5fb 2018-06-23 stsp
2530 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2531 941e9f74 2019-05-21 stsp p = path;
2532 941e9f74 2019-05-21 stsp while (*p) {
2533 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2534 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2535 56e0773d 2019-11-28 stsp char *te_name;
2536 33cbf02b 2020-01-12 stsp
2537 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2538 33cbf02b 2020-01-12 stsp p++;
2539 941e9f74 2019-05-21 stsp
2540 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2541 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2542 941e9f74 2019-05-21 stsp if (slash == NULL)
2543 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2544 33cbf02b 2020-01-12 stsp else
2545 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2546 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2547 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2548 56e0773d 2019-11-28 stsp break;
2549 941e9f74 2019-05-21 stsp }
2550 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2551 56e0773d 2019-11-28 stsp if (te == NULL) {
2552 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2553 56e0773d 2019-11-28 stsp free(te_name);
2554 941e9f74 2019-05-21 stsp break;
2555 941e9f74 2019-05-21 stsp }
2556 56e0773d 2019-11-28 stsp free(te_name);
2557 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2558 941e9f74 2019-05-21 stsp
2559 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2560 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2561 b03c880f 2019-05-21 stsp
2562 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2563 941e9f74 2019-05-21 stsp if (slash)
2564 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2565 941e9f74 2019-05-21 stsp else
2566 941e9f74 2019-05-21 stsp subpath = strdup(path);
2567 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2568 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2569 941e9f74 2019-05-21 stsp break;
2570 941e9f74 2019-05-21 stsp }
2571 941e9f74 2019-05-21 stsp
2572 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2573 941e9f74 2019-05-21 stsp subpath);
2574 941e9f74 2019-05-21 stsp if (err)
2575 941e9f74 2019-05-21 stsp break;
2576 941e9f74 2019-05-21 stsp
2577 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2578 941e9f74 2019-05-21 stsp free(tree_id);
2579 941e9f74 2019-05-21 stsp if (err)
2580 941e9f74 2019-05-21 stsp break;
2581 941e9f74 2019-05-21 stsp
2582 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2583 941e9f74 2019-05-21 stsp if (err) {
2584 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2585 941e9f74 2019-05-21 stsp break;
2586 941e9f74 2019-05-21 stsp }
2587 941e9f74 2019-05-21 stsp if (slash == NULL)
2588 941e9f74 2019-05-21 stsp break;
2589 941e9f74 2019-05-21 stsp free(subpath);
2590 941e9f74 2019-05-21 stsp subpath = NULL;
2591 941e9f74 2019-05-21 stsp p = slash;
2592 941e9f74 2019-05-21 stsp }
2593 941e9f74 2019-05-21 stsp
2594 941e9f74 2019-05-21 stsp free(subpath);
2595 1a76625f 2018-10-22 stsp return err;
2596 61266923 2020-01-14 stsp }
2597 61266923 2020-01-14 stsp
2598 61266923 2020-01-14 stsp static const struct got_error *
2599 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2600 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2601 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2602 55cccc34 2020-02-20 stsp {
2603 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2604 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2605 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2606 55cccc34 2020-02-20 stsp
2607 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2608 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2609 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2610 55cccc34 2020-02-20 stsp
2611 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2612 bc573f3b 2021-07-10 stsp if (err)
2613 55cccc34 2020-02-20 stsp return err;
2614 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2615 55cccc34 2020-02-20 stsp
2616 55cccc34 2020-02-20 stsp *new_view = tree_view;
2617 55cccc34 2020-02-20 stsp
2618 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2619 55cccc34 2020-02-20 stsp return NULL;
2620 55cccc34 2020-02-20 stsp
2621 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2622 55cccc34 2020-02-20 stsp }
2623 55cccc34 2020-02-20 stsp
2624 55cccc34 2020-02-20 stsp static const struct got_error *
2625 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2626 61266923 2020-01-14 stsp {
2627 61266923 2020-01-14 stsp sigset_t sigset;
2628 61266923 2020-01-14 stsp int errcode;
2629 61266923 2020-01-14 stsp
2630 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2631 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2632 61266923 2020-01-14 stsp
2633 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2634 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2635 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2636 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2637 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2638 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2639 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2640 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2641 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2642 61266923 2020-01-14 stsp
2643 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2644 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2645 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2646 61266923 2020-01-14 stsp
2647 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2648 61266923 2020-01-14 stsp if (errcode)
2649 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2650 61266923 2020-01-14 stsp
2651 61266923 2020-01-14 stsp return NULL;
2652 1a76625f 2018-10-22 stsp }
2653 1a76625f 2018-10-22 stsp
2654 1a76625f 2018-10-22 stsp static void *
2655 1a76625f 2018-10-22 stsp log_thread(void *arg)
2656 1a76625f 2018-10-22 stsp {
2657 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2658 1a76625f 2018-10-22 stsp int errcode = 0;
2659 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2660 1a76625f 2018-10-22 stsp int done = 0;
2661 61266923 2020-01-14 stsp
2662 5629093a 2022-07-11 stsp /*
2663 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2664 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2665 5629093a 2022-07-11 stsp */
2666 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2667 5629093a 2022-07-11 stsp if (errcode) {
2668 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2669 61266923 2020-01-14 stsp return (void *)err;
2670 5629093a 2022-07-11 stsp }
2671 1a76625f 2018-10-22 stsp
2672 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2673 5629093a 2022-07-11 stsp if (err) {
2674 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2675 5629093a 2022-07-11 stsp goto done;
2676 5629093a 2022-07-11 stsp }
2677 5629093a 2022-07-11 stsp
2678 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2679 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2680 5629093a 2022-07-11 stsp if (errcode) {
2681 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2682 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2683 5629093a 2022-07-11 stsp goto done;
2684 5629093a 2022-07-11 stsp }
2685 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2686 1a76625f 2018-10-22 stsp if (err) {
2687 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2688 5629093a 2022-07-11 stsp goto done;
2689 1a76625f 2018-10-22 stsp err = NULL;
2690 1a76625f 2018-10-22 stsp done = 1;
2691 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2692 1a76625f 2018-10-22 stsp a->commits_needed--;
2693 1a76625f 2018-10-22 stsp
2694 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2695 3abe8080 2019-04-10 stsp if (errcode) {
2696 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2697 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2698 5629093a 2022-07-11 stsp goto done;
2699 3abe8080 2019-04-10 stsp } else if (*a->quit)
2700 1a76625f 2018-10-22 stsp done = 1;
2701 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2702 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2703 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2704 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2705 1a76625f 2018-10-22 stsp }
2706 1a76625f 2018-10-22 stsp
2707 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2708 7c1452c1 2020-03-26 stsp if (errcode) {
2709 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2710 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2711 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2712 5629093a 2022-07-11 stsp goto done;
2713 7c1452c1 2020-03-26 stsp }
2714 7c1452c1 2020-03-26 stsp
2715 1a76625f 2018-10-22 stsp if (done)
2716 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2717 7c1452c1 2020-03-26 stsp else {
2718 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2719 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2720 7c1452c1 2020-03-26 stsp &tog_mutex);
2721 5629093a 2022-07-11 stsp if (errcode) {
2722 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2723 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2724 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2725 5629093a 2022-07-11 stsp goto done;
2726 5629093a 2022-07-11 stsp }
2727 21355643 2020-12-06 stsp if (*a->quit)
2728 21355643 2020-12-06 stsp done = 1;
2729 7c1452c1 2020-03-26 stsp }
2730 1a76625f 2018-10-22 stsp }
2731 1a76625f 2018-10-22 stsp }
2732 3abe8080 2019-04-10 stsp a->log_complete = 1;
2733 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2734 5629093a 2022-07-11 stsp if (errcode)
2735 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2736 5629093a 2022-07-11 stsp done:
2737 5629093a 2022-07-11 stsp if (err) {
2738 5629093a 2022-07-11 stsp tog_thread_error = 1;
2739 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2740 5629093a 2022-07-11 stsp }
2741 1a76625f 2018-10-22 stsp return (void *)err;
2742 1a76625f 2018-10-22 stsp }
2743 1a76625f 2018-10-22 stsp
2744 1a76625f 2018-10-22 stsp static const struct got_error *
2745 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2746 1a76625f 2018-10-22 stsp {
2747 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2748 1a76625f 2018-10-22 stsp int errcode;
2749 1a76625f 2018-10-22 stsp
2750 1a76625f 2018-10-22 stsp if (s->thread) {
2751 1a76625f 2018-10-22 stsp s->quit = 1;
2752 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2753 1a76625f 2018-10-22 stsp if (errcode)
2754 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2755 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2756 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2757 1a76625f 2018-10-22 stsp if (errcode)
2758 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2759 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2760 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2761 1a76625f 2018-10-22 stsp if (errcode)
2762 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2763 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2764 1a76625f 2018-10-22 stsp if (errcode)
2765 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2766 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2767 1a76625f 2018-10-22 stsp s->thread = NULL;
2768 1a76625f 2018-10-22 stsp }
2769 1a76625f 2018-10-22 stsp
2770 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2771 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2772 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2773 74467cc8 2022-06-15 stsp }
2774 74467cc8 2022-06-15 stsp
2775 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2776 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2777 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2778 74467cc8 2022-06-15 stsp if (err == NULL)
2779 74467cc8 2022-06-15 stsp err = pack_err;
2780 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2781 1a76625f 2018-10-22 stsp }
2782 1a76625f 2018-10-22 stsp
2783 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2784 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2785 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2786 1a76625f 2018-10-22 stsp }
2787 1a76625f 2018-10-22 stsp
2788 5629093a 2022-07-11 stsp return err ? err : thread_err;
2789 9343a5fb 2018-06-23 stsp }
2790 9343a5fb 2018-06-23 stsp
2791 9343a5fb 2018-06-23 stsp static const struct got_error *
2792 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2793 1a76625f 2018-10-22 stsp {
2794 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2795 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2796 276b94a1 2020-11-13 naddy int errcode;
2797 1a76625f 2018-10-22 stsp
2798 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2799 276b94a1 2020-11-13 naddy
2800 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2801 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2802 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2803 276b94a1 2020-11-13 naddy
2804 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2805 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2806 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2807 276b94a1 2020-11-13 naddy
2808 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2809 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2810 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2811 1a76625f 2018-10-22 stsp free(s->start_id);
2812 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2813 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2814 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2815 1a76625f 2018-10-22 stsp return err;
2816 1a76625f 2018-10-22 stsp }
2817 1a76625f 2018-10-22 stsp
2818 1a76625f 2018-10-22 stsp static const struct got_error *
2819 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2820 60493ae3 2019-06-20 stsp {
2821 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2822 60493ae3 2019-06-20 stsp
2823 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2824 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2825 60493ae3 2019-06-20 stsp return NULL;
2826 60493ae3 2019-06-20 stsp }
2827 60493ae3 2019-06-20 stsp
2828 60493ae3 2019-06-20 stsp static const struct got_error *
2829 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2830 60493ae3 2019-06-20 stsp {
2831 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2832 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2833 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2834 60493ae3 2019-06-20 stsp
2835 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2836 f9686aa5 2020-03-27 stsp show_log_view(view);
2837 f9686aa5 2020-03-27 stsp update_panels();
2838 f9686aa5 2020-03-27 stsp doupdate();
2839 f9686aa5 2020-03-27 stsp
2840 96e2b566 2019-07-08 stsp if (s->search_entry) {
2841 13add988 2019-10-15 stsp int errcode, ch;
2842 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2843 13add988 2019-10-15 stsp if (errcode)
2844 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2845 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2846 13add988 2019-10-15 stsp ch = wgetch(view->window);
2847 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2848 13add988 2019-10-15 stsp if (errcode)
2849 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2850 13add988 2019-10-15 stsp "pthread_mutex_lock");
2851 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2852 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2853 678cbce5 2019-07-28 stsp return NULL;
2854 678cbce5 2019-07-28 stsp }
2855 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2856 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2857 96e2b566 2019-07-08 stsp else
2858 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2859 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2860 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2861 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2862 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2863 364ac6fd 2022-06-18 stsp
2864 364ac6fd 2022-06-18 stsp /*
2865 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2866 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2867 f704b35c 2022-06-18 stsp * might have changed.
2868 364ac6fd 2022-06-18 stsp */
2869 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2870 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2871 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2872 364ac6fd 2022-06-18 stsp else
2873 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2874 4bfe9f0a 2022-06-18 stsp } else {
2875 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2876 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2877 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2878 364ac6fd 2022-06-18 stsp else
2879 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2880 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2881 4bfe9f0a 2022-06-18 stsp }
2882 20be8d96 2019-06-21 stsp } else {
2883 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2884 20be8d96 2019-06-21 stsp }
2885 60493ae3 2019-06-20 stsp
2886 60493ae3 2019-06-20 stsp while (1) {
2887 13add988 2019-10-15 stsp int have_match = 0;
2888 13add988 2019-10-15 stsp
2889 60493ae3 2019-06-20 stsp if (entry == NULL) {
2890 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2891 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2892 f9967bca 2020-03-27 stsp view->search_next_done =
2893 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2894 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2895 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2896 f801134a 2019-06-25 stsp return NULL;
2897 60493ae3 2019-06-20 stsp }
2898 96e2b566 2019-07-08 stsp /*
2899 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2900 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2901 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2902 96e2b566 2019-07-08 stsp */
2903 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2904 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2905 60493ae3 2019-06-20 stsp }
2906 60493ae3 2019-06-20 stsp
2907 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2908 13add988 2019-10-15 stsp &view->regex);
2909 5943eee2 2019-08-13 stsp if (err)
2910 13add988 2019-10-15 stsp break;
2911 13add988 2019-10-15 stsp if (have_match) {
2912 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2913 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2914 60493ae3 2019-06-20 stsp break;
2915 60493ae3 2019-06-20 stsp }
2916 13add988 2019-10-15 stsp
2917 96e2b566 2019-07-08 stsp s->search_entry = entry;
2918 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2919 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2920 b1bf1435 2019-06-21 stsp else
2921 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2922 60493ae3 2019-06-20 stsp }
2923 60493ae3 2019-06-20 stsp
2924 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2925 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2926 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2927 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2928 60493ae3 2019-06-20 stsp if (err)
2929 60493ae3 2019-06-20 stsp return err;
2930 ead14cbe 2019-06-21 stsp cur++;
2931 ead14cbe 2019-06-21 stsp }
2932 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2933 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2934 60493ae3 2019-06-20 stsp if (err)
2935 60493ae3 2019-06-20 stsp return err;
2936 ead14cbe 2019-06-21 stsp cur--;
2937 60493ae3 2019-06-20 stsp }
2938 60493ae3 2019-06-20 stsp }
2939 60493ae3 2019-06-20 stsp
2940 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2941 96e2b566 2019-07-08 stsp
2942 60493ae3 2019-06-20 stsp return NULL;
2943 60493ae3 2019-06-20 stsp }
2944 60493ae3 2019-06-20 stsp
2945 60493ae3 2019-06-20 stsp static const struct got_error *
2946 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2947 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2948 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2949 80ddbec8 2018-04-29 stsp {
2950 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2951 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2952 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2953 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2954 1a76625f 2018-10-22 stsp int errcode;
2955 80ddbec8 2018-04-29 stsp
2956 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2957 f135c941 2020-02-20 stsp free(s->in_repo_path);
2958 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2959 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2960 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2961 f135c941 2020-02-20 stsp }
2962 ecb28ae0 2018-07-16 stsp
2963 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2964 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2965 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2966 78756c87 2020-11-24 stsp
2967 fb2756b9 2018-08-04 stsp s->repo = repo;
2968 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2969 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2970 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2971 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2972 9cd7cbd1 2020-12-07 stsp goto done;
2973 9cd7cbd1 2020-12-07 stsp }
2974 9cd7cbd1 2020-12-07 stsp }
2975 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2976 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2977 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2978 5036bf37 2018-09-24 stsp goto done;
2979 5036bf37 2018-09-24 stsp }
2980 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2981 e5a0f69f 2018-08-18 stsp
2982 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2983 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2984 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2985 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2986 11b20872 2019-11-08 stsp if (err)
2987 11b20872 2019-11-08 stsp goto done;
2988 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2989 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2990 11b20872 2019-11-08 stsp if (err) {
2991 11b20872 2019-11-08 stsp free_colors(&s->colors);
2992 11b20872 2019-11-08 stsp goto done;
2993 11b20872 2019-11-08 stsp }
2994 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2995 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2996 11b20872 2019-11-08 stsp if (err) {
2997 11b20872 2019-11-08 stsp free_colors(&s->colors);
2998 11b20872 2019-11-08 stsp goto done;
2999 11b20872 2019-11-08 stsp }
3000 11b20872 2019-11-08 stsp }
3001 11b20872 2019-11-08 stsp
3002 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3003 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3004 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3005 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3006 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3007 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3008 1a76625f 2018-10-22 stsp
3009 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3010 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3011 74467cc8 2022-06-15 stsp if (err)
3012 74467cc8 2022-06-15 stsp goto done;
3013 74467cc8 2022-06-15 stsp }
3014 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3015 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3016 0ae84acc 2022-06-15 tracey if (err)
3017 0ae84acc 2022-06-15 tracey goto done;
3018 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3019 b672a97a 2020-01-27 stsp !s->log_branches);
3020 1a76625f 2018-10-22 stsp if (err)
3021 1a76625f 2018-10-22 stsp goto done;
3022 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3023 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3024 c5b78334 2020-01-12 stsp if (err)
3025 c5b78334 2020-01-12 stsp goto done;
3026 1a76625f 2018-10-22 stsp
3027 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3028 1a76625f 2018-10-22 stsp if (errcode) {
3029 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3030 1a76625f 2018-10-22 stsp goto done;
3031 1a76625f 2018-10-22 stsp }
3032 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3033 7c1452c1 2020-03-26 stsp if (errcode) {
3034 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3035 7c1452c1 2020-03-26 stsp goto done;
3036 7c1452c1 2020-03-26 stsp }
3037 1a76625f 2018-10-22 stsp
3038 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3039 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3040 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3041 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3042 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3043 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3044 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3045 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3046 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3047 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3048 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3049 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3050 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3051 ba4f502b 2018-08-04 stsp done:
3052 1a76625f 2018-10-22 stsp if (err)
3053 1a76625f 2018-10-22 stsp close_log_view(view);
3054 ba4f502b 2018-08-04 stsp return err;
3055 ba4f502b 2018-08-04 stsp }
3056 ba4f502b 2018-08-04 stsp
3057 e5a0f69f 2018-08-18 stsp static const struct got_error *
3058 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3059 ba4f502b 2018-08-04 stsp {
3060 f2f6d207 2020-11-24 stsp const struct got_error *err;
3061 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3062 ba4f502b 2018-08-04 stsp
3063 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3064 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3065 2b380cc8 2018-10-24 stsp &s->thread_args);
3066 2b380cc8 2018-10-24 stsp if (errcode)
3067 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3068 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3069 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3070 f2f6d207 2020-11-24 stsp if (err)
3071 f2f6d207 2020-11-24 stsp return err;
3072 f2f6d207 2020-11-24 stsp }
3073 2b380cc8 2018-10-24 stsp }
3074 2b380cc8 2018-10-24 stsp
3075 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3076 b880cc75 2022-06-30 mark }
3077 b880cc75 2022-06-30 mark
3078 b880cc75 2022-06-30 mark static void
3079 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3080 b880cc75 2022-06-30 mark {
3081 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3082 b880cc75 2022-06-30 mark
3083 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3084 b880cc75 2022-06-30 mark view->count = 0;
3085 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3086 b880cc75 2022-06-30 mark return;
3087 b880cc75 2022-06-30 mark
3088 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3089 b880cc75 2022-06-30 mark || home)
3090 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3091 b880cc75 2022-06-30 mark
3092 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3093 b880cc75 2022-06-30 mark --s->selected;
3094 b880cc75 2022-06-30 mark else
3095 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3096 b880cc75 2022-06-30 mark
3097 b880cc75 2022-06-30 mark select_commit(s);
3098 b880cc75 2022-06-30 mark return;
3099 b880cc75 2022-06-30 mark }
3100 b880cc75 2022-06-30 mark
3101 b880cc75 2022-06-30 mark static const struct got_error *
3102 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3103 b880cc75 2022-06-30 mark {
3104 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3105 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3106 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3107 b880cc75 2022-06-30 mark
3108 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3109 b880cc75 2022-06-30 mark if (first == NULL) {
3110 b880cc75 2022-06-30 mark view->count = 0;
3111 b880cc75 2022-06-30 mark return NULL;
3112 b880cc75 2022-06-30 mark }
3113 b880cc75 2022-06-30 mark
3114 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3115 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3116 b880cc75 2022-06-30 mark return NULL;
3117 b880cc75 2022-06-30 mark
3118 b880cc75 2022-06-30 mark if (!page) {
3119 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3120 b880cc75 2022-06-30 mark
3121 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3122 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3123 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3124 b880cc75 2022-06-30 mark ++s->selected;
3125 b880cc75 2022-06-30 mark else
3126 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3127 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3128 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3129 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3130 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3131 b880cc75 2022-06-30 mark else
3132 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3133 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3134 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3135 b880cc75 2022-06-30 mark } else {
3136 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3137 b880cc75 2022-06-30 mark if (err)
3138 b880cc75 2022-06-30 mark return err;
3139 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3140 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3141 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3142 b880cc75 2022-06-30 mark }
3143 b880cc75 2022-06-30 mark }
3144 b880cc75 2022-06-30 mark if (err)
3145 b880cc75 2022-06-30 mark return err;
3146 b880cc75 2022-06-30 mark
3147 9b058f45 2022-06-30 mark /*
3148 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3149 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3150 9b058f45 2022-06-30 mark */
3151 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3152 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3153 9b058f45 2022-06-30 mark
3154 b880cc75 2022-06-30 mark select_commit(s);
3155 b880cc75 2022-06-30 mark
3156 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3157 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3158 b880cc75 2022-06-30 mark view->count = 0;
3159 b880cc75 2022-06-30 mark
3160 b880cc75 2022-06-30 mark return NULL;
3161 e5a0f69f 2018-08-18 stsp }
3162 04cc582a 2018-08-01 stsp
3163 9b058f45 2022-06-30 mark static void
3164 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3165 9b058f45 2022-06-30 mark {
3166 76364b2d 2022-07-02 mark *x = 0;
3167 76364b2d 2022-07-02 mark *y = 0;
3168 76364b2d 2022-07-02 mark
3169 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3170 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3171 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3172 7532ccda 2022-07-11 mark else if (view->resized_y)
3173 7532ccda 2022-07-11 mark *y = view->resized_y;
3174 3c1dfe12 2022-07-08 mark else
3175 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3176 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3177 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3178 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3179 7532ccda 2022-07-11 mark else if (view->resized_x)
3180 7532ccda 2022-07-11 mark *x = view->resized_x;
3181 3c1dfe12 2022-07-08 mark else
3182 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3183 3c1dfe12 2022-07-08 mark }
3184 9b058f45 2022-06-30 mark }
3185 9b058f45 2022-06-30 mark
3186 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3187 e5a0f69f 2018-08-18 stsp static const struct got_error *
3188 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3189 9b058f45 2022-06-30 mark {
3190 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3191 9b058f45 2022-06-30 mark
3192 9b058f45 2022-06-30 mark view->nlines = y;
3193 d2366e29 2022-07-07 mark view->ncols = COLS;
3194 9b058f45 2022-06-30 mark err = view_resize(view);
3195 9b058f45 2022-06-30 mark if (err)
3196 9b058f45 2022-06-30 mark return err;
3197 9b058f45 2022-06-30 mark
3198 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3199 9b058f45 2022-06-30 mark
3200 9b058f45 2022-06-30 mark return err;
3201 9b058f45 2022-06-30 mark }
3202 9b058f45 2022-06-30 mark
3203 9b058f45 2022-06-30 mark static const struct got_error *
3204 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3205 e5a0f69f 2018-08-18 stsp {
3206 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3207 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3208 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3209 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3210 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3211 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
3212 80ddbec8 2018-04-29 stsp
3213 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3214 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3215 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3216 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3217 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3218 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3219 fb280deb 2021-08-30 stsp }
3220 b880cc75 2022-06-30 mark return err;
3221 528dedf3 2021-08-30 stsp }
3222 0dca135e 2022-06-30 mark
3223 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3224 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3225 0dca135e 2022-06-30 mark --eos; /* border */
3226 0dca135e 2022-06-30 mark
3227 528dedf3 2021-08-30 stsp switch (ch) {
3228 1e37a5c2 2019-05-12 jcs case 'q':
3229 1e37a5c2 2019-05-12 jcs s->quit = 1;
3230 145b6838 2022-06-16 stsp break;
3231 145b6838 2022-06-16 stsp case '0':
3232 145b6838 2022-06-16 stsp view->x = 0;
3233 145b6838 2022-06-16 stsp break;
3234 145b6838 2022-06-16 stsp case '$':
3235 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3236 640cd7ff 2022-06-22 mark view->count = 0;
3237 145b6838 2022-06-16 stsp break;
3238 145b6838 2022-06-16 stsp case KEY_RIGHT:
3239 145b6838 2022-06-16 stsp case 'l':
3240 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3241 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3242 640cd7ff 2022-06-22 mark else
3243 640cd7ff 2022-06-22 mark view->count = 0;
3244 1e37a5c2 2019-05-12 jcs break;
3245 145b6838 2022-06-16 stsp case KEY_LEFT:
3246 145b6838 2022-06-16 stsp case 'h':
3247 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3248 640cd7ff 2022-06-22 mark if (view->x <= 0)
3249 640cd7ff 2022-06-22 mark view->count = 0;
3250 145b6838 2022-06-16 stsp break;
3251 1e37a5c2 2019-05-12 jcs case 'k':
3252 1e37a5c2 2019-05-12 jcs case KEY_UP:
3253 1e37a5c2 2019-05-12 jcs case '<':
3254 1e37a5c2 2019-05-12 jcs case ',':
3255 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3256 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3257 912a3f79 2021-08-30 j break;
3258 912a3f79 2021-08-30 j case 'g':
3259 912a3f79 2021-08-30 j case KEY_HOME:
3260 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3261 640cd7ff 2022-06-22 mark view->count = 0;
3262 1e37a5c2 2019-05-12 jcs break;
3263 83cc4199 2022-06-13 stsp case CTRL('u'):
3264 33c3719a 2022-06-15 stsp case 'u':
3265 83cc4199 2022-06-13 stsp nscroll /= 2;
3266 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3267 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3268 a4292ac5 2019-05-12 jcs case CTRL('b'):
3269 61417565 2022-06-20 mark case 'b':
3270 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3271 1e37a5c2 2019-05-12 jcs break;
3272 1e37a5c2 2019-05-12 jcs case 'j':
3273 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3274 1e37a5c2 2019-05-12 jcs case '>':
3275 1e37a5c2 2019-05-12 jcs case '.':
3276 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3277 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3278 912a3f79 2021-08-30 j break;
3279 10aab77f 2022-07-19 op case '@':
3280 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3281 10aab77f 2022-07-19 op break;
3282 912a3f79 2021-08-30 j case 'G':
3283 912a3f79 2021-08-30 j case KEY_END: {
3284 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3285 912a3f79 2021-08-30 j * traverse them all. */
3286 640cd7ff 2022-06-22 mark view->count = 0;
3287 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3288 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3289 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3290 80ddbec8 2018-04-29 stsp }
3291 912a3f79 2021-08-30 j
3292 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3293 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3294 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3295 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3296 f3bc9f1d 2021-09-05 naddy break;
3297 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3298 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3299 f3bc9f1d 2021-09-05 naddy }
3300 f3bc9f1d 2021-09-05 naddy if (n > 0)
3301 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3302 2b779855 2020-12-05 naddy select_commit(s);
3303 1e37a5c2 2019-05-12 jcs break;
3304 912a3f79 2021-08-30 j }
3305 80b7e8da 2022-06-11 stsp case CTRL('d'):
3306 33c3719a 2022-06-15 stsp case 'd':
3307 83cc4199 2022-06-13 stsp nscroll /= 2;
3308 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3309 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3310 61417565 2022-06-20 mark case CTRL('f'):
3311 48bb96f0 2022-06-20 naddy case 'f':
3312 b880cc75 2022-06-30 mark case ' ':
3313 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3314 1e37a5c2 2019-05-12 jcs break;
3315 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3316 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3317 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3318 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3319 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3320 2b779855 2020-12-05 naddy select_commit(s);
3321 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3322 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3323 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3324 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3325 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3326 0bf7f153 2020-12-02 naddy }
3327 1e37a5c2 2019-05-12 jcs break;
3328 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3329 49b24ee5 2022-07-03 mark case '\r':
3330 640cd7ff 2022-06-22 mark view->count = 0;
3331 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3332 e5a0f69f 2018-08-18 stsp break;
3333 9b058f45 2022-06-30 mark
3334 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3335 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3336 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3337 9b058f45 2022-06-30 mark
3338 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3339 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3340 78756c87 2020-11-24 stsp view, s->repo);
3341 1e37a5c2 2019-05-12 jcs if (err)
3342 1e37a5c2 2019-05-12 jcs break;
3343 9b058f45 2022-06-30 mark
3344 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3345 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3346 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3347 9b058f45 2022-06-30 mark if (err)
3348 9b058f45 2022-06-30 mark break;
3349 9b058f45 2022-06-30 mark }
3350 9b058f45 2022-06-30 mark
3351 e78dc838 2020-12-04 stsp view->focussed = 0;
3352 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3353 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3354 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3355 9b058f45 2022-06-30 mark
3356 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3357 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
3358 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3359 f7013a22 2018-10-24 stsp if (err)
3360 1e37a5c2 2019-05-12 jcs return err;
3361 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3362 0dbbbe90 2022-06-17 op if (err)
3363 0dbbbe90 2022-06-17 op return err;
3364 e78dc838 2020-12-04 stsp view->focus_child = 1;
3365 1e37a5c2 2019-05-12 jcs } else
3366 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3367 1e37a5c2 2019-05-12 jcs break;
3368 1e37a5c2 2019-05-12 jcs case 't':
3369 640cd7ff 2022-06-22 mark view->count = 0;
3370 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3371 5036bf37 2018-09-24 stsp break;
3372 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3373 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3374 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3375 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3376 4e97c21c 2020-12-06 stsp s->repo);
3377 1e37a5c2 2019-05-12 jcs if (err)
3378 e5a0f69f 2018-08-18 stsp break;
3379 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3380 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3381 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3382 49b24ee5 2022-07-03 mark if (err)
3383 49b24ee5 2022-07-03 mark break;
3384 49b24ee5 2022-07-03 mark }
3385 e78dc838 2020-12-04 stsp view->focussed = 0;
3386 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3387 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3388 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3389 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3390 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
3391 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3392 1e37a5c2 2019-05-12 jcs if (err)
3393 1e37a5c2 2019-05-12 jcs return err;
3394 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3395 0dbbbe90 2022-06-17 op if (err)
3396 0dbbbe90 2022-06-17 op return err;
3397 e78dc838 2020-12-04 stsp view->focus_child = 1;
3398 1e37a5c2 2019-05-12 jcs } else
3399 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3400 1e37a5c2 2019-05-12 jcs break;
3401 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3402 21355643 2020-12-06 stsp case CTRL('l'):
3403 21355643 2020-12-06 stsp case 'B':
3404 640cd7ff 2022-06-22 mark view->count = 0;
3405 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3406 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3407 1e37a5c2 2019-05-12 jcs break;
3408 21355643 2020-12-06 stsp err = stop_log_thread(s);
3409 74cfe85e 2020-10-20 stsp if (err)
3410 74cfe85e 2020-10-20 stsp return err;
3411 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3412 21355643 2020-12-06 stsp char *parent_path;
3413 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3414 21355643 2020-12-06 stsp if (err)
3415 21355643 2020-12-06 stsp return err;
3416 21355643 2020-12-06 stsp free(s->in_repo_path);
3417 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3418 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3419 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3420 21355643 2020-12-06 stsp struct got_object_id *start_id;
3421 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3422 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3423 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3424 21355643 2020-12-06 stsp if (err)
3425 21355643 2020-12-06 stsp return err;
3426 21355643 2020-12-06 stsp free(s->start_id);
3427 21355643 2020-12-06 stsp s->start_id = start_id;
3428 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3429 21355643 2020-12-06 stsp } else /* 'B' */
3430 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3431 21355643 2020-12-06 stsp
3432 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3433 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3434 b0dd8d27 2022-06-16 stsp if (err)
3435 b0dd8d27 2022-06-16 stsp return err;
3436 b0dd8d27 2022-06-16 stsp }
3437 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3438 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3439 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3440 74cfe85e 2020-10-20 stsp if (err)
3441 21355643 2020-12-06 stsp return err;
3442 51a10b52 2020-12-26 stsp tog_free_refs();
3443 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3444 ca51c541 2020-12-07 stsp if (err)
3445 ca51c541 2020-12-07 stsp return err;
3446 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3447 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3448 d01904d4 2019-06-25 stsp if (err)
3449 d01904d4 2019-06-25 stsp return err;
3450 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3451 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3452 b672a97a 2020-01-27 stsp if (err)
3453 b672a97a 2020-01-27 stsp return err;
3454 21355643 2020-12-06 stsp free_commits(&s->commits);
3455 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3456 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3457 21355643 2020-12-06 stsp s->selected_entry = NULL;
3458 21355643 2020-12-06 stsp s->selected = 0;
3459 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3460 21355643 2020-12-06 stsp s->quit = 0;
3461 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3462 dfee752e 2022-06-18 op s->matched_entry = NULL;
3463 dfee752e 2022-06-18 op s->search_entry = NULL;
3464 d01904d4 2019-06-25 stsp break;
3465 6458efa5 2020-11-24 stsp case 'r':
3466 640cd7ff 2022-06-22 mark view->count = 0;
3467 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3468 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3469 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3470 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3471 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3472 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3473 6458efa5 2020-11-24 stsp if (err) {
3474 6458efa5 2020-11-24 stsp view_close(ref_view);
3475 6458efa5 2020-11-24 stsp return err;
3476 6458efa5 2020-11-24 stsp }
3477 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3478 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3479 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3480 49b24ee5 2022-07-03 mark if (err)
3481 49b24ee5 2022-07-03 mark break;
3482 49b24ee5 2022-07-03 mark }
3483 e78dc838 2020-12-04 stsp view->focussed = 0;
3484 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3485 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3486 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3487 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3488 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
3489 6458efa5 2020-11-24 stsp err = view_close_child(view);
3490 6458efa5 2020-11-24 stsp if (err)
3491 6458efa5 2020-11-24 stsp return err;
3492 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3493 0dbbbe90 2022-06-17 op if (err)
3494 0dbbbe90 2022-06-17 op return err;
3495 e78dc838 2020-12-04 stsp view->focus_child = 1;
3496 6458efa5 2020-11-24 stsp } else
3497 6458efa5 2020-11-24 stsp *new_view = ref_view;
3498 6458efa5 2020-11-24 stsp break;
3499 1e37a5c2 2019-05-12 jcs default:
3500 640cd7ff 2022-06-22 mark view->count = 0;
3501 1e37a5c2 2019-05-12 jcs break;
3502 899d86c2 2018-05-10 stsp }
3503 e5a0f69f 2018-08-18 stsp
3504 80ddbec8 2018-04-29 stsp return err;
3505 80ddbec8 2018-04-29 stsp }
3506 80ddbec8 2018-04-29 stsp
3507 4ed7e80c 2018-05-20 stsp static const struct got_error *
3508 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3509 c2db6724 2019-01-04 stsp {
3510 c2db6724 2019-01-04 stsp const struct got_error *error;
3511 c2db6724 2019-01-04 stsp
3512 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3513 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3514 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3515 37c06ea4 2019-07-15 stsp #endif
3516 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3517 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3518 c2db6724 2019-01-04 stsp
3519 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3520 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3521 c2db6724 2019-01-04 stsp
3522 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3523 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3524 c2db6724 2019-01-04 stsp
3525 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3526 c2db6724 2019-01-04 stsp if (error != NULL)
3527 c2db6724 2019-01-04 stsp return error;
3528 c2db6724 2019-01-04 stsp
3529 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3530 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3531 c2db6724 2019-01-04 stsp
3532 c2db6724 2019-01-04 stsp return NULL;
3533 c2db6724 2019-01-04 stsp }
3534 c2db6724 2019-01-04 stsp
3535 a915003a 2019-02-05 stsp static void
3536 a915003a 2019-02-05 stsp init_curses(void)
3537 a915003a 2019-02-05 stsp {
3538 2497f032 2022-05-31 stsp /*
3539 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3540 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3541 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3542 2497f032 2022-05-31 stsp */
3543 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3544 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3545 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3546 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3547 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3548 2497f032 2022-05-31 stsp
3549 a915003a 2019-02-05 stsp initscr();
3550 a915003a 2019-02-05 stsp cbreak();
3551 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3552 a915003a 2019-02-05 stsp noecho();
3553 a915003a 2019-02-05 stsp nonl();
3554 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3555 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3556 a915003a 2019-02-05 stsp curs_set(0);
3557 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3558 6d17833f 2019-11-08 stsp start_color();
3559 6d17833f 2019-11-08 stsp use_default_colors();
3560 6d17833f 2019-11-08 stsp }
3561 a915003a 2019-02-05 stsp }
3562 a915003a 2019-02-05 stsp
3563 c2db6724 2019-01-04 stsp static const struct got_error *
3564 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3565 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3566 f135c941 2020-02-20 stsp {
3567 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3568 f135c941 2020-02-20 stsp
3569 f135c941 2020-02-20 stsp if (argc == 0) {
3570 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3571 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3572 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3573 f135c941 2020-02-20 stsp return NULL;
3574 f135c941 2020-02-20 stsp }
3575 f135c941 2020-02-20 stsp
3576 f135c941 2020-02-20 stsp if (worktree) {
3577 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3578 bfd61697 2020-11-14 stsp char *p;
3579 f135c941 2020-02-20 stsp
3580 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3581 f135c941 2020-02-20 stsp if (err)
3582 f135c941 2020-02-20 stsp return err;
3583 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3584 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3585 bfd61697 2020-11-14 stsp p) == -1) {
3586 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3587 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3588 f135c941 2020-02-20 stsp }
3589 f135c941 2020-02-20 stsp free(p);
3590 f135c941 2020-02-20 stsp } else
3591 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3592 f135c941 2020-02-20 stsp
3593 f135c941 2020-02-20 stsp return err;
3594 f135c941 2020-02-20 stsp }
3595 f135c941 2020-02-20 stsp
3596 f135c941 2020-02-20 stsp static const struct got_error *
3597 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3598 9f7d7167 2018-04-29 stsp {
3599 80ddbec8 2018-04-29 stsp const struct got_error *error;
3600 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3601 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3602 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3603 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3604 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3605 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3606 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3607 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3608 04cc582a 2018-08-01 stsp struct tog_view *view;
3609 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3610 80ddbec8 2018-04-29 stsp
3611 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3612 80ddbec8 2018-04-29 stsp switch (ch) {
3613 b672a97a 2020-01-27 stsp case 'b':
3614 b672a97a 2020-01-27 stsp log_branches = 1;
3615 b672a97a 2020-01-27 stsp break;
3616 80ddbec8 2018-04-29 stsp case 'c':
3617 80ddbec8 2018-04-29 stsp start_commit = optarg;
3618 80ddbec8 2018-04-29 stsp break;
3619 ecb28ae0 2018-07-16 stsp case 'r':
3620 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3621 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3622 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3623 9ba1d308 2019-10-21 stsp optarg);
3624 ecb28ae0 2018-07-16 stsp break;
3625 80ddbec8 2018-04-29 stsp default:
3626 17020d27 2019-03-07 stsp usage_log();
3627 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3628 80ddbec8 2018-04-29 stsp }
3629 80ddbec8 2018-04-29 stsp }
3630 80ddbec8 2018-04-29 stsp
3631 80ddbec8 2018-04-29 stsp argc -= optind;
3632 80ddbec8 2018-04-29 stsp argv += optind;
3633 80ddbec8 2018-04-29 stsp
3634 f135c941 2020-02-20 stsp if (argc > 1)
3635 f135c941 2020-02-20 stsp usage_log();
3636 963f97a1 2019-03-18 stsp
3637 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3638 0ae84acc 2022-06-15 tracey if (error != NULL)
3639 0ae84acc 2022-06-15 tracey goto done;
3640 0ae84acc 2022-06-15 tracey
3641 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3642 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3643 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3644 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3645 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3646 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3647 c156c7a4 2020-12-18 stsp goto done;
3648 a1fbf39a 2019-08-11 stsp if (worktree)
3649 6962eb72 2020-02-20 stsp repo_path =
3650 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3651 a1fbf39a 2019-08-11 stsp else
3652 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3653 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3654 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3655 c156c7a4 2020-12-18 stsp goto done;
3656 c156c7a4 2020-12-18 stsp }
3657 ecb28ae0 2018-07-16 stsp }
3658 ecb28ae0 2018-07-16 stsp
3659 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3660 80ddbec8 2018-04-29 stsp if (error != NULL)
3661 ecb28ae0 2018-07-16 stsp goto done;
3662 80ddbec8 2018-04-29 stsp
3663 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3664 f135c941 2020-02-20 stsp repo, worktree);
3665 f135c941 2020-02-20 stsp if (error)
3666 f135c941 2020-02-20 stsp goto done;
3667 f135c941 2020-02-20 stsp
3668 f135c941 2020-02-20 stsp init_curses();
3669 f135c941 2020-02-20 stsp
3670 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3671 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3672 c02c541e 2019-03-29 stsp if (error)
3673 c02c541e 2019-03-29 stsp goto done;
3674 c02c541e 2019-03-29 stsp
3675 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3676 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3677 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3678 87670572 2020-12-26 naddy if (error)
3679 87670572 2020-12-26 naddy goto done;
3680 87670572 2020-12-26 naddy }
3681 51a10b52 2020-12-26 stsp
3682 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3683 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3684 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3685 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3686 d8f38dc4 2020-12-05 stsp if (error)
3687 d8f38dc4 2020-12-05 stsp goto done;
3688 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3689 d8f38dc4 2020-12-05 stsp } else {
3690 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3691 d8f38dc4 2020-12-05 stsp if (error == NULL)
3692 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3693 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3694 d8f38dc4 2020-12-05 stsp goto done;
3695 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3696 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3697 d8f38dc4 2020-12-05 stsp if (error)
3698 d8f38dc4 2020-12-05 stsp goto done;
3699 d8f38dc4 2020-12-05 stsp }
3700 8b473291 2019-02-21 stsp
3701 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3702 04cc582a 2018-08-01 stsp if (view == NULL) {
3703 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3704 04cc582a 2018-08-01 stsp goto done;
3705 04cc582a 2018-08-01 stsp }
3706 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3707 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3708 ba4f502b 2018-08-04 stsp if (error)
3709 ba4f502b 2018-08-04 stsp goto done;
3710 2fc00ff4 2019-08-31 stsp if (worktree) {
3711 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3712 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3713 2fc00ff4 2019-08-31 stsp worktree = NULL;
3714 2fc00ff4 2019-08-31 stsp }
3715 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3716 ecb28ae0 2018-07-16 stsp done:
3717 f135c941 2020-02-20 stsp free(in_repo_path);
3718 ecb28ae0 2018-07-16 stsp free(repo_path);
3719 ecb28ae0 2018-07-16 stsp free(cwd);
3720 899d86c2 2018-05-10 stsp free(start_id);
3721 d8f38dc4 2020-12-05 stsp free(label);
3722 d8f38dc4 2020-12-05 stsp if (ref)
3723 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3724 1d0f4054 2021-06-17 stsp if (repo) {
3725 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3726 1d0f4054 2021-06-17 stsp if (error == NULL)
3727 1d0f4054 2021-06-17 stsp error = close_err;
3728 1d0f4054 2021-06-17 stsp }
3729 ec142235 2019-03-07 stsp if (worktree)
3730 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3731 0ae84acc 2022-06-15 tracey if (pack_fds) {
3732 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3733 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3734 0ae84acc 2022-06-15 tracey if (error == NULL)
3735 0ae84acc 2022-06-15 tracey error = pack_err;
3736 0ae84acc 2022-06-15 tracey }
3737 51a10b52 2020-12-26 stsp tog_free_refs();
3738 80ddbec8 2018-04-29 stsp return error;
3739 9f7d7167 2018-04-29 stsp }
3740 9f7d7167 2018-04-29 stsp
3741 4ed7e80c 2018-05-20 stsp __dead static void
3742 9f7d7167 2018-04-29 stsp usage_diff(void)
3743 9f7d7167 2018-04-29 stsp {
3744 80ddbec8 2018-04-29 stsp endwin();
3745 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3746 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3747 9f7d7167 2018-04-29 stsp exit(1);
3748 b304db33 2018-05-20 stsp }
3749 b304db33 2018-05-20 stsp
3750 6d17833f 2019-11-08 stsp static int
3751 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3752 41605754 2020-11-12 stsp regmatch_t *regmatch)
3753 6d17833f 2019-11-08 stsp {
3754 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3755 6d17833f 2019-11-08 stsp }
3756 6d17833f 2019-11-08 stsp
3757 336075a4 2022-06-25 op static struct tog_color *
3758 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3759 6d17833f 2019-11-08 stsp {
3760 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3761 6d17833f 2019-11-08 stsp
3762 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3763 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3764 6d17833f 2019-11-08 stsp return tc;
3765 6d17833f 2019-11-08 stsp }
3766 6d17833f 2019-11-08 stsp
3767 6d17833f 2019-11-08 stsp return NULL;
3768 6d17833f 2019-11-08 stsp }
3769 6d17833f 2019-11-08 stsp
3770 4ed7e80c 2018-05-20 stsp static const struct got_error *
3771 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3772 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3773 41605754 2020-11-12 stsp {
3774 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3775 44a87665 2022-06-16 stsp char *exstr = NULL;
3776 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3777 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3778 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3779 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3780 41605754 2020-11-12 stsp
3781 41605754 2020-11-12 stsp *wtotal = 0;
3782 1853e0f4 2022-06-16 stsp
3783 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3784 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3785 41605754 2020-11-12 stsp
3786 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3787 44a87665 2022-06-16 stsp if (err)
3788 44a87665 2022-06-16 stsp return err;
3789 44a87665 2022-06-16 stsp
3790 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3791 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3792 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3793 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3794 44a87665 2022-06-16 stsp goto done;
3795 44a87665 2022-06-16 stsp }
3796 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3797 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3798 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3799 1853e0f4 2022-06-16 stsp goto done;
3800 1853e0f4 2022-06-16 stsp }
3801 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3802 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3803 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3804 1853e0f4 2022-06-16 stsp goto done;
3805 1853e0f4 2022-06-16 stsp }
3806 145b6838 2022-06-16 stsp
3807 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3808 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3809 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3810 1853e0f4 2022-06-16 stsp if (err)
3811 1853e0f4 2022-06-16 stsp goto done;
3812 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3813 145b6838 2022-06-16 stsp if (n) {
3814 1853e0f4 2022-06-16 stsp free(wline);
3815 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3816 1853e0f4 2022-06-16 stsp wlimit, 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 wlimit -= width;
3821 1853e0f4 2022-06-16 stsp *wtotal += width;
3822 41605754 2020-11-12 stsp }
3823 41605754 2020-11-12 stsp
3824 41605754 2020-11-12 stsp if (wlimit > 0) {
3825 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3826 1853e0f4 2022-06-16 stsp size_t wlen;
3827 1853e0f4 2022-06-16 stsp
3828 1853e0f4 2022-06-16 stsp free(wline);
3829 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3830 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3831 1853e0f4 2022-06-16 stsp if (err)
3832 1853e0f4 2022-06-16 stsp goto done;
3833 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3834 1853e0f4 2022-06-16 stsp while (i < wlen) {
3835 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3836 1853e0f4 2022-06-16 stsp if (width == -1) {
3837 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3838 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3839 1853e0f4 2022-06-16 stsp goto done;
3840 1853e0f4 2022-06-16 stsp }
3841 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3842 1853e0f4 2022-06-16 stsp break;
3843 61417565 2022-06-20 mark w += width;
3844 1853e0f4 2022-06-16 stsp i++;
3845 41605754 2020-11-12 stsp }
3846 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3847 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3848 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3849 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3850 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3851 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3852 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3853 41605754 2020-11-12 stsp }
3854 41605754 2020-11-12 stsp }
3855 41605754 2020-11-12 stsp
3856 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3857 1853e0f4 2022-06-16 stsp free(wline);
3858 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3859 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3860 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3861 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3862 1853e0f4 2022-06-16 stsp if (err)
3863 1853e0f4 2022-06-16 stsp goto done;
3864 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3865 1853e0f4 2022-06-16 stsp } else {
3866 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3867 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3868 1853e0f4 2022-06-16 stsp if (err)
3869 1853e0f4 2022-06-16 stsp goto done;
3870 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3871 1853e0f4 2022-06-16 stsp }
3872 1853e0f4 2022-06-16 stsp *wtotal += width2;
3873 41605754 2020-11-12 stsp }
3874 1853e0f4 2022-06-16 stsp done:
3875 145b6838 2022-06-16 stsp free(wline);
3876 44a87665 2022-06-16 stsp free(exstr);
3877 1853e0f4 2022-06-16 stsp free(seg0);
3878 1853e0f4 2022-06-16 stsp free(seg1);
3879 1853e0f4 2022-06-16 stsp free(seg2);
3880 1853e0f4 2022-06-16 stsp return err;
3881 41605754 2020-11-12 stsp }
3882 41605754 2020-11-12 stsp
3883 41605754 2020-11-12 stsp static const struct got_error *
3884 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3885 26ed57b2 2018-05-19 stsp {
3886 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3887 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3888 61e69b96 2018-05-20 stsp const struct got_error *err;
3889 fe621944 2020-11-10 stsp int nprinted = 0;
3890 b304db33 2018-05-20 stsp char *line;
3891 826082fe 2020-12-10 stsp size_t linesize = 0;
3892 826082fe 2020-12-10 stsp ssize_t linelen;
3893 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3894 61e69b96 2018-05-20 stsp wchar_t *wline;
3895 e0b650dd 2018-05-20 stsp int width;
3896 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3897 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3898 fe621944 2020-11-10 stsp off_t line_offset;
3899 26ed57b2 2018-05-19 stsp
3900 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3901 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3902 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3903 fe621944 2020-11-10 stsp
3904 f7d12f7e 2018-08-01 stsp werase(view->window);
3905 a3404814 2018-09-02 stsp
3906 a3404814 2018-09-02 stsp if (header) {
3907 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3908 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3909 135a2da0 2020-11-11 stsp header) == -1)
3910 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3911 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3912 ccda2f4d 2022-06-16 stsp 0, 0);
3913 135a2da0 2020-11-11 stsp free(line);
3914 135a2da0 2020-11-11 stsp if (err)
3915 a3404814 2018-09-02 stsp return err;
3916 a3404814 2018-09-02 stsp
3917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3918 a3404814 2018-09-02 stsp wstandout(view->window);
3919 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3920 e54cc94a 2020-11-11 stsp free(wline);
3921 e54cc94a 2020-11-11 stsp wline = NULL;
3922 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3923 a3404814 2018-09-02 stsp wstandend(view->window);
3924 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3925 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3926 26ed57b2 2018-05-19 stsp
3927 a3404814 2018-09-02 stsp if (max_lines <= 1)
3928 a3404814 2018-09-02 stsp return NULL;
3929 a3404814 2018-09-02 stsp max_lines--;
3930 a3404814 2018-09-02 stsp }
3931 a3404814 2018-09-02 stsp
3932 89f1a395 2020-12-01 naddy s->eof = 0;
3933 145b6838 2022-06-16 stsp view->maxx = 0;
3934 826082fe 2020-12-10 stsp line = NULL;
3935 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3936 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3937 826082fe 2020-12-10 stsp if (linelen == -1) {
3938 826082fe 2020-12-10 stsp if (feof(s->f)) {
3939 826082fe 2020-12-10 stsp s->eof = 1;
3940 826082fe 2020-12-10 stsp break;
3941 826082fe 2020-12-10 stsp }
3942 826082fe 2020-12-10 stsp free(line);
3943 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3944 61e69b96 2018-05-20 stsp }
3945 145b6838 2022-06-16 stsp
3946 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3947 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3948 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3949 1853e0f4 2022-06-16 stsp if (err) {
3950 1853e0f4 2022-06-16 stsp free(line);
3951 1853e0f4 2022-06-16 stsp return err;
3952 1853e0f4 2022-06-16 stsp }
3953 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3954 1853e0f4 2022-06-16 stsp free(wline);
3955 1853e0f4 2022-06-16 stsp wline = NULL;
3956 1853e0f4 2022-06-16 stsp
3957 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3958 f26dddb7 2019-11-08 stsp if (tc)
3959 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3960 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3961 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3962 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3963 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3964 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3965 41605754 2020-11-12 stsp if (err) {
3966 41605754 2020-11-12 stsp free(line);
3967 41605754 2020-11-12 stsp return err;
3968 41605754 2020-11-12 stsp }
3969 41605754 2020-11-12 stsp } else {
3970 969c159c 2022-06-16 stsp int skip;
3971 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3972 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3973 969c159c 2022-06-16 stsp if (err) {
3974 969c159c 2022-06-16 stsp free(line);
3975 969c159c 2022-06-16 stsp return err;
3976 969c159c 2022-06-16 stsp }
3977 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3978 969c159c 2022-06-16 stsp free(wline);
3979 41605754 2020-11-12 stsp wline = NULL;
3980 41605754 2020-11-12 stsp }
3981 f26dddb7 2019-11-08 stsp if (tc)
3982 6d17833f 2019-11-08 stsp wattr_off(view->window,
3983 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3984 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3985 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3986 fe621944 2020-11-10 stsp nprinted++;
3987 826082fe 2020-12-10 stsp }
3988 826082fe 2020-12-10 stsp free(line);
3989 fe621944 2020-11-10 stsp if (nprinted >= 1)
3990 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3991 89f1a395 2020-12-01 naddy (nprinted - 1);
3992 fe621944 2020-11-10 stsp else
3993 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3994 26ed57b2 2018-05-19 stsp
3995 9b058f45 2022-06-30 mark view_border(view);
3996 c3e9aa98 2019-05-13 jcs
3997 89f1a395 2020-12-01 naddy if (s->eof) {
3998 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3999 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4000 c3e9aa98 2019-05-13 jcs nprinted++;
4001 c3e9aa98 2019-05-13 jcs }
4002 c3e9aa98 2019-05-13 jcs
4003 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4004 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4005 c3e9aa98 2019-05-13 jcs if (err) {
4006 c3e9aa98 2019-05-13 jcs return err;
4007 c3e9aa98 2019-05-13 jcs }
4008 26ed57b2 2018-05-19 stsp
4009 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4010 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4011 e54cc94a 2020-11-11 stsp free(wline);
4012 e54cc94a 2020-11-11 stsp wline = NULL;
4013 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4014 c3e9aa98 2019-05-13 jcs }
4015 c3e9aa98 2019-05-13 jcs
4016 26ed57b2 2018-05-19 stsp return NULL;
4017 abd2672a 2018-12-23 stsp }
4018 abd2672a 2018-12-23 stsp
4019 abd2672a 2018-12-23 stsp static char *
4020 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4021 abd2672a 2018-12-23 stsp {
4022 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4023 09867e48 2019-08-13 stsp char *p, *s;
4024 09867e48 2019-08-13 stsp
4025 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4026 09867e48 2019-08-13 stsp if (tm == NULL)
4027 09867e48 2019-08-13 stsp return NULL;
4028 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4029 09867e48 2019-08-13 stsp if (s == NULL)
4030 09867e48 2019-08-13 stsp return NULL;
4031 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4032 abd2672a 2018-12-23 stsp if (p)
4033 abd2672a 2018-12-23 stsp *p = '\0';
4034 abd2672a 2018-12-23 stsp return s;
4035 9f7d7167 2018-04-29 stsp }
4036 9f7d7167 2018-04-29 stsp
4037 4ed7e80c 2018-05-20 stsp static const struct got_error *
4038 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4039 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4040 0208f208 2020-05-05 stsp {
4041 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4042 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4043 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4044 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4045 0208f208 2020-05-05 stsp
4046 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4047 0208f208 2020-05-05 stsp if (qid != NULL) {
4048 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4049 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4050 d7b5a0e8 2022-04-20 stsp &qid->id);
4051 0208f208 2020-05-05 stsp if (err)
4052 0208f208 2020-05-05 stsp return err;
4053 0208f208 2020-05-05 stsp
4054 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4055 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4056 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4057 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4058 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4059 aa8b5dd0 2021-08-01 stsp }
4060 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4061 0208f208 2020-05-05 stsp
4062 0208f208 2020-05-05 stsp }
4063 0208f208 2020-05-05 stsp
4064 0208f208 2020-05-05 stsp if (tree_id1) {
4065 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4066 0208f208 2020-05-05 stsp if (err)
4067 0208f208 2020-05-05 stsp goto done;
4068 0208f208 2020-05-05 stsp }
4069 0208f208 2020-05-05 stsp
4070 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4071 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4072 0208f208 2020-05-05 stsp if (err)
4073 0208f208 2020-05-05 stsp goto done;
4074 0208f208 2020-05-05 stsp
4075 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4076 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4077 0208f208 2020-05-05 stsp done:
4078 0208f208 2020-05-05 stsp if (tree1)
4079 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4080 0208f208 2020-05-05 stsp if (tree2)
4081 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4082 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4083 0208f208 2020-05-05 stsp return err;
4084 0208f208 2020-05-05 stsp }
4085 0208f208 2020-05-05 stsp
4086 0208f208 2020-05-05 stsp static const struct got_error *
4087 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4088 abd2672a 2018-12-23 stsp {
4089 fe621944 2020-11-10 stsp off_t *p;
4090 fe621944 2020-11-10 stsp
4091 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4092 fe621944 2020-11-10 stsp if (p == NULL)
4093 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4094 fe621944 2020-11-10 stsp *line_offsets = p;
4095 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4096 fe621944 2020-11-10 stsp (*nlines)++;
4097 fe621944 2020-11-10 stsp return NULL;
4098 fe621944 2020-11-10 stsp }
4099 fe621944 2020-11-10 stsp
4100 fe621944 2020-11-10 stsp static const struct got_error *
4101 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4102 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4103 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4104 fe621944 2020-11-10 stsp {
4105 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4106 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4107 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4108 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4109 45d799e2 2018-12-23 stsp time_t committer_time;
4110 45d799e2 2018-12-23 stsp const char *author, *committer;
4111 8b473291 2019-02-21 stsp char *refs_str = NULL;
4112 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4113 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4114 fe621944 2020-11-10 stsp off_t outoff = 0;
4115 fe621944 2020-11-10 stsp int n;
4116 abd2672a 2018-12-23 stsp
4117 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4118 0208f208 2020-05-05 stsp
4119 8b473291 2019-02-21 stsp if (refs) {
4120 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4121 8b473291 2019-02-21 stsp if (err)
4122 8b473291 2019-02-21 stsp return err;
4123 8b473291 2019-02-21 stsp }
4124 8b473291 2019-02-21 stsp
4125 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4126 abd2672a 2018-12-23 stsp if (err)
4127 abd2672a 2018-12-23 stsp return err;
4128 abd2672a 2018-12-23 stsp
4129 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4130 15a94983 2018-12-23 stsp if (err) {
4131 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4132 15a94983 2018-12-23 stsp goto done;
4133 15a94983 2018-12-23 stsp }
4134 abd2672a 2018-12-23 stsp
4135 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4136 fe621944 2020-11-10 stsp if (err)
4137 fe621944 2020-11-10 stsp goto done;
4138 fe621944 2020-11-10 stsp
4139 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4140 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4141 fe621944 2020-11-10 stsp if (n < 0) {
4142 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4143 abd2672a 2018-12-23 stsp goto done;
4144 abd2672a 2018-12-23 stsp }
4145 fe621944 2020-11-10 stsp outoff += n;
4146 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4147 fe621944 2020-11-10 stsp if (err)
4148 fe621944 2020-11-10 stsp goto done;
4149 fe621944 2020-11-10 stsp
4150 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4151 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4152 fe621944 2020-11-10 stsp if (n < 0) {
4153 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4154 abd2672a 2018-12-23 stsp goto done;
4155 abd2672a 2018-12-23 stsp }
4156 fe621944 2020-11-10 stsp outoff += n;
4157 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4158 fe621944 2020-11-10 stsp if (err)
4159 fe621944 2020-11-10 stsp goto done;
4160 fe621944 2020-11-10 stsp
4161 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4162 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4163 fe621944 2020-11-10 stsp if (datestr) {
4164 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4165 fe621944 2020-11-10 stsp if (n < 0) {
4166 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4167 fe621944 2020-11-10 stsp goto done;
4168 fe621944 2020-11-10 stsp }
4169 fe621944 2020-11-10 stsp outoff += n;
4170 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4171 fe621944 2020-11-10 stsp if (err)
4172 fe621944 2020-11-10 stsp goto done;
4173 abd2672a 2018-12-23 stsp }
4174 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4175 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4176 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4177 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4178 fe621944 2020-11-10 stsp if (n < 0) {
4179 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4180 fe621944 2020-11-10 stsp goto done;
4181 fe621944 2020-11-10 stsp }
4182 fe621944 2020-11-10 stsp outoff += n;
4183 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4184 fe621944 2020-11-10 stsp if (err)
4185 fe621944 2020-11-10 stsp goto done;
4186 abd2672a 2018-12-23 stsp }
4187 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4188 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4189 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4190 9f98ca05 2021-09-24 stsp int pn = 1;
4191 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4192 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4193 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4194 9f98ca05 2021-09-24 stsp if (err)
4195 9f98ca05 2021-09-24 stsp goto done;
4196 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4197 9f98ca05 2021-09-24 stsp if (n < 0) {
4198 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4199 9f98ca05 2021-09-24 stsp goto done;
4200 9f98ca05 2021-09-24 stsp }
4201 9f98ca05 2021-09-24 stsp outoff += n;
4202 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4203 9f98ca05 2021-09-24 stsp if (err)
4204 9f98ca05 2021-09-24 stsp goto done;
4205 9f98ca05 2021-09-24 stsp free(id_str);
4206 9f98ca05 2021-09-24 stsp id_str = NULL;
4207 9f98ca05 2021-09-24 stsp }
4208 9f98ca05 2021-09-24 stsp }
4209 9f98ca05 2021-09-24 stsp
4210 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4211 5943eee2 2019-08-13 stsp if (err)
4212 5943eee2 2019-08-13 stsp goto done;
4213 fe621944 2020-11-10 stsp s = logmsg;
4214 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4215 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4216 fe621944 2020-11-10 stsp if (n < 0) {
4217 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4218 fe621944 2020-11-10 stsp goto done;
4219 fe621944 2020-11-10 stsp }
4220 fe621944 2020-11-10 stsp outoff += n;
4221 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4222 fe621944 2020-11-10 stsp if (err)
4223 fe621944 2020-11-10 stsp goto done;
4224 abd2672a 2018-12-23 stsp }
4225 fe621944 2020-11-10 stsp
4226 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4227 0208f208 2020-05-05 stsp if (err)
4228 0208f208 2020-05-05 stsp goto done;
4229 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4230 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4231 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4232 fe621944 2020-11-10 stsp if (n < 0) {
4233 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4234 fe621944 2020-11-10 stsp goto done;
4235 fe621944 2020-11-10 stsp }
4236 fe621944 2020-11-10 stsp outoff += n;
4237 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4238 fe621944 2020-11-10 stsp if (err)
4239 fe621944 2020-11-10 stsp goto done;
4240 0208f208 2020-05-05 stsp free((char *)pe->path);
4241 0208f208 2020-05-05 stsp free(pe->data);
4242 0208f208 2020-05-05 stsp }
4243 fe621944 2020-11-10 stsp
4244 0208f208 2020-05-05 stsp fputc('\n', outfile);
4245 fe621944 2020-11-10 stsp outoff++;
4246 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4247 abd2672a 2018-12-23 stsp done:
4248 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4249 abd2672a 2018-12-23 stsp free(id_str);
4250 5943eee2 2019-08-13 stsp free(logmsg);
4251 8b473291 2019-02-21 stsp free(refs_str);
4252 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4253 7510f233 2020-08-09 stsp if (err) {
4254 ae6a6978 2020-08-09 stsp free(*line_offsets);
4255 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4256 ae6a6978 2020-08-09 stsp *nlines = 0;
4257 7510f233 2020-08-09 stsp }
4258 fe621944 2020-11-10 stsp return err;
4259 abd2672a 2018-12-23 stsp }
4260 abd2672a 2018-12-23 stsp
4261 abd2672a 2018-12-23 stsp static const struct got_error *
4262 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4263 26ed57b2 2018-05-19 stsp {
4264 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4265 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4266 15a94983 2018-12-23 stsp int obj_type;
4267 fe621944 2020-11-10 stsp
4268 fe621944 2020-11-10 stsp free(s->line_offsets);
4269 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4270 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4271 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4272 fe621944 2020-11-10 stsp s->nlines = 0;
4273 26ed57b2 2018-05-19 stsp
4274 511a516b 2018-05-19 stsp f = got_opentemp();
4275 48ae06ee 2018-10-18 stsp if (f == NULL) {
4276 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4277 48ae06ee 2018-10-18 stsp goto done;
4278 48ae06ee 2018-10-18 stsp }
4279 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4280 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4281 fb43ecf1 2019-02-11 stsp goto done;
4282 fb43ecf1 2019-02-11 stsp }
4283 48ae06ee 2018-10-18 stsp s->f = f;
4284 26ed57b2 2018-05-19 stsp
4285 15a94983 2018-12-23 stsp if (s->id1)
4286 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4287 15a94983 2018-12-23 stsp else
4288 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4289 15a94983 2018-12-23 stsp if (err)
4290 15a94983 2018-12-23 stsp goto done;
4291 15a94983 2018-12-23 stsp
4292 15a94983 2018-12-23 stsp switch (obj_type) {
4293 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4294 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4295 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4296 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4297 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4298 26ed57b2 2018-05-19 stsp break;
4299 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4300 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4301 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4302 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4304 26ed57b2 2018-05-19 stsp break;
4305 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4306 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4307 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4308 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4309 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4310 abd2672a 2018-12-23 stsp
4311 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4312 abd2672a 2018-12-23 stsp if (err)
4313 3ffacbe1 2020-02-02 tracey goto done;
4314 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4315 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4316 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4317 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4318 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4319 f44b1f58 2020-02-02 tracey if (err)
4320 f44b1f58 2020-02-02 tracey goto done;
4321 f44b1f58 2020-02-02 tracey } else {
4322 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4323 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4324 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4325 fe621944 2020-11-10 stsp err = write_commit_info(
4326 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4327 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4328 f44b1f58 2020-02-02 tracey if (err)
4329 f44b1f58 2020-02-02 tracey goto done;
4330 f5404e4e 2020-02-02 tracey break;
4331 15a087fe 2019-02-21 stsp }
4332 abd2672a 2018-12-23 stsp }
4333 abd2672a 2018-12-23 stsp }
4334 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4335 abd2672a 2018-12-23 stsp
4336 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4337 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4338 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4339 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4340 26ed57b2 2018-05-19 stsp break;
4341 abd2672a 2018-12-23 stsp }
4342 26ed57b2 2018-05-19 stsp default:
4343 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4344 48ae06ee 2018-10-18 stsp break;
4345 26ed57b2 2018-05-19 stsp }
4346 f44b1f58 2020-02-02 tracey if (err)
4347 f44b1f58 2020-02-02 tracey goto done;
4348 48ae06ee 2018-10-18 stsp done:
4349 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4350 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4351 48ae06ee 2018-10-18 stsp return err;
4352 48ae06ee 2018-10-18 stsp }
4353 26ed57b2 2018-05-19 stsp
4354 f5215bb9 2019-02-22 stsp static void
4355 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4356 f5215bb9 2019-02-22 stsp {
4357 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4358 f5215bb9 2019-02-22 stsp update_panels();
4359 f5215bb9 2019-02-22 stsp doupdate();
4360 f44b1f58 2020-02-02 tracey }
4361 f44b1f58 2020-02-02 tracey
4362 f44b1f58 2020-02-02 tracey static const struct got_error *
4363 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4364 f44b1f58 2020-02-02 tracey {
4365 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4366 f44b1f58 2020-02-02 tracey
4367 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4368 f44b1f58 2020-02-02 tracey return NULL;
4369 f44b1f58 2020-02-02 tracey }
4370 f44b1f58 2020-02-02 tracey
4371 f44b1f58 2020-02-02 tracey static const struct got_error *
4372 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4373 f44b1f58 2020-02-02 tracey {
4374 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4375 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4376 f44b1f58 2020-02-02 tracey int lineno;
4377 cb713507 2022-06-16 stsp char *line = NULL;
4378 826082fe 2020-12-10 stsp size_t linesize = 0;
4379 826082fe 2020-12-10 stsp ssize_t linelen;
4380 f44b1f58 2020-02-02 tracey
4381 f44b1f58 2020-02-02 tracey if (!view->searching) {
4382 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 f44b1f58 2020-02-02 tracey return NULL;
4384 f44b1f58 2020-02-02 tracey }
4385 f44b1f58 2020-02-02 tracey
4386 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4387 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4388 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4389 f44b1f58 2020-02-02 tracey else
4390 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4391 487cd7d2 2021-12-17 stsp } else
4392 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4393 f44b1f58 2020-02-02 tracey
4394 f44b1f58 2020-02-02 tracey while (1) {
4395 f44b1f58 2020-02-02 tracey off_t offset;
4396 f44b1f58 2020-02-02 tracey
4397 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4398 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4399 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4400 f44b1f58 2020-02-02 tracey break;
4401 f44b1f58 2020-02-02 tracey }
4402 f44b1f58 2020-02-02 tracey
4403 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4404 f44b1f58 2020-02-02 tracey lineno = 1;
4405 f44b1f58 2020-02-02 tracey else
4406 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4407 f44b1f58 2020-02-02 tracey }
4408 f44b1f58 2020-02-02 tracey
4409 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4410 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4411 f44b1f58 2020-02-02 tracey free(line);
4412 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4413 f44b1f58 2020-02-02 tracey }
4414 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4415 cb713507 2022-06-16 stsp if (linelen != -1) {
4416 cb713507 2022-06-16 stsp char *exstr;
4417 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4418 cb713507 2022-06-16 stsp if (err)
4419 cb713507 2022-06-16 stsp break;
4420 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4421 cb713507 2022-06-16 stsp &view->regmatch)) {
4422 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 cb713507 2022-06-16 stsp s->matched_line = lineno;
4424 cb713507 2022-06-16 stsp free(exstr);
4425 cb713507 2022-06-16 stsp break;
4426 cb713507 2022-06-16 stsp }
4427 cb713507 2022-06-16 stsp free(exstr);
4428 f44b1f58 2020-02-02 tracey }
4429 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4430 f44b1f58 2020-02-02 tracey lineno++;
4431 f44b1f58 2020-02-02 tracey else
4432 f44b1f58 2020-02-02 tracey lineno--;
4433 f44b1f58 2020-02-02 tracey }
4434 826082fe 2020-12-10 stsp free(line);
4435 f44b1f58 2020-02-02 tracey
4436 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4437 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4438 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4439 f44b1f58 2020-02-02 tracey }
4440 f44b1f58 2020-02-02 tracey
4441 145b6838 2022-06-16 stsp return err;
4442 f5215bb9 2019-02-22 stsp }
4443 f5215bb9 2019-02-22 stsp
4444 48ae06ee 2018-10-18 stsp static const struct got_error *
4445 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4446 b72706c3 2022-06-01 stsp {
4447 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4448 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4449 b72706c3 2022-06-01 stsp
4450 b72706c3 2022-06-01 stsp free(s->id1);
4451 b72706c3 2022-06-01 stsp s->id1 = NULL;
4452 b72706c3 2022-06-01 stsp free(s->id2);
4453 b72706c3 2022-06-01 stsp s->id2 = NULL;
4454 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4455 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4456 b72706c3 2022-06-01 stsp s->f = NULL;
4457 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4458 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4459 b72706c3 2022-06-01 stsp s->f1 = NULL;
4460 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4461 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4462 b72706c3 2022-06-01 stsp s->f2 = NULL;
4463 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4464 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4465 f9d37699 2022-06-28 stsp s->fd1 = -1;
4466 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4467 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4468 f9d37699 2022-06-28 stsp s->fd2 = -1;
4469 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4470 b72706c3 2022-06-01 stsp free(s->line_offsets);
4471 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4472 b72706c3 2022-06-01 stsp s->nlines = 0;
4473 b72706c3 2022-06-01 stsp return err;
4474 b72706c3 2022-06-01 stsp }
4475 b72706c3 2022-06-01 stsp
4476 b72706c3 2022-06-01 stsp static const struct got_error *
4477 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4478 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4479 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4480 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4481 48ae06ee 2018-10-18 stsp {
4482 48ae06ee 2018-10-18 stsp const struct got_error *err;
4483 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4484 5dc9f4bc 2018-08-04 stsp
4485 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4486 f9d37699 2022-06-28 stsp s->fd1 = -1;
4487 f9d37699 2022-06-28 stsp s->fd2 = -1;
4488 b72706c3 2022-06-01 stsp
4489 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4490 15a94983 2018-12-23 stsp int type1, type2;
4491 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4492 15a94983 2018-12-23 stsp if (err)
4493 15a94983 2018-12-23 stsp return err;
4494 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4495 15a94983 2018-12-23 stsp if (err)
4496 15a94983 2018-12-23 stsp return err;
4497 15a94983 2018-12-23 stsp
4498 15a94983 2018-12-23 stsp if (type1 != type2)
4499 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4500 15a94983 2018-12-23 stsp }
4501 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4502 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4503 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4504 f44b1f58 2020-02-02 tracey s->repo = repo;
4505 f44b1f58 2020-02-02 tracey s->id1 = id1;
4506 f44b1f58 2020-02-02 tracey s->id2 = id2;
4507 3dbaef42 2020-11-24 stsp s->label1 = label1;
4508 3dbaef42 2020-11-24 stsp s->label2 = label2;
4509 48ae06ee 2018-10-18 stsp
4510 15a94983 2018-12-23 stsp if (id1) {
4511 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4512 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4513 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4514 48ae06ee 2018-10-18 stsp } else
4515 5465d566 2020-02-01 tracey s->id1 = NULL;
4516 48ae06ee 2018-10-18 stsp
4517 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4518 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4519 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4520 b72706c3 2022-06-01 stsp goto done;
4521 48ae06ee 2018-10-18 stsp }
4522 b72706c3 2022-06-01 stsp
4523 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4524 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4525 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4526 a00719e9 2022-06-17 stsp goto done;
4527 a00719e9 2022-06-17 stsp }
4528 a00719e9 2022-06-17 stsp
4529 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4530 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4531 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4532 b72706c3 2022-06-01 stsp goto done;
4533 b72706c3 2022-06-01 stsp }
4534 b72706c3 2022-06-01 stsp
4535 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4536 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4537 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4538 f9d37699 2022-06-28 stsp goto done;
4539 f9d37699 2022-06-28 stsp }
4540 f9d37699 2022-06-28 stsp
4541 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4542 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4543 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4544 f9d37699 2022-06-28 stsp goto done;
4545 f9d37699 2022-06-28 stsp }
4546 f9d37699 2022-06-28 stsp
4547 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4548 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4549 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4550 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4551 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4552 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4553 5465d566 2020-02-01 tracey s->repo = repo;
4554 6d17833f 2019-11-08 stsp
4555 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4556 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4558 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4559 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4560 6d17833f 2019-11-08 stsp if (err)
4561 b72706c3 2022-06-01 stsp goto done;
4562 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4563 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4565 b72706c3 2022-06-01 stsp if (err)
4566 b72706c3 2022-06-01 stsp goto done;
4567 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4568 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4569 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4570 b72706c3 2022-06-01 stsp if (err)
4571 b72706c3 2022-06-01 stsp goto done;
4572 6d17833f 2019-11-08 stsp
4573 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4574 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4575 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4576 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4577 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4578 b72706c3 2022-06-01 stsp if (err)
4579 b72706c3 2022-06-01 stsp goto done;
4580 11b20872 2019-11-08 stsp
4581 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4582 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4583 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4584 b72706c3 2022-06-01 stsp if (err)
4585 b72706c3 2022-06-01 stsp goto done;
4586 11b20872 2019-11-08 stsp
4587 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4588 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4589 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4590 b72706c3 2022-06-01 stsp if (err)
4591 b72706c3 2022-06-01 stsp goto done;
4592 6d17833f 2019-11-08 stsp }
4593 5dc9f4bc 2018-08-04 stsp
4594 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4595 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4596 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4597 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4598 f5215bb9 2019-02-22 stsp
4599 5465d566 2020-02-01 tracey err = create_diff(s);
4600 48ae06ee 2018-10-18 stsp
4601 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4602 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4603 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4604 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4605 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4606 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4607 b72706c3 2022-06-01 stsp done:
4608 b72706c3 2022-06-01 stsp if (err)
4609 b72706c3 2022-06-01 stsp close_diff_view(view);
4610 e5a0f69f 2018-08-18 stsp return err;
4611 5dc9f4bc 2018-08-04 stsp }
4612 5dc9f4bc 2018-08-04 stsp
4613 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4614 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4615 5dc9f4bc 2018-08-04 stsp {
4616 a3404814 2018-09-02 stsp const struct got_error *err;
4617 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4618 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4619 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4620 a3404814 2018-09-02 stsp
4621 a3404814 2018-09-02 stsp if (s->id1) {
4622 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4623 a3404814 2018-09-02 stsp if (err)
4624 a3404814 2018-09-02 stsp return err;
4625 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4626 3dbaef42 2020-11-24 stsp } else
4627 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4628 3dbaef42 2020-11-24 stsp
4629 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4630 a3404814 2018-09-02 stsp if (err)
4631 a3404814 2018-09-02 stsp return err;
4632 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4633 26ed57b2 2018-05-19 stsp
4634 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4635 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4636 a3404814 2018-09-02 stsp free(id_str1);
4637 a3404814 2018-09-02 stsp free(id_str2);
4638 a3404814 2018-09-02 stsp return err;
4639 a3404814 2018-09-02 stsp }
4640 a3404814 2018-09-02 stsp free(id_str1);
4641 a3404814 2018-09-02 stsp free(id_str2);
4642 a3404814 2018-09-02 stsp
4643 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4644 267bb3b8 2021-08-01 stsp free(header);
4645 267bb3b8 2021-08-01 stsp return err;
4646 15a087fe 2019-02-21 stsp }
4647 15a087fe 2019-02-21 stsp
4648 15a087fe 2019-02-21 stsp static const struct got_error *
4649 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4650 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4651 15a087fe 2019-02-21 stsp {
4652 d7a04538 2019-02-21 stsp const struct got_error *err;
4653 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4654 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4655 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4656 15a087fe 2019-02-21 stsp
4657 15a087fe 2019-02-21 stsp free(s->id2);
4658 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4659 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4660 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4661 15a087fe 2019-02-21 stsp
4662 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4663 d7a04538 2019-02-21 stsp if (err)
4664 d7a04538 2019-02-21 stsp return err;
4665 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4666 15a087fe 2019-02-21 stsp free(s->id1);
4667 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4668 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4669 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4670 15a087fe 2019-02-21 stsp return NULL;
4671 0cf4efb1 2018-09-29 stsp }
4672 0cf4efb1 2018-09-29 stsp
4673 0cf4efb1 2018-09-29 stsp static const struct got_error *
4674 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4675 917d79a7 2022-07-01 stsp {
4676 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4677 917d79a7 2022-07-01 stsp
4678 917d79a7 2022-07-01 stsp view->count = 0;
4679 917d79a7 2022-07-01 stsp wclear(view->window);
4680 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4681 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4682 917d79a7 2022-07-01 stsp s->matched_line = 0;
4683 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4684 917d79a7 2022-07-01 stsp return create_diff(s);
4685 917d79a7 2022-07-01 stsp }
4686 917d79a7 2022-07-01 stsp
4687 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4688 c0f61fa4 2022-07-11 mark int, int, int);
4689 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4690 c0f61fa4 2022-07-11 mark int, int);
4691 c0f61fa4 2022-07-11 mark
4692 917d79a7 2022-07-01 stsp static const struct got_error *
4693 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4694 e5a0f69f 2018-08-18 stsp {
4695 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4696 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4697 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4698 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4699 826082fe 2020-12-10 stsp char *line = NULL;
4700 826082fe 2020-12-10 stsp size_t linesize = 0;
4701 826082fe 2020-12-10 stsp ssize_t linelen;
4702 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4703 e5a0f69f 2018-08-18 stsp
4704 e5a0f69f 2018-08-18 stsp switch (ch) {
4705 145b6838 2022-06-16 stsp case '0':
4706 145b6838 2022-06-16 stsp view->x = 0;
4707 145b6838 2022-06-16 stsp break;
4708 145b6838 2022-06-16 stsp case '$':
4709 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4710 640cd7ff 2022-06-22 mark view->count = 0;
4711 145b6838 2022-06-16 stsp break;
4712 145b6838 2022-06-16 stsp case KEY_RIGHT:
4713 145b6838 2022-06-16 stsp case 'l':
4714 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4715 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4716 640cd7ff 2022-06-22 mark else
4717 640cd7ff 2022-06-22 mark view->count = 0;
4718 145b6838 2022-06-16 stsp break;
4719 145b6838 2022-06-16 stsp case KEY_LEFT:
4720 145b6838 2022-06-16 stsp case 'h':
4721 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4722 640cd7ff 2022-06-22 mark if (view->x <= 0)
4723 640cd7ff 2022-06-22 mark view->count = 0;
4724 145b6838 2022-06-16 stsp break;
4725 64453f7e 2020-11-21 stsp case 'a':
4726 3dbaef42 2020-11-24 stsp case 'w':
4727 3dbaef42 2020-11-24 stsp if (ch == 'a')
4728 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4729 3dbaef42 2020-11-24 stsp if (ch == 'w')
4730 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4731 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4732 912a3f79 2021-08-30 j break;
4733 912a3f79 2021-08-30 j case 'g':
4734 912a3f79 2021-08-30 j case KEY_HOME:
4735 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4736 640cd7ff 2022-06-22 mark view->count = 0;
4737 64453f7e 2020-11-21 stsp break;
4738 912a3f79 2021-08-30 j case 'G':
4739 912a3f79 2021-08-30 j case KEY_END:
4740 640cd7ff 2022-06-22 mark view->count = 0;
4741 912a3f79 2021-08-30 j if (s->eof)
4742 912a3f79 2021-08-30 j break;
4743 912a3f79 2021-08-30 j
4744 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4745 912a3f79 2021-08-30 j s->eof = 1;
4746 912a3f79 2021-08-30 j break;
4747 1e37a5c2 2019-05-12 jcs case 'k':
4748 1e37a5c2 2019-05-12 jcs case KEY_UP:
4749 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4750 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4751 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4752 640cd7ff 2022-06-22 mark else
4753 640cd7ff 2022-06-22 mark view->count = 0;
4754 1e37a5c2 2019-05-12 jcs break;
4755 83cc4199 2022-06-13 stsp case CTRL('u'):
4756 33c3719a 2022-06-15 stsp case 'u':
4757 83cc4199 2022-06-13 stsp nscroll /= 2;
4758 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4759 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4760 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4761 61417565 2022-06-20 mark case 'b':
4762 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4763 640cd7ff 2022-06-22 mark view->count = 0;
4764 26ed57b2 2018-05-19 stsp break;
4765 640cd7ff 2022-06-22 mark }
4766 1e37a5c2 2019-05-12 jcs i = 0;
4767 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4768 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4769 1e37a5c2 2019-05-12 jcs break;
4770 1e37a5c2 2019-05-12 jcs case 'j':
4771 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4772 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4773 1e37a5c2 2019-05-12 jcs if (!s->eof)
4774 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4775 640cd7ff 2022-06-22 mark else
4776 640cd7ff 2022-06-22 mark view->count = 0;
4777 1e37a5c2 2019-05-12 jcs break;
4778 83cc4199 2022-06-13 stsp case CTRL('d'):
4779 33c3719a 2022-06-15 stsp case 'd':
4780 83cc4199 2022-06-13 stsp nscroll /= 2;
4781 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4782 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4783 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4784 61417565 2022-06-20 mark case 'f':
4785 1e37a5c2 2019-05-12 jcs case ' ':
4786 640cd7ff 2022-06-22 mark if (s->eof) {
4787 640cd7ff 2022-06-22 mark view->count = 0;
4788 1e37a5c2 2019-05-12 jcs break;
4789 640cd7ff 2022-06-22 mark }
4790 1e37a5c2 2019-05-12 jcs i = 0;
4791 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4792 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4793 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4794 826082fe 2020-12-10 stsp if (linelen == -1) {
4795 826082fe 2020-12-10 stsp if (feof(s->f)) {
4796 826082fe 2020-12-10 stsp s->eof = 1;
4797 826082fe 2020-12-10 stsp } else
4798 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4799 34bc9ec9 2019-02-22 stsp break;
4800 826082fe 2020-12-10 stsp }
4801 1e37a5c2 2019-05-12 jcs }
4802 826082fe 2020-12-10 stsp free(line);
4803 1e37a5c2 2019-05-12 jcs break;
4804 1e37a5c2 2019-05-12 jcs case '[':
4805 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4806 1e37a5c2 2019-05-12 jcs s->diff_context--;
4807 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4808 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4809 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4810 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4811 27829c9e 2020-11-21 stsp s->nlines) {
4812 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4813 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4814 27829c9e 2020-11-21 stsp }
4815 640cd7ff 2022-06-22 mark } else
4816 640cd7ff 2022-06-22 mark view->count = 0;
4817 1e37a5c2 2019-05-12 jcs break;
4818 1e37a5c2 2019-05-12 jcs case ']':
4819 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4820 1e37a5c2 2019-05-12 jcs s->diff_context++;
4821 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4824 640cd7ff 2022-06-22 mark } else
4825 640cd7ff 2022-06-22 mark view->count = 0;
4826 1e37a5c2 2019-05-12 jcs break;
4827 1e37a5c2 2019-05-12 jcs case '<':
4828 1e37a5c2 2019-05-12 jcs case ',':
4829 c0f61fa4 2022-07-11 mark up = 1;
4830 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4831 c0f61fa4 2022-07-11 mark case '>':
4832 c0f61fa4 2022-07-11 mark case '.':
4833 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4834 640cd7ff 2022-06-22 mark view->count = 0;
4835 48ae06ee 2018-10-18 stsp break;
4836 640cd7ff 2022-06-22 mark }
4837 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4838 6524637e 2019-02-21 stsp
4839 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4840 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4841 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4842 15a087fe 2019-02-21 stsp
4843 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4844 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4845 c0f61fa4 2022-07-11 mark if (err)
4846 c0f61fa4 2022-07-11 mark break;
4847 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4848 15a087fe 2019-02-21 stsp
4849 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4850 c0f61fa4 2022-07-11 mark break;
4851 15a087fe 2019-02-21 stsp
4852 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4853 c0f61fa4 2022-07-11 mark if (err)
4854 c0f61fa4 2022-07-11 mark break;
4855 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4856 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4857 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4858 5e224a3e 2019-02-22 stsp
4859 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4860 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4861 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4862 c0f61fa4 2022-07-11 mark
4863 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4864 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4865 c0f61fa4 2022-07-11 mark if (err)
4866 c0f61fa4 2022-07-11 mark break;
4867 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4868 5a8b5076 2020-12-05 stsp
4869 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4870 c0f61fa4 2022-07-11 mark break;
4871 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4872 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4873 c0f61fa4 2022-07-11 mark bs->selected_line);
4874 c0f61fa4 2022-07-11 mark if (id == NULL)
4875 c0f61fa4 2022-07-11 mark break;
4876 15a087fe 2019-02-21 stsp
4877 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4878 c0f61fa4 2022-07-11 mark break;
4879 15a087fe 2019-02-21 stsp
4880 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4881 c0f61fa4 2022-07-11 mark if (err)
4882 c0f61fa4 2022-07-11 mark break;
4883 c0f61fa4 2022-07-11 mark }
4884 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4885 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4886 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4887 145b6838 2022-06-16 stsp view->x = 0;
4888 1e37a5c2 2019-05-12 jcs
4889 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4890 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4891 1e37a5c2 2019-05-12 jcs break;
4892 1e37a5c2 2019-05-12 jcs default:
4893 640cd7ff 2022-06-22 mark view->count = 0;
4894 1e37a5c2 2019-05-12 jcs break;
4895 26ed57b2 2018-05-19 stsp }
4896 e5a0f69f 2018-08-18 stsp
4897 bcbd79e2 2018-08-19 stsp return err;
4898 26ed57b2 2018-05-19 stsp }
4899 26ed57b2 2018-05-19 stsp
4900 4ed7e80c 2018-05-20 stsp static const struct got_error *
4901 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4902 9f7d7167 2018-04-29 stsp {
4903 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4904 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4905 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4906 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4907 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4908 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4909 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4910 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4911 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4912 3dbaef42 2020-11-24 stsp const char *errstr;
4913 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4914 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4915 70ac5f84 2019-03-28 stsp
4916 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4917 26ed57b2 2018-05-19 stsp switch (ch) {
4918 64453f7e 2020-11-21 stsp case 'a':
4919 64453f7e 2020-11-21 stsp force_text_diff = 1;
4920 3dbaef42 2020-11-24 stsp break;
4921 3dbaef42 2020-11-24 stsp case 'C':
4922 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4923 3dbaef42 2020-11-24 stsp &errstr);
4924 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4925 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4926 5a20d08d 2022-02-09 op errstr, errstr);
4927 64453f7e 2020-11-21 stsp break;
4928 09b5bff8 2020-02-23 naddy case 'r':
4929 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4930 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4931 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4932 09b5bff8 2020-02-23 naddy optarg);
4933 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4934 09b5bff8 2020-02-23 naddy break;
4935 3dbaef42 2020-11-24 stsp case 'w':
4936 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4937 3dbaef42 2020-11-24 stsp break;
4938 26ed57b2 2018-05-19 stsp default:
4939 17020d27 2019-03-07 stsp usage_diff();
4940 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4941 26ed57b2 2018-05-19 stsp }
4942 26ed57b2 2018-05-19 stsp }
4943 26ed57b2 2018-05-19 stsp
4944 26ed57b2 2018-05-19 stsp argc -= optind;
4945 26ed57b2 2018-05-19 stsp argv += optind;
4946 26ed57b2 2018-05-19 stsp
4947 26ed57b2 2018-05-19 stsp if (argc == 0) {
4948 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4949 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4950 15a94983 2018-12-23 stsp id_str1 = argv[0];
4951 15a94983 2018-12-23 stsp id_str2 = argv[1];
4952 26ed57b2 2018-05-19 stsp } else
4953 26ed57b2 2018-05-19 stsp usage_diff();
4954 eb6600df 2019-01-04 stsp
4955 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4956 0ae84acc 2022-06-15 tracey if (error)
4957 0ae84acc 2022-06-15 tracey goto done;
4958 0ae84acc 2022-06-15 tracey
4959 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4960 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4961 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4962 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4963 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4964 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4965 c156c7a4 2020-12-18 stsp goto done;
4966 a273ac94 2020-02-23 naddy if (worktree)
4967 a273ac94 2020-02-23 naddy repo_path =
4968 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4969 a273ac94 2020-02-23 naddy else
4970 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4971 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4972 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4973 c156c7a4 2020-12-18 stsp goto done;
4974 c156c7a4 2020-12-18 stsp }
4975 a273ac94 2020-02-23 naddy }
4976 a273ac94 2020-02-23 naddy
4977 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4978 eb6600df 2019-01-04 stsp if (error)
4979 eb6600df 2019-01-04 stsp goto done;
4980 26ed57b2 2018-05-19 stsp
4981 a273ac94 2020-02-23 naddy init_curses();
4982 a273ac94 2020-02-23 naddy
4983 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4984 51a10b52 2020-12-26 stsp if (error)
4985 51a10b52 2020-12-26 stsp goto done;
4986 51a10b52 2020-12-26 stsp
4987 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4988 26ed57b2 2018-05-19 stsp if (error)
4989 26ed57b2 2018-05-19 stsp goto done;
4990 26ed57b2 2018-05-19 stsp
4991 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4992 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4993 26ed57b2 2018-05-19 stsp if (error)
4994 26ed57b2 2018-05-19 stsp goto done;
4995 26ed57b2 2018-05-19 stsp
4996 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4997 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4998 26ed57b2 2018-05-19 stsp if (error)
4999 26ed57b2 2018-05-19 stsp goto done;
5000 26ed57b2 2018-05-19 stsp
5001 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5002 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5003 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5004 ea5e7bb5 2018-08-01 stsp goto done;
5005 ea5e7bb5 2018-08-01 stsp }
5006 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5007 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5008 5dc9f4bc 2018-08-04 stsp if (error)
5009 5dc9f4bc 2018-08-04 stsp goto done;
5010 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5011 26ed57b2 2018-05-19 stsp done:
5012 3dbaef42 2020-11-24 stsp free(label1);
5013 3dbaef42 2020-11-24 stsp free(label2);
5014 c02c541e 2019-03-29 stsp free(repo_path);
5015 a273ac94 2020-02-23 naddy free(cwd);
5016 1d0f4054 2021-06-17 stsp if (repo) {
5017 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5018 1d0f4054 2021-06-17 stsp if (error == NULL)
5019 1d0f4054 2021-06-17 stsp error = close_err;
5020 1d0f4054 2021-06-17 stsp }
5021 a273ac94 2020-02-23 naddy if (worktree)
5022 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5023 0ae84acc 2022-06-15 tracey if (pack_fds) {
5024 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5025 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5026 0ae84acc 2022-06-15 tracey if (error == NULL)
5027 0ae84acc 2022-06-15 tracey error = pack_err;
5028 0ae84acc 2022-06-15 tracey }
5029 51a10b52 2020-12-26 stsp tog_free_refs();
5030 26ed57b2 2018-05-19 stsp return error;
5031 9f7d7167 2018-04-29 stsp }
5032 9f7d7167 2018-04-29 stsp
5033 4ed7e80c 2018-05-20 stsp __dead static void
5034 9f7d7167 2018-04-29 stsp usage_blame(void)
5035 9f7d7167 2018-04-29 stsp {
5036 80ddbec8 2018-04-29 stsp endwin();
5037 87411fa9 2022-06-16 stsp fprintf(stderr,
5038 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5039 9f7d7167 2018-04-29 stsp getprogname());
5040 9f7d7167 2018-04-29 stsp exit(1);
5041 9f7d7167 2018-04-29 stsp }
5042 84451b3e 2018-07-10 stsp
5043 84451b3e 2018-07-10 stsp struct tog_blame_line {
5044 84451b3e 2018-07-10 stsp int annotated;
5045 84451b3e 2018-07-10 stsp struct got_object_id *id;
5046 84451b3e 2018-07-10 stsp };
5047 9f7d7167 2018-04-29 stsp
5048 4ed7e80c 2018-05-20 stsp static const struct got_error *
5049 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5050 84451b3e 2018-07-10 stsp {
5051 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5052 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5053 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5054 84451b3e 2018-07-10 stsp const struct got_error *err;
5055 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5056 826082fe 2020-12-10 stsp char *line = NULL;
5057 826082fe 2020-12-10 stsp size_t linesize = 0;
5058 826082fe 2020-12-10 stsp ssize_t linelen;
5059 84451b3e 2018-07-10 stsp wchar_t *wline;
5060 27a741e5 2019-09-11 stsp int width;
5061 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5062 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5063 ab089a2a 2018-07-12 stsp char *id_str;
5064 11b20872 2019-11-08 stsp struct tog_color *tc;
5065 ab089a2a 2018-07-12 stsp
5066 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5067 ab089a2a 2018-07-12 stsp if (err)
5068 ab089a2a 2018-07-12 stsp return err;
5069 84451b3e 2018-07-10 stsp
5070 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5071 f7d12f7e 2018-08-01 stsp werase(view->window);
5072 84451b3e 2018-07-10 stsp
5073 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5074 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5075 ab089a2a 2018-07-12 stsp free(id_str);
5076 ab089a2a 2018-07-12 stsp return err;
5077 ab089a2a 2018-07-12 stsp }
5078 ab089a2a 2018-07-12 stsp
5079 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5080 ab089a2a 2018-07-12 stsp free(line);
5081 2550e4c3 2018-07-13 stsp line = NULL;
5082 1cae65b4 2019-09-22 stsp if (err)
5083 1cae65b4 2019-09-22 stsp return err;
5084 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5085 a3404814 2018-09-02 stsp wstandout(view->window);
5086 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5087 11b20872 2019-11-08 stsp if (tc)
5088 11b20872 2019-11-08 stsp wattr_on(view->window,
5089 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5090 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5091 11b20872 2019-11-08 stsp if (tc)
5092 11b20872 2019-11-08 stsp wattr_off(view->window,
5093 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5094 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5095 a3404814 2018-09-02 stsp wstandend(view->window);
5096 2550e4c3 2018-07-13 stsp free(wline);
5097 2550e4c3 2018-07-13 stsp wline = NULL;
5098 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5099 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5100 ab089a2a 2018-07-12 stsp
5101 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5102 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5103 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5104 ab089a2a 2018-07-12 stsp free(id_str);
5105 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5106 ab089a2a 2018-07-12 stsp }
5107 ab089a2a 2018-07-12 stsp free(id_str);
5108 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5109 3f60a8ef 2018-07-10 stsp free(line);
5110 2550e4c3 2018-07-13 stsp line = NULL;
5111 3f60a8ef 2018-07-10 stsp if (err)
5112 3f60a8ef 2018-07-10 stsp return err;
5113 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5114 2550e4c3 2018-07-13 stsp free(wline);
5115 2550e4c3 2018-07-13 stsp wline = NULL;
5116 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5117 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5118 3f60a8ef 2018-07-10 stsp
5119 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5120 145b6838 2022-06-16 stsp view->maxx = 0;
5121 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5122 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5123 826082fe 2020-12-10 stsp if (linelen == -1) {
5124 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5125 826082fe 2020-12-10 stsp s->eof = 1;
5126 826082fe 2020-12-10 stsp break;
5127 826082fe 2020-12-10 stsp }
5128 84451b3e 2018-07-10 stsp free(line);
5129 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5130 84451b3e 2018-07-10 stsp }
5131 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5132 826082fe 2020-12-10 stsp continue;
5133 1853e0f4 2022-06-16 stsp
5134 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5135 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5136 1853e0f4 2022-06-16 stsp if (err) {
5137 1853e0f4 2022-06-16 stsp free(line);
5138 1853e0f4 2022-06-16 stsp return err;
5139 1853e0f4 2022-06-16 stsp }
5140 1853e0f4 2022-06-16 stsp free(wline);
5141 1853e0f4 2022-06-16 stsp wline = NULL;
5142 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5143 84451b3e 2018-07-10 stsp
5144 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5145 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5146 b700b5d6 2018-07-10 stsp
5147 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5148 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5149 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5150 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5151 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5152 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5153 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5154 8d0fe45a 2019-08-12 stsp char *id_str;
5155 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5156 87411fa9 2022-06-16 stsp blame_line->id);
5157 8d0fe45a 2019-08-12 stsp if (err) {
5158 8d0fe45a 2019-08-12 stsp free(line);
5159 8d0fe45a 2019-08-12 stsp return err;
5160 8d0fe45a 2019-08-12 stsp }
5161 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5162 11b20872 2019-11-08 stsp if (tc)
5163 11b20872 2019-11-08 stsp wattr_on(view->window,
5164 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5165 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5166 11b20872 2019-11-08 stsp if (tc)
5167 11b20872 2019-11-08 stsp wattr_off(view->window,
5168 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5169 8d0fe45a 2019-08-12 stsp free(id_str);
5170 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5171 8d0fe45a 2019-08-12 stsp } else {
5172 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5173 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5174 84451b3e 2018-07-10 stsp }
5175 ee41ec32 2018-07-10 stsp } else {
5176 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5177 ee41ec32 2018-07-10 stsp prev_id = NULL;
5178 ee41ec32 2018-07-10 stsp }
5179 84451b3e 2018-07-10 stsp
5180 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5181 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5182 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5183 27a741e5 2019-09-11 stsp
5184 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5185 41605754 2020-11-12 stsp width = 9;
5186 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5187 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5188 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5189 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5190 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5191 41605754 2020-11-12 stsp if (err) {
5192 41605754 2020-11-12 stsp free(line);
5193 41605754 2020-11-12 stsp return err;
5194 41605754 2020-11-12 stsp }
5195 41605754 2020-11-12 stsp width += 9;
5196 41605754 2020-11-12 stsp } else {
5197 4cb9d869 2022-06-16 stsp int skip;
5198 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5199 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5200 4cb9d869 2022-06-16 stsp if (err) {
5201 4cb9d869 2022-06-16 stsp free(line);
5202 4cb9d869 2022-06-16 stsp return err;
5203 145b6838 2022-06-16 stsp }
5204 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5205 a75b3e2d 2022-06-16 stsp width += 9;
5206 41605754 2020-11-12 stsp free(wline);
5207 41605754 2020-11-12 stsp wline = NULL;
5208 41605754 2020-11-12 stsp }
5209 41605754 2020-11-12 stsp
5210 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5211 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5212 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5213 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5214 84451b3e 2018-07-10 stsp }
5215 826082fe 2020-12-10 stsp free(line);
5216 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5217 84451b3e 2018-07-10 stsp
5218 9b058f45 2022-06-30 mark view_border(view);
5219 84451b3e 2018-07-10 stsp
5220 84451b3e 2018-07-10 stsp return NULL;
5221 84451b3e 2018-07-10 stsp }
5222 84451b3e 2018-07-10 stsp
5223 84451b3e 2018-07-10 stsp static const struct got_error *
5224 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5225 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5226 84451b3e 2018-07-10 stsp {
5227 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5228 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5229 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5230 1a76625f 2018-10-22 stsp int errcode;
5231 84451b3e 2018-07-10 stsp
5232 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5233 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5234 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5235 84451b3e 2018-07-10 stsp
5236 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5237 1a76625f 2018-10-22 stsp if (errcode)
5238 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5239 84451b3e 2018-07-10 stsp
5240 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5241 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5242 d68a0a7d 2018-07-10 stsp goto done;
5243 d68a0a7d 2018-07-10 stsp }
5244 d68a0a7d 2018-07-10 stsp
5245 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5246 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5247 d68a0a7d 2018-07-10 stsp
5248 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5249 d68a0a7d 2018-07-10 stsp if (line->annotated)
5250 d68a0a7d 2018-07-10 stsp goto done;
5251 d68a0a7d 2018-07-10 stsp
5252 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5253 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5254 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5255 84451b3e 2018-07-10 stsp goto done;
5256 84451b3e 2018-07-10 stsp }
5257 84451b3e 2018-07-10 stsp line->annotated = 1;
5258 84451b3e 2018-07-10 stsp done:
5259 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5260 1a76625f 2018-10-22 stsp if (errcode)
5261 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5262 84451b3e 2018-07-10 stsp return err;
5263 84451b3e 2018-07-10 stsp }
5264 84451b3e 2018-07-10 stsp
5265 84451b3e 2018-07-10 stsp static void *
5266 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5267 84451b3e 2018-07-10 stsp {
5268 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5269 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5270 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5271 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5272 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5273 1b484788 2022-06-28 tracey
5274 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5275 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5276 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5277 e6e73e55 2022-06-30 tracey
5278 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5279 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5280 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5281 e6e73e55 2022-06-30 tracey goto done;
5282 e6e73e55 2022-06-30 tracey }
5283 18430de3 2018-07-10 stsp
5284 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5285 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5286 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5287 e6e73e55 2022-06-30 tracey goto done;
5288 e6e73e55 2022-06-30 tracey }
5289 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5290 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5291 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5292 e6e73e55 2022-06-30 tracey goto done;
5293 e6e73e55 2022-06-30 tracey }
5294 e6e73e55 2022-06-30 tracey
5295 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5296 61266923 2020-01-14 stsp if (err)
5297 e6e73e55 2022-06-30 tracey goto done;
5298 61266923 2020-01-14 stsp
5299 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5300 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5301 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5302 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5303 fc06ba56 2019-08-22 stsp err = NULL;
5304 18430de3 2018-07-10 stsp
5305 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5306 e6e73e55 2022-06-30 tracey if (errcode) {
5307 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5308 e6e73e55 2022-06-30 tracey goto done;
5309 e6e73e55 2022-06-30 tracey }
5310 18430de3 2018-07-10 stsp
5311 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5312 1d0f4054 2021-06-17 stsp if (err == NULL)
5313 1d0f4054 2021-06-17 stsp err = close_err;
5314 c9beca56 2018-07-22 stsp ta->repo = NULL;
5315 c9beca56 2018-07-22 stsp *ta->complete = 1;
5316 18430de3 2018-07-10 stsp
5317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5318 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5319 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5320 18430de3 2018-07-10 stsp
5321 e6e73e55 2022-06-30 tracey done:
5322 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5323 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5324 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5325 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5326 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5327 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5328 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5329 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5330 1b484788 2022-06-28 tracey
5331 18430de3 2018-07-10 stsp return (void *)err;
5332 84451b3e 2018-07-10 stsp }
5333 84451b3e 2018-07-10 stsp
5334 245d91c1 2018-07-12 stsp static struct got_object_id *
5335 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5336 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5337 245d91c1 2018-07-12 stsp {
5338 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5339 8d0fe45a 2019-08-12 stsp
5340 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5341 8d0fe45a 2019-08-12 stsp return NULL;
5342 b880a918 2018-07-10 stsp
5343 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5344 c0f61fa4 2022-07-11 mark if (!line->annotated)
5345 c0f61fa4 2022-07-11 mark return NULL;
5346 c0f61fa4 2022-07-11 mark
5347 c0f61fa4 2022-07-11 mark return line->id;
5348 c0f61fa4 2022-07-11 mark }
5349 c0f61fa4 2022-07-11 mark
5350 c0f61fa4 2022-07-11 mark static struct got_object_id *
5351 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5352 c0f61fa4 2022-07-11 mark int lineno)
5353 c0f61fa4 2022-07-11 mark {
5354 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5355 c0f61fa4 2022-07-11 mark
5356 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5357 c0f61fa4 2022-07-11 mark return NULL;
5358 c0f61fa4 2022-07-11 mark
5359 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5360 245d91c1 2018-07-12 stsp if (!line->annotated)
5361 245d91c1 2018-07-12 stsp return NULL;
5362 245d91c1 2018-07-12 stsp
5363 245d91c1 2018-07-12 stsp return line->id;
5364 b880a918 2018-07-10 stsp }
5365 245d91c1 2018-07-12 stsp
5366 b880a918 2018-07-10 stsp static const struct got_error *
5367 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5368 a70480e0 2018-06-23 stsp {
5369 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5370 245d91c1 2018-07-12 stsp int i;
5371 245d91c1 2018-07-12 stsp
5372 245d91c1 2018-07-12 stsp if (blame->thread) {
5373 1a76625f 2018-10-22 stsp int errcode;
5374 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5375 1a76625f 2018-10-22 stsp if (errcode)
5376 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5377 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5378 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5379 1a76625f 2018-10-22 stsp if (errcode)
5380 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5381 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5382 1a76625f 2018-10-22 stsp if (errcode)
5383 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5384 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5385 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5386 245d91c1 2018-07-12 stsp err = NULL;
5387 245d91c1 2018-07-12 stsp blame->thread = NULL;
5388 245d91c1 2018-07-12 stsp }
5389 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5390 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5391 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5392 1d0f4054 2021-06-17 stsp if (err == NULL)
5393 1d0f4054 2021-06-17 stsp err = close_err;
5394 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5395 245d91c1 2018-07-12 stsp }
5396 245d91c1 2018-07-12 stsp if (blame->f) {
5397 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5398 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5399 245d91c1 2018-07-12 stsp blame->f = NULL;
5400 245d91c1 2018-07-12 stsp }
5401 57670559 2018-12-23 stsp if (blame->lines) {
5402 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5403 57670559 2018-12-23 stsp free(blame->lines[i].id);
5404 57670559 2018-12-23 stsp free(blame->lines);
5405 57670559 2018-12-23 stsp blame->lines = NULL;
5406 57670559 2018-12-23 stsp }
5407 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5408 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5409 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5410 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5411 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5412 0ae84acc 2022-06-15 tracey if (err == NULL)
5413 0ae84acc 2022-06-15 tracey err = pack_err;
5414 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5415 0ae84acc 2022-06-15 tracey }
5416 245d91c1 2018-07-12 stsp return err;
5417 245d91c1 2018-07-12 stsp }
5418 245d91c1 2018-07-12 stsp
5419 245d91c1 2018-07-12 stsp static const struct got_error *
5420 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5421 fc06ba56 2019-08-22 stsp {
5422 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5423 fc06ba56 2019-08-22 stsp int *done = arg;
5424 fc06ba56 2019-08-22 stsp int errcode;
5425 fc06ba56 2019-08-22 stsp
5426 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5427 fc06ba56 2019-08-22 stsp if (errcode)
5428 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5429 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5430 fc06ba56 2019-08-22 stsp
5431 fc06ba56 2019-08-22 stsp if (*done)
5432 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5433 fc06ba56 2019-08-22 stsp
5434 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5435 fc06ba56 2019-08-22 stsp if (errcode)
5436 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5437 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5438 fc06ba56 2019-08-22 stsp
5439 fc06ba56 2019-08-22 stsp return err;
5440 fc06ba56 2019-08-22 stsp }
5441 fc06ba56 2019-08-22 stsp
5442 fc06ba56 2019-08-22 stsp static const struct got_error *
5443 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5444 245d91c1 2018-07-12 stsp {
5445 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5446 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5447 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5448 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5449 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5450 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5451 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5452 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5453 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5454 a70480e0 2018-06-23 stsp
5455 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5456 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5457 27d434c2 2018-09-15 stsp if (err)
5458 15a94983 2018-12-23 stsp return err;
5459 eb81bc23 2022-06-28 tracey
5460 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5461 eb81bc23 2022-06-28 tracey if (fd == -1) {
5462 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5463 eb81bc23 2022-06-28 tracey goto done;
5464 eb81bc23 2022-06-28 tracey }
5465 a44927cc 2022-04-07 stsp
5466 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5467 a44927cc 2022-04-07 stsp if (err)
5468 a44927cc 2022-04-07 stsp goto done;
5469 27d434c2 2018-09-15 stsp
5470 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5471 84451b3e 2018-07-10 stsp if (err)
5472 84451b3e 2018-07-10 stsp goto done;
5473 27d434c2 2018-09-15 stsp
5474 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5475 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5476 84451b3e 2018-07-10 stsp goto done;
5477 84451b3e 2018-07-10 stsp }
5478 a70480e0 2018-06-23 stsp
5479 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5480 a70480e0 2018-06-23 stsp if (err)
5481 a70480e0 2018-06-23 stsp goto done;
5482 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5483 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5484 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5485 84451b3e 2018-07-10 stsp goto done;
5486 84451b3e 2018-07-10 stsp }
5487 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5488 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5489 1fddf795 2021-01-20 stsp if (err)
5490 1fddf795 2021-01-20 stsp goto done;
5491 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5492 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5493 84451b3e 2018-07-10 stsp goto done;
5494 1fddf795 2021-01-20 stsp }
5495 b02560ec 2019-08-19 stsp
5496 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5497 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5498 b02560ec 2019-08-19 stsp blame->nlines--;
5499 a70480e0 2018-06-23 stsp
5500 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5501 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5502 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5503 84451b3e 2018-07-10 stsp goto done;
5504 84451b3e 2018-07-10 stsp }
5505 a70480e0 2018-06-23 stsp
5506 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5507 bd24772e 2018-07-11 stsp if (err)
5508 bd24772e 2018-07-11 stsp goto done;
5509 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5510 0ae84acc 2022-06-15 tracey pack_fds);
5511 0ae84acc 2022-06-15 tracey if (err)
5512 0ae84acc 2022-06-15 tracey goto done;
5513 bd24772e 2018-07-11 stsp
5514 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5515 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5516 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5517 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5518 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5519 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5520 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5521 245d91c1 2018-07-12 stsp goto done;
5522 245d91c1 2018-07-12 stsp }
5523 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5524 245d91c1 2018-07-12 stsp
5525 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5526 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5527 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5528 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5529 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5530 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5531 a5388363 2020-12-01 naddy s->blame_complete = 0;
5532 f5a09613 2020-12-13 naddy
5533 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5534 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5535 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5536 f5a09613 2020-12-13 naddy s->selected_line = 1;
5537 f5a09613 2020-12-13 naddy }
5538 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5539 245d91c1 2018-07-12 stsp
5540 245d91c1 2018-07-12 stsp done:
5541 a44927cc 2022-04-07 stsp if (commit)
5542 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5543 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5544 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5545 245d91c1 2018-07-12 stsp if (blob)
5546 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5547 27d434c2 2018-09-15 stsp free(obj_id);
5548 245d91c1 2018-07-12 stsp if (err)
5549 245d91c1 2018-07-12 stsp stop_blame(blame);
5550 245d91c1 2018-07-12 stsp return err;
5551 245d91c1 2018-07-12 stsp }
5552 245d91c1 2018-07-12 stsp
5553 245d91c1 2018-07-12 stsp static const struct got_error *
5554 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5555 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5556 245d91c1 2018-07-12 stsp {
5557 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5558 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5559 dbc6a6b6 2018-07-12 stsp
5560 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5561 245d91c1 2018-07-12 stsp
5562 c4843652 2019-08-12 stsp s->path = strdup(path);
5563 c4843652 2019-08-12 stsp if (s->path == NULL)
5564 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5565 c4843652 2019-08-12 stsp
5566 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5567 c4843652 2019-08-12 stsp if (err) {
5568 c4843652 2019-08-12 stsp free(s->path);
5569 7cbe629d 2018-08-04 stsp return err;
5570 c4843652 2019-08-12 stsp }
5571 245d91c1 2018-07-12 stsp
5572 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5573 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5574 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5575 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5576 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5577 fb2756b9 2018-08-04 stsp s->repo = repo;
5578 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5579 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5580 7cbe629d 2018-08-04 stsp
5581 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5582 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5583 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5584 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5585 11b20872 2019-11-08 stsp if (err)
5586 11b20872 2019-11-08 stsp return err;
5587 11b20872 2019-11-08 stsp }
5588 11b20872 2019-11-08 stsp
5589 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5590 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5591 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5592 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5593 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5594 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5595 e5a0f69f 2018-08-18 stsp
5596 a5388363 2020-12-01 naddy return run_blame(view);
5597 7cbe629d 2018-08-04 stsp }
5598 7cbe629d 2018-08-04 stsp
5599 e5a0f69f 2018-08-18 stsp static const struct got_error *
5600 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5601 7cbe629d 2018-08-04 stsp {
5602 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5603 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5604 7cbe629d 2018-08-04 stsp
5605 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5606 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5607 e5a0f69f 2018-08-18 stsp
5608 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5609 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5610 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5611 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5612 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5613 7cbe629d 2018-08-04 stsp }
5614 e5a0f69f 2018-08-18 stsp
5615 e5a0f69f 2018-08-18 stsp free(s->path);
5616 11b20872 2019-11-08 stsp free_colors(&s->colors);
5617 e5a0f69f 2018-08-18 stsp return err;
5618 7cbe629d 2018-08-04 stsp }
5619 7cbe629d 2018-08-04 stsp
5620 7cbe629d 2018-08-04 stsp static const struct got_error *
5621 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5622 6c4c42e0 2019-06-24 stsp {
5623 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5624 6c4c42e0 2019-06-24 stsp
5625 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5626 6c4c42e0 2019-06-24 stsp return NULL;
5627 6c4c42e0 2019-06-24 stsp }
5628 6c4c42e0 2019-06-24 stsp
5629 6c4c42e0 2019-06-24 stsp static const struct got_error *
5630 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5631 6c4c42e0 2019-06-24 stsp {
5632 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5633 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5634 6c4c42e0 2019-06-24 stsp int lineno;
5635 cb713507 2022-06-16 stsp char *line = NULL;
5636 826082fe 2020-12-10 stsp size_t linesize = 0;
5637 826082fe 2020-12-10 stsp ssize_t linelen;
5638 6c4c42e0 2019-06-24 stsp
5639 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5640 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5641 6c4c42e0 2019-06-24 stsp return NULL;
5642 6c4c42e0 2019-06-24 stsp }
5643 6c4c42e0 2019-06-24 stsp
5644 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5645 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5646 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5647 6c4c42e0 2019-06-24 stsp else
5648 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5649 487cd7d2 2021-12-17 stsp } else
5650 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5651 6c4c42e0 2019-06-24 stsp
5652 6c4c42e0 2019-06-24 stsp while (1) {
5653 6c4c42e0 2019-06-24 stsp off_t offset;
5654 6c4c42e0 2019-06-24 stsp
5655 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5656 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5657 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5658 6c4c42e0 2019-06-24 stsp break;
5659 6c4c42e0 2019-06-24 stsp }
5660 2246482e 2019-06-25 stsp
5661 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5662 6c4c42e0 2019-06-24 stsp lineno = 1;
5663 6c4c42e0 2019-06-24 stsp else
5664 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5665 6c4c42e0 2019-06-24 stsp }
5666 6c4c42e0 2019-06-24 stsp
5667 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5668 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5669 6c4c42e0 2019-06-24 stsp free(line);
5670 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5671 6c4c42e0 2019-06-24 stsp }
5672 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5673 cb713507 2022-06-16 stsp if (linelen != -1) {
5674 cb713507 2022-06-16 stsp char *exstr;
5675 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5676 cb713507 2022-06-16 stsp if (err)
5677 cb713507 2022-06-16 stsp break;
5678 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5679 cb713507 2022-06-16 stsp &view->regmatch)) {
5680 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5681 cb713507 2022-06-16 stsp s->matched_line = lineno;
5682 cb713507 2022-06-16 stsp free(exstr);
5683 cb713507 2022-06-16 stsp break;
5684 cb713507 2022-06-16 stsp }
5685 cb713507 2022-06-16 stsp free(exstr);
5686 6c4c42e0 2019-06-24 stsp }
5687 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5688 6c4c42e0 2019-06-24 stsp lineno++;
5689 6c4c42e0 2019-06-24 stsp else
5690 6c4c42e0 2019-06-24 stsp lineno--;
5691 6c4c42e0 2019-06-24 stsp }
5692 826082fe 2020-12-10 stsp free(line);
5693 6c4c42e0 2019-06-24 stsp
5694 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5695 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5696 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5697 6c4c42e0 2019-06-24 stsp }
5698 6c4c42e0 2019-06-24 stsp
5699 145b6838 2022-06-16 stsp return err;
5700 6c4c42e0 2019-06-24 stsp }
5701 6c4c42e0 2019-06-24 stsp
5702 6c4c42e0 2019-06-24 stsp static const struct got_error *
5703 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5704 7cbe629d 2018-08-04 stsp {
5705 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5706 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5707 2b380cc8 2018-10-24 stsp int errcode;
5708 2b380cc8 2018-10-24 stsp
5709 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5710 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5711 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5712 2b380cc8 2018-10-24 stsp if (errcode)
5713 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5714 51fe7530 2019-08-19 stsp
5715 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5716 2b380cc8 2018-10-24 stsp }
5717 e5a0f69f 2018-08-18 stsp
5718 51fe7530 2019-08-19 stsp if (s->blame_complete)
5719 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5720 51fe7530 2019-08-19 stsp
5721 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5722 e5a0f69f 2018-08-18 stsp
5723 9b058f45 2022-06-30 mark view_border(view);
5724 e5a0f69f 2018-08-18 stsp return err;
5725 e5a0f69f 2018-08-18 stsp }
5726 e5a0f69f 2018-08-18 stsp
5727 e5a0f69f 2018-08-18 stsp static const struct got_error *
5728 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5729 e5a0f69f 2018-08-18 stsp {
5730 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5731 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5732 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5733 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5734 9b058f45 2022-06-30 mark
5735 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5736 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5737 9b058f45 2022-06-30 mark --eos; /* border */
5738 7cbe629d 2018-08-04 stsp
5739 e5a0f69f 2018-08-18 stsp switch (ch) {
5740 145b6838 2022-06-16 stsp case '0':
5741 145b6838 2022-06-16 stsp view->x = 0;
5742 145b6838 2022-06-16 stsp break;
5743 145b6838 2022-06-16 stsp case '$':
5744 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5745 640cd7ff 2022-06-22 mark view->count = 0;
5746 145b6838 2022-06-16 stsp break;
5747 145b6838 2022-06-16 stsp case KEY_RIGHT:
5748 145b6838 2022-06-16 stsp case 'l':
5749 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5750 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5751 640cd7ff 2022-06-22 mark else
5752 640cd7ff 2022-06-22 mark view->count = 0;
5753 145b6838 2022-06-16 stsp break;
5754 145b6838 2022-06-16 stsp case KEY_LEFT:
5755 145b6838 2022-06-16 stsp case 'h':
5756 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5757 640cd7ff 2022-06-22 mark if (view->x <= 0)
5758 640cd7ff 2022-06-22 mark view->count = 0;
5759 145b6838 2022-06-16 stsp break;
5760 1e37a5c2 2019-05-12 jcs case 'q':
5761 1e37a5c2 2019-05-12 jcs s->done = 1;
5762 4deef56f 2021-09-02 naddy break;
5763 4deef56f 2021-09-02 naddy case 'g':
5764 4deef56f 2021-09-02 naddy case KEY_HOME:
5765 4deef56f 2021-09-02 naddy s->selected_line = 1;
5766 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5767 640cd7ff 2022-06-22 mark view->count = 0;
5768 4deef56f 2021-09-02 naddy break;
5769 4deef56f 2021-09-02 naddy case 'G':
5770 4deef56f 2021-09-02 naddy case KEY_END:
5771 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5772 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5773 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5774 4deef56f 2021-09-02 naddy } else {
5775 9b058f45 2022-06-30 mark s->selected_line = eos;
5776 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5777 4deef56f 2021-09-02 naddy }
5778 640cd7ff 2022-06-22 mark view->count = 0;
5779 1e37a5c2 2019-05-12 jcs break;
5780 1e37a5c2 2019-05-12 jcs case 'k':
5781 1e37a5c2 2019-05-12 jcs case KEY_UP:
5782 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5783 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5784 1e37a5c2 2019-05-12 jcs s->selected_line--;
5785 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5786 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5787 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5788 640cd7ff 2022-06-22 mark else
5789 640cd7ff 2022-06-22 mark view->count = 0;
5790 1e37a5c2 2019-05-12 jcs break;
5791 83cc4199 2022-06-13 stsp case CTRL('u'):
5792 33c3719a 2022-06-15 stsp case 'u':
5793 83cc4199 2022-06-13 stsp nscroll /= 2;
5794 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5795 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5796 ea025d1d 2020-02-22 naddy case CTRL('b'):
5797 61417565 2022-06-20 mark case 'b':
5798 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5799 640cd7ff 2022-06-22 mark if (view->count > 1)
5800 640cd7ff 2022-06-22 mark nscroll += nscroll;
5801 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5802 640cd7ff 2022-06-22 mark view->count = 0;
5803 e5a0f69f 2018-08-18 stsp break;
5804 1e37a5c2 2019-05-12 jcs }
5805 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5806 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5807 1e37a5c2 2019-05-12 jcs else
5808 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5809 1e37a5c2 2019-05-12 jcs break;
5810 1e37a5c2 2019-05-12 jcs case 'j':
5811 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5812 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5813 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5814 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5815 1e37a5c2 2019-05-12 jcs s->selected_line++;
5816 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5817 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5818 640cd7ff 2022-06-22 mark else
5819 640cd7ff 2022-06-22 mark view->count = 0;
5820 1e37a5c2 2019-05-12 jcs break;
5821 61417565 2022-06-20 mark case 'c':
5822 1e37a5c2 2019-05-12 jcs case 'p': {
5823 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5824 640cd7ff 2022-06-22 mark
5825 640cd7ff 2022-06-22 mark view->count = 0;
5826 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5827 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5828 1e37a5c2 2019-05-12 jcs if (id == NULL)
5829 e5a0f69f 2018-08-18 stsp break;
5830 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5831 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5832 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5833 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5834 1e37a5c2 2019-05-12 jcs int obj_type;
5835 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5836 1e37a5c2 2019-05-12 jcs s->repo, id);
5837 e5a0f69f 2018-08-18 stsp if (err)
5838 e5a0f69f 2018-08-18 stsp break;
5839 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5840 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5841 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5842 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5843 e5a0f69f 2018-08-18 stsp break;
5844 e5a0f69f 2018-08-18 stsp }
5845 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5846 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5847 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5848 a44927cc 2022-04-07 stsp if (err)
5849 a44927cc 2022-04-07 stsp break;
5850 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5851 a44927cc 2022-04-07 stsp pcommit, s->path);
5852 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5853 e5a0f69f 2018-08-18 stsp if (err) {
5854 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5855 1e37a5c2 2019-05-12 jcs err = NULL;
5856 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5857 e5a0f69f 2018-08-18 stsp break;
5858 e5a0f69f 2018-08-18 stsp }
5859 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5860 1e37a5c2 2019-05-12 jcs blob_id);
5861 1e37a5c2 2019-05-12 jcs free(blob_id);
5862 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5863 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5864 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5865 e5a0f69f 2018-08-18 stsp break;
5866 1e37a5c2 2019-05-12 jcs }
5867 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5868 d7b5a0e8 2022-04-20 stsp &pid->id);
5869 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5870 1e37a5c2 2019-05-12 jcs } else {
5871 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5872 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5873 1e37a5c2 2019-05-12 jcs break;
5874 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5875 1e37a5c2 2019-05-12 jcs id);
5876 1e37a5c2 2019-05-12 jcs }
5877 1e37a5c2 2019-05-12 jcs if (err)
5878 e5a0f69f 2018-08-18 stsp break;
5879 1e37a5c2 2019-05-12 jcs s->done = 1;
5880 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5881 1e37a5c2 2019-05-12 jcs s->done = 0;
5882 1e37a5c2 2019-05-12 jcs if (thread_err)
5883 1e37a5c2 2019-05-12 jcs break;
5884 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5885 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
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 61417565 2022-06-20 mark case 'C': {
5892 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5893 640cd7ff 2022-06-22 mark
5894 640cd7ff 2022-06-22 mark view->count = 0;
5895 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5896 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5897 1e37a5c2 2019-05-12 jcs break;
5898 1e37a5c2 2019-05-12 jcs s->done = 1;
5899 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5900 1e37a5c2 2019-05-12 jcs s->done = 0;
5901 1e37a5c2 2019-05-12 jcs if (thread_err)
5902 1e37a5c2 2019-05-12 jcs break;
5903 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5904 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5905 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5906 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5907 a5388363 2020-12-01 naddy err = run_blame(view);
5908 1e37a5c2 2019-05-12 jcs if (err)
5909 1e37a5c2 2019-05-12 jcs break;
5910 1e37a5c2 2019-05-12 jcs break;
5911 1e37a5c2 2019-05-12 jcs }
5912 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5913 1e37a5c2 2019-05-12 jcs case '\r': {
5914 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5915 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5916 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5917 640cd7ff 2022-06-22 mark
5918 640cd7ff 2022-06-22 mark view->count = 0;
5919 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5920 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5921 1e37a5c2 2019-05-12 jcs if (id == NULL)
5922 1e37a5c2 2019-05-12 jcs break;
5923 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5924 1e37a5c2 2019-05-12 jcs if (err)
5925 1e37a5c2 2019-05-12 jcs break;
5926 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5927 c0f61fa4 2022-07-11 mark if (*new_view) {
5928 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5929 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5930 c0f61fa4 2022-07-11 mark if (err)
5931 c0f61fa4 2022-07-11 mark break;
5932 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5933 c0f61fa4 2022-07-11 mark } else {
5934 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
5935 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
5936 9b058f45 2022-06-30 mark
5937 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
5938 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
5939 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
5940 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
5941 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
5942 c0f61fa4 2022-07-11 mark break;
5943 c0f61fa4 2022-07-11 mark }
5944 15a94983 2018-12-23 stsp }
5945 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5946 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
5947 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5948 1e37a5c2 2019-05-12 jcs if (err) {
5949 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5950 1e37a5c2 2019-05-12 jcs break;
5951 1e37a5c2 2019-05-12 jcs }
5952 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
5953 c0f61fa4 2022-07-11 mark s->selected_line;
5954 c0f61fa4 2022-07-11 mark if (*new_view)
5955 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
5956 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
5957 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5958 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5959 9b058f45 2022-06-30 mark if (err)
5960 9b058f45 2022-06-30 mark break;
5961 9b058f45 2022-06-30 mark }
5962 9b058f45 2022-06-30 mark
5963 e78dc838 2020-12-04 stsp view->focussed = 0;
5964 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5965 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5966 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5967 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5968 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
5969 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5970 1e37a5c2 2019-05-12 jcs if (err)
5971 34bc9ec9 2019-02-22 stsp break;
5972 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5973 0dbbbe90 2022-06-17 op if (err)
5974 0dbbbe90 2022-06-17 op break;
5975 e78dc838 2020-12-04 stsp view->focus_child = 1;
5976 1e37a5c2 2019-05-12 jcs } else
5977 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5978 1e37a5c2 2019-05-12 jcs if (err)
5979 e5a0f69f 2018-08-18 stsp break;
5980 1e37a5c2 2019-05-12 jcs break;
5981 1e37a5c2 2019-05-12 jcs }
5982 83cc4199 2022-06-13 stsp case CTRL('d'):
5983 33c3719a 2022-06-15 stsp case 'd':
5984 83cc4199 2022-06-13 stsp nscroll /= 2;
5985 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5986 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5987 ea025d1d 2020-02-22 naddy case CTRL('f'):
5988 61417565 2022-06-20 mark case 'f':
5989 1e37a5c2 2019-05-12 jcs case ' ':
5990 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5991 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5992 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5993 640cd7ff 2022-06-22 mark view->count = 0;
5994 e5a0f69f 2018-08-18 stsp break;
5995 1e37a5c2 2019-05-12 jcs }
5996 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5997 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5998 83cc4199 2022-06-13 stsp s->selected_line +=
5999 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6000 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6001 1e37a5c2 2019-05-12 jcs }
6002 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6003 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6004 1e37a5c2 2019-05-12 jcs else
6005 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6006 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6007 1e37a5c2 2019-05-12 jcs break;
6008 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6009 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6010 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6011 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6012 1e37a5c2 2019-05-12 jcs }
6013 1e37a5c2 2019-05-12 jcs break;
6014 1e37a5c2 2019-05-12 jcs default:
6015 640cd7ff 2022-06-22 mark view->count = 0;
6016 1e37a5c2 2019-05-12 jcs break;
6017 a70480e0 2018-06-23 stsp }
6018 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6019 917d79a7 2022-07-01 stsp }
6020 917d79a7 2022-07-01 stsp
6021 917d79a7 2022-07-01 stsp static const struct got_error *
6022 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6023 917d79a7 2022-07-01 stsp {
6024 917d79a7 2022-07-01 stsp const struct got_error *err;
6025 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6026 917d79a7 2022-07-01 stsp
6027 917d79a7 2022-07-01 stsp view->count = 0;
6028 917d79a7 2022-07-01 stsp s->done = 1;
6029 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6030 917d79a7 2022-07-01 stsp s->done = 0;
6031 917d79a7 2022-07-01 stsp if (err)
6032 917d79a7 2022-07-01 stsp return err;
6033 917d79a7 2022-07-01 stsp return run_blame(view);
6034 a70480e0 2018-06-23 stsp }
6035 a70480e0 2018-06-23 stsp
6036 a70480e0 2018-06-23 stsp static const struct got_error *
6037 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6038 9f7d7167 2018-04-29 stsp {
6039 a70480e0 2018-06-23 stsp const struct got_error *error;
6040 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6041 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6042 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6043 0587e10c 2020-07-23 stsp char *link_target = NULL;
6044 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6045 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6046 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6047 a70480e0 2018-06-23 stsp int ch;
6048 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6049 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6050 a70480e0 2018-06-23 stsp
6051 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6052 a70480e0 2018-06-23 stsp switch (ch) {
6053 a70480e0 2018-06-23 stsp case 'c':
6054 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6055 a70480e0 2018-06-23 stsp break;
6056 69069811 2018-08-02 stsp case 'r':
6057 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6058 69069811 2018-08-02 stsp if (repo_path == NULL)
6059 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6060 9ba1d308 2019-10-21 stsp optarg);
6061 69069811 2018-08-02 stsp break;
6062 a70480e0 2018-06-23 stsp default:
6063 17020d27 2019-03-07 stsp usage_blame();
6064 a70480e0 2018-06-23 stsp /* NOTREACHED */
6065 a70480e0 2018-06-23 stsp }
6066 a70480e0 2018-06-23 stsp }
6067 a70480e0 2018-06-23 stsp
6068 a70480e0 2018-06-23 stsp argc -= optind;
6069 a70480e0 2018-06-23 stsp argv += optind;
6070 a70480e0 2018-06-23 stsp
6071 f135c941 2020-02-20 stsp if (argc != 1)
6072 a70480e0 2018-06-23 stsp usage_blame();
6073 6962eb72 2020-02-20 stsp
6074 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6075 0ae84acc 2022-06-15 tracey if (error != NULL)
6076 0ae84acc 2022-06-15 tracey goto done;
6077 0ae84acc 2022-06-15 tracey
6078 69069811 2018-08-02 stsp if (repo_path == NULL) {
6079 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6080 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6081 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6082 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6083 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6084 c156c7a4 2020-12-18 stsp goto done;
6085 f135c941 2020-02-20 stsp if (worktree)
6086 eb41ed75 2019-02-05 stsp repo_path =
6087 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6088 f135c941 2020-02-20 stsp else
6089 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6090 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6091 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6092 c156c7a4 2020-12-18 stsp goto done;
6093 c156c7a4 2020-12-18 stsp }
6094 f135c941 2020-02-20 stsp }
6095 a915003a 2019-02-05 stsp
6096 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6097 c02c541e 2019-03-29 stsp if (error != NULL)
6098 8e94dd5b 2019-01-04 stsp goto done;
6099 69069811 2018-08-02 stsp
6100 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6101 f135c941 2020-02-20 stsp worktree);
6102 c02c541e 2019-03-29 stsp if (error)
6103 92205607 2019-01-04 stsp goto done;
6104 69069811 2018-08-02 stsp
6105 f135c941 2020-02-20 stsp init_curses();
6106 f135c941 2020-02-20 stsp
6107 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6108 51a10b52 2020-12-26 stsp if (error)
6109 51a10b52 2020-12-26 stsp goto done;
6110 51a10b52 2020-12-26 stsp
6111 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6112 eb41ed75 2019-02-05 stsp if (error)
6113 69069811 2018-08-02 stsp goto done;
6114 a70480e0 2018-06-23 stsp
6115 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6116 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6117 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6118 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6119 a70480e0 2018-06-23 stsp if (error != NULL)
6120 66b4983c 2018-06-23 stsp goto done;
6121 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6122 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6123 a70480e0 2018-06-23 stsp } else {
6124 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6125 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6126 a70480e0 2018-06-23 stsp }
6127 a19e88aa 2018-06-23 stsp if (error != NULL)
6128 8b473291 2019-02-21 stsp goto done;
6129 8b473291 2019-02-21 stsp
6130 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6131 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6132 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6133 e1cd8fed 2018-08-01 stsp goto done;
6134 e1cd8fed 2018-08-01 stsp }
6135 0587e10c 2020-07-23 stsp
6136 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6137 a44927cc 2022-04-07 stsp if (error)
6138 a44927cc 2022-04-07 stsp goto done;
6139 a44927cc 2022-04-07 stsp
6140 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6141 a44927cc 2022-04-07 stsp commit, repo);
6142 7cbe629d 2018-08-04 stsp if (error)
6143 7cbe629d 2018-08-04 stsp goto done;
6144 0587e10c 2020-07-23 stsp
6145 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6146 78756c87 2020-11-24 stsp commit_id, repo);
6147 0587e10c 2020-07-23 stsp if (error)
6148 0587e10c 2020-07-23 stsp goto done;
6149 12314ad4 2019-08-31 stsp if (worktree) {
6150 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6151 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6152 12314ad4 2019-08-31 stsp worktree = NULL;
6153 12314ad4 2019-08-31 stsp }
6154 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6155 a70480e0 2018-06-23 stsp done:
6156 69069811 2018-08-02 stsp free(repo_path);
6157 f135c941 2020-02-20 stsp free(in_repo_path);
6158 0587e10c 2020-07-23 stsp free(link_target);
6159 69069811 2018-08-02 stsp free(cwd);
6160 a70480e0 2018-06-23 stsp free(commit_id);
6161 a44927cc 2022-04-07 stsp if (commit)
6162 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6163 eb41ed75 2019-02-05 stsp if (worktree)
6164 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6165 1d0f4054 2021-06-17 stsp if (repo) {
6166 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6167 1d0f4054 2021-06-17 stsp if (error == NULL)
6168 1d0f4054 2021-06-17 stsp error = close_err;
6169 1d0f4054 2021-06-17 stsp }
6170 0ae84acc 2022-06-15 tracey if (pack_fds) {
6171 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6172 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6173 0ae84acc 2022-06-15 tracey if (error == NULL)
6174 0ae84acc 2022-06-15 tracey error = pack_err;
6175 0ae84acc 2022-06-15 tracey }
6176 51a10b52 2020-12-26 stsp tog_free_refs();
6177 a70480e0 2018-06-23 stsp return error;
6178 ffd1d5e5 2018-06-23 stsp }
6179 ffd1d5e5 2018-06-23 stsp
6180 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6181 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6182 ffd1d5e5 2018-06-23 stsp {
6183 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6184 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6185 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6186 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6187 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6188 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6189 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6190 ffd1d5e5 2018-06-23 stsp
6191 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6192 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6193 9b058f45 2022-06-30 mark --limit; /* border */
6194 ffd1d5e5 2018-06-23 stsp
6195 f7d12f7e 2018-08-01 stsp werase(view->window);
6196 ffd1d5e5 2018-06-23 stsp
6197 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6198 ffd1d5e5 2018-06-23 stsp return NULL;
6199 ffd1d5e5 2018-06-23 stsp
6200 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6201 ccda2f4d 2022-06-16 stsp 0, 0);
6202 ffd1d5e5 2018-06-23 stsp if (err)
6203 ffd1d5e5 2018-06-23 stsp return err;
6204 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6205 a3404814 2018-09-02 stsp wstandout(view->window);
6206 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6207 11b20872 2019-11-08 stsp if (tc)
6208 11b20872 2019-11-08 stsp wattr_on(view->window,
6209 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6210 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6211 11b20872 2019-11-08 stsp if (tc)
6212 11b20872 2019-11-08 stsp wattr_off(view->window,
6213 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6214 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6215 a3404814 2018-09-02 stsp wstandend(view->window);
6216 2550e4c3 2018-07-13 stsp free(wline);
6217 2550e4c3 2018-07-13 stsp wline = NULL;
6218 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6219 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6220 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6221 ffd1d5e5 2018-06-23 stsp return NULL;
6222 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6223 ccda2f4d 2022-06-16 stsp 0, 0);
6224 ce52c690 2018-06-23 stsp if (err)
6225 ce52c690 2018-06-23 stsp return err;
6226 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6227 2550e4c3 2018-07-13 stsp free(wline);
6228 2550e4c3 2018-07-13 stsp wline = NULL;
6229 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6230 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6231 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6232 ffd1d5e5 2018-06-23 stsp return NULL;
6233 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6234 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6235 a1eca9bb 2018-06-23 stsp return NULL;
6236 ffd1d5e5 2018-06-23 stsp
6237 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6238 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6239 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6240 0cf4efb1 2018-09-29 stsp if (view->focussed)
6241 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6242 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6243 ffd1d5e5 2018-06-23 stsp }
6244 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6245 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6246 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6247 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6248 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6249 ffd1d5e5 2018-06-23 stsp return NULL;
6250 ffd1d5e5 2018-06-23 stsp n = 1;
6251 ffd1d5e5 2018-06-23 stsp } else {
6252 ffd1d5e5 2018-06-23 stsp n = 0;
6253 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6254 ffd1d5e5 2018-06-23 stsp }
6255 ffd1d5e5 2018-06-23 stsp
6256 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6257 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6258 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6259 848d6979 2019-08-12 stsp const char *modestr = "";
6260 56e0773d 2019-11-28 stsp mode_t mode;
6261 1d13200f 2018-07-12 stsp
6262 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6263 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6264 56e0773d 2019-11-28 stsp
6265 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6266 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6267 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6268 1d13200f 2018-07-12 stsp if (err)
6269 638f9024 2019-05-13 stsp return got_error_from_errno(
6270 230a42bd 2019-05-11 jcs "got_object_id_str");
6271 1d13200f 2018-07-12 stsp }
6272 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6273 63c5ca5d 2019-08-24 stsp modestr = "$";
6274 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6275 0d6c6ee3 2020-05-20 stsp int i;
6276 0d6c6ee3 2020-05-20 stsp
6277 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6278 d86d3b18 2020-12-01 naddy te, s->repo);
6279 0d6c6ee3 2020-05-20 stsp if (err) {
6280 0d6c6ee3 2020-05-20 stsp free(id_str);
6281 0d6c6ee3 2020-05-20 stsp return err;
6282 0d6c6ee3 2020-05-20 stsp }
6283 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6284 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6285 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6286 0d6c6ee3 2020-05-20 stsp }
6287 848d6979 2019-08-12 stsp modestr = "@";
6288 0d6c6ee3 2020-05-20 stsp }
6289 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6290 848d6979 2019-08-12 stsp modestr = "/";
6291 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6292 848d6979 2019-08-12 stsp modestr = "*";
6293 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6294 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6295 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6296 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6297 1d13200f 2018-07-12 stsp free(id_str);
6298 0d6c6ee3 2020-05-20 stsp free(link_target);
6299 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6300 1d13200f 2018-07-12 stsp }
6301 1d13200f 2018-07-12 stsp free(id_str);
6302 0d6c6ee3 2020-05-20 stsp free(link_target);
6303 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6304 ccda2f4d 2022-06-16 stsp 0, 0);
6305 ffd1d5e5 2018-06-23 stsp if (err) {
6306 ffd1d5e5 2018-06-23 stsp free(line);
6307 ffd1d5e5 2018-06-23 stsp break;
6308 ffd1d5e5 2018-06-23 stsp }
6309 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6310 0cf4efb1 2018-09-29 stsp if (view->focussed)
6311 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6312 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6313 ffd1d5e5 2018-06-23 stsp }
6314 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6315 f26dddb7 2019-11-08 stsp if (tc)
6316 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6317 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6318 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6319 f26dddb7 2019-11-08 stsp if (tc)
6320 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6321 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6322 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6323 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6324 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6325 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6326 ffd1d5e5 2018-06-23 stsp free(line);
6327 2550e4c3 2018-07-13 stsp free(wline);
6328 2550e4c3 2018-07-13 stsp wline = NULL;
6329 ffd1d5e5 2018-06-23 stsp n++;
6330 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6331 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6332 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6333 ffd1d5e5 2018-06-23 stsp break;
6334 ffd1d5e5 2018-06-23 stsp }
6335 ffd1d5e5 2018-06-23 stsp
6336 ffd1d5e5 2018-06-23 stsp return err;
6337 ffd1d5e5 2018-06-23 stsp }
6338 ffd1d5e5 2018-06-23 stsp
6339 ffd1d5e5 2018-06-23 stsp static void
6340 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6341 ffd1d5e5 2018-06-23 stsp {
6342 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6343 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6344 fa86c4bf 2020-11-29 stsp int i = 0;
6345 ffd1d5e5 2018-06-23 stsp
6346 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6347 ffd1d5e5 2018-06-23 stsp return;
6348 ffd1d5e5 2018-06-23 stsp
6349 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6350 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6351 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6352 fa86c4bf 2020-11-29 stsp if (!isroot)
6353 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6354 fa86c4bf 2020-11-29 stsp break;
6355 fa86c4bf 2020-11-29 stsp }
6356 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6357 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6358 ffd1d5e5 2018-06-23 stsp }
6359 ffd1d5e5 2018-06-23 stsp }
6360 ffd1d5e5 2018-06-23 stsp
6361 9b058f45 2022-06-30 mark static const struct got_error *
6362 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6363 ffd1d5e5 2018-06-23 stsp {
6364 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6365 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6366 ffd1d5e5 2018-06-23 stsp int n = 0;
6367 ffd1d5e5 2018-06-23 stsp
6368 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6369 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6370 694d3271 2020-12-01 naddy s->first_displayed_entry);
6371 694d3271 2020-12-01 naddy else
6372 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6373 56e0773d 2019-11-28 stsp
6374 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6375 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6376 9b058f45 2022-06-30 mark if (last)
6377 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6378 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6379 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6380 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6381 768394f3 2019-01-24 stsp }
6382 ffd1d5e5 2018-06-23 stsp }
6383 9b058f45 2022-06-30 mark
6384 9b058f45 2022-06-30 mark return NULL;
6385 ffd1d5e5 2018-06-23 stsp }
6386 ffd1d5e5 2018-06-23 stsp
6387 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6388 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6389 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6390 ffd1d5e5 2018-06-23 stsp {
6391 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6392 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6393 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6394 ffd1d5e5 2018-06-23 stsp
6395 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6396 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6397 56e0773d 2019-11-28 stsp + 1 /* slash */;
6398 ce52c690 2018-06-23 stsp if (te)
6399 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6400 ce52c690 2018-06-23 stsp
6401 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6402 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6403 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6404 ffd1d5e5 2018-06-23 stsp
6405 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6406 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6407 d9765a41 2018-06-23 stsp while (pt) {
6408 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6409 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6410 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6411 cb2ebc8a 2018-06-23 stsp goto done;
6412 cb2ebc8a 2018-06-23 stsp }
6413 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6414 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6415 cb2ebc8a 2018-06-23 stsp goto done;
6416 cb2ebc8a 2018-06-23 stsp }
6417 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6418 ffd1d5e5 2018-06-23 stsp }
6419 ce52c690 2018-06-23 stsp if (te) {
6420 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6421 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6422 ce52c690 2018-06-23 stsp goto done;
6423 ce52c690 2018-06-23 stsp }
6424 cb2ebc8a 2018-06-23 stsp }
6425 ce52c690 2018-06-23 stsp done:
6426 ce52c690 2018-06-23 stsp if (err) {
6427 ce52c690 2018-06-23 stsp free(*path);
6428 ce52c690 2018-06-23 stsp *path = NULL;
6429 ce52c690 2018-06-23 stsp }
6430 ce52c690 2018-06-23 stsp return err;
6431 ce52c690 2018-06-23 stsp }
6432 ce52c690 2018-06-23 stsp
6433 ce52c690 2018-06-23 stsp static const struct got_error *
6434 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6435 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6436 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6437 ce52c690 2018-06-23 stsp {
6438 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6439 ce52c690 2018-06-23 stsp char *path;
6440 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6441 a0de39f3 2019-08-09 stsp
6442 a0de39f3 2019-08-09 stsp *new_view = NULL;
6443 69efd4c4 2018-07-18 stsp
6444 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6445 ce52c690 2018-06-23 stsp if (err)
6446 ce52c690 2018-06-23 stsp return err;
6447 ffd1d5e5 2018-06-23 stsp
6448 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6449 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6450 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6451 83ce39e3 2019-08-12 stsp goto done;
6452 83ce39e3 2019-08-12 stsp }
6453 cdf1ee82 2018-08-01 stsp
6454 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6455 e5a0f69f 2018-08-18 stsp if (err) {
6456 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6457 fc06ba56 2019-08-22 stsp err = NULL;
6458 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6459 e5a0f69f 2018-08-18 stsp } else
6460 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6461 83ce39e3 2019-08-12 stsp done:
6462 83ce39e3 2019-08-12 stsp free(path);
6463 69efd4c4 2018-07-18 stsp return err;
6464 69efd4c4 2018-07-18 stsp }
6465 69efd4c4 2018-07-18 stsp
6466 69efd4c4 2018-07-18 stsp static const struct got_error *
6467 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6468 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6469 69efd4c4 2018-07-18 stsp {
6470 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6471 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6472 69efd4c4 2018-07-18 stsp char *path;
6473 a0de39f3 2019-08-09 stsp
6474 a0de39f3 2019-08-09 stsp *new_view = NULL;
6475 69efd4c4 2018-07-18 stsp
6476 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6477 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6478 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6479 e5a0f69f 2018-08-18 stsp
6480 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6481 69efd4c4 2018-07-18 stsp if (err)
6482 69efd4c4 2018-07-18 stsp return err;
6483 69efd4c4 2018-07-18 stsp
6484 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6485 4e97c21c 2020-12-06 stsp path, 0);
6486 ba4f502b 2018-08-04 stsp if (err)
6487 e5a0f69f 2018-08-18 stsp view_close(log_view);
6488 e5a0f69f 2018-08-18 stsp else
6489 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6490 cb2ebc8a 2018-06-23 stsp free(path);
6491 cb2ebc8a 2018-06-23 stsp return err;
6492 ffd1d5e5 2018-06-23 stsp }
6493 ffd1d5e5 2018-06-23 stsp
6494 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6495 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6496 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6497 ffd1d5e5 2018-06-23 stsp {
6498 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6499 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6500 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6501 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6502 ffd1d5e5 2018-06-23 stsp
6503 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6504 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6505 bc573f3b 2021-07-10 stsp
6506 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6507 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6508 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6509 ffd1d5e5 2018-06-23 stsp
6510 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6511 bc573f3b 2021-07-10 stsp if (err)
6512 bc573f3b 2021-07-10 stsp goto done;
6513 bc573f3b 2021-07-10 stsp
6514 bc573f3b 2021-07-10 stsp /*
6515 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6516 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6517 bc573f3b 2021-07-10 stsp * closed on demand.
6518 bc573f3b 2021-07-10 stsp */
6519 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6520 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6521 bc573f3b 2021-07-10 stsp if (err)
6522 bc573f3b 2021-07-10 stsp goto done;
6523 bc573f3b 2021-07-10 stsp s->tree = s->root;
6524 bc573f3b 2021-07-10 stsp
6525 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6526 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6527 ffd1d5e5 2018-06-23 stsp goto done;
6528 ffd1d5e5 2018-06-23 stsp
6529 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6530 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6531 ffd1d5e5 2018-06-23 stsp goto done;
6532 ffd1d5e5 2018-06-23 stsp }
6533 ffd1d5e5 2018-06-23 stsp
6534 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6535 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6536 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6537 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6538 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6539 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6540 9cd7cbd1 2020-12-07 stsp goto done;
6541 9cd7cbd1 2020-12-07 stsp }
6542 9cd7cbd1 2020-12-07 stsp }
6543 fb2756b9 2018-08-04 stsp s->repo = repo;
6544 c0b01bdb 2019-11-08 stsp
6545 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6546 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6547 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6548 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6549 c0b01bdb 2019-11-08 stsp if (err)
6550 c0b01bdb 2019-11-08 stsp goto done;
6551 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6552 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6553 bc573f3b 2021-07-10 stsp if (err)
6554 c0b01bdb 2019-11-08 stsp goto done;
6555 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6556 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6557 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6558 bc573f3b 2021-07-10 stsp if (err)
6559 c0b01bdb 2019-11-08 stsp goto done;
6560 e5a0f69f 2018-08-18 stsp
6561 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6562 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6563 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6564 bc573f3b 2021-07-10 stsp if (err)
6565 11b20872 2019-11-08 stsp goto done;
6566 11b20872 2019-11-08 stsp
6567 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6568 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6569 bc573f3b 2021-07-10 stsp if (err)
6570 c0b01bdb 2019-11-08 stsp goto done;
6571 c0b01bdb 2019-11-08 stsp }
6572 c0b01bdb 2019-11-08 stsp
6573 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6574 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6575 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6576 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6577 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6578 ad80ab7b 2018-08-04 stsp done:
6579 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6580 bc573f3b 2021-07-10 stsp if (commit)
6581 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6582 bc573f3b 2021-07-10 stsp if (err)
6583 bc573f3b 2021-07-10 stsp close_tree_view(view);
6584 ad80ab7b 2018-08-04 stsp return err;
6585 ad80ab7b 2018-08-04 stsp }
6586 ad80ab7b 2018-08-04 stsp
6587 e5a0f69f 2018-08-18 stsp static const struct got_error *
6588 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6589 ad80ab7b 2018-08-04 stsp {
6590 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6591 ad80ab7b 2018-08-04 stsp
6592 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6593 fb2756b9 2018-08-04 stsp free(s->tree_label);
6594 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6595 6484ec90 2018-09-29 stsp free(s->commit_id);
6596 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6597 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6598 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6599 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6600 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6601 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6602 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6603 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6604 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6605 ad80ab7b 2018-08-04 stsp free(parent);
6606 ad80ab7b 2018-08-04 stsp
6607 ad80ab7b 2018-08-04 stsp }
6608 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6609 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6610 bc573f3b 2021-07-10 stsp if (s->root)
6611 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6612 7c32bd05 2019-06-22 stsp return NULL;
6613 7c32bd05 2019-06-22 stsp }
6614 7c32bd05 2019-06-22 stsp
6615 7c32bd05 2019-06-22 stsp static const struct got_error *
6616 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6617 7c32bd05 2019-06-22 stsp {
6618 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6619 7c32bd05 2019-06-22 stsp
6620 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6621 7c32bd05 2019-06-22 stsp return NULL;
6622 7c32bd05 2019-06-22 stsp }
6623 7c32bd05 2019-06-22 stsp
6624 7c32bd05 2019-06-22 stsp static int
6625 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6626 7c32bd05 2019-06-22 stsp {
6627 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6628 7c32bd05 2019-06-22 stsp
6629 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6630 56e0773d 2019-11-28 stsp 0) == 0;
6631 7c32bd05 2019-06-22 stsp }
6632 7c32bd05 2019-06-22 stsp
6633 7c32bd05 2019-06-22 stsp static const struct got_error *
6634 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6635 7c32bd05 2019-06-22 stsp {
6636 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6637 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6638 7c32bd05 2019-06-22 stsp
6639 7c32bd05 2019-06-22 stsp if (!view->searching) {
6640 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6641 7c32bd05 2019-06-22 stsp return NULL;
6642 7c32bd05 2019-06-22 stsp }
6643 7c32bd05 2019-06-22 stsp
6644 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6645 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6646 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6647 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6648 56e0773d 2019-11-28 stsp s->selected_entry);
6649 7c32bd05 2019-06-22 stsp else
6650 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6651 56e0773d 2019-11-28 stsp } else {
6652 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6653 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6654 56e0773d 2019-11-28 stsp else
6655 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6656 56e0773d 2019-11-28 stsp s->selected_entry);
6657 7c32bd05 2019-06-22 stsp }
6658 7c32bd05 2019-06-22 stsp } else {
6659 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6660 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6661 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6662 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6663 56e0773d 2019-11-28 stsp else
6664 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6665 7c32bd05 2019-06-22 stsp }
6666 7c32bd05 2019-06-22 stsp
6667 7c32bd05 2019-06-22 stsp while (1) {
6668 56e0773d 2019-11-28 stsp if (te == NULL) {
6669 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6670 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6671 ac66afb8 2019-06-24 stsp return NULL;
6672 ac66afb8 2019-06-24 stsp }
6673 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6674 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6675 56e0773d 2019-11-28 stsp else
6676 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6677 7c32bd05 2019-06-22 stsp }
6678 7c32bd05 2019-06-22 stsp
6679 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6680 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6681 56e0773d 2019-11-28 stsp s->matched_entry = te;
6682 7c32bd05 2019-06-22 stsp break;
6683 7c32bd05 2019-06-22 stsp }
6684 7c32bd05 2019-06-22 stsp
6685 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6686 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6687 56e0773d 2019-11-28 stsp else
6688 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6689 7c32bd05 2019-06-22 stsp }
6690 e5a0f69f 2018-08-18 stsp
6691 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6692 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6693 7c32bd05 2019-06-22 stsp s->selected = 0;
6694 7c32bd05 2019-06-22 stsp }
6695 7c32bd05 2019-06-22 stsp
6696 e5a0f69f 2018-08-18 stsp return NULL;
6697 ad80ab7b 2018-08-04 stsp }
6698 ad80ab7b 2018-08-04 stsp
6699 ad80ab7b 2018-08-04 stsp static const struct got_error *
6700 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6701 ad80ab7b 2018-08-04 stsp {
6702 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6703 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6704 e5a0f69f 2018-08-18 stsp char *parent_path;
6705 ad80ab7b 2018-08-04 stsp
6706 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6707 e5a0f69f 2018-08-18 stsp if (err)
6708 e5a0f69f 2018-08-18 stsp return err;
6709 ffd1d5e5 2018-06-23 stsp
6710 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6711 e5a0f69f 2018-08-18 stsp free(parent_path);
6712 669b5ffa 2018-10-07 stsp
6713 9b058f45 2022-06-30 mark view_border(view);
6714 e5a0f69f 2018-08-18 stsp return err;
6715 e5a0f69f 2018-08-18 stsp }
6716 ce52c690 2018-06-23 stsp
6717 e5a0f69f 2018-08-18 stsp static const struct got_error *
6718 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6719 e5a0f69f 2018-08-18 stsp {
6720 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6721 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6722 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6723 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6724 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6725 ffd1d5e5 2018-06-23 stsp
6726 e5a0f69f 2018-08-18 stsp switch (ch) {
6727 1e37a5c2 2019-05-12 jcs case 'i':
6728 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6729 640cd7ff 2022-06-22 mark view->count = 0;
6730 1e37a5c2 2019-05-12 jcs break;
6731 1e37a5c2 2019-05-12 jcs case 'l':
6732 640cd7ff 2022-06-22 mark view->count = 0;
6733 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6734 ffd1d5e5 2018-06-23 stsp break;
6735 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6736 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6737 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6738 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6739 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6740 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6741 49b24ee5 2022-07-03 mark if (err)
6742 49b24ee5 2022-07-03 mark break;
6743 49b24ee5 2022-07-03 mark }
6744 e78dc838 2020-12-04 stsp view->focussed = 0;
6745 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6746 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6747 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6748 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6749 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
6750 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6751 1e37a5c2 2019-05-12 jcs if (err)
6752 1e37a5c2 2019-05-12 jcs return err;
6753 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6754 0dbbbe90 2022-06-17 op if (err)
6755 0dbbbe90 2022-06-17 op return err;
6756 e78dc838 2020-12-04 stsp view->focus_child = 1;
6757 1e37a5c2 2019-05-12 jcs } else
6758 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6759 152c1c93 2020-11-29 stsp break;
6760 152c1c93 2020-11-29 stsp case 'r':
6761 640cd7ff 2022-06-22 mark view->count = 0;
6762 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6763 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6764 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6765 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6766 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6767 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6768 152c1c93 2020-11-29 stsp if (err) {
6769 152c1c93 2020-11-29 stsp view_close(ref_view);
6770 152c1c93 2020-11-29 stsp return err;
6771 152c1c93 2020-11-29 stsp }
6772 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6773 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6774 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6775 49b24ee5 2022-07-03 mark if (err)
6776 49b24ee5 2022-07-03 mark break;
6777 49b24ee5 2022-07-03 mark }
6778 e78dc838 2020-12-04 stsp view->focussed = 0;
6779 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6780 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6781 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6782 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6783 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
6784 152c1c93 2020-11-29 stsp err = view_close_child(view);
6785 152c1c93 2020-11-29 stsp if (err)
6786 152c1c93 2020-11-29 stsp return err;
6787 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6788 0dbbbe90 2022-06-17 op if (err)
6789 0dbbbe90 2022-06-17 op return err;
6790 e78dc838 2020-12-04 stsp view->focus_child = 1;
6791 152c1c93 2020-11-29 stsp } else
6792 152c1c93 2020-11-29 stsp *new_view = ref_view;
6793 e4526bf5 2021-09-03 naddy break;
6794 e4526bf5 2021-09-03 naddy case 'g':
6795 e4526bf5 2021-09-03 naddy case KEY_HOME:
6796 e4526bf5 2021-09-03 naddy s->selected = 0;
6797 640cd7ff 2022-06-22 mark view->count = 0;
6798 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6799 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6800 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6801 e4526bf5 2021-09-03 naddy else
6802 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6803 1e37a5c2 2019-05-12 jcs break;
6804 e4526bf5 2021-09-03 naddy case 'G':
6805 9b058f45 2022-06-30 mark case KEY_END: {
6806 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6807 9b058f45 2022-06-30 mark
6808 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6809 9b058f45 2022-06-30 mark --eos; /* border */
6810 e4526bf5 2021-09-03 naddy s->selected = 0;
6811 640cd7ff 2022-06-22 mark view->count = 0;
6812 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6813 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6814 e4526bf5 2021-09-03 naddy if (te == NULL) {
6815 ad055527 2022-07-16 mark if (s->tree != s->root) {
6816 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6817 e4526bf5 2021-09-03 naddy n++;
6818 e4526bf5 2021-09-03 naddy }
6819 e4526bf5 2021-09-03 naddy break;
6820 e4526bf5 2021-09-03 naddy }
6821 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6822 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6823 e4526bf5 2021-09-03 naddy }
6824 e4526bf5 2021-09-03 naddy if (n > 0)
6825 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6826 e4526bf5 2021-09-03 naddy break;
6827 9b058f45 2022-06-30 mark }
6828 1e37a5c2 2019-05-12 jcs case 'k':
6829 1e37a5c2 2019-05-12 jcs case KEY_UP:
6830 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6831 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6832 1e37a5c2 2019-05-12 jcs s->selected--;
6833 fa86c4bf 2020-11-29 stsp break;
6834 1e37a5c2 2019-05-12 jcs }
6835 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6836 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6837 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6838 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6839 640cd7ff 2022-06-22 mark view->count = 0;
6840 1e37a5c2 2019-05-12 jcs break;
6841 83cc4199 2022-06-13 stsp case CTRL('u'):
6842 33c3719a 2022-06-15 stsp case 'u':
6843 83cc4199 2022-06-13 stsp nscroll /= 2;
6844 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6845 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6846 ea025d1d 2020-02-22 naddy case CTRL('b'):
6847 61417565 2022-06-20 mark case 'b':
6848 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6849 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6850 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6851 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6852 fa86c4bf 2020-11-29 stsp } else {
6853 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6854 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6855 fa86c4bf 2020-11-29 stsp }
6856 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6857 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6858 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6859 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6860 640cd7ff 2022-06-22 mark view->count = 0;
6861 1e37a5c2 2019-05-12 jcs break;
6862 1e37a5c2 2019-05-12 jcs case 'j':
6863 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6864 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6865 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6866 1e37a5c2 2019-05-12 jcs s->selected++;
6867 1e37a5c2 2019-05-12 jcs break;
6868 1e37a5c2 2019-05-12 jcs }
6869 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6870 640cd7ff 2022-06-22 mark == NULL) {
6871 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6872 640cd7ff 2022-06-22 mark view->count = 0;
6873 1e37a5c2 2019-05-12 jcs break;
6874 640cd7ff 2022-06-22 mark }
6875 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6876 1e37a5c2 2019-05-12 jcs break;
6877 83cc4199 2022-06-13 stsp case CTRL('d'):
6878 33c3719a 2022-06-15 stsp case 'd':
6879 83cc4199 2022-06-13 stsp nscroll /= 2;
6880 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6881 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6882 ea025d1d 2020-02-22 naddy case CTRL('f'):
6883 61417565 2022-06-20 mark case 'f':
6884 48bb96f0 2022-06-20 naddy case ' ':
6885 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6886 1e37a5c2 2019-05-12 jcs == NULL) {
6887 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6888 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6889 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6890 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6891 640cd7ff 2022-06-22 mark else
6892 640cd7ff 2022-06-22 mark view->count = 0;
6893 1e37a5c2 2019-05-12 jcs break;
6894 1e37a5c2 2019-05-12 jcs }
6895 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6896 1e37a5c2 2019-05-12 jcs break;
6897 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6898 1e37a5c2 2019-05-12 jcs case '\r':
6899 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6900 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6901 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6902 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6903 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6904 640cd7ff 2022-06-22 mark view->count = 0;
6905 1e37a5c2 2019-05-12 jcs break;
6906 640cd7ff 2022-06-22 mark }
6907 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6908 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6909 1e37a5c2 2019-05-12 jcs entry);
6910 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6911 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6912 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6913 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6914 1e37a5c2 2019-05-12 jcs s->selected_entry =
6915 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6916 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6917 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6918 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6919 9b058f45 2022-06-30 mark if (err)
6920 9b058f45 2022-06-30 mark break;
6921 9b058f45 2022-06-30 mark }
6922 1e37a5c2 2019-05-12 jcs free(parent);
6923 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6924 56e0773d 2019-11-28 stsp s->selected_entry))) {
6925 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6926 640cd7ff 2022-06-22 mark view->count = 0;
6927 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6928 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6929 1e37a5c2 2019-05-12 jcs if (err)
6930 1e37a5c2 2019-05-12 jcs break;
6931 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6932 941e9f74 2019-05-21 stsp if (err) {
6933 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6934 1e37a5c2 2019-05-12 jcs break;
6935 1e37a5c2 2019-05-12 jcs }
6936 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6937 56e0773d 2019-11-28 stsp s->selected_entry))) {
6938 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6939 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6940 1e37a5c2 2019-05-12 jcs
6941 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6942 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6943 9b058f45 2022-06-30 mark
6944 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6945 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6946 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6947 1e37a5c2 2019-05-12 jcs if (err)
6948 1e37a5c2 2019-05-12 jcs break;
6949 9b058f45 2022-06-30 mark
6950 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6951 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6952 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6953 9b058f45 2022-06-30 mark if (err)
6954 9b058f45 2022-06-30 mark break;
6955 9b058f45 2022-06-30 mark }
6956 9b058f45 2022-06-30 mark
6957 640cd7ff 2022-06-22 mark view->count = 0;
6958 e78dc838 2020-12-04 stsp view->focussed = 0;
6959 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6960 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6961 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6962 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6963 3c1dfe12 2022-07-08 mark view_transfer_size(blame_view, view);
6964 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6965 669b5ffa 2018-10-07 stsp if (err)
6966 669b5ffa 2018-10-07 stsp return err;
6967 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6968 0dbbbe90 2022-06-17 op if (err)
6969 0dbbbe90 2022-06-17 op return err;
6970 e78dc838 2020-12-04 stsp view->focus_child = 1;
6971 669b5ffa 2018-10-07 stsp } else
6972 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6973 1e37a5c2 2019-05-12 jcs }
6974 1e37a5c2 2019-05-12 jcs break;
6975 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6976 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6977 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6978 640cd7ff 2022-06-22 mark view->count = 0;
6979 1e37a5c2 2019-05-12 jcs break;
6980 1e37a5c2 2019-05-12 jcs default:
6981 640cd7ff 2022-06-22 mark view->count = 0;
6982 1e37a5c2 2019-05-12 jcs break;
6983 ffd1d5e5 2018-06-23 stsp }
6984 e5a0f69f 2018-08-18 stsp
6985 ffd1d5e5 2018-06-23 stsp return err;
6986 9f7d7167 2018-04-29 stsp }
6987 9f7d7167 2018-04-29 stsp
6988 ffd1d5e5 2018-06-23 stsp __dead static void
6989 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6990 ffd1d5e5 2018-06-23 stsp {
6991 ffd1d5e5 2018-06-23 stsp endwin();
6992 87411fa9 2022-06-16 stsp fprintf(stderr,
6993 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6994 ffd1d5e5 2018-06-23 stsp getprogname());
6995 ffd1d5e5 2018-06-23 stsp exit(1);
6996 ffd1d5e5 2018-06-23 stsp }
6997 ffd1d5e5 2018-06-23 stsp
6998 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6999 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7000 ffd1d5e5 2018-06-23 stsp {
7001 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7002 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7003 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7004 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7005 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7006 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7007 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7008 4e97c21c 2020-12-06 stsp char *label = NULL;
7009 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7010 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7011 ffd1d5e5 2018-06-23 stsp int ch;
7012 5221c383 2018-08-01 stsp struct tog_view *view;
7013 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7014 70ac5f84 2019-03-28 stsp
7015 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7016 ffd1d5e5 2018-06-23 stsp switch (ch) {
7017 ffd1d5e5 2018-06-23 stsp case 'c':
7018 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7019 74283ab8 2020-02-07 stsp break;
7020 74283ab8 2020-02-07 stsp case 'r':
7021 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7022 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7023 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7024 74283ab8 2020-02-07 stsp optarg);
7025 ffd1d5e5 2018-06-23 stsp break;
7026 ffd1d5e5 2018-06-23 stsp default:
7027 e99e2d15 2020-11-24 naddy usage_tree();
7028 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7029 ffd1d5e5 2018-06-23 stsp }
7030 ffd1d5e5 2018-06-23 stsp }
7031 ffd1d5e5 2018-06-23 stsp
7032 ffd1d5e5 2018-06-23 stsp argc -= optind;
7033 ffd1d5e5 2018-06-23 stsp argv += optind;
7034 ffd1d5e5 2018-06-23 stsp
7035 55cccc34 2020-02-20 stsp if (argc > 1)
7036 e99e2d15 2020-11-24 naddy usage_tree();
7037 0ae84acc 2022-06-15 tracey
7038 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7039 0ae84acc 2022-06-15 tracey if (error != NULL)
7040 0ae84acc 2022-06-15 tracey goto done;
7041 74283ab8 2020-02-07 stsp
7042 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7043 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7044 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7045 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7046 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7047 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7048 c156c7a4 2020-12-18 stsp goto done;
7049 55cccc34 2020-02-20 stsp if (worktree)
7050 52185f70 2019-02-05 stsp repo_path =
7051 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7052 55cccc34 2020-02-20 stsp else
7053 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7054 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7055 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7056 c156c7a4 2020-12-18 stsp goto done;
7057 c156c7a4 2020-12-18 stsp }
7058 55cccc34 2020-02-20 stsp }
7059 a915003a 2019-02-05 stsp
7060 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7061 c02c541e 2019-03-29 stsp if (error != NULL)
7062 52185f70 2019-02-05 stsp goto done;
7063 d188b9a6 2019-01-04 stsp
7064 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7065 55cccc34 2020-02-20 stsp repo, worktree);
7066 55cccc34 2020-02-20 stsp if (error)
7067 55cccc34 2020-02-20 stsp goto done;
7068 55cccc34 2020-02-20 stsp
7069 55cccc34 2020-02-20 stsp init_curses();
7070 55cccc34 2020-02-20 stsp
7071 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7072 c02c541e 2019-03-29 stsp if (error)
7073 52185f70 2019-02-05 stsp goto done;
7074 ffd1d5e5 2018-06-23 stsp
7075 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7076 51a10b52 2020-12-26 stsp if (error)
7077 51a10b52 2020-12-26 stsp goto done;
7078 51a10b52 2020-12-26 stsp
7079 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7080 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7081 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7082 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7083 4e97c21c 2020-12-06 stsp if (error)
7084 4e97c21c 2020-12-06 stsp goto done;
7085 4e97c21c 2020-12-06 stsp head_ref_name = label;
7086 4e97c21c 2020-12-06 stsp } else {
7087 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7088 4e97c21c 2020-12-06 stsp if (error == NULL)
7089 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7090 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7091 4e97c21c 2020-12-06 stsp goto done;
7092 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7093 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7094 4e97c21c 2020-12-06 stsp if (error)
7095 4e97c21c 2020-12-06 stsp goto done;
7096 4e97c21c 2020-12-06 stsp }
7097 ffd1d5e5 2018-06-23 stsp
7098 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7099 a44927cc 2022-04-07 stsp if (error)
7100 a44927cc 2022-04-07 stsp goto done;
7101 a44927cc 2022-04-07 stsp
7102 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7103 5221c383 2018-08-01 stsp if (view == NULL) {
7104 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7105 5221c383 2018-08-01 stsp goto done;
7106 5221c383 2018-08-01 stsp }
7107 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7108 ad80ab7b 2018-08-04 stsp if (error)
7109 ad80ab7b 2018-08-04 stsp goto done;
7110 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7111 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7112 d91faf3b 2020-12-01 naddy in_repo_path);
7113 55cccc34 2020-02-20 stsp if (error)
7114 55cccc34 2020-02-20 stsp goto done;
7115 55cccc34 2020-02-20 stsp }
7116 55cccc34 2020-02-20 stsp
7117 55cccc34 2020-02-20 stsp if (worktree) {
7118 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7119 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7120 55cccc34 2020-02-20 stsp worktree = NULL;
7121 55cccc34 2020-02-20 stsp }
7122 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7123 ffd1d5e5 2018-06-23 stsp done:
7124 52185f70 2019-02-05 stsp free(repo_path);
7125 e4a0e26d 2020-02-20 stsp free(cwd);
7126 ffd1d5e5 2018-06-23 stsp free(commit_id);
7127 4e97c21c 2020-12-06 stsp free(label);
7128 486cd271 2020-12-06 stsp if (ref)
7129 486cd271 2020-12-06 stsp got_ref_close(ref);
7130 1d0f4054 2021-06-17 stsp if (repo) {
7131 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7132 1d0f4054 2021-06-17 stsp if (error == NULL)
7133 1d0f4054 2021-06-17 stsp error = close_err;
7134 1d0f4054 2021-06-17 stsp }
7135 0ae84acc 2022-06-15 tracey if (pack_fds) {
7136 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7137 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7138 0ae84acc 2022-06-15 tracey if (error == NULL)
7139 0ae84acc 2022-06-15 tracey error = pack_err;
7140 0ae84acc 2022-06-15 tracey }
7141 51a10b52 2020-12-26 stsp tog_free_refs();
7142 ffd1d5e5 2018-06-23 stsp return error;
7143 6458efa5 2020-11-24 stsp }
7144 6458efa5 2020-11-24 stsp
7145 6458efa5 2020-11-24 stsp static const struct got_error *
7146 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7147 6458efa5 2020-11-24 stsp {
7148 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7149 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7150 6458efa5 2020-11-24 stsp
7151 6458efa5 2020-11-24 stsp s->nrefs = 0;
7152 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7153 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7154 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7155 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7156 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7157 6458efa5 2020-11-24 stsp continue;
7158 6458efa5 2020-11-24 stsp
7159 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7160 6458efa5 2020-11-24 stsp if (re == NULL)
7161 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7162 6458efa5 2020-11-24 stsp
7163 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7164 8924d611 2020-12-26 stsp if (re->ref == NULL)
7165 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7166 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7167 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7168 6458efa5 2020-11-24 stsp }
7169 6458efa5 2020-11-24 stsp
7170 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7171 6458efa5 2020-11-24 stsp return NULL;
7172 6458efa5 2020-11-24 stsp }
7173 6458efa5 2020-11-24 stsp
7174 336075a4 2022-06-25 op static void
7175 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7176 6458efa5 2020-11-24 stsp {
7177 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7178 6458efa5 2020-11-24 stsp
7179 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7180 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7181 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7182 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7183 6458efa5 2020-11-24 stsp free(re);
7184 6458efa5 2020-11-24 stsp }
7185 6458efa5 2020-11-24 stsp }
7186 6458efa5 2020-11-24 stsp
7187 6458efa5 2020-11-24 stsp static const struct got_error *
7188 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7189 6458efa5 2020-11-24 stsp {
7190 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7191 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7192 6458efa5 2020-11-24 stsp
7193 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7194 6458efa5 2020-11-24 stsp s->repo = repo;
7195 6458efa5 2020-11-24 stsp
7196 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7197 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7198 6458efa5 2020-11-24 stsp
7199 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7200 6458efa5 2020-11-24 stsp if (err)
7201 6458efa5 2020-11-24 stsp return err;
7202 34ba6917 2020-11-30 stsp
7203 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7204 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7205 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7206 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7207 6458efa5 2020-11-24 stsp if (err)
7208 6458efa5 2020-11-24 stsp goto done;
7209 6458efa5 2020-11-24 stsp
7210 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7211 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7212 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7213 6458efa5 2020-11-24 stsp if (err)
7214 6458efa5 2020-11-24 stsp goto done;
7215 6458efa5 2020-11-24 stsp
7216 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7217 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7218 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7219 cc488aa7 2022-01-23 stsp if (err)
7220 cc488aa7 2022-01-23 stsp goto done;
7221 cc488aa7 2022-01-23 stsp
7222 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7223 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7224 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7225 6458efa5 2020-11-24 stsp if (err)
7226 6458efa5 2020-11-24 stsp goto done;
7227 6458efa5 2020-11-24 stsp }
7228 6458efa5 2020-11-24 stsp
7229 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7230 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7231 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7232 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7233 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7234 6458efa5 2020-11-24 stsp done:
7235 6458efa5 2020-11-24 stsp if (err)
7236 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7237 6458efa5 2020-11-24 stsp return err;
7238 6458efa5 2020-11-24 stsp }
7239 6458efa5 2020-11-24 stsp
7240 6458efa5 2020-11-24 stsp static const struct got_error *
7241 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7242 6458efa5 2020-11-24 stsp {
7243 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7244 6458efa5 2020-11-24 stsp
7245 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7246 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7247 6458efa5 2020-11-24 stsp
7248 6458efa5 2020-11-24 stsp return NULL;
7249 9f7d7167 2018-04-29 stsp }
7250 ce5b7c56 2019-07-09 stsp
7251 6458efa5 2020-11-24 stsp static const struct got_error *
7252 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7253 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7254 6458efa5 2020-11-24 stsp {
7255 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7256 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7257 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7258 6458efa5 2020-11-24 stsp int obj_type;
7259 6458efa5 2020-11-24 stsp
7260 c42c9805 2020-11-24 stsp *commit_id = NULL;
7261 6458efa5 2020-11-24 stsp
7262 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7263 6458efa5 2020-11-24 stsp if (err)
7264 6458efa5 2020-11-24 stsp return err;
7265 6458efa5 2020-11-24 stsp
7266 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7267 6458efa5 2020-11-24 stsp if (err)
7268 6458efa5 2020-11-24 stsp goto done;
7269 6458efa5 2020-11-24 stsp
7270 6458efa5 2020-11-24 stsp switch (obj_type) {
7271 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7272 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7273 6458efa5 2020-11-24 stsp break;
7274 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7275 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7276 6458efa5 2020-11-24 stsp if (err)
7277 6458efa5 2020-11-24 stsp goto done;
7278 c42c9805 2020-11-24 stsp free(obj_id);
7279 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7280 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7281 c42c9805 2020-11-24 stsp if (err)
7282 6458efa5 2020-11-24 stsp goto done;
7283 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7284 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7285 c42c9805 2020-11-24 stsp goto done;
7286 c42c9805 2020-11-24 stsp }
7287 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7288 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7289 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7290 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7291 c42c9805 2020-11-24 stsp goto done;
7292 c42c9805 2020-11-24 stsp }
7293 6458efa5 2020-11-24 stsp break;
7294 6458efa5 2020-11-24 stsp default:
7295 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7296 c42c9805 2020-11-24 stsp break;
7297 6458efa5 2020-11-24 stsp }
7298 6458efa5 2020-11-24 stsp
7299 c42c9805 2020-11-24 stsp done:
7300 c42c9805 2020-11-24 stsp if (tag)
7301 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7302 c42c9805 2020-11-24 stsp if (err) {
7303 c42c9805 2020-11-24 stsp free(*commit_id);
7304 c42c9805 2020-11-24 stsp *commit_id = NULL;
7305 c42c9805 2020-11-24 stsp }
7306 c42c9805 2020-11-24 stsp return err;
7307 c42c9805 2020-11-24 stsp }
7308 c42c9805 2020-11-24 stsp
7309 c42c9805 2020-11-24 stsp static const struct got_error *
7310 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7311 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7312 c42c9805 2020-11-24 stsp {
7313 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7314 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7315 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7316 c42c9805 2020-11-24 stsp
7317 c42c9805 2020-11-24 stsp *new_view = NULL;
7318 c42c9805 2020-11-24 stsp
7319 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7320 c42c9805 2020-11-24 stsp if (err) {
7321 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7322 c42c9805 2020-11-24 stsp return err;
7323 c42c9805 2020-11-24 stsp else
7324 c42c9805 2020-11-24 stsp return NULL;
7325 c42c9805 2020-11-24 stsp }
7326 c42c9805 2020-11-24 stsp
7327 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7328 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7329 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7330 6458efa5 2020-11-24 stsp goto done;
7331 6458efa5 2020-11-24 stsp }
7332 6458efa5 2020-11-24 stsp
7333 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7334 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7335 6458efa5 2020-11-24 stsp done:
7336 6458efa5 2020-11-24 stsp if (err)
7337 6458efa5 2020-11-24 stsp view_close(log_view);
7338 6458efa5 2020-11-24 stsp else
7339 6458efa5 2020-11-24 stsp *new_view = log_view;
7340 c42c9805 2020-11-24 stsp free(commit_id);
7341 6458efa5 2020-11-24 stsp return err;
7342 6458efa5 2020-11-24 stsp }
7343 6458efa5 2020-11-24 stsp
7344 ce5b7c56 2019-07-09 stsp static void
7345 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7346 6458efa5 2020-11-24 stsp {
7347 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7348 34ba6917 2020-11-30 stsp int i = 0;
7349 6458efa5 2020-11-24 stsp
7350 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7351 6458efa5 2020-11-24 stsp return;
7352 6458efa5 2020-11-24 stsp
7353 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7354 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7355 34ba6917 2020-11-30 stsp if (re == NULL)
7356 34ba6917 2020-11-30 stsp break;
7357 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7358 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7359 6458efa5 2020-11-24 stsp }
7360 6458efa5 2020-11-24 stsp }
7361 6458efa5 2020-11-24 stsp
7362 9b058f45 2022-06-30 mark static const struct got_error *
7363 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7364 6458efa5 2020-11-24 stsp {
7365 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7366 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7367 6458efa5 2020-11-24 stsp int n = 0;
7368 6458efa5 2020-11-24 stsp
7369 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7370 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7371 6458efa5 2020-11-24 stsp else
7372 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7373 6458efa5 2020-11-24 stsp
7374 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7375 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7376 9b058f45 2022-06-30 mark if (last)
7377 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7378 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7379 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7380 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7381 6458efa5 2020-11-24 stsp }
7382 6458efa5 2020-11-24 stsp }
7383 9b058f45 2022-06-30 mark
7384 9b058f45 2022-06-30 mark return NULL;
7385 6458efa5 2020-11-24 stsp }
7386 6458efa5 2020-11-24 stsp
7387 6458efa5 2020-11-24 stsp static const struct got_error *
7388 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7389 6458efa5 2020-11-24 stsp {
7390 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7391 6458efa5 2020-11-24 stsp
7392 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7393 6458efa5 2020-11-24 stsp return NULL;
7394 6458efa5 2020-11-24 stsp }
7395 6458efa5 2020-11-24 stsp
7396 6458efa5 2020-11-24 stsp static int
7397 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7398 6458efa5 2020-11-24 stsp {
7399 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7400 6458efa5 2020-11-24 stsp
7401 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7402 6458efa5 2020-11-24 stsp 0) == 0;
7403 6458efa5 2020-11-24 stsp }
7404 6458efa5 2020-11-24 stsp
7405 6458efa5 2020-11-24 stsp static const struct got_error *
7406 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7407 6458efa5 2020-11-24 stsp {
7408 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7409 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7410 6458efa5 2020-11-24 stsp
7411 6458efa5 2020-11-24 stsp if (!view->searching) {
7412 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7413 6458efa5 2020-11-24 stsp return NULL;
7414 6458efa5 2020-11-24 stsp }
7415 6458efa5 2020-11-24 stsp
7416 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7417 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7418 6458efa5 2020-11-24 stsp if (s->selected_entry)
7419 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7420 6458efa5 2020-11-24 stsp else
7421 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7422 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7423 6458efa5 2020-11-24 stsp } else {
7424 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7425 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7426 6458efa5 2020-11-24 stsp else
7427 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7428 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7429 6458efa5 2020-11-24 stsp }
7430 6458efa5 2020-11-24 stsp } else {
7431 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7432 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7433 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7434 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7435 6458efa5 2020-11-24 stsp else
7436 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7437 6458efa5 2020-11-24 stsp }
7438 6458efa5 2020-11-24 stsp
7439 6458efa5 2020-11-24 stsp while (1) {
7440 6458efa5 2020-11-24 stsp if (re == NULL) {
7441 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7442 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7443 6458efa5 2020-11-24 stsp return NULL;
7444 6458efa5 2020-11-24 stsp }
7445 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7446 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7447 6458efa5 2020-11-24 stsp else
7448 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7449 6458efa5 2020-11-24 stsp }
7450 6458efa5 2020-11-24 stsp
7451 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7452 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7453 6458efa5 2020-11-24 stsp s->matched_entry = re;
7454 6458efa5 2020-11-24 stsp break;
7455 6458efa5 2020-11-24 stsp }
7456 6458efa5 2020-11-24 stsp
7457 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7458 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7459 6458efa5 2020-11-24 stsp else
7460 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7461 6458efa5 2020-11-24 stsp }
7462 6458efa5 2020-11-24 stsp
7463 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7464 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7465 6458efa5 2020-11-24 stsp s->selected = 0;
7466 6458efa5 2020-11-24 stsp }
7467 6458efa5 2020-11-24 stsp
7468 6458efa5 2020-11-24 stsp return NULL;
7469 6458efa5 2020-11-24 stsp }
7470 6458efa5 2020-11-24 stsp
7471 6458efa5 2020-11-24 stsp static const struct got_error *
7472 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7473 6458efa5 2020-11-24 stsp {
7474 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7475 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7477 6458efa5 2020-11-24 stsp char *line = NULL;
7478 6458efa5 2020-11-24 stsp wchar_t *wline;
7479 6458efa5 2020-11-24 stsp struct tog_color *tc;
7480 6458efa5 2020-11-24 stsp int width, n;
7481 6458efa5 2020-11-24 stsp int limit = view->nlines;
7482 6458efa5 2020-11-24 stsp
7483 6458efa5 2020-11-24 stsp werase(view->window);
7484 6458efa5 2020-11-24 stsp
7485 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7486 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7487 9b058f45 2022-06-30 mark --limit; /* border */
7488 6458efa5 2020-11-24 stsp
7489 6458efa5 2020-11-24 stsp if (limit == 0)
7490 6458efa5 2020-11-24 stsp return NULL;
7491 6458efa5 2020-11-24 stsp
7492 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7493 6458efa5 2020-11-24 stsp
7494 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7495 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7496 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7497 6458efa5 2020-11-24 stsp
7498 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7499 6458efa5 2020-11-24 stsp if (err) {
7500 6458efa5 2020-11-24 stsp free(line);
7501 6458efa5 2020-11-24 stsp return err;
7502 6458efa5 2020-11-24 stsp }
7503 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7504 6458efa5 2020-11-24 stsp wstandout(view->window);
7505 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7506 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7507 6458efa5 2020-11-24 stsp wstandend(view->window);
7508 6458efa5 2020-11-24 stsp free(wline);
7509 6458efa5 2020-11-24 stsp wline = NULL;
7510 6458efa5 2020-11-24 stsp free(line);
7511 6458efa5 2020-11-24 stsp line = NULL;
7512 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7513 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7514 6458efa5 2020-11-24 stsp if (--limit <= 0)
7515 6458efa5 2020-11-24 stsp return NULL;
7516 6458efa5 2020-11-24 stsp
7517 6458efa5 2020-11-24 stsp n = 0;
7518 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7519 6458efa5 2020-11-24 stsp char *line = NULL;
7520 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7521 6458efa5 2020-11-24 stsp
7522 b4996bee 2022-06-16 stsp if (s->show_date) {
7523 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7524 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7525 b4996bee 2022-06-16 stsp struct got_object_id *id;
7526 b4996bee 2022-06-16 stsp struct tm tm;
7527 b4996bee 2022-06-16 stsp time_t t;
7528 b4996bee 2022-06-16 stsp
7529 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7530 b4996bee 2022-06-16 stsp if (err)
7531 b4996bee 2022-06-16 stsp return err;
7532 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7533 b4996bee 2022-06-16 stsp if (err) {
7534 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7535 b4996bee 2022-06-16 stsp free(id);
7536 b4996bee 2022-06-16 stsp return err;
7537 b4996bee 2022-06-16 stsp }
7538 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7539 b4996bee 2022-06-16 stsp id);
7540 b4996bee 2022-06-16 stsp if (err) {
7541 b4996bee 2022-06-16 stsp free(id);
7542 b4996bee 2022-06-16 stsp return err;
7543 b4996bee 2022-06-16 stsp }
7544 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7545 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7546 b4996bee 2022-06-16 stsp } else {
7547 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7548 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7549 b4996bee 2022-06-16 stsp }
7550 b4996bee 2022-06-16 stsp free(id);
7551 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7552 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7553 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7554 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7555 b4996bee 2022-06-16 stsp }
7556 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7557 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7558 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7559 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7560 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7561 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7562 6458efa5 2020-11-24 stsp struct got_object_id *id;
7563 6458efa5 2020-11-24 stsp char *id_str;
7564 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7565 6458efa5 2020-11-24 stsp if (err)
7566 6458efa5 2020-11-24 stsp return err;
7567 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7568 6458efa5 2020-11-24 stsp if (err) {
7569 6458efa5 2020-11-24 stsp free(id);
7570 6458efa5 2020-11-24 stsp return err;
7571 6458efa5 2020-11-24 stsp }
7572 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7573 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7574 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7575 6458efa5 2020-11-24 stsp free(id);
7576 6458efa5 2020-11-24 stsp free(id_str);
7577 6458efa5 2020-11-24 stsp return err;
7578 6458efa5 2020-11-24 stsp }
7579 6458efa5 2020-11-24 stsp free(id);
7580 6458efa5 2020-11-24 stsp free(id_str);
7581 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7582 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7583 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7584 6458efa5 2020-11-24 stsp
7585 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7586 ccda2f4d 2022-06-16 stsp 0, 0);
7587 6458efa5 2020-11-24 stsp if (err) {
7588 6458efa5 2020-11-24 stsp free(line);
7589 6458efa5 2020-11-24 stsp return err;
7590 6458efa5 2020-11-24 stsp }
7591 6458efa5 2020-11-24 stsp if (n == s->selected) {
7592 6458efa5 2020-11-24 stsp if (view->focussed)
7593 6458efa5 2020-11-24 stsp wstandout(view->window);
7594 6458efa5 2020-11-24 stsp s->selected_entry = re;
7595 6458efa5 2020-11-24 stsp }
7596 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7597 6458efa5 2020-11-24 stsp if (tc)
7598 6458efa5 2020-11-24 stsp wattr_on(view->window,
7599 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7600 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7601 6458efa5 2020-11-24 stsp if (tc)
7602 6458efa5 2020-11-24 stsp wattr_off(view->window,
7603 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7604 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7605 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7606 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7607 6458efa5 2020-11-24 stsp wstandend(view->window);
7608 6458efa5 2020-11-24 stsp free(line);
7609 6458efa5 2020-11-24 stsp free(wline);
7610 6458efa5 2020-11-24 stsp wline = NULL;
7611 6458efa5 2020-11-24 stsp n++;
7612 6458efa5 2020-11-24 stsp s->ndisplayed++;
7613 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7614 6458efa5 2020-11-24 stsp
7615 6458efa5 2020-11-24 stsp limit--;
7616 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7617 6458efa5 2020-11-24 stsp }
7618 6458efa5 2020-11-24 stsp
7619 9b058f45 2022-06-30 mark view_border(view);
7620 6458efa5 2020-11-24 stsp return err;
7621 6458efa5 2020-11-24 stsp }
7622 6458efa5 2020-11-24 stsp
7623 6458efa5 2020-11-24 stsp static const struct got_error *
7624 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7625 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7626 c42c9805 2020-11-24 stsp {
7627 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7628 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7629 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7630 c42c9805 2020-11-24 stsp
7631 c42c9805 2020-11-24 stsp *new_view = NULL;
7632 c42c9805 2020-11-24 stsp
7633 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7634 c42c9805 2020-11-24 stsp if (err) {
7635 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7636 c42c9805 2020-11-24 stsp return err;
7637 c42c9805 2020-11-24 stsp else
7638 c42c9805 2020-11-24 stsp return NULL;
7639 c42c9805 2020-11-24 stsp }
7640 c42c9805 2020-11-24 stsp
7641 c42c9805 2020-11-24 stsp
7642 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7643 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7644 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7645 c42c9805 2020-11-24 stsp goto done;
7646 c42c9805 2020-11-24 stsp }
7647 c42c9805 2020-11-24 stsp
7648 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7649 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7650 c42c9805 2020-11-24 stsp if (err)
7651 c42c9805 2020-11-24 stsp goto done;
7652 c42c9805 2020-11-24 stsp
7653 c42c9805 2020-11-24 stsp *new_view = tree_view;
7654 c42c9805 2020-11-24 stsp done:
7655 c42c9805 2020-11-24 stsp free(commit_id);
7656 c42c9805 2020-11-24 stsp return err;
7657 c42c9805 2020-11-24 stsp }
7658 c42c9805 2020-11-24 stsp static const struct got_error *
7659 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7660 6458efa5 2020-11-24 stsp {
7661 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7662 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7663 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7664 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7665 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7666 6458efa5 2020-11-24 stsp
7667 6458efa5 2020-11-24 stsp switch (ch) {
7668 6458efa5 2020-11-24 stsp case 'i':
7669 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7670 640cd7ff 2022-06-22 mark view->count = 0;
7671 7f66531d 2021-11-16 stsp break;
7672 b4996bee 2022-06-16 stsp case 'm':
7673 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7674 640cd7ff 2022-06-22 mark view->count = 0;
7675 b4996bee 2022-06-16 stsp break;
7676 07a065fe 2021-11-20 stsp case 'o':
7677 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7678 640cd7ff 2022-06-22 mark view->count = 0;
7679 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7680 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7681 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7682 50617b77 2021-11-20 stsp if (err)
7683 50617b77 2021-11-20 stsp break;
7684 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7685 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7686 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7687 7f66531d 2021-11-16 stsp if (err)
7688 7f66531d 2021-11-16 stsp break;
7689 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7690 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7691 6458efa5 2020-11-24 stsp break;
7692 6458efa5 2020-11-24 stsp case KEY_ENTER:
7693 6458efa5 2020-11-24 stsp case '\r':
7694 640cd7ff 2022-06-22 mark view->count = 0;
7695 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7696 6458efa5 2020-11-24 stsp break;
7697 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7698 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7699 9b058f45 2022-06-30 mark
7700 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7701 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7702 9b058f45 2022-06-30 mark if (err)
7703 9b058f45 2022-06-30 mark break;
7704 9b058f45 2022-06-30 mark
7705 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7706 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7707 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7708 9b058f45 2022-06-30 mark if (err)
7709 9b058f45 2022-06-30 mark break;
7710 9b058f45 2022-06-30 mark }
7711 9b058f45 2022-06-30 mark
7712 e78dc838 2020-12-04 stsp view->focussed = 0;
7713 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7714 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7715 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7716 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7717 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
7718 6458efa5 2020-11-24 stsp err = view_close_child(view);
7719 6458efa5 2020-11-24 stsp if (err)
7720 6458efa5 2020-11-24 stsp return err;
7721 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7722 0dbbbe90 2022-06-17 op if (err)
7723 0dbbbe90 2022-06-17 op return err;
7724 e78dc838 2020-12-04 stsp view->focus_child = 1;
7725 6458efa5 2020-11-24 stsp } else
7726 6458efa5 2020-11-24 stsp *new_view = log_view;
7727 6458efa5 2020-11-24 stsp break;
7728 c42c9805 2020-11-24 stsp case 't':
7729 640cd7ff 2022-06-22 mark view->count = 0;
7730 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7731 c42c9805 2020-11-24 stsp break;
7732 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7733 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7734 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7735 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7736 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7737 c42c9805 2020-11-24 stsp break;
7738 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7739 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7740 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7741 49b24ee5 2022-07-03 mark if (err)
7742 49b24ee5 2022-07-03 mark break;
7743 49b24ee5 2022-07-03 mark }
7744 e78dc838 2020-12-04 stsp view->focussed = 0;
7745 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7746 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7747 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7748 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7749 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
7750 c42c9805 2020-11-24 stsp err = view_close_child(view);
7751 c42c9805 2020-11-24 stsp if (err)
7752 c42c9805 2020-11-24 stsp return err;
7753 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7754 0dbbbe90 2022-06-17 op if (err)
7755 0dbbbe90 2022-06-17 op return err;
7756 e78dc838 2020-12-04 stsp view->focus_child = 1;
7757 c42c9805 2020-11-24 stsp } else
7758 c42c9805 2020-11-24 stsp *new_view = tree_view;
7759 c42c9805 2020-11-24 stsp break;
7760 e4526bf5 2021-09-03 naddy case 'g':
7761 e4526bf5 2021-09-03 naddy case KEY_HOME:
7762 e4526bf5 2021-09-03 naddy s->selected = 0;
7763 640cd7ff 2022-06-22 mark view->count = 0;
7764 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7765 e4526bf5 2021-09-03 naddy break;
7766 e4526bf5 2021-09-03 naddy case 'G':
7767 9b058f45 2022-06-30 mark case KEY_END: {
7768 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7769 9b058f45 2022-06-30 mark
7770 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7771 9b058f45 2022-06-30 mark --eos; /* border */
7772 e4526bf5 2021-09-03 naddy s->selected = 0;
7773 640cd7ff 2022-06-22 mark view->count = 0;
7774 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7775 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7776 e4526bf5 2021-09-03 naddy if (re == NULL)
7777 e4526bf5 2021-09-03 naddy break;
7778 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7779 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7780 e4526bf5 2021-09-03 naddy }
7781 e4526bf5 2021-09-03 naddy if (n > 0)
7782 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7783 e4526bf5 2021-09-03 naddy break;
7784 9b058f45 2022-06-30 mark }
7785 6458efa5 2020-11-24 stsp case 'k':
7786 6458efa5 2020-11-24 stsp case KEY_UP:
7787 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7788 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7789 6458efa5 2020-11-24 stsp s->selected--;
7790 6458efa5 2020-11-24 stsp break;
7791 34ba6917 2020-11-30 stsp }
7792 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7793 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7794 640cd7ff 2022-06-22 mark view->count = 0;
7795 6458efa5 2020-11-24 stsp break;
7796 83cc4199 2022-06-13 stsp case CTRL('u'):
7797 33c3719a 2022-06-15 stsp case 'u':
7798 83cc4199 2022-06-13 stsp nscroll /= 2;
7799 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7800 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7801 6458efa5 2020-11-24 stsp case CTRL('b'):
7802 61417565 2022-06-20 mark case 'b':
7803 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7804 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7805 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7806 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7807 640cd7ff 2022-06-22 mark view->count = 0;
7808 6458efa5 2020-11-24 stsp break;
7809 6458efa5 2020-11-24 stsp case 'j':
7810 6458efa5 2020-11-24 stsp case KEY_DOWN:
7811 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7812 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7813 6458efa5 2020-11-24 stsp s->selected++;
7814 6458efa5 2020-11-24 stsp break;
7815 6458efa5 2020-11-24 stsp }
7816 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7817 6458efa5 2020-11-24 stsp /* can't scroll any further */
7818 640cd7ff 2022-06-22 mark view->count = 0;
7819 6458efa5 2020-11-24 stsp break;
7820 640cd7ff 2022-06-22 mark }
7821 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7822 6458efa5 2020-11-24 stsp break;
7823 83cc4199 2022-06-13 stsp case CTRL('d'):
7824 33c3719a 2022-06-15 stsp case 'd':
7825 83cc4199 2022-06-13 stsp nscroll /= 2;
7826 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7827 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7828 6458efa5 2020-11-24 stsp case CTRL('f'):
7829 61417565 2022-06-20 mark case 'f':
7830 48bb96f0 2022-06-20 naddy case ' ':
7831 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7832 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7833 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7834 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7835 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7836 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7837 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7838 640cd7ff 2022-06-22 mark view->count = 0;
7839 6458efa5 2020-11-24 stsp break;
7840 6458efa5 2020-11-24 stsp }
7841 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7842 6458efa5 2020-11-24 stsp break;
7843 6458efa5 2020-11-24 stsp case CTRL('l'):
7844 640cd7ff 2022-06-22 mark view->count = 0;
7845 8924d611 2020-12-26 stsp tog_free_refs();
7846 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7847 8924d611 2020-12-26 stsp if (err)
7848 8924d611 2020-12-26 stsp break;
7849 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7850 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7851 6458efa5 2020-11-24 stsp break;
7852 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7853 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7854 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7855 6458efa5 2020-11-24 stsp break;
7856 6458efa5 2020-11-24 stsp default:
7857 640cd7ff 2022-06-22 mark view->count = 0;
7858 6458efa5 2020-11-24 stsp break;
7859 6458efa5 2020-11-24 stsp }
7860 6458efa5 2020-11-24 stsp
7861 6458efa5 2020-11-24 stsp return err;
7862 6458efa5 2020-11-24 stsp }
7863 6458efa5 2020-11-24 stsp
7864 6458efa5 2020-11-24 stsp __dead static void
7865 6458efa5 2020-11-24 stsp usage_ref(void)
7866 6458efa5 2020-11-24 stsp {
7867 6458efa5 2020-11-24 stsp endwin();
7868 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7869 6458efa5 2020-11-24 stsp getprogname());
7870 6458efa5 2020-11-24 stsp exit(1);
7871 6458efa5 2020-11-24 stsp }
7872 6458efa5 2020-11-24 stsp
7873 6458efa5 2020-11-24 stsp static const struct got_error *
7874 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7875 6458efa5 2020-11-24 stsp {
7876 6458efa5 2020-11-24 stsp const struct got_error *error;
7877 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7878 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7879 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7880 6458efa5 2020-11-24 stsp int ch;
7881 6458efa5 2020-11-24 stsp struct tog_view *view;
7882 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7883 6458efa5 2020-11-24 stsp
7884 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7885 6458efa5 2020-11-24 stsp switch (ch) {
7886 6458efa5 2020-11-24 stsp case 'r':
7887 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7888 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7889 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7890 6458efa5 2020-11-24 stsp optarg);
7891 6458efa5 2020-11-24 stsp break;
7892 6458efa5 2020-11-24 stsp default:
7893 e99e2d15 2020-11-24 naddy usage_ref();
7894 6458efa5 2020-11-24 stsp /* NOTREACHED */
7895 6458efa5 2020-11-24 stsp }
7896 6458efa5 2020-11-24 stsp }
7897 6458efa5 2020-11-24 stsp
7898 6458efa5 2020-11-24 stsp argc -= optind;
7899 6458efa5 2020-11-24 stsp argv += optind;
7900 6458efa5 2020-11-24 stsp
7901 6458efa5 2020-11-24 stsp if (argc > 1)
7902 e99e2d15 2020-11-24 naddy usage_ref();
7903 0ae84acc 2022-06-15 tracey
7904 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7905 0ae84acc 2022-06-15 tracey if (error != NULL)
7906 0ae84acc 2022-06-15 tracey goto done;
7907 6458efa5 2020-11-24 stsp
7908 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7909 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7910 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7911 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7912 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7913 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7914 c156c7a4 2020-12-18 stsp goto done;
7915 6458efa5 2020-11-24 stsp if (worktree)
7916 6458efa5 2020-11-24 stsp repo_path =
7917 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7918 6458efa5 2020-11-24 stsp else
7919 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7920 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7921 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7922 c156c7a4 2020-12-18 stsp goto done;
7923 c156c7a4 2020-12-18 stsp }
7924 6458efa5 2020-11-24 stsp }
7925 6458efa5 2020-11-24 stsp
7926 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7927 6458efa5 2020-11-24 stsp if (error != NULL)
7928 6458efa5 2020-11-24 stsp goto done;
7929 6458efa5 2020-11-24 stsp
7930 6458efa5 2020-11-24 stsp init_curses();
7931 6458efa5 2020-11-24 stsp
7932 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7933 51a10b52 2020-12-26 stsp if (error)
7934 51a10b52 2020-12-26 stsp goto done;
7935 51a10b52 2020-12-26 stsp
7936 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7937 6458efa5 2020-11-24 stsp if (error)
7938 6458efa5 2020-11-24 stsp goto done;
7939 6458efa5 2020-11-24 stsp
7940 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7941 6458efa5 2020-11-24 stsp if (view == NULL) {
7942 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7943 6458efa5 2020-11-24 stsp goto done;
7944 6458efa5 2020-11-24 stsp }
7945 6458efa5 2020-11-24 stsp
7946 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7947 6458efa5 2020-11-24 stsp if (error)
7948 6458efa5 2020-11-24 stsp goto done;
7949 6458efa5 2020-11-24 stsp
7950 6458efa5 2020-11-24 stsp if (worktree) {
7951 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7952 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7953 6458efa5 2020-11-24 stsp worktree = NULL;
7954 6458efa5 2020-11-24 stsp }
7955 6458efa5 2020-11-24 stsp error = view_loop(view);
7956 6458efa5 2020-11-24 stsp done:
7957 6458efa5 2020-11-24 stsp free(repo_path);
7958 6458efa5 2020-11-24 stsp free(cwd);
7959 1d0f4054 2021-06-17 stsp if (repo) {
7960 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7961 1d0f4054 2021-06-17 stsp if (close_err)
7962 1d0f4054 2021-06-17 stsp error = close_err;
7963 1d0f4054 2021-06-17 stsp }
7964 0ae84acc 2022-06-15 tracey if (pack_fds) {
7965 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7966 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7967 0ae84acc 2022-06-15 tracey if (error == NULL)
7968 0ae84acc 2022-06-15 tracey error = pack_err;
7969 0ae84acc 2022-06-15 tracey }
7970 51a10b52 2020-12-26 stsp tog_free_refs();
7971 6458efa5 2020-11-24 stsp return error;
7972 6458efa5 2020-11-24 stsp }
7973 6458efa5 2020-11-24 stsp
7974 9b058f45 2022-06-30 mark /*
7975 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7976 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7977 9b058f45 2022-06-30 mark */
7978 6458efa5 2020-11-24 stsp static void
7979 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7980 9b058f45 2022-06-30 mark {
7981 9b058f45 2022-06-30 mark switch (view->type) {
7982 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7983 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7984 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7985 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7986 9b058f45 2022-06-30 mark 1);
7987 9b058f45 2022-06-30 mark break;
7988 9b058f45 2022-06-30 mark }
7989 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7990 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7991 9b058f45 2022-06-30 mark else
7992 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7993 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7994 9b058f45 2022-06-30 mark break;
7995 9b058f45 2022-06-30 mark }
7996 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7997 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7998 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7999 9b058f45 2022-06-30 mark break;
8000 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8001 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8002 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8003 9b058f45 2022-06-30 mark break;
8004 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8005 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8006 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8007 9b058f45 2022-06-30 mark break;
8008 9b058f45 2022-06-30 mark default:
8009 9b058f45 2022-06-30 mark break;
8010 9b058f45 2022-06-30 mark }
8011 9b058f45 2022-06-30 mark
8012 9b058f45 2022-06-30 mark view->offset = 0;
8013 9b058f45 2022-06-30 mark }
8014 9b058f45 2022-06-30 mark
8015 9b058f45 2022-06-30 mark /*
8016 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8017 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8018 9b058f45 2022-06-30 mark */
8019 9b058f45 2022-06-30 mark static const struct got_error *
8020 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8021 9b058f45 2022-06-30 mark {
8022 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8023 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8024 9b058f45 2022-06-30 mark int *selected = NULL;
8025 9b058f45 2022-06-30 mark int header, offset;
8026 9b058f45 2022-06-30 mark
8027 9b058f45 2022-06-30 mark switch (view->type) {
8028 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8029 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8030 9b058f45 2022-06-30 mark header = 3;
8031 9b058f45 2022-06-30 mark scrolld = NULL;
8032 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8033 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8034 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8035 9b058f45 2022-06-30 mark s->selected_line -= offset;
8036 9b058f45 2022-06-30 mark view->offset = offset;
8037 9b058f45 2022-06-30 mark }
8038 9b058f45 2022-06-30 mark break;
8039 9b058f45 2022-06-30 mark }
8040 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8041 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8042 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8043 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8044 9b058f45 2022-06-30 mark selected = &s->selected;
8045 9b058f45 2022-06-30 mark break;
8046 9b058f45 2022-06-30 mark }
8047 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8048 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8049 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8050 9b058f45 2022-06-30 mark header = 3;
8051 9b058f45 2022-06-30 mark selected = &s->selected;
8052 9b058f45 2022-06-30 mark break;
8053 9b058f45 2022-06-30 mark }
8054 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8055 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8056 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8057 9b058f45 2022-06-30 mark header = 5;
8058 9b058f45 2022-06-30 mark selected = &s->selected;
8059 9b058f45 2022-06-30 mark break;
8060 9b058f45 2022-06-30 mark }
8061 9b058f45 2022-06-30 mark default:
8062 9b058f45 2022-06-30 mark selected = NULL;
8063 9b058f45 2022-06-30 mark scrolld = NULL;
8064 9b058f45 2022-06-30 mark header = 0;
8065 9b058f45 2022-06-30 mark break;
8066 9b058f45 2022-06-30 mark }
8067 9b058f45 2022-06-30 mark
8068 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8069 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8070 9b058f45 2022-06-30 mark view->offset = offset;
8071 9b058f45 2022-06-30 mark if (scrolld && offset) {
8072 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8073 9b058f45 2022-06-30 mark *selected -= offset;
8074 9b058f45 2022-06-30 mark }
8075 9b058f45 2022-06-30 mark }
8076 9b058f45 2022-06-30 mark
8077 9b058f45 2022-06-30 mark return err;
8078 9b058f45 2022-06-30 mark }
8079 9b058f45 2022-06-30 mark
8080 9b058f45 2022-06-30 mark static void
8081 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8082 ce5b7c56 2019-07-09 stsp {
8083 6059809a 2020-12-17 stsp size_t i;
8084 9f7d7167 2018-04-29 stsp
8085 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8086 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8087 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8088 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8089 ce5b7c56 2019-07-09 stsp }
8090 6879ba42 2020-10-01 naddy fputc('\n', fp);
8091 ce5b7c56 2019-07-09 stsp }
8092 ce5b7c56 2019-07-09 stsp
8093 4ed7e80c 2018-05-20 stsp __dead static void
8094 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8095 9f7d7167 2018-04-29 stsp {
8096 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8097 6879ba42 2020-10-01 naddy
8098 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8099 6879ba42 2020-10-01 naddy getprogname());
8100 6879ba42 2020-10-01 naddy if (hflag) {
8101 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8102 6879ba42 2020-10-01 naddy list_commands(fp);
8103 ee85c5e8 2020-02-29 stsp }
8104 6879ba42 2020-10-01 naddy exit(status);
8105 9f7d7167 2018-04-29 stsp }
8106 9f7d7167 2018-04-29 stsp
8107 c2301be8 2018-04-30 stsp static char **
8108 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8109 c2301be8 2018-04-30 stsp {
8110 ee85c5e8 2020-02-29 stsp va_list ap;
8111 c2301be8 2018-04-30 stsp char **argv;
8112 ee85c5e8 2020-02-29 stsp int i;
8113 c2301be8 2018-04-30 stsp
8114 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8115 ee85c5e8 2020-02-29 stsp
8116 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8117 c2301be8 2018-04-30 stsp if (argv == NULL)
8118 c2301be8 2018-04-30 stsp err(1, "calloc");
8119 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8120 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8121 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8122 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8123 c2301be8 2018-04-30 stsp }
8124 c2301be8 2018-04-30 stsp
8125 ee85c5e8 2020-02-29 stsp va_end(ap);
8126 c2301be8 2018-04-30 stsp return argv;
8127 ee85c5e8 2020-02-29 stsp }
8128 ee85c5e8 2020-02-29 stsp
8129 ee85c5e8 2020-02-29 stsp /*
8130 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8131 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8132 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8133 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8134 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8135 ee85c5e8 2020-02-29 stsp */
8136 ee85c5e8 2020-02-29 stsp static const struct got_error *
8137 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8138 ee85c5e8 2020-02-29 stsp {
8139 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8140 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8141 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8142 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8143 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8144 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8145 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8146 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8147 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8148 84de9106 2020-12-26 stsp
8149 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8150 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8151 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8152 ee85c5e8 2020-02-29 stsp
8153 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8154 0ae84acc 2022-06-15 tracey if (error != NULL)
8155 0ae84acc 2022-06-15 tracey goto done;
8156 0ae84acc 2022-06-15 tracey
8157 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8158 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8159 ee85c5e8 2020-02-29 stsp goto done;
8160 ee85c5e8 2020-02-29 stsp
8161 ee85c5e8 2020-02-29 stsp if (worktree)
8162 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8163 ee85c5e8 2020-02-29 stsp else
8164 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8165 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8166 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8167 ee85c5e8 2020-02-29 stsp goto done;
8168 ee85c5e8 2020-02-29 stsp }
8169 ee85c5e8 2020-02-29 stsp
8170 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8171 ee85c5e8 2020-02-29 stsp if (error != NULL)
8172 ee85c5e8 2020-02-29 stsp goto done;
8173 ee85c5e8 2020-02-29 stsp
8174 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8175 ee85c5e8 2020-02-29 stsp repo, worktree);
8176 ee85c5e8 2020-02-29 stsp if (error)
8177 ee85c5e8 2020-02-29 stsp goto done;
8178 ee85c5e8 2020-02-29 stsp
8179 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8180 84de9106 2020-12-26 stsp if (error)
8181 84de9106 2020-12-26 stsp goto done;
8182 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8183 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8184 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8185 ee85c5e8 2020-02-29 stsp if (error)
8186 ee85c5e8 2020-02-29 stsp goto done;
8187 ee85c5e8 2020-02-29 stsp
8188 ee85c5e8 2020-02-29 stsp if (worktree) {
8189 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8190 ee85c5e8 2020-02-29 stsp worktree = NULL;
8191 ee85c5e8 2020-02-29 stsp }
8192 ee85c5e8 2020-02-29 stsp
8193 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8194 a44927cc 2022-04-07 stsp if (error)
8195 a44927cc 2022-04-07 stsp goto done;
8196 a44927cc 2022-04-07 stsp
8197 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8198 ee85c5e8 2020-02-29 stsp if (error) {
8199 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8200 ee85c5e8 2020-02-29 stsp goto done;
8201 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8202 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8203 6879ba42 2020-10-01 naddy usage(1, 1);
8204 ee85c5e8 2020-02-29 stsp /* not reached */
8205 ee85c5e8 2020-02-29 stsp }
8206 ee85c5e8 2020-02-29 stsp
8207 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8208 1d0f4054 2021-06-17 stsp if (error == NULL)
8209 1d0f4054 2021-06-17 stsp error = close_err;
8210 ee85c5e8 2020-02-29 stsp repo = NULL;
8211 ee85c5e8 2020-02-29 stsp
8212 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8213 ee85c5e8 2020-02-29 stsp if (error)
8214 ee85c5e8 2020-02-29 stsp goto done;
8215 ee85c5e8 2020-02-29 stsp
8216 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8217 ee85c5e8 2020-02-29 stsp argc = 4;
8218 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8219 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8220 ee85c5e8 2020-02-29 stsp done:
8221 1d0f4054 2021-06-17 stsp if (repo) {
8222 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8223 1d0f4054 2021-06-17 stsp if (error == NULL)
8224 1d0f4054 2021-06-17 stsp error = close_err;
8225 1d0f4054 2021-06-17 stsp }
8226 a44927cc 2022-04-07 stsp if (commit)
8227 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8228 ee85c5e8 2020-02-29 stsp if (worktree)
8229 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8230 0ae84acc 2022-06-15 tracey if (pack_fds) {
8231 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8232 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8233 0ae84acc 2022-06-15 tracey if (error == NULL)
8234 0ae84acc 2022-06-15 tracey error = pack_err;
8235 0ae84acc 2022-06-15 tracey }
8236 ee85c5e8 2020-02-29 stsp free(id);
8237 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8238 ee85c5e8 2020-02-29 stsp free(commit_id);
8239 ee85c5e8 2020-02-29 stsp free(cwd);
8240 ee85c5e8 2020-02-29 stsp free(repo_path);
8241 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8242 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8243 ee85c5e8 2020-02-29 stsp int i;
8244 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8245 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8246 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8247 ee85c5e8 2020-02-29 stsp }
8248 87670572 2020-12-26 naddy tog_free_refs();
8249 ee85c5e8 2020-02-29 stsp return error;
8250 c2301be8 2018-04-30 stsp }
8251 c2301be8 2018-04-30 stsp
8252 9f7d7167 2018-04-29 stsp int
8253 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8254 9f7d7167 2018-04-29 stsp {
8255 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8256 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8257 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8258 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8259 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8260 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8261 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8262 83cd27f8 2020-01-13 stsp };
8263 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8264 9f7d7167 2018-04-29 stsp
8265 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8266 9f7d7167 2018-04-29 stsp
8267 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8268 9f7d7167 2018-04-29 stsp switch (ch) {
8269 9f7d7167 2018-04-29 stsp case 'h':
8270 9f7d7167 2018-04-29 stsp hflag = 1;
8271 9f7d7167 2018-04-29 stsp break;
8272 53ccebc2 2019-07-30 stsp case 'V':
8273 53ccebc2 2019-07-30 stsp Vflag = 1;
8274 53ccebc2 2019-07-30 stsp break;
8275 9f7d7167 2018-04-29 stsp default:
8276 6879ba42 2020-10-01 naddy usage(hflag, 1);
8277 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8278 9f7d7167 2018-04-29 stsp }
8279 9f7d7167 2018-04-29 stsp }
8280 9f7d7167 2018-04-29 stsp
8281 9f7d7167 2018-04-29 stsp argc -= optind;
8282 9f7d7167 2018-04-29 stsp argv += optind;
8283 9814e6a3 2020-09-27 naddy optind = 1;
8284 c2301be8 2018-04-30 stsp optreset = 1;
8285 9f7d7167 2018-04-29 stsp
8286 53ccebc2 2019-07-30 stsp if (Vflag) {
8287 53ccebc2 2019-07-30 stsp got_version_print_str();
8288 6879ba42 2020-10-01 naddy return 0;
8289 53ccebc2 2019-07-30 stsp }
8290 4010e238 2020-12-04 stsp
8291 4010e238 2020-12-04 stsp #ifndef PROFILE
8292 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8293 4010e238 2020-12-04 stsp NULL) == -1)
8294 4010e238 2020-12-04 stsp err(1, "pledge");
8295 4010e238 2020-12-04 stsp #endif
8296 53ccebc2 2019-07-30 stsp
8297 c2301be8 2018-04-30 stsp if (argc == 0) {
8298 f29d3e89 2018-06-23 stsp if (hflag)
8299 6879ba42 2020-10-01 naddy usage(hflag, 0);
8300 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8301 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8302 c2301be8 2018-04-30 stsp argc = 1;
8303 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8304 c2301be8 2018-04-30 stsp } else {
8305 6059809a 2020-12-17 stsp size_t i;
8306 9f7d7167 2018-04-29 stsp
8307 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8308 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8309 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8310 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8311 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8312 9f7d7167 2018-04-29 stsp break;
8313 9f7d7167 2018-04-29 stsp }
8314 9f7d7167 2018-04-29 stsp }
8315 ee85c5e8 2020-02-29 stsp }
8316 3642c4c6 2019-07-09 stsp
8317 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8318 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8319 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8320 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8321 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8322 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8323 917d79a7 2022-07-01 stsp }
8324 917d79a7 2022-07-01 stsp
8325 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8326 ee85c5e8 2020-02-29 stsp if (argc != 1)
8327 6879ba42 2020-10-01 naddy usage(0, 1);
8328 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8329 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8330 ee85c5e8 2020-02-29 stsp } else {
8331 ee85c5e8 2020-02-29 stsp if (hflag)
8332 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8333 ee85c5e8 2020-02-29 stsp else
8334 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8335 9f7d7167 2018-04-29 stsp }
8336 9f7d7167 2018-04-29 stsp
8337 9f7d7167 2018-04-29 stsp endwin();
8338 b46c1e04 2020-09-20 naddy putchar('\n');
8339 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8340 a2f4a359 2020-02-28 stsp int i;
8341 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8342 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8343 a2f4a359 2020-02-28 stsp free(cmd_argv);
8344 a2f4a359 2020-02-28 stsp }
8345 a2f4a359 2020-02-28 stsp
8346 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8347 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8348 9f7d7167 2018-04-29 stsp return 0;
8349 9f7d7167 2018-04-29 stsp }