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 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
927 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
928 6fe51fee 2022-07-22 mark int n = 0;
929 571ccd73 2022-07-19 mark
930 6fe51fee 2022-07-22 mark if (s->selected_entry)
931 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
932 6fe51fee 2022-07-22 mark
933 571ccd73 2022-07-19 mark /*
934 571ccd73 2022-07-19 mark * Request commits to account for the increased
935 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
936 571ccd73 2022-07-19 mark */
937 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
938 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
939 571ccd73 2022-07-19 mark err = request_log_commits(view);
940 571ccd73 2022-07-19 mark }
941 571ccd73 2022-07-19 mark
942 571ccd73 2022-07-19 mark return err;
943 d9a7ab53 2022-07-11 mark }
944 d9a7ab53 2022-07-11 mark
945 d9a7ab53 2022-07-11 mark static void
946 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
947 d9a7ab53 2022-07-11 mark {
948 d9a7ab53 2022-07-11 mark if (n == 0)
949 d9a7ab53 2022-07-11 mark return;
950 d9a7ab53 2022-07-11 mark
951 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
952 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
953 d9a7ab53 2022-07-11 mark view->parent->offset += n;
954 d9a7ab53 2022-07-11 mark else
955 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
956 d9a7ab53 2022-07-11 mark } else if (view->offset) {
957 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
958 d9a7ab53 2022-07-11 mark view->offset -= n;
959 d9a7ab53 2022-07-11 mark else
960 d9a7ab53 2022-07-11 mark view->offset = 0;
961 d9a7ab53 2022-07-11 mark }
962 3c1dfe12 2022-07-08 mark }
963 3c1dfe12 2022-07-08 mark
964 3c1dfe12 2022-07-08 mark static const struct got_error *
965 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
966 3c1dfe12 2022-07-08 mark {
967 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
968 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
969 3c1dfe12 2022-07-08 mark
970 3c1dfe12 2022-07-08 mark if (view->parent)
971 3c1dfe12 2022-07-08 mark v = view->parent;
972 3c1dfe12 2022-07-08 mark else
973 3c1dfe12 2022-07-08 mark v = view;
974 3c1dfe12 2022-07-08 mark
975 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
976 3c1dfe12 2022-07-08 mark return NULL;
977 3c1dfe12 2022-07-08 mark
978 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
979 3c1dfe12 2022-07-08 mark
980 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
981 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
982 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
983 3c1dfe12 2022-07-08 mark if (view->parent)
984 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
985 3c1dfe12 2022-07-08 mark else
986 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
987 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
988 3c1dfe12 2022-07-08 mark view->count = 0;
989 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
990 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
991 3c1dfe12 2022-07-08 mark view->count = 0;
992 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
993 3c1dfe12 2022-07-08 mark }
994 3c1dfe12 2022-07-08 mark v->ncols = COLS;
995 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
996 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
997 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
998 3c1dfe12 2022-07-08 mark if (err)
999 3c1dfe12 2022-07-08 mark return err;
1000 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1001 3c1dfe12 2022-07-08 mark } else {
1002 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1003 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1004 3c1dfe12 2022-07-08 mark if (view->parent)
1005 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1006 3c1dfe12 2022-07-08 mark else
1007 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1008 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1009 3c1dfe12 2022-07-08 mark view->count = 0;
1010 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1011 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1012 3c1dfe12 2022-07-08 mark view->count = 0;
1013 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1014 3c1dfe12 2022-07-08 mark }
1015 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1016 3c1dfe12 2022-07-08 mark }
1017 3c1dfe12 2022-07-08 mark
1018 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1019 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1020 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1021 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1022 3c1dfe12 2022-07-08 mark
1023 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1024 3c1dfe12 2022-07-08 mark if (err)
1025 3c1dfe12 2022-07-08 mark return err;
1026 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1027 3c1dfe12 2022-07-08 mark if (err)
1028 3c1dfe12 2022-07-08 mark return err;
1029 3c1dfe12 2022-07-08 mark
1030 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1031 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1032 3c1dfe12 2022-07-08 mark if (err)
1033 3c1dfe12 2022-07-08 mark return err;
1034 3c1dfe12 2022-07-08 mark }
1035 3c1dfe12 2022-07-08 mark
1036 6fe51fee 2022-07-22 mark if (v->resize)
1037 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1038 6fe51fee 2022-07-22 mark else if (v->child->resize)
1039 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1040 3c1dfe12 2022-07-08 mark
1041 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1042 3c1dfe12 2022-07-08 mark
1043 3c1dfe12 2022-07-08 mark return err;
1044 3c1dfe12 2022-07-08 mark }
1045 3c1dfe12 2022-07-08 mark
1046 3c1dfe12 2022-07-08 mark static void
1047 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1048 3c1dfe12 2022-07-08 mark {
1049 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1050 3c1dfe12 2022-07-08 mark
1051 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1052 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1053 669b5ffa 2018-10-07 stsp }
1054 669b5ffa 2018-10-07 stsp
1055 669b5ffa 2018-10-07 stsp static const struct got_error *
1056 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1057 669b5ffa 2018-10-07 stsp {
1058 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1059 669b5ffa 2018-10-07 stsp
1060 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1061 669b5ffa 2018-10-07 stsp return NULL;
1062 669b5ffa 2018-10-07 stsp
1063 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1064 669b5ffa 2018-10-07 stsp view->child = NULL;
1065 669b5ffa 2018-10-07 stsp return err;
1066 669b5ffa 2018-10-07 stsp }
1067 669b5ffa 2018-10-07 stsp
1068 0dbbbe90 2022-06-17 op static const struct got_error *
1069 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1070 669b5ffa 2018-10-07 stsp {
1071 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1072 3c1dfe12 2022-07-08 mark
1073 669b5ffa 2018-10-07 stsp view->child = child;
1074 669b5ffa 2018-10-07 stsp child->parent = view;
1075 0dbbbe90 2022-06-17 op
1076 3c1dfe12 2022-07-08 mark err = view_resize(view);
1077 3c1dfe12 2022-07-08 mark if (err)
1078 3c1dfe12 2022-07-08 mark return err;
1079 3c1dfe12 2022-07-08 mark
1080 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1081 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1082 3c1dfe12 2022-07-08 mark
1083 3c1dfe12 2022-07-08 mark return err;
1084 bfddd0d9 2018-09-29 stsp }
1085 bfddd0d9 2018-09-29 stsp
1086 34bc9ec9 2019-02-22 stsp static void
1087 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1088 25791caa 2018-10-24 stsp {
1089 25791caa 2018-10-24 stsp int cols, lines;
1090 25791caa 2018-10-24 stsp struct winsize size;
1091 25791caa 2018-10-24 stsp
1092 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1093 25791caa 2018-10-24 stsp cols = 80; /* Default */
1094 25791caa 2018-10-24 stsp lines = 24;
1095 25791caa 2018-10-24 stsp } else {
1096 25791caa 2018-10-24 stsp cols = size.ws_col;
1097 25791caa 2018-10-24 stsp lines = size.ws_row;
1098 25791caa 2018-10-24 stsp }
1099 25791caa 2018-10-24 stsp resize_term(lines, cols);
1100 2b49a8ae 2019-06-22 stsp }
1101 2b49a8ae 2019-06-22 stsp
1102 2b49a8ae 2019-06-22 stsp static const struct got_error *
1103 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1104 2b49a8ae 2019-06-22 stsp {
1105 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1106 9b058f45 2022-06-30 mark struct tog_view *v = view;
1107 2b49a8ae 2019-06-22 stsp char pattern[1024];
1108 2b49a8ae 2019-06-22 stsp int ret;
1109 c0c4acc8 2021-01-24 stsp
1110 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1111 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1112 c0c4acc8 2021-01-24 stsp view->searching = 0;
1113 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1114 c0c4acc8 2021-01-24 stsp }
1115 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1116 2b49a8ae 2019-06-22 stsp
1117 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1118 2b49a8ae 2019-06-22 stsp return NULL;
1119 2b49a8ae 2019-06-22 stsp
1120 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1121 9b058f45 2022-06-30 mark v = view->child;
1122 2b49a8ae 2019-06-22 stsp
1123 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1124 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1125 9b058f45 2022-06-30 mark
1126 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1127 2b49a8ae 2019-06-22 stsp nocbreak();
1128 2b49a8ae 2019-06-22 stsp echo();
1129 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1130 9b058f45 2022-06-30 mark wrefresh(v->window);
1131 2b49a8ae 2019-06-22 stsp cbreak();
1132 2b49a8ae 2019-06-22 stsp noecho();
1133 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1134 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1135 2b49a8ae 2019-06-22 stsp return NULL;
1136 2b49a8ae 2019-06-22 stsp
1137 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1138 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1139 7c32bd05 2019-06-22 stsp if (err) {
1140 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1141 7c32bd05 2019-06-22 stsp return err;
1142 7c32bd05 2019-06-22 stsp }
1143 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1144 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1145 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1146 2b49a8ae 2019-06-22 stsp view->search_next(view);
1147 2b49a8ae 2019-06-22 stsp }
1148 2b49a8ae 2019-06-22 stsp
1149 2b49a8ae 2019-06-22 stsp return NULL;
1150 d2366e29 2022-07-07 mark }
1151 d2366e29 2022-07-07 mark
1152 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1153 d2366e29 2022-07-07 mark static const struct got_error *
1154 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1155 d2366e29 2022-07-07 mark {
1156 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1157 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1158 d2366e29 2022-07-07 mark
1159 d2366e29 2022-07-07 mark if (view->parent)
1160 d2366e29 2022-07-07 mark v = view->parent;
1161 d2366e29 2022-07-07 mark else
1162 d2366e29 2022-07-07 mark v = view;
1163 d2366e29 2022-07-07 mark
1164 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1165 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1166 7532ccda 2022-07-11 mark else
1167 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1168 d2366e29 2022-07-07 mark
1169 7532ccda 2022-07-11 mark if (!v->child)
1170 7532ccda 2022-07-11 mark return NULL;
1171 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1172 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1173 7532ccda 2022-07-11 mark
1174 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1175 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1176 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1177 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1178 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1179 d2366e29 2022-07-07 mark
1180 7532ccda 2022-07-11 mark
1181 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1182 d2366e29 2022-07-07 mark v->ncols = COLS;
1183 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1184 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1185 d2366e29 2022-07-07 mark
1186 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1187 d2366e29 2022-07-07 mark if (err)
1188 d2366e29 2022-07-07 mark return err;
1189 d2366e29 2022-07-07 mark }
1190 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1191 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1192 d2366e29 2022-07-07 mark v->focus_child = 1;
1193 d2366e29 2022-07-07 mark
1194 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1195 d2366e29 2022-07-07 mark if (err)
1196 d2366e29 2022-07-07 mark return err;
1197 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1198 d2366e29 2022-07-07 mark if (err)
1199 d2366e29 2022-07-07 mark return err;
1200 d2366e29 2022-07-07 mark
1201 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1202 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1203 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1204 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1205 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1206 7532ccda 2022-07-11 mark } else {
1207 7532ccda 2022-07-11 mark offset_selection_up(v);
1208 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1209 d2366e29 2022-07-07 mark }
1210 dff91825 2022-07-22 mark if (v->resize)
1211 dff91825 2022-07-22 mark err = v->resize(v, 0);
1212 dff91825 2022-07-22 mark else if (v->child->resize)
1213 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1214 d2366e29 2022-07-07 mark
1215 d2366e29 2022-07-07 mark return err;
1216 0cf4efb1 2018-09-29 stsp }
1217 6d0fee91 2018-08-01 stsp
1218 640cd7ff 2022-06-22 mark /*
1219 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1220 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1221 640cd7ff 2022-06-22 mark */
1222 640cd7ff 2022-06-22 mark static int
1223 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1224 640cd7ff 2022-06-22 mark {
1225 9b058f45 2022-06-30 mark struct tog_view *v = view;
1226 9b058f45 2022-06-30 mark int x, n = 0;
1227 640cd7ff 2022-06-22 mark
1228 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1229 9b058f45 2022-06-30 mark v = view->child;
1230 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1231 9b058f45 2022-06-30 mark v = view->parent;
1232 9b058f45 2022-06-30 mark
1233 640cd7ff 2022-06-22 mark view->count = 0;
1234 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1235 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1236 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1237 9b058f45 2022-06-30 mark waddch(v->window, ':');
1238 640cd7ff 2022-06-22 mark
1239 640cd7ff 2022-06-22 mark do {
1240 9b058f45 2022-06-30 mark x = getcurx(v->window);
1241 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1242 9b058f45 2022-06-30 mark waddch(v->window, c);
1243 9b058f45 2022-06-30 mark wrefresh(v->window);
1244 9b058f45 2022-06-30 mark }
1245 9b058f45 2022-06-30 mark
1246 640cd7ff 2022-06-22 mark /*
1247 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1248 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1249 640cd7ff 2022-06-22 mark */
1250 640cd7ff 2022-06-22 mark if (n >= 9999999)
1251 640cd7ff 2022-06-22 mark n = 9999999;
1252 640cd7ff 2022-06-22 mark else
1253 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1254 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1255 640cd7ff 2022-06-22 mark
1256 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1257 640cd7ff 2022-06-22 mark view->count = n;
1258 640cd7ff 2022-06-22 mark
1259 640cd7ff 2022-06-22 mark return c;
1260 640cd7ff 2022-06-22 mark }
1261 640cd7ff 2022-06-22 mark
1262 0cf4efb1 2018-09-29 stsp static const struct got_error *
1263 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1264 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1265 e5a0f69f 2018-08-18 stsp {
1266 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1267 669b5ffa 2018-10-07 stsp struct tog_view *v;
1268 1a76625f 2018-10-22 stsp int ch, errcode;
1269 e5a0f69f 2018-08-18 stsp
1270 e5a0f69f 2018-08-18 stsp *new = NULL;
1271 8f4ed634 2020-03-26 stsp
1272 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1273 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1274 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1275 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1276 640cd7ff 2022-06-22 mark view->count = 0;
1277 640cd7ff 2022-06-22 mark }
1278 e5a0f69f 2018-08-18 stsp
1279 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1280 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1281 82954512 2020-02-03 stsp if (errcode)
1282 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1283 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1284 3da8ef85 2021-09-21 stsp sched_yield();
1285 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1286 82954512 2020-02-03 stsp if (errcode)
1287 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1288 82954512 2020-02-03 stsp "pthread_mutex_lock");
1289 60493ae3 2019-06-20 stsp view->search_next(view);
1290 60493ae3 2019-06-20 stsp return NULL;
1291 60493ae3 2019-06-20 stsp }
1292 60493ae3 2019-06-20 stsp
1293 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1294 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1295 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1296 1a76625f 2018-10-22 stsp if (errcode)
1297 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1298 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1299 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1300 a6d37fac 2022-07-03 mark cbreak();
1301 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1302 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1303 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1304 a6d37fac 2022-07-03 mark view->count = 0;
1305 a6d37fac 2022-07-03 mark else
1306 a6d37fac 2022-07-03 mark ch = view->ch;
1307 a6d37fac 2022-07-03 mark } else {
1308 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1309 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1310 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1311 640cd7ff 2022-06-22 mark }
1312 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1313 1a76625f 2018-10-22 stsp if (errcode)
1314 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1315 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1316 25791caa 2018-10-24 stsp
1317 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1318 25791caa 2018-10-24 stsp tog_resizeterm();
1319 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1320 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1321 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1322 25791caa 2018-10-24 stsp err = view_resize(v);
1323 25791caa 2018-10-24 stsp if (err)
1324 25791caa 2018-10-24 stsp return err;
1325 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1326 25791caa 2018-10-24 stsp if (err)
1327 25791caa 2018-10-24 stsp return err;
1328 cdfcfb03 2020-12-06 stsp if (v->child) {
1329 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1330 cdfcfb03 2020-12-06 stsp if (err)
1331 cdfcfb03 2020-12-06 stsp return err;
1332 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1333 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1334 cdfcfb03 2020-12-06 stsp if (err)
1335 cdfcfb03 2020-12-06 stsp return err;
1336 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1337 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1338 3c1dfe12 2022-07-08 mark if (err)
1339 3c1dfe12 2022-07-08 mark return err;
1340 3c1dfe12 2022-07-08 mark }
1341 cdfcfb03 2020-12-06 stsp }
1342 25791caa 2018-10-24 stsp }
1343 25791caa 2018-10-24 stsp }
1344 25791caa 2018-10-24 stsp
1345 e5a0f69f 2018-08-18 stsp switch (ch) {
1346 1e37a5c2 2019-05-12 jcs case '\t':
1347 640cd7ff 2022-06-22 mark view->count = 0;
1348 1e37a5c2 2019-05-12 jcs if (view->child) {
1349 e78dc838 2020-12-04 stsp view->focussed = 0;
1350 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1351 e78dc838 2020-12-04 stsp view->focus_child = 1;
1352 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1353 e78dc838 2020-12-04 stsp view->focussed = 0;
1354 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1355 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1356 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1357 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1358 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1359 6fe51fee 2022-07-22 mark 0);
1360 9b058f45 2022-06-30 mark if (err)
1361 9b058f45 2022-06-30 mark return err;
1362 9b058f45 2022-06-30 mark }
1363 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1364 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1365 9b058f45 2022-06-30 mark if (err)
1366 9b058f45 2022-06-30 mark return err;
1367 9b058f45 2022-06-30 mark }
1368 1e37a5c2 2019-05-12 jcs }
1369 1e37a5c2 2019-05-12 jcs break;
1370 1e37a5c2 2019-05-12 jcs case 'q':
1371 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1372 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1373 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1374 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1375 9b058f45 2022-06-30 mark if (err)
1376 9b058f45 2022-06-30 mark break;
1377 9b058f45 2022-06-30 mark }
1378 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1379 9b058f45 2022-06-30 mark }
1380 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1381 9970f7fc 2020-12-03 stsp view->dying = 1;
1382 1e37a5c2 2019-05-12 jcs break;
1383 1e37a5c2 2019-05-12 jcs case 'Q':
1384 1e37a5c2 2019-05-12 jcs *done = 1;
1385 1e37a5c2 2019-05-12 jcs break;
1386 61417565 2022-06-20 mark case 'F':
1387 640cd7ff 2022-06-22 mark view->count = 0;
1388 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1389 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1390 1e37a5c2 2019-05-12 jcs break;
1391 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1392 e78dc838 2020-12-04 stsp view->focussed = 0;
1393 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1394 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1395 3c1dfe12 2022-07-08 mark } else {
1396 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1397 3c1dfe12 2022-07-08 mark if (!err)
1398 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1399 3c1dfe12 2022-07-08 mark }
1400 1e37a5c2 2019-05-12 jcs if (err)
1401 1e37a5c2 2019-05-12 jcs break;
1402 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1403 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1404 1e37a5c2 2019-05-12 jcs } else {
1405 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1406 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1407 e78dc838 2020-12-04 stsp view->focussed = 1;
1408 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1409 1e37a5c2 2019-05-12 jcs } else {
1410 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1411 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1412 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1413 3c1dfe12 2022-07-08 mark if (!err)
1414 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1415 669b5ffa 2018-10-07 stsp }
1416 1e37a5c2 2019-05-12 jcs if (err)
1417 1e37a5c2 2019-05-12 jcs break;
1418 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1419 1e37a5c2 2019-05-12 jcs }
1420 9b058f45 2022-06-30 mark if (err)
1421 9b058f45 2022-06-30 mark break;
1422 6fe51fee 2022-07-22 mark if (view->resize) {
1423 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1424 9b058f45 2022-06-30 mark if (err)
1425 9b058f45 2022-06-30 mark break;
1426 9b058f45 2022-06-30 mark }
1427 9b058f45 2022-06-30 mark if (view->parent)
1428 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1429 9b058f45 2022-06-30 mark if (!err)
1430 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1431 1e37a5c2 2019-05-12 jcs break;
1432 d2366e29 2022-07-07 mark case 'S':
1433 3c1dfe12 2022-07-08 mark view->count = 0;
1434 d2366e29 2022-07-07 mark err = switch_split(view);
1435 3c1dfe12 2022-07-08 mark break;
1436 3c1dfe12 2022-07-08 mark case '-':
1437 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1438 d2366e29 2022-07-07 mark break;
1439 3c1dfe12 2022-07-08 mark case '+':
1440 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1441 3c1dfe12 2022-07-08 mark break;
1442 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1443 60493ae3 2019-06-20 stsp break;
1444 60493ae3 2019-06-20 stsp case '/':
1445 640cd7ff 2022-06-22 mark view->count = 0;
1446 60493ae3 2019-06-20 stsp if (view->search_start)
1447 2b49a8ae 2019-06-22 stsp view_search_start(view);
1448 60493ae3 2019-06-20 stsp else
1449 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1450 1e37a5c2 2019-05-12 jcs break;
1451 b1bf1435 2019-06-21 stsp case 'N':
1452 60493ae3 2019-06-20 stsp case 'n':
1453 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1454 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1455 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1456 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1457 60493ae3 2019-06-20 stsp view->search_next(view);
1458 60493ae3 2019-06-20 stsp } else
1459 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1460 917d79a7 2022-07-01 stsp break;
1461 917d79a7 2022-07-01 stsp case 'A':
1462 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1463 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1464 917d79a7 2022-07-01 stsp else
1465 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1466 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1467 917d79a7 2022-07-01 stsp if (v->reset) {
1468 917d79a7 2022-07-01 stsp err = v->reset(v);
1469 917d79a7 2022-07-01 stsp if (err)
1470 917d79a7 2022-07-01 stsp return err;
1471 917d79a7 2022-07-01 stsp }
1472 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1473 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1474 917d79a7 2022-07-01 stsp if (err)
1475 917d79a7 2022-07-01 stsp return err;
1476 917d79a7 2022-07-01 stsp }
1477 917d79a7 2022-07-01 stsp }
1478 60493ae3 2019-06-20 stsp break;
1479 1e37a5c2 2019-05-12 jcs default:
1480 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1481 1e37a5c2 2019-05-12 jcs break;
1482 e5a0f69f 2018-08-18 stsp }
1483 e5a0f69f 2018-08-18 stsp
1484 e5a0f69f 2018-08-18 stsp return err;
1485 e5a0f69f 2018-08-18 stsp }
1486 e5a0f69f 2018-08-18 stsp
1487 336075a4 2022-06-25 op static int
1488 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1489 a3404814 2018-09-02 stsp {
1490 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1491 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1492 669b5ffa 2018-10-07 stsp return 0;
1493 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1494 669b5ffa 2018-10-07 stsp return 0;
1495 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1496 a3404814 2018-09-02 stsp return 0;
1497 a3404814 2018-09-02 stsp
1498 669b5ffa 2018-10-07 stsp return view->focussed;
1499 a3404814 2018-09-02 stsp }
1500 a3404814 2018-09-02 stsp
1501 bcbd79e2 2018-08-19 stsp static const struct got_error *
1502 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1503 e5a0f69f 2018-08-18 stsp {
1504 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1505 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1506 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1507 d2366e29 2022-07-07 mark char *mode;
1508 fd823528 2018-10-22 stsp int fast_refresh = 10;
1509 1a76625f 2018-10-22 stsp int done = 0, errcode;
1510 e5a0f69f 2018-08-18 stsp
1511 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1512 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1513 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1514 d2366e29 2022-07-07 mark else
1515 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1516 d2366e29 2022-07-07 mark
1517 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1518 1a76625f 2018-10-22 stsp if (errcode)
1519 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1520 1a76625f 2018-10-22 stsp
1521 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1522 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1523 e5a0f69f 2018-08-18 stsp
1524 1004088d 2018-09-29 stsp view->focussed = 1;
1525 878940b7 2018-09-29 stsp err = view->show(view);
1526 0cf4efb1 2018-09-29 stsp if (err)
1527 0cf4efb1 2018-09-29 stsp return err;
1528 0cf4efb1 2018-09-29 stsp update_panels();
1529 0cf4efb1 2018-09-29 stsp doupdate();
1530 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1531 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1532 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1533 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1534 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1535 fd823528 2018-10-22 stsp
1536 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1537 e5a0f69f 2018-08-18 stsp if (err)
1538 e5a0f69f 2018-08-18 stsp break;
1539 9970f7fc 2020-12-03 stsp if (view->dying) {
1540 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1541 669b5ffa 2018-10-07 stsp
1542 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1543 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1544 9970f7fc 2020-12-03 stsp entry);
1545 e78dc838 2020-12-04 stsp else if (view->parent)
1546 669b5ffa 2018-10-07 stsp prev = view->parent;
1547 669b5ffa 2018-10-07 stsp
1548 e78dc838 2020-12-04 stsp if (view->parent) {
1549 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1550 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1551 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1552 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1553 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1554 0dbbbe90 2022-06-17 op if (err)
1555 0dbbbe90 2022-06-17 op break;
1556 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1557 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1558 e78dc838 2020-12-04 stsp } else
1559 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1560 669b5ffa 2018-10-07 stsp
1561 9970f7fc 2020-12-03 stsp err = view_close(view);
1562 fb59748f 2020-12-05 stsp if (err)
1563 e5a0f69f 2018-08-18 stsp goto done;
1564 669b5ffa 2018-10-07 stsp
1565 e78dc838 2020-12-04 stsp view = NULL;
1566 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1567 e78dc838 2020-12-04 stsp if (v->focussed)
1568 e78dc838 2020-12-04 stsp break;
1569 0cf4efb1 2018-09-29 stsp }
1570 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1571 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1572 e78dc838 2020-12-04 stsp if (prev)
1573 e78dc838 2020-12-04 stsp view = prev;
1574 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1575 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1576 e78dc838 2020-12-04 stsp tog_view_list_head);
1577 e78dc838 2020-12-04 stsp }
1578 e78dc838 2020-12-04 stsp if (view) {
1579 e78dc838 2020-12-04 stsp if (view->focus_child) {
1580 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1581 e78dc838 2020-12-04 stsp view = view->child;
1582 e78dc838 2020-12-04 stsp } else
1583 e78dc838 2020-12-04 stsp view->focussed = 1;
1584 e78dc838 2020-12-04 stsp }
1585 e78dc838 2020-12-04 stsp }
1586 e5a0f69f 2018-08-18 stsp }
1587 bcbd79e2 2018-08-19 stsp if (new_view) {
1588 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1589 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1590 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1591 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1592 86c66b02 2018-10-18 stsp continue;
1593 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1594 86c66b02 2018-10-18 stsp err = view_close(v);
1595 86c66b02 2018-10-18 stsp if (err)
1596 86c66b02 2018-10-18 stsp goto done;
1597 86c66b02 2018-10-18 stsp break;
1598 86c66b02 2018-10-18 stsp }
1599 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1600 fed7eaa8 2018-10-24 stsp view = new_view;
1601 0ae84acc 2022-06-15 tracey }
1602 669b5ffa 2018-10-07 stsp if (view) {
1603 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1604 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1605 e78dc838 2020-12-04 stsp view = view->child;
1606 e78dc838 2020-12-04 stsp } else {
1607 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1608 e78dc838 2020-12-04 stsp view = view->parent;
1609 1a76625f 2018-10-22 stsp }
1610 e78dc838 2020-12-04 stsp show_panel(view->panel);
1611 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1612 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1613 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1614 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1615 669b5ffa 2018-10-07 stsp if (err)
1616 1a76625f 2018-10-22 stsp goto done;
1617 669b5ffa 2018-10-07 stsp }
1618 669b5ffa 2018-10-07 stsp err = view->show(view);
1619 0cf4efb1 2018-09-29 stsp if (err)
1620 1a76625f 2018-10-22 stsp goto done;
1621 669b5ffa 2018-10-07 stsp if (view->child) {
1622 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1623 669b5ffa 2018-10-07 stsp if (err)
1624 1a76625f 2018-10-22 stsp goto done;
1625 669b5ffa 2018-10-07 stsp }
1626 1a76625f 2018-10-22 stsp update_panels();
1627 1a76625f 2018-10-22 stsp doupdate();
1628 0cf4efb1 2018-09-29 stsp }
1629 e5a0f69f 2018-08-18 stsp }
1630 e5a0f69f 2018-08-18 stsp done:
1631 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1632 5629093a 2022-07-11 stsp const struct got_error *close_err;
1633 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1634 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1635 5629093a 2022-07-11 stsp close_err = view_close(view);
1636 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1637 5629093a 2022-07-11 stsp err = close_err;
1638 e5a0f69f 2018-08-18 stsp }
1639 1a76625f 2018-10-22 stsp
1640 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1641 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1642 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1643 1a76625f 2018-10-22 stsp
1644 e5a0f69f 2018-08-18 stsp return err;
1645 ea5e7bb5 2018-08-01 stsp }
1646 ea5e7bb5 2018-08-01 stsp
1647 4ed7e80c 2018-05-20 stsp __dead static void
1648 9f7d7167 2018-04-29 stsp usage_log(void)
1649 9f7d7167 2018-04-29 stsp {
1650 80ddbec8 2018-04-29 stsp endwin();
1651 c70c5802 2018-08-01 stsp fprintf(stderr,
1652 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1653 9f7d7167 2018-04-29 stsp getprogname());
1654 9f7d7167 2018-04-29 stsp exit(1);
1655 80ddbec8 2018-04-29 stsp }
1656 80ddbec8 2018-04-29 stsp
1657 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1658 80ddbec8 2018-04-29 stsp static const struct got_error *
1659 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1660 963b370f 2018-05-20 stsp {
1661 00dfcb92 2018-06-11 stsp char *vis = NULL;
1662 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1663 963b370f 2018-05-20 stsp
1664 963b370f 2018-05-20 stsp *ws = NULL;
1665 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1666 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1667 00dfcb92 2018-06-11 stsp int vislen;
1668 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1669 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1670 00dfcb92 2018-06-11 stsp
1671 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1672 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1673 00dfcb92 2018-06-11 stsp if (err)
1674 00dfcb92 2018-06-11 stsp return err;
1675 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1676 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1677 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1678 a7f50699 2018-06-11 stsp goto done;
1679 a7f50699 2018-06-11 stsp }
1680 00dfcb92 2018-06-11 stsp }
1681 963b370f 2018-05-20 stsp
1682 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1683 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1685 a7f50699 2018-06-11 stsp goto done;
1686 a7f50699 2018-06-11 stsp }
1687 963b370f 2018-05-20 stsp
1688 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1689 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1690 a7f50699 2018-06-11 stsp done:
1691 00dfcb92 2018-06-11 stsp free(vis);
1692 963b370f 2018-05-20 stsp if (err) {
1693 963b370f 2018-05-20 stsp free(*ws);
1694 963b370f 2018-05-20 stsp *ws = NULL;
1695 963b370f 2018-05-20 stsp *wlen = 0;
1696 963b370f 2018-05-20 stsp }
1697 963b370f 2018-05-20 stsp return err;
1698 145b6838 2022-06-16 stsp }
1699 145b6838 2022-06-16 stsp
1700 145b6838 2022-06-16 stsp static const struct got_error *
1701 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1702 145b6838 2022-06-16 stsp {
1703 145b6838 2022-06-16 stsp char *dst;
1704 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1705 145b6838 2022-06-16 stsp
1706 145b6838 2022-06-16 stsp *ptr = NULL;
1707 145b6838 2022-06-16 stsp n = len = strlen(src);
1708 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1709 145b6838 2022-06-16 stsp if (dst == NULL)
1710 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1711 145b6838 2022-06-16 stsp
1712 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1713 145b6838 2022-06-16 stsp const char c = src[idx];
1714 145b6838 2022-06-16 stsp
1715 145b6838 2022-06-16 stsp if (c == '\t') {
1716 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1717 367ddf28 2022-06-16 mark char *p;
1718 367ddf28 2022-06-16 mark
1719 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1720 6e1c41ad 2022-06-16 mark if (p == NULL) {
1721 6e1c41ad 2022-06-16 mark free(dst);
1722 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1723 6e1c41ad 2022-06-16 mark
1724 6e1c41ad 2022-06-16 mark }
1725 6e1c41ad 2022-06-16 mark dst = p;
1726 145b6838 2022-06-16 stsp n += nb;
1727 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1728 145b6838 2022-06-16 stsp sz += nb;
1729 145b6838 2022-06-16 stsp } else
1730 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1731 145b6838 2022-06-16 stsp ++idx;
1732 145b6838 2022-06-16 stsp }
1733 145b6838 2022-06-16 stsp
1734 145b6838 2022-06-16 stsp dst[sz] = '\0';
1735 145b6838 2022-06-16 stsp *ptr = dst;
1736 145b6838 2022-06-16 stsp return NULL;
1737 963b370f 2018-05-20 stsp }
1738 963b370f 2018-05-20 stsp
1739 4e4a9ac8 2022-06-17 op /*
1740 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1741 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1742 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1743 4e4a9ac8 2022-06-17 op * *rcol.
1744 ccda2f4d 2022-06-16 stsp */
1745 4e4a9ac8 2022-06-17 op static int
1746 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1747 4e4a9ac8 2022-06-17 op {
1748 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1749 ccda2f4d 2022-06-16 stsp
1750 4e4a9ac8 2022-06-17 op if (n == 0) {
1751 4e4a9ac8 2022-06-17 op *rcol = cols;
1752 4e4a9ac8 2022-06-17 op return off;
1753 4e4a9ac8 2022-06-17 op }
1754 ccda2f4d 2022-06-16 stsp
1755 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1756 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1757 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1758 4e4a9ac8 2022-06-17 op else
1759 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1760 ccda2f4d 2022-06-16 stsp
1761 4e4a9ac8 2022-06-17 op if (width == -1) {
1762 4e4a9ac8 2022-06-17 op width = 1;
1763 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1764 ccda2f4d 2022-06-16 stsp }
1765 ccda2f4d 2022-06-16 stsp
1766 4e4a9ac8 2022-06-17 op if (cols + width > n)
1767 4e4a9ac8 2022-06-17 op break;
1768 4e4a9ac8 2022-06-17 op cols += width;
1769 ccda2f4d 2022-06-16 stsp }
1770 ccda2f4d 2022-06-16 stsp
1771 4e4a9ac8 2022-06-17 op *rcol = cols;
1772 4e4a9ac8 2022-06-17 op return i;
1773 ccda2f4d 2022-06-16 stsp }
1774 ccda2f4d 2022-06-16 stsp
1775 ccda2f4d 2022-06-16 stsp /*
1776 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1777 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1778 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1779 ccda2f4d 2022-06-16 stsp */
1780 ccda2f4d 2022-06-16 stsp static const struct got_error *
1781 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1782 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1783 ccda2f4d 2022-06-16 stsp {
1784 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1785 4e4a9ac8 2022-06-17 op int cols;
1786 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1787 145b6838 2022-06-16 stsp char *exstr = NULL;
1788 963b370f 2018-05-20 stsp size_t wlen;
1789 4e4a9ac8 2022-06-17 op int i, scrollx;
1790 963b370f 2018-05-20 stsp
1791 963b370f 2018-05-20 stsp *wlinep = NULL;
1792 b700b5d6 2018-07-10 stsp *widthp = 0;
1793 963b370f 2018-05-20 stsp
1794 145b6838 2022-06-16 stsp if (expand) {
1795 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1796 145b6838 2022-06-16 stsp if (err)
1797 145b6838 2022-06-16 stsp return err;
1798 145b6838 2022-06-16 stsp }
1799 145b6838 2022-06-16 stsp
1800 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1801 145b6838 2022-06-16 stsp free(exstr);
1802 963b370f 2018-05-20 stsp if (err)
1803 963b370f 2018-05-20 stsp return err;
1804 963b370f 2018-05-20 stsp
1805 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1806 ccda2f4d 2022-06-16 stsp
1807 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1808 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1809 3f670bfb 2020-12-10 stsp wlen--;
1810 3f670bfb 2020-12-10 stsp }
1811 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1812 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1813 3f670bfb 2020-12-10 stsp wlen--;
1814 3f670bfb 2020-12-10 stsp }
1815 3f670bfb 2020-12-10 stsp
1816 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1817 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1818 27a741e5 2019-09-11 stsp
1819 b700b5d6 2018-07-10 stsp if (widthp)
1820 b700b5d6 2018-07-10 stsp *widthp = cols;
1821 ccda2f4d 2022-06-16 stsp if (scrollxp)
1822 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1823 963b370f 2018-05-20 stsp if (err)
1824 963b370f 2018-05-20 stsp free(wline);
1825 963b370f 2018-05-20 stsp else
1826 963b370f 2018-05-20 stsp *wlinep = wline;
1827 963b370f 2018-05-20 stsp return err;
1828 963b370f 2018-05-20 stsp }
1829 963b370f 2018-05-20 stsp
1830 8b473291 2019-02-21 stsp static const struct got_error*
1831 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1832 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1833 8b473291 2019-02-21 stsp {
1834 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1835 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1836 8b473291 2019-02-21 stsp char *s;
1837 8b473291 2019-02-21 stsp const char *name;
1838 8b473291 2019-02-21 stsp
1839 8b473291 2019-02-21 stsp *refs_str = NULL;
1840 8b473291 2019-02-21 stsp
1841 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1842 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1843 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1844 52b5abe1 2019-08-13 stsp int cmp;
1845 52b5abe1 2019-08-13 stsp
1846 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1847 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1848 8b473291 2019-02-21 stsp continue;
1849 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1850 8b473291 2019-02-21 stsp name += 5;
1851 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1852 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1853 7143d404 2019-03-12 stsp continue;
1854 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1855 8b473291 2019-02-21 stsp name += 6;
1856 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1857 8b473291 2019-02-21 stsp name += 8;
1858 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1859 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1860 79cc719f 2020-04-24 stsp continue;
1861 79cc719f 2020-04-24 stsp }
1862 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1863 48cae60d 2020-09-22 stsp if (err)
1864 48cae60d 2020-09-22 stsp break;
1865 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1866 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1867 5d844a1e 2019-08-13 stsp if (err) {
1868 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1869 48cae60d 2020-09-22 stsp free(ref_id);
1870 5d844a1e 2019-08-13 stsp break;
1871 48cae60d 2020-09-22 stsp }
1872 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1873 5d844a1e 2019-08-13 stsp err = NULL;
1874 5d844a1e 2019-08-13 stsp tag = NULL;
1875 5d844a1e 2019-08-13 stsp }
1876 52b5abe1 2019-08-13 stsp }
1877 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1878 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1879 48cae60d 2020-09-22 stsp free(ref_id);
1880 52b5abe1 2019-08-13 stsp if (tag)
1881 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1882 52b5abe1 2019-08-13 stsp if (cmp != 0)
1883 52b5abe1 2019-08-13 stsp continue;
1884 8b473291 2019-02-21 stsp s = *refs_str;
1885 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1886 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1887 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1888 8b473291 2019-02-21 stsp free(s);
1889 8b473291 2019-02-21 stsp *refs_str = NULL;
1890 8b473291 2019-02-21 stsp break;
1891 8b473291 2019-02-21 stsp }
1892 8b473291 2019-02-21 stsp free(s);
1893 8b473291 2019-02-21 stsp }
1894 8b473291 2019-02-21 stsp
1895 8b473291 2019-02-21 stsp return err;
1896 8b473291 2019-02-21 stsp }
1897 8b473291 2019-02-21 stsp
1898 963b370f 2018-05-20 stsp static const struct got_error *
1899 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1900 27a741e5 2019-09-11 stsp int col_tab_align)
1901 5813d178 2019-03-09 stsp {
1902 e6b8b890 2020-12-29 naddy char *smallerthan;
1903 5813d178 2019-03-09 stsp
1904 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1905 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1906 5813d178 2019-03-09 stsp author = smallerthan + 1;
1907 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1908 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1909 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1910 5813d178 2019-03-09 stsp }
1911 5813d178 2019-03-09 stsp
1912 5813d178 2019-03-09 stsp static const struct got_error *
1913 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1914 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1915 8fdc79fe 2020-12-01 naddy int author_display_cols)
1916 80ddbec8 2018-04-29 stsp {
1917 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1918 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1919 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1920 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1921 5813d178 2019-03-09 stsp char *author = NULL;
1922 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1923 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1924 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1925 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1926 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1927 ccb26ccd 2018-11-05 stsp struct tm tm;
1928 45d799e2 2018-12-23 stsp time_t committer_time;
1929 11b20872 2019-11-08 stsp struct tog_color *tc;
1930 80ddbec8 2018-04-29 stsp
1931 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1932 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1933 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1934 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1935 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1936 b39d25c7 2018-07-10 stsp
1937 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1938 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1939 b39d25c7 2018-07-10 stsp else
1940 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1941 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1942 11b20872 2019-11-08 stsp if (tc)
1943 11b20872 2019-11-08 stsp wattr_on(view->window,
1944 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1945 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1946 11b20872 2019-11-08 stsp if (tc)
1947 11b20872 2019-11-08 stsp wattr_off(view->window,
1948 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1949 27a741e5 2019-09-11 stsp col = limit;
1950 b39d25c7 2018-07-10 stsp if (col > avail)
1951 b39d25c7 2018-07-10 stsp goto done;
1952 6570a66d 2019-11-08 stsp
1953 6570a66d 2019-11-08 stsp if (avail >= 120) {
1954 6570a66d 2019-11-08 stsp char *id_str;
1955 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1956 6570a66d 2019-11-08 stsp if (err)
1957 6570a66d 2019-11-08 stsp goto done;
1958 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1959 11b20872 2019-11-08 stsp if (tc)
1960 11b20872 2019-11-08 stsp wattr_on(view->window,
1961 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1962 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1963 11b20872 2019-11-08 stsp if (tc)
1964 11b20872 2019-11-08 stsp wattr_off(view->window,
1965 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1966 6570a66d 2019-11-08 stsp free(id_str);
1967 6570a66d 2019-11-08 stsp col += 9;
1968 6570a66d 2019-11-08 stsp if (col > avail)
1969 6570a66d 2019-11-08 stsp goto done;
1970 6570a66d 2019-11-08 stsp }
1971 b39d25c7 2018-07-10 stsp
1972 10aab77f 2022-07-19 op if (s->use_committer)
1973 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
1974 10aab77f 2022-07-19 op else
1975 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
1976 5813d178 2019-03-09 stsp if (author == NULL) {
1977 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1978 80ddbec8 2018-04-29 stsp goto done;
1979 80ddbec8 2018-04-29 stsp }
1980 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1981 bb737323 2018-05-20 stsp if (err)
1982 bb737323 2018-05-20 stsp goto done;
1983 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1984 11b20872 2019-11-08 stsp if (tc)
1985 11b20872 2019-11-08 stsp wattr_on(view->window,
1986 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1987 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1988 11b20872 2019-11-08 stsp if (tc)
1989 11b20872 2019-11-08 stsp wattr_off(view->window,
1990 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1991 bb737323 2018-05-20 stsp col += author_width;
1992 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1993 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1994 bb737323 2018-05-20 stsp col++;
1995 bb737323 2018-05-20 stsp author_width++;
1996 bb737323 2018-05-20 stsp }
1997 9c2eaf34 2018-05-20 stsp if (col > avail)
1998 9c2eaf34 2018-05-20 stsp goto done;
1999 80ddbec8 2018-04-29 stsp
2000 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2001 5943eee2 2019-08-13 stsp if (err)
2002 6d9fbc00 2018-04-29 stsp goto done;
2003 bb737323 2018-05-20 stsp logmsg = logmsg0;
2004 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2005 bb737323 2018-05-20 stsp logmsg++;
2006 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2007 bb737323 2018-05-20 stsp if (newline)
2008 bb737323 2018-05-20 stsp *newline = '\0';
2009 ccda2f4d 2022-06-16 stsp limit = avail - col;
2010 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2011 4d1f6af3 2022-06-17 op limit--; /* for the border */
2012 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2013 ccda2f4d 2022-06-16 stsp limit, col, 1);
2014 29688b02 2022-06-16 stsp if (err)
2015 29688b02 2022-06-16 stsp goto done;
2016 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2017 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2018 27a741e5 2019-09-11 stsp while (col < avail) {
2019 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2020 bb737323 2018-05-20 stsp col++;
2021 881b2d3e 2018-04-30 stsp }
2022 80ddbec8 2018-04-29 stsp done:
2023 80ddbec8 2018-04-29 stsp free(logmsg0);
2024 bb737323 2018-05-20 stsp free(wlogmsg);
2025 5813d178 2019-03-09 stsp free(author);
2026 bb737323 2018-05-20 stsp free(wauthor);
2027 80ddbec8 2018-04-29 stsp free(line);
2028 80ddbec8 2018-04-29 stsp return err;
2029 80ddbec8 2018-04-29 stsp }
2030 26ed57b2 2018-05-19 stsp
2031 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2032 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2033 899d86c2 2018-05-10 stsp struct got_object_id *id)
2034 80ddbec8 2018-04-29 stsp {
2035 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2036 80ddbec8 2018-04-29 stsp
2037 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2038 80ddbec8 2018-04-29 stsp if (entry == NULL)
2039 899d86c2 2018-05-10 stsp return NULL;
2040 99db9666 2018-05-07 stsp
2041 899d86c2 2018-05-10 stsp entry->id = id;
2042 99db9666 2018-05-07 stsp entry->commit = commit;
2043 899d86c2 2018-05-10 stsp return entry;
2044 99db9666 2018-05-07 stsp }
2045 80ddbec8 2018-04-29 stsp
2046 99db9666 2018-05-07 stsp static void
2047 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2048 99db9666 2018-05-07 stsp {
2049 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2050 99db9666 2018-05-07 stsp
2051 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2052 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2053 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2054 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2055 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2056 99db9666 2018-05-07 stsp free(entry);
2057 99db9666 2018-05-07 stsp }
2058 99db9666 2018-05-07 stsp
2059 99db9666 2018-05-07 stsp static void
2060 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2061 99db9666 2018-05-07 stsp {
2062 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2063 99db9666 2018-05-07 stsp pop_commit(commits);
2064 c4972b91 2018-05-07 stsp }
2065 c4972b91 2018-05-07 stsp
2066 c4972b91 2018-05-07 stsp static const struct got_error *
2067 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2068 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2069 13add988 2019-10-15 stsp {
2070 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2071 13add988 2019-10-15 stsp regmatch_t regmatch;
2072 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2073 13add988 2019-10-15 stsp
2074 13add988 2019-10-15 stsp *have_match = 0;
2075 13add988 2019-10-15 stsp
2076 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2077 13add988 2019-10-15 stsp if (err)
2078 13add988 2019-10-15 stsp return err;
2079 13add988 2019-10-15 stsp
2080 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2081 13add988 2019-10-15 stsp if (err)
2082 13add988 2019-10-15 stsp goto done;
2083 13add988 2019-10-15 stsp
2084 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2085 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2086 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2087 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2088 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2089 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2090 13add988 2019-10-15 stsp *have_match = 1;
2091 13add988 2019-10-15 stsp done:
2092 13add988 2019-10-15 stsp free(id_str);
2093 13add988 2019-10-15 stsp free(logmsg);
2094 13add988 2019-10-15 stsp return err;
2095 13add988 2019-10-15 stsp }
2096 13add988 2019-10-15 stsp
2097 13add988 2019-10-15 stsp static const struct got_error *
2098 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2099 c4972b91 2018-05-07 stsp {
2100 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2101 9ba79e04 2018-06-11 stsp
2102 1a76625f 2018-10-22 stsp /*
2103 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2104 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2105 1a76625f 2018-10-22 stsp * while updating the display.
2106 1a76625f 2018-10-22 stsp */
2107 4e0d2870 2020-12-07 naddy do {
2108 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2109 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2110 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2111 1a76625f 2018-10-22 stsp int errcode;
2112 899d86c2 2018-05-10 stsp
2113 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2114 4e0d2870 2020-12-07 naddy NULL, NULL);
2115 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2116 ecb28ae0 2018-07-16 stsp break;
2117 899d86c2 2018-05-10 stsp
2118 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2119 9ba79e04 2018-06-11 stsp if (err)
2120 9ba79e04 2018-06-11 stsp break;
2121 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2122 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2123 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2124 9ba79e04 2018-06-11 stsp break;
2125 9ba79e04 2018-06-11 stsp }
2126 93e45b7c 2018-09-24 stsp
2127 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2128 1a76625f 2018-10-22 stsp if (errcode) {
2129 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2130 13add988 2019-10-15 stsp "pthread_mutex_lock");
2131 1a76625f 2018-10-22 stsp break;
2132 1a76625f 2018-10-22 stsp }
2133 1a76625f 2018-10-22 stsp
2134 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2135 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2136 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2137 1a76625f 2018-10-22 stsp
2138 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2139 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2140 7c1452c1 2020-03-26 stsp int have_match;
2141 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2142 7c1452c1 2020-03-26 stsp if (err)
2143 7c1452c1 2020-03-26 stsp break;
2144 7c1452c1 2020-03-26 stsp if (have_match)
2145 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2146 13add988 2019-10-15 stsp }
2147 13add988 2019-10-15 stsp
2148 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2149 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2150 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2151 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2152 7c1452c1 2020-03-26 stsp if (err)
2153 13add988 2019-10-15 stsp break;
2154 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2155 899d86c2 2018-05-10 stsp
2156 9ba79e04 2018-06-11 stsp return err;
2157 0553a4e3 2018-04-30 stsp }
2158 0553a4e3 2018-04-30 stsp
2159 2b779855 2020-12-05 naddy static void
2160 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2161 2b779855 2020-12-05 naddy {
2162 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2163 2b779855 2020-12-05 naddy int ncommits = 0;
2164 2b779855 2020-12-05 naddy
2165 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2166 2b779855 2020-12-05 naddy while (entry) {
2167 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2168 2b779855 2020-12-05 naddy s->selected_entry = entry;
2169 2b779855 2020-12-05 naddy break;
2170 2b779855 2020-12-05 naddy }
2171 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2172 2b779855 2020-12-05 naddy ncommits++;
2173 2b779855 2020-12-05 naddy }
2174 2b779855 2020-12-05 naddy }
2175 2b779855 2020-12-05 naddy
2176 0553a4e3 2018-04-30 stsp static const struct got_error *
2177 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2178 0553a4e3 2018-04-30 stsp {
2179 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2180 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2181 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2182 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2183 60493ae3 2019-06-20 stsp int width;
2184 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2185 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2186 8b473291 2019-02-21 stsp char *refs_str = NULL;
2187 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2188 11b20872 2019-11-08 stsp struct tog_color *tc;
2189 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2190 0553a4e3 2018-04-30 stsp
2191 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2192 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2193 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2194 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2195 1a76625f 2018-10-22 stsp if (err)
2196 ecb28ae0 2018-07-16 stsp return err;
2197 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2198 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2199 d2075bf3 2020-12-25 stsp if (refs) {
2200 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2201 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2202 d2075bf3 2020-12-25 stsp if (err)
2203 d2075bf3 2020-12-25 stsp goto done;
2204 d2075bf3 2020-12-25 stsp }
2205 867c6645 2018-07-10 stsp }
2206 359bfafd 2019-02-22 stsp
2207 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2208 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2209 1a76625f 2018-10-22 stsp
2210 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2211 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2212 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2213 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2214 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2215 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2216 8f4ed634 2020-03-26 stsp goto done;
2217 8f4ed634 2020-03-26 stsp }
2218 8f4ed634 2020-03-26 stsp } else {
2219 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2220 f9686aa5 2020-03-27 stsp
2221 f9686aa5 2020-03-27 stsp if (view->searching) {
2222 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2223 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2224 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2225 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2226 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2227 f9686aa5 2020-03-27 stsp search_str = "searching...";
2228 f9686aa5 2020-03-27 stsp }
2229 f9686aa5 2020-03-27 stsp
2230 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2231 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2232 f9686aa5 2020-03-27 stsp search_str ? search_str :
2233 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2234 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2235 8f4ed634 2020-03-26 stsp goto done;
2236 8f4ed634 2020-03-26 stsp }
2237 8b473291 2019-02-21 stsp }
2238 1a76625f 2018-10-22 stsp
2239 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2240 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2241 87411fa9 2022-06-16 stsp "........................................",
2242 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2243 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2244 1a76625f 2018-10-22 stsp header = NULL;
2245 1a76625f 2018-10-22 stsp goto done;
2246 1a76625f 2018-10-22 stsp }
2247 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2248 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2249 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2250 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2251 1a76625f 2018-10-22 stsp header = NULL;
2252 1a76625f 2018-10-22 stsp goto done;
2253 ecb28ae0 2018-07-16 stsp }
2254 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2255 1a76625f 2018-10-22 stsp if (err)
2256 1a76625f 2018-10-22 stsp goto done;
2257 867c6645 2018-07-10 stsp
2258 2814baeb 2018-08-01 stsp werase(view->window);
2259 867c6645 2018-07-10 stsp
2260 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2261 a3404814 2018-09-02 stsp wstandout(view->window);
2262 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2263 11b20872 2019-11-08 stsp if (tc)
2264 11b20872 2019-11-08 stsp wattr_on(view->window,
2265 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2266 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2267 11b20872 2019-11-08 stsp if (tc)
2268 11b20872 2019-11-08 stsp wattr_off(view->window,
2269 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2270 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2271 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2272 1a76625f 2018-10-22 stsp width++;
2273 1a76625f 2018-10-22 stsp }
2274 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2275 a3404814 2018-09-02 stsp wstandend(view->window);
2276 ecb28ae0 2018-07-16 stsp free(wline);
2277 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2278 1a76625f 2018-10-22 stsp goto done;
2279 0553a4e3 2018-04-30 stsp
2280 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2281 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2282 5813d178 2019-03-09 stsp ncommits = 0;
2283 145b6838 2022-06-16 stsp view->maxx = 0;
2284 5813d178 2019-03-09 stsp while (entry) {
2285 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2286 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2287 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2288 5813d178 2019-03-09 stsp int width;
2289 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2290 5813d178 2019-03-09 stsp break;
2291 10aab77f 2022-07-19 op if (s->use_committer)
2292 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2293 10aab77f 2022-07-19 op else
2294 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2295 5813d178 2019-03-09 stsp if (author == NULL) {
2296 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2297 5813d178 2019-03-09 stsp goto done;
2298 5813d178 2019-03-09 stsp }
2299 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2300 27a741e5 2019-09-11 stsp date_display_cols);
2301 5813d178 2019-03-09 stsp if (author_cols < width)
2302 5813d178 2019-03-09 stsp author_cols = width;
2303 5813d178 2019-03-09 stsp free(wauthor);
2304 5813d178 2019-03-09 stsp free(author);
2305 a310d9c3 2022-07-21 florian if (err)
2306 a310d9c3 2022-07-21 florian goto done;
2307 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2308 145b6838 2022-06-16 stsp if (err)
2309 145b6838 2022-06-16 stsp goto done;
2310 145b6838 2022-06-16 stsp msg = msg0;
2311 145b6838 2022-06-16 stsp while (*msg == '\n')
2312 145b6838 2022-06-16 stsp ++msg;
2313 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2314 29688b02 2022-06-16 stsp *eol = '\0';
2315 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2316 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2317 29688b02 2022-06-16 stsp if (err)
2318 29688b02 2022-06-16 stsp goto done;
2319 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2320 145b6838 2022-06-16 stsp free(msg0);
2321 29688b02 2022-06-16 stsp free(wmsg);
2322 7ca04879 2019-10-19 stsp ncommits++;
2323 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2324 5813d178 2019-03-09 stsp }
2325 5813d178 2019-03-09 stsp
2326 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2327 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2328 867c6645 2018-07-10 stsp ncommits = 0;
2329 899d86c2 2018-05-10 stsp while (entry) {
2330 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2331 899d86c2 2018-05-10 stsp break;
2332 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2333 2814baeb 2018-08-01 stsp wstandout(view->window);
2334 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2335 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2336 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2337 2814baeb 2018-08-01 stsp wstandend(view->window);
2338 0553a4e3 2018-04-30 stsp if (err)
2339 60493ae3 2019-06-20 stsp goto done;
2340 0553a4e3 2018-04-30 stsp ncommits++;
2341 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2342 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2343 80ddbec8 2018-04-29 stsp }
2344 80ddbec8 2018-04-29 stsp
2345 9b058f45 2022-06-30 mark view_border(view);
2346 1a76625f 2018-10-22 stsp done:
2347 1a76625f 2018-10-22 stsp free(id_str);
2348 8b473291 2019-02-21 stsp free(refs_str);
2349 1a76625f 2018-10-22 stsp free(ncommits_str);
2350 1a76625f 2018-10-22 stsp free(header);
2351 80ddbec8 2018-04-29 stsp return err;
2352 9f7d7167 2018-04-29 stsp }
2353 07b55e75 2018-05-10 stsp
2354 07b55e75 2018-05-10 stsp static void
2355 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2356 07b55e75 2018-05-10 stsp {
2357 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2358 07b55e75 2018-05-10 stsp int nscrolled = 0;
2359 07b55e75 2018-05-10 stsp
2360 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2361 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2362 07b55e75 2018-05-10 stsp return;
2363 9f7d7167 2018-04-29 stsp
2364 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2365 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2366 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2367 07b55e75 2018-05-10 stsp if (entry) {
2368 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2369 07b55e75 2018-05-10 stsp nscrolled++;
2370 07b55e75 2018-05-10 stsp }
2371 07b55e75 2018-05-10 stsp }
2372 aa075928 2018-05-10 stsp }
2373 aa075928 2018-05-10 stsp
2374 aa075928 2018-05-10 stsp static const struct got_error *
2375 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2376 aa075928 2018-05-10 stsp {
2377 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2378 5e224a3e 2019-02-22 stsp int errcode;
2379 8a42fca8 2019-02-22 stsp
2380 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2381 aa075928 2018-05-10 stsp
2382 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2383 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2384 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2385 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2386 7aafa0d1 2019-02-22 stsp if (errcode)
2387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2388 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2389 7c1452c1 2020-03-26 stsp
2390 7c1452c1 2020-03-26 stsp /*
2391 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2392 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2393 7c1452c1 2020-03-26 stsp */
2394 7c1452c1 2020-03-26 stsp if (!wait)
2395 7c1452c1 2020-03-26 stsp break;
2396 7c1452c1 2020-03-26 stsp
2397 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2398 ffe38506 2020-12-01 naddy show_log_view(view);
2399 7c1452c1 2020-03-26 stsp update_panels();
2400 7c1452c1 2020-03-26 stsp doupdate();
2401 7c1452c1 2020-03-26 stsp
2402 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2403 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2404 82954512 2020-02-03 stsp if (errcode)
2405 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2406 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2407 82954512 2020-02-03 stsp
2408 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2409 ffe38506 2020-12-01 naddy show_log_view(view);
2410 7c1452c1 2020-03-26 stsp update_panels();
2411 7c1452c1 2020-03-26 stsp doupdate();
2412 5e224a3e 2019-02-22 stsp }
2413 5e224a3e 2019-02-22 stsp
2414 5e224a3e 2019-02-22 stsp return NULL;
2415 5e224a3e 2019-02-22 stsp }
2416 5e224a3e 2019-02-22 stsp
2417 5e224a3e 2019-02-22 stsp static const struct got_error *
2418 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2419 9b058f45 2022-06-30 mark {
2420 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2421 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2422 2525dccb 2022-07-13 mark
2423 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2424 2525dccb 2022-07-13 mark return NULL;
2425 9b058f45 2022-06-30 mark
2426 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2427 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2428 9b058f45 2022-06-30 mark view->nscrolled = 0;
2429 9b058f45 2022-06-30 mark
2430 9b058f45 2022-06-30 mark return err;
2431 9b058f45 2022-06-30 mark }
2432 9b058f45 2022-06-30 mark
2433 9b058f45 2022-06-30 mark static const struct got_error *
2434 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2435 5e224a3e 2019-02-22 stsp {
2436 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2437 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2438 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2439 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2440 5e224a3e 2019-02-22 stsp
2441 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2442 5e224a3e 2019-02-22 stsp return NULL;
2443 5e224a3e 2019-02-22 stsp
2444 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2445 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2446 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2447 08ebd0a9 2019-02-22 stsp /*
2448 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2449 08ebd0a9 2019-02-22 stsp */
2450 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2451 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2452 5e224a3e 2019-02-22 stsp if (err)
2453 5e224a3e 2019-02-22 stsp return err;
2454 7aafa0d1 2019-02-22 stsp }
2455 b295e71b 2019-02-22 stsp
2456 7aafa0d1 2019-02-22 stsp do {
2457 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2458 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2459 88048b54 2019-02-21 stsp break;
2460 88048b54 2019-02-21 stsp
2461 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2462 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2463 aa075928 2018-05-10 stsp
2464 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2465 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2466 dd0a52c1 2018-05-20 stsp break;
2467 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2468 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2469 aa075928 2018-05-10 stsp
2470 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2471 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2472 9b058f45 2022-06-30 mark else
2473 9b058f45 2022-06-30 mark view->nscrolled = 0;
2474 9b058f45 2022-06-30 mark
2475 dd0a52c1 2018-05-20 stsp return err;
2476 07b55e75 2018-05-10 stsp }
2477 4a7f7875 2018-05-10 stsp
2478 cd0acaa7 2018-05-20 stsp static const struct got_error *
2479 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2480 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2481 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2482 cd0acaa7 2018-05-20 stsp {
2483 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2484 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2485 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2486 cd0acaa7 2018-05-20 stsp
2487 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2488 15a94983 2018-12-23 stsp if (diff_view == NULL)
2489 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2490 ea5e7bb5 2018-08-01 stsp
2491 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2492 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2493 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2494 e5a0f69f 2018-08-18 stsp if (err == NULL)
2495 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2496 cd0acaa7 2018-05-20 stsp return err;
2497 4a7f7875 2018-05-10 stsp }
2498 4a7f7875 2018-05-10 stsp
2499 80ddbec8 2018-04-29 stsp static const struct got_error *
2500 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2501 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2502 9343a5fb 2018-06-23 stsp {
2503 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2504 941e9f74 2019-05-21 stsp
2505 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2506 941e9f74 2019-05-21 stsp if (parent == NULL)
2507 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2508 941e9f74 2019-05-21 stsp
2509 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2510 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2511 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2512 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2513 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2514 941e9f74 2019-05-21 stsp s->tree = subtree;
2515 941e9f74 2019-05-21 stsp s->selected = 0;
2516 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2517 941e9f74 2019-05-21 stsp return NULL;
2518 941e9f74 2019-05-21 stsp }
2519 941e9f74 2019-05-21 stsp
2520 941e9f74 2019-05-21 stsp static const struct got_error *
2521 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2522 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2523 941e9f74 2019-05-21 stsp {
2524 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2525 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2526 941e9f74 2019-05-21 stsp const char *p;
2527 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2528 9343a5fb 2018-06-23 stsp
2529 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2530 941e9f74 2019-05-21 stsp p = path;
2531 941e9f74 2019-05-21 stsp while (*p) {
2532 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2533 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2534 56e0773d 2019-11-28 stsp char *te_name;
2535 33cbf02b 2020-01-12 stsp
2536 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2537 33cbf02b 2020-01-12 stsp p++;
2538 941e9f74 2019-05-21 stsp
2539 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2540 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2541 941e9f74 2019-05-21 stsp if (slash == NULL)
2542 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2543 33cbf02b 2020-01-12 stsp else
2544 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2545 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2546 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2547 56e0773d 2019-11-28 stsp break;
2548 941e9f74 2019-05-21 stsp }
2549 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2550 56e0773d 2019-11-28 stsp if (te == NULL) {
2551 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2552 56e0773d 2019-11-28 stsp free(te_name);
2553 941e9f74 2019-05-21 stsp break;
2554 941e9f74 2019-05-21 stsp }
2555 56e0773d 2019-11-28 stsp free(te_name);
2556 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2557 941e9f74 2019-05-21 stsp
2558 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2559 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2560 b03c880f 2019-05-21 stsp
2561 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2562 941e9f74 2019-05-21 stsp if (slash)
2563 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2564 941e9f74 2019-05-21 stsp else
2565 941e9f74 2019-05-21 stsp subpath = strdup(path);
2566 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2567 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2568 941e9f74 2019-05-21 stsp break;
2569 941e9f74 2019-05-21 stsp }
2570 941e9f74 2019-05-21 stsp
2571 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2572 941e9f74 2019-05-21 stsp subpath);
2573 941e9f74 2019-05-21 stsp if (err)
2574 941e9f74 2019-05-21 stsp break;
2575 941e9f74 2019-05-21 stsp
2576 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2577 941e9f74 2019-05-21 stsp free(tree_id);
2578 941e9f74 2019-05-21 stsp if (err)
2579 941e9f74 2019-05-21 stsp break;
2580 941e9f74 2019-05-21 stsp
2581 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2582 941e9f74 2019-05-21 stsp if (err) {
2583 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2584 941e9f74 2019-05-21 stsp break;
2585 941e9f74 2019-05-21 stsp }
2586 941e9f74 2019-05-21 stsp if (slash == NULL)
2587 941e9f74 2019-05-21 stsp break;
2588 941e9f74 2019-05-21 stsp free(subpath);
2589 941e9f74 2019-05-21 stsp subpath = NULL;
2590 941e9f74 2019-05-21 stsp p = slash;
2591 941e9f74 2019-05-21 stsp }
2592 941e9f74 2019-05-21 stsp
2593 941e9f74 2019-05-21 stsp free(subpath);
2594 1a76625f 2018-10-22 stsp return err;
2595 61266923 2020-01-14 stsp }
2596 61266923 2020-01-14 stsp
2597 61266923 2020-01-14 stsp static const struct got_error *
2598 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2599 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2600 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2601 55cccc34 2020-02-20 stsp {
2602 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2603 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2604 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2605 55cccc34 2020-02-20 stsp
2606 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2607 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2608 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2609 55cccc34 2020-02-20 stsp
2610 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2611 bc573f3b 2021-07-10 stsp if (err)
2612 55cccc34 2020-02-20 stsp return err;
2613 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2614 55cccc34 2020-02-20 stsp
2615 55cccc34 2020-02-20 stsp *new_view = tree_view;
2616 55cccc34 2020-02-20 stsp
2617 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2618 55cccc34 2020-02-20 stsp return NULL;
2619 55cccc34 2020-02-20 stsp
2620 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2621 55cccc34 2020-02-20 stsp }
2622 55cccc34 2020-02-20 stsp
2623 55cccc34 2020-02-20 stsp static const struct got_error *
2624 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2625 61266923 2020-01-14 stsp {
2626 61266923 2020-01-14 stsp sigset_t sigset;
2627 61266923 2020-01-14 stsp int errcode;
2628 61266923 2020-01-14 stsp
2629 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2630 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2631 61266923 2020-01-14 stsp
2632 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2633 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2634 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2635 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2636 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2637 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2638 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2639 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2640 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2641 61266923 2020-01-14 stsp
2642 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2643 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2644 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2645 61266923 2020-01-14 stsp
2646 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2647 61266923 2020-01-14 stsp if (errcode)
2648 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2649 61266923 2020-01-14 stsp
2650 61266923 2020-01-14 stsp return NULL;
2651 1a76625f 2018-10-22 stsp }
2652 1a76625f 2018-10-22 stsp
2653 1a76625f 2018-10-22 stsp static void *
2654 1a76625f 2018-10-22 stsp log_thread(void *arg)
2655 1a76625f 2018-10-22 stsp {
2656 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2657 1a76625f 2018-10-22 stsp int errcode = 0;
2658 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2659 1a76625f 2018-10-22 stsp int done = 0;
2660 61266923 2020-01-14 stsp
2661 5629093a 2022-07-11 stsp /*
2662 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2663 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2664 5629093a 2022-07-11 stsp */
2665 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2666 5629093a 2022-07-11 stsp if (errcode) {
2667 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2668 61266923 2020-01-14 stsp return (void *)err;
2669 5629093a 2022-07-11 stsp }
2670 1a76625f 2018-10-22 stsp
2671 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2672 5629093a 2022-07-11 stsp if (err) {
2673 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2674 5629093a 2022-07-11 stsp goto done;
2675 5629093a 2022-07-11 stsp }
2676 5629093a 2022-07-11 stsp
2677 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2678 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2679 5629093a 2022-07-11 stsp if (errcode) {
2680 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2681 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2682 5629093a 2022-07-11 stsp goto done;
2683 5629093a 2022-07-11 stsp }
2684 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2685 1a76625f 2018-10-22 stsp if (err) {
2686 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2687 5629093a 2022-07-11 stsp goto done;
2688 1a76625f 2018-10-22 stsp err = NULL;
2689 1a76625f 2018-10-22 stsp done = 1;
2690 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2691 1a76625f 2018-10-22 stsp a->commits_needed--;
2692 1a76625f 2018-10-22 stsp
2693 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2694 3abe8080 2019-04-10 stsp if (errcode) {
2695 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2696 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2697 5629093a 2022-07-11 stsp goto done;
2698 3abe8080 2019-04-10 stsp } else if (*a->quit)
2699 1a76625f 2018-10-22 stsp done = 1;
2700 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2701 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2702 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2703 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2704 1a76625f 2018-10-22 stsp }
2705 1a76625f 2018-10-22 stsp
2706 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2707 7c1452c1 2020-03-26 stsp if (errcode) {
2708 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2709 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2710 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2711 5629093a 2022-07-11 stsp goto done;
2712 7c1452c1 2020-03-26 stsp }
2713 7c1452c1 2020-03-26 stsp
2714 1a76625f 2018-10-22 stsp if (done)
2715 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2716 7c1452c1 2020-03-26 stsp else {
2717 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2718 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2719 7c1452c1 2020-03-26 stsp &tog_mutex);
2720 5629093a 2022-07-11 stsp if (errcode) {
2721 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2722 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2723 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2724 5629093a 2022-07-11 stsp goto done;
2725 5629093a 2022-07-11 stsp }
2726 21355643 2020-12-06 stsp if (*a->quit)
2727 21355643 2020-12-06 stsp done = 1;
2728 7c1452c1 2020-03-26 stsp }
2729 1a76625f 2018-10-22 stsp }
2730 1a76625f 2018-10-22 stsp }
2731 3abe8080 2019-04-10 stsp a->log_complete = 1;
2732 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2733 5629093a 2022-07-11 stsp if (errcode)
2734 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2735 5629093a 2022-07-11 stsp done:
2736 5629093a 2022-07-11 stsp if (err) {
2737 5629093a 2022-07-11 stsp tog_thread_error = 1;
2738 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2739 5629093a 2022-07-11 stsp }
2740 1a76625f 2018-10-22 stsp return (void *)err;
2741 1a76625f 2018-10-22 stsp }
2742 1a76625f 2018-10-22 stsp
2743 1a76625f 2018-10-22 stsp static const struct got_error *
2744 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2745 1a76625f 2018-10-22 stsp {
2746 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2747 1a76625f 2018-10-22 stsp int errcode;
2748 1a76625f 2018-10-22 stsp
2749 1a76625f 2018-10-22 stsp if (s->thread) {
2750 1a76625f 2018-10-22 stsp s->quit = 1;
2751 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2752 1a76625f 2018-10-22 stsp if (errcode)
2753 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2754 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2755 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2756 1a76625f 2018-10-22 stsp if (errcode)
2757 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2758 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2759 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2760 1a76625f 2018-10-22 stsp if (errcode)
2761 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2762 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2763 1a76625f 2018-10-22 stsp if (errcode)
2764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2765 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2766 1a76625f 2018-10-22 stsp s->thread = NULL;
2767 1a76625f 2018-10-22 stsp }
2768 1a76625f 2018-10-22 stsp
2769 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2770 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2771 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2772 74467cc8 2022-06-15 stsp }
2773 74467cc8 2022-06-15 stsp
2774 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2775 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2776 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2777 74467cc8 2022-06-15 stsp if (err == NULL)
2778 74467cc8 2022-06-15 stsp err = pack_err;
2779 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2780 1a76625f 2018-10-22 stsp }
2781 1a76625f 2018-10-22 stsp
2782 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2783 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2784 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2785 1a76625f 2018-10-22 stsp }
2786 1a76625f 2018-10-22 stsp
2787 5629093a 2022-07-11 stsp return err ? err : thread_err;
2788 9343a5fb 2018-06-23 stsp }
2789 9343a5fb 2018-06-23 stsp
2790 9343a5fb 2018-06-23 stsp static const struct got_error *
2791 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2792 1a76625f 2018-10-22 stsp {
2793 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2794 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2795 276b94a1 2020-11-13 naddy int errcode;
2796 1a76625f 2018-10-22 stsp
2797 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2798 276b94a1 2020-11-13 naddy
2799 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2800 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2801 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2802 276b94a1 2020-11-13 naddy
2803 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2804 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2805 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2806 276b94a1 2020-11-13 naddy
2807 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2808 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2809 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2810 1a76625f 2018-10-22 stsp free(s->start_id);
2811 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2812 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2813 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2814 1a76625f 2018-10-22 stsp return err;
2815 1a76625f 2018-10-22 stsp }
2816 1a76625f 2018-10-22 stsp
2817 1a76625f 2018-10-22 stsp static const struct got_error *
2818 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2819 60493ae3 2019-06-20 stsp {
2820 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2821 60493ae3 2019-06-20 stsp
2822 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2823 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2824 60493ae3 2019-06-20 stsp return NULL;
2825 60493ae3 2019-06-20 stsp }
2826 60493ae3 2019-06-20 stsp
2827 60493ae3 2019-06-20 stsp static const struct got_error *
2828 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2829 60493ae3 2019-06-20 stsp {
2830 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2831 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2832 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2833 60493ae3 2019-06-20 stsp
2834 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2835 f9686aa5 2020-03-27 stsp show_log_view(view);
2836 f9686aa5 2020-03-27 stsp update_panels();
2837 f9686aa5 2020-03-27 stsp doupdate();
2838 f9686aa5 2020-03-27 stsp
2839 96e2b566 2019-07-08 stsp if (s->search_entry) {
2840 13add988 2019-10-15 stsp int errcode, ch;
2841 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2842 13add988 2019-10-15 stsp if (errcode)
2843 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2844 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2845 13add988 2019-10-15 stsp ch = wgetch(view->window);
2846 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2847 13add988 2019-10-15 stsp if (errcode)
2848 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2849 13add988 2019-10-15 stsp "pthread_mutex_lock");
2850 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2851 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2852 678cbce5 2019-07-28 stsp return NULL;
2853 678cbce5 2019-07-28 stsp }
2854 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2855 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2856 96e2b566 2019-07-08 stsp else
2857 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2858 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2859 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2860 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2861 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2862 364ac6fd 2022-06-18 stsp
2863 364ac6fd 2022-06-18 stsp /*
2864 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2865 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2866 f704b35c 2022-06-18 stsp * might have changed.
2867 364ac6fd 2022-06-18 stsp */
2868 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2869 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2870 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2871 364ac6fd 2022-06-18 stsp else
2872 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2873 4bfe9f0a 2022-06-18 stsp } else {
2874 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2875 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2876 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2877 364ac6fd 2022-06-18 stsp else
2878 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2879 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2880 4bfe9f0a 2022-06-18 stsp }
2881 20be8d96 2019-06-21 stsp } else {
2882 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2883 20be8d96 2019-06-21 stsp }
2884 60493ae3 2019-06-20 stsp
2885 60493ae3 2019-06-20 stsp while (1) {
2886 13add988 2019-10-15 stsp int have_match = 0;
2887 13add988 2019-10-15 stsp
2888 60493ae3 2019-06-20 stsp if (entry == NULL) {
2889 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2890 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2891 f9967bca 2020-03-27 stsp view->search_next_done =
2892 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2893 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2894 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2895 f801134a 2019-06-25 stsp return NULL;
2896 60493ae3 2019-06-20 stsp }
2897 96e2b566 2019-07-08 stsp /*
2898 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2899 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2900 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2901 96e2b566 2019-07-08 stsp */
2902 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2903 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2904 60493ae3 2019-06-20 stsp }
2905 60493ae3 2019-06-20 stsp
2906 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2907 13add988 2019-10-15 stsp &view->regex);
2908 5943eee2 2019-08-13 stsp if (err)
2909 13add988 2019-10-15 stsp break;
2910 13add988 2019-10-15 stsp if (have_match) {
2911 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2912 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2913 60493ae3 2019-06-20 stsp break;
2914 60493ae3 2019-06-20 stsp }
2915 13add988 2019-10-15 stsp
2916 96e2b566 2019-07-08 stsp s->search_entry = entry;
2917 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2918 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2919 b1bf1435 2019-06-21 stsp else
2920 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2921 60493ae3 2019-06-20 stsp }
2922 60493ae3 2019-06-20 stsp
2923 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2924 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2925 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2926 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2927 60493ae3 2019-06-20 stsp if (err)
2928 60493ae3 2019-06-20 stsp return err;
2929 ead14cbe 2019-06-21 stsp cur++;
2930 ead14cbe 2019-06-21 stsp }
2931 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2932 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2933 60493ae3 2019-06-20 stsp if (err)
2934 60493ae3 2019-06-20 stsp return err;
2935 ead14cbe 2019-06-21 stsp cur--;
2936 60493ae3 2019-06-20 stsp }
2937 60493ae3 2019-06-20 stsp }
2938 60493ae3 2019-06-20 stsp
2939 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2940 96e2b566 2019-07-08 stsp
2941 60493ae3 2019-06-20 stsp return NULL;
2942 60493ae3 2019-06-20 stsp }
2943 60493ae3 2019-06-20 stsp
2944 60493ae3 2019-06-20 stsp static const struct got_error *
2945 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2946 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2947 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2948 80ddbec8 2018-04-29 stsp {
2949 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2950 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2951 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2952 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2953 1a76625f 2018-10-22 stsp int errcode;
2954 80ddbec8 2018-04-29 stsp
2955 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2956 f135c941 2020-02-20 stsp free(s->in_repo_path);
2957 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2958 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2959 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2960 f135c941 2020-02-20 stsp }
2961 ecb28ae0 2018-07-16 stsp
2962 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2963 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2964 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2965 78756c87 2020-11-24 stsp
2966 fb2756b9 2018-08-04 stsp s->repo = repo;
2967 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2968 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2969 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2970 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2971 9cd7cbd1 2020-12-07 stsp goto done;
2972 9cd7cbd1 2020-12-07 stsp }
2973 9cd7cbd1 2020-12-07 stsp }
2974 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2975 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2976 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2977 5036bf37 2018-09-24 stsp goto done;
2978 5036bf37 2018-09-24 stsp }
2979 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2980 e5a0f69f 2018-08-18 stsp
2981 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2982 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2983 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2984 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2985 11b20872 2019-11-08 stsp if (err)
2986 11b20872 2019-11-08 stsp goto done;
2987 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2988 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2989 11b20872 2019-11-08 stsp if (err) {
2990 11b20872 2019-11-08 stsp free_colors(&s->colors);
2991 11b20872 2019-11-08 stsp goto done;
2992 11b20872 2019-11-08 stsp }
2993 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2994 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2995 11b20872 2019-11-08 stsp if (err) {
2996 11b20872 2019-11-08 stsp free_colors(&s->colors);
2997 11b20872 2019-11-08 stsp goto done;
2998 11b20872 2019-11-08 stsp }
2999 11b20872 2019-11-08 stsp }
3000 11b20872 2019-11-08 stsp
3001 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3002 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3003 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3004 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3005 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3006 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3007 1a76625f 2018-10-22 stsp
3008 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3009 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3010 74467cc8 2022-06-15 stsp if (err)
3011 74467cc8 2022-06-15 stsp goto done;
3012 74467cc8 2022-06-15 stsp }
3013 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3014 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3015 0ae84acc 2022-06-15 tracey if (err)
3016 0ae84acc 2022-06-15 tracey goto done;
3017 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3018 b672a97a 2020-01-27 stsp !s->log_branches);
3019 1a76625f 2018-10-22 stsp if (err)
3020 1a76625f 2018-10-22 stsp goto done;
3021 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3022 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3023 c5b78334 2020-01-12 stsp if (err)
3024 c5b78334 2020-01-12 stsp goto done;
3025 1a76625f 2018-10-22 stsp
3026 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3027 1a76625f 2018-10-22 stsp if (errcode) {
3028 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3029 1a76625f 2018-10-22 stsp goto done;
3030 1a76625f 2018-10-22 stsp }
3031 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3032 7c1452c1 2020-03-26 stsp if (errcode) {
3033 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3034 7c1452c1 2020-03-26 stsp goto done;
3035 7c1452c1 2020-03-26 stsp }
3036 1a76625f 2018-10-22 stsp
3037 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3038 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3039 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3040 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3041 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3042 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3043 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3044 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3045 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3046 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3047 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3048 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3049 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3050 ba4f502b 2018-08-04 stsp done:
3051 1a76625f 2018-10-22 stsp if (err)
3052 1a76625f 2018-10-22 stsp close_log_view(view);
3053 ba4f502b 2018-08-04 stsp return err;
3054 ba4f502b 2018-08-04 stsp }
3055 ba4f502b 2018-08-04 stsp
3056 e5a0f69f 2018-08-18 stsp static const struct got_error *
3057 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3058 ba4f502b 2018-08-04 stsp {
3059 f2f6d207 2020-11-24 stsp const struct got_error *err;
3060 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3061 ba4f502b 2018-08-04 stsp
3062 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3063 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3064 2b380cc8 2018-10-24 stsp &s->thread_args);
3065 2b380cc8 2018-10-24 stsp if (errcode)
3066 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3067 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3068 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3069 f2f6d207 2020-11-24 stsp if (err)
3070 f2f6d207 2020-11-24 stsp return err;
3071 f2f6d207 2020-11-24 stsp }
3072 2b380cc8 2018-10-24 stsp }
3073 2b380cc8 2018-10-24 stsp
3074 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3075 b880cc75 2022-06-30 mark }
3076 b880cc75 2022-06-30 mark
3077 b880cc75 2022-06-30 mark static void
3078 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3079 b880cc75 2022-06-30 mark {
3080 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3081 b880cc75 2022-06-30 mark
3082 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3083 b880cc75 2022-06-30 mark view->count = 0;
3084 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3085 b880cc75 2022-06-30 mark return;
3086 b880cc75 2022-06-30 mark
3087 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3088 b880cc75 2022-06-30 mark || home)
3089 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3090 b880cc75 2022-06-30 mark
3091 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3092 b880cc75 2022-06-30 mark --s->selected;
3093 b880cc75 2022-06-30 mark else
3094 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3095 b880cc75 2022-06-30 mark
3096 b880cc75 2022-06-30 mark select_commit(s);
3097 b880cc75 2022-06-30 mark return;
3098 b880cc75 2022-06-30 mark }
3099 b880cc75 2022-06-30 mark
3100 b880cc75 2022-06-30 mark static const struct got_error *
3101 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3102 b880cc75 2022-06-30 mark {
3103 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3104 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3105 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3106 b880cc75 2022-06-30 mark
3107 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3108 b880cc75 2022-06-30 mark if (first == NULL) {
3109 b880cc75 2022-06-30 mark view->count = 0;
3110 b880cc75 2022-06-30 mark return NULL;
3111 b880cc75 2022-06-30 mark }
3112 b880cc75 2022-06-30 mark
3113 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3114 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3115 b880cc75 2022-06-30 mark return NULL;
3116 b880cc75 2022-06-30 mark
3117 b880cc75 2022-06-30 mark if (!page) {
3118 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3119 b880cc75 2022-06-30 mark
3120 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3121 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3122 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3123 b880cc75 2022-06-30 mark ++s->selected;
3124 b880cc75 2022-06-30 mark else
3125 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3126 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3127 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3128 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3129 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3130 b880cc75 2022-06-30 mark else
3131 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3132 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3133 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3134 b880cc75 2022-06-30 mark } else {
3135 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3136 b880cc75 2022-06-30 mark if (err)
3137 b880cc75 2022-06-30 mark return err;
3138 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3139 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3140 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3141 b880cc75 2022-06-30 mark }
3142 b880cc75 2022-06-30 mark }
3143 b880cc75 2022-06-30 mark if (err)
3144 b880cc75 2022-06-30 mark return err;
3145 b880cc75 2022-06-30 mark
3146 9b058f45 2022-06-30 mark /*
3147 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3148 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3149 9b058f45 2022-06-30 mark */
3150 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3151 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3152 9b058f45 2022-06-30 mark
3153 b880cc75 2022-06-30 mark select_commit(s);
3154 b880cc75 2022-06-30 mark
3155 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3156 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3157 b880cc75 2022-06-30 mark view->count = 0;
3158 b880cc75 2022-06-30 mark
3159 b880cc75 2022-06-30 mark return NULL;
3160 e5a0f69f 2018-08-18 stsp }
3161 04cc582a 2018-08-01 stsp
3162 9b058f45 2022-06-30 mark static void
3163 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3164 9b058f45 2022-06-30 mark {
3165 76364b2d 2022-07-02 mark *x = 0;
3166 76364b2d 2022-07-02 mark *y = 0;
3167 76364b2d 2022-07-02 mark
3168 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3169 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3170 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3171 7532ccda 2022-07-11 mark else if (view->resized_y)
3172 7532ccda 2022-07-11 mark *y = view->resized_y;
3173 3c1dfe12 2022-07-08 mark else
3174 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3175 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3176 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3177 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3178 7532ccda 2022-07-11 mark else if (view->resized_x)
3179 7532ccda 2022-07-11 mark *x = view->resized_x;
3180 3c1dfe12 2022-07-08 mark else
3181 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3182 3c1dfe12 2022-07-08 mark }
3183 9b058f45 2022-06-30 mark }
3184 9b058f45 2022-06-30 mark
3185 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3186 e5a0f69f 2018-08-18 stsp static const struct got_error *
3187 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3188 9b058f45 2022-06-30 mark {
3189 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3190 9b058f45 2022-06-30 mark
3191 9b058f45 2022-06-30 mark view->nlines = y;
3192 d2366e29 2022-07-07 mark view->ncols = COLS;
3193 9b058f45 2022-06-30 mark err = view_resize(view);
3194 9b058f45 2022-06-30 mark if (err)
3195 9b058f45 2022-06-30 mark return err;
3196 9b058f45 2022-06-30 mark
3197 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3198 9b058f45 2022-06-30 mark
3199 9b058f45 2022-06-30 mark return err;
3200 9b058f45 2022-06-30 mark }
3201 9b058f45 2022-06-30 mark
3202 9b058f45 2022-06-30 mark static const struct got_error *
3203 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3204 e5a0f69f 2018-08-18 stsp {
3205 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3206 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3207 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3208 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3209 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3210 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
3211 80ddbec8 2018-04-29 stsp
3212 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3213 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3214 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3215 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3216 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3217 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3218 fb280deb 2021-08-30 stsp }
3219 b880cc75 2022-06-30 mark return err;
3220 528dedf3 2021-08-30 stsp }
3221 0dca135e 2022-06-30 mark
3222 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3223 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3224 0dca135e 2022-06-30 mark --eos; /* border */
3225 0dca135e 2022-06-30 mark
3226 528dedf3 2021-08-30 stsp switch (ch) {
3227 1e37a5c2 2019-05-12 jcs case 'q':
3228 1e37a5c2 2019-05-12 jcs s->quit = 1;
3229 145b6838 2022-06-16 stsp break;
3230 145b6838 2022-06-16 stsp case '0':
3231 145b6838 2022-06-16 stsp view->x = 0;
3232 145b6838 2022-06-16 stsp break;
3233 145b6838 2022-06-16 stsp case '$':
3234 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3235 640cd7ff 2022-06-22 mark view->count = 0;
3236 145b6838 2022-06-16 stsp break;
3237 145b6838 2022-06-16 stsp case KEY_RIGHT:
3238 145b6838 2022-06-16 stsp case 'l':
3239 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3240 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3241 640cd7ff 2022-06-22 mark else
3242 640cd7ff 2022-06-22 mark view->count = 0;
3243 1e37a5c2 2019-05-12 jcs break;
3244 145b6838 2022-06-16 stsp case KEY_LEFT:
3245 145b6838 2022-06-16 stsp case 'h':
3246 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3247 640cd7ff 2022-06-22 mark if (view->x <= 0)
3248 640cd7ff 2022-06-22 mark view->count = 0;
3249 145b6838 2022-06-16 stsp break;
3250 1e37a5c2 2019-05-12 jcs case 'k':
3251 1e37a5c2 2019-05-12 jcs case KEY_UP:
3252 1e37a5c2 2019-05-12 jcs case '<':
3253 1e37a5c2 2019-05-12 jcs case ',':
3254 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3255 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3256 912a3f79 2021-08-30 j break;
3257 912a3f79 2021-08-30 j case 'g':
3258 912a3f79 2021-08-30 j case KEY_HOME:
3259 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3260 640cd7ff 2022-06-22 mark view->count = 0;
3261 1e37a5c2 2019-05-12 jcs break;
3262 83cc4199 2022-06-13 stsp case CTRL('u'):
3263 33c3719a 2022-06-15 stsp case 'u':
3264 83cc4199 2022-06-13 stsp nscroll /= 2;
3265 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3266 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3267 a4292ac5 2019-05-12 jcs case CTRL('b'):
3268 61417565 2022-06-20 mark case 'b':
3269 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3270 1e37a5c2 2019-05-12 jcs break;
3271 1e37a5c2 2019-05-12 jcs case 'j':
3272 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3273 1e37a5c2 2019-05-12 jcs case '>':
3274 1e37a5c2 2019-05-12 jcs case '.':
3275 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3276 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3277 912a3f79 2021-08-30 j break;
3278 10aab77f 2022-07-19 op case '@':
3279 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3280 10aab77f 2022-07-19 op break;
3281 912a3f79 2021-08-30 j case 'G':
3282 912a3f79 2021-08-30 j case KEY_END: {
3283 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3284 912a3f79 2021-08-30 j * traverse them all. */
3285 640cd7ff 2022-06-22 mark view->count = 0;
3286 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3287 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3288 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3289 80ddbec8 2018-04-29 stsp }
3290 912a3f79 2021-08-30 j
3291 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3292 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3293 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3294 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3295 f3bc9f1d 2021-09-05 naddy break;
3296 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3297 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3298 f3bc9f1d 2021-09-05 naddy }
3299 f3bc9f1d 2021-09-05 naddy if (n > 0)
3300 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3301 2b779855 2020-12-05 naddy select_commit(s);
3302 1e37a5c2 2019-05-12 jcs break;
3303 912a3f79 2021-08-30 j }
3304 80b7e8da 2022-06-11 stsp case CTRL('d'):
3305 33c3719a 2022-06-15 stsp case 'd':
3306 83cc4199 2022-06-13 stsp nscroll /= 2;
3307 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3308 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3309 61417565 2022-06-20 mark case CTRL('f'):
3310 48bb96f0 2022-06-20 naddy case 'f':
3311 b880cc75 2022-06-30 mark case ' ':
3312 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3313 1e37a5c2 2019-05-12 jcs break;
3314 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3315 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3316 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3317 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3318 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3319 2b779855 2020-12-05 naddy select_commit(s);
3320 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3321 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3322 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3323 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3324 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3325 0bf7f153 2020-12-02 naddy }
3326 1e37a5c2 2019-05-12 jcs break;
3327 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3328 49b24ee5 2022-07-03 mark case '\r':
3329 640cd7ff 2022-06-22 mark view->count = 0;
3330 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3331 e5a0f69f 2018-08-18 stsp break;
3332 9b058f45 2022-06-30 mark
3333 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3334 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3335 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3336 9b058f45 2022-06-30 mark
3337 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3338 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3339 78756c87 2020-11-24 stsp view, s->repo);
3340 1e37a5c2 2019-05-12 jcs if (err)
3341 1e37a5c2 2019-05-12 jcs break;
3342 9b058f45 2022-06-30 mark
3343 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3344 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3345 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3346 9b058f45 2022-06-30 mark if (err)
3347 9b058f45 2022-06-30 mark break;
3348 9b058f45 2022-06-30 mark }
3349 9b058f45 2022-06-30 mark
3350 e78dc838 2020-12-04 stsp view->focussed = 0;
3351 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3352 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3353 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3354 9b058f45 2022-06-30 mark
3355 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3356 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
3357 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3358 f7013a22 2018-10-24 stsp if (err)
3359 1e37a5c2 2019-05-12 jcs return err;
3360 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3361 0dbbbe90 2022-06-17 op if (err)
3362 0dbbbe90 2022-06-17 op return err;
3363 e78dc838 2020-12-04 stsp view->focus_child = 1;
3364 1e37a5c2 2019-05-12 jcs } else
3365 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3366 1e37a5c2 2019-05-12 jcs break;
3367 5e98fb33 2022-07-22 mark case 'T':
3368 640cd7ff 2022-06-22 mark view->count = 0;
3369 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3370 5036bf37 2018-09-24 stsp break;
3371 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3372 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3373 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3374 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3375 4e97c21c 2020-12-06 stsp s->repo);
3376 1e37a5c2 2019-05-12 jcs if (err)
3377 e5a0f69f 2018-08-18 stsp break;
3378 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3379 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3380 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3381 49b24ee5 2022-07-03 mark if (err)
3382 49b24ee5 2022-07-03 mark break;
3383 49b24ee5 2022-07-03 mark }
3384 e78dc838 2020-12-04 stsp view->focussed = 0;
3385 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3386 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3387 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3388 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3389 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
3390 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3391 1e37a5c2 2019-05-12 jcs if (err)
3392 1e37a5c2 2019-05-12 jcs return err;
3393 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3394 0dbbbe90 2022-06-17 op if (err)
3395 0dbbbe90 2022-06-17 op return err;
3396 e78dc838 2020-12-04 stsp view->focus_child = 1;
3397 1e37a5c2 2019-05-12 jcs } else
3398 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3399 1e37a5c2 2019-05-12 jcs break;
3400 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3401 21355643 2020-12-06 stsp case CTRL('l'):
3402 21355643 2020-12-06 stsp case 'B':
3403 640cd7ff 2022-06-22 mark view->count = 0;
3404 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3405 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3406 1e37a5c2 2019-05-12 jcs break;
3407 21355643 2020-12-06 stsp err = stop_log_thread(s);
3408 74cfe85e 2020-10-20 stsp if (err)
3409 74cfe85e 2020-10-20 stsp return err;
3410 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3411 21355643 2020-12-06 stsp char *parent_path;
3412 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3413 21355643 2020-12-06 stsp if (err)
3414 21355643 2020-12-06 stsp return err;
3415 21355643 2020-12-06 stsp free(s->in_repo_path);
3416 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3417 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3418 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3419 21355643 2020-12-06 stsp struct got_object_id *start_id;
3420 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3421 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3422 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3423 21355643 2020-12-06 stsp if (err)
3424 21355643 2020-12-06 stsp return err;
3425 21355643 2020-12-06 stsp free(s->start_id);
3426 21355643 2020-12-06 stsp s->start_id = start_id;
3427 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3428 21355643 2020-12-06 stsp } else /* 'B' */
3429 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3430 21355643 2020-12-06 stsp
3431 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3432 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3433 b0dd8d27 2022-06-16 stsp if (err)
3434 b0dd8d27 2022-06-16 stsp return err;
3435 b0dd8d27 2022-06-16 stsp }
3436 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3437 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3438 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3439 74cfe85e 2020-10-20 stsp if (err)
3440 21355643 2020-12-06 stsp return err;
3441 51a10b52 2020-12-26 stsp tog_free_refs();
3442 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3443 ca51c541 2020-12-07 stsp if (err)
3444 ca51c541 2020-12-07 stsp return err;
3445 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3446 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3447 d01904d4 2019-06-25 stsp if (err)
3448 d01904d4 2019-06-25 stsp return err;
3449 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3450 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3451 b672a97a 2020-01-27 stsp if (err)
3452 b672a97a 2020-01-27 stsp return err;
3453 21355643 2020-12-06 stsp free_commits(&s->commits);
3454 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3455 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3456 21355643 2020-12-06 stsp s->selected_entry = NULL;
3457 21355643 2020-12-06 stsp s->selected = 0;
3458 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3459 21355643 2020-12-06 stsp s->quit = 0;
3460 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3461 dfee752e 2022-06-18 op s->matched_entry = NULL;
3462 dfee752e 2022-06-18 op s->search_entry = NULL;
3463 01a7bcaf 2022-07-22 mark view->offset = 0;
3464 d01904d4 2019-06-25 stsp break;
3465 5e98fb33 2022-07-22 mark case 'R':
3466 640cd7ff 2022-06-22 mark view->count = 0;
3467 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3468 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3469 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3470 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3471 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3472 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3473 6458efa5 2020-11-24 stsp if (err) {
3474 6458efa5 2020-11-24 stsp view_close(ref_view);
3475 6458efa5 2020-11-24 stsp return err;
3476 6458efa5 2020-11-24 stsp }
3477 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3478 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3479 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3480 49b24ee5 2022-07-03 mark if (err)
3481 49b24ee5 2022-07-03 mark break;
3482 49b24ee5 2022-07-03 mark }
3483 e78dc838 2020-12-04 stsp view->focussed = 0;
3484 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3485 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3486 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3487 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3488 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
3489 6458efa5 2020-11-24 stsp err = view_close_child(view);
3490 6458efa5 2020-11-24 stsp if (err)
3491 6458efa5 2020-11-24 stsp return err;
3492 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3493 0dbbbe90 2022-06-17 op if (err)
3494 0dbbbe90 2022-06-17 op return err;
3495 e78dc838 2020-12-04 stsp view->focus_child = 1;
3496 6458efa5 2020-11-24 stsp } else
3497 6458efa5 2020-11-24 stsp *new_view = ref_view;
3498 6458efa5 2020-11-24 stsp break;
3499 1e37a5c2 2019-05-12 jcs default:
3500 640cd7ff 2022-06-22 mark view->count = 0;
3501 1e37a5c2 2019-05-12 jcs break;
3502 899d86c2 2018-05-10 stsp }
3503 e5a0f69f 2018-08-18 stsp
3504 80ddbec8 2018-04-29 stsp return err;
3505 80ddbec8 2018-04-29 stsp }
3506 80ddbec8 2018-04-29 stsp
3507 4ed7e80c 2018-05-20 stsp static const struct got_error *
3508 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3509 c2db6724 2019-01-04 stsp {
3510 c2db6724 2019-01-04 stsp const struct got_error *error;
3511 c2db6724 2019-01-04 stsp
3512 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3513 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3514 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3515 37c06ea4 2019-07-15 stsp #endif
3516 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3517 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3518 c2db6724 2019-01-04 stsp
3519 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3520 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3521 c2db6724 2019-01-04 stsp
3522 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3523 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3524 c2db6724 2019-01-04 stsp
3525 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3526 c2db6724 2019-01-04 stsp if (error != NULL)
3527 c2db6724 2019-01-04 stsp return error;
3528 c2db6724 2019-01-04 stsp
3529 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3530 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3531 c2db6724 2019-01-04 stsp
3532 c2db6724 2019-01-04 stsp return NULL;
3533 c2db6724 2019-01-04 stsp }
3534 c2db6724 2019-01-04 stsp
3535 a915003a 2019-02-05 stsp static void
3536 a915003a 2019-02-05 stsp init_curses(void)
3537 a915003a 2019-02-05 stsp {
3538 2497f032 2022-05-31 stsp /*
3539 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3540 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3541 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3542 2497f032 2022-05-31 stsp */
3543 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3544 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3545 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3546 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3547 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3548 2497f032 2022-05-31 stsp
3549 a915003a 2019-02-05 stsp initscr();
3550 a915003a 2019-02-05 stsp cbreak();
3551 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3552 a915003a 2019-02-05 stsp noecho();
3553 a915003a 2019-02-05 stsp nonl();
3554 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3555 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3556 a915003a 2019-02-05 stsp curs_set(0);
3557 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3558 6d17833f 2019-11-08 stsp start_color();
3559 6d17833f 2019-11-08 stsp use_default_colors();
3560 6d17833f 2019-11-08 stsp }
3561 a915003a 2019-02-05 stsp }
3562 a915003a 2019-02-05 stsp
3563 c2db6724 2019-01-04 stsp static const struct got_error *
3564 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3565 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3566 f135c941 2020-02-20 stsp {
3567 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3568 f135c941 2020-02-20 stsp
3569 f135c941 2020-02-20 stsp if (argc == 0) {
3570 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3571 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3572 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3573 f135c941 2020-02-20 stsp return NULL;
3574 f135c941 2020-02-20 stsp }
3575 f135c941 2020-02-20 stsp
3576 f135c941 2020-02-20 stsp if (worktree) {
3577 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3578 bfd61697 2020-11-14 stsp char *p;
3579 f135c941 2020-02-20 stsp
3580 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3581 f135c941 2020-02-20 stsp if (err)
3582 f135c941 2020-02-20 stsp return err;
3583 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3584 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3585 bfd61697 2020-11-14 stsp p) == -1) {
3586 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3587 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3588 f135c941 2020-02-20 stsp }
3589 f135c941 2020-02-20 stsp free(p);
3590 f135c941 2020-02-20 stsp } else
3591 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3592 f135c941 2020-02-20 stsp
3593 f135c941 2020-02-20 stsp return err;
3594 f135c941 2020-02-20 stsp }
3595 f135c941 2020-02-20 stsp
3596 f135c941 2020-02-20 stsp static const struct got_error *
3597 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3598 9f7d7167 2018-04-29 stsp {
3599 80ddbec8 2018-04-29 stsp const struct got_error *error;
3600 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3601 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3602 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3603 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3604 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3605 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3606 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3607 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3608 04cc582a 2018-08-01 stsp struct tog_view *view;
3609 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3610 80ddbec8 2018-04-29 stsp
3611 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3612 80ddbec8 2018-04-29 stsp switch (ch) {
3613 b672a97a 2020-01-27 stsp case 'b':
3614 b672a97a 2020-01-27 stsp log_branches = 1;
3615 b672a97a 2020-01-27 stsp break;
3616 80ddbec8 2018-04-29 stsp case 'c':
3617 80ddbec8 2018-04-29 stsp start_commit = optarg;
3618 80ddbec8 2018-04-29 stsp break;
3619 ecb28ae0 2018-07-16 stsp case 'r':
3620 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3621 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3622 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3623 9ba1d308 2019-10-21 stsp optarg);
3624 ecb28ae0 2018-07-16 stsp break;
3625 80ddbec8 2018-04-29 stsp default:
3626 17020d27 2019-03-07 stsp usage_log();
3627 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3628 80ddbec8 2018-04-29 stsp }
3629 80ddbec8 2018-04-29 stsp }
3630 80ddbec8 2018-04-29 stsp
3631 80ddbec8 2018-04-29 stsp argc -= optind;
3632 80ddbec8 2018-04-29 stsp argv += optind;
3633 80ddbec8 2018-04-29 stsp
3634 f135c941 2020-02-20 stsp if (argc > 1)
3635 f135c941 2020-02-20 stsp usage_log();
3636 963f97a1 2019-03-18 stsp
3637 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3638 0ae84acc 2022-06-15 tracey if (error != NULL)
3639 0ae84acc 2022-06-15 tracey goto done;
3640 0ae84acc 2022-06-15 tracey
3641 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3642 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3643 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3644 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3645 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3646 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3647 c156c7a4 2020-12-18 stsp goto done;
3648 a1fbf39a 2019-08-11 stsp if (worktree)
3649 6962eb72 2020-02-20 stsp repo_path =
3650 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3651 a1fbf39a 2019-08-11 stsp else
3652 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3653 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3654 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3655 c156c7a4 2020-12-18 stsp goto done;
3656 c156c7a4 2020-12-18 stsp }
3657 ecb28ae0 2018-07-16 stsp }
3658 ecb28ae0 2018-07-16 stsp
3659 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3660 80ddbec8 2018-04-29 stsp if (error != NULL)
3661 ecb28ae0 2018-07-16 stsp goto done;
3662 80ddbec8 2018-04-29 stsp
3663 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3664 f135c941 2020-02-20 stsp repo, worktree);
3665 f135c941 2020-02-20 stsp if (error)
3666 f135c941 2020-02-20 stsp goto done;
3667 f135c941 2020-02-20 stsp
3668 f135c941 2020-02-20 stsp init_curses();
3669 f135c941 2020-02-20 stsp
3670 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3671 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3672 c02c541e 2019-03-29 stsp if (error)
3673 c02c541e 2019-03-29 stsp goto done;
3674 c02c541e 2019-03-29 stsp
3675 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3676 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3677 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3678 87670572 2020-12-26 naddy if (error)
3679 87670572 2020-12-26 naddy goto done;
3680 87670572 2020-12-26 naddy }
3681 51a10b52 2020-12-26 stsp
3682 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3683 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3684 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3685 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3686 d8f38dc4 2020-12-05 stsp if (error)
3687 d8f38dc4 2020-12-05 stsp goto done;
3688 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3689 d8f38dc4 2020-12-05 stsp } else {
3690 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3691 d8f38dc4 2020-12-05 stsp if (error == NULL)
3692 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3693 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3694 d8f38dc4 2020-12-05 stsp goto done;
3695 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3696 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3697 d8f38dc4 2020-12-05 stsp if (error)
3698 d8f38dc4 2020-12-05 stsp goto done;
3699 d8f38dc4 2020-12-05 stsp }
3700 8b473291 2019-02-21 stsp
3701 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3702 04cc582a 2018-08-01 stsp if (view == NULL) {
3703 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3704 04cc582a 2018-08-01 stsp goto done;
3705 04cc582a 2018-08-01 stsp }
3706 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3707 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3708 ba4f502b 2018-08-04 stsp if (error)
3709 ba4f502b 2018-08-04 stsp goto done;
3710 2fc00ff4 2019-08-31 stsp if (worktree) {
3711 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3712 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3713 2fc00ff4 2019-08-31 stsp worktree = NULL;
3714 2fc00ff4 2019-08-31 stsp }
3715 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3716 ecb28ae0 2018-07-16 stsp done:
3717 f135c941 2020-02-20 stsp free(in_repo_path);
3718 ecb28ae0 2018-07-16 stsp free(repo_path);
3719 ecb28ae0 2018-07-16 stsp free(cwd);
3720 899d86c2 2018-05-10 stsp free(start_id);
3721 d8f38dc4 2020-12-05 stsp free(label);
3722 d8f38dc4 2020-12-05 stsp if (ref)
3723 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3724 1d0f4054 2021-06-17 stsp if (repo) {
3725 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3726 1d0f4054 2021-06-17 stsp if (error == NULL)
3727 1d0f4054 2021-06-17 stsp error = close_err;
3728 1d0f4054 2021-06-17 stsp }
3729 ec142235 2019-03-07 stsp if (worktree)
3730 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3731 0ae84acc 2022-06-15 tracey if (pack_fds) {
3732 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3733 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3734 0ae84acc 2022-06-15 tracey if (error == NULL)
3735 0ae84acc 2022-06-15 tracey error = pack_err;
3736 0ae84acc 2022-06-15 tracey }
3737 51a10b52 2020-12-26 stsp tog_free_refs();
3738 80ddbec8 2018-04-29 stsp return error;
3739 9f7d7167 2018-04-29 stsp }
3740 9f7d7167 2018-04-29 stsp
3741 4ed7e80c 2018-05-20 stsp __dead static void
3742 9f7d7167 2018-04-29 stsp usage_diff(void)
3743 9f7d7167 2018-04-29 stsp {
3744 80ddbec8 2018-04-29 stsp endwin();
3745 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3746 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3747 9f7d7167 2018-04-29 stsp exit(1);
3748 b304db33 2018-05-20 stsp }
3749 b304db33 2018-05-20 stsp
3750 6d17833f 2019-11-08 stsp static int
3751 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3752 41605754 2020-11-12 stsp regmatch_t *regmatch)
3753 6d17833f 2019-11-08 stsp {
3754 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3755 6d17833f 2019-11-08 stsp }
3756 6d17833f 2019-11-08 stsp
3757 336075a4 2022-06-25 op static struct tog_color *
3758 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3759 6d17833f 2019-11-08 stsp {
3760 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3761 6d17833f 2019-11-08 stsp
3762 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3763 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3764 6d17833f 2019-11-08 stsp return tc;
3765 6d17833f 2019-11-08 stsp }
3766 6d17833f 2019-11-08 stsp
3767 6d17833f 2019-11-08 stsp return NULL;
3768 6d17833f 2019-11-08 stsp }
3769 6d17833f 2019-11-08 stsp
3770 4ed7e80c 2018-05-20 stsp static const struct got_error *
3771 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3772 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3773 41605754 2020-11-12 stsp {
3774 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3775 44a87665 2022-06-16 stsp char *exstr = NULL;
3776 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3777 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3778 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3779 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3780 41605754 2020-11-12 stsp
3781 41605754 2020-11-12 stsp *wtotal = 0;
3782 1853e0f4 2022-06-16 stsp
3783 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3784 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3785 41605754 2020-11-12 stsp
3786 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3787 44a87665 2022-06-16 stsp if (err)
3788 44a87665 2022-06-16 stsp return err;
3789 44a87665 2022-06-16 stsp
3790 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3791 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3792 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3793 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3794 44a87665 2022-06-16 stsp goto done;
3795 44a87665 2022-06-16 stsp }
3796 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3797 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3798 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3799 1853e0f4 2022-06-16 stsp goto done;
3800 1853e0f4 2022-06-16 stsp }
3801 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3802 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3803 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3804 1853e0f4 2022-06-16 stsp goto done;
3805 1853e0f4 2022-06-16 stsp }
3806 145b6838 2022-06-16 stsp
3807 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3808 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3809 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3810 1853e0f4 2022-06-16 stsp if (err)
3811 1853e0f4 2022-06-16 stsp goto done;
3812 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3813 145b6838 2022-06-16 stsp if (n) {
3814 1853e0f4 2022-06-16 stsp free(wline);
3815 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3816 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3817 1853e0f4 2022-06-16 stsp if (err)
3818 1853e0f4 2022-06-16 stsp goto done;
3819 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3820 1853e0f4 2022-06-16 stsp wlimit -= width;
3821 1853e0f4 2022-06-16 stsp *wtotal += width;
3822 41605754 2020-11-12 stsp }
3823 41605754 2020-11-12 stsp
3824 41605754 2020-11-12 stsp if (wlimit > 0) {
3825 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3826 1853e0f4 2022-06-16 stsp size_t wlen;
3827 1853e0f4 2022-06-16 stsp
3828 1853e0f4 2022-06-16 stsp free(wline);
3829 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3830 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3831 1853e0f4 2022-06-16 stsp if (err)
3832 1853e0f4 2022-06-16 stsp goto done;
3833 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3834 1853e0f4 2022-06-16 stsp while (i < wlen) {
3835 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3836 1853e0f4 2022-06-16 stsp if (width == -1) {
3837 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3838 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3839 1853e0f4 2022-06-16 stsp goto done;
3840 1853e0f4 2022-06-16 stsp }
3841 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3842 1853e0f4 2022-06-16 stsp break;
3843 61417565 2022-06-20 mark w += width;
3844 1853e0f4 2022-06-16 stsp i++;
3845 41605754 2020-11-12 stsp }
3846 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3847 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3848 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3849 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3850 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3851 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3852 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3853 41605754 2020-11-12 stsp }
3854 41605754 2020-11-12 stsp }
3855 41605754 2020-11-12 stsp
3856 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3857 1853e0f4 2022-06-16 stsp free(wline);
3858 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3859 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3860 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3861 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3862 1853e0f4 2022-06-16 stsp if (err)
3863 1853e0f4 2022-06-16 stsp goto done;
3864 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3865 1853e0f4 2022-06-16 stsp } else {
3866 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3867 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3868 1853e0f4 2022-06-16 stsp if (err)
3869 1853e0f4 2022-06-16 stsp goto done;
3870 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3871 1853e0f4 2022-06-16 stsp }
3872 1853e0f4 2022-06-16 stsp *wtotal += width2;
3873 41605754 2020-11-12 stsp }
3874 1853e0f4 2022-06-16 stsp done:
3875 145b6838 2022-06-16 stsp free(wline);
3876 44a87665 2022-06-16 stsp free(exstr);
3877 1853e0f4 2022-06-16 stsp free(seg0);
3878 1853e0f4 2022-06-16 stsp free(seg1);
3879 1853e0f4 2022-06-16 stsp free(seg2);
3880 1853e0f4 2022-06-16 stsp return err;
3881 41605754 2020-11-12 stsp }
3882 41605754 2020-11-12 stsp
3883 41605754 2020-11-12 stsp static const struct got_error *
3884 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3885 26ed57b2 2018-05-19 stsp {
3886 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3887 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3888 61e69b96 2018-05-20 stsp const struct got_error *err;
3889 fe621944 2020-11-10 stsp int nprinted = 0;
3890 b304db33 2018-05-20 stsp char *line;
3891 826082fe 2020-12-10 stsp size_t linesize = 0;
3892 826082fe 2020-12-10 stsp ssize_t linelen;
3893 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3894 61e69b96 2018-05-20 stsp wchar_t *wline;
3895 e0b650dd 2018-05-20 stsp int width;
3896 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3897 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3898 fe621944 2020-11-10 stsp off_t line_offset;
3899 26ed57b2 2018-05-19 stsp
3900 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3901 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3902 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3903 fe621944 2020-11-10 stsp
3904 f7d12f7e 2018-08-01 stsp werase(view->window);
3905 a3404814 2018-09-02 stsp
3906 a3404814 2018-09-02 stsp if (header) {
3907 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3908 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3909 135a2da0 2020-11-11 stsp header) == -1)
3910 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3911 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3912 ccda2f4d 2022-06-16 stsp 0, 0);
3913 135a2da0 2020-11-11 stsp free(line);
3914 135a2da0 2020-11-11 stsp if (err)
3915 a3404814 2018-09-02 stsp return err;
3916 a3404814 2018-09-02 stsp
3917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3918 a3404814 2018-09-02 stsp wstandout(view->window);
3919 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3920 e54cc94a 2020-11-11 stsp free(wline);
3921 e54cc94a 2020-11-11 stsp wline = NULL;
3922 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3923 a3404814 2018-09-02 stsp wstandend(view->window);
3924 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3925 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3926 26ed57b2 2018-05-19 stsp
3927 a3404814 2018-09-02 stsp if (max_lines <= 1)
3928 a3404814 2018-09-02 stsp return NULL;
3929 a3404814 2018-09-02 stsp max_lines--;
3930 a3404814 2018-09-02 stsp }
3931 a3404814 2018-09-02 stsp
3932 89f1a395 2020-12-01 naddy s->eof = 0;
3933 145b6838 2022-06-16 stsp view->maxx = 0;
3934 826082fe 2020-12-10 stsp line = NULL;
3935 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3936 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3937 826082fe 2020-12-10 stsp if (linelen == -1) {
3938 826082fe 2020-12-10 stsp if (feof(s->f)) {
3939 826082fe 2020-12-10 stsp s->eof = 1;
3940 826082fe 2020-12-10 stsp break;
3941 826082fe 2020-12-10 stsp }
3942 826082fe 2020-12-10 stsp free(line);
3943 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3944 61e69b96 2018-05-20 stsp }
3945 145b6838 2022-06-16 stsp
3946 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3947 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3948 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3949 1853e0f4 2022-06-16 stsp if (err) {
3950 1853e0f4 2022-06-16 stsp free(line);
3951 1853e0f4 2022-06-16 stsp return err;
3952 1853e0f4 2022-06-16 stsp }
3953 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3954 1853e0f4 2022-06-16 stsp free(wline);
3955 1853e0f4 2022-06-16 stsp wline = NULL;
3956 1853e0f4 2022-06-16 stsp
3957 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3958 f26dddb7 2019-11-08 stsp if (tc)
3959 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3960 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3961 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3962 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3963 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3964 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3965 41605754 2020-11-12 stsp if (err) {
3966 41605754 2020-11-12 stsp free(line);
3967 41605754 2020-11-12 stsp return err;
3968 41605754 2020-11-12 stsp }
3969 41605754 2020-11-12 stsp } else {
3970 969c159c 2022-06-16 stsp int skip;
3971 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3972 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3973 969c159c 2022-06-16 stsp if (err) {
3974 969c159c 2022-06-16 stsp free(line);
3975 969c159c 2022-06-16 stsp return err;
3976 969c159c 2022-06-16 stsp }
3977 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3978 969c159c 2022-06-16 stsp free(wline);
3979 41605754 2020-11-12 stsp wline = NULL;
3980 41605754 2020-11-12 stsp }
3981 f26dddb7 2019-11-08 stsp if (tc)
3982 6d17833f 2019-11-08 stsp wattr_off(view->window,
3983 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3984 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3985 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3986 fe621944 2020-11-10 stsp nprinted++;
3987 826082fe 2020-12-10 stsp }
3988 826082fe 2020-12-10 stsp free(line);
3989 fe621944 2020-11-10 stsp if (nprinted >= 1)
3990 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3991 89f1a395 2020-12-01 naddy (nprinted - 1);
3992 fe621944 2020-11-10 stsp else
3993 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3994 26ed57b2 2018-05-19 stsp
3995 9b058f45 2022-06-30 mark view_border(view);
3996 c3e9aa98 2019-05-13 jcs
3997 89f1a395 2020-12-01 naddy if (s->eof) {
3998 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3999 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4000 c3e9aa98 2019-05-13 jcs nprinted++;
4001 c3e9aa98 2019-05-13 jcs }
4002 c3e9aa98 2019-05-13 jcs
4003 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4004 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4005 c3e9aa98 2019-05-13 jcs if (err) {
4006 c3e9aa98 2019-05-13 jcs return err;
4007 c3e9aa98 2019-05-13 jcs }
4008 26ed57b2 2018-05-19 stsp
4009 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4010 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4011 e54cc94a 2020-11-11 stsp free(wline);
4012 e54cc94a 2020-11-11 stsp wline = NULL;
4013 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4014 c3e9aa98 2019-05-13 jcs }
4015 c3e9aa98 2019-05-13 jcs
4016 26ed57b2 2018-05-19 stsp return NULL;
4017 abd2672a 2018-12-23 stsp }
4018 abd2672a 2018-12-23 stsp
4019 abd2672a 2018-12-23 stsp static char *
4020 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4021 abd2672a 2018-12-23 stsp {
4022 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4023 09867e48 2019-08-13 stsp char *p, *s;
4024 09867e48 2019-08-13 stsp
4025 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4026 09867e48 2019-08-13 stsp if (tm == NULL)
4027 09867e48 2019-08-13 stsp return NULL;
4028 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4029 09867e48 2019-08-13 stsp if (s == NULL)
4030 09867e48 2019-08-13 stsp return NULL;
4031 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4032 abd2672a 2018-12-23 stsp if (p)
4033 abd2672a 2018-12-23 stsp *p = '\0';
4034 abd2672a 2018-12-23 stsp return s;
4035 9f7d7167 2018-04-29 stsp }
4036 9f7d7167 2018-04-29 stsp
4037 4ed7e80c 2018-05-20 stsp static const struct got_error *
4038 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4039 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4040 0208f208 2020-05-05 stsp {
4041 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4042 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4043 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4044 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4045 0208f208 2020-05-05 stsp
4046 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4047 0208f208 2020-05-05 stsp if (qid != NULL) {
4048 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4049 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4050 d7b5a0e8 2022-04-20 stsp &qid->id);
4051 0208f208 2020-05-05 stsp if (err)
4052 0208f208 2020-05-05 stsp return err;
4053 0208f208 2020-05-05 stsp
4054 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4055 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4056 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4057 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4058 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4059 aa8b5dd0 2021-08-01 stsp }
4060 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4061 0208f208 2020-05-05 stsp
4062 0208f208 2020-05-05 stsp }
4063 0208f208 2020-05-05 stsp
4064 0208f208 2020-05-05 stsp if (tree_id1) {
4065 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4066 0208f208 2020-05-05 stsp if (err)
4067 0208f208 2020-05-05 stsp goto done;
4068 0208f208 2020-05-05 stsp }
4069 0208f208 2020-05-05 stsp
4070 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4071 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4072 0208f208 2020-05-05 stsp if (err)
4073 0208f208 2020-05-05 stsp goto done;
4074 0208f208 2020-05-05 stsp
4075 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4076 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4077 0208f208 2020-05-05 stsp done:
4078 0208f208 2020-05-05 stsp if (tree1)
4079 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4080 0208f208 2020-05-05 stsp if (tree2)
4081 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4082 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4083 0208f208 2020-05-05 stsp return err;
4084 0208f208 2020-05-05 stsp }
4085 0208f208 2020-05-05 stsp
4086 0208f208 2020-05-05 stsp static const struct got_error *
4087 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4088 abd2672a 2018-12-23 stsp {
4089 fe621944 2020-11-10 stsp off_t *p;
4090 fe621944 2020-11-10 stsp
4091 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4092 fe621944 2020-11-10 stsp if (p == NULL)
4093 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4094 fe621944 2020-11-10 stsp *line_offsets = p;
4095 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4096 fe621944 2020-11-10 stsp (*nlines)++;
4097 fe621944 2020-11-10 stsp return NULL;
4098 fe621944 2020-11-10 stsp }
4099 fe621944 2020-11-10 stsp
4100 fe621944 2020-11-10 stsp static const struct got_error *
4101 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4102 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4103 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4104 fe621944 2020-11-10 stsp {
4105 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4106 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4107 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4108 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4109 45d799e2 2018-12-23 stsp time_t committer_time;
4110 45d799e2 2018-12-23 stsp const char *author, *committer;
4111 8b473291 2019-02-21 stsp char *refs_str = NULL;
4112 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4113 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4114 fe621944 2020-11-10 stsp off_t outoff = 0;
4115 fe621944 2020-11-10 stsp int n;
4116 abd2672a 2018-12-23 stsp
4117 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4118 0208f208 2020-05-05 stsp
4119 8b473291 2019-02-21 stsp if (refs) {
4120 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4121 8b473291 2019-02-21 stsp if (err)
4122 8b473291 2019-02-21 stsp return err;
4123 8b473291 2019-02-21 stsp }
4124 8b473291 2019-02-21 stsp
4125 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4126 abd2672a 2018-12-23 stsp if (err)
4127 abd2672a 2018-12-23 stsp return err;
4128 abd2672a 2018-12-23 stsp
4129 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4130 15a94983 2018-12-23 stsp if (err) {
4131 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4132 15a94983 2018-12-23 stsp goto done;
4133 15a94983 2018-12-23 stsp }
4134 abd2672a 2018-12-23 stsp
4135 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4136 fe621944 2020-11-10 stsp if (err)
4137 fe621944 2020-11-10 stsp goto done;
4138 fe621944 2020-11-10 stsp
4139 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4140 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4141 fe621944 2020-11-10 stsp if (n < 0) {
4142 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4143 abd2672a 2018-12-23 stsp goto done;
4144 abd2672a 2018-12-23 stsp }
4145 fe621944 2020-11-10 stsp outoff += n;
4146 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4147 fe621944 2020-11-10 stsp if (err)
4148 fe621944 2020-11-10 stsp goto done;
4149 fe621944 2020-11-10 stsp
4150 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4151 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4152 fe621944 2020-11-10 stsp if (n < 0) {
4153 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4154 abd2672a 2018-12-23 stsp goto done;
4155 abd2672a 2018-12-23 stsp }
4156 fe621944 2020-11-10 stsp outoff += n;
4157 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4158 fe621944 2020-11-10 stsp if (err)
4159 fe621944 2020-11-10 stsp goto done;
4160 fe621944 2020-11-10 stsp
4161 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4162 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4163 fe621944 2020-11-10 stsp if (datestr) {
4164 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4165 fe621944 2020-11-10 stsp if (n < 0) {
4166 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4167 fe621944 2020-11-10 stsp goto done;
4168 fe621944 2020-11-10 stsp }
4169 fe621944 2020-11-10 stsp outoff += n;
4170 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4171 fe621944 2020-11-10 stsp if (err)
4172 fe621944 2020-11-10 stsp goto done;
4173 abd2672a 2018-12-23 stsp }
4174 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4175 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4176 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4177 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4178 fe621944 2020-11-10 stsp if (n < 0) {
4179 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4180 fe621944 2020-11-10 stsp goto done;
4181 fe621944 2020-11-10 stsp }
4182 fe621944 2020-11-10 stsp outoff += n;
4183 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4184 fe621944 2020-11-10 stsp if (err)
4185 fe621944 2020-11-10 stsp goto done;
4186 abd2672a 2018-12-23 stsp }
4187 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4188 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4189 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4190 9f98ca05 2021-09-24 stsp int pn = 1;
4191 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4192 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4193 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4194 9f98ca05 2021-09-24 stsp if (err)
4195 9f98ca05 2021-09-24 stsp goto done;
4196 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4197 9f98ca05 2021-09-24 stsp if (n < 0) {
4198 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4199 9f98ca05 2021-09-24 stsp goto done;
4200 9f98ca05 2021-09-24 stsp }
4201 9f98ca05 2021-09-24 stsp outoff += n;
4202 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4203 9f98ca05 2021-09-24 stsp if (err)
4204 9f98ca05 2021-09-24 stsp goto done;
4205 9f98ca05 2021-09-24 stsp free(id_str);
4206 9f98ca05 2021-09-24 stsp id_str = NULL;
4207 9f98ca05 2021-09-24 stsp }
4208 9f98ca05 2021-09-24 stsp }
4209 9f98ca05 2021-09-24 stsp
4210 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4211 5943eee2 2019-08-13 stsp if (err)
4212 5943eee2 2019-08-13 stsp goto done;
4213 fe621944 2020-11-10 stsp s = logmsg;
4214 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4215 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4216 fe621944 2020-11-10 stsp if (n < 0) {
4217 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4218 fe621944 2020-11-10 stsp goto done;
4219 fe621944 2020-11-10 stsp }
4220 fe621944 2020-11-10 stsp outoff += n;
4221 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4222 fe621944 2020-11-10 stsp if (err)
4223 fe621944 2020-11-10 stsp goto done;
4224 abd2672a 2018-12-23 stsp }
4225 fe621944 2020-11-10 stsp
4226 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4227 0208f208 2020-05-05 stsp if (err)
4228 0208f208 2020-05-05 stsp goto done;
4229 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4230 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4231 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4232 fe621944 2020-11-10 stsp if (n < 0) {
4233 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4234 fe621944 2020-11-10 stsp goto done;
4235 fe621944 2020-11-10 stsp }
4236 fe621944 2020-11-10 stsp outoff += n;
4237 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4238 fe621944 2020-11-10 stsp if (err)
4239 fe621944 2020-11-10 stsp goto done;
4240 0208f208 2020-05-05 stsp free((char *)pe->path);
4241 0208f208 2020-05-05 stsp free(pe->data);
4242 0208f208 2020-05-05 stsp }
4243 fe621944 2020-11-10 stsp
4244 0208f208 2020-05-05 stsp fputc('\n', outfile);
4245 fe621944 2020-11-10 stsp outoff++;
4246 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4247 abd2672a 2018-12-23 stsp done:
4248 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4249 abd2672a 2018-12-23 stsp free(id_str);
4250 5943eee2 2019-08-13 stsp free(logmsg);
4251 8b473291 2019-02-21 stsp free(refs_str);
4252 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4253 7510f233 2020-08-09 stsp if (err) {
4254 ae6a6978 2020-08-09 stsp free(*line_offsets);
4255 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4256 ae6a6978 2020-08-09 stsp *nlines = 0;
4257 7510f233 2020-08-09 stsp }
4258 fe621944 2020-11-10 stsp return err;
4259 abd2672a 2018-12-23 stsp }
4260 abd2672a 2018-12-23 stsp
4261 abd2672a 2018-12-23 stsp static const struct got_error *
4262 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4263 26ed57b2 2018-05-19 stsp {
4264 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4265 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4266 15a94983 2018-12-23 stsp int obj_type;
4267 fe621944 2020-11-10 stsp
4268 fe621944 2020-11-10 stsp free(s->line_offsets);
4269 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4270 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4271 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4272 fe621944 2020-11-10 stsp s->nlines = 0;
4273 26ed57b2 2018-05-19 stsp
4274 511a516b 2018-05-19 stsp f = got_opentemp();
4275 48ae06ee 2018-10-18 stsp if (f == NULL) {
4276 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4277 48ae06ee 2018-10-18 stsp goto done;
4278 48ae06ee 2018-10-18 stsp }
4279 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4280 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4281 fb43ecf1 2019-02-11 stsp goto done;
4282 fb43ecf1 2019-02-11 stsp }
4283 48ae06ee 2018-10-18 stsp s->f = f;
4284 26ed57b2 2018-05-19 stsp
4285 15a94983 2018-12-23 stsp if (s->id1)
4286 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4287 15a94983 2018-12-23 stsp else
4288 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4289 15a94983 2018-12-23 stsp if (err)
4290 15a94983 2018-12-23 stsp goto done;
4291 15a94983 2018-12-23 stsp
4292 15a94983 2018-12-23 stsp switch (obj_type) {
4293 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4294 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4295 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4296 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4297 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4298 26ed57b2 2018-05-19 stsp break;
4299 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4300 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4301 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4302 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4304 26ed57b2 2018-05-19 stsp break;
4305 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4306 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4307 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4308 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4309 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4310 abd2672a 2018-12-23 stsp
4311 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4312 abd2672a 2018-12-23 stsp if (err)
4313 3ffacbe1 2020-02-02 tracey goto done;
4314 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4315 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4316 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4317 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4318 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4319 f44b1f58 2020-02-02 tracey if (err)
4320 f44b1f58 2020-02-02 tracey goto done;
4321 f44b1f58 2020-02-02 tracey } else {
4322 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4323 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4324 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4325 fe621944 2020-11-10 stsp err = write_commit_info(
4326 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4327 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4328 f44b1f58 2020-02-02 tracey if (err)
4329 f44b1f58 2020-02-02 tracey goto done;
4330 f5404e4e 2020-02-02 tracey break;
4331 15a087fe 2019-02-21 stsp }
4332 abd2672a 2018-12-23 stsp }
4333 abd2672a 2018-12-23 stsp }
4334 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4335 abd2672a 2018-12-23 stsp
4336 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4337 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4338 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4339 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4340 26ed57b2 2018-05-19 stsp break;
4341 abd2672a 2018-12-23 stsp }
4342 26ed57b2 2018-05-19 stsp default:
4343 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4344 48ae06ee 2018-10-18 stsp break;
4345 26ed57b2 2018-05-19 stsp }
4346 f44b1f58 2020-02-02 tracey if (err)
4347 f44b1f58 2020-02-02 tracey goto done;
4348 48ae06ee 2018-10-18 stsp done:
4349 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4350 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4351 48ae06ee 2018-10-18 stsp return err;
4352 48ae06ee 2018-10-18 stsp }
4353 26ed57b2 2018-05-19 stsp
4354 f5215bb9 2019-02-22 stsp static void
4355 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4356 f5215bb9 2019-02-22 stsp {
4357 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4358 f5215bb9 2019-02-22 stsp update_panels();
4359 f5215bb9 2019-02-22 stsp doupdate();
4360 f44b1f58 2020-02-02 tracey }
4361 f44b1f58 2020-02-02 tracey
4362 f44b1f58 2020-02-02 tracey static const struct got_error *
4363 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4364 f44b1f58 2020-02-02 tracey {
4365 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4366 f44b1f58 2020-02-02 tracey
4367 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4368 f44b1f58 2020-02-02 tracey return NULL;
4369 f44b1f58 2020-02-02 tracey }
4370 f44b1f58 2020-02-02 tracey
4371 f44b1f58 2020-02-02 tracey static const struct got_error *
4372 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4373 f44b1f58 2020-02-02 tracey {
4374 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4375 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4376 f44b1f58 2020-02-02 tracey int lineno;
4377 cb713507 2022-06-16 stsp char *line = NULL;
4378 826082fe 2020-12-10 stsp size_t linesize = 0;
4379 826082fe 2020-12-10 stsp ssize_t linelen;
4380 f44b1f58 2020-02-02 tracey
4381 f44b1f58 2020-02-02 tracey if (!view->searching) {
4382 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 f44b1f58 2020-02-02 tracey return NULL;
4384 f44b1f58 2020-02-02 tracey }
4385 f44b1f58 2020-02-02 tracey
4386 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4387 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4388 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4389 f44b1f58 2020-02-02 tracey else
4390 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4391 487cd7d2 2021-12-17 stsp } else
4392 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4393 f44b1f58 2020-02-02 tracey
4394 f44b1f58 2020-02-02 tracey while (1) {
4395 f44b1f58 2020-02-02 tracey off_t offset;
4396 f44b1f58 2020-02-02 tracey
4397 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4398 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4399 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4400 f44b1f58 2020-02-02 tracey break;
4401 f44b1f58 2020-02-02 tracey }
4402 f44b1f58 2020-02-02 tracey
4403 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4404 f44b1f58 2020-02-02 tracey lineno = 1;
4405 f44b1f58 2020-02-02 tracey else
4406 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4407 f44b1f58 2020-02-02 tracey }
4408 f44b1f58 2020-02-02 tracey
4409 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4410 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4411 f44b1f58 2020-02-02 tracey free(line);
4412 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4413 f44b1f58 2020-02-02 tracey }
4414 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4415 cb713507 2022-06-16 stsp if (linelen != -1) {
4416 cb713507 2022-06-16 stsp char *exstr;
4417 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4418 cb713507 2022-06-16 stsp if (err)
4419 cb713507 2022-06-16 stsp break;
4420 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4421 cb713507 2022-06-16 stsp &view->regmatch)) {
4422 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 cb713507 2022-06-16 stsp s->matched_line = lineno;
4424 cb713507 2022-06-16 stsp free(exstr);
4425 cb713507 2022-06-16 stsp break;
4426 cb713507 2022-06-16 stsp }
4427 cb713507 2022-06-16 stsp free(exstr);
4428 f44b1f58 2020-02-02 tracey }
4429 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4430 f44b1f58 2020-02-02 tracey lineno++;
4431 f44b1f58 2020-02-02 tracey else
4432 f44b1f58 2020-02-02 tracey lineno--;
4433 f44b1f58 2020-02-02 tracey }
4434 826082fe 2020-12-10 stsp free(line);
4435 f44b1f58 2020-02-02 tracey
4436 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4437 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4438 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4439 f44b1f58 2020-02-02 tracey }
4440 f44b1f58 2020-02-02 tracey
4441 145b6838 2022-06-16 stsp return err;
4442 f5215bb9 2019-02-22 stsp }
4443 f5215bb9 2019-02-22 stsp
4444 48ae06ee 2018-10-18 stsp static const struct got_error *
4445 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4446 b72706c3 2022-06-01 stsp {
4447 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4448 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4449 b72706c3 2022-06-01 stsp
4450 b72706c3 2022-06-01 stsp free(s->id1);
4451 b72706c3 2022-06-01 stsp s->id1 = NULL;
4452 b72706c3 2022-06-01 stsp free(s->id2);
4453 b72706c3 2022-06-01 stsp s->id2 = NULL;
4454 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4455 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4456 b72706c3 2022-06-01 stsp s->f = NULL;
4457 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4458 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4459 b72706c3 2022-06-01 stsp s->f1 = NULL;
4460 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4461 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4462 b72706c3 2022-06-01 stsp s->f2 = NULL;
4463 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4464 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4465 f9d37699 2022-06-28 stsp s->fd1 = -1;
4466 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4467 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4468 f9d37699 2022-06-28 stsp s->fd2 = -1;
4469 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4470 b72706c3 2022-06-01 stsp free(s->line_offsets);
4471 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4472 b72706c3 2022-06-01 stsp s->nlines = 0;
4473 b72706c3 2022-06-01 stsp return err;
4474 b72706c3 2022-06-01 stsp }
4475 b72706c3 2022-06-01 stsp
4476 b72706c3 2022-06-01 stsp static const struct got_error *
4477 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4478 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4479 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4480 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4481 48ae06ee 2018-10-18 stsp {
4482 48ae06ee 2018-10-18 stsp const struct got_error *err;
4483 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4484 5dc9f4bc 2018-08-04 stsp
4485 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4486 f9d37699 2022-06-28 stsp s->fd1 = -1;
4487 f9d37699 2022-06-28 stsp s->fd2 = -1;
4488 b72706c3 2022-06-01 stsp
4489 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4490 15a94983 2018-12-23 stsp int type1, type2;
4491 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4492 15a94983 2018-12-23 stsp if (err)
4493 15a94983 2018-12-23 stsp return err;
4494 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4495 15a94983 2018-12-23 stsp if (err)
4496 15a94983 2018-12-23 stsp return err;
4497 15a94983 2018-12-23 stsp
4498 15a94983 2018-12-23 stsp if (type1 != type2)
4499 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4500 15a94983 2018-12-23 stsp }
4501 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4502 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4503 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4504 f44b1f58 2020-02-02 tracey s->repo = repo;
4505 f44b1f58 2020-02-02 tracey s->id1 = id1;
4506 f44b1f58 2020-02-02 tracey s->id2 = id2;
4507 3dbaef42 2020-11-24 stsp s->label1 = label1;
4508 3dbaef42 2020-11-24 stsp s->label2 = label2;
4509 48ae06ee 2018-10-18 stsp
4510 15a94983 2018-12-23 stsp if (id1) {
4511 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4512 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4513 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4514 48ae06ee 2018-10-18 stsp } else
4515 5465d566 2020-02-01 tracey s->id1 = NULL;
4516 48ae06ee 2018-10-18 stsp
4517 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4518 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4519 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4520 b72706c3 2022-06-01 stsp goto done;
4521 48ae06ee 2018-10-18 stsp }
4522 b72706c3 2022-06-01 stsp
4523 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4524 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4525 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4526 a00719e9 2022-06-17 stsp goto done;
4527 a00719e9 2022-06-17 stsp }
4528 a00719e9 2022-06-17 stsp
4529 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4530 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4531 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4532 b72706c3 2022-06-01 stsp goto done;
4533 b72706c3 2022-06-01 stsp }
4534 b72706c3 2022-06-01 stsp
4535 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4536 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4537 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4538 f9d37699 2022-06-28 stsp goto done;
4539 f9d37699 2022-06-28 stsp }
4540 f9d37699 2022-06-28 stsp
4541 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4542 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4543 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4544 f9d37699 2022-06-28 stsp goto done;
4545 f9d37699 2022-06-28 stsp }
4546 f9d37699 2022-06-28 stsp
4547 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4548 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4549 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4550 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4551 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4552 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4553 5465d566 2020-02-01 tracey s->repo = repo;
4554 6d17833f 2019-11-08 stsp
4555 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4556 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4558 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4559 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4560 6d17833f 2019-11-08 stsp if (err)
4561 b72706c3 2022-06-01 stsp goto done;
4562 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4563 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4565 b72706c3 2022-06-01 stsp if (err)
4566 b72706c3 2022-06-01 stsp goto done;
4567 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4568 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4569 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4570 b72706c3 2022-06-01 stsp if (err)
4571 b72706c3 2022-06-01 stsp goto done;
4572 6d17833f 2019-11-08 stsp
4573 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4574 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4575 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4576 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4577 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4578 b72706c3 2022-06-01 stsp if (err)
4579 b72706c3 2022-06-01 stsp goto done;
4580 11b20872 2019-11-08 stsp
4581 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4582 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4583 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4584 b72706c3 2022-06-01 stsp if (err)
4585 b72706c3 2022-06-01 stsp goto done;
4586 11b20872 2019-11-08 stsp
4587 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4588 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4589 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4590 b72706c3 2022-06-01 stsp if (err)
4591 b72706c3 2022-06-01 stsp goto done;
4592 6d17833f 2019-11-08 stsp }
4593 5dc9f4bc 2018-08-04 stsp
4594 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4595 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4596 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4597 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4598 f5215bb9 2019-02-22 stsp
4599 5465d566 2020-02-01 tracey err = create_diff(s);
4600 48ae06ee 2018-10-18 stsp
4601 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4602 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4603 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4604 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4605 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4606 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4607 b72706c3 2022-06-01 stsp done:
4608 b72706c3 2022-06-01 stsp if (err)
4609 b72706c3 2022-06-01 stsp close_diff_view(view);
4610 e5a0f69f 2018-08-18 stsp return err;
4611 5dc9f4bc 2018-08-04 stsp }
4612 5dc9f4bc 2018-08-04 stsp
4613 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4614 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4615 5dc9f4bc 2018-08-04 stsp {
4616 a3404814 2018-09-02 stsp const struct got_error *err;
4617 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4618 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4619 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4620 a3404814 2018-09-02 stsp
4621 a3404814 2018-09-02 stsp if (s->id1) {
4622 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4623 a3404814 2018-09-02 stsp if (err)
4624 a3404814 2018-09-02 stsp return err;
4625 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4626 3dbaef42 2020-11-24 stsp } else
4627 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4628 3dbaef42 2020-11-24 stsp
4629 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4630 a3404814 2018-09-02 stsp if (err)
4631 a3404814 2018-09-02 stsp return err;
4632 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4633 26ed57b2 2018-05-19 stsp
4634 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4635 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4636 a3404814 2018-09-02 stsp free(id_str1);
4637 a3404814 2018-09-02 stsp free(id_str2);
4638 a3404814 2018-09-02 stsp return err;
4639 a3404814 2018-09-02 stsp }
4640 a3404814 2018-09-02 stsp free(id_str1);
4641 a3404814 2018-09-02 stsp free(id_str2);
4642 a3404814 2018-09-02 stsp
4643 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4644 267bb3b8 2021-08-01 stsp free(header);
4645 267bb3b8 2021-08-01 stsp return err;
4646 15a087fe 2019-02-21 stsp }
4647 15a087fe 2019-02-21 stsp
4648 15a087fe 2019-02-21 stsp static const struct got_error *
4649 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4650 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4651 15a087fe 2019-02-21 stsp {
4652 d7a04538 2019-02-21 stsp const struct got_error *err;
4653 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4654 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4655 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4656 15a087fe 2019-02-21 stsp
4657 15a087fe 2019-02-21 stsp free(s->id2);
4658 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4659 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4660 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4661 15a087fe 2019-02-21 stsp
4662 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4663 d7a04538 2019-02-21 stsp if (err)
4664 d7a04538 2019-02-21 stsp return err;
4665 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4666 15a087fe 2019-02-21 stsp free(s->id1);
4667 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4668 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4669 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4670 15a087fe 2019-02-21 stsp return NULL;
4671 0cf4efb1 2018-09-29 stsp }
4672 0cf4efb1 2018-09-29 stsp
4673 0cf4efb1 2018-09-29 stsp static const struct got_error *
4674 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4675 917d79a7 2022-07-01 stsp {
4676 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4677 917d79a7 2022-07-01 stsp
4678 917d79a7 2022-07-01 stsp view->count = 0;
4679 917d79a7 2022-07-01 stsp wclear(view->window);
4680 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4681 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4682 917d79a7 2022-07-01 stsp s->matched_line = 0;
4683 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4684 917d79a7 2022-07-01 stsp return create_diff(s);
4685 917d79a7 2022-07-01 stsp }
4686 917d79a7 2022-07-01 stsp
4687 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4688 c0f61fa4 2022-07-11 mark int, int, int);
4689 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4690 c0f61fa4 2022-07-11 mark int, int);
4691 c0f61fa4 2022-07-11 mark
4692 917d79a7 2022-07-01 stsp static const struct got_error *
4693 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4694 e5a0f69f 2018-08-18 stsp {
4695 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4696 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4697 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4698 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4699 826082fe 2020-12-10 stsp char *line = NULL;
4700 826082fe 2020-12-10 stsp size_t linesize = 0;
4701 826082fe 2020-12-10 stsp ssize_t linelen;
4702 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4703 e5a0f69f 2018-08-18 stsp
4704 e5a0f69f 2018-08-18 stsp switch (ch) {
4705 145b6838 2022-06-16 stsp case '0':
4706 145b6838 2022-06-16 stsp view->x = 0;
4707 145b6838 2022-06-16 stsp break;
4708 145b6838 2022-06-16 stsp case '$':
4709 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4710 640cd7ff 2022-06-22 mark view->count = 0;
4711 145b6838 2022-06-16 stsp break;
4712 145b6838 2022-06-16 stsp case KEY_RIGHT:
4713 145b6838 2022-06-16 stsp case 'l':
4714 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4715 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4716 640cd7ff 2022-06-22 mark else
4717 640cd7ff 2022-06-22 mark view->count = 0;
4718 145b6838 2022-06-16 stsp break;
4719 145b6838 2022-06-16 stsp case KEY_LEFT:
4720 145b6838 2022-06-16 stsp case 'h':
4721 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4722 640cd7ff 2022-06-22 mark if (view->x <= 0)
4723 640cd7ff 2022-06-22 mark view->count = 0;
4724 145b6838 2022-06-16 stsp break;
4725 64453f7e 2020-11-21 stsp case 'a':
4726 3dbaef42 2020-11-24 stsp case 'w':
4727 3dbaef42 2020-11-24 stsp if (ch == 'a')
4728 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4729 3dbaef42 2020-11-24 stsp if (ch == 'w')
4730 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4731 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4732 912a3f79 2021-08-30 j break;
4733 912a3f79 2021-08-30 j case 'g':
4734 912a3f79 2021-08-30 j case KEY_HOME:
4735 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4736 640cd7ff 2022-06-22 mark view->count = 0;
4737 64453f7e 2020-11-21 stsp break;
4738 912a3f79 2021-08-30 j case 'G':
4739 912a3f79 2021-08-30 j case KEY_END:
4740 640cd7ff 2022-06-22 mark view->count = 0;
4741 912a3f79 2021-08-30 j if (s->eof)
4742 912a3f79 2021-08-30 j break;
4743 912a3f79 2021-08-30 j
4744 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4745 912a3f79 2021-08-30 j s->eof = 1;
4746 912a3f79 2021-08-30 j break;
4747 1e37a5c2 2019-05-12 jcs case 'k':
4748 1e37a5c2 2019-05-12 jcs case KEY_UP:
4749 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4750 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4751 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4752 640cd7ff 2022-06-22 mark else
4753 640cd7ff 2022-06-22 mark view->count = 0;
4754 1e37a5c2 2019-05-12 jcs break;
4755 83cc4199 2022-06-13 stsp case CTRL('u'):
4756 33c3719a 2022-06-15 stsp case 'u':
4757 83cc4199 2022-06-13 stsp nscroll /= 2;
4758 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4759 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4760 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4761 61417565 2022-06-20 mark case 'b':
4762 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4763 640cd7ff 2022-06-22 mark view->count = 0;
4764 26ed57b2 2018-05-19 stsp break;
4765 640cd7ff 2022-06-22 mark }
4766 1e37a5c2 2019-05-12 jcs i = 0;
4767 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4768 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4769 1e37a5c2 2019-05-12 jcs break;
4770 1e37a5c2 2019-05-12 jcs case 'j':
4771 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4772 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4773 1e37a5c2 2019-05-12 jcs if (!s->eof)
4774 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4775 640cd7ff 2022-06-22 mark else
4776 640cd7ff 2022-06-22 mark view->count = 0;
4777 1e37a5c2 2019-05-12 jcs break;
4778 83cc4199 2022-06-13 stsp case CTRL('d'):
4779 33c3719a 2022-06-15 stsp case 'd':
4780 83cc4199 2022-06-13 stsp nscroll /= 2;
4781 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4782 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4783 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4784 61417565 2022-06-20 mark case 'f':
4785 1e37a5c2 2019-05-12 jcs case ' ':
4786 640cd7ff 2022-06-22 mark if (s->eof) {
4787 640cd7ff 2022-06-22 mark view->count = 0;
4788 1e37a5c2 2019-05-12 jcs break;
4789 640cd7ff 2022-06-22 mark }
4790 1e37a5c2 2019-05-12 jcs i = 0;
4791 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4792 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4793 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4794 826082fe 2020-12-10 stsp if (linelen == -1) {
4795 826082fe 2020-12-10 stsp if (feof(s->f)) {
4796 826082fe 2020-12-10 stsp s->eof = 1;
4797 826082fe 2020-12-10 stsp } else
4798 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4799 34bc9ec9 2019-02-22 stsp break;
4800 826082fe 2020-12-10 stsp }
4801 1e37a5c2 2019-05-12 jcs }
4802 826082fe 2020-12-10 stsp free(line);
4803 1e37a5c2 2019-05-12 jcs break;
4804 1e37a5c2 2019-05-12 jcs case '[':
4805 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4806 1e37a5c2 2019-05-12 jcs s->diff_context--;
4807 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4808 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4809 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4810 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4811 27829c9e 2020-11-21 stsp s->nlines) {
4812 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4813 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4814 27829c9e 2020-11-21 stsp }
4815 640cd7ff 2022-06-22 mark } else
4816 640cd7ff 2022-06-22 mark view->count = 0;
4817 1e37a5c2 2019-05-12 jcs break;
4818 1e37a5c2 2019-05-12 jcs case ']':
4819 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4820 1e37a5c2 2019-05-12 jcs s->diff_context++;
4821 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4824 640cd7ff 2022-06-22 mark } else
4825 640cd7ff 2022-06-22 mark view->count = 0;
4826 1e37a5c2 2019-05-12 jcs break;
4827 1e37a5c2 2019-05-12 jcs case '<':
4828 1e37a5c2 2019-05-12 jcs case ',':
4829 2b3e6702 2022-07-20 mark case 'K':
4830 c0f61fa4 2022-07-11 mark up = 1;
4831 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4832 c0f61fa4 2022-07-11 mark case '>':
4833 c0f61fa4 2022-07-11 mark case '.':
4834 2b3e6702 2022-07-20 mark case 'J':
4835 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4836 640cd7ff 2022-06-22 mark view->count = 0;
4837 48ae06ee 2018-10-18 stsp break;
4838 640cd7ff 2022-06-22 mark }
4839 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4840 6524637e 2019-02-21 stsp
4841 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4842 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4843 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4844 15a087fe 2019-02-21 stsp
4845 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4846 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4847 c0f61fa4 2022-07-11 mark if (err)
4848 c0f61fa4 2022-07-11 mark break;
4849 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4850 15a087fe 2019-02-21 stsp
4851 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4852 c0f61fa4 2022-07-11 mark break;
4853 15a087fe 2019-02-21 stsp
4854 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4855 c0f61fa4 2022-07-11 mark if (err)
4856 c0f61fa4 2022-07-11 mark break;
4857 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4858 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4859 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4860 5e224a3e 2019-02-22 stsp
4861 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4862 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4863 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4864 c0f61fa4 2022-07-11 mark
4865 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4866 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4867 c0f61fa4 2022-07-11 mark if (err)
4868 c0f61fa4 2022-07-11 mark break;
4869 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4870 5a8b5076 2020-12-05 stsp
4871 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4872 c0f61fa4 2022-07-11 mark break;
4873 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4874 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4875 c0f61fa4 2022-07-11 mark bs->selected_line);
4876 c0f61fa4 2022-07-11 mark if (id == NULL)
4877 c0f61fa4 2022-07-11 mark break;
4878 15a087fe 2019-02-21 stsp
4879 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4880 c0f61fa4 2022-07-11 mark break;
4881 15a087fe 2019-02-21 stsp
4882 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4883 c0f61fa4 2022-07-11 mark if (err)
4884 c0f61fa4 2022-07-11 mark break;
4885 c0f61fa4 2022-07-11 mark }
4886 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4887 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4888 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4889 145b6838 2022-06-16 stsp view->x = 0;
4890 1e37a5c2 2019-05-12 jcs
4891 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4892 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4893 1e37a5c2 2019-05-12 jcs break;
4894 1e37a5c2 2019-05-12 jcs default:
4895 640cd7ff 2022-06-22 mark view->count = 0;
4896 1e37a5c2 2019-05-12 jcs break;
4897 26ed57b2 2018-05-19 stsp }
4898 e5a0f69f 2018-08-18 stsp
4899 bcbd79e2 2018-08-19 stsp return err;
4900 26ed57b2 2018-05-19 stsp }
4901 26ed57b2 2018-05-19 stsp
4902 4ed7e80c 2018-05-20 stsp static const struct got_error *
4903 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4904 9f7d7167 2018-04-29 stsp {
4905 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4906 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4907 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4908 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4909 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4910 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4911 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4912 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4913 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4914 3dbaef42 2020-11-24 stsp const char *errstr;
4915 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4916 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4917 70ac5f84 2019-03-28 stsp
4918 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4919 26ed57b2 2018-05-19 stsp switch (ch) {
4920 64453f7e 2020-11-21 stsp case 'a':
4921 64453f7e 2020-11-21 stsp force_text_diff = 1;
4922 3dbaef42 2020-11-24 stsp break;
4923 3dbaef42 2020-11-24 stsp case 'C':
4924 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4925 3dbaef42 2020-11-24 stsp &errstr);
4926 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4927 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4928 5a20d08d 2022-02-09 op errstr, errstr);
4929 64453f7e 2020-11-21 stsp break;
4930 09b5bff8 2020-02-23 naddy case 'r':
4931 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4932 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4933 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4934 09b5bff8 2020-02-23 naddy optarg);
4935 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4936 09b5bff8 2020-02-23 naddy break;
4937 3dbaef42 2020-11-24 stsp case 'w':
4938 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4939 3dbaef42 2020-11-24 stsp break;
4940 26ed57b2 2018-05-19 stsp default:
4941 17020d27 2019-03-07 stsp usage_diff();
4942 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4943 26ed57b2 2018-05-19 stsp }
4944 26ed57b2 2018-05-19 stsp }
4945 26ed57b2 2018-05-19 stsp
4946 26ed57b2 2018-05-19 stsp argc -= optind;
4947 26ed57b2 2018-05-19 stsp argv += optind;
4948 26ed57b2 2018-05-19 stsp
4949 26ed57b2 2018-05-19 stsp if (argc == 0) {
4950 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4951 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4952 15a94983 2018-12-23 stsp id_str1 = argv[0];
4953 15a94983 2018-12-23 stsp id_str2 = argv[1];
4954 26ed57b2 2018-05-19 stsp } else
4955 26ed57b2 2018-05-19 stsp usage_diff();
4956 eb6600df 2019-01-04 stsp
4957 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4958 0ae84acc 2022-06-15 tracey if (error)
4959 0ae84acc 2022-06-15 tracey goto done;
4960 0ae84acc 2022-06-15 tracey
4961 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4962 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4963 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4964 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4965 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4966 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4967 c156c7a4 2020-12-18 stsp goto done;
4968 a273ac94 2020-02-23 naddy if (worktree)
4969 a273ac94 2020-02-23 naddy repo_path =
4970 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4971 a273ac94 2020-02-23 naddy else
4972 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4973 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4974 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4975 c156c7a4 2020-12-18 stsp goto done;
4976 c156c7a4 2020-12-18 stsp }
4977 a273ac94 2020-02-23 naddy }
4978 a273ac94 2020-02-23 naddy
4979 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4980 eb6600df 2019-01-04 stsp if (error)
4981 eb6600df 2019-01-04 stsp goto done;
4982 26ed57b2 2018-05-19 stsp
4983 a273ac94 2020-02-23 naddy init_curses();
4984 a273ac94 2020-02-23 naddy
4985 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4986 51a10b52 2020-12-26 stsp if (error)
4987 51a10b52 2020-12-26 stsp goto done;
4988 51a10b52 2020-12-26 stsp
4989 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4990 26ed57b2 2018-05-19 stsp if (error)
4991 26ed57b2 2018-05-19 stsp goto done;
4992 26ed57b2 2018-05-19 stsp
4993 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4994 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4995 26ed57b2 2018-05-19 stsp if (error)
4996 26ed57b2 2018-05-19 stsp goto done;
4997 26ed57b2 2018-05-19 stsp
4998 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4999 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 26ed57b2 2018-05-19 stsp if (error)
5001 26ed57b2 2018-05-19 stsp goto done;
5002 26ed57b2 2018-05-19 stsp
5003 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5004 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5005 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5006 ea5e7bb5 2018-08-01 stsp goto done;
5007 ea5e7bb5 2018-08-01 stsp }
5008 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5009 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5010 5dc9f4bc 2018-08-04 stsp if (error)
5011 5dc9f4bc 2018-08-04 stsp goto done;
5012 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5013 26ed57b2 2018-05-19 stsp done:
5014 3dbaef42 2020-11-24 stsp free(label1);
5015 3dbaef42 2020-11-24 stsp free(label2);
5016 c02c541e 2019-03-29 stsp free(repo_path);
5017 a273ac94 2020-02-23 naddy free(cwd);
5018 1d0f4054 2021-06-17 stsp if (repo) {
5019 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5020 1d0f4054 2021-06-17 stsp if (error == NULL)
5021 1d0f4054 2021-06-17 stsp error = close_err;
5022 1d0f4054 2021-06-17 stsp }
5023 a273ac94 2020-02-23 naddy if (worktree)
5024 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5025 0ae84acc 2022-06-15 tracey if (pack_fds) {
5026 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5027 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5028 0ae84acc 2022-06-15 tracey if (error == NULL)
5029 0ae84acc 2022-06-15 tracey error = pack_err;
5030 0ae84acc 2022-06-15 tracey }
5031 51a10b52 2020-12-26 stsp tog_free_refs();
5032 26ed57b2 2018-05-19 stsp return error;
5033 9f7d7167 2018-04-29 stsp }
5034 9f7d7167 2018-04-29 stsp
5035 4ed7e80c 2018-05-20 stsp __dead static void
5036 9f7d7167 2018-04-29 stsp usage_blame(void)
5037 9f7d7167 2018-04-29 stsp {
5038 80ddbec8 2018-04-29 stsp endwin();
5039 87411fa9 2022-06-16 stsp fprintf(stderr,
5040 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5041 9f7d7167 2018-04-29 stsp getprogname());
5042 9f7d7167 2018-04-29 stsp exit(1);
5043 9f7d7167 2018-04-29 stsp }
5044 84451b3e 2018-07-10 stsp
5045 84451b3e 2018-07-10 stsp struct tog_blame_line {
5046 84451b3e 2018-07-10 stsp int annotated;
5047 84451b3e 2018-07-10 stsp struct got_object_id *id;
5048 84451b3e 2018-07-10 stsp };
5049 9f7d7167 2018-04-29 stsp
5050 4ed7e80c 2018-05-20 stsp static const struct got_error *
5051 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5052 84451b3e 2018-07-10 stsp {
5053 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5054 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5055 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5056 84451b3e 2018-07-10 stsp const struct got_error *err;
5057 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5058 826082fe 2020-12-10 stsp char *line = NULL;
5059 826082fe 2020-12-10 stsp size_t linesize = 0;
5060 826082fe 2020-12-10 stsp ssize_t linelen;
5061 84451b3e 2018-07-10 stsp wchar_t *wline;
5062 27a741e5 2019-09-11 stsp int width;
5063 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5064 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5065 ab089a2a 2018-07-12 stsp char *id_str;
5066 11b20872 2019-11-08 stsp struct tog_color *tc;
5067 ab089a2a 2018-07-12 stsp
5068 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5069 ab089a2a 2018-07-12 stsp if (err)
5070 ab089a2a 2018-07-12 stsp return err;
5071 84451b3e 2018-07-10 stsp
5072 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5073 f7d12f7e 2018-08-01 stsp werase(view->window);
5074 84451b3e 2018-07-10 stsp
5075 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5076 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5077 ab089a2a 2018-07-12 stsp free(id_str);
5078 ab089a2a 2018-07-12 stsp return err;
5079 ab089a2a 2018-07-12 stsp }
5080 ab089a2a 2018-07-12 stsp
5081 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5082 ab089a2a 2018-07-12 stsp free(line);
5083 2550e4c3 2018-07-13 stsp line = NULL;
5084 1cae65b4 2019-09-22 stsp if (err)
5085 1cae65b4 2019-09-22 stsp return err;
5086 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5087 a3404814 2018-09-02 stsp wstandout(view->window);
5088 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5089 11b20872 2019-11-08 stsp if (tc)
5090 11b20872 2019-11-08 stsp wattr_on(view->window,
5091 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5092 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5093 11b20872 2019-11-08 stsp if (tc)
5094 11b20872 2019-11-08 stsp wattr_off(view->window,
5095 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5096 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5097 a3404814 2018-09-02 stsp wstandend(view->window);
5098 2550e4c3 2018-07-13 stsp free(wline);
5099 2550e4c3 2018-07-13 stsp wline = NULL;
5100 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5101 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5102 ab089a2a 2018-07-12 stsp
5103 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5104 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5105 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5106 ab089a2a 2018-07-12 stsp free(id_str);
5107 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5108 ab089a2a 2018-07-12 stsp }
5109 ab089a2a 2018-07-12 stsp free(id_str);
5110 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5111 3f60a8ef 2018-07-10 stsp free(line);
5112 2550e4c3 2018-07-13 stsp line = NULL;
5113 3f60a8ef 2018-07-10 stsp if (err)
5114 3f60a8ef 2018-07-10 stsp return err;
5115 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5116 2550e4c3 2018-07-13 stsp free(wline);
5117 2550e4c3 2018-07-13 stsp wline = NULL;
5118 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5119 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5120 3f60a8ef 2018-07-10 stsp
5121 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5122 145b6838 2022-06-16 stsp view->maxx = 0;
5123 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5124 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5125 826082fe 2020-12-10 stsp if (linelen == -1) {
5126 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5127 826082fe 2020-12-10 stsp s->eof = 1;
5128 826082fe 2020-12-10 stsp break;
5129 826082fe 2020-12-10 stsp }
5130 84451b3e 2018-07-10 stsp free(line);
5131 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5132 84451b3e 2018-07-10 stsp }
5133 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5134 826082fe 2020-12-10 stsp continue;
5135 1853e0f4 2022-06-16 stsp
5136 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5137 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5138 1853e0f4 2022-06-16 stsp if (err) {
5139 1853e0f4 2022-06-16 stsp free(line);
5140 1853e0f4 2022-06-16 stsp return err;
5141 1853e0f4 2022-06-16 stsp }
5142 1853e0f4 2022-06-16 stsp free(wline);
5143 1853e0f4 2022-06-16 stsp wline = NULL;
5144 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5145 84451b3e 2018-07-10 stsp
5146 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5147 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5148 b700b5d6 2018-07-10 stsp
5149 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5150 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5151 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5152 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5153 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5154 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5155 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5156 8d0fe45a 2019-08-12 stsp char *id_str;
5157 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5158 87411fa9 2022-06-16 stsp blame_line->id);
5159 8d0fe45a 2019-08-12 stsp if (err) {
5160 8d0fe45a 2019-08-12 stsp free(line);
5161 8d0fe45a 2019-08-12 stsp return err;
5162 8d0fe45a 2019-08-12 stsp }
5163 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5164 11b20872 2019-11-08 stsp if (tc)
5165 11b20872 2019-11-08 stsp wattr_on(view->window,
5166 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5167 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5168 11b20872 2019-11-08 stsp if (tc)
5169 11b20872 2019-11-08 stsp wattr_off(view->window,
5170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5171 8d0fe45a 2019-08-12 stsp free(id_str);
5172 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5173 8d0fe45a 2019-08-12 stsp } else {
5174 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5175 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5176 84451b3e 2018-07-10 stsp }
5177 ee41ec32 2018-07-10 stsp } else {
5178 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5179 ee41ec32 2018-07-10 stsp prev_id = NULL;
5180 ee41ec32 2018-07-10 stsp }
5181 84451b3e 2018-07-10 stsp
5182 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5183 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5184 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5185 27a741e5 2019-09-11 stsp
5186 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5187 41605754 2020-11-12 stsp width = 9;
5188 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5189 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5190 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5191 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5192 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5193 41605754 2020-11-12 stsp if (err) {
5194 41605754 2020-11-12 stsp free(line);
5195 41605754 2020-11-12 stsp return err;
5196 41605754 2020-11-12 stsp }
5197 41605754 2020-11-12 stsp width += 9;
5198 41605754 2020-11-12 stsp } else {
5199 4cb9d869 2022-06-16 stsp int skip;
5200 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5201 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5202 4cb9d869 2022-06-16 stsp if (err) {
5203 4cb9d869 2022-06-16 stsp free(line);
5204 4cb9d869 2022-06-16 stsp return err;
5205 145b6838 2022-06-16 stsp }
5206 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5207 a75b3e2d 2022-06-16 stsp width += 9;
5208 41605754 2020-11-12 stsp free(wline);
5209 41605754 2020-11-12 stsp wline = NULL;
5210 41605754 2020-11-12 stsp }
5211 41605754 2020-11-12 stsp
5212 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5213 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5214 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5215 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5216 84451b3e 2018-07-10 stsp }
5217 826082fe 2020-12-10 stsp free(line);
5218 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5219 84451b3e 2018-07-10 stsp
5220 9b058f45 2022-06-30 mark view_border(view);
5221 84451b3e 2018-07-10 stsp
5222 84451b3e 2018-07-10 stsp return NULL;
5223 84451b3e 2018-07-10 stsp }
5224 84451b3e 2018-07-10 stsp
5225 84451b3e 2018-07-10 stsp static const struct got_error *
5226 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5227 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5228 84451b3e 2018-07-10 stsp {
5229 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5230 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5231 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5232 1a76625f 2018-10-22 stsp int errcode;
5233 84451b3e 2018-07-10 stsp
5234 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5235 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5236 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5237 84451b3e 2018-07-10 stsp
5238 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5239 1a76625f 2018-10-22 stsp if (errcode)
5240 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5241 84451b3e 2018-07-10 stsp
5242 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5243 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5244 d68a0a7d 2018-07-10 stsp goto done;
5245 d68a0a7d 2018-07-10 stsp }
5246 d68a0a7d 2018-07-10 stsp
5247 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5248 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5249 d68a0a7d 2018-07-10 stsp
5250 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5251 d68a0a7d 2018-07-10 stsp if (line->annotated)
5252 d68a0a7d 2018-07-10 stsp goto done;
5253 d68a0a7d 2018-07-10 stsp
5254 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5255 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5256 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5257 84451b3e 2018-07-10 stsp goto done;
5258 84451b3e 2018-07-10 stsp }
5259 84451b3e 2018-07-10 stsp line->annotated = 1;
5260 84451b3e 2018-07-10 stsp done:
5261 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5262 1a76625f 2018-10-22 stsp if (errcode)
5263 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5264 84451b3e 2018-07-10 stsp return err;
5265 84451b3e 2018-07-10 stsp }
5266 84451b3e 2018-07-10 stsp
5267 84451b3e 2018-07-10 stsp static void *
5268 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5269 84451b3e 2018-07-10 stsp {
5270 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5271 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5272 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5273 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5274 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5275 1b484788 2022-06-28 tracey
5276 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5277 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5278 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5279 e6e73e55 2022-06-30 tracey
5280 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5281 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5282 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5283 e6e73e55 2022-06-30 tracey goto done;
5284 e6e73e55 2022-06-30 tracey }
5285 18430de3 2018-07-10 stsp
5286 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5287 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5288 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5289 e6e73e55 2022-06-30 tracey goto done;
5290 e6e73e55 2022-06-30 tracey }
5291 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5292 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5293 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5294 e6e73e55 2022-06-30 tracey goto done;
5295 e6e73e55 2022-06-30 tracey }
5296 e6e73e55 2022-06-30 tracey
5297 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5298 61266923 2020-01-14 stsp if (err)
5299 e6e73e55 2022-06-30 tracey goto done;
5300 61266923 2020-01-14 stsp
5301 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5302 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5303 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5304 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5305 fc06ba56 2019-08-22 stsp err = NULL;
5306 18430de3 2018-07-10 stsp
5307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5308 e6e73e55 2022-06-30 tracey if (errcode) {
5309 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5310 e6e73e55 2022-06-30 tracey goto done;
5311 e6e73e55 2022-06-30 tracey }
5312 18430de3 2018-07-10 stsp
5313 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5314 1d0f4054 2021-06-17 stsp if (err == NULL)
5315 1d0f4054 2021-06-17 stsp err = close_err;
5316 c9beca56 2018-07-22 stsp ta->repo = NULL;
5317 c9beca56 2018-07-22 stsp *ta->complete = 1;
5318 18430de3 2018-07-10 stsp
5319 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5320 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5321 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5322 18430de3 2018-07-10 stsp
5323 e6e73e55 2022-06-30 tracey done:
5324 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5325 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5326 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5327 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5328 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5329 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5330 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5331 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5332 1b484788 2022-06-28 tracey
5333 18430de3 2018-07-10 stsp return (void *)err;
5334 84451b3e 2018-07-10 stsp }
5335 84451b3e 2018-07-10 stsp
5336 245d91c1 2018-07-12 stsp static struct got_object_id *
5337 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5338 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5339 245d91c1 2018-07-12 stsp {
5340 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5341 8d0fe45a 2019-08-12 stsp
5342 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5343 8d0fe45a 2019-08-12 stsp return NULL;
5344 b880a918 2018-07-10 stsp
5345 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5346 c0f61fa4 2022-07-11 mark if (!line->annotated)
5347 c0f61fa4 2022-07-11 mark return NULL;
5348 c0f61fa4 2022-07-11 mark
5349 c0f61fa4 2022-07-11 mark return line->id;
5350 c0f61fa4 2022-07-11 mark }
5351 c0f61fa4 2022-07-11 mark
5352 c0f61fa4 2022-07-11 mark static struct got_object_id *
5353 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5354 c0f61fa4 2022-07-11 mark int lineno)
5355 c0f61fa4 2022-07-11 mark {
5356 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5357 c0f61fa4 2022-07-11 mark
5358 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5359 c0f61fa4 2022-07-11 mark return NULL;
5360 c0f61fa4 2022-07-11 mark
5361 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5362 245d91c1 2018-07-12 stsp if (!line->annotated)
5363 245d91c1 2018-07-12 stsp return NULL;
5364 245d91c1 2018-07-12 stsp
5365 245d91c1 2018-07-12 stsp return line->id;
5366 b880a918 2018-07-10 stsp }
5367 245d91c1 2018-07-12 stsp
5368 b880a918 2018-07-10 stsp static const struct got_error *
5369 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5370 a70480e0 2018-06-23 stsp {
5371 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5372 245d91c1 2018-07-12 stsp int i;
5373 245d91c1 2018-07-12 stsp
5374 245d91c1 2018-07-12 stsp if (blame->thread) {
5375 1a76625f 2018-10-22 stsp int errcode;
5376 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5377 1a76625f 2018-10-22 stsp if (errcode)
5378 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5379 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5380 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5381 1a76625f 2018-10-22 stsp if (errcode)
5382 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5383 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5384 1a76625f 2018-10-22 stsp if (errcode)
5385 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5386 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5387 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5388 245d91c1 2018-07-12 stsp err = NULL;
5389 245d91c1 2018-07-12 stsp blame->thread = NULL;
5390 245d91c1 2018-07-12 stsp }
5391 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5392 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5393 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5394 1d0f4054 2021-06-17 stsp if (err == NULL)
5395 1d0f4054 2021-06-17 stsp err = close_err;
5396 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5397 245d91c1 2018-07-12 stsp }
5398 245d91c1 2018-07-12 stsp if (blame->f) {
5399 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5400 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5401 245d91c1 2018-07-12 stsp blame->f = NULL;
5402 245d91c1 2018-07-12 stsp }
5403 57670559 2018-12-23 stsp if (blame->lines) {
5404 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5405 57670559 2018-12-23 stsp free(blame->lines[i].id);
5406 57670559 2018-12-23 stsp free(blame->lines);
5407 57670559 2018-12-23 stsp blame->lines = NULL;
5408 57670559 2018-12-23 stsp }
5409 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5410 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5411 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5412 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5413 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5414 0ae84acc 2022-06-15 tracey if (err == NULL)
5415 0ae84acc 2022-06-15 tracey err = pack_err;
5416 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5417 0ae84acc 2022-06-15 tracey }
5418 245d91c1 2018-07-12 stsp return err;
5419 245d91c1 2018-07-12 stsp }
5420 245d91c1 2018-07-12 stsp
5421 245d91c1 2018-07-12 stsp static const struct got_error *
5422 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5423 fc06ba56 2019-08-22 stsp {
5424 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5425 fc06ba56 2019-08-22 stsp int *done = arg;
5426 fc06ba56 2019-08-22 stsp int errcode;
5427 fc06ba56 2019-08-22 stsp
5428 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5429 fc06ba56 2019-08-22 stsp if (errcode)
5430 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5431 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5432 fc06ba56 2019-08-22 stsp
5433 fc06ba56 2019-08-22 stsp if (*done)
5434 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5435 fc06ba56 2019-08-22 stsp
5436 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5437 fc06ba56 2019-08-22 stsp if (errcode)
5438 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5439 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5440 fc06ba56 2019-08-22 stsp
5441 fc06ba56 2019-08-22 stsp return err;
5442 fc06ba56 2019-08-22 stsp }
5443 fc06ba56 2019-08-22 stsp
5444 fc06ba56 2019-08-22 stsp static const struct got_error *
5445 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5446 245d91c1 2018-07-12 stsp {
5447 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5448 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5449 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5450 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5451 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5452 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5453 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5454 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5455 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5456 a70480e0 2018-06-23 stsp
5457 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5458 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5459 27d434c2 2018-09-15 stsp if (err)
5460 15a94983 2018-12-23 stsp return err;
5461 eb81bc23 2022-06-28 tracey
5462 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5463 eb81bc23 2022-06-28 tracey if (fd == -1) {
5464 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5465 eb81bc23 2022-06-28 tracey goto done;
5466 eb81bc23 2022-06-28 tracey }
5467 a44927cc 2022-04-07 stsp
5468 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5469 a44927cc 2022-04-07 stsp if (err)
5470 a44927cc 2022-04-07 stsp goto done;
5471 27d434c2 2018-09-15 stsp
5472 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5473 84451b3e 2018-07-10 stsp if (err)
5474 84451b3e 2018-07-10 stsp goto done;
5475 27d434c2 2018-09-15 stsp
5476 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5477 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5478 84451b3e 2018-07-10 stsp goto done;
5479 84451b3e 2018-07-10 stsp }
5480 a70480e0 2018-06-23 stsp
5481 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5482 a70480e0 2018-06-23 stsp if (err)
5483 a70480e0 2018-06-23 stsp goto done;
5484 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5485 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5486 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5487 84451b3e 2018-07-10 stsp goto done;
5488 84451b3e 2018-07-10 stsp }
5489 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5490 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5491 1fddf795 2021-01-20 stsp if (err)
5492 1fddf795 2021-01-20 stsp goto done;
5493 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5494 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5495 84451b3e 2018-07-10 stsp goto done;
5496 1fddf795 2021-01-20 stsp }
5497 b02560ec 2019-08-19 stsp
5498 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5499 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5500 b02560ec 2019-08-19 stsp blame->nlines--;
5501 a70480e0 2018-06-23 stsp
5502 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5503 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5504 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5505 84451b3e 2018-07-10 stsp goto done;
5506 84451b3e 2018-07-10 stsp }
5507 a70480e0 2018-06-23 stsp
5508 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5509 bd24772e 2018-07-11 stsp if (err)
5510 bd24772e 2018-07-11 stsp goto done;
5511 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5512 0ae84acc 2022-06-15 tracey pack_fds);
5513 0ae84acc 2022-06-15 tracey if (err)
5514 0ae84acc 2022-06-15 tracey goto done;
5515 bd24772e 2018-07-11 stsp
5516 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5517 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5518 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5519 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5520 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5521 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5522 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5523 245d91c1 2018-07-12 stsp goto done;
5524 245d91c1 2018-07-12 stsp }
5525 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5526 245d91c1 2018-07-12 stsp
5527 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5528 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5529 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5530 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5531 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5532 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5533 a5388363 2020-12-01 naddy s->blame_complete = 0;
5534 f5a09613 2020-12-13 naddy
5535 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5536 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5537 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5538 f5a09613 2020-12-13 naddy s->selected_line = 1;
5539 f5a09613 2020-12-13 naddy }
5540 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5541 245d91c1 2018-07-12 stsp
5542 245d91c1 2018-07-12 stsp done:
5543 a44927cc 2022-04-07 stsp if (commit)
5544 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5545 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5546 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5547 245d91c1 2018-07-12 stsp if (blob)
5548 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5549 27d434c2 2018-09-15 stsp free(obj_id);
5550 245d91c1 2018-07-12 stsp if (err)
5551 245d91c1 2018-07-12 stsp stop_blame(blame);
5552 245d91c1 2018-07-12 stsp return err;
5553 245d91c1 2018-07-12 stsp }
5554 245d91c1 2018-07-12 stsp
5555 245d91c1 2018-07-12 stsp static const struct got_error *
5556 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5557 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5558 245d91c1 2018-07-12 stsp {
5559 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5560 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5561 dbc6a6b6 2018-07-12 stsp
5562 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5563 245d91c1 2018-07-12 stsp
5564 c4843652 2019-08-12 stsp s->path = strdup(path);
5565 c4843652 2019-08-12 stsp if (s->path == NULL)
5566 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5567 c4843652 2019-08-12 stsp
5568 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5569 c4843652 2019-08-12 stsp if (err) {
5570 c4843652 2019-08-12 stsp free(s->path);
5571 7cbe629d 2018-08-04 stsp return err;
5572 c4843652 2019-08-12 stsp }
5573 245d91c1 2018-07-12 stsp
5574 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5575 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5576 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5577 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5578 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5579 fb2756b9 2018-08-04 stsp s->repo = repo;
5580 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5581 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5582 7cbe629d 2018-08-04 stsp
5583 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5584 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5585 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5586 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5587 11b20872 2019-11-08 stsp if (err)
5588 11b20872 2019-11-08 stsp return err;
5589 11b20872 2019-11-08 stsp }
5590 11b20872 2019-11-08 stsp
5591 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5592 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5593 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5594 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5595 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5596 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5597 e5a0f69f 2018-08-18 stsp
5598 a5388363 2020-12-01 naddy return run_blame(view);
5599 7cbe629d 2018-08-04 stsp }
5600 7cbe629d 2018-08-04 stsp
5601 e5a0f69f 2018-08-18 stsp static const struct got_error *
5602 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5603 7cbe629d 2018-08-04 stsp {
5604 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5605 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5606 7cbe629d 2018-08-04 stsp
5607 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5608 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5609 e5a0f69f 2018-08-18 stsp
5610 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5611 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5612 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5613 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5614 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5615 7cbe629d 2018-08-04 stsp }
5616 e5a0f69f 2018-08-18 stsp
5617 e5a0f69f 2018-08-18 stsp free(s->path);
5618 11b20872 2019-11-08 stsp free_colors(&s->colors);
5619 e5a0f69f 2018-08-18 stsp return err;
5620 7cbe629d 2018-08-04 stsp }
5621 7cbe629d 2018-08-04 stsp
5622 7cbe629d 2018-08-04 stsp static const struct got_error *
5623 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5624 6c4c42e0 2019-06-24 stsp {
5625 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5626 6c4c42e0 2019-06-24 stsp
5627 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5628 6c4c42e0 2019-06-24 stsp return NULL;
5629 6c4c42e0 2019-06-24 stsp }
5630 6c4c42e0 2019-06-24 stsp
5631 6c4c42e0 2019-06-24 stsp static const struct got_error *
5632 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5633 6c4c42e0 2019-06-24 stsp {
5634 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5635 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5636 6c4c42e0 2019-06-24 stsp int lineno;
5637 cb713507 2022-06-16 stsp char *line = NULL;
5638 826082fe 2020-12-10 stsp size_t linesize = 0;
5639 826082fe 2020-12-10 stsp ssize_t linelen;
5640 6c4c42e0 2019-06-24 stsp
5641 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5642 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 6c4c42e0 2019-06-24 stsp return NULL;
5644 6c4c42e0 2019-06-24 stsp }
5645 6c4c42e0 2019-06-24 stsp
5646 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5647 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5648 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5649 6c4c42e0 2019-06-24 stsp else
5650 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5651 487cd7d2 2021-12-17 stsp } else
5652 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5653 6c4c42e0 2019-06-24 stsp
5654 6c4c42e0 2019-06-24 stsp while (1) {
5655 6c4c42e0 2019-06-24 stsp off_t offset;
5656 6c4c42e0 2019-06-24 stsp
5657 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5658 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5659 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5660 6c4c42e0 2019-06-24 stsp break;
5661 6c4c42e0 2019-06-24 stsp }
5662 2246482e 2019-06-25 stsp
5663 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5664 6c4c42e0 2019-06-24 stsp lineno = 1;
5665 6c4c42e0 2019-06-24 stsp else
5666 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5667 6c4c42e0 2019-06-24 stsp }
5668 6c4c42e0 2019-06-24 stsp
5669 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5670 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5671 6c4c42e0 2019-06-24 stsp free(line);
5672 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5673 6c4c42e0 2019-06-24 stsp }
5674 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5675 cb713507 2022-06-16 stsp if (linelen != -1) {
5676 cb713507 2022-06-16 stsp char *exstr;
5677 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5678 cb713507 2022-06-16 stsp if (err)
5679 cb713507 2022-06-16 stsp break;
5680 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5681 cb713507 2022-06-16 stsp &view->regmatch)) {
5682 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5683 cb713507 2022-06-16 stsp s->matched_line = lineno;
5684 cb713507 2022-06-16 stsp free(exstr);
5685 cb713507 2022-06-16 stsp break;
5686 cb713507 2022-06-16 stsp }
5687 cb713507 2022-06-16 stsp free(exstr);
5688 6c4c42e0 2019-06-24 stsp }
5689 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5690 6c4c42e0 2019-06-24 stsp lineno++;
5691 6c4c42e0 2019-06-24 stsp else
5692 6c4c42e0 2019-06-24 stsp lineno--;
5693 6c4c42e0 2019-06-24 stsp }
5694 826082fe 2020-12-10 stsp free(line);
5695 6c4c42e0 2019-06-24 stsp
5696 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5697 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5698 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5699 6c4c42e0 2019-06-24 stsp }
5700 6c4c42e0 2019-06-24 stsp
5701 145b6838 2022-06-16 stsp return err;
5702 6c4c42e0 2019-06-24 stsp }
5703 6c4c42e0 2019-06-24 stsp
5704 6c4c42e0 2019-06-24 stsp static const struct got_error *
5705 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5706 7cbe629d 2018-08-04 stsp {
5707 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5708 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5709 2b380cc8 2018-10-24 stsp int errcode;
5710 2b380cc8 2018-10-24 stsp
5711 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5712 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5713 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5714 2b380cc8 2018-10-24 stsp if (errcode)
5715 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5716 51fe7530 2019-08-19 stsp
5717 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5718 2b380cc8 2018-10-24 stsp }
5719 e5a0f69f 2018-08-18 stsp
5720 51fe7530 2019-08-19 stsp if (s->blame_complete)
5721 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5722 51fe7530 2019-08-19 stsp
5723 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5724 e5a0f69f 2018-08-18 stsp
5725 9b058f45 2022-06-30 mark view_border(view);
5726 e5a0f69f 2018-08-18 stsp return err;
5727 e5a0f69f 2018-08-18 stsp }
5728 e5a0f69f 2018-08-18 stsp
5729 e5a0f69f 2018-08-18 stsp static const struct got_error *
5730 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5731 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5732 05f04cdf 2022-07-20 mark {
5733 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5734 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5735 05f04cdf 2022-07-20 mark
5736 05f04cdf 2022-07-20 mark *new_view = NULL;
5737 05f04cdf 2022-07-20 mark
5738 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5739 05f04cdf 2022-07-20 mark if (log_view == NULL)
5740 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5741 05f04cdf 2022-07-20 mark
5742 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5743 05f04cdf 2022-07-20 mark if (err)
5744 05f04cdf 2022-07-20 mark view_close(log_view);
5745 05f04cdf 2022-07-20 mark else
5746 05f04cdf 2022-07-20 mark *new_view = log_view;
5747 05f04cdf 2022-07-20 mark
5748 05f04cdf 2022-07-20 mark return err;
5749 05f04cdf 2022-07-20 mark }
5750 05f04cdf 2022-07-20 mark
5751 05f04cdf 2022-07-20 mark static const struct got_error *
5752 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5753 e5a0f69f 2018-08-18 stsp {
5754 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5755 05f04cdf 2022-07-20 mark struct tog_view *diff_view, *log_view;
5756 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5757 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5758 9b058f45 2022-06-30 mark
5759 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5760 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5761 9b058f45 2022-06-30 mark --eos; /* border */
5762 7cbe629d 2018-08-04 stsp
5763 e5a0f69f 2018-08-18 stsp switch (ch) {
5764 145b6838 2022-06-16 stsp case '0':
5765 145b6838 2022-06-16 stsp view->x = 0;
5766 145b6838 2022-06-16 stsp break;
5767 145b6838 2022-06-16 stsp case '$':
5768 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5769 640cd7ff 2022-06-22 mark view->count = 0;
5770 145b6838 2022-06-16 stsp break;
5771 145b6838 2022-06-16 stsp case KEY_RIGHT:
5772 145b6838 2022-06-16 stsp case 'l':
5773 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5774 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5775 640cd7ff 2022-06-22 mark else
5776 640cd7ff 2022-06-22 mark view->count = 0;
5777 145b6838 2022-06-16 stsp break;
5778 145b6838 2022-06-16 stsp case KEY_LEFT:
5779 145b6838 2022-06-16 stsp case 'h':
5780 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5781 640cd7ff 2022-06-22 mark if (view->x <= 0)
5782 640cd7ff 2022-06-22 mark view->count = 0;
5783 145b6838 2022-06-16 stsp break;
5784 1e37a5c2 2019-05-12 jcs case 'q':
5785 1e37a5c2 2019-05-12 jcs s->done = 1;
5786 4deef56f 2021-09-02 naddy break;
5787 4deef56f 2021-09-02 naddy case 'g':
5788 4deef56f 2021-09-02 naddy case KEY_HOME:
5789 4deef56f 2021-09-02 naddy s->selected_line = 1;
5790 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5791 640cd7ff 2022-06-22 mark view->count = 0;
5792 4deef56f 2021-09-02 naddy break;
5793 4deef56f 2021-09-02 naddy case 'G':
5794 4deef56f 2021-09-02 naddy case KEY_END:
5795 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5796 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5797 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5798 4deef56f 2021-09-02 naddy } else {
5799 9b058f45 2022-06-30 mark s->selected_line = eos;
5800 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5801 4deef56f 2021-09-02 naddy }
5802 640cd7ff 2022-06-22 mark view->count = 0;
5803 1e37a5c2 2019-05-12 jcs break;
5804 1e37a5c2 2019-05-12 jcs case 'k':
5805 1e37a5c2 2019-05-12 jcs case KEY_UP:
5806 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5807 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5808 1e37a5c2 2019-05-12 jcs s->selected_line--;
5809 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5810 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5811 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5812 640cd7ff 2022-06-22 mark else
5813 640cd7ff 2022-06-22 mark view->count = 0;
5814 1e37a5c2 2019-05-12 jcs break;
5815 83cc4199 2022-06-13 stsp case CTRL('u'):
5816 33c3719a 2022-06-15 stsp case 'u':
5817 83cc4199 2022-06-13 stsp nscroll /= 2;
5818 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5819 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5820 ea025d1d 2020-02-22 naddy case CTRL('b'):
5821 61417565 2022-06-20 mark case 'b':
5822 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5823 640cd7ff 2022-06-22 mark if (view->count > 1)
5824 640cd7ff 2022-06-22 mark nscroll += nscroll;
5825 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5826 640cd7ff 2022-06-22 mark view->count = 0;
5827 e5a0f69f 2018-08-18 stsp break;
5828 1e37a5c2 2019-05-12 jcs }
5829 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5830 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5831 1e37a5c2 2019-05-12 jcs else
5832 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5833 1e37a5c2 2019-05-12 jcs break;
5834 1e37a5c2 2019-05-12 jcs case 'j':
5835 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5836 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5837 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5838 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5839 1e37a5c2 2019-05-12 jcs s->selected_line++;
5840 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5841 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5842 640cd7ff 2022-06-22 mark else
5843 640cd7ff 2022-06-22 mark view->count = 0;
5844 1e37a5c2 2019-05-12 jcs break;
5845 61417565 2022-06-20 mark case 'c':
5846 1e37a5c2 2019-05-12 jcs case 'p': {
5847 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5848 640cd7ff 2022-06-22 mark
5849 640cd7ff 2022-06-22 mark view->count = 0;
5850 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5851 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5852 1e37a5c2 2019-05-12 jcs if (id == NULL)
5853 e5a0f69f 2018-08-18 stsp break;
5854 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5855 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5856 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5857 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5858 1e37a5c2 2019-05-12 jcs int obj_type;
5859 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5860 1e37a5c2 2019-05-12 jcs s->repo, id);
5861 e5a0f69f 2018-08-18 stsp if (err)
5862 e5a0f69f 2018-08-18 stsp break;
5863 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5864 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5865 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5866 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5867 e5a0f69f 2018-08-18 stsp break;
5868 e5a0f69f 2018-08-18 stsp }
5869 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5870 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5871 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5872 a44927cc 2022-04-07 stsp if (err)
5873 a44927cc 2022-04-07 stsp break;
5874 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5875 a44927cc 2022-04-07 stsp pcommit, s->path);
5876 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5877 e5a0f69f 2018-08-18 stsp if (err) {
5878 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5879 1e37a5c2 2019-05-12 jcs err = NULL;
5880 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5881 e5a0f69f 2018-08-18 stsp break;
5882 e5a0f69f 2018-08-18 stsp }
5883 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5884 1e37a5c2 2019-05-12 jcs blob_id);
5885 1e37a5c2 2019-05-12 jcs free(blob_id);
5886 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5887 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5888 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5889 e5a0f69f 2018-08-18 stsp break;
5890 1e37a5c2 2019-05-12 jcs }
5891 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5892 d7b5a0e8 2022-04-20 stsp &pid->id);
5893 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5894 1e37a5c2 2019-05-12 jcs } else {
5895 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5896 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5897 1e37a5c2 2019-05-12 jcs break;
5898 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5899 1e37a5c2 2019-05-12 jcs id);
5900 1e37a5c2 2019-05-12 jcs }
5901 1e37a5c2 2019-05-12 jcs if (err)
5902 e5a0f69f 2018-08-18 stsp break;
5903 1e37a5c2 2019-05-12 jcs s->done = 1;
5904 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5905 1e37a5c2 2019-05-12 jcs s->done = 0;
5906 1e37a5c2 2019-05-12 jcs if (thread_err)
5907 1e37a5c2 2019-05-12 jcs break;
5908 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5909 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5910 a5388363 2020-12-01 naddy err = run_blame(view);
5911 1e37a5c2 2019-05-12 jcs if (err)
5912 1e37a5c2 2019-05-12 jcs break;
5913 1e37a5c2 2019-05-12 jcs break;
5914 1e37a5c2 2019-05-12 jcs }
5915 61417565 2022-06-20 mark case 'C': {
5916 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5917 640cd7ff 2022-06-22 mark
5918 640cd7ff 2022-06-22 mark view->count = 0;
5919 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5920 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5921 1e37a5c2 2019-05-12 jcs break;
5922 1e37a5c2 2019-05-12 jcs s->done = 1;
5923 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5924 1e37a5c2 2019-05-12 jcs s->done = 0;
5925 1e37a5c2 2019-05-12 jcs if (thread_err)
5926 1e37a5c2 2019-05-12 jcs break;
5927 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5928 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5929 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5930 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5931 a5388363 2020-12-01 naddy err = run_blame(view);
5932 1e37a5c2 2019-05-12 jcs if (err)
5933 1e37a5c2 2019-05-12 jcs break;
5934 1e37a5c2 2019-05-12 jcs break;
5935 1e37a5c2 2019-05-12 jcs }
5936 05f04cdf 2022-07-20 mark case 'L': {
5937 05f04cdf 2022-07-20 mark struct got_object_id *id = NULL;
5938 05f04cdf 2022-07-20 mark
5939 05f04cdf 2022-07-20 mark view->count = 0;
5940 05f04cdf 2022-07-20 mark id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5941 05f04cdf 2022-07-20 mark s->first_displayed_line, s->selected_line);
5942 05f04cdf 2022-07-20 mark if (id == NULL)
5943 05f04cdf 2022-07-20 mark break;
5944 05f04cdf 2022-07-20 mark
5945 05f04cdf 2022-07-20 mark if (view_is_parent_view(view))
5946 05f04cdf 2022-07-20 mark view_get_split(view, &begin_y, &begin_x);
5947 05f04cdf 2022-07-20 mark err = log_annotated_line(&log_view, begin_y, begin_x,
5948 05f04cdf 2022-07-20 mark s->repo, id);
5949 05f04cdf 2022-07-20 mark if (err)
5950 05f04cdf 2022-07-20 mark break;
5951 05f04cdf 2022-07-20 mark if (view_is_parent_view(view) &&
5952 05f04cdf 2022-07-20 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5953 05f04cdf 2022-07-20 mark err = view_init_hsplit(view, begin_y);
5954 05f04cdf 2022-07-20 mark if (err)
5955 05f04cdf 2022-07-20 mark break;
5956 05f04cdf 2022-07-20 mark }
5957 05f04cdf 2022-07-20 mark
5958 05f04cdf 2022-07-20 mark view->focussed = 0;
5959 05f04cdf 2022-07-20 mark log_view->focussed = 1;
5960 05f04cdf 2022-07-20 mark log_view->mode = view->mode;
5961 05f04cdf 2022-07-20 mark log_view->nlines = view->lines - begin_y;
5962 05f04cdf 2022-07-20 mark if (view_is_parent_view(view)) {
5963 05f04cdf 2022-07-20 mark view_transfer_size(log_view, view);
5964 05f04cdf 2022-07-20 mark err = view_close_child(view);
5965 05f04cdf 2022-07-20 mark if (err)
5966 05f04cdf 2022-07-20 mark return err;
5967 05f04cdf 2022-07-20 mark err = view_set_child(view, log_view);
5968 05f04cdf 2022-07-20 mark if (err)
5969 05f04cdf 2022-07-20 mark return err;
5970 05f04cdf 2022-07-20 mark view->focus_child = 1;
5971 05f04cdf 2022-07-20 mark } else
5972 05f04cdf 2022-07-20 mark *new_view = log_view;
5973 05f04cdf 2022-07-20 mark break;
5974 05f04cdf 2022-07-20 mark }
5975 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5976 1e37a5c2 2019-05-12 jcs case '\r': {
5977 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5978 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5979 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5980 640cd7ff 2022-06-22 mark
5981 640cd7ff 2022-06-22 mark view->count = 0;
5982 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5983 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5984 1e37a5c2 2019-05-12 jcs if (id == NULL)
5985 1e37a5c2 2019-05-12 jcs break;
5986 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5987 1e37a5c2 2019-05-12 jcs if (err)
5988 1e37a5c2 2019-05-12 jcs break;
5989 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5990 c0f61fa4 2022-07-11 mark if (*new_view) {
5991 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5992 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5993 c0f61fa4 2022-07-11 mark if (err)
5994 c0f61fa4 2022-07-11 mark break;
5995 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5996 c0f61fa4 2022-07-11 mark } else {
5997 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
5998 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
5999 9b058f45 2022-06-30 mark
6000 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6001 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6002 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6003 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6004 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6005 c0f61fa4 2022-07-11 mark break;
6006 c0f61fa4 2022-07-11 mark }
6007 15a94983 2018-12-23 stsp }
6008 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6009 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6010 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6011 1e37a5c2 2019-05-12 jcs if (err) {
6012 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6013 1e37a5c2 2019-05-12 jcs break;
6014 1e37a5c2 2019-05-12 jcs }
6015 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6016 c0f61fa4 2022-07-11 mark s->selected_line;
6017 c0f61fa4 2022-07-11 mark if (*new_view)
6018 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6019 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6020 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6021 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6022 9b058f45 2022-06-30 mark if (err)
6023 9b058f45 2022-06-30 mark break;
6024 9b058f45 2022-06-30 mark }
6025 9b058f45 2022-06-30 mark
6026 e78dc838 2020-12-04 stsp view->focussed = 0;
6027 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6028 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6029 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6030 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6031 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6032 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6033 1e37a5c2 2019-05-12 jcs if (err)
6034 34bc9ec9 2019-02-22 stsp break;
6035 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6036 0dbbbe90 2022-06-17 op if (err)
6037 0dbbbe90 2022-06-17 op break;
6038 e78dc838 2020-12-04 stsp view->focus_child = 1;
6039 1e37a5c2 2019-05-12 jcs } else
6040 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6041 1e37a5c2 2019-05-12 jcs if (err)
6042 e5a0f69f 2018-08-18 stsp break;
6043 1e37a5c2 2019-05-12 jcs break;
6044 1e37a5c2 2019-05-12 jcs }
6045 83cc4199 2022-06-13 stsp case CTRL('d'):
6046 33c3719a 2022-06-15 stsp case 'd':
6047 83cc4199 2022-06-13 stsp nscroll /= 2;
6048 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6049 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6050 ea025d1d 2020-02-22 naddy case CTRL('f'):
6051 61417565 2022-06-20 mark case 'f':
6052 1e37a5c2 2019-05-12 jcs case ' ':
6053 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6054 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6055 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6056 640cd7ff 2022-06-22 mark view->count = 0;
6057 e5a0f69f 2018-08-18 stsp break;
6058 1e37a5c2 2019-05-12 jcs }
6059 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6060 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6061 83cc4199 2022-06-13 stsp s->selected_line +=
6062 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6063 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6064 1e37a5c2 2019-05-12 jcs }
6065 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6066 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6067 1e37a5c2 2019-05-12 jcs else
6068 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6069 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6070 1e37a5c2 2019-05-12 jcs break;
6071 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6072 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6073 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6074 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6075 1e37a5c2 2019-05-12 jcs }
6076 1e37a5c2 2019-05-12 jcs break;
6077 1e37a5c2 2019-05-12 jcs default:
6078 640cd7ff 2022-06-22 mark view->count = 0;
6079 1e37a5c2 2019-05-12 jcs break;
6080 a70480e0 2018-06-23 stsp }
6081 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6082 917d79a7 2022-07-01 stsp }
6083 917d79a7 2022-07-01 stsp
6084 917d79a7 2022-07-01 stsp static const struct got_error *
6085 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6086 917d79a7 2022-07-01 stsp {
6087 917d79a7 2022-07-01 stsp const struct got_error *err;
6088 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6089 917d79a7 2022-07-01 stsp
6090 917d79a7 2022-07-01 stsp view->count = 0;
6091 917d79a7 2022-07-01 stsp s->done = 1;
6092 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6093 917d79a7 2022-07-01 stsp s->done = 0;
6094 917d79a7 2022-07-01 stsp if (err)
6095 917d79a7 2022-07-01 stsp return err;
6096 917d79a7 2022-07-01 stsp return run_blame(view);
6097 a70480e0 2018-06-23 stsp }
6098 a70480e0 2018-06-23 stsp
6099 a70480e0 2018-06-23 stsp static const struct got_error *
6100 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6101 9f7d7167 2018-04-29 stsp {
6102 a70480e0 2018-06-23 stsp const struct got_error *error;
6103 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6104 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6105 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6106 0587e10c 2020-07-23 stsp char *link_target = NULL;
6107 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6108 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6109 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6110 a70480e0 2018-06-23 stsp int ch;
6111 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6112 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6113 a70480e0 2018-06-23 stsp
6114 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6115 a70480e0 2018-06-23 stsp switch (ch) {
6116 a70480e0 2018-06-23 stsp case 'c':
6117 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6118 a70480e0 2018-06-23 stsp break;
6119 69069811 2018-08-02 stsp case 'r':
6120 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6121 69069811 2018-08-02 stsp if (repo_path == NULL)
6122 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6123 9ba1d308 2019-10-21 stsp optarg);
6124 69069811 2018-08-02 stsp break;
6125 a70480e0 2018-06-23 stsp default:
6126 17020d27 2019-03-07 stsp usage_blame();
6127 a70480e0 2018-06-23 stsp /* NOTREACHED */
6128 a70480e0 2018-06-23 stsp }
6129 a70480e0 2018-06-23 stsp }
6130 a70480e0 2018-06-23 stsp
6131 a70480e0 2018-06-23 stsp argc -= optind;
6132 a70480e0 2018-06-23 stsp argv += optind;
6133 a70480e0 2018-06-23 stsp
6134 f135c941 2020-02-20 stsp if (argc != 1)
6135 a70480e0 2018-06-23 stsp usage_blame();
6136 6962eb72 2020-02-20 stsp
6137 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6138 0ae84acc 2022-06-15 tracey if (error != NULL)
6139 0ae84acc 2022-06-15 tracey goto done;
6140 0ae84acc 2022-06-15 tracey
6141 69069811 2018-08-02 stsp if (repo_path == NULL) {
6142 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6143 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6144 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6145 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6146 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6147 c156c7a4 2020-12-18 stsp goto done;
6148 f135c941 2020-02-20 stsp if (worktree)
6149 eb41ed75 2019-02-05 stsp repo_path =
6150 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6151 f135c941 2020-02-20 stsp else
6152 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6153 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6154 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6155 c156c7a4 2020-12-18 stsp goto done;
6156 c156c7a4 2020-12-18 stsp }
6157 f135c941 2020-02-20 stsp }
6158 a915003a 2019-02-05 stsp
6159 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6160 c02c541e 2019-03-29 stsp if (error != NULL)
6161 8e94dd5b 2019-01-04 stsp goto done;
6162 69069811 2018-08-02 stsp
6163 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6164 f135c941 2020-02-20 stsp worktree);
6165 c02c541e 2019-03-29 stsp if (error)
6166 92205607 2019-01-04 stsp goto done;
6167 69069811 2018-08-02 stsp
6168 f135c941 2020-02-20 stsp init_curses();
6169 f135c941 2020-02-20 stsp
6170 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6171 51a10b52 2020-12-26 stsp if (error)
6172 51a10b52 2020-12-26 stsp goto done;
6173 51a10b52 2020-12-26 stsp
6174 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6175 eb41ed75 2019-02-05 stsp if (error)
6176 69069811 2018-08-02 stsp goto done;
6177 a70480e0 2018-06-23 stsp
6178 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6179 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6180 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6181 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6182 a70480e0 2018-06-23 stsp if (error != NULL)
6183 66b4983c 2018-06-23 stsp goto done;
6184 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6185 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6186 a70480e0 2018-06-23 stsp } else {
6187 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6188 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6189 a70480e0 2018-06-23 stsp }
6190 a19e88aa 2018-06-23 stsp if (error != NULL)
6191 8b473291 2019-02-21 stsp goto done;
6192 8b473291 2019-02-21 stsp
6193 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6194 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6195 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6196 e1cd8fed 2018-08-01 stsp goto done;
6197 e1cd8fed 2018-08-01 stsp }
6198 0587e10c 2020-07-23 stsp
6199 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6200 a44927cc 2022-04-07 stsp if (error)
6201 a44927cc 2022-04-07 stsp goto done;
6202 a44927cc 2022-04-07 stsp
6203 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6204 a44927cc 2022-04-07 stsp commit, repo);
6205 7cbe629d 2018-08-04 stsp if (error)
6206 7cbe629d 2018-08-04 stsp goto done;
6207 0587e10c 2020-07-23 stsp
6208 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6209 78756c87 2020-11-24 stsp commit_id, repo);
6210 0587e10c 2020-07-23 stsp if (error)
6211 0587e10c 2020-07-23 stsp goto done;
6212 12314ad4 2019-08-31 stsp if (worktree) {
6213 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6214 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6215 12314ad4 2019-08-31 stsp worktree = NULL;
6216 12314ad4 2019-08-31 stsp }
6217 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6218 a70480e0 2018-06-23 stsp done:
6219 69069811 2018-08-02 stsp free(repo_path);
6220 f135c941 2020-02-20 stsp free(in_repo_path);
6221 0587e10c 2020-07-23 stsp free(link_target);
6222 69069811 2018-08-02 stsp free(cwd);
6223 a70480e0 2018-06-23 stsp free(commit_id);
6224 a44927cc 2022-04-07 stsp if (commit)
6225 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6226 eb41ed75 2019-02-05 stsp if (worktree)
6227 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6228 1d0f4054 2021-06-17 stsp if (repo) {
6229 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6230 1d0f4054 2021-06-17 stsp if (error == NULL)
6231 1d0f4054 2021-06-17 stsp error = close_err;
6232 1d0f4054 2021-06-17 stsp }
6233 0ae84acc 2022-06-15 tracey if (pack_fds) {
6234 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6235 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6236 0ae84acc 2022-06-15 tracey if (error == NULL)
6237 0ae84acc 2022-06-15 tracey error = pack_err;
6238 0ae84acc 2022-06-15 tracey }
6239 51a10b52 2020-12-26 stsp tog_free_refs();
6240 a70480e0 2018-06-23 stsp return error;
6241 ffd1d5e5 2018-06-23 stsp }
6242 ffd1d5e5 2018-06-23 stsp
6243 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6244 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6245 ffd1d5e5 2018-06-23 stsp {
6246 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6247 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6248 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6249 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6250 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6251 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6252 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6253 ffd1d5e5 2018-06-23 stsp
6254 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6255 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6256 9b058f45 2022-06-30 mark --limit; /* border */
6257 ffd1d5e5 2018-06-23 stsp
6258 f7d12f7e 2018-08-01 stsp werase(view->window);
6259 ffd1d5e5 2018-06-23 stsp
6260 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6261 ffd1d5e5 2018-06-23 stsp return NULL;
6262 ffd1d5e5 2018-06-23 stsp
6263 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6264 ccda2f4d 2022-06-16 stsp 0, 0);
6265 ffd1d5e5 2018-06-23 stsp if (err)
6266 ffd1d5e5 2018-06-23 stsp return err;
6267 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6268 a3404814 2018-09-02 stsp wstandout(view->window);
6269 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6270 11b20872 2019-11-08 stsp if (tc)
6271 11b20872 2019-11-08 stsp wattr_on(view->window,
6272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6273 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6274 11b20872 2019-11-08 stsp if (tc)
6275 11b20872 2019-11-08 stsp wattr_off(view->window,
6276 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6277 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6278 a3404814 2018-09-02 stsp wstandend(view->window);
6279 2550e4c3 2018-07-13 stsp free(wline);
6280 2550e4c3 2018-07-13 stsp wline = NULL;
6281 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6282 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6283 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6284 ffd1d5e5 2018-06-23 stsp return NULL;
6285 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6286 ccda2f4d 2022-06-16 stsp 0, 0);
6287 ce52c690 2018-06-23 stsp if (err)
6288 ce52c690 2018-06-23 stsp return err;
6289 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6290 2550e4c3 2018-07-13 stsp free(wline);
6291 2550e4c3 2018-07-13 stsp wline = NULL;
6292 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6293 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6294 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6295 ffd1d5e5 2018-06-23 stsp return NULL;
6296 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6297 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6298 a1eca9bb 2018-06-23 stsp return NULL;
6299 ffd1d5e5 2018-06-23 stsp
6300 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6301 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6302 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6303 0cf4efb1 2018-09-29 stsp if (view->focussed)
6304 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6305 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6306 ffd1d5e5 2018-06-23 stsp }
6307 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6308 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6309 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6310 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6311 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6312 ffd1d5e5 2018-06-23 stsp return NULL;
6313 ffd1d5e5 2018-06-23 stsp n = 1;
6314 ffd1d5e5 2018-06-23 stsp } else {
6315 ffd1d5e5 2018-06-23 stsp n = 0;
6316 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6317 ffd1d5e5 2018-06-23 stsp }
6318 ffd1d5e5 2018-06-23 stsp
6319 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6320 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6321 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6322 848d6979 2019-08-12 stsp const char *modestr = "";
6323 56e0773d 2019-11-28 stsp mode_t mode;
6324 1d13200f 2018-07-12 stsp
6325 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6326 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6327 56e0773d 2019-11-28 stsp
6328 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6329 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6330 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6331 1d13200f 2018-07-12 stsp if (err)
6332 638f9024 2019-05-13 stsp return got_error_from_errno(
6333 230a42bd 2019-05-11 jcs "got_object_id_str");
6334 1d13200f 2018-07-12 stsp }
6335 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6336 63c5ca5d 2019-08-24 stsp modestr = "$";
6337 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6338 0d6c6ee3 2020-05-20 stsp int i;
6339 0d6c6ee3 2020-05-20 stsp
6340 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6341 d86d3b18 2020-12-01 naddy te, s->repo);
6342 0d6c6ee3 2020-05-20 stsp if (err) {
6343 0d6c6ee3 2020-05-20 stsp free(id_str);
6344 0d6c6ee3 2020-05-20 stsp return err;
6345 0d6c6ee3 2020-05-20 stsp }
6346 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6347 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6348 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6349 0d6c6ee3 2020-05-20 stsp }
6350 848d6979 2019-08-12 stsp modestr = "@";
6351 0d6c6ee3 2020-05-20 stsp }
6352 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6353 848d6979 2019-08-12 stsp modestr = "/";
6354 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6355 848d6979 2019-08-12 stsp modestr = "*";
6356 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6357 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6358 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6359 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6360 1d13200f 2018-07-12 stsp free(id_str);
6361 0d6c6ee3 2020-05-20 stsp free(link_target);
6362 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6363 1d13200f 2018-07-12 stsp }
6364 1d13200f 2018-07-12 stsp free(id_str);
6365 0d6c6ee3 2020-05-20 stsp free(link_target);
6366 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6367 ccda2f4d 2022-06-16 stsp 0, 0);
6368 ffd1d5e5 2018-06-23 stsp if (err) {
6369 ffd1d5e5 2018-06-23 stsp free(line);
6370 ffd1d5e5 2018-06-23 stsp break;
6371 ffd1d5e5 2018-06-23 stsp }
6372 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6373 0cf4efb1 2018-09-29 stsp if (view->focussed)
6374 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6375 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6376 ffd1d5e5 2018-06-23 stsp }
6377 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6378 f26dddb7 2019-11-08 stsp if (tc)
6379 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6380 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6381 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6382 f26dddb7 2019-11-08 stsp if (tc)
6383 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6384 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6385 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6386 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6387 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6388 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6389 ffd1d5e5 2018-06-23 stsp free(line);
6390 2550e4c3 2018-07-13 stsp free(wline);
6391 2550e4c3 2018-07-13 stsp wline = NULL;
6392 ffd1d5e5 2018-06-23 stsp n++;
6393 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6394 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6395 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6396 ffd1d5e5 2018-06-23 stsp break;
6397 ffd1d5e5 2018-06-23 stsp }
6398 ffd1d5e5 2018-06-23 stsp
6399 ffd1d5e5 2018-06-23 stsp return err;
6400 ffd1d5e5 2018-06-23 stsp }
6401 ffd1d5e5 2018-06-23 stsp
6402 ffd1d5e5 2018-06-23 stsp static void
6403 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6404 ffd1d5e5 2018-06-23 stsp {
6405 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6406 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6407 fa86c4bf 2020-11-29 stsp int i = 0;
6408 ffd1d5e5 2018-06-23 stsp
6409 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6410 ffd1d5e5 2018-06-23 stsp return;
6411 ffd1d5e5 2018-06-23 stsp
6412 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6413 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6414 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6415 fa86c4bf 2020-11-29 stsp if (!isroot)
6416 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6417 fa86c4bf 2020-11-29 stsp break;
6418 fa86c4bf 2020-11-29 stsp }
6419 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6420 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6421 ffd1d5e5 2018-06-23 stsp }
6422 ffd1d5e5 2018-06-23 stsp }
6423 ffd1d5e5 2018-06-23 stsp
6424 9b058f45 2022-06-30 mark static const struct got_error *
6425 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6426 ffd1d5e5 2018-06-23 stsp {
6427 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6428 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6429 ffd1d5e5 2018-06-23 stsp int n = 0;
6430 ffd1d5e5 2018-06-23 stsp
6431 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6432 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6433 694d3271 2020-12-01 naddy s->first_displayed_entry);
6434 694d3271 2020-12-01 naddy else
6435 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6436 56e0773d 2019-11-28 stsp
6437 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6438 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6439 9b058f45 2022-06-30 mark if (last)
6440 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6441 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6442 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6443 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6444 768394f3 2019-01-24 stsp }
6445 ffd1d5e5 2018-06-23 stsp }
6446 9b058f45 2022-06-30 mark
6447 9b058f45 2022-06-30 mark return NULL;
6448 ffd1d5e5 2018-06-23 stsp }
6449 ffd1d5e5 2018-06-23 stsp
6450 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6451 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6452 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6453 ffd1d5e5 2018-06-23 stsp {
6454 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6455 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6456 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6457 ffd1d5e5 2018-06-23 stsp
6458 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6459 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6460 56e0773d 2019-11-28 stsp + 1 /* slash */;
6461 ce52c690 2018-06-23 stsp if (te)
6462 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6463 ce52c690 2018-06-23 stsp
6464 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6465 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6466 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6467 ffd1d5e5 2018-06-23 stsp
6468 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6469 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6470 d9765a41 2018-06-23 stsp while (pt) {
6471 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6472 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6473 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6474 cb2ebc8a 2018-06-23 stsp goto done;
6475 cb2ebc8a 2018-06-23 stsp }
6476 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6477 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6478 cb2ebc8a 2018-06-23 stsp goto done;
6479 cb2ebc8a 2018-06-23 stsp }
6480 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6481 ffd1d5e5 2018-06-23 stsp }
6482 ce52c690 2018-06-23 stsp if (te) {
6483 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6484 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6485 ce52c690 2018-06-23 stsp goto done;
6486 ce52c690 2018-06-23 stsp }
6487 cb2ebc8a 2018-06-23 stsp }
6488 ce52c690 2018-06-23 stsp done:
6489 ce52c690 2018-06-23 stsp if (err) {
6490 ce52c690 2018-06-23 stsp free(*path);
6491 ce52c690 2018-06-23 stsp *path = NULL;
6492 ce52c690 2018-06-23 stsp }
6493 ce52c690 2018-06-23 stsp return err;
6494 ce52c690 2018-06-23 stsp }
6495 ce52c690 2018-06-23 stsp
6496 ce52c690 2018-06-23 stsp static const struct got_error *
6497 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6498 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6499 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6500 ce52c690 2018-06-23 stsp {
6501 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6502 ce52c690 2018-06-23 stsp char *path;
6503 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6504 a0de39f3 2019-08-09 stsp
6505 a0de39f3 2019-08-09 stsp *new_view = NULL;
6506 69efd4c4 2018-07-18 stsp
6507 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6508 ce52c690 2018-06-23 stsp if (err)
6509 ce52c690 2018-06-23 stsp return err;
6510 ffd1d5e5 2018-06-23 stsp
6511 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6512 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6513 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6514 83ce39e3 2019-08-12 stsp goto done;
6515 83ce39e3 2019-08-12 stsp }
6516 cdf1ee82 2018-08-01 stsp
6517 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6518 e5a0f69f 2018-08-18 stsp if (err) {
6519 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6520 fc06ba56 2019-08-22 stsp err = NULL;
6521 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6522 e5a0f69f 2018-08-18 stsp } else
6523 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6524 83ce39e3 2019-08-12 stsp done:
6525 83ce39e3 2019-08-12 stsp free(path);
6526 69efd4c4 2018-07-18 stsp return err;
6527 69efd4c4 2018-07-18 stsp }
6528 69efd4c4 2018-07-18 stsp
6529 69efd4c4 2018-07-18 stsp static const struct got_error *
6530 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6531 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6532 69efd4c4 2018-07-18 stsp {
6533 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6534 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6535 69efd4c4 2018-07-18 stsp char *path;
6536 a0de39f3 2019-08-09 stsp
6537 a0de39f3 2019-08-09 stsp *new_view = NULL;
6538 69efd4c4 2018-07-18 stsp
6539 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6540 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6541 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6542 e5a0f69f 2018-08-18 stsp
6543 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6544 69efd4c4 2018-07-18 stsp if (err)
6545 69efd4c4 2018-07-18 stsp return err;
6546 69efd4c4 2018-07-18 stsp
6547 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6548 4e97c21c 2020-12-06 stsp path, 0);
6549 ba4f502b 2018-08-04 stsp if (err)
6550 e5a0f69f 2018-08-18 stsp view_close(log_view);
6551 e5a0f69f 2018-08-18 stsp else
6552 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6553 cb2ebc8a 2018-06-23 stsp free(path);
6554 cb2ebc8a 2018-06-23 stsp return err;
6555 ffd1d5e5 2018-06-23 stsp }
6556 ffd1d5e5 2018-06-23 stsp
6557 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6558 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6559 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6560 ffd1d5e5 2018-06-23 stsp {
6561 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6562 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6563 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6564 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6565 ffd1d5e5 2018-06-23 stsp
6566 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6567 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6568 bc573f3b 2021-07-10 stsp
6569 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6570 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6571 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6572 ffd1d5e5 2018-06-23 stsp
6573 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6574 bc573f3b 2021-07-10 stsp if (err)
6575 bc573f3b 2021-07-10 stsp goto done;
6576 bc573f3b 2021-07-10 stsp
6577 bc573f3b 2021-07-10 stsp /*
6578 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6579 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6580 bc573f3b 2021-07-10 stsp * closed on demand.
6581 bc573f3b 2021-07-10 stsp */
6582 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6583 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6584 bc573f3b 2021-07-10 stsp if (err)
6585 bc573f3b 2021-07-10 stsp goto done;
6586 bc573f3b 2021-07-10 stsp s->tree = s->root;
6587 bc573f3b 2021-07-10 stsp
6588 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6589 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6590 ffd1d5e5 2018-06-23 stsp goto done;
6591 ffd1d5e5 2018-06-23 stsp
6592 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6593 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6594 ffd1d5e5 2018-06-23 stsp goto done;
6595 ffd1d5e5 2018-06-23 stsp }
6596 ffd1d5e5 2018-06-23 stsp
6597 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6598 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6599 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6600 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6601 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6602 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6603 9cd7cbd1 2020-12-07 stsp goto done;
6604 9cd7cbd1 2020-12-07 stsp }
6605 9cd7cbd1 2020-12-07 stsp }
6606 fb2756b9 2018-08-04 stsp s->repo = repo;
6607 c0b01bdb 2019-11-08 stsp
6608 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6609 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6610 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6611 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6612 c0b01bdb 2019-11-08 stsp if (err)
6613 c0b01bdb 2019-11-08 stsp goto done;
6614 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6615 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6616 bc573f3b 2021-07-10 stsp if (err)
6617 c0b01bdb 2019-11-08 stsp goto done;
6618 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6619 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6621 bc573f3b 2021-07-10 stsp if (err)
6622 c0b01bdb 2019-11-08 stsp goto done;
6623 e5a0f69f 2018-08-18 stsp
6624 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6625 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6626 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6627 bc573f3b 2021-07-10 stsp if (err)
6628 11b20872 2019-11-08 stsp goto done;
6629 11b20872 2019-11-08 stsp
6630 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6631 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6632 bc573f3b 2021-07-10 stsp if (err)
6633 c0b01bdb 2019-11-08 stsp goto done;
6634 c0b01bdb 2019-11-08 stsp }
6635 c0b01bdb 2019-11-08 stsp
6636 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6637 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6638 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6639 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6640 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6641 ad80ab7b 2018-08-04 stsp done:
6642 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6643 bc573f3b 2021-07-10 stsp if (commit)
6644 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6645 bc573f3b 2021-07-10 stsp if (err)
6646 bc573f3b 2021-07-10 stsp close_tree_view(view);
6647 ad80ab7b 2018-08-04 stsp return err;
6648 ad80ab7b 2018-08-04 stsp }
6649 ad80ab7b 2018-08-04 stsp
6650 e5a0f69f 2018-08-18 stsp static const struct got_error *
6651 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6652 ad80ab7b 2018-08-04 stsp {
6653 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6654 ad80ab7b 2018-08-04 stsp
6655 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6656 fb2756b9 2018-08-04 stsp free(s->tree_label);
6657 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6658 6484ec90 2018-09-29 stsp free(s->commit_id);
6659 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6660 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6661 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6662 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6663 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6664 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6665 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6666 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6667 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6668 ad80ab7b 2018-08-04 stsp free(parent);
6669 ad80ab7b 2018-08-04 stsp
6670 ad80ab7b 2018-08-04 stsp }
6671 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6672 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6673 bc573f3b 2021-07-10 stsp if (s->root)
6674 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6675 7c32bd05 2019-06-22 stsp return NULL;
6676 7c32bd05 2019-06-22 stsp }
6677 7c32bd05 2019-06-22 stsp
6678 7c32bd05 2019-06-22 stsp static const struct got_error *
6679 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6680 7c32bd05 2019-06-22 stsp {
6681 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6682 7c32bd05 2019-06-22 stsp
6683 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6684 7c32bd05 2019-06-22 stsp return NULL;
6685 7c32bd05 2019-06-22 stsp }
6686 7c32bd05 2019-06-22 stsp
6687 7c32bd05 2019-06-22 stsp static int
6688 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6689 7c32bd05 2019-06-22 stsp {
6690 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6691 7c32bd05 2019-06-22 stsp
6692 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6693 56e0773d 2019-11-28 stsp 0) == 0;
6694 7c32bd05 2019-06-22 stsp }
6695 7c32bd05 2019-06-22 stsp
6696 7c32bd05 2019-06-22 stsp static const struct got_error *
6697 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6698 7c32bd05 2019-06-22 stsp {
6699 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6700 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6701 7c32bd05 2019-06-22 stsp
6702 7c32bd05 2019-06-22 stsp if (!view->searching) {
6703 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6704 7c32bd05 2019-06-22 stsp return NULL;
6705 7c32bd05 2019-06-22 stsp }
6706 7c32bd05 2019-06-22 stsp
6707 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6708 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6709 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6710 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6711 56e0773d 2019-11-28 stsp s->selected_entry);
6712 7c32bd05 2019-06-22 stsp else
6713 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6714 56e0773d 2019-11-28 stsp } else {
6715 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6716 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6717 56e0773d 2019-11-28 stsp else
6718 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6719 56e0773d 2019-11-28 stsp s->selected_entry);
6720 7c32bd05 2019-06-22 stsp }
6721 7c32bd05 2019-06-22 stsp } else {
6722 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6723 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6724 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6725 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6726 56e0773d 2019-11-28 stsp else
6727 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6728 7c32bd05 2019-06-22 stsp }
6729 7c32bd05 2019-06-22 stsp
6730 7c32bd05 2019-06-22 stsp while (1) {
6731 56e0773d 2019-11-28 stsp if (te == NULL) {
6732 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6733 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6734 ac66afb8 2019-06-24 stsp return NULL;
6735 ac66afb8 2019-06-24 stsp }
6736 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6737 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6738 56e0773d 2019-11-28 stsp else
6739 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6740 7c32bd05 2019-06-22 stsp }
6741 7c32bd05 2019-06-22 stsp
6742 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6743 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6744 56e0773d 2019-11-28 stsp s->matched_entry = te;
6745 7c32bd05 2019-06-22 stsp break;
6746 7c32bd05 2019-06-22 stsp }
6747 7c32bd05 2019-06-22 stsp
6748 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6749 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6750 56e0773d 2019-11-28 stsp else
6751 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6752 7c32bd05 2019-06-22 stsp }
6753 e5a0f69f 2018-08-18 stsp
6754 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6755 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6756 7c32bd05 2019-06-22 stsp s->selected = 0;
6757 7c32bd05 2019-06-22 stsp }
6758 7c32bd05 2019-06-22 stsp
6759 e5a0f69f 2018-08-18 stsp return NULL;
6760 ad80ab7b 2018-08-04 stsp }
6761 ad80ab7b 2018-08-04 stsp
6762 ad80ab7b 2018-08-04 stsp static const struct got_error *
6763 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6764 ad80ab7b 2018-08-04 stsp {
6765 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6766 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6767 e5a0f69f 2018-08-18 stsp char *parent_path;
6768 ad80ab7b 2018-08-04 stsp
6769 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6770 e5a0f69f 2018-08-18 stsp if (err)
6771 e5a0f69f 2018-08-18 stsp return err;
6772 ffd1d5e5 2018-06-23 stsp
6773 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6774 e5a0f69f 2018-08-18 stsp free(parent_path);
6775 669b5ffa 2018-10-07 stsp
6776 9b058f45 2022-06-30 mark view_border(view);
6777 e5a0f69f 2018-08-18 stsp return err;
6778 e5a0f69f 2018-08-18 stsp }
6779 ce52c690 2018-06-23 stsp
6780 e5a0f69f 2018-08-18 stsp static const struct got_error *
6781 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6782 e5a0f69f 2018-08-18 stsp {
6783 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6784 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6785 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6786 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6787 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6788 ffd1d5e5 2018-06-23 stsp
6789 e5a0f69f 2018-08-18 stsp switch (ch) {
6790 1e37a5c2 2019-05-12 jcs case 'i':
6791 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6792 640cd7ff 2022-06-22 mark view->count = 0;
6793 1e37a5c2 2019-05-12 jcs break;
6794 5e98fb33 2022-07-22 mark case 'L':
6795 640cd7ff 2022-06-22 mark view->count = 0;
6796 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6797 ffd1d5e5 2018-06-23 stsp break;
6798 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6799 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6800 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6801 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6802 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6803 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6804 49b24ee5 2022-07-03 mark if (err)
6805 49b24ee5 2022-07-03 mark break;
6806 49b24ee5 2022-07-03 mark }
6807 e78dc838 2020-12-04 stsp view->focussed = 0;
6808 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6809 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6810 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6811 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6812 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
6813 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6814 1e37a5c2 2019-05-12 jcs if (err)
6815 1e37a5c2 2019-05-12 jcs return err;
6816 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6817 0dbbbe90 2022-06-17 op if (err)
6818 0dbbbe90 2022-06-17 op return err;
6819 e78dc838 2020-12-04 stsp view->focus_child = 1;
6820 1e37a5c2 2019-05-12 jcs } else
6821 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6822 152c1c93 2020-11-29 stsp break;
6823 5e98fb33 2022-07-22 mark case 'R':
6824 640cd7ff 2022-06-22 mark view->count = 0;
6825 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6826 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6827 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6828 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6829 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6830 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6831 152c1c93 2020-11-29 stsp if (err) {
6832 152c1c93 2020-11-29 stsp view_close(ref_view);
6833 152c1c93 2020-11-29 stsp return err;
6834 152c1c93 2020-11-29 stsp }
6835 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6836 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6837 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6838 49b24ee5 2022-07-03 mark if (err)
6839 49b24ee5 2022-07-03 mark break;
6840 49b24ee5 2022-07-03 mark }
6841 e78dc838 2020-12-04 stsp view->focussed = 0;
6842 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6843 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6844 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6845 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6846 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
6847 152c1c93 2020-11-29 stsp err = view_close_child(view);
6848 152c1c93 2020-11-29 stsp if (err)
6849 152c1c93 2020-11-29 stsp return err;
6850 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6851 0dbbbe90 2022-06-17 op if (err)
6852 0dbbbe90 2022-06-17 op return err;
6853 e78dc838 2020-12-04 stsp view->focus_child = 1;
6854 152c1c93 2020-11-29 stsp } else
6855 152c1c93 2020-11-29 stsp *new_view = ref_view;
6856 e4526bf5 2021-09-03 naddy break;
6857 e4526bf5 2021-09-03 naddy case 'g':
6858 e4526bf5 2021-09-03 naddy case KEY_HOME:
6859 e4526bf5 2021-09-03 naddy s->selected = 0;
6860 640cd7ff 2022-06-22 mark view->count = 0;
6861 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6862 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6863 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6864 e4526bf5 2021-09-03 naddy else
6865 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6866 1e37a5c2 2019-05-12 jcs break;
6867 e4526bf5 2021-09-03 naddy case 'G':
6868 9b058f45 2022-06-30 mark case KEY_END: {
6869 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6870 9b058f45 2022-06-30 mark
6871 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6872 9b058f45 2022-06-30 mark --eos; /* border */
6873 e4526bf5 2021-09-03 naddy s->selected = 0;
6874 640cd7ff 2022-06-22 mark view->count = 0;
6875 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6876 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6877 e4526bf5 2021-09-03 naddy if (te == NULL) {
6878 ad055527 2022-07-16 mark if (s->tree != s->root) {
6879 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6880 e4526bf5 2021-09-03 naddy n++;
6881 e4526bf5 2021-09-03 naddy }
6882 e4526bf5 2021-09-03 naddy break;
6883 e4526bf5 2021-09-03 naddy }
6884 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6885 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6886 e4526bf5 2021-09-03 naddy }
6887 e4526bf5 2021-09-03 naddy if (n > 0)
6888 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6889 e4526bf5 2021-09-03 naddy break;
6890 9b058f45 2022-06-30 mark }
6891 1e37a5c2 2019-05-12 jcs case 'k':
6892 1e37a5c2 2019-05-12 jcs case KEY_UP:
6893 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6894 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6895 1e37a5c2 2019-05-12 jcs s->selected--;
6896 fa86c4bf 2020-11-29 stsp break;
6897 1e37a5c2 2019-05-12 jcs }
6898 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6899 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6900 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6901 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6902 640cd7ff 2022-06-22 mark view->count = 0;
6903 1e37a5c2 2019-05-12 jcs break;
6904 83cc4199 2022-06-13 stsp case CTRL('u'):
6905 33c3719a 2022-06-15 stsp case 'u':
6906 83cc4199 2022-06-13 stsp nscroll /= 2;
6907 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6908 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6909 ea025d1d 2020-02-22 naddy case CTRL('b'):
6910 61417565 2022-06-20 mark case 'b':
6911 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6912 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6913 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6914 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6915 fa86c4bf 2020-11-29 stsp } else {
6916 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6917 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6918 fa86c4bf 2020-11-29 stsp }
6919 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6920 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6921 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6922 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6923 640cd7ff 2022-06-22 mark view->count = 0;
6924 1e37a5c2 2019-05-12 jcs break;
6925 1e37a5c2 2019-05-12 jcs case 'j':
6926 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6927 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6928 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6929 1e37a5c2 2019-05-12 jcs s->selected++;
6930 1e37a5c2 2019-05-12 jcs break;
6931 1e37a5c2 2019-05-12 jcs }
6932 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6933 640cd7ff 2022-06-22 mark == NULL) {
6934 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6935 640cd7ff 2022-06-22 mark view->count = 0;
6936 1e37a5c2 2019-05-12 jcs break;
6937 640cd7ff 2022-06-22 mark }
6938 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6939 1e37a5c2 2019-05-12 jcs break;
6940 83cc4199 2022-06-13 stsp case CTRL('d'):
6941 33c3719a 2022-06-15 stsp case 'd':
6942 83cc4199 2022-06-13 stsp nscroll /= 2;
6943 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6944 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6945 ea025d1d 2020-02-22 naddy case CTRL('f'):
6946 61417565 2022-06-20 mark case 'f':
6947 48bb96f0 2022-06-20 naddy case ' ':
6948 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6949 1e37a5c2 2019-05-12 jcs == NULL) {
6950 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6951 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6952 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6953 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6954 640cd7ff 2022-06-22 mark else
6955 640cd7ff 2022-06-22 mark view->count = 0;
6956 1e37a5c2 2019-05-12 jcs break;
6957 1e37a5c2 2019-05-12 jcs }
6958 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6959 1e37a5c2 2019-05-12 jcs break;
6960 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6961 1e37a5c2 2019-05-12 jcs case '\r':
6962 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6963 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6964 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6965 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6966 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6967 640cd7ff 2022-06-22 mark view->count = 0;
6968 1e37a5c2 2019-05-12 jcs break;
6969 640cd7ff 2022-06-22 mark }
6970 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6971 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6972 1e37a5c2 2019-05-12 jcs entry);
6973 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6974 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6975 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6976 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6977 1e37a5c2 2019-05-12 jcs s->selected_entry =
6978 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6979 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6980 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6981 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6982 9b058f45 2022-06-30 mark if (err)
6983 9b058f45 2022-06-30 mark break;
6984 9b058f45 2022-06-30 mark }
6985 1e37a5c2 2019-05-12 jcs free(parent);
6986 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6987 56e0773d 2019-11-28 stsp s->selected_entry))) {
6988 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6989 640cd7ff 2022-06-22 mark view->count = 0;
6990 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6991 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6992 1e37a5c2 2019-05-12 jcs if (err)
6993 1e37a5c2 2019-05-12 jcs break;
6994 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6995 941e9f74 2019-05-21 stsp if (err) {
6996 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6997 1e37a5c2 2019-05-12 jcs break;
6998 1e37a5c2 2019-05-12 jcs }
6999 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7000 56e0773d 2019-11-28 stsp s->selected_entry))) {
7001 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7002 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
7003 1e37a5c2 2019-05-12 jcs
7004 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
7005 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7006 9b058f45 2022-06-30 mark
7007 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
7008 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7009 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7010 1e37a5c2 2019-05-12 jcs if (err)
7011 1e37a5c2 2019-05-12 jcs break;
7012 9b058f45 2022-06-30 mark
7013 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7014 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7015 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7016 9b058f45 2022-06-30 mark if (err)
7017 9b058f45 2022-06-30 mark break;
7018 9b058f45 2022-06-30 mark }
7019 9b058f45 2022-06-30 mark
7020 640cd7ff 2022-06-22 mark view->count = 0;
7021 e78dc838 2020-12-04 stsp view->focussed = 0;
7022 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7023 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
7024 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
7025 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7026 3c1dfe12 2022-07-08 mark view_transfer_size(blame_view, view);
7027 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7028 669b5ffa 2018-10-07 stsp if (err)
7029 669b5ffa 2018-10-07 stsp return err;
7030 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
7031 0dbbbe90 2022-06-17 op if (err)
7032 0dbbbe90 2022-06-17 op return err;
7033 e78dc838 2020-12-04 stsp view->focus_child = 1;
7034 669b5ffa 2018-10-07 stsp } else
7035 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7036 1e37a5c2 2019-05-12 jcs }
7037 1e37a5c2 2019-05-12 jcs break;
7038 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7039 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7040 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7041 640cd7ff 2022-06-22 mark view->count = 0;
7042 1e37a5c2 2019-05-12 jcs break;
7043 1e37a5c2 2019-05-12 jcs default:
7044 640cd7ff 2022-06-22 mark view->count = 0;
7045 1e37a5c2 2019-05-12 jcs break;
7046 ffd1d5e5 2018-06-23 stsp }
7047 e5a0f69f 2018-08-18 stsp
7048 ffd1d5e5 2018-06-23 stsp return err;
7049 9f7d7167 2018-04-29 stsp }
7050 9f7d7167 2018-04-29 stsp
7051 ffd1d5e5 2018-06-23 stsp __dead static void
7052 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7053 ffd1d5e5 2018-06-23 stsp {
7054 ffd1d5e5 2018-06-23 stsp endwin();
7055 87411fa9 2022-06-16 stsp fprintf(stderr,
7056 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7057 ffd1d5e5 2018-06-23 stsp getprogname());
7058 ffd1d5e5 2018-06-23 stsp exit(1);
7059 ffd1d5e5 2018-06-23 stsp }
7060 ffd1d5e5 2018-06-23 stsp
7061 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7062 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7063 ffd1d5e5 2018-06-23 stsp {
7064 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7065 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7066 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7067 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7068 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7069 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7070 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7071 4e97c21c 2020-12-06 stsp char *label = NULL;
7072 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7073 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7074 ffd1d5e5 2018-06-23 stsp int ch;
7075 5221c383 2018-08-01 stsp struct tog_view *view;
7076 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7077 70ac5f84 2019-03-28 stsp
7078 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7079 ffd1d5e5 2018-06-23 stsp switch (ch) {
7080 ffd1d5e5 2018-06-23 stsp case 'c':
7081 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7082 74283ab8 2020-02-07 stsp break;
7083 74283ab8 2020-02-07 stsp case 'r':
7084 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7085 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7086 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7087 74283ab8 2020-02-07 stsp optarg);
7088 ffd1d5e5 2018-06-23 stsp break;
7089 ffd1d5e5 2018-06-23 stsp default:
7090 e99e2d15 2020-11-24 naddy usage_tree();
7091 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7092 ffd1d5e5 2018-06-23 stsp }
7093 ffd1d5e5 2018-06-23 stsp }
7094 ffd1d5e5 2018-06-23 stsp
7095 ffd1d5e5 2018-06-23 stsp argc -= optind;
7096 ffd1d5e5 2018-06-23 stsp argv += optind;
7097 ffd1d5e5 2018-06-23 stsp
7098 55cccc34 2020-02-20 stsp if (argc > 1)
7099 e99e2d15 2020-11-24 naddy usage_tree();
7100 0ae84acc 2022-06-15 tracey
7101 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7102 0ae84acc 2022-06-15 tracey if (error != NULL)
7103 0ae84acc 2022-06-15 tracey goto done;
7104 74283ab8 2020-02-07 stsp
7105 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7106 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7107 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7108 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7109 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7110 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7111 c156c7a4 2020-12-18 stsp goto done;
7112 55cccc34 2020-02-20 stsp if (worktree)
7113 52185f70 2019-02-05 stsp repo_path =
7114 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7115 55cccc34 2020-02-20 stsp else
7116 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7117 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7118 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7119 c156c7a4 2020-12-18 stsp goto done;
7120 c156c7a4 2020-12-18 stsp }
7121 55cccc34 2020-02-20 stsp }
7122 a915003a 2019-02-05 stsp
7123 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7124 c02c541e 2019-03-29 stsp if (error != NULL)
7125 52185f70 2019-02-05 stsp goto done;
7126 d188b9a6 2019-01-04 stsp
7127 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7128 55cccc34 2020-02-20 stsp repo, worktree);
7129 55cccc34 2020-02-20 stsp if (error)
7130 55cccc34 2020-02-20 stsp goto done;
7131 55cccc34 2020-02-20 stsp
7132 55cccc34 2020-02-20 stsp init_curses();
7133 55cccc34 2020-02-20 stsp
7134 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7135 c02c541e 2019-03-29 stsp if (error)
7136 52185f70 2019-02-05 stsp goto done;
7137 ffd1d5e5 2018-06-23 stsp
7138 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7139 51a10b52 2020-12-26 stsp if (error)
7140 51a10b52 2020-12-26 stsp goto done;
7141 51a10b52 2020-12-26 stsp
7142 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7143 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7144 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7145 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7146 4e97c21c 2020-12-06 stsp if (error)
7147 4e97c21c 2020-12-06 stsp goto done;
7148 4e97c21c 2020-12-06 stsp head_ref_name = label;
7149 4e97c21c 2020-12-06 stsp } else {
7150 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7151 4e97c21c 2020-12-06 stsp if (error == NULL)
7152 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7153 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7154 4e97c21c 2020-12-06 stsp goto done;
7155 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7156 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7157 4e97c21c 2020-12-06 stsp if (error)
7158 4e97c21c 2020-12-06 stsp goto done;
7159 4e97c21c 2020-12-06 stsp }
7160 ffd1d5e5 2018-06-23 stsp
7161 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7162 a44927cc 2022-04-07 stsp if (error)
7163 a44927cc 2022-04-07 stsp goto done;
7164 a44927cc 2022-04-07 stsp
7165 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7166 5221c383 2018-08-01 stsp if (view == NULL) {
7167 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7168 5221c383 2018-08-01 stsp goto done;
7169 5221c383 2018-08-01 stsp }
7170 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7171 ad80ab7b 2018-08-04 stsp if (error)
7172 ad80ab7b 2018-08-04 stsp goto done;
7173 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7174 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7175 d91faf3b 2020-12-01 naddy in_repo_path);
7176 55cccc34 2020-02-20 stsp if (error)
7177 55cccc34 2020-02-20 stsp goto done;
7178 55cccc34 2020-02-20 stsp }
7179 55cccc34 2020-02-20 stsp
7180 55cccc34 2020-02-20 stsp if (worktree) {
7181 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7182 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7183 55cccc34 2020-02-20 stsp worktree = NULL;
7184 55cccc34 2020-02-20 stsp }
7185 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7186 ffd1d5e5 2018-06-23 stsp done:
7187 52185f70 2019-02-05 stsp free(repo_path);
7188 e4a0e26d 2020-02-20 stsp free(cwd);
7189 ffd1d5e5 2018-06-23 stsp free(commit_id);
7190 4e97c21c 2020-12-06 stsp free(label);
7191 486cd271 2020-12-06 stsp if (ref)
7192 486cd271 2020-12-06 stsp got_ref_close(ref);
7193 1d0f4054 2021-06-17 stsp if (repo) {
7194 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7195 1d0f4054 2021-06-17 stsp if (error == NULL)
7196 1d0f4054 2021-06-17 stsp error = close_err;
7197 1d0f4054 2021-06-17 stsp }
7198 0ae84acc 2022-06-15 tracey if (pack_fds) {
7199 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7200 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7201 0ae84acc 2022-06-15 tracey if (error == NULL)
7202 0ae84acc 2022-06-15 tracey error = pack_err;
7203 0ae84acc 2022-06-15 tracey }
7204 51a10b52 2020-12-26 stsp tog_free_refs();
7205 ffd1d5e5 2018-06-23 stsp return error;
7206 6458efa5 2020-11-24 stsp }
7207 6458efa5 2020-11-24 stsp
7208 6458efa5 2020-11-24 stsp static const struct got_error *
7209 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7210 6458efa5 2020-11-24 stsp {
7211 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7212 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7213 6458efa5 2020-11-24 stsp
7214 6458efa5 2020-11-24 stsp s->nrefs = 0;
7215 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7216 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7217 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7218 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7219 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7220 6458efa5 2020-11-24 stsp continue;
7221 6458efa5 2020-11-24 stsp
7222 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7223 6458efa5 2020-11-24 stsp if (re == NULL)
7224 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7225 6458efa5 2020-11-24 stsp
7226 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7227 8924d611 2020-12-26 stsp if (re->ref == NULL)
7228 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7229 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7230 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7231 6458efa5 2020-11-24 stsp }
7232 6458efa5 2020-11-24 stsp
7233 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7234 6458efa5 2020-11-24 stsp return NULL;
7235 6458efa5 2020-11-24 stsp }
7236 6458efa5 2020-11-24 stsp
7237 336075a4 2022-06-25 op static void
7238 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7239 6458efa5 2020-11-24 stsp {
7240 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7241 6458efa5 2020-11-24 stsp
7242 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7243 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7244 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7245 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7246 6458efa5 2020-11-24 stsp free(re);
7247 6458efa5 2020-11-24 stsp }
7248 6458efa5 2020-11-24 stsp }
7249 6458efa5 2020-11-24 stsp
7250 6458efa5 2020-11-24 stsp static const struct got_error *
7251 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7252 6458efa5 2020-11-24 stsp {
7253 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7254 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7255 6458efa5 2020-11-24 stsp
7256 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7257 6458efa5 2020-11-24 stsp s->repo = repo;
7258 6458efa5 2020-11-24 stsp
7259 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7260 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7261 6458efa5 2020-11-24 stsp
7262 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7263 6458efa5 2020-11-24 stsp if (err)
7264 6458efa5 2020-11-24 stsp return err;
7265 34ba6917 2020-11-30 stsp
7266 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7267 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7268 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7269 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7270 6458efa5 2020-11-24 stsp if (err)
7271 6458efa5 2020-11-24 stsp goto done;
7272 6458efa5 2020-11-24 stsp
7273 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7274 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7275 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7276 6458efa5 2020-11-24 stsp if (err)
7277 6458efa5 2020-11-24 stsp goto done;
7278 6458efa5 2020-11-24 stsp
7279 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7280 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7281 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7282 cc488aa7 2022-01-23 stsp if (err)
7283 cc488aa7 2022-01-23 stsp goto done;
7284 cc488aa7 2022-01-23 stsp
7285 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7286 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7287 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7288 6458efa5 2020-11-24 stsp if (err)
7289 6458efa5 2020-11-24 stsp goto done;
7290 6458efa5 2020-11-24 stsp }
7291 6458efa5 2020-11-24 stsp
7292 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7293 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7294 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7295 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7296 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7297 6458efa5 2020-11-24 stsp done:
7298 6458efa5 2020-11-24 stsp if (err)
7299 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7300 6458efa5 2020-11-24 stsp return err;
7301 6458efa5 2020-11-24 stsp }
7302 6458efa5 2020-11-24 stsp
7303 6458efa5 2020-11-24 stsp static const struct got_error *
7304 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7305 6458efa5 2020-11-24 stsp {
7306 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7307 6458efa5 2020-11-24 stsp
7308 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7309 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7310 6458efa5 2020-11-24 stsp
7311 6458efa5 2020-11-24 stsp return NULL;
7312 9f7d7167 2018-04-29 stsp }
7313 ce5b7c56 2019-07-09 stsp
7314 6458efa5 2020-11-24 stsp static const struct got_error *
7315 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7316 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7317 6458efa5 2020-11-24 stsp {
7318 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7319 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7320 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7321 6458efa5 2020-11-24 stsp int obj_type;
7322 6458efa5 2020-11-24 stsp
7323 c42c9805 2020-11-24 stsp *commit_id = NULL;
7324 6458efa5 2020-11-24 stsp
7325 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7326 6458efa5 2020-11-24 stsp if (err)
7327 6458efa5 2020-11-24 stsp return err;
7328 6458efa5 2020-11-24 stsp
7329 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7330 6458efa5 2020-11-24 stsp if (err)
7331 6458efa5 2020-11-24 stsp goto done;
7332 6458efa5 2020-11-24 stsp
7333 6458efa5 2020-11-24 stsp switch (obj_type) {
7334 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7335 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7336 6458efa5 2020-11-24 stsp break;
7337 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7338 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7339 6458efa5 2020-11-24 stsp if (err)
7340 6458efa5 2020-11-24 stsp goto done;
7341 c42c9805 2020-11-24 stsp free(obj_id);
7342 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7343 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7344 c42c9805 2020-11-24 stsp if (err)
7345 6458efa5 2020-11-24 stsp goto done;
7346 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7347 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7348 c42c9805 2020-11-24 stsp goto done;
7349 c42c9805 2020-11-24 stsp }
7350 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7351 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7352 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7353 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7354 c42c9805 2020-11-24 stsp goto done;
7355 c42c9805 2020-11-24 stsp }
7356 6458efa5 2020-11-24 stsp break;
7357 6458efa5 2020-11-24 stsp default:
7358 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7359 c42c9805 2020-11-24 stsp break;
7360 6458efa5 2020-11-24 stsp }
7361 6458efa5 2020-11-24 stsp
7362 c42c9805 2020-11-24 stsp done:
7363 c42c9805 2020-11-24 stsp if (tag)
7364 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7365 c42c9805 2020-11-24 stsp if (err) {
7366 c42c9805 2020-11-24 stsp free(*commit_id);
7367 c42c9805 2020-11-24 stsp *commit_id = NULL;
7368 c42c9805 2020-11-24 stsp }
7369 c42c9805 2020-11-24 stsp return err;
7370 c42c9805 2020-11-24 stsp }
7371 c42c9805 2020-11-24 stsp
7372 c42c9805 2020-11-24 stsp static const struct got_error *
7373 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7374 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7375 c42c9805 2020-11-24 stsp {
7376 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7377 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7378 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7379 c42c9805 2020-11-24 stsp
7380 c42c9805 2020-11-24 stsp *new_view = NULL;
7381 c42c9805 2020-11-24 stsp
7382 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7383 c42c9805 2020-11-24 stsp if (err) {
7384 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7385 c42c9805 2020-11-24 stsp return err;
7386 c42c9805 2020-11-24 stsp else
7387 c42c9805 2020-11-24 stsp return NULL;
7388 c42c9805 2020-11-24 stsp }
7389 c42c9805 2020-11-24 stsp
7390 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7391 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7392 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7393 6458efa5 2020-11-24 stsp goto done;
7394 6458efa5 2020-11-24 stsp }
7395 6458efa5 2020-11-24 stsp
7396 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7397 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7398 6458efa5 2020-11-24 stsp done:
7399 6458efa5 2020-11-24 stsp if (err)
7400 6458efa5 2020-11-24 stsp view_close(log_view);
7401 6458efa5 2020-11-24 stsp else
7402 6458efa5 2020-11-24 stsp *new_view = log_view;
7403 c42c9805 2020-11-24 stsp free(commit_id);
7404 6458efa5 2020-11-24 stsp return err;
7405 6458efa5 2020-11-24 stsp }
7406 6458efa5 2020-11-24 stsp
7407 ce5b7c56 2019-07-09 stsp static void
7408 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7409 6458efa5 2020-11-24 stsp {
7410 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7411 34ba6917 2020-11-30 stsp int i = 0;
7412 6458efa5 2020-11-24 stsp
7413 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7414 6458efa5 2020-11-24 stsp return;
7415 6458efa5 2020-11-24 stsp
7416 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7417 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7418 34ba6917 2020-11-30 stsp if (re == NULL)
7419 34ba6917 2020-11-30 stsp break;
7420 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7421 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7422 6458efa5 2020-11-24 stsp }
7423 6458efa5 2020-11-24 stsp }
7424 6458efa5 2020-11-24 stsp
7425 9b058f45 2022-06-30 mark static const struct got_error *
7426 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7427 6458efa5 2020-11-24 stsp {
7428 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7429 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7430 6458efa5 2020-11-24 stsp int n = 0;
7431 6458efa5 2020-11-24 stsp
7432 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7433 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7434 6458efa5 2020-11-24 stsp else
7435 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7436 6458efa5 2020-11-24 stsp
7437 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7438 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7439 9b058f45 2022-06-30 mark if (last)
7440 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7441 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7442 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7443 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7444 6458efa5 2020-11-24 stsp }
7445 6458efa5 2020-11-24 stsp }
7446 9b058f45 2022-06-30 mark
7447 9b058f45 2022-06-30 mark return NULL;
7448 6458efa5 2020-11-24 stsp }
7449 6458efa5 2020-11-24 stsp
7450 6458efa5 2020-11-24 stsp static const struct got_error *
7451 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7452 6458efa5 2020-11-24 stsp {
7453 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7454 6458efa5 2020-11-24 stsp
7455 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7456 6458efa5 2020-11-24 stsp return NULL;
7457 6458efa5 2020-11-24 stsp }
7458 6458efa5 2020-11-24 stsp
7459 6458efa5 2020-11-24 stsp static int
7460 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7461 6458efa5 2020-11-24 stsp {
7462 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7463 6458efa5 2020-11-24 stsp
7464 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7465 6458efa5 2020-11-24 stsp 0) == 0;
7466 6458efa5 2020-11-24 stsp }
7467 6458efa5 2020-11-24 stsp
7468 6458efa5 2020-11-24 stsp static const struct got_error *
7469 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7470 6458efa5 2020-11-24 stsp {
7471 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7472 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7473 6458efa5 2020-11-24 stsp
7474 6458efa5 2020-11-24 stsp if (!view->searching) {
7475 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7476 6458efa5 2020-11-24 stsp return NULL;
7477 6458efa5 2020-11-24 stsp }
7478 6458efa5 2020-11-24 stsp
7479 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7480 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7481 6458efa5 2020-11-24 stsp if (s->selected_entry)
7482 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7483 6458efa5 2020-11-24 stsp else
7484 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7485 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7486 6458efa5 2020-11-24 stsp } else {
7487 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7488 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7489 6458efa5 2020-11-24 stsp else
7490 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7491 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7492 6458efa5 2020-11-24 stsp }
7493 6458efa5 2020-11-24 stsp } else {
7494 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7495 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7496 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7497 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7498 6458efa5 2020-11-24 stsp else
7499 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7500 6458efa5 2020-11-24 stsp }
7501 6458efa5 2020-11-24 stsp
7502 6458efa5 2020-11-24 stsp while (1) {
7503 6458efa5 2020-11-24 stsp if (re == NULL) {
7504 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7505 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7506 6458efa5 2020-11-24 stsp return NULL;
7507 6458efa5 2020-11-24 stsp }
7508 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7509 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7510 6458efa5 2020-11-24 stsp else
7511 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7512 6458efa5 2020-11-24 stsp }
7513 6458efa5 2020-11-24 stsp
7514 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7515 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7516 6458efa5 2020-11-24 stsp s->matched_entry = re;
7517 6458efa5 2020-11-24 stsp break;
7518 6458efa5 2020-11-24 stsp }
7519 6458efa5 2020-11-24 stsp
7520 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7521 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7522 6458efa5 2020-11-24 stsp else
7523 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7524 6458efa5 2020-11-24 stsp }
7525 6458efa5 2020-11-24 stsp
7526 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7527 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7528 6458efa5 2020-11-24 stsp s->selected = 0;
7529 6458efa5 2020-11-24 stsp }
7530 6458efa5 2020-11-24 stsp
7531 6458efa5 2020-11-24 stsp return NULL;
7532 6458efa5 2020-11-24 stsp }
7533 6458efa5 2020-11-24 stsp
7534 6458efa5 2020-11-24 stsp static const struct got_error *
7535 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7536 6458efa5 2020-11-24 stsp {
7537 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7538 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7539 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7540 6458efa5 2020-11-24 stsp char *line = NULL;
7541 6458efa5 2020-11-24 stsp wchar_t *wline;
7542 6458efa5 2020-11-24 stsp struct tog_color *tc;
7543 6458efa5 2020-11-24 stsp int width, n;
7544 6458efa5 2020-11-24 stsp int limit = view->nlines;
7545 6458efa5 2020-11-24 stsp
7546 6458efa5 2020-11-24 stsp werase(view->window);
7547 6458efa5 2020-11-24 stsp
7548 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7549 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7550 9b058f45 2022-06-30 mark --limit; /* border */
7551 6458efa5 2020-11-24 stsp
7552 6458efa5 2020-11-24 stsp if (limit == 0)
7553 6458efa5 2020-11-24 stsp return NULL;
7554 6458efa5 2020-11-24 stsp
7555 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7556 6458efa5 2020-11-24 stsp
7557 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7558 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7559 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7560 6458efa5 2020-11-24 stsp
7561 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7562 6458efa5 2020-11-24 stsp if (err) {
7563 6458efa5 2020-11-24 stsp free(line);
7564 6458efa5 2020-11-24 stsp return err;
7565 6458efa5 2020-11-24 stsp }
7566 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7567 6458efa5 2020-11-24 stsp wstandout(view->window);
7568 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7569 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7570 6458efa5 2020-11-24 stsp wstandend(view->window);
7571 6458efa5 2020-11-24 stsp free(wline);
7572 6458efa5 2020-11-24 stsp wline = NULL;
7573 6458efa5 2020-11-24 stsp free(line);
7574 6458efa5 2020-11-24 stsp line = NULL;
7575 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7576 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7577 6458efa5 2020-11-24 stsp if (--limit <= 0)
7578 6458efa5 2020-11-24 stsp return NULL;
7579 6458efa5 2020-11-24 stsp
7580 6458efa5 2020-11-24 stsp n = 0;
7581 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7582 6458efa5 2020-11-24 stsp char *line = NULL;
7583 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7584 6458efa5 2020-11-24 stsp
7585 b4996bee 2022-06-16 stsp if (s->show_date) {
7586 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7587 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7588 b4996bee 2022-06-16 stsp struct got_object_id *id;
7589 b4996bee 2022-06-16 stsp struct tm tm;
7590 b4996bee 2022-06-16 stsp time_t t;
7591 b4996bee 2022-06-16 stsp
7592 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7593 b4996bee 2022-06-16 stsp if (err)
7594 b4996bee 2022-06-16 stsp return err;
7595 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7596 b4996bee 2022-06-16 stsp if (err) {
7597 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7598 b4996bee 2022-06-16 stsp free(id);
7599 b4996bee 2022-06-16 stsp return err;
7600 b4996bee 2022-06-16 stsp }
7601 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7602 b4996bee 2022-06-16 stsp id);
7603 b4996bee 2022-06-16 stsp if (err) {
7604 b4996bee 2022-06-16 stsp free(id);
7605 b4996bee 2022-06-16 stsp return err;
7606 b4996bee 2022-06-16 stsp }
7607 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7608 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7609 b4996bee 2022-06-16 stsp } else {
7610 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7611 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7612 b4996bee 2022-06-16 stsp }
7613 b4996bee 2022-06-16 stsp free(id);
7614 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7615 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7616 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7617 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7618 b4996bee 2022-06-16 stsp }
7619 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7620 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7621 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7622 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7623 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7624 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7625 6458efa5 2020-11-24 stsp struct got_object_id *id;
7626 6458efa5 2020-11-24 stsp char *id_str;
7627 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7628 6458efa5 2020-11-24 stsp if (err)
7629 6458efa5 2020-11-24 stsp return err;
7630 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7631 6458efa5 2020-11-24 stsp if (err) {
7632 6458efa5 2020-11-24 stsp free(id);
7633 6458efa5 2020-11-24 stsp return err;
7634 6458efa5 2020-11-24 stsp }
7635 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7636 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7637 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7638 6458efa5 2020-11-24 stsp free(id);
7639 6458efa5 2020-11-24 stsp free(id_str);
7640 6458efa5 2020-11-24 stsp return err;
7641 6458efa5 2020-11-24 stsp }
7642 6458efa5 2020-11-24 stsp free(id);
7643 6458efa5 2020-11-24 stsp free(id_str);
7644 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7645 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7646 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7647 6458efa5 2020-11-24 stsp
7648 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7649 ccda2f4d 2022-06-16 stsp 0, 0);
7650 6458efa5 2020-11-24 stsp if (err) {
7651 6458efa5 2020-11-24 stsp free(line);
7652 6458efa5 2020-11-24 stsp return err;
7653 6458efa5 2020-11-24 stsp }
7654 6458efa5 2020-11-24 stsp if (n == s->selected) {
7655 6458efa5 2020-11-24 stsp if (view->focussed)
7656 6458efa5 2020-11-24 stsp wstandout(view->window);
7657 6458efa5 2020-11-24 stsp s->selected_entry = re;
7658 6458efa5 2020-11-24 stsp }
7659 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7660 6458efa5 2020-11-24 stsp if (tc)
7661 6458efa5 2020-11-24 stsp wattr_on(view->window,
7662 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7663 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7664 6458efa5 2020-11-24 stsp if (tc)
7665 6458efa5 2020-11-24 stsp wattr_off(view->window,
7666 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7667 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7668 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7669 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7670 6458efa5 2020-11-24 stsp wstandend(view->window);
7671 6458efa5 2020-11-24 stsp free(line);
7672 6458efa5 2020-11-24 stsp free(wline);
7673 6458efa5 2020-11-24 stsp wline = NULL;
7674 6458efa5 2020-11-24 stsp n++;
7675 6458efa5 2020-11-24 stsp s->ndisplayed++;
7676 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7677 6458efa5 2020-11-24 stsp
7678 6458efa5 2020-11-24 stsp limit--;
7679 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7680 6458efa5 2020-11-24 stsp }
7681 6458efa5 2020-11-24 stsp
7682 9b058f45 2022-06-30 mark view_border(view);
7683 6458efa5 2020-11-24 stsp return err;
7684 6458efa5 2020-11-24 stsp }
7685 6458efa5 2020-11-24 stsp
7686 6458efa5 2020-11-24 stsp static const struct got_error *
7687 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7688 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7689 c42c9805 2020-11-24 stsp {
7690 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7691 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7692 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7693 c42c9805 2020-11-24 stsp
7694 c42c9805 2020-11-24 stsp *new_view = NULL;
7695 c42c9805 2020-11-24 stsp
7696 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7697 c42c9805 2020-11-24 stsp if (err) {
7698 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7699 c42c9805 2020-11-24 stsp return err;
7700 c42c9805 2020-11-24 stsp else
7701 c42c9805 2020-11-24 stsp return NULL;
7702 c42c9805 2020-11-24 stsp }
7703 c42c9805 2020-11-24 stsp
7704 c42c9805 2020-11-24 stsp
7705 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7706 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7707 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7708 c42c9805 2020-11-24 stsp goto done;
7709 c42c9805 2020-11-24 stsp }
7710 c42c9805 2020-11-24 stsp
7711 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7712 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7713 c42c9805 2020-11-24 stsp if (err)
7714 c42c9805 2020-11-24 stsp goto done;
7715 c42c9805 2020-11-24 stsp
7716 c42c9805 2020-11-24 stsp *new_view = tree_view;
7717 c42c9805 2020-11-24 stsp done:
7718 c42c9805 2020-11-24 stsp free(commit_id);
7719 c42c9805 2020-11-24 stsp return err;
7720 c42c9805 2020-11-24 stsp }
7721 c42c9805 2020-11-24 stsp static const struct got_error *
7722 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7723 6458efa5 2020-11-24 stsp {
7724 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7725 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7726 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7727 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7728 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7729 6458efa5 2020-11-24 stsp
7730 6458efa5 2020-11-24 stsp switch (ch) {
7731 6458efa5 2020-11-24 stsp case 'i':
7732 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7733 640cd7ff 2022-06-22 mark view->count = 0;
7734 7f66531d 2021-11-16 stsp break;
7735 b4996bee 2022-06-16 stsp case 'm':
7736 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7737 640cd7ff 2022-06-22 mark view->count = 0;
7738 b4996bee 2022-06-16 stsp break;
7739 07a065fe 2021-11-20 stsp case 'o':
7740 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7741 640cd7ff 2022-06-22 mark view->count = 0;
7742 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7743 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7744 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7745 50617b77 2021-11-20 stsp if (err)
7746 50617b77 2021-11-20 stsp break;
7747 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7748 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7749 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7750 7f66531d 2021-11-16 stsp if (err)
7751 7f66531d 2021-11-16 stsp break;
7752 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7753 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7754 6458efa5 2020-11-24 stsp break;
7755 6458efa5 2020-11-24 stsp case KEY_ENTER:
7756 6458efa5 2020-11-24 stsp case '\r':
7757 640cd7ff 2022-06-22 mark view->count = 0;
7758 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7759 6458efa5 2020-11-24 stsp break;
7760 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7761 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7762 9b058f45 2022-06-30 mark
7763 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7764 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7765 9b058f45 2022-06-30 mark if (err)
7766 9b058f45 2022-06-30 mark break;
7767 9b058f45 2022-06-30 mark
7768 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7769 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7770 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7771 9b058f45 2022-06-30 mark if (err)
7772 9b058f45 2022-06-30 mark break;
7773 9b058f45 2022-06-30 mark }
7774 9b058f45 2022-06-30 mark
7775 e78dc838 2020-12-04 stsp view->focussed = 0;
7776 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7777 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7778 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7779 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7780 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
7781 6458efa5 2020-11-24 stsp err = view_close_child(view);
7782 6458efa5 2020-11-24 stsp if (err)
7783 6458efa5 2020-11-24 stsp return err;
7784 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7785 0dbbbe90 2022-06-17 op if (err)
7786 0dbbbe90 2022-06-17 op return err;
7787 e78dc838 2020-12-04 stsp view->focus_child = 1;
7788 6458efa5 2020-11-24 stsp } else
7789 6458efa5 2020-11-24 stsp *new_view = log_view;
7790 6458efa5 2020-11-24 stsp break;
7791 5e98fb33 2022-07-22 mark case 'T':
7792 640cd7ff 2022-06-22 mark view->count = 0;
7793 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7794 c42c9805 2020-11-24 stsp break;
7795 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7796 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7797 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7798 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7799 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7800 c42c9805 2020-11-24 stsp break;
7801 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7802 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7803 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7804 49b24ee5 2022-07-03 mark if (err)
7805 49b24ee5 2022-07-03 mark break;
7806 49b24ee5 2022-07-03 mark }
7807 e78dc838 2020-12-04 stsp view->focussed = 0;
7808 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7809 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7810 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7811 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7812 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
7813 c42c9805 2020-11-24 stsp err = view_close_child(view);
7814 c42c9805 2020-11-24 stsp if (err)
7815 c42c9805 2020-11-24 stsp return err;
7816 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7817 0dbbbe90 2022-06-17 op if (err)
7818 0dbbbe90 2022-06-17 op return err;
7819 e78dc838 2020-12-04 stsp view->focus_child = 1;
7820 c42c9805 2020-11-24 stsp } else
7821 c42c9805 2020-11-24 stsp *new_view = tree_view;
7822 c42c9805 2020-11-24 stsp break;
7823 e4526bf5 2021-09-03 naddy case 'g':
7824 e4526bf5 2021-09-03 naddy case KEY_HOME:
7825 e4526bf5 2021-09-03 naddy s->selected = 0;
7826 640cd7ff 2022-06-22 mark view->count = 0;
7827 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7828 e4526bf5 2021-09-03 naddy break;
7829 e4526bf5 2021-09-03 naddy case 'G':
7830 9b058f45 2022-06-30 mark case KEY_END: {
7831 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7832 9b058f45 2022-06-30 mark
7833 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7834 9b058f45 2022-06-30 mark --eos; /* border */
7835 e4526bf5 2021-09-03 naddy s->selected = 0;
7836 640cd7ff 2022-06-22 mark view->count = 0;
7837 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7838 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7839 e4526bf5 2021-09-03 naddy if (re == NULL)
7840 e4526bf5 2021-09-03 naddy break;
7841 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7842 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7843 e4526bf5 2021-09-03 naddy }
7844 e4526bf5 2021-09-03 naddy if (n > 0)
7845 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7846 e4526bf5 2021-09-03 naddy break;
7847 9b058f45 2022-06-30 mark }
7848 6458efa5 2020-11-24 stsp case 'k':
7849 6458efa5 2020-11-24 stsp case KEY_UP:
7850 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7851 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7852 6458efa5 2020-11-24 stsp s->selected--;
7853 6458efa5 2020-11-24 stsp break;
7854 34ba6917 2020-11-30 stsp }
7855 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7856 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7857 640cd7ff 2022-06-22 mark view->count = 0;
7858 6458efa5 2020-11-24 stsp break;
7859 83cc4199 2022-06-13 stsp case CTRL('u'):
7860 33c3719a 2022-06-15 stsp case 'u':
7861 83cc4199 2022-06-13 stsp nscroll /= 2;
7862 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7863 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7864 6458efa5 2020-11-24 stsp case CTRL('b'):
7865 61417565 2022-06-20 mark case 'b':
7866 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7867 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7868 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7869 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7870 640cd7ff 2022-06-22 mark view->count = 0;
7871 6458efa5 2020-11-24 stsp break;
7872 6458efa5 2020-11-24 stsp case 'j':
7873 6458efa5 2020-11-24 stsp case KEY_DOWN:
7874 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7875 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7876 6458efa5 2020-11-24 stsp s->selected++;
7877 6458efa5 2020-11-24 stsp break;
7878 6458efa5 2020-11-24 stsp }
7879 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7880 6458efa5 2020-11-24 stsp /* can't scroll any further */
7881 640cd7ff 2022-06-22 mark view->count = 0;
7882 6458efa5 2020-11-24 stsp break;
7883 640cd7ff 2022-06-22 mark }
7884 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7885 6458efa5 2020-11-24 stsp break;
7886 83cc4199 2022-06-13 stsp case CTRL('d'):
7887 33c3719a 2022-06-15 stsp case 'd':
7888 83cc4199 2022-06-13 stsp nscroll /= 2;
7889 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7890 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7891 6458efa5 2020-11-24 stsp case CTRL('f'):
7892 61417565 2022-06-20 mark case 'f':
7893 48bb96f0 2022-06-20 naddy case ' ':
7894 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7895 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7896 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7897 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7898 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7899 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7900 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7901 640cd7ff 2022-06-22 mark view->count = 0;
7902 6458efa5 2020-11-24 stsp break;
7903 6458efa5 2020-11-24 stsp }
7904 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7905 6458efa5 2020-11-24 stsp break;
7906 6458efa5 2020-11-24 stsp case CTRL('l'):
7907 640cd7ff 2022-06-22 mark view->count = 0;
7908 8924d611 2020-12-26 stsp tog_free_refs();
7909 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7910 8924d611 2020-12-26 stsp if (err)
7911 8924d611 2020-12-26 stsp break;
7912 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7913 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7914 6458efa5 2020-11-24 stsp break;
7915 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7916 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7917 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7918 6458efa5 2020-11-24 stsp break;
7919 6458efa5 2020-11-24 stsp default:
7920 640cd7ff 2022-06-22 mark view->count = 0;
7921 6458efa5 2020-11-24 stsp break;
7922 6458efa5 2020-11-24 stsp }
7923 6458efa5 2020-11-24 stsp
7924 6458efa5 2020-11-24 stsp return err;
7925 6458efa5 2020-11-24 stsp }
7926 6458efa5 2020-11-24 stsp
7927 6458efa5 2020-11-24 stsp __dead static void
7928 6458efa5 2020-11-24 stsp usage_ref(void)
7929 6458efa5 2020-11-24 stsp {
7930 6458efa5 2020-11-24 stsp endwin();
7931 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7932 6458efa5 2020-11-24 stsp getprogname());
7933 6458efa5 2020-11-24 stsp exit(1);
7934 6458efa5 2020-11-24 stsp }
7935 6458efa5 2020-11-24 stsp
7936 6458efa5 2020-11-24 stsp static const struct got_error *
7937 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7938 6458efa5 2020-11-24 stsp {
7939 6458efa5 2020-11-24 stsp const struct got_error *error;
7940 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7941 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7942 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7943 6458efa5 2020-11-24 stsp int ch;
7944 6458efa5 2020-11-24 stsp struct tog_view *view;
7945 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7946 6458efa5 2020-11-24 stsp
7947 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7948 6458efa5 2020-11-24 stsp switch (ch) {
7949 6458efa5 2020-11-24 stsp case 'r':
7950 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7951 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7952 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7953 6458efa5 2020-11-24 stsp optarg);
7954 6458efa5 2020-11-24 stsp break;
7955 6458efa5 2020-11-24 stsp default:
7956 e99e2d15 2020-11-24 naddy usage_ref();
7957 6458efa5 2020-11-24 stsp /* NOTREACHED */
7958 6458efa5 2020-11-24 stsp }
7959 6458efa5 2020-11-24 stsp }
7960 6458efa5 2020-11-24 stsp
7961 6458efa5 2020-11-24 stsp argc -= optind;
7962 6458efa5 2020-11-24 stsp argv += optind;
7963 6458efa5 2020-11-24 stsp
7964 6458efa5 2020-11-24 stsp if (argc > 1)
7965 e99e2d15 2020-11-24 naddy usage_ref();
7966 0ae84acc 2022-06-15 tracey
7967 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7968 0ae84acc 2022-06-15 tracey if (error != NULL)
7969 0ae84acc 2022-06-15 tracey goto done;
7970 6458efa5 2020-11-24 stsp
7971 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7972 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7973 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7974 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7975 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7976 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7977 c156c7a4 2020-12-18 stsp goto done;
7978 6458efa5 2020-11-24 stsp if (worktree)
7979 6458efa5 2020-11-24 stsp repo_path =
7980 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7981 6458efa5 2020-11-24 stsp else
7982 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7983 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7984 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7985 c156c7a4 2020-12-18 stsp goto done;
7986 c156c7a4 2020-12-18 stsp }
7987 6458efa5 2020-11-24 stsp }
7988 6458efa5 2020-11-24 stsp
7989 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7990 6458efa5 2020-11-24 stsp if (error != NULL)
7991 6458efa5 2020-11-24 stsp goto done;
7992 6458efa5 2020-11-24 stsp
7993 6458efa5 2020-11-24 stsp init_curses();
7994 6458efa5 2020-11-24 stsp
7995 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7996 51a10b52 2020-12-26 stsp if (error)
7997 51a10b52 2020-12-26 stsp goto done;
7998 51a10b52 2020-12-26 stsp
7999 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8000 6458efa5 2020-11-24 stsp if (error)
8001 6458efa5 2020-11-24 stsp goto done;
8002 6458efa5 2020-11-24 stsp
8003 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8004 6458efa5 2020-11-24 stsp if (view == NULL) {
8005 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8006 6458efa5 2020-11-24 stsp goto done;
8007 6458efa5 2020-11-24 stsp }
8008 6458efa5 2020-11-24 stsp
8009 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8010 6458efa5 2020-11-24 stsp if (error)
8011 6458efa5 2020-11-24 stsp goto done;
8012 6458efa5 2020-11-24 stsp
8013 6458efa5 2020-11-24 stsp if (worktree) {
8014 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8015 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8016 6458efa5 2020-11-24 stsp worktree = NULL;
8017 6458efa5 2020-11-24 stsp }
8018 6458efa5 2020-11-24 stsp error = view_loop(view);
8019 6458efa5 2020-11-24 stsp done:
8020 6458efa5 2020-11-24 stsp free(repo_path);
8021 6458efa5 2020-11-24 stsp free(cwd);
8022 1d0f4054 2021-06-17 stsp if (repo) {
8023 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8024 1d0f4054 2021-06-17 stsp if (close_err)
8025 1d0f4054 2021-06-17 stsp error = close_err;
8026 1d0f4054 2021-06-17 stsp }
8027 0ae84acc 2022-06-15 tracey if (pack_fds) {
8028 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8029 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8030 0ae84acc 2022-06-15 tracey if (error == NULL)
8031 0ae84acc 2022-06-15 tracey error = pack_err;
8032 0ae84acc 2022-06-15 tracey }
8033 51a10b52 2020-12-26 stsp tog_free_refs();
8034 6458efa5 2020-11-24 stsp return error;
8035 6458efa5 2020-11-24 stsp }
8036 6458efa5 2020-11-24 stsp
8037 9b058f45 2022-06-30 mark /*
8038 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8039 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8040 9b058f45 2022-06-30 mark */
8041 6458efa5 2020-11-24 stsp static void
8042 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8043 9b058f45 2022-06-30 mark {
8044 9b058f45 2022-06-30 mark switch (view->type) {
8045 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8046 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8047 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8048 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8049 9b058f45 2022-06-30 mark 1);
8050 9b058f45 2022-06-30 mark break;
8051 9b058f45 2022-06-30 mark }
8052 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8053 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8054 9b058f45 2022-06-30 mark else
8055 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8056 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8057 9b058f45 2022-06-30 mark break;
8058 9b058f45 2022-06-30 mark }
8059 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8060 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8061 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8062 9b058f45 2022-06-30 mark break;
8063 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8064 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8065 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8066 9b058f45 2022-06-30 mark break;
8067 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8068 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8069 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8070 9b058f45 2022-06-30 mark break;
8071 9b058f45 2022-06-30 mark default:
8072 9b058f45 2022-06-30 mark break;
8073 9b058f45 2022-06-30 mark }
8074 9b058f45 2022-06-30 mark
8075 9b058f45 2022-06-30 mark view->offset = 0;
8076 9b058f45 2022-06-30 mark }
8077 9b058f45 2022-06-30 mark
8078 9b058f45 2022-06-30 mark /*
8079 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8080 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8081 9b058f45 2022-06-30 mark */
8082 9b058f45 2022-06-30 mark static const struct got_error *
8083 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8084 9b058f45 2022-06-30 mark {
8085 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8086 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8087 9b058f45 2022-06-30 mark int *selected = NULL;
8088 9b058f45 2022-06-30 mark int header, offset;
8089 9b058f45 2022-06-30 mark
8090 9b058f45 2022-06-30 mark switch (view->type) {
8091 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8092 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8093 9b058f45 2022-06-30 mark header = 3;
8094 9b058f45 2022-06-30 mark scrolld = NULL;
8095 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8096 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8097 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8098 9b058f45 2022-06-30 mark s->selected_line -= offset;
8099 9b058f45 2022-06-30 mark view->offset = offset;
8100 9b058f45 2022-06-30 mark }
8101 9b058f45 2022-06-30 mark break;
8102 9b058f45 2022-06-30 mark }
8103 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8104 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8105 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8106 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8107 9b058f45 2022-06-30 mark selected = &s->selected;
8108 9b058f45 2022-06-30 mark break;
8109 9b058f45 2022-06-30 mark }
8110 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8111 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8112 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8113 9b058f45 2022-06-30 mark header = 3;
8114 9b058f45 2022-06-30 mark selected = &s->selected;
8115 9b058f45 2022-06-30 mark break;
8116 9b058f45 2022-06-30 mark }
8117 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8118 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8119 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8120 9b058f45 2022-06-30 mark header = 5;
8121 9b058f45 2022-06-30 mark selected = &s->selected;
8122 9b058f45 2022-06-30 mark break;
8123 9b058f45 2022-06-30 mark }
8124 9b058f45 2022-06-30 mark default:
8125 9b058f45 2022-06-30 mark selected = NULL;
8126 9b058f45 2022-06-30 mark scrolld = NULL;
8127 9b058f45 2022-06-30 mark header = 0;
8128 9b058f45 2022-06-30 mark break;
8129 9b058f45 2022-06-30 mark }
8130 9b058f45 2022-06-30 mark
8131 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8132 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8133 9b058f45 2022-06-30 mark view->offset = offset;
8134 9b058f45 2022-06-30 mark if (scrolld && offset) {
8135 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8136 9b058f45 2022-06-30 mark *selected -= offset;
8137 9b058f45 2022-06-30 mark }
8138 9b058f45 2022-06-30 mark }
8139 9b058f45 2022-06-30 mark
8140 9b058f45 2022-06-30 mark return err;
8141 9b058f45 2022-06-30 mark }
8142 9b058f45 2022-06-30 mark
8143 9b058f45 2022-06-30 mark static void
8144 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8145 ce5b7c56 2019-07-09 stsp {
8146 6059809a 2020-12-17 stsp size_t i;
8147 9f7d7167 2018-04-29 stsp
8148 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8149 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8150 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8151 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8152 ce5b7c56 2019-07-09 stsp }
8153 6879ba42 2020-10-01 naddy fputc('\n', fp);
8154 ce5b7c56 2019-07-09 stsp }
8155 ce5b7c56 2019-07-09 stsp
8156 4ed7e80c 2018-05-20 stsp __dead static void
8157 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8158 9f7d7167 2018-04-29 stsp {
8159 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8160 6879ba42 2020-10-01 naddy
8161 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8162 6879ba42 2020-10-01 naddy getprogname());
8163 6879ba42 2020-10-01 naddy if (hflag) {
8164 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8165 6879ba42 2020-10-01 naddy list_commands(fp);
8166 ee85c5e8 2020-02-29 stsp }
8167 6879ba42 2020-10-01 naddy exit(status);
8168 9f7d7167 2018-04-29 stsp }
8169 9f7d7167 2018-04-29 stsp
8170 c2301be8 2018-04-30 stsp static char **
8171 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8172 c2301be8 2018-04-30 stsp {
8173 ee85c5e8 2020-02-29 stsp va_list ap;
8174 c2301be8 2018-04-30 stsp char **argv;
8175 ee85c5e8 2020-02-29 stsp int i;
8176 c2301be8 2018-04-30 stsp
8177 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8178 ee85c5e8 2020-02-29 stsp
8179 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8180 c2301be8 2018-04-30 stsp if (argv == NULL)
8181 c2301be8 2018-04-30 stsp err(1, "calloc");
8182 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8183 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8184 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8185 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8186 c2301be8 2018-04-30 stsp }
8187 c2301be8 2018-04-30 stsp
8188 ee85c5e8 2020-02-29 stsp va_end(ap);
8189 c2301be8 2018-04-30 stsp return argv;
8190 ee85c5e8 2020-02-29 stsp }
8191 ee85c5e8 2020-02-29 stsp
8192 ee85c5e8 2020-02-29 stsp /*
8193 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8194 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8195 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8196 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8197 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8198 ee85c5e8 2020-02-29 stsp */
8199 ee85c5e8 2020-02-29 stsp static const struct got_error *
8200 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8201 ee85c5e8 2020-02-29 stsp {
8202 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8203 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8204 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8205 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8206 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8207 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8208 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8209 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8210 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8211 84de9106 2020-12-26 stsp
8212 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8213 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8214 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8215 ee85c5e8 2020-02-29 stsp
8216 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8217 0ae84acc 2022-06-15 tracey if (error != NULL)
8218 0ae84acc 2022-06-15 tracey goto done;
8219 0ae84acc 2022-06-15 tracey
8220 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8221 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8222 ee85c5e8 2020-02-29 stsp goto done;
8223 ee85c5e8 2020-02-29 stsp
8224 ee85c5e8 2020-02-29 stsp if (worktree)
8225 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8226 ee85c5e8 2020-02-29 stsp else
8227 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8228 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8229 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8230 ee85c5e8 2020-02-29 stsp goto done;
8231 ee85c5e8 2020-02-29 stsp }
8232 ee85c5e8 2020-02-29 stsp
8233 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8234 ee85c5e8 2020-02-29 stsp if (error != NULL)
8235 ee85c5e8 2020-02-29 stsp goto done;
8236 ee85c5e8 2020-02-29 stsp
8237 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8238 ee85c5e8 2020-02-29 stsp repo, worktree);
8239 ee85c5e8 2020-02-29 stsp if (error)
8240 ee85c5e8 2020-02-29 stsp goto done;
8241 ee85c5e8 2020-02-29 stsp
8242 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8243 84de9106 2020-12-26 stsp if (error)
8244 84de9106 2020-12-26 stsp goto done;
8245 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8246 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8247 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8248 ee85c5e8 2020-02-29 stsp if (error)
8249 ee85c5e8 2020-02-29 stsp goto done;
8250 ee85c5e8 2020-02-29 stsp
8251 ee85c5e8 2020-02-29 stsp if (worktree) {
8252 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8253 ee85c5e8 2020-02-29 stsp worktree = NULL;
8254 ee85c5e8 2020-02-29 stsp }
8255 ee85c5e8 2020-02-29 stsp
8256 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8257 a44927cc 2022-04-07 stsp if (error)
8258 a44927cc 2022-04-07 stsp goto done;
8259 a44927cc 2022-04-07 stsp
8260 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8261 ee85c5e8 2020-02-29 stsp if (error) {
8262 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8263 ee85c5e8 2020-02-29 stsp goto done;
8264 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8265 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8266 6879ba42 2020-10-01 naddy usage(1, 1);
8267 ee85c5e8 2020-02-29 stsp /* not reached */
8268 ee85c5e8 2020-02-29 stsp }
8269 ee85c5e8 2020-02-29 stsp
8270 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8271 1d0f4054 2021-06-17 stsp if (error == NULL)
8272 1d0f4054 2021-06-17 stsp error = close_err;
8273 ee85c5e8 2020-02-29 stsp repo = NULL;
8274 ee85c5e8 2020-02-29 stsp
8275 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8276 ee85c5e8 2020-02-29 stsp if (error)
8277 ee85c5e8 2020-02-29 stsp goto done;
8278 ee85c5e8 2020-02-29 stsp
8279 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8280 ee85c5e8 2020-02-29 stsp argc = 4;
8281 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8282 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8283 ee85c5e8 2020-02-29 stsp done:
8284 1d0f4054 2021-06-17 stsp if (repo) {
8285 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8286 1d0f4054 2021-06-17 stsp if (error == NULL)
8287 1d0f4054 2021-06-17 stsp error = close_err;
8288 1d0f4054 2021-06-17 stsp }
8289 a44927cc 2022-04-07 stsp if (commit)
8290 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8291 ee85c5e8 2020-02-29 stsp if (worktree)
8292 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8293 0ae84acc 2022-06-15 tracey if (pack_fds) {
8294 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8295 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8296 0ae84acc 2022-06-15 tracey if (error == NULL)
8297 0ae84acc 2022-06-15 tracey error = pack_err;
8298 0ae84acc 2022-06-15 tracey }
8299 ee85c5e8 2020-02-29 stsp free(id);
8300 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8301 ee85c5e8 2020-02-29 stsp free(commit_id);
8302 ee85c5e8 2020-02-29 stsp free(cwd);
8303 ee85c5e8 2020-02-29 stsp free(repo_path);
8304 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8305 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8306 ee85c5e8 2020-02-29 stsp int i;
8307 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8308 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8309 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8310 ee85c5e8 2020-02-29 stsp }
8311 87670572 2020-12-26 naddy tog_free_refs();
8312 ee85c5e8 2020-02-29 stsp return error;
8313 c2301be8 2018-04-30 stsp }
8314 c2301be8 2018-04-30 stsp
8315 9f7d7167 2018-04-29 stsp int
8316 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8317 9f7d7167 2018-04-29 stsp {
8318 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8319 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8320 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8321 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8322 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8323 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8324 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8325 83cd27f8 2020-01-13 stsp };
8326 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8327 9f7d7167 2018-04-29 stsp
8328 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8329 9f7d7167 2018-04-29 stsp
8330 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8331 9f7d7167 2018-04-29 stsp switch (ch) {
8332 9f7d7167 2018-04-29 stsp case 'h':
8333 9f7d7167 2018-04-29 stsp hflag = 1;
8334 9f7d7167 2018-04-29 stsp break;
8335 53ccebc2 2019-07-30 stsp case 'V':
8336 53ccebc2 2019-07-30 stsp Vflag = 1;
8337 53ccebc2 2019-07-30 stsp break;
8338 9f7d7167 2018-04-29 stsp default:
8339 6879ba42 2020-10-01 naddy usage(hflag, 1);
8340 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8341 9f7d7167 2018-04-29 stsp }
8342 9f7d7167 2018-04-29 stsp }
8343 9f7d7167 2018-04-29 stsp
8344 9f7d7167 2018-04-29 stsp argc -= optind;
8345 9f7d7167 2018-04-29 stsp argv += optind;
8346 9814e6a3 2020-09-27 naddy optind = 1;
8347 c2301be8 2018-04-30 stsp optreset = 1;
8348 9f7d7167 2018-04-29 stsp
8349 53ccebc2 2019-07-30 stsp if (Vflag) {
8350 53ccebc2 2019-07-30 stsp got_version_print_str();
8351 6879ba42 2020-10-01 naddy return 0;
8352 53ccebc2 2019-07-30 stsp }
8353 4010e238 2020-12-04 stsp
8354 4010e238 2020-12-04 stsp #ifndef PROFILE
8355 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8356 4010e238 2020-12-04 stsp NULL) == -1)
8357 4010e238 2020-12-04 stsp err(1, "pledge");
8358 4010e238 2020-12-04 stsp #endif
8359 53ccebc2 2019-07-30 stsp
8360 c2301be8 2018-04-30 stsp if (argc == 0) {
8361 f29d3e89 2018-06-23 stsp if (hflag)
8362 6879ba42 2020-10-01 naddy usage(hflag, 0);
8363 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8364 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8365 c2301be8 2018-04-30 stsp argc = 1;
8366 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8367 c2301be8 2018-04-30 stsp } else {
8368 6059809a 2020-12-17 stsp size_t i;
8369 9f7d7167 2018-04-29 stsp
8370 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8371 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8372 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8373 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8374 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8375 9f7d7167 2018-04-29 stsp break;
8376 9f7d7167 2018-04-29 stsp }
8377 9f7d7167 2018-04-29 stsp }
8378 ee85c5e8 2020-02-29 stsp }
8379 3642c4c6 2019-07-09 stsp
8380 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8381 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8382 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8383 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8384 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8385 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8386 917d79a7 2022-07-01 stsp }
8387 917d79a7 2022-07-01 stsp
8388 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8389 ee85c5e8 2020-02-29 stsp if (argc != 1)
8390 6879ba42 2020-10-01 naddy usage(0, 1);
8391 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8392 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8393 ee85c5e8 2020-02-29 stsp } else {
8394 ee85c5e8 2020-02-29 stsp if (hflag)
8395 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8396 ee85c5e8 2020-02-29 stsp else
8397 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8398 9f7d7167 2018-04-29 stsp }
8399 9f7d7167 2018-04-29 stsp
8400 9f7d7167 2018-04-29 stsp endwin();
8401 b46c1e04 2020-09-20 naddy putchar('\n');
8402 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8403 a2f4a359 2020-02-28 stsp int i;
8404 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8405 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8406 a2f4a359 2020-02-28 stsp free(cmd_argv);
8407 a2f4a359 2020-02-28 stsp }
8408 a2f4a359 2020-02-28 stsp
8409 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8410 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8411 9f7d7167 2018-04-29 stsp return 0;
8412 9f7d7167 2018-04-29 stsp }