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 a310d9c3 2022-07-21 florian if (err)
2309 a310d9c3 2022-07-21 florian goto done;
2310 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2311 145b6838 2022-06-16 stsp if (err)
2312 145b6838 2022-06-16 stsp goto done;
2313 145b6838 2022-06-16 stsp msg = msg0;
2314 145b6838 2022-06-16 stsp while (*msg == '\n')
2315 145b6838 2022-06-16 stsp ++msg;
2316 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2317 29688b02 2022-06-16 stsp *eol = '\0';
2318 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2319 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2320 29688b02 2022-06-16 stsp if (err)
2321 29688b02 2022-06-16 stsp goto done;
2322 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2323 145b6838 2022-06-16 stsp free(msg0);
2324 29688b02 2022-06-16 stsp free(wmsg);
2325 7ca04879 2019-10-19 stsp ncommits++;
2326 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2327 5813d178 2019-03-09 stsp }
2328 5813d178 2019-03-09 stsp
2329 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2330 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2331 867c6645 2018-07-10 stsp ncommits = 0;
2332 899d86c2 2018-05-10 stsp while (entry) {
2333 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2334 899d86c2 2018-05-10 stsp break;
2335 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2336 2814baeb 2018-08-01 stsp wstandout(view->window);
2337 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2338 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2339 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2340 2814baeb 2018-08-01 stsp wstandend(view->window);
2341 0553a4e3 2018-04-30 stsp if (err)
2342 60493ae3 2019-06-20 stsp goto done;
2343 0553a4e3 2018-04-30 stsp ncommits++;
2344 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2345 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2346 80ddbec8 2018-04-29 stsp }
2347 80ddbec8 2018-04-29 stsp
2348 9b058f45 2022-06-30 mark view_border(view);
2349 1a76625f 2018-10-22 stsp done:
2350 1a76625f 2018-10-22 stsp free(id_str);
2351 8b473291 2019-02-21 stsp free(refs_str);
2352 1a76625f 2018-10-22 stsp free(ncommits_str);
2353 1a76625f 2018-10-22 stsp free(header);
2354 80ddbec8 2018-04-29 stsp return err;
2355 9f7d7167 2018-04-29 stsp }
2356 07b55e75 2018-05-10 stsp
2357 07b55e75 2018-05-10 stsp static void
2358 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2359 07b55e75 2018-05-10 stsp {
2360 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2361 07b55e75 2018-05-10 stsp int nscrolled = 0;
2362 07b55e75 2018-05-10 stsp
2363 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2364 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2365 07b55e75 2018-05-10 stsp return;
2366 9f7d7167 2018-04-29 stsp
2367 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2368 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2369 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2370 07b55e75 2018-05-10 stsp if (entry) {
2371 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2372 07b55e75 2018-05-10 stsp nscrolled++;
2373 07b55e75 2018-05-10 stsp }
2374 07b55e75 2018-05-10 stsp }
2375 aa075928 2018-05-10 stsp }
2376 aa075928 2018-05-10 stsp
2377 aa075928 2018-05-10 stsp static const struct got_error *
2378 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2379 aa075928 2018-05-10 stsp {
2380 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2381 5e224a3e 2019-02-22 stsp int errcode;
2382 8a42fca8 2019-02-22 stsp
2383 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2384 aa075928 2018-05-10 stsp
2385 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2386 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2387 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2388 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2389 7aafa0d1 2019-02-22 stsp if (errcode)
2390 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2391 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2392 7c1452c1 2020-03-26 stsp
2393 7c1452c1 2020-03-26 stsp /*
2394 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2395 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2396 7c1452c1 2020-03-26 stsp */
2397 7c1452c1 2020-03-26 stsp if (!wait)
2398 7c1452c1 2020-03-26 stsp break;
2399 7c1452c1 2020-03-26 stsp
2400 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2401 ffe38506 2020-12-01 naddy show_log_view(view);
2402 7c1452c1 2020-03-26 stsp update_panels();
2403 7c1452c1 2020-03-26 stsp doupdate();
2404 7c1452c1 2020-03-26 stsp
2405 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2406 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2407 82954512 2020-02-03 stsp if (errcode)
2408 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2409 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2410 82954512 2020-02-03 stsp
2411 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2412 ffe38506 2020-12-01 naddy show_log_view(view);
2413 7c1452c1 2020-03-26 stsp update_panels();
2414 7c1452c1 2020-03-26 stsp doupdate();
2415 5e224a3e 2019-02-22 stsp }
2416 5e224a3e 2019-02-22 stsp
2417 5e224a3e 2019-02-22 stsp return NULL;
2418 5e224a3e 2019-02-22 stsp }
2419 5e224a3e 2019-02-22 stsp
2420 5e224a3e 2019-02-22 stsp static const struct got_error *
2421 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2422 9b058f45 2022-06-30 mark {
2423 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2424 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2425 2525dccb 2022-07-13 mark
2426 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2427 2525dccb 2022-07-13 mark return NULL;
2428 9b058f45 2022-06-30 mark
2429 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2430 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2431 9b058f45 2022-06-30 mark view->nscrolled = 0;
2432 9b058f45 2022-06-30 mark
2433 9b058f45 2022-06-30 mark return err;
2434 9b058f45 2022-06-30 mark }
2435 9b058f45 2022-06-30 mark
2436 9b058f45 2022-06-30 mark static const struct got_error *
2437 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2438 5e224a3e 2019-02-22 stsp {
2439 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2440 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2441 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2442 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2443 5e224a3e 2019-02-22 stsp
2444 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2445 5e224a3e 2019-02-22 stsp return NULL;
2446 5e224a3e 2019-02-22 stsp
2447 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2448 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2449 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2450 08ebd0a9 2019-02-22 stsp /*
2451 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2452 08ebd0a9 2019-02-22 stsp */
2453 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2454 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2455 5e224a3e 2019-02-22 stsp if (err)
2456 5e224a3e 2019-02-22 stsp return err;
2457 7aafa0d1 2019-02-22 stsp }
2458 b295e71b 2019-02-22 stsp
2459 7aafa0d1 2019-02-22 stsp do {
2460 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2461 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2462 88048b54 2019-02-21 stsp break;
2463 88048b54 2019-02-21 stsp
2464 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2465 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2466 aa075928 2018-05-10 stsp
2467 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2468 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2469 dd0a52c1 2018-05-20 stsp break;
2470 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2471 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2472 aa075928 2018-05-10 stsp
2473 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2474 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2475 9b058f45 2022-06-30 mark else
2476 9b058f45 2022-06-30 mark view->nscrolled = 0;
2477 9b058f45 2022-06-30 mark
2478 dd0a52c1 2018-05-20 stsp return err;
2479 07b55e75 2018-05-10 stsp }
2480 4a7f7875 2018-05-10 stsp
2481 cd0acaa7 2018-05-20 stsp static const struct got_error *
2482 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2483 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2484 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2485 cd0acaa7 2018-05-20 stsp {
2486 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2487 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2488 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2489 cd0acaa7 2018-05-20 stsp
2490 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2491 15a94983 2018-12-23 stsp if (diff_view == NULL)
2492 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2493 ea5e7bb5 2018-08-01 stsp
2494 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2495 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2496 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2497 e5a0f69f 2018-08-18 stsp if (err == NULL)
2498 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2499 cd0acaa7 2018-05-20 stsp return err;
2500 4a7f7875 2018-05-10 stsp }
2501 4a7f7875 2018-05-10 stsp
2502 80ddbec8 2018-04-29 stsp static const struct got_error *
2503 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2504 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2505 9343a5fb 2018-06-23 stsp {
2506 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2507 941e9f74 2019-05-21 stsp
2508 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2509 941e9f74 2019-05-21 stsp if (parent == NULL)
2510 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2511 941e9f74 2019-05-21 stsp
2512 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2513 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2514 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2515 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2516 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2517 941e9f74 2019-05-21 stsp s->tree = subtree;
2518 941e9f74 2019-05-21 stsp s->selected = 0;
2519 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2520 941e9f74 2019-05-21 stsp return NULL;
2521 941e9f74 2019-05-21 stsp }
2522 941e9f74 2019-05-21 stsp
2523 941e9f74 2019-05-21 stsp static const struct got_error *
2524 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2525 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2526 941e9f74 2019-05-21 stsp {
2527 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2528 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2529 941e9f74 2019-05-21 stsp const char *p;
2530 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2531 9343a5fb 2018-06-23 stsp
2532 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2533 941e9f74 2019-05-21 stsp p = path;
2534 941e9f74 2019-05-21 stsp while (*p) {
2535 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2536 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2537 56e0773d 2019-11-28 stsp char *te_name;
2538 33cbf02b 2020-01-12 stsp
2539 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2540 33cbf02b 2020-01-12 stsp p++;
2541 941e9f74 2019-05-21 stsp
2542 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2543 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2544 941e9f74 2019-05-21 stsp if (slash == NULL)
2545 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2546 33cbf02b 2020-01-12 stsp else
2547 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2548 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2549 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2550 56e0773d 2019-11-28 stsp break;
2551 941e9f74 2019-05-21 stsp }
2552 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2553 56e0773d 2019-11-28 stsp if (te == NULL) {
2554 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2555 56e0773d 2019-11-28 stsp free(te_name);
2556 941e9f74 2019-05-21 stsp break;
2557 941e9f74 2019-05-21 stsp }
2558 56e0773d 2019-11-28 stsp free(te_name);
2559 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2560 941e9f74 2019-05-21 stsp
2561 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2562 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2563 b03c880f 2019-05-21 stsp
2564 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2565 941e9f74 2019-05-21 stsp if (slash)
2566 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2567 941e9f74 2019-05-21 stsp else
2568 941e9f74 2019-05-21 stsp subpath = strdup(path);
2569 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2570 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2571 941e9f74 2019-05-21 stsp break;
2572 941e9f74 2019-05-21 stsp }
2573 941e9f74 2019-05-21 stsp
2574 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2575 941e9f74 2019-05-21 stsp subpath);
2576 941e9f74 2019-05-21 stsp if (err)
2577 941e9f74 2019-05-21 stsp break;
2578 941e9f74 2019-05-21 stsp
2579 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2580 941e9f74 2019-05-21 stsp free(tree_id);
2581 941e9f74 2019-05-21 stsp if (err)
2582 941e9f74 2019-05-21 stsp break;
2583 941e9f74 2019-05-21 stsp
2584 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2585 941e9f74 2019-05-21 stsp if (err) {
2586 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2587 941e9f74 2019-05-21 stsp break;
2588 941e9f74 2019-05-21 stsp }
2589 941e9f74 2019-05-21 stsp if (slash == NULL)
2590 941e9f74 2019-05-21 stsp break;
2591 941e9f74 2019-05-21 stsp free(subpath);
2592 941e9f74 2019-05-21 stsp subpath = NULL;
2593 941e9f74 2019-05-21 stsp p = slash;
2594 941e9f74 2019-05-21 stsp }
2595 941e9f74 2019-05-21 stsp
2596 941e9f74 2019-05-21 stsp free(subpath);
2597 1a76625f 2018-10-22 stsp return err;
2598 61266923 2020-01-14 stsp }
2599 61266923 2020-01-14 stsp
2600 61266923 2020-01-14 stsp static const struct got_error *
2601 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2602 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2603 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2604 55cccc34 2020-02-20 stsp {
2605 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2606 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2607 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2608 55cccc34 2020-02-20 stsp
2609 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2610 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2611 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2612 55cccc34 2020-02-20 stsp
2613 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2614 bc573f3b 2021-07-10 stsp if (err)
2615 55cccc34 2020-02-20 stsp return err;
2616 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2617 55cccc34 2020-02-20 stsp
2618 55cccc34 2020-02-20 stsp *new_view = tree_view;
2619 55cccc34 2020-02-20 stsp
2620 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2621 55cccc34 2020-02-20 stsp return NULL;
2622 55cccc34 2020-02-20 stsp
2623 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2624 55cccc34 2020-02-20 stsp }
2625 55cccc34 2020-02-20 stsp
2626 55cccc34 2020-02-20 stsp static const struct got_error *
2627 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2628 61266923 2020-01-14 stsp {
2629 61266923 2020-01-14 stsp sigset_t sigset;
2630 61266923 2020-01-14 stsp int errcode;
2631 61266923 2020-01-14 stsp
2632 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2633 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2634 61266923 2020-01-14 stsp
2635 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2636 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2637 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2638 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2639 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2640 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2641 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2642 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2643 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2644 61266923 2020-01-14 stsp
2645 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2646 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2647 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2648 61266923 2020-01-14 stsp
2649 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2650 61266923 2020-01-14 stsp if (errcode)
2651 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2652 61266923 2020-01-14 stsp
2653 61266923 2020-01-14 stsp return NULL;
2654 1a76625f 2018-10-22 stsp }
2655 1a76625f 2018-10-22 stsp
2656 1a76625f 2018-10-22 stsp static void *
2657 1a76625f 2018-10-22 stsp log_thread(void *arg)
2658 1a76625f 2018-10-22 stsp {
2659 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2660 1a76625f 2018-10-22 stsp int errcode = 0;
2661 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2662 1a76625f 2018-10-22 stsp int done = 0;
2663 61266923 2020-01-14 stsp
2664 5629093a 2022-07-11 stsp /*
2665 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2666 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2667 5629093a 2022-07-11 stsp */
2668 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2669 5629093a 2022-07-11 stsp if (errcode) {
2670 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2671 61266923 2020-01-14 stsp return (void *)err;
2672 5629093a 2022-07-11 stsp }
2673 1a76625f 2018-10-22 stsp
2674 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2675 5629093a 2022-07-11 stsp if (err) {
2676 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2677 5629093a 2022-07-11 stsp goto done;
2678 5629093a 2022-07-11 stsp }
2679 5629093a 2022-07-11 stsp
2680 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2681 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2682 5629093a 2022-07-11 stsp if (errcode) {
2683 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2684 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2685 5629093a 2022-07-11 stsp goto done;
2686 5629093a 2022-07-11 stsp }
2687 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2688 1a76625f 2018-10-22 stsp if (err) {
2689 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2690 5629093a 2022-07-11 stsp goto done;
2691 1a76625f 2018-10-22 stsp err = NULL;
2692 1a76625f 2018-10-22 stsp done = 1;
2693 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2694 1a76625f 2018-10-22 stsp a->commits_needed--;
2695 1a76625f 2018-10-22 stsp
2696 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2697 3abe8080 2019-04-10 stsp if (errcode) {
2698 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2699 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2700 5629093a 2022-07-11 stsp goto done;
2701 3abe8080 2019-04-10 stsp } else if (*a->quit)
2702 1a76625f 2018-10-22 stsp done = 1;
2703 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2704 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2705 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2706 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2707 1a76625f 2018-10-22 stsp }
2708 1a76625f 2018-10-22 stsp
2709 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2710 7c1452c1 2020-03-26 stsp if (errcode) {
2711 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2712 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2713 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2714 5629093a 2022-07-11 stsp goto done;
2715 7c1452c1 2020-03-26 stsp }
2716 7c1452c1 2020-03-26 stsp
2717 1a76625f 2018-10-22 stsp if (done)
2718 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2719 7c1452c1 2020-03-26 stsp else {
2720 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2721 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2722 7c1452c1 2020-03-26 stsp &tog_mutex);
2723 5629093a 2022-07-11 stsp if (errcode) {
2724 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2725 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2726 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2727 5629093a 2022-07-11 stsp goto done;
2728 5629093a 2022-07-11 stsp }
2729 21355643 2020-12-06 stsp if (*a->quit)
2730 21355643 2020-12-06 stsp done = 1;
2731 7c1452c1 2020-03-26 stsp }
2732 1a76625f 2018-10-22 stsp }
2733 1a76625f 2018-10-22 stsp }
2734 3abe8080 2019-04-10 stsp a->log_complete = 1;
2735 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2736 5629093a 2022-07-11 stsp if (errcode)
2737 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2738 5629093a 2022-07-11 stsp done:
2739 5629093a 2022-07-11 stsp if (err) {
2740 5629093a 2022-07-11 stsp tog_thread_error = 1;
2741 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2742 5629093a 2022-07-11 stsp }
2743 1a76625f 2018-10-22 stsp return (void *)err;
2744 1a76625f 2018-10-22 stsp }
2745 1a76625f 2018-10-22 stsp
2746 1a76625f 2018-10-22 stsp static const struct got_error *
2747 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2748 1a76625f 2018-10-22 stsp {
2749 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2750 1a76625f 2018-10-22 stsp int errcode;
2751 1a76625f 2018-10-22 stsp
2752 1a76625f 2018-10-22 stsp if (s->thread) {
2753 1a76625f 2018-10-22 stsp s->quit = 1;
2754 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2755 1a76625f 2018-10-22 stsp if (errcode)
2756 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2757 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2758 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2759 1a76625f 2018-10-22 stsp if (errcode)
2760 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2761 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2762 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2763 1a76625f 2018-10-22 stsp if (errcode)
2764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2765 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2766 1a76625f 2018-10-22 stsp if (errcode)
2767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2768 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2769 1a76625f 2018-10-22 stsp s->thread = NULL;
2770 1a76625f 2018-10-22 stsp }
2771 1a76625f 2018-10-22 stsp
2772 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2773 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2774 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2775 74467cc8 2022-06-15 stsp }
2776 74467cc8 2022-06-15 stsp
2777 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2778 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2779 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2780 74467cc8 2022-06-15 stsp if (err == NULL)
2781 74467cc8 2022-06-15 stsp err = pack_err;
2782 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2783 1a76625f 2018-10-22 stsp }
2784 1a76625f 2018-10-22 stsp
2785 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2786 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2787 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2788 1a76625f 2018-10-22 stsp }
2789 1a76625f 2018-10-22 stsp
2790 5629093a 2022-07-11 stsp return err ? err : thread_err;
2791 9343a5fb 2018-06-23 stsp }
2792 9343a5fb 2018-06-23 stsp
2793 9343a5fb 2018-06-23 stsp static const struct got_error *
2794 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2795 1a76625f 2018-10-22 stsp {
2796 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2797 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2798 276b94a1 2020-11-13 naddy int errcode;
2799 1a76625f 2018-10-22 stsp
2800 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2801 276b94a1 2020-11-13 naddy
2802 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2803 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2804 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2805 276b94a1 2020-11-13 naddy
2806 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2807 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2808 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2809 276b94a1 2020-11-13 naddy
2810 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2811 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2812 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2813 1a76625f 2018-10-22 stsp free(s->start_id);
2814 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2815 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2816 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2817 1a76625f 2018-10-22 stsp return err;
2818 1a76625f 2018-10-22 stsp }
2819 1a76625f 2018-10-22 stsp
2820 1a76625f 2018-10-22 stsp static const struct got_error *
2821 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2822 60493ae3 2019-06-20 stsp {
2823 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2824 60493ae3 2019-06-20 stsp
2825 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2826 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2827 60493ae3 2019-06-20 stsp return NULL;
2828 60493ae3 2019-06-20 stsp }
2829 60493ae3 2019-06-20 stsp
2830 60493ae3 2019-06-20 stsp static const struct got_error *
2831 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2832 60493ae3 2019-06-20 stsp {
2833 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2834 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2835 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2836 60493ae3 2019-06-20 stsp
2837 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2838 f9686aa5 2020-03-27 stsp show_log_view(view);
2839 f9686aa5 2020-03-27 stsp update_panels();
2840 f9686aa5 2020-03-27 stsp doupdate();
2841 f9686aa5 2020-03-27 stsp
2842 96e2b566 2019-07-08 stsp if (s->search_entry) {
2843 13add988 2019-10-15 stsp int errcode, ch;
2844 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2845 13add988 2019-10-15 stsp if (errcode)
2846 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2847 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2848 13add988 2019-10-15 stsp ch = wgetch(view->window);
2849 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2850 13add988 2019-10-15 stsp if (errcode)
2851 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2852 13add988 2019-10-15 stsp "pthread_mutex_lock");
2853 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2854 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2855 678cbce5 2019-07-28 stsp return NULL;
2856 678cbce5 2019-07-28 stsp }
2857 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2858 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2859 96e2b566 2019-07-08 stsp else
2860 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2861 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2862 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2863 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2864 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2865 364ac6fd 2022-06-18 stsp
2866 364ac6fd 2022-06-18 stsp /*
2867 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2868 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2869 f704b35c 2022-06-18 stsp * might have changed.
2870 364ac6fd 2022-06-18 stsp */
2871 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2872 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2873 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2874 364ac6fd 2022-06-18 stsp else
2875 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2876 4bfe9f0a 2022-06-18 stsp } else {
2877 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2878 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2879 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2880 364ac6fd 2022-06-18 stsp else
2881 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2882 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2883 4bfe9f0a 2022-06-18 stsp }
2884 20be8d96 2019-06-21 stsp } else {
2885 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2886 20be8d96 2019-06-21 stsp }
2887 60493ae3 2019-06-20 stsp
2888 60493ae3 2019-06-20 stsp while (1) {
2889 13add988 2019-10-15 stsp int have_match = 0;
2890 13add988 2019-10-15 stsp
2891 60493ae3 2019-06-20 stsp if (entry == NULL) {
2892 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2893 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2894 f9967bca 2020-03-27 stsp view->search_next_done =
2895 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2896 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2897 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2898 f801134a 2019-06-25 stsp return NULL;
2899 60493ae3 2019-06-20 stsp }
2900 96e2b566 2019-07-08 stsp /*
2901 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2902 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2903 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2904 96e2b566 2019-07-08 stsp */
2905 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2906 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2907 60493ae3 2019-06-20 stsp }
2908 60493ae3 2019-06-20 stsp
2909 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2910 13add988 2019-10-15 stsp &view->regex);
2911 5943eee2 2019-08-13 stsp if (err)
2912 13add988 2019-10-15 stsp break;
2913 13add988 2019-10-15 stsp if (have_match) {
2914 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2915 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2916 60493ae3 2019-06-20 stsp break;
2917 60493ae3 2019-06-20 stsp }
2918 13add988 2019-10-15 stsp
2919 96e2b566 2019-07-08 stsp s->search_entry = entry;
2920 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2921 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2922 b1bf1435 2019-06-21 stsp else
2923 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2924 60493ae3 2019-06-20 stsp }
2925 60493ae3 2019-06-20 stsp
2926 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2927 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2928 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2929 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2930 60493ae3 2019-06-20 stsp if (err)
2931 60493ae3 2019-06-20 stsp return err;
2932 ead14cbe 2019-06-21 stsp cur++;
2933 ead14cbe 2019-06-21 stsp }
2934 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2935 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2936 60493ae3 2019-06-20 stsp if (err)
2937 60493ae3 2019-06-20 stsp return err;
2938 ead14cbe 2019-06-21 stsp cur--;
2939 60493ae3 2019-06-20 stsp }
2940 60493ae3 2019-06-20 stsp }
2941 60493ae3 2019-06-20 stsp
2942 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2943 96e2b566 2019-07-08 stsp
2944 60493ae3 2019-06-20 stsp return NULL;
2945 60493ae3 2019-06-20 stsp }
2946 60493ae3 2019-06-20 stsp
2947 60493ae3 2019-06-20 stsp static const struct got_error *
2948 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2949 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2950 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2951 80ddbec8 2018-04-29 stsp {
2952 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2953 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2954 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2955 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2956 1a76625f 2018-10-22 stsp int errcode;
2957 80ddbec8 2018-04-29 stsp
2958 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2959 f135c941 2020-02-20 stsp free(s->in_repo_path);
2960 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2961 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2962 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2963 f135c941 2020-02-20 stsp }
2964 ecb28ae0 2018-07-16 stsp
2965 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2966 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2967 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2968 78756c87 2020-11-24 stsp
2969 fb2756b9 2018-08-04 stsp s->repo = repo;
2970 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2971 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2972 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2973 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2974 9cd7cbd1 2020-12-07 stsp goto done;
2975 9cd7cbd1 2020-12-07 stsp }
2976 9cd7cbd1 2020-12-07 stsp }
2977 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2978 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2979 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2980 5036bf37 2018-09-24 stsp goto done;
2981 5036bf37 2018-09-24 stsp }
2982 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2983 e5a0f69f 2018-08-18 stsp
2984 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2985 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2986 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2987 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2988 11b20872 2019-11-08 stsp if (err)
2989 11b20872 2019-11-08 stsp goto done;
2990 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2991 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2992 11b20872 2019-11-08 stsp if (err) {
2993 11b20872 2019-11-08 stsp free_colors(&s->colors);
2994 11b20872 2019-11-08 stsp goto done;
2995 11b20872 2019-11-08 stsp }
2996 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2997 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2998 11b20872 2019-11-08 stsp if (err) {
2999 11b20872 2019-11-08 stsp free_colors(&s->colors);
3000 11b20872 2019-11-08 stsp goto done;
3001 11b20872 2019-11-08 stsp }
3002 11b20872 2019-11-08 stsp }
3003 11b20872 2019-11-08 stsp
3004 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3005 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3006 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3007 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3008 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3009 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3010 1a76625f 2018-10-22 stsp
3011 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3012 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3013 74467cc8 2022-06-15 stsp if (err)
3014 74467cc8 2022-06-15 stsp goto done;
3015 74467cc8 2022-06-15 stsp }
3016 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3017 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3018 0ae84acc 2022-06-15 tracey if (err)
3019 0ae84acc 2022-06-15 tracey goto done;
3020 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3021 b672a97a 2020-01-27 stsp !s->log_branches);
3022 1a76625f 2018-10-22 stsp if (err)
3023 1a76625f 2018-10-22 stsp goto done;
3024 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3025 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3026 c5b78334 2020-01-12 stsp if (err)
3027 c5b78334 2020-01-12 stsp goto done;
3028 1a76625f 2018-10-22 stsp
3029 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3030 1a76625f 2018-10-22 stsp if (errcode) {
3031 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3032 1a76625f 2018-10-22 stsp goto done;
3033 1a76625f 2018-10-22 stsp }
3034 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3035 7c1452c1 2020-03-26 stsp if (errcode) {
3036 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3037 7c1452c1 2020-03-26 stsp goto done;
3038 7c1452c1 2020-03-26 stsp }
3039 1a76625f 2018-10-22 stsp
3040 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3041 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3042 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3043 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3044 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3045 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3046 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3047 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3048 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3049 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3050 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3051 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3052 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3053 ba4f502b 2018-08-04 stsp done:
3054 1a76625f 2018-10-22 stsp if (err)
3055 1a76625f 2018-10-22 stsp close_log_view(view);
3056 ba4f502b 2018-08-04 stsp return err;
3057 ba4f502b 2018-08-04 stsp }
3058 ba4f502b 2018-08-04 stsp
3059 e5a0f69f 2018-08-18 stsp static const struct got_error *
3060 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3061 ba4f502b 2018-08-04 stsp {
3062 f2f6d207 2020-11-24 stsp const struct got_error *err;
3063 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3064 ba4f502b 2018-08-04 stsp
3065 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3066 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3067 2b380cc8 2018-10-24 stsp &s->thread_args);
3068 2b380cc8 2018-10-24 stsp if (errcode)
3069 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3070 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3071 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3072 f2f6d207 2020-11-24 stsp if (err)
3073 f2f6d207 2020-11-24 stsp return err;
3074 f2f6d207 2020-11-24 stsp }
3075 2b380cc8 2018-10-24 stsp }
3076 2b380cc8 2018-10-24 stsp
3077 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3078 b880cc75 2022-06-30 mark }
3079 b880cc75 2022-06-30 mark
3080 b880cc75 2022-06-30 mark static void
3081 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3082 b880cc75 2022-06-30 mark {
3083 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3084 b880cc75 2022-06-30 mark
3085 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3086 b880cc75 2022-06-30 mark view->count = 0;
3087 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3088 b880cc75 2022-06-30 mark return;
3089 b880cc75 2022-06-30 mark
3090 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3091 b880cc75 2022-06-30 mark || home)
3092 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3093 b880cc75 2022-06-30 mark
3094 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3095 b880cc75 2022-06-30 mark --s->selected;
3096 b880cc75 2022-06-30 mark else
3097 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3098 b880cc75 2022-06-30 mark
3099 b880cc75 2022-06-30 mark select_commit(s);
3100 b880cc75 2022-06-30 mark return;
3101 b880cc75 2022-06-30 mark }
3102 b880cc75 2022-06-30 mark
3103 b880cc75 2022-06-30 mark static const struct got_error *
3104 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3105 b880cc75 2022-06-30 mark {
3106 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3107 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3108 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3109 b880cc75 2022-06-30 mark
3110 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3111 b880cc75 2022-06-30 mark if (first == NULL) {
3112 b880cc75 2022-06-30 mark view->count = 0;
3113 b880cc75 2022-06-30 mark return NULL;
3114 b880cc75 2022-06-30 mark }
3115 b880cc75 2022-06-30 mark
3116 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3117 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3118 b880cc75 2022-06-30 mark return NULL;
3119 b880cc75 2022-06-30 mark
3120 b880cc75 2022-06-30 mark if (!page) {
3121 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3122 b880cc75 2022-06-30 mark
3123 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3124 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3125 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3126 b880cc75 2022-06-30 mark ++s->selected;
3127 b880cc75 2022-06-30 mark else
3128 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3129 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3130 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3131 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3132 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3133 b880cc75 2022-06-30 mark else
3134 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3135 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3136 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3137 b880cc75 2022-06-30 mark } else {
3138 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3139 b880cc75 2022-06-30 mark if (err)
3140 b880cc75 2022-06-30 mark return err;
3141 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3142 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3143 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3144 b880cc75 2022-06-30 mark }
3145 b880cc75 2022-06-30 mark }
3146 b880cc75 2022-06-30 mark if (err)
3147 b880cc75 2022-06-30 mark return err;
3148 b880cc75 2022-06-30 mark
3149 9b058f45 2022-06-30 mark /*
3150 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3151 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3152 9b058f45 2022-06-30 mark */
3153 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3154 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3155 9b058f45 2022-06-30 mark
3156 b880cc75 2022-06-30 mark select_commit(s);
3157 b880cc75 2022-06-30 mark
3158 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3159 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3160 b880cc75 2022-06-30 mark view->count = 0;
3161 b880cc75 2022-06-30 mark
3162 b880cc75 2022-06-30 mark return NULL;
3163 e5a0f69f 2018-08-18 stsp }
3164 04cc582a 2018-08-01 stsp
3165 9b058f45 2022-06-30 mark static void
3166 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3167 9b058f45 2022-06-30 mark {
3168 76364b2d 2022-07-02 mark *x = 0;
3169 76364b2d 2022-07-02 mark *y = 0;
3170 76364b2d 2022-07-02 mark
3171 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3172 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3173 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3174 7532ccda 2022-07-11 mark else if (view->resized_y)
3175 7532ccda 2022-07-11 mark *y = view->resized_y;
3176 3c1dfe12 2022-07-08 mark else
3177 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3178 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3179 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3180 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3181 7532ccda 2022-07-11 mark else if (view->resized_x)
3182 7532ccda 2022-07-11 mark *x = view->resized_x;
3183 3c1dfe12 2022-07-08 mark else
3184 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3185 3c1dfe12 2022-07-08 mark }
3186 9b058f45 2022-06-30 mark }
3187 9b058f45 2022-06-30 mark
3188 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3189 e5a0f69f 2018-08-18 stsp static const struct got_error *
3190 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3191 9b058f45 2022-06-30 mark {
3192 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3193 9b058f45 2022-06-30 mark
3194 9b058f45 2022-06-30 mark view->nlines = y;
3195 d2366e29 2022-07-07 mark view->ncols = COLS;
3196 9b058f45 2022-06-30 mark err = view_resize(view);
3197 9b058f45 2022-06-30 mark if (err)
3198 9b058f45 2022-06-30 mark return err;
3199 9b058f45 2022-06-30 mark
3200 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3201 9b058f45 2022-06-30 mark
3202 9b058f45 2022-06-30 mark return err;
3203 9b058f45 2022-06-30 mark }
3204 9b058f45 2022-06-30 mark
3205 9b058f45 2022-06-30 mark static const struct got_error *
3206 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3207 e5a0f69f 2018-08-18 stsp {
3208 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3209 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3210 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3211 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3212 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3213 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
3214 80ddbec8 2018-04-29 stsp
3215 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3216 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3217 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3218 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3219 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3220 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3221 fb280deb 2021-08-30 stsp }
3222 b880cc75 2022-06-30 mark return err;
3223 528dedf3 2021-08-30 stsp }
3224 0dca135e 2022-06-30 mark
3225 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3226 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3227 0dca135e 2022-06-30 mark --eos; /* border */
3228 0dca135e 2022-06-30 mark
3229 528dedf3 2021-08-30 stsp switch (ch) {
3230 1e37a5c2 2019-05-12 jcs case 'q':
3231 1e37a5c2 2019-05-12 jcs s->quit = 1;
3232 145b6838 2022-06-16 stsp break;
3233 145b6838 2022-06-16 stsp case '0':
3234 145b6838 2022-06-16 stsp view->x = 0;
3235 145b6838 2022-06-16 stsp break;
3236 145b6838 2022-06-16 stsp case '$':
3237 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3238 640cd7ff 2022-06-22 mark view->count = 0;
3239 145b6838 2022-06-16 stsp break;
3240 145b6838 2022-06-16 stsp case KEY_RIGHT:
3241 145b6838 2022-06-16 stsp case 'l':
3242 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3243 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3244 640cd7ff 2022-06-22 mark else
3245 640cd7ff 2022-06-22 mark view->count = 0;
3246 1e37a5c2 2019-05-12 jcs break;
3247 145b6838 2022-06-16 stsp case KEY_LEFT:
3248 145b6838 2022-06-16 stsp case 'h':
3249 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3250 640cd7ff 2022-06-22 mark if (view->x <= 0)
3251 640cd7ff 2022-06-22 mark view->count = 0;
3252 145b6838 2022-06-16 stsp break;
3253 1e37a5c2 2019-05-12 jcs case 'k':
3254 1e37a5c2 2019-05-12 jcs case KEY_UP:
3255 1e37a5c2 2019-05-12 jcs case '<':
3256 1e37a5c2 2019-05-12 jcs case ',':
3257 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3258 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3259 912a3f79 2021-08-30 j break;
3260 912a3f79 2021-08-30 j case 'g':
3261 912a3f79 2021-08-30 j case KEY_HOME:
3262 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3263 640cd7ff 2022-06-22 mark view->count = 0;
3264 1e37a5c2 2019-05-12 jcs break;
3265 83cc4199 2022-06-13 stsp case CTRL('u'):
3266 33c3719a 2022-06-15 stsp case 'u':
3267 83cc4199 2022-06-13 stsp nscroll /= 2;
3268 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3269 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3270 a4292ac5 2019-05-12 jcs case CTRL('b'):
3271 61417565 2022-06-20 mark case 'b':
3272 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3273 1e37a5c2 2019-05-12 jcs break;
3274 1e37a5c2 2019-05-12 jcs case 'j':
3275 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3276 1e37a5c2 2019-05-12 jcs case '>':
3277 1e37a5c2 2019-05-12 jcs case '.':
3278 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3279 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3280 912a3f79 2021-08-30 j break;
3281 10aab77f 2022-07-19 op case '@':
3282 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3283 10aab77f 2022-07-19 op break;
3284 912a3f79 2021-08-30 j case 'G':
3285 912a3f79 2021-08-30 j case KEY_END: {
3286 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3287 912a3f79 2021-08-30 j * traverse them all. */
3288 640cd7ff 2022-06-22 mark view->count = 0;
3289 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3290 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3291 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3292 80ddbec8 2018-04-29 stsp }
3293 912a3f79 2021-08-30 j
3294 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3295 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3296 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3297 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3298 f3bc9f1d 2021-09-05 naddy break;
3299 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3300 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3301 f3bc9f1d 2021-09-05 naddy }
3302 f3bc9f1d 2021-09-05 naddy if (n > 0)
3303 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3304 2b779855 2020-12-05 naddy select_commit(s);
3305 1e37a5c2 2019-05-12 jcs break;
3306 912a3f79 2021-08-30 j }
3307 80b7e8da 2022-06-11 stsp case CTRL('d'):
3308 33c3719a 2022-06-15 stsp case 'd':
3309 83cc4199 2022-06-13 stsp nscroll /= 2;
3310 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3311 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3312 61417565 2022-06-20 mark case CTRL('f'):
3313 48bb96f0 2022-06-20 naddy case 'f':
3314 b880cc75 2022-06-30 mark case ' ':
3315 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3316 1e37a5c2 2019-05-12 jcs break;
3317 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3318 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3319 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3320 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3321 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3322 2b779855 2020-12-05 naddy select_commit(s);
3323 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3324 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3325 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3326 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3327 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3328 0bf7f153 2020-12-02 naddy }
3329 1e37a5c2 2019-05-12 jcs break;
3330 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3331 49b24ee5 2022-07-03 mark case '\r':
3332 640cd7ff 2022-06-22 mark view->count = 0;
3333 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3334 e5a0f69f 2018-08-18 stsp break;
3335 9b058f45 2022-06-30 mark
3336 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3337 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3338 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3339 9b058f45 2022-06-30 mark
3340 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3341 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3342 78756c87 2020-11-24 stsp view, s->repo);
3343 1e37a5c2 2019-05-12 jcs if (err)
3344 1e37a5c2 2019-05-12 jcs break;
3345 9b058f45 2022-06-30 mark
3346 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3347 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3348 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3349 9b058f45 2022-06-30 mark if (err)
3350 9b058f45 2022-06-30 mark break;
3351 9b058f45 2022-06-30 mark }
3352 9b058f45 2022-06-30 mark
3353 e78dc838 2020-12-04 stsp view->focussed = 0;
3354 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3355 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3356 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3357 9b058f45 2022-06-30 mark
3358 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3359 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
3360 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3361 f7013a22 2018-10-24 stsp if (err)
3362 1e37a5c2 2019-05-12 jcs return err;
3363 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3364 0dbbbe90 2022-06-17 op if (err)
3365 0dbbbe90 2022-06-17 op return err;
3366 e78dc838 2020-12-04 stsp view->focus_child = 1;
3367 1e37a5c2 2019-05-12 jcs } else
3368 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3369 1e37a5c2 2019-05-12 jcs break;
3370 1e37a5c2 2019-05-12 jcs case 't':
3371 640cd7ff 2022-06-22 mark view->count = 0;
3372 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3373 5036bf37 2018-09-24 stsp break;
3374 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3375 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3376 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3377 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3378 4e97c21c 2020-12-06 stsp s->repo);
3379 1e37a5c2 2019-05-12 jcs if (err)
3380 e5a0f69f 2018-08-18 stsp break;
3381 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3382 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3383 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3384 49b24ee5 2022-07-03 mark if (err)
3385 49b24ee5 2022-07-03 mark break;
3386 49b24ee5 2022-07-03 mark }
3387 e78dc838 2020-12-04 stsp view->focussed = 0;
3388 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3389 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3390 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3391 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3392 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
3393 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3394 1e37a5c2 2019-05-12 jcs if (err)
3395 1e37a5c2 2019-05-12 jcs return err;
3396 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3397 0dbbbe90 2022-06-17 op if (err)
3398 0dbbbe90 2022-06-17 op return err;
3399 e78dc838 2020-12-04 stsp view->focus_child = 1;
3400 1e37a5c2 2019-05-12 jcs } else
3401 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3402 1e37a5c2 2019-05-12 jcs break;
3403 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3404 21355643 2020-12-06 stsp case CTRL('l'):
3405 21355643 2020-12-06 stsp case 'B':
3406 640cd7ff 2022-06-22 mark view->count = 0;
3407 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3408 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3409 1e37a5c2 2019-05-12 jcs break;
3410 21355643 2020-12-06 stsp err = stop_log_thread(s);
3411 74cfe85e 2020-10-20 stsp if (err)
3412 74cfe85e 2020-10-20 stsp return err;
3413 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3414 21355643 2020-12-06 stsp char *parent_path;
3415 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3416 21355643 2020-12-06 stsp if (err)
3417 21355643 2020-12-06 stsp return err;
3418 21355643 2020-12-06 stsp free(s->in_repo_path);
3419 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3420 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3421 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3422 21355643 2020-12-06 stsp struct got_object_id *start_id;
3423 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3424 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3425 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3426 21355643 2020-12-06 stsp if (err)
3427 21355643 2020-12-06 stsp return err;
3428 21355643 2020-12-06 stsp free(s->start_id);
3429 21355643 2020-12-06 stsp s->start_id = start_id;
3430 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3431 21355643 2020-12-06 stsp } else /* 'B' */
3432 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3433 21355643 2020-12-06 stsp
3434 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3435 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3436 b0dd8d27 2022-06-16 stsp if (err)
3437 b0dd8d27 2022-06-16 stsp return err;
3438 b0dd8d27 2022-06-16 stsp }
3439 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3440 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3441 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3442 74cfe85e 2020-10-20 stsp if (err)
3443 21355643 2020-12-06 stsp return err;
3444 51a10b52 2020-12-26 stsp tog_free_refs();
3445 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3446 ca51c541 2020-12-07 stsp if (err)
3447 ca51c541 2020-12-07 stsp return err;
3448 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3449 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3450 d01904d4 2019-06-25 stsp if (err)
3451 d01904d4 2019-06-25 stsp return err;
3452 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3453 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3454 b672a97a 2020-01-27 stsp if (err)
3455 b672a97a 2020-01-27 stsp return err;
3456 21355643 2020-12-06 stsp free_commits(&s->commits);
3457 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3458 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3459 21355643 2020-12-06 stsp s->selected_entry = NULL;
3460 21355643 2020-12-06 stsp s->selected = 0;
3461 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3462 21355643 2020-12-06 stsp s->quit = 0;
3463 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3464 dfee752e 2022-06-18 op s->matched_entry = NULL;
3465 dfee752e 2022-06-18 op s->search_entry = NULL;
3466 d01904d4 2019-06-25 stsp break;
3467 6458efa5 2020-11-24 stsp case 'r':
3468 640cd7ff 2022-06-22 mark view->count = 0;
3469 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3470 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3471 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3472 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3473 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3474 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3475 6458efa5 2020-11-24 stsp if (err) {
3476 6458efa5 2020-11-24 stsp view_close(ref_view);
3477 6458efa5 2020-11-24 stsp return err;
3478 6458efa5 2020-11-24 stsp }
3479 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3480 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3481 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3482 49b24ee5 2022-07-03 mark if (err)
3483 49b24ee5 2022-07-03 mark break;
3484 49b24ee5 2022-07-03 mark }
3485 e78dc838 2020-12-04 stsp view->focussed = 0;
3486 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3487 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3488 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3489 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3490 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
3491 6458efa5 2020-11-24 stsp err = view_close_child(view);
3492 6458efa5 2020-11-24 stsp if (err)
3493 6458efa5 2020-11-24 stsp return err;
3494 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3495 0dbbbe90 2022-06-17 op if (err)
3496 0dbbbe90 2022-06-17 op return err;
3497 e78dc838 2020-12-04 stsp view->focus_child = 1;
3498 6458efa5 2020-11-24 stsp } else
3499 6458efa5 2020-11-24 stsp *new_view = ref_view;
3500 6458efa5 2020-11-24 stsp break;
3501 1e37a5c2 2019-05-12 jcs default:
3502 640cd7ff 2022-06-22 mark view->count = 0;
3503 1e37a5c2 2019-05-12 jcs break;
3504 899d86c2 2018-05-10 stsp }
3505 e5a0f69f 2018-08-18 stsp
3506 80ddbec8 2018-04-29 stsp return err;
3507 80ddbec8 2018-04-29 stsp }
3508 80ddbec8 2018-04-29 stsp
3509 4ed7e80c 2018-05-20 stsp static const struct got_error *
3510 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3511 c2db6724 2019-01-04 stsp {
3512 c2db6724 2019-01-04 stsp const struct got_error *error;
3513 c2db6724 2019-01-04 stsp
3514 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3515 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3516 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3517 37c06ea4 2019-07-15 stsp #endif
3518 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3519 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3520 c2db6724 2019-01-04 stsp
3521 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3522 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3523 c2db6724 2019-01-04 stsp
3524 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3525 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3526 c2db6724 2019-01-04 stsp
3527 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3528 c2db6724 2019-01-04 stsp if (error != NULL)
3529 c2db6724 2019-01-04 stsp return error;
3530 c2db6724 2019-01-04 stsp
3531 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3532 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3533 c2db6724 2019-01-04 stsp
3534 c2db6724 2019-01-04 stsp return NULL;
3535 c2db6724 2019-01-04 stsp }
3536 c2db6724 2019-01-04 stsp
3537 a915003a 2019-02-05 stsp static void
3538 a915003a 2019-02-05 stsp init_curses(void)
3539 a915003a 2019-02-05 stsp {
3540 2497f032 2022-05-31 stsp /*
3541 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3542 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3543 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3544 2497f032 2022-05-31 stsp */
3545 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3546 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3547 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3548 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3549 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3550 2497f032 2022-05-31 stsp
3551 a915003a 2019-02-05 stsp initscr();
3552 a915003a 2019-02-05 stsp cbreak();
3553 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3554 a915003a 2019-02-05 stsp noecho();
3555 a915003a 2019-02-05 stsp nonl();
3556 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3557 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3558 a915003a 2019-02-05 stsp curs_set(0);
3559 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3560 6d17833f 2019-11-08 stsp start_color();
3561 6d17833f 2019-11-08 stsp use_default_colors();
3562 6d17833f 2019-11-08 stsp }
3563 a915003a 2019-02-05 stsp }
3564 a915003a 2019-02-05 stsp
3565 c2db6724 2019-01-04 stsp static const struct got_error *
3566 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3567 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3568 f135c941 2020-02-20 stsp {
3569 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3570 f135c941 2020-02-20 stsp
3571 f135c941 2020-02-20 stsp if (argc == 0) {
3572 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3573 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3574 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3575 f135c941 2020-02-20 stsp return NULL;
3576 f135c941 2020-02-20 stsp }
3577 f135c941 2020-02-20 stsp
3578 f135c941 2020-02-20 stsp if (worktree) {
3579 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3580 bfd61697 2020-11-14 stsp char *p;
3581 f135c941 2020-02-20 stsp
3582 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3583 f135c941 2020-02-20 stsp if (err)
3584 f135c941 2020-02-20 stsp return err;
3585 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3586 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3587 bfd61697 2020-11-14 stsp p) == -1) {
3588 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3589 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3590 f135c941 2020-02-20 stsp }
3591 f135c941 2020-02-20 stsp free(p);
3592 f135c941 2020-02-20 stsp } else
3593 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3594 f135c941 2020-02-20 stsp
3595 f135c941 2020-02-20 stsp return err;
3596 f135c941 2020-02-20 stsp }
3597 f135c941 2020-02-20 stsp
3598 f135c941 2020-02-20 stsp static const struct got_error *
3599 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3600 9f7d7167 2018-04-29 stsp {
3601 80ddbec8 2018-04-29 stsp const struct got_error *error;
3602 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3603 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3604 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3605 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3606 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3607 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3608 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3609 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3610 04cc582a 2018-08-01 stsp struct tog_view *view;
3611 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3612 80ddbec8 2018-04-29 stsp
3613 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3614 80ddbec8 2018-04-29 stsp switch (ch) {
3615 b672a97a 2020-01-27 stsp case 'b':
3616 b672a97a 2020-01-27 stsp log_branches = 1;
3617 b672a97a 2020-01-27 stsp break;
3618 80ddbec8 2018-04-29 stsp case 'c':
3619 80ddbec8 2018-04-29 stsp start_commit = optarg;
3620 80ddbec8 2018-04-29 stsp break;
3621 ecb28ae0 2018-07-16 stsp case 'r':
3622 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3623 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3624 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3625 9ba1d308 2019-10-21 stsp optarg);
3626 ecb28ae0 2018-07-16 stsp break;
3627 80ddbec8 2018-04-29 stsp default:
3628 17020d27 2019-03-07 stsp usage_log();
3629 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3630 80ddbec8 2018-04-29 stsp }
3631 80ddbec8 2018-04-29 stsp }
3632 80ddbec8 2018-04-29 stsp
3633 80ddbec8 2018-04-29 stsp argc -= optind;
3634 80ddbec8 2018-04-29 stsp argv += optind;
3635 80ddbec8 2018-04-29 stsp
3636 f135c941 2020-02-20 stsp if (argc > 1)
3637 f135c941 2020-02-20 stsp usage_log();
3638 963f97a1 2019-03-18 stsp
3639 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3640 0ae84acc 2022-06-15 tracey if (error != NULL)
3641 0ae84acc 2022-06-15 tracey goto done;
3642 0ae84acc 2022-06-15 tracey
3643 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3644 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3645 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3646 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3647 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3648 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3649 c156c7a4 2020-12-18 stsp goto done;
3650 a1fbf39a 2019-08-11 stsp if (worktree)
3651 6962eb72 2020-02-20 stsp repo_path =
3652 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3653 a1fbf39a 2019-08-11 stsp else
3654 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3655 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3656 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3657 c156c7a4 2020-12-18 stsp goto done;
3658 c156c7a4 2020-12-18 stsp }
3659 ecb28ae0 2018-07-16 stsp }
3660 ecb28ae0 2018-07-16 stsp
3661 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3662 80ddbec8 2018-04-29 stsp if (error != NULL)
3663 ecb28ae0 2018-07-16 stsp goto done;
3664 80ddbec8 2018-04-29 stsp
3665 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3666 f135c941 2020-02-20 stsp repo, worktree);
3667 f135c941 2020-02-20 stsp if (error)
3668 f135c941 2020-02-20 stsp goto done;
3669 f135c941 2020-02-20 stsp
3670 f135c941 2020-02-20 stsp init_curses();
3671 f135c941 2020-02-20 stsp
3672 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3673 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3674 c02c541e 2019-03-29 stsp if (error)
3675 c02c541e 2019-03-29 stsp goto done;
3676 c02c541e 2019-03-29 stsp
3677 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3678 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3679 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3680 87670572 2020-12-26 naddy if (error)
3681 87670572 2020-12-26 naddy goto done;
3682 87670572 2020-12-26 naddy }
3683 51a10b52 2020-12-26 stsp
3684 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3685 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3686 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3687 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3688 d8f38dc4 2020-12-05 stsp if (error)
3689 d8f38dc4 2020-12-05 stsp goto done;
3690 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3691 d8f38dc4 2020-12-05 stsp } else {
3692 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3693 d8f38dc4 2020-12-05 stsp if (error == NULL)
3694 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3695 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3696 d8f38dc4 2020-12-05 stsp goto done;
3697 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3698 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3699 d8f38dc4 2020-12-05 stsp if (error)
3700 d8f38dc4 2020-12-05 stsp goto done;
3701 d8f38dc4 2020-12-05 stsp }
3702 8b473291 2019-02-21 stsp
3703 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3704 04cc582a 2018-08-01 stsp if (view == NULL) {
3705 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3706 04cc582a 2018-08-01 stsp goto done;
3707 04cc582a 2018-08-01 stsp }
3708 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3709 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3710 ba4f502b 2018-08-04 stsp if (error)
3711 ba4f502b 2018-08-04 stsp goto done;
3712 2fc00ff4 2019-08-31 stsp if (worktree) {
3713 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3714 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3715 2fc00ff4 2019-08-31 stsp worktree = NULL;
3716 2fc00ff4 2019-08-31 stsp }
3717 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3718 ecb28ae0 2018-07-16 stsp done:
3719 f135c941 2020-02-20 stsp free(in_repo_path);
3720 ecb28ae0 2018-07-16 stsp free(repo_path);
3721 ecb28ae0 2018-07-16 stsp free(cwd);
3722 899d86c2 2018-05-10 stsp free(start_id);
3723 d8f38dc4 2020-12-05 stsp free(label);
3724 d8f38dc4 2020-12-05 stsp if (ref)
3725 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3726 1d0f4054 2021-06-17 stsp if (repo) {
3727 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3728 1d0f4054 2021-06-17 stsp if (error == NULL)
3729 1d0f4054 2021-06-17 stsp error = close_err;
3730 1d0f4054 2021-06-17 stsp }
3731 ec142235 2019-03-07 stsp if (worktree)
3732 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3733 0ae84acc 2022-06-15 tracey if (pack_fds) {
3734 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3735 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3736 0ae84acc 2022-06-15 tracey if (error == NULL)
3737 0ae84acc 2022-06-15 tracey error = pack_err;
3738 0ae84acc 2022-06-15 tracey }
3739 51a10b52 2020-12-26 stsp tog_free_refs();
3740 80ddbec8 2018-04-29 stsp return error;
3741 9f7d7167 2018-04-29 stsp }
3742 9f7d7167 2018-04-29 stsp
3743 4ed7e80c 2018-05-20 stsp __dead static void
3744 9f7d7167 2018-04-29 stsp usage_diff(void)
3745 9f7d7167 2018-04-29 stsp {
3746 80ddbec8 2018-04-29 stsp endwin();
3747 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3748 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3749 9f7d7167 2018-04-29 stsp exit(1);
3750 b304db33 2018-05-20 stsp }
3751 b304db33 2018-05-20 stsp
3752 6d17833f 2019-11-08 stsp static int
3753 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3754 41605754 2020-11-12 stsp regmatch_t *regmatch)
3755 6d17833f 2019-11-08 stsp {
3756 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3757 6d17833f 2019-11-08 stsp }
3758 6d17833f 2019-11-08 stsp
3759 336075a4 2022-06-25 op static struct tog_color *
3760 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3761 6d17833f 2019-11-08 stsp {
3762 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3763 6d17833f 2019-11-08 stsp
3764 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3765 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3766 6d17833f 2019-11-08 stsp return tc;
3767 6d17833f 2019-11-08 stsp }
3768 6d17833f 2019-11-08 stsp
3769 6d17833f 2019-11-08 stsp return NULL;
3770 6d17833f 2019-11-08 stsp }
3771 6d17833f 2019-11-08 stsp
3772 4ed7e80c 2018-05-20 stsp static const struct got_error *
3773 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3774 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3775 41605754 2020-11-12 stsp {
3776 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3777 44a87665 2022-06-16 stsp char *exstr = NULL;
3778 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3779 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3780 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3781 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3782 41605754 2020-11-12 stsp
3783 41605754 2020-11-12 stsp *wtotal = 0;
3784 1853e0f4 2022-06-16 stsp
3785 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3786 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3787 41605754 2020-11-12 stsp
3788 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3789 44a87665 2022-06-16 stsp if (err)
3790 44a87665 2022-06-16 stsp return err;
3791 44a87665 2022-06-16 stsp
3792 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3793 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3794 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3795 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3796 44a87665 2022-06-16 stsp goto done;
3797 44a87665 2022-06-16 stsp }
3798 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3799 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3800 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3801 1853e0f4 2022-06-16 stsp goto done;
3802 1853e0f4 2022-06-16 stsp }
3803 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3804 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3805 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3806 1853e0f4 2022-06-16 stsp goto done;
3807 1853e0f4 2022-06-16 stsp }
3808 145b6838 2022-06-16 stsp
3809 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3810 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3811 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3812 1853e0f4 2022-06-16 stsp if (err)
3813 1853e0f4 2022-06-16 stsp goto done;
3814 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3815 145b6838 2022-06-16 stsp if (n) {
3816 1853e0f4 2022-06-16 stsp free(wline);
3817 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3818 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3819 1853e0f4 2022-06-16 stsp if (err)
3820 1853e0f4 2022-06-16 stsp goto done;
3821 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3822 1853e0f4 2022-06-16 stsp wlimit -= width;
3823 1853e0f4 2022-06-16 stsp *wtotal += width;
3824 41605754 2020-11-12 stsp }
3825 41605754 2020-11-12 stsp
3826 41605754 2020-11-12 stsp if (wlimit > 0) {
3827 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3828 1853e0f4 2022-06-16 stsp size_t wlen;
3829 1853e0f4 2022-06-16 stsp
3830 1853e0f4 2022-06-16 stsp free(wline);
3831 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3832 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3833 1853e0f4 2022-06-16 stsp if (err)
3834 1853e0f4 2022-06-16 stsp goto done;
3835 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3836 1853e0f4 2022-06-16 stsp while (i < wlen) {
3837 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3838 1853e0f4 2022-06-16 stsp if (width == -1) {
3839 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3840 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3841 1853e0f4 2022-06-16 stsp goto done;
3842 1853e0f4 2022-06-16 stsp }
3843 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3844 1853e0f4 2022-06-16 stsp break;
3845 61417565 2022-06-20 mark w += width;
3846 1853e0f4 2022-06-16 stsp i++;
3847 41605754 2020-11-12 stsp }
3848 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3849 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3850 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3851 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3852 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3853 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3854 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3855 41605754 2020-11-12 stsp }
3856 41605754 2020-11-12 stsp }
3857 41605754 2020-11-12 stsp
3858 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3859 1853e0f4 2022-06-16 stsp free(wline);
3860 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3861 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3862 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3863 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3864 1853e0f4 2022-06-16 stsp if (err)
3865 1853e0f4 2022-06-16 stsp goto done;
3866 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3867 1853e0f4 2022-06-16 stsp } else {
3868 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3869 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3870 1853e0f4 2022-06-16 stsp if (err)
3871 1853e0f4 2022-06-16 stsp goto done;
3872 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3873 1853e0f4 2022-06-16 stsp }
3874 1853e0f4 2022-06-16 stsp *wtotal += width2;
3875 41605754 2020-11-12 stsp }
3876 1853e0f4 2022-06-16 stsp done:
3877 145b6838 2022-06-16 stsp free(wline);
3878 44a87665 2022-06-16 stsp free(exstr);
3879 1853e0f4 2022-06-16 stsp free(seg0);
3880 1853e0f4 2022-06-16 stsp free(seg1);
3881 1853e0f4 2022-06-16 stsp free(seg2);
3882 1853e0f4 2022-06-16 stsp return err;
3883 41605754 2020-11-12 stsp }
3884 41605754 2020-11-12 stsp
3885 41605754 2020-11-12 stsp static const struct got_error *
3886 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3887 26ed57b2 2018-05-19 stsp {
3888 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3889 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3890 61e69b96 2018-05-20 stsp const struct got_error *err;
3891 fe621944 2020-11-10 stsp int nprinted = 0;
3892 b304db33 2018-05-20 stsp char *line;
3893 826082fe 2020-12-10 stsp size_t linesize = 0;
3894 826082fe 2020-12-10 stsp ssize_t linelen;
3895 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3896 61e69b96 2018-05-20 stsp wchar_t *wline;
3897 e0b650dd 2018-05-20 stsp int width;
3898 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3899 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3900 fe621944 2020-11-10 stsp off_t line_offset;
3901 26ed57b2 2018-05-19 stsp
3902 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3903 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3904 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3905 fe621944 2020-11-10 stsp
3906 f7d12f7e 2018-08-01 stsp werase(view->window);
3907 a3404814 2018-09-02 stsp
3908 a3404814 2018-09-02 stsp if (header) {
3909 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3910 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3911 135a2da0 2020-11-11 stsp header) == -1)
3912 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3913 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3914 ccda2f4d 2022-06-16 stsp 0, 0);
3915 135a2da0 2020-11-11 stsp free(line);
3916 135a2da0 2020-11-11 stsp if (err)
3917 a3404814 2018-09-02 stsp return err;
3918 a3404814 2018-09-02 stsp
3919 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3920 a3404814 2018-09-02 stsp wstandout(view->window);
3921 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3922 e54cc94a 2020-11-11 stsp free(wline);
3923 e54cc94a 2020-11-11 stsp wline = NULL;
3924 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3925 a3404814 2018-09-02 stsp wstandend(view->window);
3926 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3927 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3928 26ed57b2 2018-05-19 stsp
3929 a3404814 2018-09-02 stsp if (max_lines <= 1)
3930 a3404814 2018-09-02 stsp return NULL;
3931 a3404814 2018-09-02 stsp max_lines--;
3932 a3404814 2018-09-02 stsp }
3933 a3404814 2018-09-02 stsp
3934 89f1a395 2020-12-01 naddy s->eof = 0;
3935 145b6838 2022-06-16 stsp view->maxx = 0;
3936 826082fe 2020-12-10 stsp line = NULL;
3937 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3938 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3939 826082fe 2020-12-10 stsp if (linelen == -1) {
3940 826082fe 2020-12-10 stsp if (feof(s->f)) {
3941 826082fe 2020-12-10 stsp s->eof = 1;
3942 826082fe 2020-12-10 stsp break;
3943 826082fe 2020-12-10 stsp }
3944 826082fe 2020-12-10 stsp free(line);
3945 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3946 61e69b96 2018-05-20 stsp }
3947 145b6838 2022-06-16 stsp
3948 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3949 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3950 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3951 1853e0f4 2022-06-16 stsp if (err) {
3952 1853e0f4 2022-06-16 stsp free(line);
3953 1853e0f4 2022-06-16 stsp return err;
3954 1853e0f4 2022-06-16 stsp }
3955 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3956 1853e0f4 2022-06-16 stsp free(wline);
3957 1853e0f4 2022-06-16 stsp wline = NULL;
3958 1853e0f4 2022-06-16 stsp
3959 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3960 f26dddb7 2019-11-08 stsp if (tc)
3961 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3962 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3963 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3964 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3965 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3966 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3967 41605754 2020-11-12 stsp if (err) {
3968 41605754 2020-11-12 stsp free(line);
3969 41605754 2020-11-12 stsp return err;
3970 41605754 2020-11-12 stsp }
3971 41605754 2020-11-12 stsp } else {
3972 969c159c 2022-06-16 stsp int skip;
3973 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3974 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3975 969c159c 2022-06-16 stsp if (err) {
3976 969c159c 2022-06-16 stsp free(line);
3977 969c159c 2022-06-16 stsp return err;
3978 969c159c 2022-06-16 stsp }
3979 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3980 969c159c 2022-06-16 stsp free(wline);
3981 41605754 2020-11-12 stsp wline = NULL;
3982 41605754 2020-11-12 stsp }
3983 f26dddb7 2019-11-08 stsp if (tc)
3984 6d17833f 2019-11-08 stsp wattr_off(view->window,
3985 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3986 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3987 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3988 fe621944 2020-11-10 stsp nprinted++;
3989 826082fe 2020-12-10 stsp }
3990 826082fe 2020-12-10 stsp free(line);
3991 fe621944 2020-11-10 stsp if (nprinted >= 1)
3992 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3993 89f1a395 2020-12-01 naddy (nprinted - 1);
3994 fe621944 2020-11-10 stsp else
3995 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3996 26ed57b2 2018-05-19 stsp
3997 9b058f45 2022-06-30 mark view_border(view);
3998 c3e9aa98 2019-05-13 jcs
3999 89f1a395 2020-12-01 naddy if (s->eof) {
4000 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4001 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4002 c3e9aa98 2019-05-13 jcs nprinted++;
4003 c3e9aa98 2019-05-13 jcs }
4004 c3e9aa98 2019-05-13 jcs
4005 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4006 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4007 c3e9aa98 2019-05-13 jcs if (err) {
4008 c3e9aa98 2019-05-13 jcs return err;
4009 c3e9aa98 2019-05-13 jcs }
4010 26ed57b2 2018-05-19 stsp
4011 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4012 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4013 e54cc94a 2020-11-11 stsp free(wline);
4014 e54cc94a 2020-11-11 stsp wline = NULL;
4015 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4016 c3e9aa98 2019-05-13 jcs }
4017 c3e9aa98 2019-05-13 jcs
4018 26ed57b2 2018-05-19 stsp return NULL;
4019 abd2672a 2018-12-23 stsp }
4020 abd2672a 2018-12-23 stsp
4021 abd2672a 2018-12-23 stsp static char *
4022 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4023 abd2672a 2018-12-23 stsp {
4024 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4025 09867e48 2019-08-13 stsp char *p, *s;
4026 09867e48 2019-08-13 stsp
4027 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4028 09867e48 2019-08-13 stsp if (tm == NULL)
4029 09867e48 2019-08-13 stsp return NULL;
4030 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4031 09867e48 2019-08-13 stsp if (s == NULL)
4032 09867e48 2019-08-13 stsp return NULL;
4033 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4034 abd2672a 2018-12-23 stsp if (p)
4035 abd2672a 2018-12-23 stsp *p = '\0';
4036 abd2672a 2018-12-23 stsp return s;
4037 9f7d7167 2018-04-29 stsp }
4038 9f7d7167 2018-04-29 stsp
4039 4ed7e80c 2018-05-20 stsp static const struct got_error *
4040 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4041 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4042 0208f208 2020-05-05 stsp {
4043 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4044 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4045 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4046 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4047 0208f208 2020-05-05 stsp
4048 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4049 0208f208 2020-05-05 stsp if (qid != NULL) {
4050 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4051 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4052 d7b5a0e8 2022-04-20 stsp &qid->id);
4053 0208f208 2020-05-05 stsp if (err)
4054 0208f208 2020-05-05 stsp return err;
4055 0208f208 2020-05-05 stsp
4056 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4057 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4058 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4059 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4060 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4061 aa8b5dd0 2021-08-01 stsp }
4062 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4063 0208f208 2020-05-05 stsp
4064 0208f208 2020-05-05 stsp }
4065 0208f208 2020-05-05 stsp
4066 0208f208 2020-05-05 stsp if (tree_id1) {
4067 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4068 0208f208 2020-05-05 stsp if (err)
4069 0208f208 2020-05-05 stsp goto done;
4070 0208f208 2020-05-05 stsp }
4071 0208f208 2020-05-05 stsp
4072 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4073 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4074 0208f208 2020-05-05 stsp if (err)
4075 0208f208 2020-05-05 stsp goto done;
4076 0208f208 2020-05-05 stsp
4077 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4078 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4079 0208f208 2020-05-05 stsp done:
4080 0208f208 2020-05-05 stsp if (tree1)
4081 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4082 0208f208 2020-05-05 stsp if (tree2)
4083 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4084 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4085 0208f208 2020-05-05 stsp return err;
4086 0208f208 2020-05-05 stsp }
4087 0208f208 2020-05-05 stsp
4088 0208f208 2020-05-05 stsp static const struct got_error *
4089 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4090 abd2672a 2018-12-23 stsp {
4091 fe621944 2020-11-10 stsp off_t *p;
4092 fe621944 2020-11-10 stsp
4093 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4094 fe621944 2020-11-10 stsp if (p == NULL)
4095 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4096 fe621944 2020-11-10 stsp *line_offsets = p;
4097 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4098 fe621944 2020-11-10 stsp (*nlines)++;
4099 fe621944 2020-11-10 stsp return NULL;
4100 fe621944 2020-11-10 stsp }
4101 fe621944 2020-11-10 stsp
4102 fe621944 2020-11-10 stsp static const struct got_error *
4103 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4104 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4105 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4106 fe621944 2020-11-10 stsp {
4107 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4108 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4109 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4110 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4111 45d799e2 2018-12-23 stsp time_t committer_time;
4112 45d799e2 2018-12-23 stsp const char *author, *committer;
4113 8b473291 2019-02-21 stsp char *refs_str = NULL;
4114 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4115 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4116 fe621944 2020-11-10 stsp off_t outoff = 0;
4117 fe621944 2020-11-10 stsp int n;
4118 abd2672a 2018-12-23 stsp
4119 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4120 0208f208 2020-05-05 stsp
4121 8b473291 2019-02-21 stsp if (refs) {
4122 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4123 8b473291 2019-02-21 stsp if (err)
4124 8b473291 2019-02-21 stsp return err;
4125 8b473291 2019-02-21 stsp }
4126 8b473291 2019-02-21 stsp
4127 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4128 abd2672a 2018-12-23 stsp if (err)
4129 abd2672a 2018-12-23 stsp return err;
4130 abd2672a 2018-12-23 stsp
4131 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4132 15a94983 2018-12-23 stsp if (err) {
4133 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4134 15a94983 2018-12-23 stsp goto done;
4135 15a94983 2018-12-23 stsp }
4136 abd2672a 2018-12-23 stsp
4137 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4138 fe621944 2020-11-10 stsp if (err)
4139 fe621944 2020-11-10 stsp goto done;
4140 fe621944 2020-11-10 stsp
4141 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4142 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4143 fe621944 2020-11-10 stsp if (n < 0) {
4144 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4145 abd2672a 2018-12-23 stsp goto done;
4146 abd2672a 2018-12-23 stsp }
4147 fe621944 2020-11-10 stsp outoff += n;
4148 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4149 fe621944 2020-11-10 stsp if (err)
4150 fe621944 2020-11-10 stsp goto done;
4151 fe621944 2020-11-10 stsp
4152 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4153 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4154 fe621944 2020-11-10 stsp if (n < 0) {
4155 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4156 abd2672a 2018-12-23 stsp goto done;
4157 abd2672a 2018-12-23 stsp }
4158 fe621944 2020-11-10 stsp outoff += n;
4159 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4160 fe621944 2020-11-10 stsp if (err)
4161 fe621944 2020-11-10 stsp goto done;
4162 fe621944 2020-11-10 stsp
4163 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4164 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4165 fe621944 2020-11-10 stsp if (datestr) {
4166 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4167 fe621944 2020-11-10 stsp if (n < 0) {
4168 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4169 fe621944 2020-11-10 stsp goto done;
4170 fe621944 2020-11-10 stsp }
4171 fe621944 2020-11-10 stsp outoff += n;
4172 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4173 fe621944 2020-11-10 stsp if (err)
4174 fe621944 2020-11-10 stsp goto done;
4175 abd2672a 2018-12-23 stsp }
4176 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4177 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4178 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4179 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4180 fe621944 2020-11-10 stsp if (n < 0) {
4181 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4182 fe621944 2020-11-10 stsp goto done;
4183 fe621944 2020-11-10 stsp }
4184 fe621944 2020-11-10 stsp outoff += n;
4185 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4186 fe621944 2020-11-10 stsp if (err)
4187 fe621944 2020-11-10 stsp goto done;
4188 abd2672a 2018-12-23 stsp }
4189 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4190 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4191 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4192 9f98ca05 2021-09-24 stsp int pn = 1;
4193 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4194 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4195 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4196 9f98ca05 2021-09-24 stsp if (err)
4197 9f98ca05 2021-09-24 stsp goto done;
4198 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4199 9f98ca05 2021-09-24 stsp if (n < 0) {
4200 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4201 9f98ca05 2021-09-24 stsp goto done;
4202 9f98ca05 2021-09-24 stsp }
4203 9f98ca05 2021-09-24 stsp outoff += n;
4204 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4205 9f98ca05 2021-09-24 stsp if (err)
4206 9f98ca05 2021-09-24 stsp goto done;
4207 9f98ca05 2021-09-24 stsp free(id_str);
4208 9f98ca05 2021-09-24 stsp id_str = NULL;
4209 9f98ca05 2021-09-24 stsp }
4210 9f98ca05 2021-09-24 stsp }
4211 9f98ca05 2021-09-24 stsp
4212 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4213 5943eee2 2019-08-13 stsp if (err)
4214 5943eee2 2019-08-13 stsp goto done;
4215 fe621944 2020-11-10 stsp s = logmsg;
4216 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4217 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4218 fe621944 2020-11-10 stsp if (n < 0) {
4219 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4220 fe621944 2020-11-10 stsp goto done;
4221 fe621944 2020-11-10 stsp }
4222 fe621944 2020-11-10 stsp outoff += n;
4223 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4224 fe621944 2020-11-10 stsp if (err)
4225 fe621944 2020-11-10 stsp goto done;
4226 abd2672a 2018-12-23 stsp }
4227 fe621944 2020-11-10 stsp
4228 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4229 0208f208 2020-05-05 stsp if (err)
4230 0208f208 2020-05-05 stsp goto done;
4231 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4232 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4233 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4234 fe621944 2020-11-10 stsp if (n < 0) {
4235 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4236 fe621944 2020-11-10 stsp goto done;
4237 fe621944 2020-11-10 stsp }
4238 fe621944 2020-11-10 stsp outoff += n;
4239 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4240 fe621944 2020-11-10 stsp if (err)
4241 fe621944 2020-11-10 stsp goto done;
4242 0208f208 2020-05-05 stsp free((char *)pe->path);
4243 0208f208 2020-05-05 stsp free(pe->data);
4244 0208f208 2020-05-05 stsp }
4245 fe621944 2020-11-10 stsp
4246 0208f208 2020-05-05 stsp fputc('\n', outfile);
4247 fe621944 2020-11-10 stsp outoff++;
4248 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4249 abd2672a 2018-12-23 stsp done:
4250 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4251 abd2672a 2018-12-23 stsp free(id_str);
4252 5943eee2 2019-08-13 stsp free(logmsg);
4253 8b473291 2019-02-21 stsp free(refs_str);
4254 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4255 7510f233 2020-08-09 stsp if (err) {
4256 ae6a6978 2020-08-09 stsp free(*line_offsets);
4257 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4258 ae6a6978 2020-08-09 stsp *nlines = 0;
4259 7510f233 2020-08-09 stsp }
4260 fe621944 2020-11-10 stsp return err;
4261 abd2672a 2018-12-23 stsp }
4262 abd2672a 2018-12-23 stsp
4263 abd2672a 2018-12-23 stsp static const struct got_error *
4264 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4265 26ed57b2 2018-05-19 stsp {
4266 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4267 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4268 15a94983 2018-12-23 stsp int obj_type;
4269 fe621944 2020-11-10 stsp
4270 fe621944 2020-11-10 stsp free(s->line_offsets);
4271 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4272 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4273 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4274 fe621944 2020-11-10 stsp s->nlines = 0;
4275 26ed57b2 2018-05-19 stsp
4276 511a516b 2018-05-19 stsp f = got_opentemp();
4277 48ae06ee 2018-10-18 stsp if (f == NULL) {
4278 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4279 48ae06ee 2018-10-18 stsp goto done;
4280 48ae06ee 2018-10-18 stsp }
4281 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4282 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4283 fb43ecf1 2019-02-11 stsp goto done;
4284 fb43ecf1 2019-02-11 stsp }
4285 48ae06ee 2018-10-18 stsp s->f = f;
4286 26ed57b2 2018-05-19 stsp
4287 15a94983 2018-12-23 stsp if (s->id1)
4288 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4289 15a94983 2018-12-23 stsp else
4290 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4291 15a94983 2018-12-23 stsp if (err)
4292 15a94983 2018-12-23 stsp goto done;
4293 15a94983 2018-12-23 stsp
4294 15a94983 2018-12-23 stsp switch (obj_type) {
4295 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4296 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4297 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4298 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4299 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4300 26ed57b2 2018-05-19 stsp break;
4301 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4302 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4303 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4304 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4305 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4306 26ed57b2 2018-05-19 stsp break;
4307 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4308 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4309 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4310 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4311 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4312 abd2672a 2018-12-23 stsp
4313 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4314 abd2672a 2018-12-23 stsp if (err)
4315 3ffacbe1 2020-02-02 tracey goto done;
4316 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4317 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4318 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4319 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4320 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4321 f44b1f58 2020-02-02 tracey if (err)
4322 f44b1f58 2020-02-02 tracey goto done;
4323 f44b1f58 2020-02-02 tracey } else {
4324 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4325 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4326 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4327 fe621944 2020-11-10 stsp err = write_commit_info(
4328 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4329 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4330 f44b1f58 2020-02-02 tracey if (err)
4331 f44b1f58 2020-02-02 tracey goto done;
4332 f5404e4e 2020-02-02 tracey break;
4333 15a087fe 2019-02-21 stsp }
4334 abd2672a 2018-12-23 stsp }
4335 abd2672a 2018-12-23 stsp }
4336 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4337 abd2672a 2018-12-23 stsp
4338 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4339 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4340 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4341 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4342 26ed57b2 2018-05-19 stsp break;
4343 abd2672a 2018-12-23 stsp }
4344 26ed57b2 2018-05-19 stsp default:
4345 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4346 48ae06ee 2018-10-18 stsp break;
4347 26ed57b2 2018-05-19 stsp }
4348 f44b1f58 2020-02-02 tracey if (err)
4349 f44b1f58 2020-02-02 tracey goto done;
4350 48ae06ee 2018-10-18 stsp done:
4351 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4352 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4353 48ae06ee 2018-10-18 stsp return err;
4354 48ae06ee 2018-10-18 stsp }
4355 26ed57b2 2018-05-19 stsp
4356 f5215bb9 2019-02-22 stsp static void
4357 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4358 f5215bb9 2019-02-22 stsp {
4359 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4360 f5215bb9 2019-02-22 stsp update_panels();
4361 f5215bb9 2019-02-22 stsp doupdate();
4362 f44b1f58 2020-02-02 tracey }
4363 f44b1f58 2020-02-02 tracey
4364 f44b1f58 2020-02-02 tracey static const struct got_error *
4365 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4366 f44b1f58 2020-02-02 tracey {
4367 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4368 f44b1f58 2020-02-02 tracey
4369 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4370 f44b1f58 2020-02-02 tracey return NULL;
4371 f44b1f58 2020-02-02 tracey }
4372 f44b1f58 2020-02-02 tracey
4373 f44b1f58 2020-02-02 tracey static const struct got_error *
4374 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4375 f44b1f58 2020-02-02 tracey {
4376 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4377 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4378 f44b1f58 2020-02-02 tracey int lineno;
4379 cb713507 2022-06-16 stsp char *line = NULL;
4380 826082fe 2020-12-10 stsp size_t linesize = 0;
4381 826082fe 2020-12-10 stsp ssize_t linelen;
4382 f44b1f58 2020-02-02 tracey
4383 f44b1f58 2020-02-02 tracey if (!view->searching) {
4384 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 f44b1f58 2020-02-02 tracey return NULL;
4386 f44b1f58 2020-02-02 tracey }
4387 f44b1f58 2020-02-02 tracey
4388 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4389 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4390 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4391 f44b1f58 2020-02-02 tracey else
4392 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4393 487cd7d2 2021-12-17 stsp } else
4394 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4395 f44b1f58 2020-02-02 tracey
4396 f44b1f58 2020-02-02 tracey while (1) {
4397 f44b1f58 2020-02-02 tracey off_t offset;
4398 f44b1f58 2020-02-02 tracey
4399 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4400 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4401 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4402 f44b1f58 2020-02-02 tracey break;
4403 f44b1f58 2020-02-02 tracey }
4404 f44b1f58 2020-02-02 tracey
4405 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4406 f44b1f58 2020-02-02 tracey lineno = 1;
4407 f44b1f58 2020-02-02 tracey else
4408 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4409 f44b1f58 2020-02-02 tracey }
4410 f44b1f58 2020-02-02 tracey
4411 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4412 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4413 f44b1f58 2020-02-02 tracey free(line);
4414 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4415 f44b1f58 2020-02-02 tracey }
4416 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4417 cb713507 2022-06-16 stsp if (linelen != -1) {
4418 cb713507 2022-06-16 stsp char *exstr;
4419 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4420 cb713507 2022-06-16 stsp if (err)
4421 cb713507 2022-06-16 stsp break;
4422 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4423 cb713507 2022-06-16 stsp &view->regmatch)) {
4424 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4425 cb713507 2022-06-16 stsp s->matched_line = lineno;
4426 cb713507 2022-06-16 stsp free(exstr);
4427 cb713507 2022-06-16 stsp break;
4428 cb713507 2022-06-16 stsp }
4429 cb713507 2022-06-16 stsp free(exstr);
4430 f44b1f58 2020-02-02 tracey }
4431 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4432 f44b1f58 2020-02-02 tracey lineno++;
4433 f44b1f58 2020-02-02 tracey else
4434 f44b1f58 2020-02-02 tracey lineno--;
4435 f44b1f58 2020-02-02 tracey }
4436 826082fe 2020-12-10 stsp free(line);
4437 f44b1f58 2020-02-02 tracey
4438 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4439 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4440 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4441 f44b1f58 2020-02-02 tracey }
4442 f44b1f58 2020-02-02 tracey
4443 145b6838 2022-06-16 stsp return err;
4444 f5215bb9 2019-02-22 stsp }
4445 f5215bb9 2019-02-22 stsp
4446 48ae06ee 2018-10-18 stsp static const struct got_error *
4447 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4448 b72706c3 2022-06-01 stsp {
4449 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4450 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4451 b72706c3 2022-06-01 stsp
4452 b72706c3 2022-06-01 stsp free(s->id1);
4453 b72706c3 2022-06-01 stsp s->id1 = NULL;
4454 b72706c3 2022-06-01 stsp free(s->id2);
4455 b72706c3 2022-06-01 stsp s->id2 = NULL;
4456 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4457 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4458 b72706c3 2022-06-01 stsp s->f = NULL;
4459 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4460 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4461 b72706c3 2022-06-01 stsp s->f1 = NULL;
4462 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4463 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4464 b72706c3 2022-06-01 stsp s->f2 = NULL;
4465 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4466 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4467 f9d37699 2022-06-28 stsp s->fd1 = -1;
4468 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4469 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4470 f9d37699 2022-06-28 stsp s->fd2 = -1;
4471 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4472 b72706c3 2022-06-01 stsp free(s->line_offsets);
4473 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4474 b72706c3 2022-06-01 stsp s->nlines = 0;
4475 b72706c3 2022-06-01 stsp return err;
4476 b72706c3 2022-06-01 stsp }
4477 b72706c3 2022-06-01 stsp
4478 b72706c3 2022-06-01 stsp static const struct got_error *
4479 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4480 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4481 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4482 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4483 48ae06ee 2018-10-18 stsp {
4484 48ae06ee 2018-10-18 stsp const struct got_error *err;
4485 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4486 5dc9f4bc 2018-08-04 stsp
4487 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4488 f9d37699 2022-06-28 stsp s->fd1 = -1;
4489 f9d37699 2022-06-28 stsp s->fd2 = -1;
4490 b72706c3 2022-06-01 stsp
4491 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4492 15a94983 2018-12-23 stsp int type1, type2;
4493 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4494 15a94983 2018-12-23 stsp if (err)
4495 15a94983 2018-12-23 stsp return err;
4496 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4497 15a94983 2018-12-23 stsp if (err)
4498 15a94983 2018-12-23 stsp return err;
4499 15a94983 2018-12-23 stsp
4500 15a94983 2018-12-23 stsp if (type1 != type2)
4501 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4502 15a94983 2018-12-23 stsp }
4503 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4504 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4505 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4506 f44b1f58 2020-02-02 tracey s->repo = repo;
4507 f44b1f58 2020-02-02 tracey s->id1 = id1;
4508 f44b1f58 2020-02-02 tracey s->id2 = id2;
4509 3dbaef42 2020-11-24 stsp s->label1 = label1;
4510 3dbaef42 2020-11-24 stsp s->label2 = label2;
4511 48ae06ee 2018-10-18 stsp
4512 15a94983 2018-12-23 stsp if (id1) {
4513 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4514 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4515 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4516 48ae06ee 2018-10-18 stsp } else
4517 5465d566 2020-02-01 tracey s->id1 = NULL;
4518 48ae06ee 2018-10-18 stsp
4519 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4520 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4521 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4522 b72706c3 2022-06-01 stsp goto done;
4523 48ae06ee 2018-10-18 stsp }
4524 b72706c3 2022-06-01 stsp
4525 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4526 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4527 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4528 a00719e9 2022-06-17 stsp goto done;
4529 a00719e9 2022-06-17 stsp }
4530 a00719e9 2022-06-17 stsp
4531 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4532 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4533 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4534 b72706c3 2022-06-01 stsp goto done;
4535 b72706c3 2022-06-01 stsp }
4536 b72706c3 2022-06-01 stsp
4537 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4538 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4539 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4540 f9d37699 2022-06-28 stsp goto done;
4541 f9d37699 2022-06-28 stsp }
4542 f9d37699 2022-06-28 stsp
4543 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4544 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4545 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4546 f9d37699 2022-06-28 stsp goto done;
4547 f9d37699 2022-06-28 stsp }
4548 f9d37699 2022-06-28 stsp
4549 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4550 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4551 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4552 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4553 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4554 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4555 5465d566 2020-02-01 tracey s->repo = repo;
4556 6d17833f 2019-11-08 stsp
4557 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4558 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4559 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4560 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4561 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4562 6d17833f 2019-11-08 stsp if (err)
4563 b72706c3 2022-06-01 stsp goto done;
4564 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4565 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4566 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4567 b72706c3 2022-06-01 stsp if (err)
4568 b72706c3 2022-06-01 stsp goto done;
4569 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4570 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4571 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4572 b72706c3 2022-06-01 stsp if (err)
4573 b72706c3 2022-06-01 stsp goto done;
4574 6d17833f 2019-11-08 stsp
4575 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4576 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4577 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4578 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4579 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4580 b72706c3 2022-06-01 stsp if (err)
4581 b72706c3 2022-06-01 stsp goto done;
4582 11b20872 2019-11-08 stsp
4583 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4584 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4585 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4586 b72706c3 2022-06-01 stsp if (err)
4587 b72706c3 2022-06-01 stsp goto done;
4588 11b20872 2019-11-08 stsp
4589 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4590 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4591 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4592 b72706c3 2022-06-01 stsp if (err)
4593 b72706c3 2022-06-01 stsp goto done;
4594 6d17833f 2019-11-08 stsp }
4595 5dc9f4bc 2018-08-04 stsp
4596 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4597 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4598 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4599 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4600 f5215bb9 2019-02-22 stsp
4601 5465d566 2020-02-01 tracey err = create_diff(s);
4602 48ae06ee 2018-10-18 stsp
4603 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4604 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4605 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4606 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4607 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4608 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4609 b72706c3 2022-06-01 stsp done:
4610 b72706c3 2022-06-01 stsp if (err)
4611 b72706c3 2022-06-01 stsp close_diff_view(view);
4612 e5a0f69f 2018-08-18 stsp return err;
4613 5dc9f4bc 2018-08-04 stsp }
4614 5dc9f4bc 2018-08-04 stsp
4615 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4616 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4617 5dc9f4bc 2018-08-04 stsp {
4618 a3404814 2018-09-02 stsp const struct got_error *err;
4619 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4620 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4621 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4622 a3404814 2018-09-02 stsp
4623 a3404814 2018-09-02 stsp if (s->id1) {
4624 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4625 a3404814 2018-09-02 stsp if (err)
4626 a3404814 2018-09-02 stsp return err;
4627 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4628 3dbaef42 2020-11-24 stsp } else
4629 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4630 3dbaef42 2020-11-24 stsp
4631 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4632 a3404814 2018-09-02 stsp if (err)
4633 a3404814 2018-09-02 stsp return err;
4634 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4635 26ed57b2 2018-05-19 stsp
4636 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4637 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4638 a3404814 2018-09-02 stsp free(id_str1);
4639 a3404814 2018-09-02 stsp free(id_str2);
4640 a3404814 2018-09-02 stsp return err;
4641 a3404814 2018-09-02 stsp }
4642 a3404814 2018-09-02 stsp free(id_str1);
4643 a3404814 2018-09-02 stsp free(id_str2);
4644 a3404814 2018-09-02 stsp
4645 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4646 267bb3b8 2021-08-01 stsp free(header);
4647 267bb3b8 2021-08-01 stsp return err;
4648 15a087fe 2019-02-21 stsp }
4649 15a087fe 2019-02-21 stsp
4650 15a087fe 2019-02-21 stsp static const struct got_error *
4651 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4652 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4653 15a087fe 2019-02-21 stsp {
4654 d7a04538 2019-02-21 stsp const struct got_error *err;
4655 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4656 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4657 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4658 15a087fe 2019-02-21 stsp
4659 15a087fe 2019-02-21 stsp free(s->id2);
4660 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4661 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4662 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4663 15a087fe 2019-02-21 stsp
4664 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4665 d7a04538 2019-02-21 stsp if (err)
4666 d7a04538 2019-02-21 stsp return err;
4667 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4668 15a087fe 2019-02-21 stsp free(s->id1);
4669 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4670 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4671 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4672 15a087fe 2019-02-21 stsp return NULL;
4673 0cf4efb1 2018-09-29 stsp }
4674 0cf4efb1 2018-09-29 stsp
4675 0cf4efb1 2018-09-29 stsp static const struct got_error *
4676 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4677 917d79a7 2022-07-01 stsp {
4678 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4679 917d79a7 2022-07-01 stsp
4680 917d79a7 2022-07-01 stsp view->count = 0;
4681 917d79a7 2022-07-01 stsp wclear(view->window);
4682 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4683 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4684 917d79a7 2022-07-01 stsp s->matched_line = 0;
4685 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4686 917d79a7 2022-07-01 stsp return create_diff(s);
4687 917d79a7 2022-07-01 stsp }
4688 917d79a7 2022-07-01 stsp
4689 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4690 c0f61fa4 2022-07-11 mark int, int, int);
4691 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4692 c0f61fa4 2022-07-11 mark int, int);
4693 c0f61fa4 2022-07-11 mark
4694 917d79a7 2022-07-01 stsp static const struct got_error *
4695 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4696 e5a0f69f 2018-08-18 stsp {
4697 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4698 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4699 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4700 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4701 826082fe 2020-12-10 stsp char *line = NULL;
4702 826082fe 2020-12-10 stsp size_t linesize = 0;
4703 826082fe 2020-12-10 stsp ssize_t linelen;
4704 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4705 e5a0f69f 2018-08-18 stsp
4706 e5a0f69f 2018-08-18 stsp switch (ch) {
4707 145b6838 2022-06-16 stsp case '0':
4708 145b6838 2022-06-16 stsp view->x = 0;
4709 145b6838 2022-06-16 stsp break;
4710 145b6838 2022-06-16 stsp case '$':
4711 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4712 640cd7ff 2022-06-22 mark view->count = 0;
4713 145b6838 2022-06-16 stsp break;
4714 145b6838 2022-06-16 stsp case KEY_RIGHT:
4715 145b6838 2022-06-16 stsp case 'l':
4716 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4717 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4718 640cd7ff 2022-06-22 mark else
4719 640cd7ff 2022-06-22 mark view->count = 0;
4720 145b6838 2022-06-16 stsp break;
4721 145b6838 2022-06-16 stsp case KEY_LEFT:
4722 145b6838 2022-06-16 stsp case 'h':
4723 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4724 640cd7ff 2022-06-22 mark if (view->x <= 0)
4725 640cd7ff 2022-06-22 mark view->count = 0;
4726 145b6838 2022-06-16 stsp break;
4727 64453f7e 2020-11-21 stsp case 'a':
4728 3dbaef42 2020-11-24 stsp case 'w':
4729 3dbaef42 2020-11-24 stsp if (ch == 'a')
4730 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4731 3dbaef42 2020-11-24 stsp if (ch == 'w')
4732 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4733 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4734 912a3f79 2021-08-30 j break;
4735 912a3f79 2021-08-30 j case 'g':
4736 912a3f79 2021-08-30 j case KEY_HOME:
4737 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4738 640cd7ff 2022-06-22 mark view->count = 0;
4739 64453f7e 2020-11-21 stsp break;
4740 912a3f79 2021-08-30 j case 'G':
4741 912a3f79 2021-08-30 j case KEY_END:
4742 640cd7ff 2022-06-22 mark view->count = 0;
4743 912a3f79 2021-08-30 j if (s->eof)
4744 912a3f79 2021-08-30 j break;
4745 912a3f79 2021-08-30 j
4746 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4747 912a3f79 2021-08-30 j s->eof = 1;
4748 912a3f79 2021-08-30 j break;
4749 1e37a5c2 2019-05-12 jcs case 'k':
4750 1e37a5c2 2019-05-12 jcs case KEY_UP:
4751 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4752 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4753 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4754 640cd7ff 2022-06-22 mark else
4755 640cd7ff 2022-06-22 mark view->count = 0;
4756 1e37a5c2 2019-05-12 jcs break;
4757 83cc4199 2022-06-13 stsp case CTRL('u'):
4758 33c3719a 2022-06-15 stsp case 'u':
4759 83cc4199 2022-06-13 stsp nscroll /= 2;
4760 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4761 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4762 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4763 61417565 2022-06-20 mark case 'b':
4764 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4765 640cd7ff 2022-06-22 mark view->count = 0;
4766 26ed57b2 2018-05-19 stsp break;
4767 640cd7ff 2022-06-22 mark }
4768 1e37a5c2 2019-05-12 jcs i = 0;
4769 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4771 1e37a5c2 2019-05-12 jcs break;
4772 1e37a5c2 2019-05-12 jcs case 'j':
4773 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4774 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4775 1e37a5c2 2019-05-12 jcs if (!s->eof)
4776 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4777 640cd7ff 2022-06-22 mark else
4778 640cd7ff 2022-06-22 mark view->count = 0;
4779 1e37a5c2 2019-05-12 jcs break;
4780 83cc4199 2022-06-13 stsp case CTRL('d'):
4781 33c3719a 2022-06-15 stsp case 'd':
4782 83cc4199 2022-06-13 stsp nscroll /= 2;
4783 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4784 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4785 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4786 61417565 2022-06-20 mark case 'f':
4787 1e37a5c2 2019-05-12 jcs case ' ':
4788 640cd7ff 2022-06-22 mark if (s->eof) {
4789 640cd7ff 2022-06-22 mark view->count = 0;
4790 1e37a5c2 2019-05-12 jcs break;
4791 640cd7ff 2022-06-22 mark }
4792 1e37a5c2 2019-05-12 jcs i = 0;
4793 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4794 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4795 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4796 826082fe 2020-12-10 stsp if (linelen == -1) {
4797 826082fe 2020-12-10 stsp if (feof(s->f)) {
4798 826082fe 2020-12-10 stsp s->eof = 1;
4799 826082fe 2020-12-10 stsp } else
4800 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4801 34bc9ec9 2019-02-22 stsp break;
4802 826082fe 2020-12-10 stsp }
4803 1e37a5c2 2019-05-12 jcs }
4804 826082fe 2020-12-10 stsp free(line);
4805 1e37a5c2 2019-05-12 jcs break;
4806 1e37a5c2 2019-05-12 jcs case '[':
4807 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4808 1e37a5c2 2019-05-12 jcs s->diff_context--;
4809 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4810 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4811 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4812 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4813 27829c9e 2020-11-21 stsp s->nlines) {
4814 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4815 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4816 27829c9e 2020-11-21 stsp }
4817 640cd7ff 2022-06-22 mark } else
4818 640cd7ff 2022-06-22 mark view->count = 0;
4819 1e37a5c2 2019-05-12 jcs break;
4820 1e37a5c2 2019-05-12 jcs case ']':
4821 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4822 1e37a5c2 2019-05-12 jcs s->diff_context++;
4823 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4824 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4825 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4826 640cd7ff 2022-06-22 mark } else
4827 640cd7ff 2022-06-22 mark view->count = 0;
4828 1e37a5c2 2019-05-12 jcs break;
4829 1e37a5c2 2019-05-12 jcs case '<':
4830 1e37a5c2 2019-05-12 jcs case ',':
4831 2b3e6702 2022-07-20 mark case 'K':
4832 c0f61fa4 2022-07-11 mark up = 1;
4833 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4834 c0f61fa4 2022-07-11 mark case '>':
4835 c0f61fa4 2022-07-11 mark case '.':
4836 2b3e6702 2022-07-20 mark case 'J':
4837 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4838 640cd7ff 2022-06-22 mark view->count = 0;
4839 48ae06ee 2018-10-18 stsp break;
4840 640cd7ff 2022-06-22 mark }
4841 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4842 6524637e 2019-02-21 stsp
4843 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4844 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4845 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4846 15a087fe 2019-02-21 stsp
4847 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4848 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4849 c0f61fa4 2022-07-11 mark if (err)
4850 c0f61fa4 2022-07-11 mark break;
4851 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4852 15a087fe 2019-02-21 stsp
4853 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4854 c0f61fa4 2022-07-11 mark break;
4855 15a087fe 2019-02-21 stsp
4856 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4857 c0f61fa4 2022-07-11 mark if (err)
4858 c0f61fa4 2022-07-11 mark break;
4859 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4860 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4861 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4862 5e224a3e 2019-02-22 stsp
4863 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4864 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4865 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4866 c0f61fa4 2022-07-11 mark
4867 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4868 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4869 c0f61fa4 2022-07-11 mark if (err)
4870 c0f61fa4 2022-07-11 mark break;
4871 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4872 5a8b5076 2020-12-05 stsp
4873 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4874 c0f61fa4 2022-07-11 mark break;
4875 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4876 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4877 c0f61fa4 2022-07-11 mark bs->selected_line);
4878 c0f61fa4 2022-07-11 mark if (id == NULL)
4879 c0f61fa4 2022-07-11 mark break;
4880 15a087fe 2019-02-21 stsp
4881 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4882 c0f61fa4 2022-07-11 mark break;
4883 15a087fe 2019-02-21 stsp
4884 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4885 c0f61fa4 2022-07-11 mark if (err)
4886 c0f61fa4 2022-07-11 mark break;
4887 c0f61fa4 2022-07-11 mark }
4888 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4889 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4890 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4891 145b6838 2022-06-16 stsp view->x = 0;
4892 1e37a5c2 2019-05-12 jcs
4893 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4894 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4895 1e37a5c2 2019-05-12 jcs break;
4896 1e37a5c2 2019-05-12 jcs default:
4897 640cd7ff 2022-06-22 mark view->count = 0;
4898 1e37a5c2 2019-05-12 jcs break;
4899 26ed57b2 2018-05-19 stsp }
4900 e5a0f69f 2018-08-18 stsp
4901 bcbd79e2 2018-08-19 stsp return err;
4902 26ed57b2 2018-05-19 stsp }
4903 26ed57b2 2018-05-19 stsp
4904 4ed7e80c 2018-05-20 stsp static const struct got_error *
4905 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4906 9f7d7167 2018-04-29 stsp {
4907 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4908 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4909 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4910 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4911 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4912 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4913 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4914 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4915 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4916 3dbaef42 2020-11-24 stsp const char *errstr;
4917 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4918 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4919 70ac5f84 2019-03-28 stsp
4920 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4921 26ed57b2 2018-05-19 stsp switch (ch) {
4922 64453f7e 2020-11-21 stsp case 'a':
4923 64453f7e 2020-11-21 stsp force_text_diff = 1;
4924 3dbaef42 2020-11-24 stsp break;
4925 3dbaef42 2020-11-24 stsp case 'C':
4926 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4927 3dbaef42 2020-11-24 stsp &errstr);
4928 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4929 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4930 5a20d08d 2022-02-09 op errstr, errstr);
4931 64453f7e 2020-11-21 stsp break;
4932 09b5bff8 2020-02-23 naddy case 'r':
4933 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4934 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4935 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4936 09b5bff8 2020-02-23 naddy optarg);
4937 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4938 09b5bff8 2020-02-23 naddy break;
4939 3dbaef42 2020-11-24 stsp case 'w':
4940 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4941 3dbaef42 2020-11-24 stsp break;
4942 26ed57b2 2018-05-19 stsp default:
4943 17020d27 2019-03-07 stsp usage_diff();
4944 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4945 26ed57b2 2018-05-19 stsp }
4946 26ed57b2 2018-05-19 stsp }
4947 26ed57b2 2018-05-19 stsp
4948 26ed57b2 2018-05-19 stsp argc -= optind;
4949 26ed57b2 2018-05-19 stsp argv += optind;
4950 26ed57b2 2018-05-19 stsp
4951 26ed57b2 2018-05-19 stsp if (argc == 0) {
4952 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4953 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4954 15a94983 2018-12-23 stsp id_str1 = argv[0];
4955 15a94983 2018-12-23 stsp id_str2 = argv[1];
4956 26ed57b2 2018-05-19 stsp } else
4957 26ed57b2 2018-05-19 stsp usage_diff();
4958 eb6600df 2019-01-04 stsp
4959 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4960 0ae84acc 2022-06-15 tracey if (error)
4961 0ae84acc 2022-06-15 tracey goto done;
4962 0ae84acc 2022-06-15 tracey
4963 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4964 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4965 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4966 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4967 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4968 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4969 c156c7a4 2020-12-18 stsp goto done;
4970 a273ac94 2020-02-23 naddy if (worktree)
4971 a273ac94 2020-02-23 naddy repo_path =
4972 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4973 a273ac94 2020-02-23 naddy else
4974 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4975 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4976 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4977 c156c7a4 2020-12-18 stsp goto done;
4978 c156c7a4 2020-12-18 stsp }
4979 a273ac94 2020-02-23 naddy }
4980 a273ac94 2020-02-23 naddy
4981 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4982 eb6600df 2019-01-04 stsp if (error)
4983 eb6600df 2019-01-04 stsp goto done;
4984 26ed57b2 2018-05-19 stsp
4985 a273ac94 2020-02-23 naddy init_curses();
4986 a273ac94 2020-02-23 naddy
4987 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4988 51a10b52 2020-12-26 stsp if (error)
4989 51a10b52 2020-12-26 stsp goto done;
4990 51a10b52 2020-12-26 stsp
4991 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4992 26ed57b2 2018-05-19 stsp if (error)
4993 26ed57b2 2018-05-19 stsp goto done;
4994 26ed57b2 2018-05-19 stsp
4995 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4996 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4997 26ed57b2 2018-05-19 stsp if (error)
4998 26ed57b2 2018-05-19 stsp goto done;
4999 26ed57b2 2018-05-19 stsp
5000 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5001 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5002 26ed57b2 2018-05-19 stsp if (error)
5003 26ed57b2 2018-05-19 stsp goto done;
5004 26ed57b2 2018-05-19 stsp
5005 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5006 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5007 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5008 ea5e7bb5 2018-08-01 stsp goto done;
5009 ea5e7bb5 2018-08-01 stsp }
5010 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5011 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5012 5dc9f4bc 2018-08-04 stsp if (error)
5013 5dc9f4bc 2018-08-04 stsp goto done;
5014 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5015 26ed57b2 2018-05-19 stsp done:
5016 3dbaef42 2020-11-24 stsp free(label1);
5017 3dbaef42 2020-11-24 stsp free(label2);
5018 c02c541e 2019-03-29 stsp free(repo_path);
5019 a273ac94 2020-02-23 naddy free(cwd);
5020 1d0f4054 2021-06-17 stsp if (repo) {
5021 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5022 1d0f4054 2021-06-17 stsp if (error == NULL)
5023 1d0f4054 2021-06-17 stsp error = close_err;
5024 1d0f4054 2021-06-17 stsp }
5025 a273ac94 2020-02-23 naddy if (worktree)
5026 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5027 0ae84acc 2022-06-15 tracey if (pack_fds) {
5028 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5029 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5030 0ae84acc 2022-06-15 tracey if (error == NULL)
5031 0ae84acc 2022-06-15 tracey error = pack_err;
5032 0ae84acc 2022-06-15 tracey }
5033 51a10b52 2020-12-26 stsp tog_free_refs();
5034 26ed57b2 2018-05-19 stsp return error;
5035 9f7d7167 2018-04-29 stsp }
5036 9f7d7167 2018-04-29 stsp
5037 4ed7e80c 2018-05-20 stsp __dead static void
5038 9f7d7167 2018-04-29 stsp usage_blame(void)
5039 9f7d7167 2018-04-29 stsp {
5040 80ddbec8 2018-04-29 stsp endwin();
5041 87411fa9 2022-06-16 stsp fprintf(stderr,
5042 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5043 9f7d7167 2018-04-29 stsp getprogname());
5044 9f7d7167 2018-04-29 stsp exit(1);
5045 9f7d7167 2018-04-29 stsp }
5046 84451b3e 2018-07-10 stsp
5047 84451b3e 2018-07-10 stsp struct tog_blame_line {
5048 84451b3e 2018-07-10 stsp int annotated;
5049 84451b3e 2018-07-10 stsp struct got_object_id *id;
5050 84451b3e 2018-07-10 stsp };
5051 9f7d7167 2018-04-29 stsp
5052 4ed7e80c 2018-05-20 stsp static const struct got_error *
5053 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5054 84451b3e 2018-07-10 stsp {
5055 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5056 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5057 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5058 84451b3e 2018-07-10 stsp const struct got_error *err;
5059 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5060 826082fe 2020-12-10 stsp char *line = NULL;
5061 826082fe 2020-12-10 stsp size_t linesize = 0;
5062 826082fe 2020-12-10 stsp ssize_t linelen;
5063 84451b3e 2018-07-10 stsp wchar_t *wline;
5064 27a741e5 2019-09-11 stsp int width;
5065 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5066 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5067 ab089a2a 2018-07-12 stsp char *id_str;
5068 11b20872 2019-11-08 stsp struct tog_color *tc;
5069 ab089a2a 2018-07-12 stsp
5070 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5071 ab089a2a 2018-07-12 stsp if (err)
5072 ab089a2a 2018-07-12 stsp return err;
5073 84451b3e 2018-07-10 stsp
5074 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5075 f7d12f7e 2018-08-01 stsp werase(view->window);
5076 84451b3e 2018-07-10 stsp
5077 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5078 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5079 ab089a2a 2018-07-12 stsp free(id_str);
5080 ab089a2a 2018-07-12 stsp return err;
5081 ab089a2a 2018-07-12 stsp }
5082 ab089a2a 2018-07-12 stsp
5083 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5084 ab089a2a 2018-07-12 stsp free(line);
5085 2550e4c3 2018-07-13 stsp line = NULL;
5086 1cae65b4 2019-09-22 stsp if (err)
5087 1cae65b4 2019-09-22 stsp return err;
5088 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5089 a3404814 2018-09-02 stsp wstandout(view->window);
5090 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5091 11b20872 2019-11-08 stsp if (tc)
5092 11b20872 2019-11-08 stsp wattr_on(view->window,
5093 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5094 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5095 11b20872 2019-11-08 stsp if (tc)
5096 11b20872 2019-11-08 stsp wattr_off(view->window,
5097 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5098 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5099 a3404814 2018-09-02 stsp wstandend(view->window);
5100 2550e4c3 2018-07-13 stsp free(wline);
5101 2550e4c3 2018-07-13 stsp wline = NULL;
5102 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5103 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5104 ab089a2a 2018-07-12 stsp
5105 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5106 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5107 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5108 ab089a2a 2018-07-12 stsp free(id_str);
5109 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5110 ab089a2a 2018-07-12 stsp }
5111 ab089a2a 2018-07-12 stsp free(id_str);
5112 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5113 3f60a8ef 2018-07-10 stsp free(line);
5114 2550e4c3 2018-07-13 stsp line = NULL;
5115 3f60a8ef 2018-07-10 stsp if (err)
5116 3f60a8ef 2018-07-10 stsp return err;
5117 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5118 2550e4c3 2018-07-13 stsp free(wline);
5119 2550e4c3 2018-07-13 stsp wline = NULL;
5120 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5121 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5122 3f60a8ef 2018-07-10 stsp
5123 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5124 145b6838 2022-06-16 stsp view->maxx = 0;
5125 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5126 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5127 826082fe 2020-12-10 stsp if (linelen == -1) {
5128 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5129 826082fe 2020-12-10 stsp s->eof = 1;
5130 826082fe 2020-12-10 stsp break;
5131 826082fe 2020-12-10 stsp }
5132 84451b3e 2018-07-10 stsp free(line);
5133 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5134 84451b3e 2018-07-10 stsp }
5135 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5136 826082fe 2020-12-10 stsp continue;
5137 1853e0f4 2022-06-16 stsp
5138 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5139 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5140 1853e0f4 2022-06-16 stsp if (err) {
5141 1853e0f4 2022-06-16 stsp free(line);
5142 1853e0f4 2022-06-16 stsp return err;
5143 1853e0f4 2022-06-16 stsp }
5144 1853e0f4 2022-06-16 stsp free(wline);
5145 1853e0f4 2022-06-16 stsp wline = NULL;
5146 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5147 84451b3e 2018-07-10 stsp
5148 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5149 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5150 b700b5d6 2018-07-10 stsp
5151 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5152 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5153 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5154 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5155 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5156 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5157 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5158 8d0fe45a 2019-08-12 stsp char *id_str;
5159 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5160 87411fa9 2022-06-16 stsp blame_line->id);
5161 8d0fe45a 2019-08-12 stsp if (err) {
5162 8d0fe45a 2019-08-12 stsp free(line);
5163 8d0fe45a 2019-08-12 stsp return err;
5164 8d0fe45a 2019-08-12 stsp }
5165 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5166 11b20872 2019-11-08 stsp if (tc)
5167 11b20872 2019-11-08 stsp wattr_on(view->window,
5168 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5169 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5170 11b20872 2019-11-08 stsp if (tc)
5171 11b20872 2019-11-08 stsp wattr_off(view->window,
5172 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5173 8d0fe45a 2019-08-12 stsp free(id_str);
5174 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5175 8d0fe45a 2019-08-12 stsp } else {
5176 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5177 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5178 84451b3e 2018-07-10 stsp }
5179 ee41ec32 2018-07-10 stsp } else {
5180 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5181 ee41ec32 2018-07-10 stsp prev_id = NULL;
5182 ee41ec32 2018-07-10 stsp }
5183 84451b3e 2018-07-10 stsp
5184 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5185 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5186 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5187 27a741e5 2019-09-11 stsp
5188 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5189 41605754 2020-11-12 stsp width = 9;
5190 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5191 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5192 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5193 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5194 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5195 41605754 2020-11-12 stsp if (err) {
5196 41605754 2020-11-12 stsp free(line);
5197 41605754 2020-11-12 stsp return err;
5198 41605754 2020-11-12 stsp }
5199 41605754 2020-11-12 stsp width += 9;
5200 41605754 2020-11-12 stsp } else {
5201 4cb9d869 2022-06-16 stsp int skip;
5202 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5203 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5204 4cb9d869 2022-06-16 stsp if (err) {
5205 4cb9d869 2022-06-16 stsp free(line);
5206 4cb9d869 2022-06-16 stsp return err;
5207 145b6838 2022-06-16 stsp }
5208 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5209 a75b3e2d 2022-06-16 stsp width += 9;
5210 41605754 2020-11-12 stsp free(wline);
5211 41605754 2020-11-12 stsp wline = NULL;
5212 41605754 2020-11-12 stsp }
5213 41605754 2020-11-12 stsp
5214 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5215 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5216 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5217 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5218 84451b3e 2018-07-10 stsp }
5219 826082fe 2020-12-10 stsp free(line);
5220 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5221 84451b3e 2018-07-10 stsp
5222 9b058f45 2022-06-30 mark view_border(view);
5223 84451b3e 2018-07-10 stsp
5224 84451b3e 2018-07-10 stsp return NULL;
5225 84451b3e 2018-07-10 stsp }
5226 84451b3e 2018-07-10 stsp
5227 84451b3e 2018-07-10 stsp static const struct got_error *
5228 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5229 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5230 84451b3e 2018-07-10 stsp {
5231 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5232 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5233 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5234 1a76625f 2018-10-22 stsp int errcode;
5235 84451b3e 2018-07-10 stsp
5236 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5237 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5238 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5239 84451b3e 2018-07-10 stsp
5240 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5241 1a76625f 2018-10-22 stsp if (errcode)
5242 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5243 84451b3e 2018-07-10 stsp
5244 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5245 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5246 d68a0a7d 2018-07-10 stsp goto done;
5247 d68a0a7d 2018-07-10 stsp }
5248 d68a0a7d 2018-07-10 stsp
5249 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5250 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5251 d68a0a7d 2018-07-10 stsp
5252 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5253 d68a0a7d 2018-07-10 stsp if (line->annotated)
5254 d68a0a7d 2018-07-10 stsp goto done;
5255 d68a0a7d 2018-07-10 stsp
5256 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5257 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5258 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5259 84451b3e 2018-07-10 stsp goto done;
5260 84451b3e 2018-07-10 stsp }
5261 84451b3e 2018-07-10 stsp line->annotated = 1;
5262 84451b3e 2018-07-10 stsp done:
5263 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5264 1a76625f 2018-10-22 stsp if (errcode)
5265 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5266 84451b3e 2018-07-10 stsp return err;
5267 84451b3e 2018-07-10 stsp }
5268 84451b3e 2018-07-10 stsp
5269 84451b3e 2018-07-10 stsp static void *
5270 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5271 84451b3e 2018-07-10 stsp {
5272 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5273 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5274 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5275 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5276 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5277 1b484788 2022-06-28 tracey
5278 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5279 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5280 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5281 e6e73e55 2022-06-30 tracey
5282 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5283 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5284 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5285 e6e73e55 2022-06-30 tracey goto done;
5286 e6e73e55 2022-06-30 tracey }
5287 18430de3 2018-07-10 stsp
5288 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5289 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5290 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5291 e6e73e55 2022-06-30 tracey goto done;
5292 e6e73e55 2022-06-30 tracey }
5293 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5294 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5295 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5296 e6e73e55 2022-06-30 tracey goto done;
5297 e6e73e55 2022-06-30 tracey }
5298 e6e73e55 2022-06-30 tracey
5299 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5300 61266923 2020-01-14 stsp if (err)
5301 e6e73e55 2022-06-30 tracey goto done;
5302 61266923 2020-01-14 stsp
5303 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5304 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5305 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5306 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5307 fc06ba56 2019-08-22 stsp err = NULL;
5308 18430de3 2018-07-10 stsp
5309 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5310 e6e73e55 2022-06-30 tracey if (errcode) {
5311 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5312 e6e73e55 2022-06-30 tracey goto done;
5313 e6e73e55 2022-06-30 tracey }
5314 18430de3 2018-07-10 stsp
5315 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5316 1d0f4054 2021-06-17 stsp if (err == NULL)
5317 1d0f4054 2021-06-17 stsp err = close_err;
5318 c9beca56 2018-07-22 stsp ta->repo = NULL;
5319 c9beca56 2018-07-22 stsp *ta->complete = 1;
5320 18430de3 2018-07-10 stsp
5321 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5322 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5323 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5324 18430de3 2018-07-10 stsp
5325 e6e73e55 2022-06-30 tracey done:
5326 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5327 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5328 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5329 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5330 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5331 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5332 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5333 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5334 1b484788 2022-06-28 tracey
5335 18430de3 2018-07-10 stsp return (void *)err;
5336 84451b3e 2018-07-10 stsp }
5337 84451b3e 2018-07-10 stsp
5338 245d91c1 2018-07-12 stsp static struct got_object_id *
5339 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5340 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5341 245d91c1 2018-07-12 stsp {
5342 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5343 8d0fe45a 2019-08-12 stsp
5344 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5345 8d0fe45a 2019-08-12 stsp return NULL;
5346 b880a918 2018-07-10 stsp
5347 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5348 c0f61fa4 2022-07-11 mark if (!line->annotated)
5349 c0f61fa4 2022-07-11 mark return NULL;
5350 c0f61fa4 2022-07-11 mark
5351 c0f61fa4 2022-07-11 mark return line->id;
5352 c0f61fa4 2022-07-11 mark }
5353 c0f61fa4 2022-07-11 mark
5354 c0f61fa4 2022-07-11 mark static struct got_object_id *
5355 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5356 c0f61fa4 2022-07-11 mark int lineno)
5357 c0f61fa4 2022-07-11 mark {
5358 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5359 c0f61fa4 2022-07-11 mark
5360 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5361 c0f61fa4 2022-07-11 mark return NULL;
5362 c0f61fa4 2022-07-11 mark
5363 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5364 245d91c1 2018-07-12 stsp if (!line->annotated)
5365 245d91c1 2018-07-12 stsp return NULL;
5366 245d91c1 2018-07-12 stsp
5367 245d91c1 2018-07-12 stsp return line->id;
5368 b880a918 2018-07-10 stsp }
5369 245d91c1 2018-07-12 stsp
5370 b880a918 2018-07-10 stsp static const struct got_error *
5371 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5372 a70480e0 2018-06-23 stsp {
5373 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5374 245d91c1 2018-07-12 stsp int i;
5375 245d91c1 2018-07-12 stsp
5376 245d91c1 2018-07-12 stsp if (blame->thread) {
5377 1a76625f 2018-10-22 stsp int errcode;
5378 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5379 1a76625f 2018-10-22 stsp if (errcode)
5380 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5381 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5382 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5383 1a76625f 2018-10-22 stsp if (errcode)
5384 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5385 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5386 1a76625f 2018-10-22 stsp if (errcode)
5387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5388 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5389 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5390 245d91c1 2018-07-12 stsp err = NULL;
5391 245d91c1 2018-07-12 stsp blame->thread = NULL;
5392 245d91c1 2018-07-12 stsp }
5393 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5394 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5395 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5396 1d0f4054 2021-06-17 stsp if (err == NULL)
5397 1d0f4054 2021-06-17 stsp err = close_err;
5398 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5399 245d91c1 2018-07-12 stsp }
5400 245d91c1 2018-07-12 stsp if (blame->f) {
5401 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5402 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5403 245d91c1 2018-07-12 stsp blame->f = NULL;
5404 245d91c1 2018-07-12 stsp }
5405 57670559 2018-12-23 stsp if (blame->lines) {
5406 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5407 57670559 2018-12-23 stsp free(blame->lines[i].id);
5408 57670559 2018-12-23 stsp free(blame->lines);
5409 57670559 2018-12-23 stsp blame->lines = NULL;
5410 57670559 2018-12-23 stsp }
5411 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5412 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5413 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5414 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5415 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5416 0ae84acc 2022-06-15 tracey if (err == NULL)
5417 0ae84acc 2022-06-15 tracey err = pack_err;
5418 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5419 0ae84acc 2022-06-15 tracey }
5420 245d91c1 2018-07-12 stsp return err;
5421 245d91c1 2018-07-12 stsp }
5422 245d91c1 2018-07-12 stsp
5423 245d91c1 2018-07-12 stsp static const struct got_error *
5424 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5425 fc06ba56 2019-08-22 stsp {
5426 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5427 fc06ba56 2019-08-22 stsp int *done = arg;
5428 fc06ba56 2019-08-22 stsp int errcode;
5429 fc06ba56 2019-08-22 stsp
5430 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5431 fc06ba56 2019-08-22 stsp if (errcode)
5432 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5433 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5434 fc06ba56 2019-08-22 stsp
5435 fc06ba56 2019-08-22 stsp if (*done)
5436 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5437 fc06ba56 2019-08-22 stsp
5438 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5439 fc06ba56 2019-08-22 stsp if (errcode)
5440 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5441 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5442 fc06ba56 2019-08-22 stsp
5443 fc06ba56 2019-08-22 stsp return err;
5444 fc06ba56 2019-08-22 stsp }
5445 fc06ba56 2019-08-22 stsp
5446 fc06ba56 2019-08-22 stsp static const struct got_error *
5447 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5448 245d91c1 2018-07-12 stsp {
5449 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5450 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5451 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5452 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5453 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5454 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5455 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5456 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5457 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5458 a70480e0 2018-06-23 stsp
5459 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5460 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5461 27d434c2 2018-09-15 stsp if (err)
5462 15a94983 2018-12-23 stsp return err;
5463 eb81bc23 2022-06-28 tracey
5464 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5465 eb81bc23 2022-06-28 tracey if (fd == -1) {
5466 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5467 eb81bc23 2022-06-28 tracey goto done;
5468 eb81bc23 2022-06-28 tracey }
5469 a44927cc 2022-04-07 stsp
5470 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5471 a44927cc 2022-04-07 stsp if (err)
5472 a44927cc 2022-04-07 stsp goto done;
5473 27d434c2 2018-09-15 stsp
5474 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5475 84451b3e 2018-07-10 stsp if (err)
5476 84451b3e 2018-07-10 stsp goto done;
5477 27d434c2 2018-09-15 stsp
5478 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5479 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5480 84451b3e 2018-07-10 stsp goto done;
5481 84451b3e 2018-07-10 stsp }
5482 a70480e0 2018-06-23 stsp
5483 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5484 a70480e0 2018-06-23 stsp if (err)
5485 a70480e0 2018-06-23 stsp goto done;
5486 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5487 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5488 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5489 84451b3e 2018-07-10 stsp goto done;
5490 84451b3e 2018-07-10 stsp }
5491 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5492 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5493 1fddf795 2021-01-20 stsp if (err)
5494 1fddf795 2021-01-20 stsp goto done;
5495 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5496 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5497 84451b3e 2018-07-10 stsp goto done;
5498 1fddf795 2021-01-20 stsp }
5499 b02560ec 2019-08-19 stsp
5500 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5501 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5502 b02560ec 2019-08-19 stsp blame->nlines--;
5503 a70480e0 2018-06-23 stsp
5504 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5505 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5506 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5507 84451b3e 2018-07-10 stsp goto done;
5508 84451b3e 2018-07-10 stsp }
5509 a70480e0 2018-06-23 stsp
5510 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5511 bd24772e 2018-07-11 stsp if (err)
5512 bd24772e 2018-07-11 stsp goto done;
5513 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5514 0ae84acc 2022-06-15 tracey pack_fds);
5515 0ae84acc 2022-06-15 tracey if (err)
5516 0ae84acc 2022-06-15 tracey goto done;
5517 bd24772e 2018-07-11 stsp
5518 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5519 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5520 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5521 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5522 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5523 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5524 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5525 245d91c1 2018-07-12 stsp goto done;
5526 245d91c1 2018-07-12 stsp }
5527 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5528 245d91c1 2018-07-12 stsp
5529 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5530 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5531 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5532 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5533 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5534 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5535 a5388363 2020-12-01 naddy s->blame_complete = 0;
5536 f5a09613 2020-12-13 naddy
5537 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5538 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5539 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5540 f5a09613 2020-12-13 naddy s->selected_line = 1;
5541 f5a09613 2020-12-13 naddy }
5542 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5543 245d91c1 2018-07-12 stsp
5544 245d91c1 2018-07-12 stsp done:
5545 a44927cc 2022-04-07 stsp if (commit)
5546 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5547 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5548 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5549 245d91c1 2018-07-12 stsp if (blob)
5550 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5551 27d434c2 2018-09-15 stsp free(obj_id);
5552 245d91c1 2018-07-12 stsp if (err)
5553 245d91c1 2018-07-12 stsp stop_blame(blame);
5554 245d91c1 2018-07-12 stsp return err;
5555 245d91c1 2018-07-12 stsp }
5556 245d91c1 2018-07-12 stsp
5557 245d91c1 2018-07-12 stsp static const struct got_error *
5558 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5559 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5560 245d91c1 2018-07-12 stsp {
5561 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5562 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5563 dbc6a6b6 2018-07-12 stsp
5564 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5565 245d91c1 2018-07-12 stsp
5566 c4843652 2019-08-12 stsp s->path = strdup(path);
5567 c4843652 2019-08-12 stsp if (s->path == NULL)
5568 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5569 c4843652 2019-08-12 stsp
5570 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5571 c4843652 2019-08-12 stsp if (err) {
5572 c4843652 2019-08-12 stsp free(s->path);
5573 7cbe629d 2018-08-04 stsp return err;
5574 c4843652 2019-08-12 stsp }
5575 245d91c1 2018-07-12 stsp
5576 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5577 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5578 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5579 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5580 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5581 fb2756b9 2018-08-04 stsp s->repo = repo;
5582 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5583 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5584 7cbe629d 2018-08-04 stsp
5585 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5586 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5587 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5588 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5589 11b20872 2019-11-08 stsp if (err)
5590 11b20872 2019-11-08 stsp return err;
5591 11b20872 2019-11-08 stsp }
5592 11b20872 2019-11-08 stsp
5593 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5594 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5595 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5596 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5597 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5598 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5599 e5a0f69f 2018-08-18 stsp
5600 a5388363 2020-12-01 naddy return run_blame(view);
5601 7cbe629d 2018-08-04 stsp }
5602 7cbe629d 2018-08-04 stsp
5603 e5a0f69f 2018-08-18 stsp static const struct got_error *
5604 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5605 7cbe629d 2018-08-04 stsp {
5606 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5607 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5608 7cbe629d 2018-08-04 stsp
5609 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5610 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5611 e5a0f69f 2018-08-18 stsp
5612 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5613 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5614 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5615 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5616 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5617 7cbe629d 2018-08-04 stsp }
5618 e5a0f69f 2018-08-18 stsp
5619 e5a0f69f 2018-08-18 stsp free(s->path);
5620 11b20872 2019-11-08 stsp free_colors(&s->colors);
5621 e5a0f69f 2018-08-18 stsp return err;
5622 7cbe629d 2018-08-04 stsp }
5623 7cbe629d 2018-08-04 stsp
5624 7cbe629d 2018-08-04 stsp static const struct got_error *
5625 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5626 6c4c42e0 2019-06-24 stsp {
5627 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5628 6c4c42e0 2019-06-24 stsp
5629 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5630 6c4c42e0 2019-06-24 stsp return NULL;
5631 6c4c42e0 2019-06-24 stsp }
5632 6c4c42e0 2019-06-24 stsp
5633 6c4c42e0 2019-06-24 stsp static const struct got_error *
5634 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5635 6c4c42e0 2019-06-24 stsp {
5636 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5637 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5638 6c4c42e0 2019-06-24 stsp int lineno;
5639 cb713507 2022-06-16 stsp char *line = NULL;
5640 826082fe 2020-12-10 stsp size_t linesize = 0;
5641 826082fe 2020-12-10 stsp ssize_t linelen;
5642 6c4c42e0 2019-06-24 stsp
5643 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5644 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5645 6c4c42e0 2019-06-24 stsp return NULL;
5646 6c4c42e0 2019-06-24 stsp }
5647 6c4c42e0 2019-06-24 stsp
5648 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5649 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5650 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5651 6c4c42e0 2019-06-24 stsp else
5652 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5653 487cd7d2 2021-12-17 stsp } else
5654 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5655 6c4c42e0 2019-06-24 stsp
5656 6c4c42e0 2019-06-24 stsp while (1) {
5657 6c4c42e0 2019-06-24 stsp off_t offset;
5658 6c4c42e0 2019-06-24 stsp
5659 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5660 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5661 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5662 6c4c42e0 2019-06-24 stsp break;
5663 6c4c42e0 2019-06-24 stsp }
5664 2246482e 2019-06-25 stsp
5665 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5666 6c4c42e0 2019-06-24 stsp lineno = 1;
5667 6c4c42e0 2019-06-24 stsp else
5668 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5669 6c4c42e0 2019-06-24 stsp }
5670 6c4c42e0 2019-06-24 stsp
5671 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5672 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5673 6c4c42e0 2019-06-24 stsp free(line);
5674 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5675 6c4c42e0 2019-06-24 stsp }
5676 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5677 cb713507 2022-06-16 stsp if (linelen != -1) {
5678 cb713507 2022-06-16 stsp char *exstr;
5679 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5680 cb713507 2022-06-16 stsp if (err)
5681 cb713507 2022-06-16 stsp break;
5682 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5683 cb713507 2022-06-16 stsp &view->regmatch)) {
5684 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5685 cb713507 2022-06-16 stsp s->matched_line = lineno;
5686 cb713507 2022-06-16 stsp free(exstr);
5687 cb713507 2022-06-16 stsp break;
5688 cb713507 2022-06-16 stsp }
5689 cb713507 2022-06-16 stsp free(exstr);
5690 6c4c42e0 2019-06-24 stsp }
5691 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5692 6c4c42e0 2019-06-24 stsp lineno++;
5693 6c4c42e0 2019-06-24 stsp else
5694 6c4c42e0 2019-06-24 stsp lineno--;
5695 6c4c42e0 2019-06-24 stsp }
5696 826082fe 2020-12-10 stsp free(line);
5697 6c4c42e0 2019-06-24 stsp
5698 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5699 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5700 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5701 6c4c42e0 2019-06-24 stsp }
5702 6c4c42e0 2019-06-24 stsp
5703 145b6838 2022-06-16 stsp return err;
5704 6c4c42e0 2019-06-24 stsp }
5705 6c4c42e0 2019-06-24 stsp
5706 6c4c42e0 2019-06-24 stsp static const struct got_error *
5707 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5708 7cbe629d 2018-08-04 stsp {
5709 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5710 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5711 2b380cc8 2018-10-24 stsp int errcode;
5712 2b380cc8 2018-10-24 stsp
5713 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5714 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5715 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5716 2b380cc8 2018-10-24 stsp if (errcode)
5717 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5718 51fe7530 2019-08-19 stsp
5719 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5720 2b380cc8 2018-10-24 stsp }
5721 e5a0f69f 2018-08-18 stsp
5722 51fe7530 2019-08-19 stsp if (s->blame_complete)
5723 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5724 51fe7530 2019-08-19 stsp
5725 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5726 e5a0f69f 2018-08-18 stsp
5727 9b058f45 2022-06-30 mark view_border(view);
5728 e5a0f69f 2018-08-18 stsp return err;
5729 e5a0f69f 2018-08-18 stsp }
5730 e5a0f69f 2018-08-18 stsp
5731 e5a0f69f 2018-08-18 stsp static const struct got_error *
5732 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5733 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5734 05f04cdf 2022-07-20 mark {
5735 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5736 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5737 05f04cdf 2022-07-20 mark
5738 05f04cdf 2022-07-20 mark *new_view = NULL;
5739 05f04cdf 2022-07-20 mark
5740 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5741 05f04cdf 2022-07-20 mark if (log_view == NULL)
5742 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5743 05f04cdf 2022-07-20 mark
5744 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5745 05f04cdf 2022-07-20 mark if (err)
5746 05f04cdf 2022-07-20 mark view_close(log_view);
5747 05f04cdf 2022-07-20 mark else
5748 05f04cdf 2022-07-20 mark *new_view = log_view;
5749 05f04cdf 2022-07-20 mark
5750 05f04cdf 2022-07-20 mark return err;
5751 05f04cdf 2022-07-20 mark }
5752 05f04cdf 2022-07-20 mark
5753 05f04cdf 2022-07-20 mark static const struct got_error *
5754 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5755 e5a0f69f 2018-08-18 stsp {
5756 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5757 05f04cdf 2022-07-20 mark struct tog_view *diff_view, *log_view;
5758 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5759 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5760 9b058f45 2022-06-30 mark
5761 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5762 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5763 9b058f45 2022-06-30 mark --eos; /* border */
5764 7cbe629d 2018-08-04 stsp
5765 e5a0f69f 2018-08-18 stsp switch (ch) {
5766 145b6838 2022-06-16 stsp case '0':
5767 145b6838 2022-06-16 stsp view->x = 0;
5768 145b6838 2022-06-16 stsp break;
5769 145b6838 2022-06-16 stsp case '$':
5770 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5771 640cd7ff 2022-06-22 mark view->count = 0;
5772 145b6838 2022-06-16 stsp break;
5773 145b6838 2022-06-16 stsp case KEY_RIGHT:
5774 145b6838 2022-06-16 stsp case 'l':
5775 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5776 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5777 640cd7ff 2022-06-22 mark else
5778 640cd7ff 2022-06-22 mark view->count = 0;
5779 145b6838 2022-06-16 stsp break;
5780 145b6838 2022-06-16 stsp case KEY_LEFT:
5781 145b6838 2022-06-16 stsp case 'h':
5782 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5783 640cd7ff 2022-06-22 mark if (view->x <= 0)
5784 640cd7ff 2022-06-22 mark view->count = 0;
5785 145b6838 2022-06-16 stsp break;
5786 1e37a5c2 2019-05-12 jcs case 'q':
5787 1e37a5c2 2019-05-12 jcs s->done = 1;
5788 4deef56f 2021-09-02 naddy break;
5789 4deef56f 2021-09-02 naddy case 'g':
5790 4deef56f 2021-09-02 naddy case KEY_HOME:
5791 4deef56f 2021-09-02 naddy s->selected_line = 1;
5792 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5793 640cd7ff 2022-06-22 mark view->count = 0;
5794 4deef56f 2021-09-02 naddy break;
5795 4deef56f 2021-09-02 naddy case 'G':
5796 4deef56f 2021-09-02 naddy case KEY_END:
5797 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5798 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5799 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5800 4deef56f 2021-09-02 naddy } else {
5801 9b058f45 2022-06-30 mark s->selected_line = eos;
5802 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5803 4deef56f 2021-09-02 naddy }
5804 640cd7ff 2022-06-22 mark view->count = 0;
5805 1e37a5c2 2019-05-12 jcs break;
5806 1e37a5c2 2019-05-12 jcs case 'k':
5807 1e37a5c2 2019-05-12 jcs case KEY_UP:
5808 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5809 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5810 1e37a5c2 2019-05-12 jcs s->selected_line--;
5811 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5812 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5813 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5814 640cd7ff 2022-06-22 mark else
5815 640cd7ff 2022-06-22 mark view->count = 0;
5816 1e37a5c2 2019-05-12 jcs break;
5817 83cc4199 2022-06-13 stsp case CTRL('u'):
5818 33c3719a 2022-06-15 stsp case 'u':
5819 83cc4199 2022-06-13 stsp nscroll /= 2;
5820 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5821 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5822 ea025d1d 2020-02-22 naddy case CTRL('b'):
5823 61417565 2022-06-20 mark case 'b':
5824 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5825 640cd7ff 2022-06-22 mark if (view->count > 1)
5826 640cd7ff 2022-06-22 mark nscroll += nscroll;
5827 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5828 640cd7ff 2022-06-22 mark view->count = 0;
5829 e5a0f69f 2018-08-18 stsp break;
5830 1e37a5c2 2019-05-12 jcs }
5831 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5832 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5833 1e37a5c2 2019-05-12 jcs else
5834 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5835 1e37a5c2 2019-05-12 jcs break;
5836 1e37a5c2 2019-05-12 jcs case 'j':
5837 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5838 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5839 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5840 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5841 1e37a5c2 2019-05-12 jcs s->selected_line++;
5842 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5843 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5844 640cd7ff 2022-06-22 mark else
5845 640cd7ff 2022-06-22 mark view->count = 0;
5846 1e37a5c2 2019-05-12 jcs break;
5847 61417565 2022-06-20 mark case 'c':
5848 1e37a5c2 2019-05-12 jcs case 'p': {
5849 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5850 640cd7ff 2022-06-22 mark
5851 640cd7ff 2022-06-22 mark view->count = 0;
5852 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5853 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5854 1e37a5c2 2019-05-12 jcs if (id == NULL)
5855 e5a0f69f 2018-08-18 stsp break;
5856 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5857 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5858 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5859 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5860 1e37a5c2 2019-05-12 jcs int obj_type;
5861 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5862 1e37a5c2 2019-05-12 jcs s->repo, id);
5863 e5a0f69f 2018-08-18 stsp if (err)
5864 e5a0f69f 2018-08-18 stsp break;
5865 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5866 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5867 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5868 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5869 e5a0f69f 2018-08-18 stsp break;
5870 e5a0f69f 2018-08-18 stsp }
5871 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5872 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5873 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5874 a44927cc 2022-04-07 stsp if (err)
5875 a44927cc 2022-04-07 stsp break;
5876 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5877 a44927cc 2022-04-07 stsp pcommit, s->path);
5878 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5879 e5a0f69f 2018-08-18 stsp if (err) {
5880 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5881 1e37a5c2 2019-05-12 jcs err = NULL;
5882 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5883 e5a0f69f 2018-08-18 stsp break;
5884 e5a0f69f 2018-08-18 stsp }
5885 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5886 1e37a5c2 2019-05-12 jcs blob_id);
5887 1e37a5c2 2019-05-12 jcs free(blob_id);
5888 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5889 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5890 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5891 e5a0f69f 2018-08-18 stsp break;
5892 1e37a5c2 2019-05-12 jcs }
5893 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5894 d7b5a0e8 2022-04-20 stsp &pid->id);
5895 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5896 1e37a5c2 2019-05-12 jcs } else {
5897 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5898 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5899 1e37a5c2 2019-05-12 jcs break;
5900 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5901 1e37a5c2 2019-05-12 jcs id);
5902 1e37a5c2 2019-05-12 jcs }
5903 1e37a5c2 2019-05-12 jcs if (err)
5904 e5a0f69f 2018-08-18 stsp break;
5905 1e37a5c2 2019-05-12 jcs s->done = 1;
5906 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5907 1e37a5c2 2019-05-12 jcs s->done = 0;
5908 1e37a5c2 2019-05-12 jcs if (thread_err)
5909 1e37a5c2 2019-05-12 jcs break;
5910 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5911 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5912 a5388363 2020-12-01 naddy err = run_blame(view);
5913 1e37a5c2 2019-05-12 jcs if (err)
5914 1e37a5c2 2019-05-12 jcs break;
5915 1e37a5c2 2019-05-12 jcs break;
5916 1e37a5c2 2019-05-12 jcs }
5917 61417565 2022-06-20 mark case 'C': {
5918 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5919 640cd7ff 2022-06-22 mark
5920 640cd7ff 2022-06-22 mark view->count = 0;
5921 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5922 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5923 1e37a5c2 2019-05-12 jcs break;
5924 1e37a5c2 2019-05-12 jcs s->done = 1;
5925 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5926 1e37a5c2 2019-05-12 jcs s->done = 0;
5927 1e37a5c2 2019-05-12 jcs if (thread_err)
5928 1e37a5c2 2019-05-12 jcs break;
5929 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5930 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5931 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5932 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5933 a5388363 2020-12-01 naddy err = run_blame(view);
5934 1e37a5c2 2019-05-12 jcs if (err)
5935 1e37a5c2 2019-05-12 jcs break;
5936 1e37a5c2 2019-05-12 jcs break;
5937 1e37a5c2 2019-05-12 jcs }
5938 05f04cdf 2022-07-20 mark case 'L': {
5939 05f04cdf 2022-07-20 mark struct got_object_id *id = NULL;
5940 05f04cdf 2022-07-20 mark
5941 05f04cdf 2022-07-20 mark view->count = 0;
5942 05f04cdf 2022-07-20 mark id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5943 05f04cdf 2022-07-20 mark s->first_displayed_line, s->selected_line);
5944 05f04cdf 2022-07-20 mark if (id == NULL)
5945 05f04cdf 2022-07-20 mark break;
5946 05f04cdf 2022-07-20 mark
5947 05f04cdf 2022-07-20 mark if (view_is_parent_view(view))
5948 05f04cdf 2022-07-20 mark view_get_split(view, &begin_y, &begin_x);
5949 05f04cdf 2022-07-20 mark err = log_annotated_line(&log_view, begin_y, begin_x,
5950 05f04cdf 2022-07-20 mark s->repo, id);
5951 05f04cdf 2022-07-20 mark if (err)
5952 05f04cdf 2022-07-20 mark break;
5953 05f04cdf 2022-07-20 mark if (view_is_parent_view(view) &&
5954 05f04cdf 2022-07-20 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5955 05f04cdf 2022-07-20 mark err = view_init_hsplit(view, begin_y);
5956 05f04cdf 2022-07-20 mark if (err)
5957 05f04cdf 2022-07-20 mark break;
5958 05f04cdf 2022-07-20 mark }
5959 05f04cdf 2022-07-20 mark
5960 05f04cdf 2022-07-20 mark view->focussed = 0;
5961 05f04cdf 2022-07-20 mark log_view->focussed = 1;
5962 05f04cdf 2022-07-20 mark log_view->mode = view->mode;
5963 05f04cdf 2022-07-20 mark log_view->nlines = view->lines - begin_y;
5964 05f04cdf 2022-07-20 mark if (view_is_parent_view(view)) {
5965 05f04cdf 2022-07-20 mark view_transfer_size(log_view, view);
5966 05f04cdf 2022-07-20 mark err = view_close_child(view);
5967 05f04cdf 2022-07-20 mark if (err)
5968 05f04cdf 2022-07-20 mark return err;
5969 05f04cdf 2022-07-20 mark err = view_set_child(view, log_view);
5970 05f04cdf 2022-07-20 mark if (err)
5971 05f04cdf 2022-07-20 mark return err;
5972 05f04cdf 2022-07-20 mark view->focus_child = 1;
5973 05f04cdf 2022-07-20 mark } else
5974 05f04cdf 2022-07-20 mark *new_view = log_view;
5975 05f04cdf 2022-07-20 mark break;
5976 05f04cdf 2022-07-20 mark }
5977 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5978 1e37a5c2 2019-05-12 jcs case '\r': {
5979 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5980 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5981 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5982 640cd7ff 2022-06-22 mark
5983 640cd7ff 2022-06-22 mark view->count = 0;
5984 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5985 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5986 1e37a5c2 2019-05-12 jcs if (id == NULL)
5987 1e37a5c2 2019-05-12 jcs break;
5988 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5989 1e37a5c2 2019-05-12 jcs if (err)
5990 1e37a5c2 2019-05-12 jcs break;
5991 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5992 c0f61fa4 2022-07-11 mark if (*new_view) {
5993 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5994 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5995 c0f61fa4 2022-07-11 mark if (err)
5996 c0f61fa4 2022-07-11 mark break;
5997 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5998 c0f61fa4 2022-07-11 mark } else {
5999 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6000 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6001 9b058f45 2022-06-30 mark
6002 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6003 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6004 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6005 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6006 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6007 c0f61fa4 2022-07-11 mark break;
6008 c0f61fa4 2022-07-11 mark }
6009 15a94983 2018-12-23 stsp }
6010 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6011 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6012 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6013 1e37a5c2 2019-05-12 jcs if (err) {
6014 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6015 1e37a5c2 2019-05-12 jcs break;
6016 1e37a5c2 2019-05-12 jcs }
6017 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6018 c0f61fa4 2022-07-11 mark s->selected_line;
6019 c0f61fa4 2022-07-11 mark if (*new_view)
6020 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6021 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6022 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6023 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6024 9b058f45 2022-06-30 mark if (err)
6025 9b058f45 2022-06-30 mark break;
6026 9b058f45 2022-06-30 mark }
6027 9b058f45 2022-06-30 mark
6028 e78dc838 2020-12-04 stsp view->focussed = 0;
6029 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6030 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6031 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6032 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6033 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6034 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6035 1e37a5c2 2019-05-12 jcs if (err)
6036 34bc9ec9 2019-02-22 stsp break;
6037 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6038 0dbbbe90 2022-06-17 op if (err)
6039 0dbbbe90 2022-06-17 op break;
6040 e78dc838 2020-12-04 stsp view->focus_child = 1;
6041 1e37a5c2 2019-05-12 jcs } else
6042 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6043 1e37a5c2 2019-05-12 jcs if (err)
6044 e5a0f69f 2018-08-18 stsp break;
6045 1e37a5c2 2019-05-12 jcs break;
6046 1e37a5c2 2019-05-12 jcs }
6047 83cc4199 2022-06-13 stsp case CTRL('d'):
6048 33c3719a 2022-06-15 stsp case 'd':
6049 83cc4199 2022-06-13 stsp nscroll /= 2;
6050 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6051 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6052 ea025d1d 2020-02-22 naddy case CTRL('f'):
6053 61417565 2022-06-20 mark case 'f':
6054 1e37a5c2 2019-05-12 jcs case ' ':
6055 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6056 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6057 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6058 640cd7ff 2022-06-22 mark view->count = 0;
6059 e5a0f69f 2018-08-18 stsp break;
6060 1e37a5c2 2019-05-12 jcs }
6061 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6062 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6063 83cc4199 2022-06-13 stsp s->selected_line +=
6064 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6065 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6066 1e37a5c2 2019-05-12 jcs }
6067 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6068 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6069 1e37a5c2 2019-05-12 jcs else
6070 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6071 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6072 1e37a5c2 2019-05-12 jcs break;
6073 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6074 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6075 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6076 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6077 1e37a5c2 2019-05-12 jcs }
6078 1e37a5c2 2019-05-12 jcs break;
6079 1e37a5c2 2019-05-12 jcs default:
6080 640cd7ff 2022-06-22 mark view->count = 0;
6081 1e37a5c2 2019-05-12 jcs break;
6082 a70480e0 2018-06-23 stsp }
6083 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6084 917d79a7 2022-07-01 stsp }
6085 917d79a7 2022-07-01 stsp
6086 917d79a7 2022-07-01 stsp static const struct got_error *
6087 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6088 917d79a7 2022-07-01 stsp {
6089 917d79a7 2022-07-01 stsp const struct got_error *err;
6090 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6091 917d79a7 2022-07-01 stsp
6092 917d79a7 2022-07-01 stsp view->count = 0;
6093 917d79a7 2022-07-01 stsp s->done = 1;
6094 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6095 917d79a7 2022-07-01 stsp s->done = 0;
6096 917d79a7 2022-07-01 stsp if (err)
6097 917d79a7 2022-07-01 stsp return err;
6098 917d79a7 2022-07-01 stsp return run_blame(view);
6099 a70480e0 2018-06-23 stsp }
6100 a70480e0 2018-06-23 stsp
6101 a70480e0 2018-06-23 stsp static const struct got_error *
6102 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6103 9f7d7167 2018-04-29 stsp {
6104 a70480e0 2018-06-23 stsp const struct got_error *error;
6105 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6106 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6107 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6108 0587e10c 2020-07-23 stsp char *link_target = NULL;
6109 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6110 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6111 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6112 a70480e0 2018-06-23 stsp int ch;
6113 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6114 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6115 a70480e0 2018-06-23 stsp
6116 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6117 a70480e0 2018-06-23 stsp switch (ch) {
6118 a70480e0 2018-06-23 stsp case 'c':
6119 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6120 a70480e0 2018-06-23 stsp break;
6121 69069811 2018-08-02 stsp case 'r':
6122 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6123 69069811 2018-08-02 stsp if (repo_path == NULL)
6124 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6125 9ba1d308 2019-10-21 stsp optarg);
6126 69069811 2018-08-02 stsp break;
6127 a70480e0 2018-06-23 stsp default:
6128 17020d27 2019-03-07 stsp usage_blame();
6129 a70480e0 2018-06-23 stsp /* NOTREACHED */
6130 a70480e0 2018-06-23 stsp }
6131 a70480e0 2018-06-23 stsp }
6132 a70480e0 2018-06-23 stsp
6133 a70480e0 2018-06-23 stsp argc -= optind;
6134 a70480e0 2018-06-23 stsp argv += optind;
6135 a70480e0 2018-06-23 stsp
6136 f135c941 2020-02-20 stsp if (argc != 1)
6137 a70480e0 2018-06-23 stsp usage_blame();
6138 6962eb72 2020-02-20 stsp
6139 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6140 0ae84acc 2022-06-15 tracey if (error != NULL)
6141 0ae84acc 2022-06-15 tracey goto done;
6142 0ae84acc 2022-06-15 tracey
6143 69069811 2018-08-02 stsp if (repo_path == NULL) {
6144 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6145 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6146 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6147 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6148 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6149 c156c7a4 2020-12-18 stsp goto done;
6150 f135c941 2020-02-20 stsp if (worktree)
6151 eb41ed75 2019-02-05 stsp repo_path =
6152 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6153 f135c941 2020-02-20 stsp else
6154 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6155 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6156 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6157 c156c7a4 2020-12-18 stsp goto done;
6158 c156c7a4 2020-12-18 stsp }
6159 f135c941 2020-02-20 stsp }
6160 a915003a 2019-02-05 stsp
6161 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6162 c02c541e 2019-03-29 stsp if (error != NULL)
6163 8e94dd5b 2019-01-04 stsp goto done;
6164 69069811 2018-08-02 stsp
6165 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6166 f135c941 2020-02-20 stsp worktree);
6167 c02c541e 2019-03-29 stsp if (error)
6168 92205607 2019-01-04 stsp goto done;
6169 69069811 2018-08-02 stsp
6170 f135c941 2020-02-20 stsp init_curses();
6171 f135c941 2020-02-20 stsp
6172 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6173 51a10b52 2020-12-26 stsp if (error)
6174 51a10b52 2020-12-26 stsp goto done;
6175 51a10b52 2020-12-26 stsp
6176 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6177 eb41ed75 2019-02-05 stsp if (error)
6178 69069811 2018-08-02 stsp goto done;
6179 a70480e0 2018-06-23 stsp
6180 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6181 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6182 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6183 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6184 a70480e0 2018-06-23 stsp if (error != NULL)
6185 66b4983c 2018-06-23 stsp goto done;
6186 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6187 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6188 a70480e0 2018-06-23 stsp } else {
6189 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6190 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6191 a70480e0 2018-06-23 stsp }
6192 a19e88aa 2018-06-23 stsp if (error != NULL)
6193 8b473291 2019-02-21 stsp goto done;
6194 8b473291 2019-02-21 stsp
6195 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6196 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6197 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6198 e1cd8fed 2018-08-01 stsp goto done;
6199 e1cd8fed 2018-08-01 stsp }
6200 0587e10c 2020-07-23 stsp
6201 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6202 a44927cc 2022-04-07 stsp if (error)
6203 a44927cc 2022-04-07 stsp goto done;
6204 a44927cc 2022-04-07 stsp
6205 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6206 a44927cc 2022-04-07 stsp commit, repo);
6207 7cbe629d 2018-08-04 stsp if (error)
6208 7cbe629d 2018-08-04 stsp goto done;
6209 0587e10c 2020-07-23 stsp
6210 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6211 78756c87 2020-11-24 stsp commit_id, repo);
6212 0587e10c 2020-07-23 stsp if (error)
6213 0587e10c 2020-07-23 stsp goto done;
6214 12314ad4 2019-08-31 stsp if (worktree) {
6215 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6216 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6217 12314ad4 2019-08-31 stsp worktree = NULL;
6218 12314ad4 2019-08-31 stsp }
6219 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6220 a70480e0 2018-06-23 stsp done:
6221 69069811 2018-08-02 stsp free(repo_path);
6222 f135c941 2020-02-20 stsp free(in_repo_path);
6223 0587e10c 2020-07-23 stsp free(link_target);
6224 69069811 2018-08-02 stsp free(cwd);
6225 a70480e0 2018-06-23 stsp free(commit_id);
6226 a44927cc 2022-04-07 stsp if (commit)
6227 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6228 eb41ed75 2019-02-05 stsp if (worktree)
6229 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6230 1d0f4054 2021-06-17 stsp if (repo) {
6231 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6232 1d0f4054 2021-06-17 stsp if (error == NULL)
6233 1d0f4054 2021-06-17 stsp error = close_err;
6234 1d0f4054 2021-06-17 stsp }
6235 0ae84acc 2022-06-15 tracey if (pack_fds) {
6236 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6237 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6238 0ae84acc 2022-06-15 tracey if (error == NULL)
6239 0ae84acc 2022-06-15 tracey error = pack_err;
6240 0ae84acc 2022-06-15 tracey }
6241 51a10b52 2020-12-26 stsp tog_free_refs();
6242 a70480e0 2018-06-23 stsp return error;
6243 ffd1d5e5 2018-06-23 stsp }
6244 ffd1d5e5 2018-06-23 stsp
6245 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6246 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6247 ffd1d5e5 2018-06-23 stsp {
6248 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6249 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6250 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6251 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6252 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6253 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6254 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6255 ffd1d5e5 2018-06-23 stsp
6256 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6257 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6258 9b058f45 2022-06-30 mark --limit; /* border */
6259 ffd1d5e5 2018-06-23 stsp
6260 f7d12f7e 2018-08-01 stsp werase(view->window);
6261 ffd1d5e5 2018-06-23 stsp
6262 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6263 ffd1d5e5 2018-06-23 stsp return NULL;
6264 ffd1d5e5 2018-06-23 stsp
6265 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6266 ccda2f4d 2022-06-16 stsp 0, 0);
6267 ffd1d5e5 2018-06-23 stsp if (err)
6268 ffd1d5e5 2018-06-23 stsp return err;
6269 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6270 a3404814 2018-09-02 stsp wstandout(view->window);
6271 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6272 11b20872 2019-11-08 stsp if (tc)
6273 11b20872 2019-11-08 stsp wattr_on(view->window,
6274 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6275 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6276 11b20872 2019-11-08 stsp if (tc)
6277 11b20872 2019-11-08 stsp wattr_off(view->window,
6278 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6279 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6280 a3404814 2018-09-02 stsp wstandend(view->window);
6281 2550e4c3 2018-07-13 stsp free(wline);
6282 2550e4c3 2018-07-13 stsp wline = NULL;
6283 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6284 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6285 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6286 ffd1d5e5 2018-06-23 stsp return NULL;
6287 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6288 ccda2f4d 2022-06-16 stsp 0, 0);
6289 ce52c690 2018-06-23 stsp if (err)
6290 ce52c690 2018-06-23 stsp return err;
6291 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6292 2550e4c3 2018-07-13 stsp free(wline);
6293 2550e4c3 2018-07-13 stsp wline = NULL;
6294 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6295 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6296 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6297 ffd1d5e5 2018-06-23 stsp return NULL;
6298 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6299 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6300 a1eca9bb 2018-06-23 stsp return NULL;
6301 ffd1d5e5 2018-06-23 stsp
6302 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6303 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6304 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6305 0cf4efb1 2018-09-29 stsp if (view->focussed)
6306 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6307 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6308 ffd1d5e5 2018-06-23 stsp }
6309 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6310 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6311 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6312 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6313 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6314 ffd1d5e5 2018-06-23 stsp return NULL;
6315 ffd1d5e5 2018-06-23 stsp n = 1;
6316 ffd1d5e5 2018-06-23 stsp } else {
6317 ffd1d5e5 2018-06-23 stsp n = 0;
6318 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6319 ffd1d5e5 2018-06-23 stsp }
6320 ffd1d5e5 2018-06-23 stsp
6321 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6322 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6323 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6324 848d6979 2019-08-12 stsp const char *modestr = "";
6325 56e0773d 2019-11-28 stsp mode_t mode;
6326 1d13200f 2018-07-12 stsp
6327 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6328 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6329 56e0773d 2019-11-28 stsp
6330 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6331 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6332 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6333 1d13200f 2018-07-12 stsp if (err)
6334 638f9024 2019-05-13 stsp return got_error_from_errno(
6335 230a42bd 2019-05-11 jcs "got_object_id_str");
6336 1d13200f 2018-07-12 stsp }
6337 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6338 63c5ca5d 2019-08-24 stsp modestr = "$";
6339 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6340 0d6c6ee3 2020-05-20 stsp int i;
6341 0d6c6ee3 2020-05-20 stsp
6342 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6343 d86d3b18 2020-12-01 naddy te, s->repo);
6344 0d6c6ee3 2020-05-20 stsp if (err) {
6345 0d6c6ee3 2020-05-20 stsp free(id_str);
6346 0d6c6ee3 2020-05-20 stsp return err;
6347 0d6c6ee3 2020-05-20 stsp }
6348 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6349 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6350 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6351 0d6c6ee3 2020-05-20 stsp }
6352 848d6979 2019-08-12 stsp modestr = "@";
6353 0d6c6ee3 2020-05-20 stsp }
6354 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6355 848d6979 2019-08-12 stsp modestr = "/";
6356 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6357 848d6979 2019-08-12 stsp modestr = "*";
6358 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6359 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6360 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6361 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6362 1d13200f 2018-07-12 stsp free(id_str);
6363 0d6c6ee3 2020-05-20 stsp free(link_target);
6364 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6365 1d13200f 2018-07-12 stsp }
6366 1d13200f 2018-07-12 stsp free(id_str);
6367 0d6c6ee3 2020-05-20 stsp free(link_target);
6368 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6369 ccda2f4d 2022-06-16 stsp 0, 0);
6370 ffd1d5e5 2018-06-23 stsp if (err) {
6371 ffd1d5e5 2018-06-23 stsp free(line);
6372 ffd1d5e5 2018-06-23 stsp break;
6373 ffd1d5e5 2018-06-23 stsp }
6374 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6375 0cf4efb1 2018-09-29 stsp if (view->focussed)
6376 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6377 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6378 ffd1d5e5 2018-06-23 stsp }
6379 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6380 f26dddb7 2019-11-08 stsp if (tc)
6381 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6382 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6383 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6384 f26dddb7 2019-11-08 stsp if (tc)
6385 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6386 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6387 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6388 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6389 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6390 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6391 ffd1d5e5 2018-06-23 stsp free(line);
6392 2550e4c3 2018-07-13 stsp free(wline);
6393 2550e4c3 2018-07-13 stsp wline = NULL;
6394 ffd1d5e5 2018-06-23 stsp n++;
6395 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6396 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6397 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6398 ffd1d5e5 2018-06-23 stsp break;
6399 ffd1d5e5 2018-06-23 stsp }
6400 ffd1d5e5 2018-06-23 stsp
6401 ffd1d5e5 2018-06-23 stsp return err;
6402 ffd1d5e5 2018-06-23 stsp }
6403 ffd1d5e5 2018-06-23 stsp
6404 ffd1d5e5 2018-06-23 stsp static void
6405 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6406 ffd1d5e5 2018-06-23 stsp {
6407 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6408 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6409 fa86c4bf 2020-11-29 stsp int i = 0;
6410 ffd1d5e5 2018-06-23 stsp
6411 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6412 ffd1d5e5 2018-06-23 stsp return;
6413 ffd1d5e5 2018-06-23 stsp
6414 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6415 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6416 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6417 fa86c4bf 2020-11-29 stsp if (!isroot)
6418 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6419 fa86c4bf 2020-11-29 stsp break;
6420 fa86c4bf 2020-11-29 stsp }
6421 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6422 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6423 ffd1d5e5 2018-06-23 stsp }
6424 ffd1d5e5 2018-06-23 stsp }
6425 ffd1d5e5 2018-06-23 stsp
6426 9b058f45 2022-06-30 mark static const struct got_error *
6427 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6428 ffd1d5e5 2018-06-23 stsp {
6429 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6430 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6431 ffd1d5e5 2018-06-23 stsp int n = 0;
6432 ffd1d5e5 2018-06-23 stsp
6433 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6434 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6435 694d3271 2020-12-01 naddy s->first_displayed_entry);
6436 694d3271 2020-12-01 naddy else
6437 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6438 56e0773d 2019-11-28 stsp
6439 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6440 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6441 9b058f45 2022-06-30 mark if (last)
6442 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6443 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6444 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6445 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6446 768394f3 2019-01-24 stsp }
6447 ffd1d5e5 2018-06-23 stsp }
6448 9b058f45 2022-06-30 mark
6449 9b058f45 2022-06-30 mark return NULL;
6450 ffd1d5e5 2018-06-23 stsp }
6451 ffd1d5e5 2018-06-23 stsp
6452 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6453 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6454 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6455 ffd1d5e5 2018-06-23 stsp {
6456 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6457 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6458 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6459 ffd1d5e5 2018-06-23 stsp
6460 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6461 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6462 56e0773d 2019-11-28 stsp + 1 /* slash */;
6463 ce52c690 2018-06-23 stsp if (te)
6464 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6465 ce52c690 2018-06-23 stsp
6466 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6467 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6468 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6469 ffd1d5e5 2018-06-23 stsp
6470 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6471 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6472 d9765a41 2018-06-23 stsp while (pt) {
6473 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6474 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6475 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6476 cb2ebc8a 2018-06-23 stsp goto done;
6477 cb2ebc8a 2018-06-23 stsp }
6478 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6479 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6480 cb2ebc8a 2018-06-23 stsp goto done;
6481 cb2ebc8a 2018-06-23 stsp }
6482 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6483 ffd1d5e5 2018-06-23 stsp }
6484 ce52c690 2018-06-23 stsp if (te) {
6485 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6486 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6487 ce52c690 2018-06-23 stsp goto done;
6488 ce52c690 2018-06-23 stsp }
6489 cb2ebc8a 2018-06-23 stsp }
6490 ce52c690 2018-06-23 stsp done:
6491 ce52c690 2018-06-23 stsp if (err) {
6492 ce52c690 2018-06-23 stsp free(*path);
6493 ce52c690 2018-06-23 stsp *path = NULL;
6494 ce52c690 2018-06-23 stsp }
6495 ce52c690 2018-06-23 stsp return err;
6496 ce52c690 2018-06-23 stsp }
6497 ce52c690 2018-06-23 stsp
6498 ce52c690 2018-06-23 stsp static const struct got_error *
6499 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6500 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6501 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6502 ce52c690 2018-06-23 stsp {
6503 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6504 ce52c690 2018-06-23 stsp char *path;
6505 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6506 a0de39f3 2019-08-09 stsp
6507 a0de39f3 2019-08-09 stsp *new_view = NULL;
6508 69efd4c4 2018-07-18 stsp
6509 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6510 ce52c690 2018-06-23 stsp if (err)
6511 ce52c690 2018-06-23 stsp return err;
6512 ffd1d5e5 2018-06-23 stsp
6513 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6514 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6515 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6516 83ce39e3 2019-08-12 stsp goto done;
6517 83ce39e3 2019-08-12 stsp }
6518 cdf1ee82 2018-08-01 stsp
6519 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6520 e5a0f69f 2018-08-18 stsp if (err) {
6521 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6522 fc06ba56 2019-08-22 stsp err = NULL;
6523 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6524 e5a0f69f 2018-08-18 stsp } else
6525 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6526 83ce39e3 2019-08-12 stsp done:
6527 83ce39e3 2019-08-12 stsp free(path);
6528 69efd4c4 2018-07-18 stsp return err;
6529 69efd4c4 2018-07-18 stsp }
6530 69efd4c4 2018-07-18 stsp
6531 69efd4c4 2018-07-18 stsp static const struct got_error *
6532 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6533 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6534 69efd4c4 2018-07-18 stsp {
6535 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6536 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6537 69efd4c4 2018-07-18 stsp char *path;
6538 a0de39f3 2019-08-09 stsp
6539 a0de39f3 2019-08-09 stsp *new_view = NULL;
6540 69efd4c4 2018-07-18 stsp
6541 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6542 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6543 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6544 e5a0f69f 2018-08-18 stsp
6545 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6546 69efd4c4 2018-07-18 stsp if (err)
6547 69efd4c4 2018-07-18 stsp return err;
6548 69efd4c4 2018-07-18 stsp
6549 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6550 4e97c21c 2020-12-06 stsp path, 0);
6551 ba4f502b 2018-08-04 stsp if (err)
6552 e5a0f69f 2018-08-18 stsp view_close(log_view);
6553 e5a0f69f 2018-08-18 stsp else
6554 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6555 cb2ebc8a 2018-06-23 stsp free(path);
6556 cb2ebc8a 2018-06-23 stsp return err;
6557 ffd1d5e5 2018-06-23 stsp }
6558 ffd1d5e5 2018-06-23 stsp
6559 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6560 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6561 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6562 ffd1d5e5 2018-06-23 stsp {
6563 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6564 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6565 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6566 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6567 ffd1d5e5 2018-06-23 stsp
6568 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6569 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6570 bc573f3b 2021-07-10 stsp
6571 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6572 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6573 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6574 ffd1d5e5 2018-06-23 stsp
6575 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6576 bc573f3b 2021-07-10 stsp if (err)
6577 bc573f3b 2021-07-10 stsp goto done;
6578 bc573f3b 2021-07-10 stsp
6579 bc573f3b 2021-07-10 stsp /*
6580 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6581 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6582 bc573f3b 2021-07-10 stsp * closed on demand.
6583 bc573f3b 2021-07-10 stsp */
6584 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6585 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6586 bc573f3b 2021-07-10 stsp if (err)
6587 bc573f3b 2021-07-10 stsp goto done;
6588 bc573f3b 2021-07-10 stsp s->tree = s->root;
6589 bc573f3b 2021-07-10 stsp
6590 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6591 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6592 ffd1d5e5 2018-06-23 stsp goto done;
6593 ffd1d5e5 2018-06-23 stsp
6594 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6595 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6596 ffd1d5e5 2018-06-23 stsp goto done;
6597 ffd1d5e5 2018-06-23 stsp }
6598 ffd1d5e5 2018-06-23 stsp
6599 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6600 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6601 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6602 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6603 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6604 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6605 9cd7cbd1 2020-12-07 stsp goto done;
6606 9cd7cbd1 2020-12-07 stsp }
6607 9cd7cbd1 2020-12-07 stsp }
6608 fb2756b9 2018-08-04 stsp s->repo = repo;
6609 c0b01bdb 2019-11-08 stsp
6610 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6611 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6612 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6613 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6614 c0b01bdb 2019-11-08 stsp if (err)
6615 c0b01bdb 2019-11-08 stsp goto done;
6616 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6617 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6618 bc573f3b 2021-07-10 stsp if (err)
6619 c0b01bdb 2019-11-08 stsp goto done;
6620 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6621 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6622 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6623 bc573f3b 2021-07-10 stsp if (err)
6624 c0b01bdb 2019-11-08 stsp goto done;
6625 e5a0f69f 2018-08-18 stsp
6626 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6627 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6628 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6629 bc573f3b 2021-07-10 stsp if (err)
6630 11b20872 2019-11-08 stsp goto done;
6631 11b20872 2019-11-08 stsp
6632 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6633 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6634 bc573f3b 2021-07-10 stsp if (err)
6635 c0b01bdb 2019-11-08 stsp goto done;
6636 c0b01bdb 2019-11-08 stsp }
6637 c0b01bdb 2019-11-08 stsp
6638 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6639 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6640 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6641 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6642 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6643 ad80ab7b 2018-08-04 stsp done:
6644 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6645 bc573f3b 2021-07-10 stsp if (commit)
6646 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6647 bc573f3b 2021-07-10 stsp if (err)
6648 bc573f3b 2021-07-10 stsp close_tree_view(view);
6649 ad80ab7b 2018-08-04 stsp return err;
6650 ad80ab7b 2018-08-04 stsp }
6651 ad80ab7b 2018-08-04 stsp
6652 e5a0f69f 2018-08-18 stsp static const struct got_error *
6653 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6654 ad80ab7b 2018-08-04 stsp {
6655 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6656 ad80ab7b 2018-08-04 stsp
6657 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6658 fb2756b9 2018-08-04 stsp free(s->tree_label);
6659 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6660 6484ec90 2018-09-29 stsp free(s->commit_id);
6661 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6662 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6663 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6664 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6665 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6666 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6667 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6668 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6669 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6670 ad80ab7b 2018-08-04 stsp free(parent);
6671 ad80ab7b 2018-08-04 stsp
6672 ad80ab7b 2018-08-04 stsp }
6673 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6674 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6675 bc573f3b 2021-07-10 stsp if (s->root)
6676 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6677 7c32bd05 2019-06-22 stsp return NULL;
6678 7c32bd05 2019-06-22 stsp }
6679 7c32bd05 2019-06-22 stsp
6680 7c32bd05 2019-06-22 stsp static const struct got_error *
6681 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6682 7c32bd05 2019-06-22 stsp {
6683 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6684 7c32bd05 2019-06-22 stsp
6685 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6686 7c32bd05 2019-06-22 stsp return NULL;
6687 7c32bd05 2019-06-22 stsp }
6688 7c32bd05 2019-06-22 stsp
6689 7c32bd05 2019-06-22 stsp static int
6690 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6691 7c32bd05 2019-06-22 stsp {
6692 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6693 7c32bd05 2019-06-22 stsp
6694 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6695 56e0773d 2019-11-28 stsp 0) == 0;
6696 7c32bd05 2019-06-22 stsp }
6697 7c32bd05 2019-06-22 stsp
6698 7c32bd05 2019-06-22 stsp static const struct got_error *
6699 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6700 7c32bd05 2019-06-22 stsp {
6701 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6702 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6703 7c32bd05 2019-06-22 stsp
6704 7c32bd05 2019-06-22 stsp if (!view->searching) {
6705 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6706 7c32bd05 2019-06-22 stsp return NULL;
6707 7c32bd05 2019-06-22 stsp }
6708 7c32bd05 2019-06-22 stsp
6709 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6710 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6711 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6712 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6713 56e0773d 2019-11-28 stsp s->selected_entry);
6714 7c32bd05 2019-06-22 stsp else
6715 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6716 56e0773d 2019-11-28 stsp } else {
6717 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6718 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6719 56e0773d 2019-11-28 stsp else
6720 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6721 56e0773d 2019-11-28 stsp s->selected_entry);
6722 7c32bd05 2019-06-22 stsp }
6723 7c32bd05 2019-06-22 stsp } else {
6724 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6725 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6726 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6727 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6728 56e0773d 2019-11-28 stsp else
6729 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6730 7c32bd05 2019-06-22 stsp }
6731 7c32bd05 2019-06-22 stsp
6732 7c32bd05 2019-06-22 stsp while (1) {
6733 56e0773d 2019-11-28 stsp if (te == NULL) {
6734 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6735 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6736 ac66afb8 2019-06-24 stsp return NULL;
6737 ac66afb8 2019-06-24 stsp }
6738 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6739 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6740 56e0773d 2019-11-28 stsp else
6741 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6742 7c32bd05 2019-06-22 stsp }
6743 7c32bd05 2019-06-22 stsp
6744 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6745 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6746 56e0773d 2019-11-28 stsp s->matched_entry = te;
6747 7c32bd05 2019-06-22 stsp break;
6748 7c32bd05 2019-06-22 stsp }
6749 7c32bd05 2019-06-22 stsp
6750 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6751 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6752 56e0773d 2019-11-28 stsp else
6753 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6754 7c32bd05 2019-06-22 stsp }
6755 e5a0f69f 2018-08-18 stsp
6756 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6757 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6758 7c32bd05 2019-06-22 stsp s->selected = 0;
6759 7c32bd05 2019-06-22 stsp }
6760 7c32bd05 2019-06-22 stsp
6761 e5a0f69f 2018-08-18 stsp return NULL;
6762 ad80ab7b 2018-08-04 stsp }
6763 ad80ab7b 2018-08-04 stsp
6764 ad80ab7b 2018-08-04 stsp static const struct got_error *
6765 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6766 ad80ab7b 2018-08-04 stsp {
6767 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6768 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6769 e5a0f69f 2018-08-18 stsp char *parent_path;
6770 ad80ab7b 2018-08-04 stsp
6771 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6772 e5a0f69f 2018-08-18 stsp if (err)
6773 e5a0f69f 2018-08-18 stsp return err;
6774 ffd1d5e5 2018-06-23 stsp
6775 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6776 e5a0f69f 2018-08-18 stsp free(parent_path);
6777 669b5ffa 2018-10-07 stsp
6778 9b058f45 2022-06-30 mark view_border(view);
6779 e5a0f69f 2018-08-18 stsp return err;
6780 e5a0f69f 2018-08-18 stsp }
6781 ce52c690 2018-06-23 stsp
6782 e5a0f69f 2018-08-18 stsp static const struct got_error *
6783 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6784 e5a0f69f 2018-08-18 stsp {
6785 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6786 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6787 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6788 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6789 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6790 ffd1d5e5 2018-06-23 stsp
6791 e5a0f69f 2018-08-18 stsp switch (ch) {
6792 1e37a5c2 2019-05-12 jcs case 'i':
6793 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6794 640cd7ff 2022-06-22 mark view->count = 0;
6795 1e37a5c2 2019-05-12 jcs break;
6796 1e37a5c2 2019-05-12 jcs case 'l':
6797 640cd7ff 2022-06-22 mark view->count = 0;
6798 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6799 ffd1d5e5 2018-06-23 stsp break;
6800 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6801 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6802 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6803 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6804 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6805 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6806 49b24ee5 2022-07-03 mark if (err)
6807 49b24ee5 2022-07-03 mark break;
6808 49b24ee5 2022-07-03 mark }
6809 e78dc838 2020-12-04 stsp view->focussed = 0;
6810 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6811 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6812 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6813 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6814 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
6815 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6816 1e37a5c2 2019-05-12 jcs if (err)
6817 1e37a5c2 2019-05-12 jcs return err;
6818 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6819 0dbbbe90 2022-06-17 op if (err)
6820 0dbbbe90 2022-06-17 op return err;
6821 e78dc838 2020-12-04 stsp view->focus_child = 1;
6822 1e37a5c2 2019-05-12 jcs } else
6823 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6824 152c1c93 2020-11-29 stsp break;
6825 152c1c93 2020-11-29 stsp case 'r':
6826 640cd7ff 2022-06-22 mark view->count = 0;
6827 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6828 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6829 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6830 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6831 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6832 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6833 152c1c93 2020-11-29 stsp if (err) {
6834 152c1c93 2020-11-29 stsp view_close(ref_view);
6835 152c1c93 2020-11-29 stsp return err;
6836 152c1c93 2020-11-29 stsp }
6837 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6838 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6839 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6840 49b24ee5 2022-07-03 mark if (err)
6841 49b24ee5 2022-07-03 mark break;
6842 49b24ee5 2022-07-03 mark }
6843 e78dc838 2020-12-04 stsp view->focussed = 0;
6844 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6845 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6846 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6847 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6848 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
6849 152c1c93 2020-11-29 stsp err = view_close_child(view);
6850 152c1c93 2020-11-29 stsp if (err)
6851 152c1c93 2020-11-29 stsp return err;
6852 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6853 0dbbbe90 2022-06-17 op if (err)
6854 0dbbbe90 2022-06-17 op return err;
6855 e78dc838 2020-12-04 stsp view->focus_child = 1;
6856 152c1c93 2020-11-29 stsp } else
6857 152c1c93 2020-11-29 stsp *new_view = ref_view;
6858 e4526bf5 2021-09-03 naddy break;
6859 e4526bf5 2021-09-03 naddy case 'g':
6860 e4526bf5 2021-09-03 naddy case KEY_HOME:
6861 e4526bf5 2021-09-03 naddy s->selected = 0;
6862 640cd7ff 2022-06-22 mark view->count = 0;
6863 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6864 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6865 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6866 e4526bf5 2021-09-03 naddy else
6867 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6868 1e37a5c2 2019-05-12 jcs break;
6869 e4526bf5 2021-09-03 naddy case 'G':
6870 9b058f45 2022-06-30 mark case KEY_END: {
6871 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6872 9b058f45 2022-06-30 mark
6873 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6874 9b058f45 2022-06-30 mark --eos; /* border */
6875 e4526bf5 2021-09-03 naddy s->selected = 0;
6876 640cd7ff 2022-06-22 mark view->count = 0;
6877 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6878 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6879 e4526bf5 2021-09-03 naddy if (te == NULL) {
6880 ad055527 2022-07-16 mark if (s->tree != s->root) {
6881 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6882 e4526bf5 2021-09-03 naddy n++;
6883 e4526bf5 2021-09-03 naddy }
6884 e4526bf5 2021-09-03 naddy break;
6885 e4526bf5 2021-09-03 naddy }
6886 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6887 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6888 e4526bf5 2021-09-03 naddy }
6889 e4526bf5 2021-09-03 naddy if (n > 0)
6890 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6891 e4526bf5 2021-09-03 naddy break;
6892 9b058f45 2022-06-30 mark }
6893 1e37a5c2 2019-05-12 jcs case 'k':
6894 1e37a5c2 2019-05-12 jcs case KEY_UP:
6895 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6896 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6897 1e37a5c2 2019-05-12 jcs s->selected--;
6898 fa86c4bf 2020-11-29 stsp break;
6899 1e37a5c2 2019-05-12 jcs }
6900 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6901 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6902 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6903 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6904 640cd7ff 2022-06-22 mark view->count = 0;
6905 1e37a5c2 2019-05-12 jcs break;
6906 83cc4199 2022-06-13 stsp case CTRL('u'):
6907 33c3719a 2022-06-15 stsp case 'u':
6908 83cc4199 2022-06-13 stsp nscroll /= 2;
6909 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6910 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6911 ea025d1d 2020-02-22 naddy case CTRL('b'):
6912 61417565 2022-06-20 mark case 'b':
6913 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6914 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6915 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6916 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6917 fa86c4bf 2020-11-29 stsp } else {
6918 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6919 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6920 fa86c4bf 2020-11-29 stsp }
6921 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6922 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6923 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6924 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6925 640cd7ff 2022-06-22 mark view->count = 0;
6926 1e37a5c2 2019-05-12 jcs break;
6927 1e37a5c2 2019-05-12 jcs case 'j':
6928 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6929 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6930 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6931 1e37a5c2 2019-05-12 jcs s->selected++;
6932 1e37a5c2 2019-05-12 jcs break;
6933 1e37a5c2 2019-05-12 jcs }
6934 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6935 640cd7ff 2022-06-22 mark == NULL) {
6936 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6937 640cd7ff 2022-06-22 mark view->count = 0;
6938 1e37a5c2 2019-05-12 jcs break;
6939 640cd7ff 2022-06-22 mark }
6940 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6941 1e37a5c2 2019-05-12 jcs break;
6942 83cc4199 2022-06-13 stsp case CTRL('d'):
6943 33c3719a 2022-06-15 stsp case 'd':
6944 83cc4199 2022-06-13 stsp nscroll /= 2;
6945 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6946 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6947 ea025d1d 2020-02-22 naddy case CTRL('f'):
6948 61417565 2022-06-20 mark case 'f':
6949 48bb96f0 2022-06-20 naddy case ' ':
6950 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6951 1e37a5c2 2019-05-12 jcs == NULL) {
6952 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6953 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6954 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6955 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6956 640cd7ff 2022-06-22 mark else
6957 640cd7ff 2022-06-22 mark view->count = 0;
6958 1e37a5c2 2019-05-12 jcs break;
6959 1e37a5c2 2019-05-12 jcs }
6960 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6961 1e37a5c2 2019-05-12 jcs break;
6962 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6963 1e37a5c2 2019-05-12 jcs case '\r':
6964 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6965 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6966 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6967 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6968 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6969 640cd7ff 2022-06-22 mark view->count = 0;
6970 1e37a5c2 2019-05-12 jcs break;
6971 640cd7ff 2022-06-22 mark }
6972 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6973 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6974 1e37a5c2 2019-05-12 jcs entry);
6975 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6976 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6977 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6978 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6979 1e37a5c2 2019-05-12 jcs s->selected_entry =
6980 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6981 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6982 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6983 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6984 9b058f45 2022-06-30 mark if (err)
6985 9b058f45 2022-06-30 mark break;
6986 9b058f45 2022-06-30 mark }
6987 1e37a5c2 2019-05-12 jcs free(parent);
6988 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6989 56e0773d 2019-11-28 stsp s->selected_entry))) {
6990 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6991 640cd7ff 2022-06-22 mark view->count = 0;
6992 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6993 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6994 1e37a5c2 2019-05-12 jcs if (err)
6995 1e37a5c2 2019-05-12 jcs break;
6996 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6997 941e9f74 2019-05-21 stsp if (err) {
6998 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6999 1e37a5c2 2019-05-12 jcs break;
7000 1e37a5c2 2019-05-12 jcs }
7001 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7002 56e0773d 2019-11-28 stsp s->selected_entry))) {
7003 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7004 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
7005 1e37a5c2 2019-05-12 jcs
7006 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
7007 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7008 9b058f45 2022-06-30 mark
7009 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
7010 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7011 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7012 1e37a5c2 2019-05-12 jcs if (err)
7013 1e37a5c2 2019-05-12 jcs break;
7014 9b058f45 2022-06-30 mark
7015 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7016 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7017 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7018 9b058f45 2022-06-30 mark if (err)
7019 9b058f45 2022-06-30 mark break;
7020 9b058f45 2022-06-30 mark }
7021 9b058f45 2022-06-30 mark
7022 640cd7ff 2022-06-22 mark view->count = 0;
7023 e78dc838 2020-12-04 stsp view->focussed = 0;
7024 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7025 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
7026 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
7027 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7028 3c1dfe12 2022-07-08 mark view_transfer_size(blame_view, view);
7029 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7030 669b5ffa 2018-10-07 stsp if (err)
7031 669b5ffa 2018-10-07 stsp return err;
7032 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
7033 0dbbbe90 2022-06-17 op if (err)
7034 0dbbbe90 2022-06-17 op return err;
7035 e78dc838 2020-12-04 stsp view->focus_child = 1;
7036 669b5ffa 2018-10-07 stsp } else
7037 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7038 1e37a5c2 2019-05-12 jcs }
7039 1e37a5c2 2019-05-12 jcs break;
7040 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7041 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7042 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7043 640cd7ff 2022-06-22 mark view->count = 0;
7044 1e37a5c2 2019-05-12 jcs break;
7045 1e37a5c2 2019-05-12 jcs default:
7046 640cd7ff 2022-06-22 mark view->count = 0;
7047 1e37a5c2 2019-05-12 jcs break;
7048 ffd1d5e5 2018-06-23 stsp }
7049 e5a0f69f 2018-08-18 stsp
7050 ffd1d5e5 2018-06-23 stsp return err;
7051 9f7d7167 2018-04-29 stsp }
7052 9f7d7167 2018-04-29 stsp
7053 ffd1d5e5 2018-06-23 stsp __dead static void
7054 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7055 ffd1d5e5 2018-06-23 stsp {
7056 ffd1d5e5 2018-06-23 stsp endwin();
7057 87411fa9 2022-06-16 stsp fprintf(stderr,
7058 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7059 ffd1d5e5 2018-06-23 stsp getprogname());
7060 ffd1d5e5 2018-06-23 stsp exit(1);
7061 ffd1d5e5 2018-06-23 stsp }
7062 ffd1d5e5 2018-06-23 stsp
7063 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7064 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7065 ffd1d5e5 2018-06-23 stsp {
7066 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7067 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7068 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7069 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7070 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7071 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7072 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7073 4e97c21c 2020-12-06 stsp char *label = NULL;
7074 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7075 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7076 ffd1d5e5 2018-06-23 stsp int ch;
7077 5221c383 2018-08-01 stsp struct tog_view *view;
7078 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7079 70ac5f84 2019-03-28 stsp
7080 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7081 ffd1d5e5 2018-06-23 stsp switch (ch) {
7082 ffd1d5e5 2018-06-23 stsp case 'c':
7083 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7084 74283ab8 2020-02-07 stsp break;
7085 74283ab8 2020-02-07 stsp case 'r':
7086 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7087 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7088 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7089 74283ab8 2020-02-07 stsp optarg);
7090 ffd1d5e5 2018-06-23 stsp break;
7091 ffd1d5e5 2018-06-23 stsp default:
7092 e99e2d15 2020-11-24 naddy usage_tree();
7093 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7094 ffd1d5e5 2018-06-23 stsp }
7095 ffd1d5e5 2018-06-23 stsp }
7096 ffd1d5e5 2018-06-23 stsp
7097 ffd1d5e5 2018-06-23 stsp argc -= optind;
7098 ffd1d5e5 2018-06-23 stsp argv += optind;
7099 ffd1d5e5 2018-06-23 stsp
7100 55cccc34 2020-02-20 stsp if (argc > 1)
7101 e99e2d15 2020-11-24 naddy usage_tree();
7102 0ae84acc 2022-06-15 tracey
7103 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7104 0ae84acc 2022-06-15 tracey if (error != NULL)
7105 0ae84acc 2022-06-15 tracey goto done;
7106 74283ab8 2020-02-07 stsp
7107 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7108 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7109 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7110 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7111 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7112 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7113 c156c7a4 2020-12-18 stsp goto done;
7114 55cccc34 2020-02-20 stsp if (worktree)
7115 52185f70 2019-02-05 stsp repo_path =
7116 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7117 55cccc34 2020-02-20 stsp else
7118 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7119 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7120 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7121 c156c7a4 2020-12-18 stsp goto done;
7122 c156c7a4 2020-12-18 stsp }
7123 55cccc34 2020-02-20 stsp }
7124 a915003a 2019-02-05 stsp
7125 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7126 c02c541e 2019-03-29 stsp if (error != NULL)
7127 52185f70 2019-02-05 stsp goto done;
7128 d188b9a6 2019-01-04 stsp
7129 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7130 55cccc34 2020-02-20 stsp repo, worktree);
7131 55cccc34 2020-02-20 stsp if (error)
7132 55cccc34 2020-02-20 stsp goto done;
7133 55cccc34 2020-02-20 stsp
7134 55cccc34 2020-02-20 stsp init_curses();
7135 55cccc34 2020-02-20 stsp
7136 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7137 c02c541e 2019-03-29 stsp if (error)
7138 52185f70 2019-02-05 stsp goto done;
7139 ffd1d5e5 2018-06-23 stsp
7140 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7141 51a10b52 2020-12-26 stsp if (error)
7142 51a10b52 2020-12-26 stsp goto done;
7143 51a10b52 2020-12-26 stsp
7144 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7145 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7146 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7147 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7148 4e97c21c 2020-12-06 stsp if (error)
7149 4e97c21c 2020-12-06 stsp goto done;
7150 4e97c21c 2020-12-06 stsp head_ref_name = label;
7151 4e97c21c 2020-12-06 stsp } else {
7152 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7153 4e97c21c 2020-12-06 stsp if (error == NULL)
7154 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7155 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7156 4e97c21c 2020-12-06 stsp goto done;
7157 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7158 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7159 4e97c21c 2020-12-06 stsp if (error)
7160 4e97c21c 2020-12-06 stsp goto done;
7161 4e97c21c 2020-12-06 stsp }
7162 ffd1d5e5 2018-06-23 stsp
7163 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7164 a44927cc 2022-04-07 stsp if (error)
7165 a44927cc 2022-04-07 stsp goto done;
7166 a44927cc 2022-04-07 stsp
7167 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7168 5221c383 2018-08-01 stsp if (view == NULL) {
7169 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7170 5221c383 2018-08-01 stsp goto done;
7171 5221c383 2018-08-01 stsp }
7172 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7173 ad80ab7b 2018-08-04 stsp if (error)
7174 ad80ab7b 2018-08-04 stsp goto done;
7175 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7176 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7177 d91faf3b 2020-12-01 naddy in_repo_path);
7178 55cccc34 2020-02-20 stsp if (error)
7179 55cccc34 2020-02-20 stsp goto done;
7180 55cccc34 2020-02-20 stsp }
7181 55cccc34 2020-02-20 stsp
7182 55cccc34 2020-02-20 stsp if (worktree) {
7183 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7184 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7185 55cccc34 2020-02-20 stsp worktree = NULL;
7186 55cccc34 2020-02-20 stsp }
7187 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7188 ffd1d5e5 2018-06-23 stsp done:
7189 52185f70 2019-02-05 stsp free(repo_path);
7190 e4a0e26d 2020-02-20 stsp free(cwd);
7191 ffd1d5e5 2018-06-23 stsp free(commit_id);
7192 4e97c21c 2020-12-06 stsp free(label);
7193 486cd271 2020-12-06 stsp if (ref)
7194 486cd271 2020-12-06 stsp got_ref_close(ref);
7195 1d0f4054 2021-06-17 stsp if (repo) {
7196 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7197 1d0f4054 2021-06-17 stsp if (error == NULL)
7198 1d0f4054 2021-06-17 stsp error = close_err;
7199 1d0f4054 2021-06-17 stsp }
7200 0ae84acc 2022-06-15 tracey if (pack_fds) {
7201 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7202 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7203 0ae84acc 2022-06-15 tracey if (error == NULL)
7204 0ae84acc 2022-06-15 tracey error = pack_err;
7205 0ae84acc 2022-06-15 tracey }
7206 51a10b52 2020-12-26 stsp tog_free_refs();
7207 ffd1d5e5 2018-06-23 stsp return error;
7208 6458efa5 2020-11-24 stsp }
7209 6458efa5 2020-11-24 stsp
7210 6458efa5 2020-11-24 stsp static const struct got_error *
7211 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7212 6458efa5 2020-11-24 stsp {
7213 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7214 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7215 6458efa5 2020-11-24 stsp
7216 6458efa5 2020-11-24 stsp s->nrefs = 0;
7217 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7218 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7219 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7220 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7221 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7222 6458efa5 2020-11-24 stsp continue;
7223 6458efa5 2020-11-24 stsp
7224 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7225 6458efa5 2020-11-24 stsp if (re == NULL)
7226 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7227 6458efa5 2020-11-24 stsp
7228 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7229 8924d611 2020-12-26 stsp if (re->ref == NULL)
7230 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7231 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7232 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7233 6458efa5 2020-11-24 stsp }
7234 6458efa5 2020-11-24 stsp
7235 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7236 6458efa5 2020-11-24 stsp return NULL;
7237 6458efa5 2020-11-24 stsp }
7238 6458efa5 2020-11-24 stsp
7239 336075a4 2022-06-25 op static void
7240 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7241 6458efa5 2020-11-24 stsp {
7242 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7243 6458efa5 2020-11-24 stsp
7244 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7245 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7246 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7247 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7248 6458efa5 2020-11-24 stsp free(re);
7249 6458efa5 2020-11-24 stsp }
7250 6458efa5 2020-11-24 stsp }
7251 6458efa5 2020-11-24 stsp
7252 6458efa5 2020-11-24 stsp static const struct got_error *
7253 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7254 6458efa5 2020-11-24 stsp {
7255 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7256 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7257 6458efa5 2020-11-24 stsp
7258 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7259 6458efa5 2020-11-24 stsp s->repo = repo;
7260 6458efa5 2020-11-24 stsp
7261 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7262 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7263 6458efa5 2020-11-24 stsp
7264 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7265 6458efa5 2020-11-24 stsp if (err)
7266 6458efa5 2020-11-24 stsp return err;
7267 34ba6917 2020-11-30 stsp
7268 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7269 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7270 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7271 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7272 6458efa5 2020-11-24 stsp if (err)
7273 6458efa5 2020-11-24 stsp goto done;
7274 6458efa5 2020-11-24 stsp
7275 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7276 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7277 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7278 6458efa5 2020-11-24 stsp if (err)
7279 6458efa5 2020-11-24 stsp goto done;
7280 6458efa5 2020-11-24 stsp
7281 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7282 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7283 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7284 cc488aa7 2022-01-23 stsp if (err)
7285 cc488aa7 2022-01-23 stsp goto done;
7286 cc488aa7 2022-01-23 stsp
7287 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7288 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7289 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7290 6458efa5 2020-11-24 stsp if (err)
7291 6458efa5 2020-11-24 stsp goto done;
7292 6458efa5 2020-11-24 stsp }
7293 6458efa5 2020-11-24 stsp
7294 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7295 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7296 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7297 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7298 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7299 6458efa5 2020-11-24 stsp done:
7300 6458efa5 2020-11-24 stsp if (err)
7301 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7302 6458efa5 2020-11-24 stsp return err;
7303 6458efa5 2020-11-24 stsp }
7304 6458efa5 2020-11-24 stsp
7305 6458efa5 2020-11-24 stsp static const struct got_error *
7306 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7307 6458efa5 2020-11-24 stsp {
7308 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7309 6458efa5 2020-11-24 stsp
7310 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7311 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7312 6458efa5 2020-11-24 stsp
7313 6458efa5 2020-11-24 stsp return NULL;
7314 9f7d7167 2018-04-29 stsp }
7315 ce5b7c56 2019-07-09 stsp
7316 6458efa5 2020-11-24 stsp static const struct got_error *
7317 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7318 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7319 6458efa5 2020-11-24 stsp {
7320 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7321 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7322 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7323 6458efa5 2020-11-24 stsp int obj_type;
7324 6458efa5 2020-11-24 stsp
7325 c42c9805 2020-11-24 stsp *commit_id = NULL;
7326 6458efa5 2020-11-24 stsp
7327 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7328 6458efa5 2020-11-24 stsp if (err)
7329 6458efa5 2020-11-24 stsp return err;
7330 6458efa5 2020-11-24 stsp
7331 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7332 6458efa5 2020-11-24 stsp if (err)
7333 6458efa5 2020-11-24 stsp goto done;
7334 6458efa5 2020-11-24 stsp
7335 6458efa5 2020-11-24 stsp switch (obj_type) {
7336 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7337 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7338 6458efa5 2020-11-24 stsp break;
7339 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7340 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7341 6458efa5 2020-11-24 stsp if (err)
7342 6458efa5 2020-11-24 stsp goto done;
7343 c42c9805 2020-11-24 stsp free(obj_id);
7344 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7345 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7346 c42c9805 2020-11-24 stsp if (err)
7347 6458efa5 2020-11-24 stsp goto done;
7348 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7349 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7350 c42c9805 2020-11-24 stsp goto done;
7351 c42c9805 2020-11-24 stsp }
7352 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7353 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7354 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7355 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7356 c42c9805 2020-11-24 stsp goto done;
7357 c42c9805 2020-11-24 stsp }
7358 6458efa5 2020-11-24 stsp break;
7359 6458efa5 2020-11-24 stsp default:
7360 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7361 c42c9805 2020-11-24 stsp break;
7362 6458efa5 2020-11-24 stsp }
7363 6458efa5 2020-11-24 stsp
7364 c42c9805 2020-11-24 stsp done:
7365 c42c9805 2020-11-24 stsp if (tag)
7366 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7367 c42c9805 2020-11-24 stsp if (err) {
7368 c42c9805 2020-11-24 stsp free(*commit_id);
7369 c42c9805 2020-11-24 stsp *commit_id = NULL;
7370 c42c9805 2020-11-24 stsp }
7371 c42c9805 2020-11-24 stsp return err;
7372 c42c9805 2020-11-24 stsp }
7373 c42c9805 2020-11-24 stsp
7374 c42c9805 2020-11-24 stsp static const struct got_error *
7375 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7376 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7377 c42c9805 2020-11-24 stsp {
7378 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7379 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7380 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7381 c42c9805 2020-11-24 stsp
7382 c42c9805 2020-11-24 stsp *new_view = NULL;
7383 c42c9805 2020-11-24 stsp
7384 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7385 c42c9805 2020-11-24 stsp if (err) {
7386 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7387 c42c9805 2020-11-24 stsp return err;
7388 c42c9805 2020-11-24 stsp else
7389 c42c9805 2020-11-24 stsp return NULL;
7390 c42c9805 2020-11-24 stsp }
7391 c42c9805 2020-11-24 stsp
7392 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7393 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7394 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7395 6458efa5 2020-11-24 stsp goto done;
7396 6458efa5 2020-11-24 stsp }
7397 6458efa5 2020-11-24 stsp
7398 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7399 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7400 6458efa5 2020-11-24 stsp done:
7401 6458efa5 2020-11-24 stsp if (err)
7402 6458efa5 2020-11-24 stsp view_close(log_view);
7403 6458efa5 2020-11-24 stsp else
7404 6458efa5 2020-11-24 stsp *new_view = log_view;
7405 c42c9805 2020-11-24 stsp free(commit_id);
7406 6458efa5 2020-11-24 stsp return err;
7407 6458efa5 2020-11-24 stsp }
7408 6458efa5 2020-11-24 stsp
7409 ce5b7c56 2019-07-09 stsp static void
7410 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7411 6458efa5 2020-11-24 stsp {
7412 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7413 34ba6917 2020-11-30 stsp int i = 0;
7414 6458efa5 2020-11-24 stsp
7415 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7416 6458efa5 2020-11-24 stsp return;
7417 6458efa5 2020-11-24 stsp
7418 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7419 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7420 34ba6917 2020-11-30 stsp if (re == NULL)
7421 34ba6917 2020-11-30 stsp break;
7422 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7423 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7424 6458efa5 2020-11-24 stsp }
7425 6458efa5 2020-11-24 stsp }
7426 6458efa5 2020-11-24 stsp
7427 9b058f45 2022-06-30 mark static const struct got_error *
7428 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7429 6458efa5 2020-11-24 stsp {
7430 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7431 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7432 6458efa5 2020-11-24 stsp int n = 0;
7433 6458efa5 2020-11-24 stsp
7434 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7435 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7436 6458efa5 2020-11-24 stsp else
7437 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7438 6458efa5 2020-11-24 stsp
7439 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7440 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7441 9b058f45 2022-06-30 mark if (last)
7442 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7443 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7444 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7445 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7446 6458efa5 2020-11-24 stsp }
7447 6458efa5 2020-11-24 stsp }
7448 9b058f45 2022-06-30 mark
7449 9b058f45 2022-06-30 mark return NULL;
7450 6458efa5 2020-11-24 stsp }
7451 6458efa5 2020-11-24 stsp
7452 6458efa5 2020-11-24 stsp static const struct got_error *
7453 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7454 6458efa5 2020-11-24 stsp {
7455 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7456 6458efa5 2020-11-24 stsp
7457 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7458 6458efa5 2020-11-24 stsp return NULL;
7459 6458efa5 2020-11-24 stsp }
7460 6458efa5 2020-11-24 stsp
7461 6458efa5 2020-11-24 stsp static int
7462 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7463 6458efa5 2020-11-24 stsp {
7464 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7465 6458efa5 2020-11-24 stsp
7466 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7467 6458efa5 2020-11-24 stsp 0) == 0;
7468 6458efa5 2020-11-24 stsp }
7469 6458efa5 2020-11-24 stsp
7470 6458efa5 2020-11-24 stsp static const struct got_error *
7471 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7472 6458efa5 2020-11-24 stsp {
7473 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7474 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7475 6458efa5 2020-11-24 stsp
7476 6458efa5 2020-11-24 stsp if (!view->searching) {
7477 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7478 6458efa5 2020-11-24 stsp return NULL;
7479 6458efa5 2020-11-24 stsp }
7480 6458efa5 2020-11-24 stsp
7481 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7482 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7483 6458efa5 2020-11-24 stsp if (s->selected_entry)
7484 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7485 6458efa5 2020-11-24 stsp else
7486 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7487 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7488 6458efa5 2020-11-24 stsp } else {
7489 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7490 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7491 6458efa5 2020-11-24 stsp else
7492 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7493 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7494 6458efa5 2020-11-24 stsp }
7495 6458efa5 2020-11-24 stsp } else {
7496 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7497 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7498 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7499 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7500 6458efa5 2020-11-24 stsp else
7501 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7502 6458efa5 2020-11-24 stsp }
7503 6458efa5 2020-11-24 stsp
7504 6458efa5 2020-11-24 stsp while (1) {
7505 6458efa5 2020-11-24 stsp if (re == NULL) {
7506 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7507 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7508 6458efa5 2020-11-24 stsp return NULL;
7509 6458efa5 2020-11-24 stsp }
7510 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7511 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7512 6458efa5 2020-11-24 stsp else
7513 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7514 6458efa5 2020-11-24 stsp }
7515 6458efa5 2020-11-24 stsp
7516 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7517 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7518 6458efa5 2020-11-24 stsp s->matched_entry = re;
7519 6458efa5 2020-11-24 stsp break;
7520 6458efa5 2020-11-24 stsp }
7521 6458efa5 2020-11-24 stsp
7522 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7523 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7524 6458efa5 2020-11-24 stsp else
7525 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7526 6458efa5 2020-11-24 stsp }
7527 6458efa5 2020-11-24 stsp
7528 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7529 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7530 6458efa5 2020-11-24 stsp s->selected = 0;
7531 6458efa5 2020-11-24 stsp }
7532 6458efa5 2020-11-24 stsp
7533 6458efa5 2020-11-24 stsp return NULL;
7534 6458efa5 2020-11-24 stsp }
7535 6458efa5 2020-11-24 stsp
7536 6458efa5 2020-11-24 stsp static const struct got_error *
7537 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7538 6458efa5 2020-11-24 stsp {
7539 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7540 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7541 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7542 6458efa5 2020-11-24 stsp char *line = NULL;
7543 6458efa5 2020-11-24 stsp wchar_t *wline;
7544 6458efa5 2020-11-24 stsp struct tog_color *tc;
7545 6458efa5 2020-11-24 stsp int width, n;
7546 6458efa5 2020-11-24 stsp int limit = view->nlines;
7547 6458efa5 2020-11-24 stsp
7548 6458efa5 2020-11-24 stsp werase(view->window);
7549 6458efa5 2020-11-24 stsp
7550 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7551 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7552 9b058f45 2022-06-30 mark --limit; /* border */
7553 6458efa5 2020-11-24 stsp
7554 6458efa5 2020-11-24 stsp if (limit == 0)
7555 6458efa5 2020-11-24 stsp return NULL;
7556 6458efa5 2020-11-24 stsp
7557 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7558 6458efa5 2020-11-24 stsp
7559 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7560 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7561 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7562 6458efa5 2020-11-24 stsp
7563 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7564 6458efa5 2020-11-24 stsp if (err) {
7565 6458efa5 2020-11-24 stsp free(line);
7566 6458efa5 2020-11-24 stsp return err;
7567 6458efa5 2020-11-24 stsp }
7568 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7569 6458efa5 2020-11-24 stsp wstandout(view->window);
7570 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7571 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7572 6458efa5 2020-11-24 stsp wstandend(view->window);
7573 6458efa5 2020-11-24 stsp free(wline);
7574 6458efa5 2020-11-24 stsp wline = NULL;
7575 6458efa5 2020-11-24 stsp free(line);
7576 6458efa5 2020-11-24 stsp line = NULL;
7577 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7578 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7579 6458efa5 2020-11-24 stsp if (--limit <= 0)
7580 6458efa5 2020-11-24 stsp return NULL;
7581 6458efa5 2020-11-24 stsp
7582 6458efa5 2020-11-24 stsp n = 0;
7583 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7584 6458efa5 2020-11-24 stsp char *line = NULL;
7585 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7586 6458efa5 2020-11-24 stsp
7587 b4996bee 2022-06-16 stsp if (s->show_date) {
7588 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7589 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7590 b4996bee 2022-06-16 stsp struct got_object_id *id;
7591 b4996bee 2022-06-16 stsp struct tm tm;
7592 b4996bee 2022-06-16 stsp time_t t;
7593 b4996bee 2022-06-16 stsp
7594 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7595 b4996bee 2022-06-16 stsp if (err)
7596 b4996bee 2022-06-16 stsp return err;
7597 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7598 b4996bee 2022-06-16 stsp if (err) {
7599 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7600 b4996bee 2022-06-16 stsp free(id);
7601 b4996bee 2022-06-16 stsp return err;
7602 b4996bee 2022-06-16 stsp }
7603 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7604 b4996bee 2022-06-16 stsp id);
7605 b4996bee 2022-06-16 stsp if (err) {
7606 b4996bee 2022-06-16 stsp free(id);
7607 b4996bee 2022-06-16 stsp return err;
7608 b4996bee 2022-06-16 stsp }
7609 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7610 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7611 b4996bee 2022-06-16 stsp } else {
7612 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7613 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7614 b4996bee 2022-06-16 stsp }
7615 b4996bee 2022-06-16 stsp free(id);
7616 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7617 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7618 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7619 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7620 b4996bee 2022-06-16 stsp }
7621 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7622 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7623 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7624 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7625 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7626 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7627 6458efa5 2020-11-24 stsp struct got_object_id *id;
7628 6458efa5 2020-11-24 stsp char *id_str;
7629 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7630 6458efa5 2020-11-24 stsp if (err)
7631 6458efa5 2020-11-24 stsp return err;
7632 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7633 6458efa5 2020-11-24 stsp if (err) {
7634 6458efa5 2020-11-24 stsp free(id);
7635 6458efa5 2020-11-24 stsp return err;
7636 6458efa5 2020-11-24 stsp }
7637 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7638 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7639 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7640 6458efa5 2020-11-24 stsp free(id);
7641 6458efa5 2020-11-24 stsp free(id_str);
7642 6458efa5 2020-11-24 stsp return err;
7643 6458efa5 2020-11-24 stsp }
7644 6458efa5 2020-11-24 stsp free(id);
7645 6458efa5 2020-11-24 stsp free(id_str);
7646 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7647 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7648 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7649 6458efa5 2020-11-24 stsp
7650 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7651 ccda2f4d 2022-06-16 stsp 0, 0);
7652 6458efa5 2020-11-24 stsp if (err) {
7653 6458efa5 2020-11-24 stsp free(line);
7654 6458efa5 2020-11-24 stsp return err;
7655 6458efa5 2020-11-24 stsp }
7656 6458efa5 2020-11-24 stsp if (n == s->selected) {
7657 6458efa5 2020-11-24 stsp if (view->focussed)
7658 6458efa5 2020-11-24 stsp wstandout(view->window);
7659 6458efa5 2020-11-24 stsp s->selected_entry = re;
7660 6458efa5 2020-11-24 stsp }
7661 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7662 6458efa5 2020-11-24 stsp if (tc)
7663 6458efa5 2020-11-24 stsp wattr_on(view->window,
7664 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7665 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7666 6458efa5 2020-11-24 stsp if (tc)
7667 6458efa5 2020-11-24 stsp wattr_off(view->window,
7668 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7669 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7670 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7671 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7672 6458efa5 2020-11-24 stsp wstandend(view->window);
7673 6458efa5 2020-11-24 stsp free(line);
7674 6458efa5 2020-11-24 stsp free(wline);
7675 6458efa5 2020-11-24 stsp wline = NULL;
7676 6458efa5 2020-11-24 stsp n++;
7677 6458efa5 2020-11-24 stsp s->ndisplayed++;
7678 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7679 6458efa5 2020-11-24 stsp
7680 6458efa5 2020-11-24 stsp limit--;
7681 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7682 6458efa5 2020-11-24 stsp }
7683 6458efa5 2020-11-24 stsp
7684 9b058f45 2022-06-30 mark view_border(view);
7685 6458efa5 2020-11-24 stsp return err;
7686 6458efa5 2020-11-24 stsp }
7687 6458efa5 2020-11-24 stsp
7688 6458efa5 2020-11-24 stsp static const struct got_error *
7689 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7690 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7691 c42c9805 2020-11-24 stsp {
7692 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7693 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7694 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7695 c42c9805 2020-11-24 stsp
7696 c42c9805 2020-11-24 stsp *new_view = NULL;
7697 c42c9805 2020-11-24 stsp
7698 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7699 c42c9805 2020-11-24 stsp if (err) {
7700 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7701 c42c9805 2020-11-24 stsp return err;
7702 c42c9805 2020-11-24 stsp else
7703 c42c9805 2020-11-24 stsp return NULL;
7704 c42c9805 2020-11-24 stsp }
7705 c42c9805 2020-11-24 stsp
7706 c42c9805 2020-11-24 stsp
7707 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7708 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7709 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7710 c42c9805 2020-11-24 stsp goto done;
7711 c42c9805 2020-11-24 stsp }
7712 c42c9805 2020-11-24 stsp
7713 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7714 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7715 c42c9805 2020-11-24 stsp if (err)
7716 c42c9805 2020-11-24 stsp goto done;
7717 c42c9805 2020-11-24 stsp
7718 c42c9805 2020-11-24 stsp *new_view = tree_view;
7719 c42c9805 2020-11-24 stsp done:
7720 c42c9805 2020-11-24 stsp free(commit_id);
7721 c42c9805 2020-11-24 stsp return err;
7722 c42c9805 2020-11-24 stsp }
7723 c42c9805 2020-11-24 stsp static const struct got_error *
7724 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7725 6458efa5 2020-11-24 stsp {
7726 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7727 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7728 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7729 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7730 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7731 6458efa5 2020-11-24 stsp
7732 6458efa5 2020-11-24 stsp switch (ch) {
7733 6458efa5 2020-11-24 stsp case 'i':
7734 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7735 640cd7ff 2022-06-22 mark view->count = 0;
7736 7f66531d 2021-11-16 stsp break;
7737 b4996bee 2022-06-16 stsp case 'm':
7738 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7739 640cd7ff 2022-06-22 mark view->count = 0;
7740 b4996bee 2022-06-16 stsp break;
7741 07a065fe 2021-11-20 stsp case 'o':
7742 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7743 640cd7ff 2022-06-22 mark view->count = 0;
7744 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7745 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7746 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7747 50617b77 2021-11-20 stsp if (err)
7748 50617b77 2021-11-20 stsp break;
7749 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7750 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7751 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7752 7f66531d 2021-11-16 stsp if (err)
7753 7f66531d 2021-11-16 stsp break;
7754 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7755 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7756 6458efa5 2020-11-24 stsp break;
7757 6458efa5 2020-11-24 stsp case KEY_ENTER:
7758 6458efa5 2020-11-24 stsp case '\r':
7759 640cd7ff 2022-06-22 mark view->count = 0;
7760 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7761 6458efa5 2020-11-24 stsp break;
7762 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7763 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7764 9b058f45 2022-06-30 mark
7765 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7766 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7767 9b058f45 2022-06-30 mark if (err)
7768 9b058f45 2022-06-30 mark break;
7769 9b058f45 2022-06-30 mark
7770 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7771 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7772 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7773 9b058f45 2022-06-30 mark if (err)
7774 9b058f45 2022-06-30 mark break;
7775 9b058f45 2022-06-30 mark }
7776 9b058f45 2022-06-30 mark
7777 e78dc838 2020-12-04 stsp view->focussed = 0;
7778 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7779 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7780 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7781 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7782 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
7783 6458efa5 2020-11-24 stsp err = view_close_child(view);
7784 6458efa5 2020-11-24 stsp if (err)
7785 6458efa5 2020-11-24 stsp return err;
7786 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7787 0dbbbe90 2022-06-17 op if (err)
7788 0dbbbe90 2022-06-17 op return err;
7789 e78dc838 2020-12-04 stsp view->focus_child = 1;
7790 6458efa5 2020-11-24 stsp } else
7791 6458efa5 2020-11-24 stsp *new_view = log_view;
7792 6458efa5 2020-11-24 stsp break;
7793 c42c9805 2020-11-24 stsp case 't':
7794 640cd7ff 2022-06-22 mark view->count = 0;
7795 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7796 c42c9805 2020-11-24 stsp break;
7797 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7798 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7799 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7800 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7801 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7802 c42c9805 2020-11-24 stsp break;
7803 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7804 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7805 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7806 49b24ee5 2022-07-03 mark if (err)
7807 49b24ee5 2022-07-03 mark break;
7808 49b24ee5 2022-07-03 mark }
7809 e78dc838 2020-12-04 stsp view->focussed = 0;
7810 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7811 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7812 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7813 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7814 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
7815 c42c9805 2020-11-24 stsp err = view_close_child(view);
7816 c42c9805 2020-11-24 stsp if (err)
7817 c42c9805 2020-11-24 stsp return err;
7818 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7819 0dbbbe90 2022-06-17 op if (err)
7820 0dbbbe90 2022-06-17 op return err;
7821 e78dc838 2020-12-04 stsp view->focus_child = 1;
7822 c42c9805 2020-11-24 stsp } else
7823 c42c9805 2020-11-24 stsp *new_view = tree_view;
7824 c42c9805 2020-11-24 stsp break;
7825 e4526bf5 2021-09-03 naddy case 'g':
7826 e4526bf5 2021-09-03 naddy case KEY_HOME:
7827 e4526bf5 2021-09-03 naddy s->selected = 0;
7828 640cd7ff 2022-06-22 mark view->count = 0;
7829 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7830 e4526bf5 2021-09-03 naddy break;
7831 e4526bf5 2021-09-03 naddy case 'G':
7832 9b058f45 2022-06-30 mark case KEY_END: {
7833 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7834 9b058f45 2022-06-30 mark
7835 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7836 9b058f45 2022-06-30 mark --eos; /* border */
7837 e4526bf5 2021-09-03 naddy s->selected = 0;
7838 640cd7ff 2022-06-22 mark view->count = 0;
7839 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7840 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7841 e4526bf5 2021-09-03 naddy if (re == NULL)
7842 e4526bf5 2021-09-03 naddy break;
7843 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7844 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7845 e4526bf5 2021-09-03 naddy }
7846 e4526bf5 2021-09-03 naddy if (n > 0)
7847 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7848 e4526bf5 2021-09-03 naddy break;
7849 9b058f45 2022-06-30 mark }
7850 6458efa5 2020-11-24 stsp case 'k':
7851 6458efa5 2020-11-24 stsp case KEY_UP:
7852 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7853 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7854 6458efa5 2020-11-24 stsp s->selected--;
7855 6458efa5 2020-11-24 stsp break;
7856 34ba6917 2020-11-30 stsp }
7857 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7858 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7859 640cd7ff 2022-06-22 mark view->count = 0;
7860 6458efa5 2020-11-24 stsp break;
7861 83cc4199 2022-06-13 stsp case CTRL('u'):
7862 33c3719a 2022-06-15 stsp case 'u':
7863 83cc4199 2022-06-13 stsp nscroll /= 2;
7864 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7865 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7866 6458efa5 2020-11-24 stsp case CTRL('b'):
7867 61417565 2022-06-20 mark case 'b':
7868 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7869 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7870 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7871 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7872 640cd7ff 2022-06-22 mark view->count = 0;
7873 6458efa5 2020-11-24 stsp break;
7874 6458efa5 2020-11-24 stsp case 'j':
7875 6458efa5 2020-11-24 stsp case KEY_DOWN:
7876 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7877 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7878 6458efa5 2020-11-24 stsp s->selected++;
7879 6458efa5 2020-11-24 stsp break;
7880 6458efa5 2020-11-24 stsp }
7881 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7882 6458efa5 2020-11-24 stsp /* can't scroll any further */
7883 640cd7ff 2022-06-22 mark view->count = 0;
7884 6458efa5 2020-11-24 stsp break;
7885 640cd7ff 2022-06-22 mark }
7886 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7887 6458efa5 2020-11-24 stsp break;
7888 83cc4199 2022-06-13 stsp case CTRL('d'):
7889 33c3719a 2022-06-15 stsp case 'd':
7890 83cc4199 2022-06-13 stsp nscroll /= 2;
7891 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7892 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7893 6458efa5 2020-11-24 stsp case CTRL('f'):
7894 61417565 2022-06-20 mark case 'f':
7895 48bb96f0 2022-06-20 naddy case ' ':
7896 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7897 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7898 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7899 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7900 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7901 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7902 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7903 640cd7ff 2022-06-22 mark view->count = 0;
7904 6458efa5 2020-11-24 stsp break;
7905 6458efa5 2020-11-24 stsp }
7906 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7907 6458efa5 2020-11-24 stsp break;
7908 6458efa5 2020-11-24 stsp case CTRL('l'):
7909 640cd7ff 2022-06-22 mark view->count = 0;
7910 8924d611 2020-12-26 stsp tog_free_refs();
7911 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7912 8924d611 2020-12-26 stsp if (err)
7913 8924d611 2020-12-26 stsp break;
7914 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7915 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7916 6458efa5 2020-11-24 stsp break;
7917 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7918 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7919 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7920 6458efa5 2020-11-24 stsp break;
7921 6458efa5 2020-11-24 stsp default:
7922 640cd7ff 2022-06-22 mark view->count = 0;
7923 6458efa5 2020-11-24 stsp break;
7924 6458efa5 2020-11-24 stsp }
7925 6458efa5 2020-11-24 stsp
7926 6458efa5 2020-11-24 stsp return err;
7927 6458efa5 2020-11-24 stsp }
7928 6458efa5 2020-11-24 stsp
7929 6458efa5 2020-11-24 stsp __dead static void
7930 6458efa5 2020-11-24 stsp usage_ref(void)
7931 6458efa5 2020-11-24 stsp {
7932 6458efa5 2020-11-24 stsp endwin();
7933 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7934 6458efa5 2020-11-24 stsp getprogname());
7935 6458efa5 2020-11-24 stsp exit(1);
7936 6458efa5 2020-11-24 stsp }
7937 6458efa5 2020-11-24 stsp
7938 6458efa5 2020-11-24 stsp static const struct got_error *
7939 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7940 6458efa5 2020-11-24 stsp {
7941 6458efa5 2020-11-24 stsp const struct got_error *error;
7942 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7943 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7944 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7945 6458efa5 2020-11-24 stsp int ch;
7946 6458efa5 2020-11-24 stsp struct tog_view *view;
7947 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7948 6458efa5 2020-11-24 stsp
7949 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7950 6458efa5 2020-11-24 stsp switch (ch) {
7951 6458efa5 2020-11-24 stsp case 'r':
7952 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7953 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7954 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7955 6458efa5 2020-11-24 stsp optarg);
7956 6458efa5 2020-11-24 stsp break;
7957 6458efa5 2020-11-24 stsp default:
7958 e99e2d15 2020-11-24 naddy usage_ref();
7959 6458efa5 2020-11-24 stsp /* NOTREACHED */
7960 6458efa5 2020-11-24 stsp }
7961 6458efa5 2020-11-24 stsp }
7962 6458efa5 2020-11-24 stsp
7963 6458efa5 2020-11-24 stsp argc -= optind;
7964 6458efa5 2020-11-24 stsp argv += optind;
7965 6458efa5 2020-11-24 stsp
7966 6458efa5 2020-11-24 stsp if (argc > 1)
7967 e99e2d15 2020-11-24 naddy usage_ref();
7968 0ae84acc 2022-06-15 tracey
7969 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7970 0ae84acc 2022-06-15 tracey if (error != NULL)
7971 0ae84acc 2022-06-15 tracey goto done;
7972 6458efa5 2020-11-24 stsp
7973 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7974 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7975 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7976 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7977 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7978 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7979 c156c7a4 2020-12-18 stsp goto done;
7980 6458efa5 2020-11-24 stsp if (worktree)
7981 6458efa5 2020-11-24 stsp repo_path =
7982 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7983 6458efa5 2020-11-24 stsp else
7984 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7985 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7986 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7987 c156c7a4 2020-12-18 stsp goto done;
7988 c156c7a4 2020-12-18 stsp }
7989 6458efa5 2020-11-24 stsp }
7990 6458efa5 2020-11-24 stsp
7991 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7992 6458efa5 2020-11-24 stsp if (error != NULL)
7993 6458efa5 2020-11-24 stsp goto done;
7994 6458efa5 2020-11-24 stsp
7995 6458efa5 2020-11-24 stsp init_curses();
7996 6458efa5 2020-11-24 stsp
7997 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7998 51a10b52 2020-12-26 stsp if (error)
7999 51a10b52 2020-12-26 stsp goto done;
8000 51a10b52 2020-12-26 stsp
8001 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8002 6458efa5 2020-11-24 stsp if (error)
8003 6458efa5 2020-11-24 stsp goto done;
8004 6458efa5 2020-11-24 stsp
8005 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8006 6458efa5 2020-11-24 stsp if (view == NULL) {
8007 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8008 6458efa5 2020-11-24 stsp goto done;
8009 6458efa5 2020-11-24 stsp }
8010 6458efa5 2020-11-24 stsp
8011 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8012 6458efa5 2020-11-24 stsp if (error)
8013 6458efa5 2020-11-24 stsp goto done;
8014 6458efa5 2020-11-24 stsp
8015 6458efa5 2020-11-24 stsp if (worktree) {
8016 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8017 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8018 6458efa5 2020-11-24 stsp worktree = NULL;
8019 6458efa5 2020-11-24 stsp }
8020 6458efa5 2020-11-24 stsp error = view_loop(view);
8021 6458efa5 2020-11-24 stsp done:
8022 6458efa5 2020-11-24 stsp free(repo_path);
8023 6458efa5 2020-11-24 stsp free(cwd);
8024 1d0f4054 2021-06-17 stsp if (repo) {
8025 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8026 1d0f4054 2021-06-17 stsp if (close_err)
8027 1d0f4054 2021-06-17 stsp error = close_err;
8028 1d0f4054 2021-06-17 stsp }
8029 0ae84acc 2022-06-15 tracey if (pack_fds) {
8030 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8031 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8032 0ae84acc 2022-06-15 tracey if (error == NULL)
8033 0ae84acc 2022-06-15 tracey error = pack_err;
8034 0ae84acc 2022-06-15 tracey }
8035 51a10b52 2020-12-26 stsp tog_free_refs();
8036 6458efa5 2020-11-24 stsp return error;
8037 6458efa5 2020-11-24 stsp }
8038 6458efa5 2020-11-24 stsp
8039 9b058f45 2022-06-30 mark /*
8040 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8041 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8042 9b058f45 2022-06-30 mark */
8043 6458efa5 2020-11-24 stsp static void
8044 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8045 9b058f45 2022-06-30 mark {
8046 9b058f45 2022-06-30 mark switch (view->type) {
8047 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8048 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8049 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8050 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8051 9b058f45 2022-06-30 mark 1);
8052 9b058f45 2022-06-30 mark break;
8053 9b058f45 2022-06-30 mark }
8054 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8055 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8056 9b058f45 2022-06-30 mark else
8057 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8058 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8059 9b058f45 2022-06-30 mark break;
8060 9b058f45 2022-06-30 mark }
8061 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8062 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8063 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8064 9b058f45 2022-06-30 mark break;
8065 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8066 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8067 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8068 9b058f45 2022-06-30 mark break;
8069 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8070 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8071 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8072 9b058f45 2022-06-30 mark break;
8073 9b058f45 2022-06-30 mark default:
8074 9b058f45 2022-06-30 mark break;
8075 9b058f45 2022-06-30 mark }
8076 9b058f45 2022-06-30 mark
8077 9b058f45 2022-06-30 mark view->offset = 0;
8078 9b058f45 2022-06-30 mark }
8079 9b058f45 2022-06-30 mark
8080 9b058f45 2022-06-30 mark /*
8081 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8082 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8083 9b058f45 2022-06-30 mark */
8084 9b058f45 2022-06-30 mark static const struct got_error *
8085 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8086 9b058f45 2022-06-30 mark {
8087 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8088 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8089 9b058f45 2022-06-30 mark int *selected = NULL;
8090 9b058f45 2022-06-30 mark int header, offset;
8091 9b058f45 2022-06-30 mark
8092 9b058f45 2022-06-30 mark switch (view->type) {
8093 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8094 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8095 9b058f45 2022-06-30 mark header = 3;
8096 9b058f45 2022-06-30 mark scrolld = NULL;
8097 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8098 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8099 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8100 9b058f45 2022-06-30 mark s->selected_line -= offset;
8101 9b058f45 2022-06-30 mark view->offset = offset;
8102 9b058f45 2022-06-30 mark }
8103 9b058f45 2022-06-30 mark break;
8104 9b058f45 2022-06-30 mark }
8105 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8106 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8107 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8108 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8109 9b058f45 2022-06-30 mark selected = &s->selected;
8110 9b058f45 2022-06-30 mark break;
8111 9b058f45 2022-06-30 mark }
8112 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8113 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8114 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8115 9b058f45 2022-06-30 mark header = 3;
8116 9b058f45 2022-06-30 mark selected = &s->selected;
8117 9b058f45 2022-06-30 mark break;
8118 9b058f45 2022-06-30 mark }
8119 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8120 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8121 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8122 9b058f45 2022-06-30 mark header = 5;
8123 9b058f45 2022-06-30 mark selected = &s->selected;
8124 9b058f45 2022-06-30 mark break;
8125 9b058f45 2022-06-30 mark }
8126 9b058f45 2022-06-30 mark default:
8127 9b058f45 2022-06-30 mark selected = NULL;
8128 9b058f45 2022-06-30 mark scrolld = NULL;
8129 9b058f45 2022-06-30 mark header = 0;
8130 9b058f45 2022-06-30 mark break;
8131 9b058f45 2022-06-30 mark }
8132 9b058f45 2022-06-30 mark
8133 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8134 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8135 9b058f45 2022-06-30 mark view->offset = offset;
8136 9b058f45 2022-06-30 mark if (scrolld && offset) {
8137 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8138 9b058f45 2022-06-30 mark *selected -= offset;
8139 9b058f45 2022-06-30 mark }
8140 9b058f45 2022-06-30 mark }
8141 9b058f45 2022-06-30 mark
8142 9b058f45 2022-06-30 mark return err;
8143 9b058f45 2022-06-30 mark }
8144 9b058f45 2022-06-30 mark
8145 9b058f45 2022-06-30 mark static void
8146 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8147 ce5b7c56 2019-07-09 stsp {
8148 6059809a 2020-12-17 stsp size_t i;
8149 9f7d7167 2018-04-29 stsp
8150 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8151 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8152 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8153 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8154 ce5b7c56 2019-07-09 stsp }
8155 6879ba42 2020-10-01 naddy fputc('\n', fp);
8156 ce5b7c56 2019-07-09 stsp }
8157 ce5b7c56 2019-07-09 stsp
8158 4ed7e80c 2018-05-20 stsp __dead static void
8159 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8160 9f7d7167 2018-04-29 stsp {
8161 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8162 6879ba42 2020-10-01 naddy
8163 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8164 6879ba42 2020-10-01 naddy getprogname());
8165 6879ba42 2020-10-01 naddy if (hflag) {
8166 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8167 6879ba42 2020-10-01 naddy list_commands(fp);
8168 ee85c5e8 2020-02-29 stsp }
8169 6879ba42 2020-10-01 naddy exit(status);
8170 9f7d7167 2018-04-29 stsp }
8171 9f7d7167 2018-04-29 stsp
8172 c2301be8 2018-04-30 stsp static char **
8173 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8174 c2301be8 2018-04-30 stsp {
8175 ee85c5e8 2020-02-29 stsp va_list ap;
8176 c2301be8 2018-04-30 stsp char **argv;
8177 ee85c5e8 2020-02-29 stsp int i;
8178 c2301be8 2018-04-30 stsp
8179 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8180 ee85c5e8 2020-02-29 stsp
8181 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8182 c2301be8 2018-04-30 stsp if (argv == NULL)
8183 c2301be8 2018-04-30 stsp err(1, "calloc");
8184 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8185 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8186 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8187 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8188 c2301be8 2018-04-30 stsp }
8189 c2301be8 2018-04-30 stsp
8190 ee85c5e8 2020-02-29 stsp va_end(ap);
8191 c2301be8 2018-04-30 stsp return argv;
8192 ee85c5e8 2020-02-29 stsp }
8193 ee85c5e8 2020-02-29 stsp
8194 ee85c5e8 2020-02-29 stsp /*
8195 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8196 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8197 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8198 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8199 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8200 ee85c5e8 2020-02-29 stsp */
8201 ee85c5e8 2020-02-29 stsp static const struct got_error *
8202 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8203 ee85c5e8 2020-02-29 stsp {
8204 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8205 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8206 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8207 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8208 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8209 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8210 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8211 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8212 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8213 84de9106 2020-12-26 stsp
8214 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8215 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8216 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8217 ee85c5e8 2020-02-29 stsp
8218 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8219 0ae84acc 2022-06-15 tracey if (error != NULL)
8220 0ae84acc 2022-06-15 tracey goto done;
8221 0ae84acc 2022-06-15 tracey
8222 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8223 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8224 ee85c5e8 2020-02-29 stsp goto done;
8225 ee85c5e8 2020-02-29 stsp
8226 ee85c5e8 2020-02-29 stsp if (worktree)
8227 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8228 ee85c5e8 2020-02-29 stsp else
8229 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8230 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8231 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8232 ee85c5e8 2020-02-29 stsp goto done;
8233 ee85c5e8 2020-02-29 stsp }
8234 ee85c5e8 2020-02-29 stsp
8235 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8236 ee85c5e8 2020-02-29 stsp if (error != NULL)
8237 ee85c5e8 2020-02-29 stsp goto done;
8238 ee85c5e8 2020-02-29 stsp
8239 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8240 ee85c5e8 2020-02-29 stsp repo, worktree);
8241 ee85c5e8 2020-02-29 stsp if (error)
8242 ee85c5e8 2020-02-29 stsp goto done;
8243 ee85c5e8 2020-02-29 stsp
8244 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8245 84de9106 2020-12-26 stsp if (error)
8246 84de9106 2020-12-26 stsp goto done;
8247 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8248 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8249 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8250 ee85c5e8 2020-02-29 stsp if (error)
8251 ee85c5e8 2020-02-29 stsp goto done;
8252 ee85c5e8 2020-02-29 stsp
8253 ee85c5e8 2020-02-29 stsp if (worktree) {
8254 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8255 ee85c5e8 2020-02-29 stsp worktree = NULL;
8256 ee85c5e8 2020-02-29 stsp }
8257 ee85c5e8 2020-02-29 stsp
8258 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8259 a44927cc 2022-04-07 stsp if (error)
8260 a44927cc 2022-04-07 stsp goto done;
8261 a44927cc 2022-04-07 stsp
8262 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8263 ee85c5e8 2020-02-29 stsp if (error) {
8264 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8265 ee85c5e8 2020-02-29 stsp goto done;
8266 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8267 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8268 6879ba42 2020-10-01 naddy usage(1, 1);
8269 ee85c5e8 2020-02-29 stsp /* not reached */
8270 ee85c5e8 2020-02-29 stsp }
8271 ee85c5e8 2020-02-29 stsp
8272 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8273 1d0f4054 2021-06-17 stsp if (error == NULL)
8274 1d0f4054 2021-06-17 stsp error = close_err;
8275 ee85c5e8 2020-02-29 stsp repo = NULL;
8276 ee85c5e8 2020-02-29 stsp
8277 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8278 ee85c5e8 2020-02-29 stsp if (error)
8279 ee85c5e8 2020-02-29 stsp goto done;
8280 ee85c5e8 2020-02-29 stsp
8281 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8282 ee85c5e8 2020-02-29 stsp argc = 4;
8283 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8284 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8285 ee85c5e8 2020-02-29 stsp done:
8286 1d0f4054 2021-06-17 stsp if (repo) {
8287 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8288 1d0f4054 2021-06-17 stsp if (error == NULL)
8289 1d0f4054 2021-06-17 stsp error = close_err;
8290 1d0f4054 2021-06-17 stsp }
8291 a44927cc 2022-04-07 stsp if (commit)
8292 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8293 ee85c5e8 2020-02-29 stsp if (worktree)
8294 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8295 0ae84acc 2022-06-15 tracey if (pack_fds) {
8296 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8297 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8298 0ae84acc 2022-06-15 tracey if (error == NULL)
8299 0ae84acc 2022-06-15 tracey error = pack_err;
8300 0ae84acc 2022-06-15 tracey }
8301 ee85c5e8 2020-02-29 stsp free(id);
8302 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8303 ee85c5e8 2020-02-29 stsp free(commit_id);
8304 ee85c5e8 2020-02-29 stsp free(cwd);
8305 ee85c5e8 2020-02-29 stsp free(repo_path);
8306 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8307 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8308 ee85c5e8 2020-02-29 stsp int i;
8309 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8310 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8311 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8312 ee85c5e8 2020-02-29 stsp }
8313 87670572 2020-12-26 naddy tog_free_refs();
8314 ee85c5e8 2020-02-29 stsp return error;
8315 c2301be8 2018-04-30 stsp }
8316 c2301be8 2018-04-30 stsp
8317 9f7d7167 2018-04-29 stsp int
8318 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8319 9f7d7167 2018-04-29 stsp {
8320 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8321 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8322 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8323 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8324 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8325 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8326 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8327 83cd27f8 2020-01-13 stsp };
8328 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8329 9f7d7167 2018-04-29 stsp
8330 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8331 9f7d7167 2018-04-29 stsp
8332 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8333 9f7d7167 2018-04-29 stsp switch (ch) {
8334 9f7d7167 2018-04-29 stsp case 'h':
8335 9f7d7167 2018-04-29 stsp hflag = 1;
8336 9f7d7167 2018-04-29 stsp break;
8337 53ccebc2 2019-07-30 stsp case 'V':
8338 53ccebc2 2019-07-30 stsp Vflag = 1;
8339 53ccebc2 2019-07-30 stsp break;
8340 9f7d7167 2018-04-29 stsp default:
8341 6879ba42 2020-10-01 naddy usage(hflag, 1);
8342 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8343 9f7d7167 2018-04-29 stsp }
8344 9f7d7167 2018-04-29 stsp }
8345 9f7d7167 2018-04-29 stsp
8346 9f7d7167 2018-04-29 stsp argc -= optind;
8347 9f7d7167 2018-04-29 stsp argv += optind;
8348 9814e6a3 2020-09-27 naddy optind = 1;
8349 c2301be8 2018-04-30 stsp optreset = 1;
8350 9f7d7167 2018-04-29 stsp
8351 53ccebc2 2019-07-30 stsp if (Vflag) {
8352 53ccebc2 2019-07-30 stsp got_version_print_str();
8353 6879ba42 2020-10-01 naddy return 0;
8354 53ccebc2 2019-07-30 stsp }
8355 4010e238 2020-12-04 stsp
8356 4010e238 2020-12-04 stsp #ifndef PROFILE
8357 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8358 4010e238 2020-12-04 stsp NULL) == -1)
8359 4010e238 2020-12-04 stsp err(1, "pledge");
8360 4010e238 2020-12-04 stsp #endif
8361 53ccebc2 2019-07-30 stsp
8362 c2301be8 2018-04-30 stsp if (argc == 0) {
8363 f29d3e89 2018-06-23 stsp if (hflag)
8364 6879ba42 2020-10-01 naddy usage(hflag, 0);
8365 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8366 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8367 c2301be8 2018-04-30 stsp argc = 1;
8368 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8369 c2301be8 2018-04-30 stsp } else {
8370 6059809a 2020-12-17 stsp size_t i;
8371 9f7d7167 2018-04-29 stsp
8372 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8373 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8374 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8375 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8376 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8377 9f7d7167 2018-04-29 stsp break;
8378 9f7d7167 2018-04-29 stsp }
8379 9f7d7167 2018-04-29 stsp }
8380 ee85c5e8 2020-02-29 stsp }
8381 3642c4c6 2019-07-09 stsp
8382 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8383 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8384 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8385 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8386 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8387 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8388 917d79a7 2022-07-01 stsp }
8389 917d79a7 2022-07-01 stsp
8390 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8391 ee85c5e8 2020-02-29 stsp if (argc != 1)
8392 6879ba42 2020-10-01 naddy usage(0, 1);
8393 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8394 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8395 ee85c5e8 2020-02-29 stsp } else {
8396 ee85c5e8 2020-02-29 stsp if (hflag)
8397 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8398 ee85c5e8 2020-02-29 stsp else
8399 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8400 9f7d7167 2018-04-29 stsp }
8401 9f7d7167 2018-04-29 stsp
8402 9f7d7167 2018-04-29 stsp endwin();
8403 b46c1e04 2020-09-20 naddy putchar('\n');
8404 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8405 a2f4a359 2020-02-28 stsp int i;
8406 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8407 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8408 a2f4a359 2020-02-28 stsp free(cmd_argv);
8409 a2f4a359 2020-02-28 stsp }
8410 a2f4a359 2020-02-28 stsp
8411 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8412 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8413 9f7d7167 2018-04-29 stsp return 0;
8414 9f7d7167 2018-04-29 stsp }