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 ba4f502b 2018-08-04 stsp };
380 11b20872 2019-11-08 stsp
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
389 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
390 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
391 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
395 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
396 ba4f502b 2018-08-04 stsp
397 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
398 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
399 e9424729 2018-08-04 stsp int nlines;
400 e9424729 2018-08-04 stsp
401 e9424729 2018-08-04 stsp struct tog_view *view;
402 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
403 e9424729 2018-08-04 stsp int *quit;
404 e9424729 2018-08-04 stsp };
405 e9424729 2018-08-04 stsp
406 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
407 e9424729 2018-08-04 stsp const char *path;
408 e9424729 2018-08-04 stsp struct got_repository *repo;
409 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
410 e9424729 2018-08-04 stsp int *complete;
411 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
412 fc06ba56 2019-08-22 stsp void *cancel_arg;
413 e9424729 2018-08-04 stsp };
414 e9424729 2018-08-04 stsp
415 e9424729 2018-08-04 stsp struct tog_blame {
416 e9424729 2018-08-04 stsp FILE *f;
417 be659d10 2020-11-18 stsp off_t filesize;
418 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
419 6fcac457 2018-11-19 stsp int nlines;
420 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
421 e9424729 2018-08-04 stsp pthread_t thread;
422 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
424 e9424729 2018-08-04 stsp const char *path;
425 0ae84acc 2022-06-15 tracey int *pack_fds;
426 e9424729 2018-08-04 stsp };
427 e9424729 2018-08-04 stsp
428 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
429 7cbe629d 2018-08-04 stsp int first_displayed_line;
430 7cbe629d 2018-08-04 stsp int last_displayed_line;
431 7cbe629d 2018-08-04 stsp int selected_line;
432 c0f61fa4 2022-07-11 mark int last_diffed_line;
433 7cbe629d 2018-08-04 stsp int blame_complete;
434 e5a0f69f 2018-08-18 stsp int eof;
435 e5a0f69f 2018-08-18 stsp int done;
436 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
437 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
438 e5a0f69f 2018-08-18 stsp char *path;
439 7cbe629d 2018-08-04 stsp struct got_repository *repo;
440 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
441 e9424729 2018-08-04 stsp struct tog_blame blame;
442 6c4c42e0 2019-06-24 stsp int matched_line;
443 11b20872 2019-11-08 stsp struct tog_colors colors;
444 ad80ab7b 2018-08-04 stsp };
445 ad80ab7b 2018-08-04 stsp
446 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
447 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
448 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
451 ad80ab7b 2018-08-04 stsp int selected;
452 ad80ab7b 2018-08-04 stsp };
453 ad80ab7b 2018-08-04 stsp
454 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
455 ad80ab7b 2018-08-04 stsp
456 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
457 ad80ab7b 2018-08-04 stsp char *tree_label;
458 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
459 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
460 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
461 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
462 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
464 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
465 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
466 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
467 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
468 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
469 6458efa5 2020-11-24 stsp struct tog_colors colors;
470 6458efa5 2020-11-24 stsp };
471 6458efa5 2020-11-24 stsp
472 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
473 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
474 6458efa5 2020-11-24 stsp struct got_reference *ref;
475 6458efa5 2020-11-24 stsp int idx;
476 6458efa5 2020-11-24 stsp };
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
479 6458efa5 2020-11-24 stsp
480 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
481 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
483 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
485 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
486 6458efa5 2020-11-24 stsp struct got_repository *repo;
487 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
488 bddb1296 2019-11-08 stsp struct tog_colors colors;
489 7cbe629d 2018-08-04 stsp };
490 7cbe629d 2018-08-04 stsp
491 669b5ffa 2018-10-07 stsp /*
492 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
493 669b5ffa 2018-10-07 stsp *
494 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
495 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
496 669b5ffa 2018-10-07 stsp * there is enough screen estate.
497 669b5ffa 2018-10-07 stsp *
498 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
499 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
500 669b5ffa 2018-10-07 stsp *
501 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
502 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
503 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
504 669b5ffa 2018-10-07 stsp *
505 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
506 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
507 669b5ffa 2018-10-07 stsp */
508 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
509 669b5ffa 2018-10-07 stsp
510 cc3c9aac 2018-08-01 stsp struct tog_view {
511 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
512 26ed57b2 2018-05-19 stsp WINDOW *window;
513 26ed57b2 2018-05-19 stsp PANEL *panel;
514 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
515 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
516 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
517 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
518 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
519 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
520 3c1dfe12 2022-07-08 mark int resize; /* set when in a resize event */
521 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
522 9970f7fc 2020-12-03 stsp int dying;
523 669b5ffa 2018-10-07 stsp struct tog_view *parent;
524 669b5ffa 2018-10-07 stsp struct tog_view *child;
525 5dc9f4bc 2018-08-04 stsp
526 e78dc838 2020-12-04 stsp /*
527 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
528 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
529 e78dc838 2020-12-04 stsp * between parent and child.
530 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
531 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
532 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
533 e78dc838 2020-12-04 stsp * situations.
534 e78dc838 2020-12-04 stsp */
535 e78dc838 2020-12-04 stsp int focus_child;
536 e78dc838 2020-12-04 stsp
537 9b058f45 2022-06-30 mark enum tog_view_mode mode;
538 5dc9f4bc 2018-08-04 stsp /* type-specific state */
539 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
540 5dc9f4bc 2018-08-04 stsp union {
541 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
542 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
543 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
544 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
545 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
546 5dc9f4bc 2018-08-04 stsp } state;
547 e5a0f69f 2018-08-18 stsp
548 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
549 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
550 e78dc838 2020-12-04 stsp struct tog_view *, int);
551 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
553 60493ae3 2019-06-20 stsp
554 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
555 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
556 c0c4acc8 2021-01-24 stsp int search_started;
557 60493ae3 2019-06-20 stsp int searching;
558 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
559 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
560 60493ae3 2019-06-20 stsp int search_next_done;
561 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
562 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
563 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
564 1803e47f 2019-06-22 stsp regex_t regex;
565 41605754 2020-11-12 stsp regmatch_t regmatch;
566 cc3c9aac 2018-08-01 stsp };
567 cd0acaa7 2018-05-20 stsp
568 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
569 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
570 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
571 78756c87 2020-11-24 stsp struct got_repository *);
572 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
574 e78dc838 2020-12-04 stsp struct tog_view *, int);
575 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
577 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
578 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp
580 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
581 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
582 78756c87 2020-11-24 stsp const char *, const char *, int);
583 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
585 e78dc838 2020-12-04 stsp struct tog_view *, int);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
587 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
588 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp
590 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
591 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
592 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
594 e78dc838 2020-12-04 stsp struct tog_view *, int);
595 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
596 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
597 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
598 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp
600 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
601 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
602 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
603 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
604 e78dc838 2020-12-04 stsp struct tog_view *, int);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
606 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
607 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp
609 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
610 6458efa5 2020-11-24 stsp struct got_repository *);
611 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
615 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
617 25791caa 2018-10-24 stsp
618 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
619 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
620 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
621 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
622 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
623 25791caa 2018-10-24 stsp
624 25791caa 2018-10-24 stsp static void
625 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
626 25791caa 2018-10-24 stsp {
627 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
628 83baff54 2019-08-12 stsp }
629 83baff54 2019-08-12 stsp
630 83baff54 2019-08-12 stsp static void
631 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
632 83baff54 2019-08-12 stsp {
633 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
634 25791caa 2018-10-24 stsp }
635 26ed57b2 2018-05-19 stsp
636 61266923 2020-01-14 stsp static void
637 61266923 2020-01-14 stsp tog_sigcont(int signo)
638 61266923 2020-01-14 stsp {
639 61266923 2020-01-14 stsp tog_sigcont_received = 1;
640 61266923 2020-01-14 stsp }
641 61266923 2020-01-14 stsp
642 2497f032 2022-05-31 stsp static void
643 2497f032 2022-05-31 stsp tog_sigint(int signo)
644 2497f032 2022-05-31 stsp {
645 2497f032 2022-05-31 stsp tog_sigint_received = 1;
646 2497f032 2022-05-31 stsp }
647 2497f032 2022-05-31 stsp
648 2497f032 2022-05-31 stsp static void
649 2497f032 2022-05-31 stsp tog_sigterm(int signo)
650 2497f032 2022-05-31 stsp {
651 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
652 2497f032 2022-05-31 stsp }
653 2497f032 2022-05-31 stsp
654 2497f032 2022-05-31 stsp static int
655 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
656 2497f032 2022-05-31 stsp {
657 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
658 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
659 2497f032 2022-05-31 stsp }
660 2497f032 2022-05-31 stsp
661 e5a0f69f 2018-08-18 stsp static const struct got_error *
662 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
663 ea5e7bb5 2018-08-01 stsp {
664 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
665 e5a0f69f 2018-08-18 stsp
666 669b5ffa 2018-10-07 stsp if (view->child) {
667 5629093a 2022-07-11 stsp child_err = view_close(view->child);
668 669b5ffa 2018-10-07 stsp view->child = NULL;
669 669b5ffa 2018-10-07 stsp }
670 e5a0f69f 2018-08-18 stsp if (view->close)
671 e5a0f69f 2018-08-18 stsp err = view->close(view);
672 ea5e7bb5 2018-08-01 stsp if (view->panel)
673 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
674 ea5e7bb5 2018-08-01 stsp if (view->window)
675 ea5e7bb5 2018-08-01 stsp delwin(view->window);
676 ea5e7bb5 2018-08-01 stsp free(view);
677 5629093a 2022-07-11 stsp return err ? err : child_err;
678 ea5e7bb5 2018-08-01 stsp }
679 ea5e7bb5 2018-08-01 stsp
680 ea5e7bb5 2018-08-01 stsp static struct tog_view *
681 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
682 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
683 ea5e7bb5 2018-08-01 stsp {
684 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
685 ea5e7bb5 2018-08-01 stsp
686 ea5e7bb5 2018-08-01 stsp if (view == NULL)
687 ea5e7bb5 2018-08-01 stsp return NULL;
688 ea5e7bb5 2018-08-01 stsp
689 d6b05b5b 2018-08-04 stsp view->type = type;
690 f7d12f7e 2018-08-01 stsp view->lines = LINES;
691 f7d12f7e 2018-08-01 stsp view->cols = COLS;
692 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
693 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
694 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
695 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
696 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
697 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
698 96a765a8 2018-08-04 stsp view_close(view);
699 ea5e7bb5 2018-08-01 stsp return NULL;
700 ea5e7bb5 2018-08-01 stsp }
701 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
702 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
703 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
704 96a765a8 2018-08-04 stsp view_close(view);
705 ea5e7bb5 2018-08-01 stsp return NULL;
706 ea5e7bb5 2018-08-01 stsp }
707 ea5e7bb5 2018-08-01 stsp
708 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
709 ea5e7bb5 2018-08-01 stsp return view;
710 cdf1ee82 2018-08-01 stsp }
711 cdf1ee82 2018-08-01 stsp
712 0cf4efb1 2018-09-29 stsp static int
713 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
714 0cf4efb1 2018-09-29 stsp {
715 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
716 0cf4efb1 2018-09-29 stsp return 0;
717 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
718 5c60c32a 2018-10-18 stsp }
719 5c60c32a 2018-10-18 stsp
720 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
721 9b058f45 2022-06-30 mark static int
722 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
723 9b058f45 2022-06-30 mark {
724 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
725 9b058f45 2022-06-30 mark }
726 9b058f45 2022-06-30 mark
727 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
728 5c60c32a 2018-10-18 stsp
729 5c60c32a 2018-10-18 stsp static const struct got_error *
730 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
731 5c60c32a 2018-10-18 stsp {
732 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
733 5c60c32a 2018-10-18 stsp
734 3c1dfe12 2022-07-08 mark if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
735 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
736 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
737 3c1dfe12 2022-07-08 mark else
738 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
739 9b058f45 2022-06-30 mark view->begin_x = 0;
740 3c1dfe12 2022-07-08 mark } else if (!view->resize) {
741 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
742 3c1dfe12 2022-07-08 mark view->cols > 119)
743 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
744 3c1dfe12 2022-07-08 mark else
745 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
746 9b058f45 2022-06-30 mark view->begin_y = 0;
747 9b058f45 2022-06-30 mark }
748 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
749 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
750 5c60c32a 2018-10-18 stsp view->lines = LINES;
751 5c60c32a 2018-10-18 stsp view->cols = COLS;
752 5c60c32a 2018-10-18 stsp err = view_resize(view);
753 5c60c32a 2018-10-18 stsp if (err)
754 5c60c32a 2018-10-18 stsp return err;
755 5c60c32a 2018-10-18 stsp
756 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
757 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
758 9b058f45 2022-06-30 mark
759 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
760 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
761 5c60c32a 2018-10-18 stsp
762 5c60c32a 2018-10-18 stsp return NULL;
763 5c60c32a 2018-10-18 stsp }
764 5c60c32a 2018-10-18 stsp
765 5c60c32a 2018-10-18 stsp static const struct got_error *
766 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
767 5c60c32a 2018-10-18 stsp {
768 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
769 5c60c32a 2018-10-18 stsp
770 5c60c32a 2018-10-18 stsp view->begin_x = 0;
771 3c1dfe12 2022-07-08 mark view->begin_y = view->resize ? view->begin_y : 0;
772 3c1dfe12 2022-07-08 mark view->nlines = view->resize ? view->nlines : LINES;
773 5c60c32a 2018-10-18 stsp view->ncols = COLS;
774 5c60c32a 2018-10-18 stsp view->lines = LINES;
775 5c60c32a 2018-10-18 stsp view->cols = COLS;
776 5c60c32a 2018-10-18 stsp err = view_resize(view);
777 5c60c32a 2018-10-18 stsp if (err)
778 5c60c32a 2018-10-18 stsp return err;
779 5c60c32a 2018-10-18 stsp
780 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
781 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
782 5c60c32a 2018-10-18 stsp
783 5c60c32a 2018-10-18 stsp return NULL;
784 0cf4efb1 2018-09-29 stsp }
785 0cf4efb1 2018-09-29 stsp
786 5c60c32a 2018-10-18 stsp static int
787 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
788 5c60c32a 2018-10-18 stsp {
789 5c60c32a 2018-10-18 stsp return view->parent == NULL;
790 5c60c32a 2018-10-18 stsp }
791 5c60c32a 2018-10-18 stsp
792 6131ff18 2022-06-20 mark static int
793 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
794 6131ff18 2022-06-20 mark {
795 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
796 24b9cfdc 2022-06-30 stsp }
797 24b9cfdc 2022-06-30 stsp
798 24b9cfdc 2022-06-30 stsp static int
799 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
800 24b9cfdc 2022-06-30 stsp {
801 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
802 6131ff18 2022-06-20 mark }
803 6131ff18 2022-06-20 mark
804 49b24ee5 2022-07-03 mark static int
805 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
806 49b24ee5 2022-07-03 mark {
807 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
808 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
809 49b24ee5 2022-07-03 mark }
810 49b24ee5 2022-07-03 mark
811 9b058f45 2022-06-30 mark static void
812 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
813 9b058f45 2022-06-30 mark {
814 9b058f45 2022-06-30 mark PANEL *panel;
815 9b058f45 2022-06-30 mark const struct tog_view *view_above;
816 6131ff18 2022-06-20 mark
817 9b058f45 2022-06-30 mark if (view->parent)
818 9b058f45 2022-06-30 mark return view_border(view->parent);
819 9b058f45 2022-06-30 mark
820 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
821 9b058f45 2022-06-30 mark if (panel == NULL)
822 9b058f45 2022-06-30 mark return;
823 9b058f45 2022-06-30 mark
824 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
825 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
826 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
827 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
828 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
829 9b058f45 2022-06-30 mark else
830 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
831 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
832 9b058f45 2022-06-30 mark }
833 9b058f45 2022-06-30 mark
834 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
835 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
836 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
837 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
838 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
839 9b058f45 2022-06-30 mark
840 4d8c2215 2018-08-19 stsp static const struct got_error *
841 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
842 f7d12f7e 2018-08-01 stsp {
843 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
844 9b058f45 2022-06-30 mark int dif, nlines, ncols;
845 f7d12f7e 2018-08-01 stsp
846 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
847 9b058f45 2022-06-30 mark
848 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
849 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
850 0cf4efb1 2018-09-29 stsp else
851 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
852 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
853 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
854 0cf4efb1 2018-09-29 stsp else
855 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
856 6d0fee91 2018-08-01 stsp
857 4dd27a72 2022-06-29 stsp if (view->child) {
858 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
859 9b058f45 2022-06-30 mark
860 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
861 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
862 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
863 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
864 0dbbbe90 2022-06-17 op ncols = COLS;
865 0dbbbe90 2022-06-17 op
866 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
867 5c60c32a 2018-10-18 stsp if (view->child->focussed)
868 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
869 5c60c32a 2018-10-18 stsp else
870 5c60c32a 2018-10-18 stsp show_panel(view->panel);
871 5c60c32a 2018-10-18 stsp } else {
872 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
873 0dbbbe90 2022-06-17 op
874 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
875 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
876 5c60c32a 2018-10-18 stsp }
877 9b058f45 2022-06-30 mark /*
878 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
879 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
880 9b058f45 2022-06-30 mark */
881 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
882 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
883 9b058f45 2022-06-30 mark
884 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
885 9b058f45 2022-06-30 mark view->lines - ts->selected) {
886 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
887 9b058f45 2022-06-30 mark view->lines - ts->selected -
888 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
889 9b058f45 2022-06-30 mark err = request_log_commits(view);
890 9b058f45 2022-06-30 mark if (err)
891 9b058f45 2022-06-30 mark return err;
892 9b058f45 2022-06-30 mark }
893 9b058f45 2022-06-30 mark }
894 9b058f45 2022-06-30 mark
895 9b058f45 2022-06-30 mark /*
896 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
897 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
898 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
899 9b058f45 2022-06-30 mark */
900 9b058f45 2022-06-30 mark if (hs) {
901 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
902 9b058f45 2022-06-30 mark if (err)
903 9b058f45 2022-06-30 mark return err;
904 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
905 9b058f45 2022-06-30 mark err = offset_selection_down(view);
906 9b058f45 2022-06-30 mark if (err)
907 9b058f45 2022-06-30 mark return err;
908 9b058f45 2022-06-30 mark }
909 9b058f45 2022-06-30 mark view_border(view);
910 9b058f45 2022-06-30 mark update_panels();
911 9b058f45 2022-06-30 mark doupdate();
912 9b058f45 2022-06-30 mark show_panel(view->child->panel);
913 9b058f45 2022-06-30 mark nlines = view->nlines;
914 9b058f45 2022-06-30 mark }
915 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
916 0dbbbe90 2022-06-17 op ncols = COLS;
917 669b5ffa 2018-10-07 stsp
918 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
919 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
920 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
921 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
922 0dbbbe90 2022-06-17 op wclear(view->window);
923 0dbbbe90 2022-06-17 op
924 0dbbbe90 2022-06-17 op view->nlines = nlines;
925 0dbbbe90 2022-06-17 op view->ncols = ncols;
926 0dbbbe90 2022-06-17 op view->lines = LINES;
927 0dbbbe90 2022-06-17 op view->cols = COLS;
928 0dbbbe90 2022-06-17 op
929 5c60c32a 2018-10-18 stsp return NULL;
930 d9a7ab53 2022-07-11 mark }
931 d9a7ab53 2022-07-11 mark
932 d9a7ab53 2022-07-11 mark static void
933 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
934 d9a7ab53 2022-07-11 mark {
935 d9a7ab53 2022-07-11 mark if (n == 0)
936 d9a7ab53 2022-07-11 mark return;
937 d9a7ab53 2022-07-11 mark
938 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
939 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
940 d9a7ab53 2022-07-11 mark view->parent->offset += n;
941 d9a7ab53 2022-07-11 mark else
942 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
943 d9a7ab53 2022-07-11 mark } else if (view->offset) {
944 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
945 d9a7ab53 2022-07-11 mark view->offset -= n;
946 d9a7ab53 2022-07-11 mark else
947 d9a7ab53 2022-07-11 mark view->offset = 0;
948 d9a7ab53 2022-07-11 mark }
949 3c1dfe12 2022-07-08 mark }
950 3c1dfe12 2022-07-08 mark
951 3c1dfe12 2022-07-08 mark static const struct got_error *
952 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
953 3c1dfe12 2022-07-08 mark {
954 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
955 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
956 3c1dfe12 2022-07-08 mark
957 3c1dfe12 2022-07-08 mark if (view->parent)
958 3c1dfe12 2022-07-08 mark v = view->parent;
959 3c1dfe12 2022-07-08 mark else
960 3c1dfe12 2022-07-08 mark v = view;
961 3c1dfe12 2022-07-08 mark
962 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
963 3c1dfe12 2022-07-08 mark return NULL;
964 3c1dfe12 2022-07-08 mark
965 3c1dfe12 2022-07-08 mark v->resize = v->child->resize = resize; /* lock for resize event */
966 3c1dfe12 2022-07-08 mark
967 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
968 27187d45 2022-07-11 mark int y = v->child->begin_y;
969 27187d45 2022-07-11 mark
970 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
971 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
972 3c1dfe12 2022-07-08 mark if (view->parent)
973 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
974 3c1dfe12 2022-07-08 mark else
975 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
976 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
977 3c1dfe12 2022-07-08 mark view->count = 0;
978 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
979 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
980 3c1dfe12 2022-07-08 mark view->count = 0;
981 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
982 3c1dfe12 2022-07-08 mark }
983 3c1dfe12 2022-07-08 mark v->ncols = COLS;
984 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
985 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
986 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
987 3c1dfe12 2022-07-08 mark if (err)
988 3c1dfe12 2022-07-08 mark return err;
989 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
990 27187d45 2022-07-11 mark if (y > v->child->begin_y) /* split increased */
991 27187d45 2022-07-11 mark v->child->nscrolled = y - v->child->begin_y;
992 3c1dfe12 2022-07-08 mark } else {
993 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
994 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
995 3c1dfe12 2022-07-08 mark if (view->parent)
996 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
997 3c1dfe12 2022-07-08 mark else
998 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
999 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1000 3c1dfe12 2022-07-08 mark view->count = 0;
1001 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1002 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1003 3c1dfe12 2022-07-08 mark view->count = 0;
1004 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1005 3c1dfe12 2022-07-08 mark }
1006 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1007 3c1dfe12 2022-07-08 mark }
1008 3c1dfe12 2022-07-08 mark
1009 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1010 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1011 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1012 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1013 3c1dfe12 2022-07-08 mark
1014 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1015 3c1dfe12 2022-07-08 mark if (err)
1016 3c1dfe12 2022-07-08 mark return err;
1017 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1018 3c1dfe12 2022-07-08 mark if (err)
1019 3c1dfe12 2022-07-08 mark return err;
1020 3c1dfe12 2022-07-08 mark
1021 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1022 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1023 3c1dfe12 2022-07-08 mark if (err)
1024 3c1dfe12 2022-07-08 mark return err;
1025 3c1dfe12 2022-07-08 mark }
1026 3c1dfe12 2022-07-08 mark
1027 27187d45 2022-07-11 mark if (v->type == TOG_VIEW_LOG && v->nscrolled)
1028 3c1dfe12 2022-07-08 mark err = request_log_commits(v);
1029 27187d45 2022-07-11 mark else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1030 3c1dfe12 2022-07-08 mark err = request_log_commits(v->child);
1031 3c1dfe12 2022-07-08 mark
1032 3c1dfe12 2022-07-08 mark v->resize = v->child->resize = 0;
1033 3c1dfe12 2022-07-08 mark
1034 3c1dfe12 2022-07-08 mark return err;
1035 3c1dfe12 2022-07-08 mark }
1036 3c1dfe12 2022-07-08 mark
1037 3c1dfe12 2022-07-08 mark static void
1038 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1039 3c1dfe12 2022-07-08 mark {
1040 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1041 3c1dfe12 2022-07-08 mark
1042 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1043 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1044 669b5ffa 2018-10-07 stsp }
1045 669b5ffa 2018-10-07 stsp
1046 669b5ffa 2018-10-07 stsp static const struct got_error *
1047 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1048 669b5ffa 2018-10-07 stsp {
1049 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1050 669b5ffa 2018-10-07 stsp
1051 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1052 669b5ffa 2018-10-07 stsp return NULL;
1053 669b5ffa 2018-10-07 stsp
1054 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1055 669b5ffa 2018-10-07 stsp view->child = NULL;
1056 669b5ffa 2018-10-07 stsp return err;
1057 669b5ffa 2018-10-07 stsp }
1058 669b5ffa 2018-10-07 stsp
1059 0dbbbe90 2022-06-17 op static const struct got_error *
1060 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1061 669b5ffa 2018-10-07 stsp {
1062 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1063 3c1dfe12 2022-07-08 mark
1064 669b5ffa 2018-10-07 stsp view->child = child;
1065 669b5ffa 2018-10-07 stsp child->parent = view;
1066 0dbbbe90 2022-06-17 op
1067 3c1dfe12 2022-07-08 mark err = view_resize(view);
1068 3c1dfe12 2022-07-08 mark if (err)
1069 3c1dfe12 2022-07-08 mark return err;
1070 3c1dfe12 2022-07-08 mark
1071 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1072 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1073 3c1dfe12 2022-07-08 mark
1074 3c1dfe12 2022-07-08 mark return err;
1075 bfddd0d9 2018-09-29 stsp }
1076 bfddd0d9 2018-09-29 stsp
1077 34bc9ec9 2019-02-22 stsp static void
1078 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1079 25791caa 2018-10-24 stsp {
1080 25791caa 2018-10-24 stsp int cols, lines;
1081 25791caa 2018-10-24 stsp struct winsize size;
1082 25791caa 2018-10-24 stsp
1083 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1084 25791caa 2018-10-24 stsp cols = 80; /* Default */
1085 25791caa 2018-10-24 stsp lines = 24;
1086 25791caa 2018-10-24 stsp } else {
1087 25791caa 2018-10-24 stsp cols = size.ws_col;
1088 25791caa 2018-10-24 stsp lines = size.ws_row;
1089 25791caa 2018-10-24 stsp }
1090 25791caa 2018-10-24 stsp resize_term(lines, cols);
1091 2b49a8ae 2019-06-22 stsp }
1092 2b49a8ae 2019-06-22 stsp
1093 2b49a8ae 2019-06-22 stsp static const struct got_error *
1094 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1095 2b49a8ae 2019-06-22 stsp {
1096 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1097 9b058f45 2022-06-30 mark struct tog_view *v = view;
1098 2b49a8ae 2019-06-22 stsp char pattern[1024];
1099 2b49a8ae 2019-06-22 stsp int ret;
1100 c0c4acc8 2021-01-24 stsp
1101 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1102 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1103 c0c4acc8 2021-01-24 stsp view->searching = 0;
1104 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1105 c0c4acc8 2021-01-24 stsp }
1106 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1107 2b49a8ae 2019-06-22 stsp
1108 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1109 2b49a8ae 2019-06-22 stsp return NULL;
1110 2b49a8ae 2019-06-22 stsp
1111 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1112 9b058f45 2022-06-30 mark v = view->child;
1113 2b49a8ae 2019-06-22 stsp
1114 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1115 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1116 9b058f45 2022-06-30 mark
1117 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1118 2b49a8ae 2019-06-22 stsp nocbreak();
1119 2b49a8ae 2019-06-22 stsp echo();
1120 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1121 9b058f45 2022-06-30 mark wrefresh(v->window);
1122 2b49a8ae 2019-06-22 stsp cbreak();
1123 2b49a8ae 2019-06-22 stsp noecho();
1124 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1125 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1126 2b49a8ae 2019-06-22 stsp return NULL;
1127 2b49a8ae 2019-06-22 stsp
1128 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1129 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1130 7c32bd05 2019-06-22 stsp if (err) {
1131 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1132 7c32bd05 2019-06-22 stsp return err;
1133 7c32bd05 2019-06-22 stsp }
1134 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1135 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1136 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1137 2b49a8ae 2019-06-22 stsp view->search_next(view);
1138 2b49a8ae 2019-06-22 stsp }
1139 2b49a8ae 2019-06-22 stsp
1140 2b49a8ae 2019-06-22 stsp return NULL;
1141 d2366e29 2022-07-07 mark }
1142 d2366e29 2022-07-07 mark
1143 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1144 d2366e29 2022-07-07 mark static const struct got_error *
1145 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1146 d2366e29 2022-07-07 mark {
1147 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1148 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1149 d2366e29 2022-07-07 mark
1150 d2366e29 2022-07-07 mark if (view->parent)
1151 d2366e29 2022-07-07 mark v = view->parent;
1152 d2366e29 2022-07-07 mark else
1153 d2366e29 2022-07-07 mark v = view;
1154 d2366e29 2022-07-07 mark
1155 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1156 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1157 7532ccda 2022-07-11 mark else
1158 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1159 d2366e29 2022-07-07 mark
1160 7532ccda 2022-07-11 mark if (!v->child)
1161 7532ccda 2022-07-11 mark return NULL;
1162 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1163 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1164 7532ccda 2022-07-11 mark
1165 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1166 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1167 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1168 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1169 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1170 d2366e29 2022-07-07 mark
1171 7532ccda 2022-07-11 mark
1172 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1173 d2366e29 2022-07-07 mark v->ncols = COLS;
1174 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1175 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1176 d2366e29 2022-07-07 mark
1177 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1178 d2366e29 2022-07-07 mark if (err)
1179 d2366e29 2022-07-07 mark return err;
1180 d2366e29 2022-07-07 mark }
1181 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1182 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1183 d2366e29 2022-07-07 mark v->focus_child = 1;
1184 d2366e29 2022-07-07 mark
1185 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1186 d2366e29 2022-07-07 mark if (err)
1187 d2366e29 2022-07-07 mark return err;
1188 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1189 d2366e29 2022-07-07 mark if (err)
1190 d2366e29 2022-07-07 mark return err;
1191 d2366e29 2022-07-07 mark
1192 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1193 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1194 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1195 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1196 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1197 7532ccda 2022-07-11 mark } else {
1198 7532ccda 2022-07-11 mark offset_selection_up(v);
1199 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1200 d2366e29 2022-07-07 mark }
1201 27187d45 2022-07-11 mark if (v->type == TOG_VIEW_LOG && v->nscrolled)
1202 7532ccda 2022-07-11 mark err = request_log_commits(v);
1203 27187d45 2022-07-11 mark else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1204 7532ccda 2022-07-11 mark err = request_log_commits(v->child);
1205 d2366e29 2022-07-07 mark
1206 d2366e29 2022-07-07 mark return err;
1207 0cf4efb1 2018-09-29 stsp }
1208 6d0fee91 2018-08-01 stsp
1209 640cd7ff 2022-06-22 mark /*
1210 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1211 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1212 640cd7ff 2022-06-22 mark */
1213 640cd7ff 2022-06-22 mark static int
1214 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1215 640cd7ff 2022-06-22 mark {
1216 9b058f45 2022-06-30 mark struct tog_view *v = view;
1217 9b058f45 2022-06-30 mark int x, n = 0;
1218 640cd7ff 2022-06-22 mark
1219 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1220 9b058f45 2022-06-30 mark v = view->child;
1221 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1222 9b058f45 2022-06-30 mark v = view->parent;
1223 9b058f45 2022-06-30 mark
1224 640cd7ff 2022-06-22 mark view->count = 0;
1225 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1226 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1227 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1228 9b058f45 2022-06-30 mark waddch(v->window, ':');
1229 640cd7ff 2022-06-22 mark
1230 640cd7ff 2022-06-22 mark do {
1231 9b058f45 2022-06-30 mark x = getcurx(v->window);
1232 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1233 9b058f45 2022-06-30 mark waddch(v->window, c);
1234 9b058f45 2022-06-30 mark wrefresh(v->window);
1235 9b058f45 2022-06-30 mark }
1236 9b058f45 2022-06-30 mark
1237 640cd7ff 2022-06-22 mark /*
1238 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1239 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1240 640cd7ff 2022-06-22 mark */
1241 640cd7ff 2022-06-22 mark if (n >= 9999999)
1242 640cd7ff 2022-06-22 mark n = 9999999;
1243 640cd7ff 2022-06-22 mark else
1244 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1245 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1246 640cd7ff 2022-06-22 mark
1247 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1248 640cd7ff 2022-06-22 mark view->count = n;
1249 640cd7ff 2022-06-22 mark
1250 640cd7ff 2022-06-22 mark return c;
1251 640cd7ff 2022-06-22 mark }
1252 640cd7ff 2022-06-22 mark
1253 0cf4efb1 2018-09-29 stsp static const struct got_error *
1254 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1255 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1256 e5a0f69f 2018-08-18 stsp {
1257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1258 669b5ffa 2018-10-07 stsp struct tog_view *v;
1259 1a76625f 2018-10-22 stsp int ch, errcode;
1260 e5a0f69f 2018-08-18 stsp
1261 e5a0f69f 2018-08-18 stsp *new = NULL;
1262 8f4ed634 2020-03-26 stsp
1263 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1264 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1265 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1266 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1267 640cd7ff 2022-06-22 mark view->count = 0;
1268 640cd7ff 2022-06-22 mark }
1269 e5a0f69f 2018-08-18 stsp
1270 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1271 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1272 82954512 2020-02-03 stsp if (errcode)
1273 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1274 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1275 3da8ef85 2021-09-21 stsp sched_yield();
1276 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1277 82954512 2020-02-03 stsp if (errcode)
1278 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1279 82954512 2020-02-03 stsp "pthread_mutex_lock");
1280 60493ae3 2019-06-20 stsp view->search_next(view);
1281 60493ae3 2019-06-20 stsp return NULL;
1282 60493ae3 2019-06-20 stsp }
1283 60493ae3 2019-06-20 stsp
1284 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1285 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1286 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1287 1a76625f 2018-10-22 stsp if (errcode)
1288 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1289 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1290 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1291 a6d37fac 2022-07-03 mark cbreak();
1292 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1293 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1294 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1295 a6d37fac 2022-07-03 mark view->count = 0;
1296 a6d37fac 2022-07-03 mark else
1297 a6d37fac 2022-07-03 mark ch = view->ch;
1298 a6d37fac 2022-07-03 mark } else {
1299 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1300 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1301 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1302 640cd7ff 2022-06-22 mark }
1303 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1304 1a76625f 2018-10-22 stsp if (errcode)
1305 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1306 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1307 25791caa 2018-10-24 stsp
1308 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1309 25791caa 2018-10-24 stsp tog_resizeterm();
1310 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1311 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1312 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1313 25791caa 2018-10-24 stsp err = view_resize(v);
1314 25791caa 2018-10-24 stsp if (err)
1315 25791caa 2018-10-24 stsp return err;
1316 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1317 25791caa 2018-10-24 stsp if (err)
1318 25791caa 2018-10-24 stsp return err;
1319 cdfcfb03 2020-12-06 stsp if (v->child) {
1320 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1321 cdfcfb03 2020-12-06 stsp if (err)
1322 cdfcfb03 2020-12-06 stsp return err;
1323 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1324 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1325 cdfcfb03 2020-12-06 stsp if (err)
1326 cdfcfb03 2020-12-06 stsp return err;
1327 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1328 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1329 3c1dfe12 2022-07-08 mark if (err)
1330 3c1dfe12 2022-07-08 mark return err;
1331 3c1dfe12 2022-07-08 mark }
1332 cdfcfb03 2020-12-06 stsp }
1333 25791caa 2018-10-24 stsp }
1334 25791caa 2018-10-24 stsp }
1335 25791caa 2018-10-24 stsp
1336 e5a0f69f 2018-08-18 stsp switch (ch) {
1337 1e37a5c2 2019-05-12 jcs case '\t':
1338 640cd7ff 2022-06-22 mark view->count = 0;
1339 1e37a5c2 2019-05-12 jcs if (view->child) {
1340 e78dc838 2020-12-04 stsp view->focussed = 0;
1341 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1342 e78dc838 2020-12-04 stsp view->focus_child = 1;
1343 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1344 e78dc838 2020-12-04 stsp view->focussed = 0;
1345 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1346 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1347 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1348 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1349 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1350 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1351 9b058f45 2022-06-30 mark if (err)
1352 9b058f45 2022-06-30 mark return err;
1353 9b058f45 2022-06-30 mark }
1354 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1355 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1356 9b058f45 2022-06-30 mark if (err)
1357 9b058f45 2022-06-30 mark return err;
1358 9b058f45 2022-06-30 mark }
1359 1e37a5c2 2019-05-12 jcs }
1360 1e37a5c2 2019-05-12 jcs break;
1361 1e37a5c2 2019-05-12 jcs case 'q':
1362 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1363 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1364 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1365 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1366 9b058f45 2022-06-30 mark if (err)
1367 9b058f45 2022-06-30 mark break;
1368 9b058f45 2022-06-30 mark }
1369 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1370 9b058f45 2022-06-30 mark }
1371 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1372 9970f7fc 2020-12-03 stsp view->dying = 1;
1373 1e37a5c2 2019-05-12 jcs break;
1374 1e37a5c2 2019-05-12 jcs case 'Q':
1375 1e37a5c2 2019-05-12 jcs *done = 1;
1376 1e37a5c2 2019-05-12 jcs break;
1377 61417565 2022-06-20 mark case 'F':
1378 640cd7ff 2022-06-22 mark view->count = 0;
1379 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1380 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1381 1e37a5c2 2019-05-12 jcs break;
1382 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1383 e78dc838 2020-12-04 stsp view->focussed = 0;
1384 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1385 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1386 3c1dfe12 2022-07-08 mark } else {
1387 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1388 3c1dfe12 2022-07-08 mark if (!err)
1389 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1390 3c1dfe12 2022-07-08 mark }
1391 1e37a5c2 2019-05-12 jcs if (err)
1392 1e37a5c2 2019-05-12 jcs break;
1393 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1394 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1395 1e37a5c2 2019-05-12 jcs } else {
1396 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1397 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1398 e78dc838 2020-12-04 stsp view->focussed = 1;
1399 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1400 1e37a5c2 2019-05-12 jcs } else {
1401 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1402 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1403 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1404 3c1dfe12 2022-07-08 mark if (!err)
1405 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1406 669b5ffa 2018-10-07 stsp }
1407 1e37a5c2 2019-05-12 jcs if (err)
1408 1e37a5c2 2019-05-12 jcs break;
1409 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1410 1e37a5c2 2019-05-12 jcs }
1411 9b058f45 2022-06-30 mark if (err)
1412 9b058f45 2022-06-30 mark break;
1413 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1414 9b058f45 2022-06-30 mark err = request_log_commits(view);
1415 9b058f45 2022-06-30 mark if (err)
1416 9b058f45 2022-06-30 mark break;
1417 9b058f45 2022-06-30 mark }
1418 9b058f45 2022-06-30 mark if (view->parent)
1419 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1420 9b058f45 2022-06-30 mark if (!err)
1421 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1422 1e37a5c2 2019-05-12 jcs break;
1423 d2366e29 2022-07-07 mark case 'S':
1424 3c1dfe12 2022-07-08 mark view->count = 0;
1425 d2366e29 2022-07-07 mark err = switch_split(view);
1426 3c1dfe12 2022-07-08 mark break;
1427 3c1dfe12 2022-07-08 mark case '-':
1428 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1429 d2366e29 2022-07-07 mark break;
1430 3c1dfe12 2022-07-08 mark case '+':
1431 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1432 3c1dfe12 2022-07-08 mark break;
1433 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1434 60493ae3 2019-06-20 stsp break;
1435 60493ae3 2019-06-20 stsp case '/':
1436 640cd7ff 2022-06-22 mark view->count = 0;
1437 60493ae3 2019-06-20 stsp if (view->search_start)
1438 2b49a8ae 2019-06-22 stsp view_search_start(view);
1439 60493ae3 2019-06-20 stsp else
1440 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1441 1e37a5c2 2019-05-12 jcs break;
1442 b1bf1435 2019-06-21 stsp case 'N':
1443 60493ae3 2019-06-20 stsp case 'n':
1444 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1445 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1446 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1447 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1448 60493ae3 2019-06-20 stsp view->search_next(view);
1449 60493ae3 2019-06-20 stsp } else
1450 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1451 917d79a7 2022-07-01 stsp break;
1452 917d79a7 2022-07-01 stsp case 'A':
1453 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1454 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1455 917d79a7 2022-07-01 stsp else
1456 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1457 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1458 917d79a7 2022-07-01 stsp if (v->reset) {
1459 917d79a7 2022-07-01 stsp err = v->reset(v);
1460 917d79a7 2022-07-01 stsp if (err)
1461 917d79a7 2022-07-01 stsp return err;
1462 917d79a7 2022-07-01 stsp }
1463 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1464 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1465 917d79a7 2022-07-01 stsp if (err)
1466 917d79a7 2022-07-01 stsp return err;
1467 917d79a7 2022-07-01 stsp }
1468 917d79a7 2022-07-01 stsp }
1469 60493ae3 2019-06-20 stsp break;
1470 1e37a5c2 2019-05-12 jcs default:
1471 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1472 1e37a5c2 2019-05-12 jcs break;
1473 e5a0f69f 2018-08-18 stsp }
1474 e5a0f69f 2018-08-18 stsp
1475 e5a0f69f 2018-08-18 stsp return err;
1476 e5a0f69f 2018-08-18 stsp }
1477 e5a0f69f 2018-08-18 stsp
1478 336075a4 2022-06-25 op static int
1479 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1480 a3404814 2018-09-02 stsp {
1481 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1482 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1483 669b5ffa 2018-10-07 stsp return 0;
1484 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1485 669b5ffa 2018-10-07 stsp return 0;
1486 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1487 a3404814 2018-09-02 stsp return 0;
1488 a3404814 2018-09-02 stsp
1489 669b5ffa 2018-10-07 stsp return view->focussed;
1490 a3404814 2018-09-02 stsp }
1491 a3404814 2018-09-02 stsp
1492 bcbd79e2 2018-08-19 stsp static const struct got_error *
1493 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1494 e5a0f69f 2018-08-18 stsp {
1495 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1496 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1497 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1498 d2366e29 2022-07-07 mark char *mode;
1499 fd823528 2018-10-22 stsp int fast_refresh = 10;
1500 1a76625f 2018-10-22 stsp int done = 0, errcode;
1501 e5a0f69f 2018-08-18 stsp
1502 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1503 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1504 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1505 d2366e29 2022-07-07 mark else
1506 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1507 d2366e29 2022-07-07 mark
1508 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1509 1a76625f 2018-10-22 stsp if (errcode)
1510 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1511 1a76625f 2018-10-22 stsp
1512 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1513 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1514 e5a0f69f 2018-08-18 stsp
1515 1004088d 2018-09-29 stsp view->focussed = 1;
1516 878940b7 2018-09-29 stsp err = view->show(view);
1517 0cf4efb1 2018-09-29 stsp if (err)
1518 0cf4efb1 2018-09-29 stsp return err;
1519 0cf4efb1 2018-09-29 stsp update_panels();
1520 0cf4efb1 2018-09-29 stsp doupdate();
1521 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1522 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1523 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1524 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1525 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1526 fd823528 2018-10-22 stsp
1527 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1528 e5a0f69f 2018-08-18 stsp if (err)
1529 e5a0f69f 2018-08-18 stsp break;
1530 9970f7fc 2020-12-03 stsp if (view->dying) {
1531 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1532 669b5ffa 2018-10-07 stsp
1533 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1534 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1535 9970f7fc 2020-12-03 stsp entry);
1536 e78dc838 2020-12-04 stsp else if (view->parent)
1537 669b5ffa 2018-10-07 stsp prev = view->parent;
1538 669b5ffa 2018-10-07 stsp
1539 e78dc838 2020-12-04 stsp if (view->parent) {
1540 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1541 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1542 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1543 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1544 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1545 0dbbbe90 2022-06-17 op if (err)
1546 0dbbbe90 2022-06-17 op break;
1547 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1548 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1549 e78dc838 2020-12-04 stsp } else
1550 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1551 669b5ffa 2018-10-07 stsp
1552 9970f7fc 2020-12-03 stsp err = view_close(view);
1553 fb59748f 2020-12-05 stsp if (err)
1554 e5a0f69f 2018-08-18 stsp goto done;
1555 669b5ffa 2018-10-07 stsp
1556 e78dc838 2020-12-04 stsp view = NULL;
1557 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1558 e78dc838 2020-12-04 stsp if (v->focussed)
1559 e78dc838 2020-12-04 stsp break;
1560 0cf4efb1 2018-09-29 stsp }
1561 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1562 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1563 e78dc838 2020-12-04 stsp if (prev)
1564 e78dc838 2020-12-04 stsp view = prev;
1565 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1566 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1567 e78dc838 2020-12-04 stsp tog_view_list_head);
1568 e78dc838 2020-12-04 stsp }
1569 e78dc838 2020-12-04 stsp if (view) {
1570 e78dc838 2020-12-04 stsp if (view->focus_child) {
1571 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1572 e78dc838 2020-12-04 stsp view = view->child;
1573 e78dc838 2020-12-04 stsp } else
1574 e78dc838 2020-12-04 stsp view->focussed = 1;
1575 e78dc838 2020-12-04 stsp }
1576 e78dc838 2020-12-04 stsp }
1577 e5a0f69f 2018-08-18 stsp }
1578 bcbd79e2 2018-08-19 stsp if (new_view) {
1579 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1580 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1581 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1582 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1583 86c66b02 2018-10-18 stsp continue;
1584 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1585 86c66b02 2018-10-18 stsp err = view_close(v);
1586 86c66b02 2018-10-18 stsp if (err)
1587 86c66b02 2018-10-18 stsp goto done;
1588 86c66b02 2018-10-18 stsp break;
1589 86c66b02 2018-10-18 stsp }
1590 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1591 fed7eaa8 2018-10-24 stsp view = new_view;
1592 0ae84acc 2022-06-15 tracey }
1593 669b5ffa 2018-10-07 stsp if (view) {
1594 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1595 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1596 e78dc838 2020-12-04 stsp view = view->child;
1597 e78dc838 2020-12-04 stsp } else {
1598 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1599 e78dc838 2020-12-04 stsp view = view->parent;
1600 1a76625f 2018-10-22 stsp }
1601 e78dc838 2020-12-04 stsp show_panel(view->panel);
1602 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1603 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1604 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1605 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1606 669b5ffa 2018-10-07 stsp if (err)
1607 1a76625f 2018-10-22 stsp goto done;
1608 669b5ffa 2018-10-07 stsp }
1609 669b5ffa 2018-10-07 stsp err = view->show(view);
1610 0cf4efb1 2018-09-29 stsp if (err)
1611 1a76625f 2018-10-22 stsp goto done;
1612 669b5ffa 2018-10-07 stsp if (view->child) {
1613 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1614 669b5ffa 2018-10-07 stsp if (err)
1615 1a76625f 2018-10-22 stsp goto done;
1616 669b5ffa 2018-10-07 stsp }
1617 1a76625f 2018-10-22 stsp update_panels();
1618 1a76625f 2018-10-22 stsp doupdate();
1619 0cf4efb1 2018-09-29 stsp }
1620 e5a0f69f 2018-08-18 stsp }
1621 e5a0f69f 2018-08-18 stsp done:
1622 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1623 5629093a 2022-07-11 stsp const struct got_error *close_err;
1624 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1625 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1626 5629093a 2022-07-11 stsp close_err = view_close(view);
1627 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1628 5629093a 2022-07-11 stsp err = close_err;
1629 e5a0f69f 2018-08-18 stsp }
1630 1a76625f 2018-10-22 stsp
1631 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1632 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1633 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1634 1a76625f 2018-10-22 stsp
1635 e5a0f69f 2018-08-18 stsp return err;
1636 ea5e7bb5 2018-08-01 stsp }
1637 ea5e7bb5 2018-08-01 stsp
1638 4ed7e80c 2018-05-20 stsp __dead static void
1639 9f7d7167 2018-04-29 stsp usage_log(void)
1640 9f7d7167 2018-04-29 stsp {
1641 80ddbec8 2018-04-29 stsp endwin();
1642 c70c5802 2018-08-01 stsp fprintf(stderr,
1643 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1644 9f7d7167 2018-04-29 stsp getprogname());
1645 9f7d7167 2018-04-29 stsp exit(1);
1646 80ddbec8 2018-04-29 stsp }
1647 80ddbec8 2018-04-29 stsp
1648 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1649 80ddbec8 2018-04-29 stsp static const struct got_error *
1650 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1651 963b370f 2018-05-20 stsp {
1652 00dfcb92 2018-06-11 stsp char *vis = NULL;
1653 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1654 963b370f 2018-05-20 stsp
1655 963b370f 2018-05-20 stsp *ws = NULL;
1656 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1657 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1658 00dfcb92 2018-06-11 stsp int vislen;
1659 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1660 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1661 00dfcb92 2018-06-11 stsp
1662 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1663 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1664 00dfcb92 2018-06-11 stsp if (err)
1665 00dfcb92 2018-06-11 stsp return err;
1666 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1667 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1668 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1669 a7f50699 2018-06-11 stsp goto done;
1670 a7f50699 2018-06-11 stsp }
1671 00dfcb92 2018-06-11 stsp }
1672 963b370f 2018-05-20 stsp
1673 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1674 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1675 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1676 a7f50699 2018-06-11 stsp goto done;
1677 a7f50699 2018-06-11 stsp }
1678 963b370f 2018-05-20 stsp
1679 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1680 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1681 a7f50699 2018-06-11 stsp done:
1682 00dfcb92 2018-06-11 stsp free(vis);
1683 963b370f 2018-05-20 stsp if (err) {
1684 963b370f 2018-05-20 stsp free(*ws);
1685 963b370f 2018-05-20 stsp *ws = NULL;
1686 963b370f 2018-05-20 stsp *wlen = 0;
1687 963b370f 2018-05-20 stsp }
1688 963b370f 2018-05-20 stsp return err;
1689 145b6838 2022-06-16 stsp }
1690 145b6838 2022-06-16 stsp
1691 145b6838 2022-06-16 stsp static const struct got_error *
1692 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1693 145b6838 2022-06-16 stsp {
1694 145b6838 2022-06-16 stsp char *dst;
1695 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1696 145b6838 2022-06-16 stsp
1697 145b6838 2022-06-16 stsp *ptr = NULL;
1698 145b6838 2022-06-16 stsp n = len = strlen(src);
1699 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1700 145b6838 2022-06-16 stsp if (dst == NULL)
1701 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1702 145b6838 2022-06-16 stsp
1703 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1704 145b6838 2022-06-16 stsp const char c = src[idx];
1705 145b6838 2022-06-16 stsp
1706 145b6838 2022-06-16 stsp if (c == '\t') {
1707 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1708 367ddf28 2022-06-16 mark char *p;
1709 367ddf28 2022-06-16 mark
1710 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1711 6e1c41ad 2022-06-16 mark if (p == NULL) {
1712 6e1c41ad 2022-06-16 mark free(dst);
1713 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1714 6e1c41ad 2022-06-16 mark
1715 6e1c41ad 2022-06-16 mark }
1716 6e1c41ad 2022-06-16 mark dst = p;
1717 145b6838 2022-06-16 stsp n += nb;
1718 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1719 145b6838 2022-06-16 stsp sz += nb;
1720 145b6838 2022-06-16 stsp } else
1721 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1722 145b6838 2022-06-16 stsp ++idx;
1723 145b6838 2022-06-16 stsp }
1724 145b6838 2022-06-16 stsp
1725 145b6838 2022-06-16 stsp dst[sz] = '\0';
1726 145b6838 2022-06-16 stsp *ptr = dst;
1727 145b6838 2022-06-16 stsp return NULL;
1728 963b370f 2018-05-20 stsp }
1729 963b370f 2018-05-20 stsp
1730 4e4a9ac8 2022-06-17 op /*
1731 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1732 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1733 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1734 4e4a9ac8 2022-06-17 op * *rcol.
1735 ccda2f4d 2022-06-16 stsp */
1736 4e4a9ac8 2022-06-17 op static int
1737 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1738 4e4a9ac8 2022-06-17 op {
1739 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1740 ccda2f4d 2022-06-16 stsp
1741 4e4a9ac8 2022-06-17 op if (n == 0) {
1742 4e4a9ac8 2022-06-17 op *rcol = cols;
1743 4e4a9ac8 2022-06-17 op return off;
1744 4e4a9ac8 2022-06-17 op }
1745 ccda2f4d 2022-06-16 stsp
1746 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1747 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1748 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1749 4e4a9ac8 2022-06-17 op else
1750 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1751 ccda2f4d 2022-06-16 stsp
1752 4e4a9ac8 2022-06-17 op if (width == -1) {
1753 4e4a9ac8 2022-06-17 op width = 1;
1754 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1755 ccda2f4d 2022-06-16 stsp }
1756 ccda2f4d 2022-06-16 stsp
1757 4e4a9ac8 2022-06-17 op if (cols + width > n)
1758 4e4a9ac8 2022-06-17 op break;
1759 4e4a9ac8 2022-06-17 op cols += width;
1760 ccda2f4d 2022-06-16 stsp }
1761 ccda2f4d 2022-06-16 stsp
1762 4e4a9ac8 2022-06-17 op *rcol = cols;
1763 4e4a9ac8 2022-06-17 op return i;
1764 ccda2f4d 2022-06-16 stsp }
1765 ccda2f4d 2022-06-16 stsp
1766 ccda2f4d 2022-06-16 stsp /*
1767 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1768 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1769 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1770 ccda2f4d 2022-06-16 stsp */
1771 ccda2f4d 2022-06-16 stsp static const struct got_error *
1772 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1773 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1774 ccda2f4d 2022-06-16 stsp {
1775 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1776 4e4a9ac8 2022-06-17 op int cols;
1777 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1778 145b6838 2022-06-16 stsp char *exstr = NULL;
1779 963b370f 2018-05-20 stsp size_t wlen;
1780 4e4a9ac8 2022-06-17 op int i, scrollx;
1781 963b370f 2018-05-20 stsp
1782 963b370f 2018-05-20 stsp *wlinep = NULL;
1783 b700b5d6 2018-07-10 stsp *widthp = 0;
1784 963b370f 2018-05-20 stsp
1785 145b6838 2022-06-16 stsp if (expand) {
1786 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1787 145b6838 2022-06-16 stsp if (err)
1788 145b6838 2022-06-16 stsp return err;
1789 145b6838 2022-06-16 stsp }
1790 145b6838 2022-06-16 stsp
1791 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1792 145b6838 2022-06-16 stsp free(exstr);
1793 963b370f 2018-05-20 stsp if (err)
1794 963b370f 2018-05-20 stsp return err;
1795 963b370f 2018-05-20 stsp
1796 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1797 ccda2f4d 2022-06-16 stsp
1798 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1799 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1800 3f670bfb 2020-12-10 stsp wlen--;
1801 3f670bfb 2020-12-10 stsp }
1802 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1803 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1804 3f670bfb 2020-12-10 stsp wlen--;
1805 3f670bfb 2020-12-10 stsp }
1806 3f670bfb 2020-12-10 stsp
1807 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1808 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1809 27a741e5 2019-09-11 stsp
1810 b700b5d6 2018-07-10 stsp if (widthp)
1811 b700b5d6 2018-07-10 stsp *widthp = cols;
1812 ccda2f4d 2022-06-16 stsp if (scrollxp)
1813 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1814 963b370f 2018-05-20 stsp if (err)
1815 963b370f 2018-05-20 stsp free(wline);
1816 963b370f 2018-05-20 stsp else
1817 963b370f 2018-05-20 stsp *wlinep = wline;
1818 963b370f 2018-05-20 stsp return err;
1819 963b370f 2018-05-20 stsp }
1820 963b370f 2018-05-20 stsp
1821 8b473291 2019-02-21 stsp static const struct got_error*
1822 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1823 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1824 8b473291 2019-02-21 stsp {
1825 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1826 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1827 8b473291 2019-02-21 stsp char *s;
1828 8b473291 2019-02-21 stsp const char *name;
1829 8b473291 2019-02-21 stsp
1830 8b473291 2019-02-21 stsp *refs_str = NULL;
1831 8b473291 2019-02-21 stsp
1832 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1833 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1834 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1835 52b5abe1 2019-08-13 stsp int cmp;
1836 52b5abe1 2019-08-13 stsp
1837 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1838 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1839 8b473291 2019-02-21 stsp continue;
1840 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1841 8b473291 2019-02-21 stsp name += 5;
1842 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1843 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1844 7143d404 2019-03-12 stsp continue;
1845 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1846 8b473291 2019-02-21 stsp name += 6;
1847 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1848 8b473291 2019-02-21 stsp name += 8;
1849 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1850 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1851 79cc719f 2020-04-24 stsp continue;
1852 79cc719f 2020-04-24 stsp }
1853 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1854 48cae60d 2020-09-22 stsp if (err)
1855 48cae60d 2020-09-22 stsp break;
1856 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1857 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1858 5d844a1e 2019-08-13 stsp if (err) {
1859 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1860 48cae60d 2020-09-22 stsp free(ref_id);
1861 5d844a1e 2019-08-13 stsp break;
1862 48cae60d 2020-09-22 stsp }
1863 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1864 5d844a1e 2019-08-13 stsp err = NULL;
1865 5d844a1e 2019-08-13 stsp tag = NULL;
1866 5d844a1e 2019-08-13 stsp }
1867 52b5abe1 2019-08-13 stsp }
1868 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1869 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1870 48cae60d 2020-09-22 stsp free(ref_id);
1871 52b5abe1 2019-08-13 stsp if (tag)
1872 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1873 52b5abe1 2019-08-13 stsp if (cmp != 0)
1874 52b5abe1 2019-08-13 stsp continue;
1875 8b473291 2019-02-21 stsp s = *refs_str;
1876 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1877 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1878 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1879 8b473291 2019-02-21 stsp free(s);
1880 8b473291 2019-02-21 stsp *refs_str = NULL;
1881 8b473291 2019-02-21 stsp break;
1882 8b473291 2019-02-21 stsp }
1883 8b473291 2019-02-21 stsp free(s);
1884 8b473291 2019-02-21 stsp }
1885 8b473291 2019-02-21 stsp
1886 8b473291 2019-02-21 stsp return err;
1887 8b473291 2019-02-21 stsp }
1888 8b473291 2019-02-21 stsp
1889 963b370f 2018-05-20 stsp static const struct got_error *
1890 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1891 27a741e5 2019-09-11 stsp int col_tab_align)
1892 5813d178 2019-03-09 stsp {
1893 e6b8b890 2020-12-29 naddy char *smallerthan;
1894 5813d178 2019-03-09 stsp
1895 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1896 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1897 5813d178 2019-03-09 stsp author = smallerthan + 1;
1898 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1899 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1900 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1901 5813d178 2019-03-09 stsp }
1902 5813d178 2019-03-09 stsp
1903 5813d178 2019-03-09 stsp static const struct got_error *
1904 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1905 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1906 8fdc79fe 2020-12-01 naddy int author_display_cols)
1907 80ddbec8 2018-04-29 stsp {
1908 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1909 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1910 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1911 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1912 5813d178 2019-03-09 stsp char *author = NULL;
1913 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1914 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1915 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1916 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1917 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1918 ccb26ccd 2018-11-05 stsp struct tm tm;
1919 45d799e2 2018-12-23 stsp time_t committer_time;
1920 11b20872 2019-11-08 stsp struct tog_color *tc;
1921 80ddbec8 2018-04-29 stsp
1922 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1923 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1924 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1925 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1926 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1927 b39d25c7 2018-07-10 stsp
1928 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1929 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1930 b39d25c7 2018-07-10 stsp else
1931 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1932 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1933 11b20872 2019-11-08 stsp if (tc)
1934 11b20872 2019-11-08 stsp wattr_on(view->window,
1935 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1936 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1937 11b20872 2019-11-08 stsp if (tc)
1938 11b20872 2019-11-08 stsp wattr_off(view->window,
1939 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1940 27a741e5 2019-09-11 stsp col = limit;
1941 b39d25c7 2018-07-10 stsp if (col > avail)
1942 b39d25c7 2018-07-10 stsp goto done;
1943 6570a66d 2019-11-08 stsp
1944 6570a66d 2019-11-08 stsp if (avail >= 120) {
1945 6570a66d 2019-11-08 stsp char *id_str;
1946 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1947 6570a66d 2019-11-08 stsp if (err)
1948 6570a66d 2019-11-08 stsp goto done;
1949 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1950 11b20872 2019-11-08 stsp if (tc)
1951 11b20872 2019-11-08 stsp wattr_on(view->window,
1952 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1953 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1954 11b20872 2019-11-08 stsp if (tc)
1955 11b20872 2019-11-08 stsp wattr_off(view->window,
1956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1957 6570a66d 2019-11-08 stsp free(id_str);
1958 6570a66d 2019-11-08 stsp col += 9;
1959 6570a66d 2019-11-08 stsp if (col > avail)
1960 6570a66d 2019-11-08 stsp goto done;
1961 6570a66d 2019-11-08 stsp }
1962 b39d25c7 2018-07-10 stsp
1963 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1964 5813d178 2019-03-09 stsp if (author == NULL) {
1965 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1966 80ddbec8 2018-04-29 stsp goto done;
1967 80ddbec8 2018-04-29 stsp }
1968 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1969 bb737323 2018-05-20 stsp if (err)
1970 bb737323 2018-05-20 stsp goto done;
1971 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1972 11b20872 2019-11-08 stsp if (tc)
1973 11b20872 2019-11-08 stsp wattr_on(view->window,
1974 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1975 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1976 11b20872 2019-11-08 stsp if (tc)
1977 11b20872 2019-11-08 stsp wattr_off(view->window,
1978 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1979 bb737323 2018-05-20 stsp col += author_width;
1980 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1981 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1982 bb737323 2018-05-20 stsp col++;
1983 bb737323 2018-05-20 stsp author_width++;
1984 bb737323 2018-05-20 stsp }
1985 9c2eaf34 2018-05-20 stsp if (col > avail)
1986 9c2eaf34 2018-05-20 stsp goto done;
1987 80ddbec8 2018-04-29 stsp
1988 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1989 5943eee2 2019-08-13 stsp if (err)
1990 6d9fbc00 2018-04-29 stsp goto done;
1991 bb737323 2018-05-20 stsp logmsg = logmsg0;
1992 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1993 bb737323 2018-05-20 stsp logmsg++;
1994 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1995 bb737323 2018-05-20 stsp if (newline)
1996 bb737323 2018-05-20 stsp *newline = '\0';
1997 ccda2f4d 2022-06-16 stsp limit = avail - col;
1998 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
1999 4d1f6af3 2022-06-17 op limit--; /* for the border */
2000 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2001 ccda2f4d 2022-06-16 stsp limit, col, 1);
2002 29688b02 2022-06-16 stsp if (err)
2003 29688b02 2022-06-16 stsp goto done;
2004 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2005 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2006 27a741e5 2019-09-11 stsp while (col < avail) {
2007 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2008 bb737323 2018-05-20 stsp col++;
2009 881b2d3e 2018-04-30 stsp }
2010 80ddbec8 2018-04-29 stsp done:
2011 80ddbec8 2018-04-29 stsp free(logmsg0);
2012 bb737323 2018-05-20 stsp free(wlogmsg);
2013 5813d178 2019-03-09 stsp free(author);
2014 bb737323 2018-05-20 stsp free(wauthor);
2015 80ddbec8 2018-04-29 stsp free(line);
2016 80ddbec8 2018-04-29 stsp return err;
2017 80ddbec8 2018-04-29 stsp }
2018 26ed57b2 2018-05-19 stsp
2019 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2020 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2021 899d86c2 2018-05-10 stsp struct got_object_id *id)
2022 80ddbec8 2018-04-29 stsp {
2023 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2024 80ddbec8 2018-04-29 stsp
2025 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2026 80ddbec8 2018-04-29 stsp if (entry == NULL)
2027 899d86c2 2018-05-10 stsp return NULL;
2028 99db9666 2018-05-07 stsp
2029 899d86c2 2018-05-10 stsp entry->id = id;
2030 99db9666 2018-05-07 stsp entry->commit = commit;
2031 899d86c2 2018-05-10 stsp return entry;
2032 99db9666 2018-05-07 stsp }
2033 80ddbec8 2018-04-29 stsp
2034 99db9666 2018-05-07 stsp static void
2035 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2036 99db9666 2018-05-07 stsp {
2037 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2038 99db9666 2018-05-07 stsp
2039 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2040 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2041 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2042 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2043 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2044 99db9666 2018-05-07 stsp free(entry);
2045 99db9666 2018-05-07 stsp }
2046 99db9666 2018-05-07 stsp
2047 99db9666 2018-05-07 stsp static void
2048 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2049 99db9666 2018-05-07 stsp {
2050 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2051 99db9666 2018-05-07 stsp pop_commit(commits);
2052 c4972b91 2018-05-07 stsp }
2053 c4972b91 2018-05-07 stsp
2054 c4972b91 2018-05-07 stsp static const struct got_error *
2055 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2056 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2057 13add988 2019-10-15 stsp {
2058 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2059 13add988 2019-10-15 stsp regmatch_t regmatch;
2060 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2061 13add988 2019-10-15 stsp
2062 13add988 2019-10-15 stsp *have_match = 0;
2063 13add988 2019-10-15 stsp
2064 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2065 13add988 2019-10-15 stsp if (err)
2066 13add988 2019-10-15 stsp return err;
2067 13add988 2019-10-15 stsp
2068 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2069 13add988 2019-10-15 stsp if (err)
2070 13add988 2019-10-15 stsp goto done;
2071 13add988 2019-10-15 stsp
2072 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2073 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2074 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2075 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2076 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2077 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2078 13add988 2019-10-15 stsp *have_match = 1;
2079 13add988 2019-10-15 stsp done:
2080 13add988 2019-10-15 stsp free(id_str);
2081 13add988 2019-10-15 stsp free(logmsg);
2082 13add988 2019-10-15 stsp return err;
2083 13add988 2019-10-15 stsp }
2084 13add988 2019-10-15 stsp
2085 13add988 2019-10-15 stsp static const struct got_error *
2086 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2087 c4972b91 2018-05-07 stsp {
2088 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2089 9ba79e04 2018-06-11 stsp
2090 1a76625f 2018-10-22 stsp /*
2091 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2092 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2093 1a76625f 2018-10-22 stsp * while updating the display.
2094 1a76625f 2018-10-22 stsp */
2095 4e0d2870 2020-12-07 naddy do {
2096 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2097 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2098 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2099 1a76625f 2018-10-22 stsp int errcode;
2100 899d86c2 2018-05-10 stsp
2101 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2102 4e0d2870 2020-12-07 naddy NULL, NULL);
2103 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2104 ecb28ae0 2018-07-16 stsp break;
2105 899d86c2 2018-05-10 stsp
2106 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2107 9ba79e04 2018-06-11 stsp if (err)
2108 9ba79e04 2018-06-11 stsp break;
2109 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2110 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2111 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2112 9ba79e04 2018-06-11 stsp break;
2113 9ba79e04 2018-06-11 stsp }
2114 93e45b7c 2018-09-24 stsp
2115 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2116 1a76625f 2018-10-22 stsp if (errcode) {
2117 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2118 13add988 2019-10-15 stsp "pthread_mutex_lock");
2119 1a76625f 2018-10-22 stsp break;
2120 1a76625f 2018-10-22 stsp }
2121 1a76625f 2018-10-22 stsp
2122 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2123 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2124 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2125 1a76625f 2018-10-22 stsp
2126 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2127 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2128 7c1452c1 2020-03-26 stsp int have_match;
2129 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2130 7c1452c1 2020-03-26 stsp if (err)
2131 7c1452c1 2020-03-26 stsp break;
2132 7c1452c1 2020-03-26 stsp if (have_match)
2133 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2134 13add988 2019-10-15 stsp }
2135 13add988 2019-10-15 stsp
2136 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2137 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2138 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2139 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2140 7c1452c1 2020-03-26 stsp if (err)
2141 13add988 2019-10-15 stsp break;
2142 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2143 899d86c2 2018-05-10 stsp
2144 9ba79e04 2018-06-11 stsp return err;
2145 0553a4e3 2018-04-30 stsp }
2146 0553a4e3 2018-04-30 stsp
2147 2b779855 2020-12-05 naddy static void
2148 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2149 2b779855 2020-12-05 naddy {
2150 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2151 2b779855 2020-12-05 naddy int ncommits = 0;
2152 2b779855 2020-12-05 naddy
2153 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2154 2b779855 2020-12-05 naddy while (entry) {
2155 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2156 2b779855 2020-12-05 naddy s->selected_entry = entry;
2157 2b779855 2020-12-05 naddy break;
2158 2b779855 2020-12-05 naddy }
2159 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2160 2b779855 2020-12-05 naddy ncommits++;
2161 2b779855 2020-12-05 naddy }
2162 2b779855 2020-12-05 naddy }
2163 2b779855 2020-12-05 naddy
2164 0553a4e3 2018-04-30 stsp static const struct got_error *
2165 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2166 0553a4e3 2018-04-30 stsp {
2167 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2168 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2169 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2170 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2171 60493ae3 2019-06-20 stsp int width;
2172 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2173 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2174 8b473291 2019-02-21 stsp char *refs_str = NULL;
2175 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2176 11b20872 2019-11-08 stsp struct tog_color *tc;
2177 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2178 0553a4e3 2018-04-30 stsp
2179 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2180 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2181 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2182 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2183 1a76625f 2018-10-22 stsp if (err)
2184 ecb28ae0 2018-07-16 stsp return err;
2185 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2186 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2187 d2075bf3 2020-12-25 stsp if (refs) {
2188 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2189 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2190 d2075bf3 2020-12-25 stsp if (err)
2191 d2075bf3 2020-12-25 stsp goto done;
2192 d2075bf3 2020-12-25 stsp }
2193 867c6645 2018-07-10 stsp }
2194 359bfafd 2019-02-22 stsp
2195 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2196 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2197 1a76625f 2018-10-22 stsp
2198 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2199 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2200 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2201 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2202 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2203 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2204 8f4ed634 2020-03-26 stsp goto done;
2205 8f4ed634 2020-03-26 stsp }
2206 8f4ed634 2020-03-26 stsp } else {
2207 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2208 f9686aa5 2020-03-27 stsp
2209 f9686aa5 2020-03-27 stsp if (view->searching) {
2210 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2211 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2212 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2213 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2214 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2215 f9686aa5 2020-03-27 stsp search_str = "searching...";
2216 f9686aa5 2020-03-27 stsp }
2217 f9686aa5 2020-03-27 stsp
2218 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2219 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2220 f9686aa5 2020-03-27 stsp search_str ? search_str :
2221 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2222 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2223 8f4ed634 2020-03-26 stsp goto done;
2224 8f4ed634 2020-03-26 stsp }
2225 8b473291 2019-02-21 stsp }
2226 1a76625f 2018-10-22 stsp
2227 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2228 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2229 87411fa9 2022-06-16 stsp "........................................",
2230 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2231 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2232 1a76625f 2018-10-22 stsp header = NULL;
2233 1a76625f 2018-10-22 stsp goto done;
2234 1a76625f 2018-10-22 stsp }
2235 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2236 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2237 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2238 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2239 1a76625f 2018-10-22 stsp header = NULL;
2240 1a76625f 2018-10-22 stsp goto done;
2241 ecb28ae0 2018-07-16 stsp }
2242 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2243 1a76625f 2018-10-22 stsp if (err)
2244 1a76625f 2018-10-22 stsp goto done;
2245 867c6645 2018-07-10 stsp
2246 2814baeb 2018-08-01 stsp werase(view->window);
2247 867c6645 2018-07-10 stsp
2248 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2249 a3404814 2018-09-02 stsp wstandout(view->window);
2250 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2251 11b20872 2019-11-08 stsp if (tc)
2252 11b20872 2019-11-08 stsp wattr_on(view->window,
2253 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2254 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2255 11b20872 2019-11-08 stsp if (tc)
2256 11b20872 2019-11-08 stsp wattr_off(view->window,
2257 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2258 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2259 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2260 1a76625f 2018-10-22 stsp width++;
2261 1a76625f 2018-10-22 stsp }
2262 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2263 a3404814 2018-09-02 stsp wstandend(view->window);
2264 ecb28ae0 2018-07-16 stsp free(wline);
2265 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2266 1a76625f 2018-10-22 stsp goto done;
2267 0553a4e3 2018-04-30 stsp
2268 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2269 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2270 5813d178 2019-03-09 stsp ncommits = 0;
2271 145b6838 2022-06-16 stsp view->maxx = 0;
2272 5813d178 2019-03-09 stsp while (entry) {
2273 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2274 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2275 5813d178 2019-03-09 stsp int width;
2276 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2277 5813d178 2019-03-09 stsp break;
2278 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2279 5813d178 2019-03-09 stsp if (author == NULL) {
2280 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2281 5813d178 2019-03-09 stsp goto done;
2282 5813d178 2019-03-09 stsp }
2283 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2284 27a741e5 2019-09-11 stsp date_display_cols);
2285 5813d178 2019-03-09 stsp if (author_cols < width)
2286 5813d178 2019-03-09 stsp author_cols = width;
2287 5813d178 2019-03-09 stsp free(wauthor);
2288 5813d178 2019-03-09 stsp free(author);
2289 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2290 145b6838 2022-06-16 stsp if (err)
2291 145b6838 2022-06-16 stsp goto done;
2292 145b6838 2022-06-16 stsp msg = msg0;
2293 145b6838 2022-06-16 stsp while (*msg == '\n')
2294 145b6838 2022-06-16 stsp ++msg;
2295 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2296 29688b02 2022-06-16 stsp *eol = '\0';
2297 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2298 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2299 29688b02 2022-06-16 stsp if (err)
2300 29688b02 2022-06-16 stsp goto done;
2301 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2302 145b6838 2022-06-16 stsp free(msg0);
2303 29688b02 2022-06-16 stsp free(wmsg);
2304 7ca04879 2019-10-19 stsp ncommits++;
2305 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2306 5813d178 2019-03-09 stsp }
2307 5813d178 2019-03-09 stsp
2308 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2309 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2310 867c6645 2018-07-10 stsp ncommits = 0;
2311 899d86c2 2018-05-10 stsp while (entry) {
2312 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2313 899d86c2 2018-05-10 stsp break;
2314 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2315 2814baeb 2018-08-01 stsp wstandout(view->window);
2316 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2317 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2318 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2319 2814baeb 2018-08-01 stsp wstandend(view->window);
2320 0553a4e3 2018-04-30 stsp if (err)
2321 60493ae3 2019-06-20 stsp goto done;
2322 0553a4e3 2018-04-30 stsp ncommits++;
2323 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2324 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2325 80ddbec8 2018-04-29 stsp }
2326 80ddbec8 2018-04-29 stsp
2327 9b058f45 2022-06-30 mark view_border(view);
2328 1a76625f 2018-10-22 stsp done:
2329 1a76625f 2018-10-22 stsp free(id_str);
2330 8b473291 2019-02-21 stsp free(refs_str);
2331 1a76625f 2018-10-22 stsp free(ncommits_str);
2332 1a76625f 2018-10-22 stsp free(header);
2333 80ddbec8 2018-04-29 stsp return err;
2334 9f7d7167 2018-04-29 stsp }
2335 07b55e75 2018-05-10 stsp
2336 07b55e75 2018-05-10 stsp static void
2337 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2338 07b55e75 2018-05-10 stsp {
2339 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2340 07b55e75 2018-05-10 stsp int nscrolled = 0;
2341 07b55e75 2018-05-10 stsp
2342 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2343 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2344 07b55e75 2018-05-10 stsp return;
2345 9f7d7167 2018-04-29 stsp
2346 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2347 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2348 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2349 07b55e75 2018-05-10 stsp if (entry) {
2350 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2351 07b55e75 2018-05-10 stsp nscrolled++;
2352 07b55e75 2018-05-10 stsp }
2353 07b55e75 2018-05-10 stsp }
2354 aa075928 2018-05-10 stsp }
2355 aa075928 2018-05-10 stsp
2356 aa075928 2018-05-10 stsp static const struct got_error *
2357 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2358 aa075928 2018-05-10 stsp {
2359 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2360 5e224a3e 2019-02-22 stsp int errcode;
2361 8a42fca8 2019-02-22 stsp
2362 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2363 aa075928 2018-05-10 stsp
2364 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2365 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2366 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2367 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2368 7aafa0d1 2019-02-22 stsp if (errcode)
2369 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2370 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2371 7c1452c1 2020-03-26 stsp
2372 7c1452c1 2020-03-26 stsp /*
2373 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2374 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2375 7c1452c1 2020-03-26 stsp */
2376 7c1452c1 2020-03-26 stsp if (!wait)
2377 7c1452c1 2020-03-26 stsp break;
2378 7c1452c1 2020-03-26 stsp
2379 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2380 ffe38506 2020-12-01 naddy show_log_view(view);
2381 7c1452c1 2020-03-26 stsp update_panels();
2382 7c1452c1 2020-03-26 stsp doupdate();
2383 7c1452c1 2020-03-26 stsp
2384 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2385 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2386 82954512 2020-02-03 stsp if (errcode)
2387 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2388 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2389 82954512 2020-02-03 stsp
2390 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2391 ffe38506 2020-12-01 naddy show_log_view(view);
2392 7c1452c1 2020-03-26 stsp update_panels();
2393 7c1452c1 2020-03-26 stsp doupdate();
2394 5e224a3e 2019-02-22 stsp }
2395 5e224a3e 2019-02-22 stsp
2396 5e224a3e 2019-02-22 stsp return NULL;
2397 5e224a3e 2019-02-22 stsp }
2398 5e224a3e 2019-02-22 stsp
2399 5e224a3e 2019-02-22 stsp static const struct got_error *
2400 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2401 9b058f45 2022-06-30 mark {
2402 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2403 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2404 9b058f45 2022-06-30 mark
2405 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2406 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2407 9b058f45 2022-06-30 mark view->nscrolled = 0;
2408 9b058f45 2022-06-30 mark
2409 9b058f45 2022-06-30 mark return err;
2410 9b058f45 2022-06-30 mark }
2411 9b058f45 2022-06-30 mark
2412 9b058f45 2022-06-30 mark static const struct got_error *
2413 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2414 5e224a3e 2019-02-22 stsp {
2415 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2416 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2417 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2418 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2419 5e224a3e 2019-02-22 stsp
2420 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2421 5e224a3e 2019-02-22 stsp return NULL;
2422 5e224a3e 2019-02-22 stsp
2423 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2424 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2425 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2426 08ebd0a9 2019-02-22 stsp /*
2427 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2428 08ebd0a9 2019-02-22 stsp */
2429 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2430 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2431 5e224a3e 2019-02-22 stsp if (err)
2432 5e224a3e 2019-02-22 stsp return err;
2433 7aafa0d1 2019-02-22 stsp }
2434 b295e71b 2019-02-22 stsp
2435 7aafa0d1 2019-02-22 stsp do {
2436 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2437 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2438 88048b54 2019-02-21 stsp break;
2439 88048b54 2019-02-21 stsp
2440 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2441 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2442 aa075928 2018-05-10 stsp
2443 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2444 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2445 dd0a52c1 2018-05-20 stsp break;
2446 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2447 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2448 aa075928 2018-05-10 stsp
2449 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2450 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2451 9b058f45 2022-06-30 mark else
2452 9b058f45 2022-06-30 mark view->nscrolled = 0;
2453 9b058f45 2022-06-30 mark
2454 dd0a52c1 2018-05-20 stsp return err;
2455 07b55e75 2018-05-10 stsp }
2456 4a7f7875 2018-05-10 stsp
2457 cd0acaa7 2018-05-20 stsp static const struct got_error *
2458 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2459 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2460 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2461 cd0acaa7 2018-05-20 stsp {
2462 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2463 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2464 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2465 cd0acaa7 2018-05-20 stsp
2466 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2467 15a94983 2018-12-23 stsp if (diff_view == NULL)
2468 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2469 ea5e7bb5 2018-08-01 stsp
2470 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2471 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2472 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2473 e5a0f69f 2018-08-18 stsp if (err == NULL)
2474 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2475 cd0acaa7 2018-05-20 stsp return err;
2476 4a7f7875 2018-05-10 stsp }
2477 4a7f7875 2018-05-10 stsp
2478 80ddbec8 2018-04-29 stsp static const struct got_error *
2479 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2480 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2481 9343a5fb 2018-06-23 stsp {
2482 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2483 941e9f74 2019-05-21 stsp
2484 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2485 941e9f74 2019-05-21 stsp if (parent == NULL)
2486 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2487 941e9f74 2019-05-21 stsp
2488 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2489 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2490 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2491 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2492 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2493 941e9f74 2019-05-21 stsp s->tree = subtree;
2494 941e9f74 2019-05-21 stsp s->selected = 0;
2495 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2496 941e9f74 2019-05-21 stsp return NULL;
2497 941e9f74 2019-05-21 stsp }
2498 941e9f74 2019-05-21 stsp
2499 941e9f74 2019-05-21 stsp static const struct got_error *
2500 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2501 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2502 941e9f74 2019-05-21 stsp {
2503 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2504 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2505 941e9f74 2019-05-21 stsp const char *p;
2506 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2507 9343a5fb 2018-06-23 stsp
2508 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2509 941e9f74 2019-05-21 stsp p = path;
2510 941e9f74 2019-05-21 stsp while (*p) {
2511 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2512 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2513 56e0773d 2019-11-28 stsp char *te_name;
2514 33cbf02b 2020-01-12 stsp
2515 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2516 33cbf02b 2020-01-12 stsp p++;
2517 941e9f74 2019-05-21 stsp
2518 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2519 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2520 941e9f74 2019-05-21 stsp if (slash == NULL)
2521 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2522 33cbf02b 2020-01-12 stsp else
2523 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2524 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2525 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2526 56e0773d 2019-11-28 stsp break;
2527 941e9f74 2019-05-21 stsp }
2528 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2529 56e0773d 2019-11-28 stsp if (te == NULL) {
2530 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2531 56e0773d 2019-11-28 stsp free(te_name);
2532 941e9f74 2019-05-21 stsp break;
2533 941e9f74 2019-05-21 stsp }
2534 56e0773d 2019-11-28 stsp free(te_name);
2535 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2536 941e9f74 2019-05-21 stsp
2537 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2538 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2539 b03c880f 2019-05-21 stsp
2540 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2541 941e9f74 2019-05-21 stsp if (slash)
2542 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2543 941e9f74 2019-05-21 stsp else
2544 941e9f74 2019-05-21 stsp subpath = strdup(path);
2545 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2546 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2547 941e9f74 2019-05-21 stsp break;
2548 941e9f74 2019-05-21 stsp }
2549 941e9f74 2019-05-21 stsp
2550 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2551 941e9f74 2019-05-21 stsp subpath);
2552 941e9f74 2019-05-21 stsp if (err)
2553 941e9f74 2019-05-21 stsp break;
2554 941e9f74 2019-05-21 stsp
2555 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2556 941e9f74 2019-05-21 stsp free(tree_id);
2557 941e9f74 2019-05-21 stsp if (err)
2558 941e9f74 2019-05-21 stsp break;
2559 941e9f74 2019-05-21 stsp
2560 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2561 941e9f74 2019-05-21 stsp if (err) {
2562 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2563 941e9f74 2019-05-21 stsp break;
2564 941e9f74 2019-05-21 stsp }
2565 941e9f74 2019-05-21 stsp if (slash == NULL)
2566 941e9f74 2019-05-21 stsp break;
2567 941e9f74 2019-05-21 stsp free(subpath);
2568 941e9f74 2019-05-21 stsp subpath = NULL;
2569 941e9f74 2019-05-21 stsp p = slash;
2570 941e9f74 2019-05-21 stsp }
2571 941e9f74 2019-05-21 stsp
2572 941e9f74 2019-05-21 stsp free(subpath);
2573 1a76625f 2018-10-22 stsp return err;
2574 61266923 2020-01-14 stsp }
2575 61266923 2020-01-14 stsp
2576 61266923 2020-01-14 stsp static const struct got_error *
2577 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2578 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2579 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2580 55cccc34 2020-02-20 stsp {
2581 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2582 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2583 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2584 55cccc34 2020-02-20 stsp
2585 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2586 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2587 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2588 55cccc34 2020-02-20 stsp
2589 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2590 bc573f3b 2021-07-10 stsp if (err)
2591 55cccc34 2020-02-20 stsp return err;
2592 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2593 55cccc34 2020-02-20 stsp
2594 55cccc34 2020-02-20 stsp *new_view = tree_view;
2595 55cccc34 2020-02-20 stsp
2596 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2597 55cccc34 2020-02-20 stsp return NULL;
2598 55cccc34 2020-02-20 stsp
2599 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2600 55cccc34 2020-02-20 stsp }
2601 55cccc34 2020-02-20 stsp
2602 55cccc34 2020-02-20 stsp static const struct got_error *
2603 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2604 61266923 2020-01-14 stsp {
2605 61266923 2020-01-14 stsp sigset_t sigset;
2606 61266923 2020-01-14 stsp int errcode;
2607 61266923 2020-01-14 stsp
2608 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2609 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2610 61266923 2020-01-14 stsp
2611 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2612 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2613 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2614 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2615 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2616 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2617 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2618 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2619 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2620 61266923 2020-01-14 stsp
2621 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2622 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2623 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2624 61266923 2020-01-14 stsp
2625 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2626 61266923 2020-01-14 stsp if (errcode)
2627 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2628 61266923 2020-01-14 stsp
2629 61266923 2020-01-14 stsp return NULL;
2630 1a76625f 2018-10-22 stsp }
2631 1a76625f 2018-10-22 stsp
2632 1a76625f 2018-10-22 stsp static void *
2633 1a76625f 2018-10-22 stsp log_thread(void *arg)
2634 1a76625f 2018-10-22 stsp {
2635 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2636 1a76625f 2018-10-22 stsp int errcode = 0;
2637 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2638 1a76625f 2018-10-22 stsp int done = 0;
2639 61266923 2020-01-14 stsp
2640 5629093a 2022-07-11 stsp /*
2641 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2642 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2643 5629093a 2022-07-11 stsp */
2644 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2645 5629093a 2022-07-11 stsp if (errcode) {
2646 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2647 61266923 2020-01-14 stsp return (void *)err;
2648 5629093a 2022-07-11 stsp }
2649 1a76625f 2018-10-22 stsp
2650 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2651 5629093a 2022-07-11 stsp if (err) {
2652 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2653 5629093a 2022-07-11 stsp goto done;
2654 5629093a 2022-07-11 stsp }
2655 5629093a 2022-07-11 stsp
2656 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2657 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2658 5629093a 2022-07-11 stsp if (errcode) {
2659 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2660 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2661 5629093a 2022-07-11 stsp goto done;
2662 5629093a 2022-07-11 stsp }
2663 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2664 1a76625f 2018-10-22 stsp if (err) {
2665 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2666 5629093a 2022-07-11 stsp goto done;
2667 1a76625f 2018-10-22 stsp err = NULL;
2668 1a76625f 2018-10-22 stsp done = 1;
2669 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2670 1a76625f 2018-10-22 stsp a->commits_needed--;
2671 1a76625f 2018-10-22 stsp
2672 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2673 3abe8080 2019-04-10 stsp if (errcode) {
2674 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2675 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2676 5629093a 2022-07-11 stsp goto done;
2677 3abe8080 2019-04-10 stsp } else if (*a->quit)
2678 1a76625f 2018-10-22 stsp done = 1;
2679 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2680 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2681 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2682 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2683 1a76625f 2018-10-22 stsp }
2684 1a76625f 2018-10-22 stsp
2685 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2686 7c1452c1 2020-03-26 stsp if (errcode) {
2687 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2688 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2689 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2690 5629093a 2022-07-11 stsp goto done;
2691 7c1452c1 2020-03-26 stsp }
2692 7c1452c1 2020-03-26 stsp
2693 1a76625f 2018-10-22 stsp if (done)
2694 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2695 7c1452c1 2020-03-26 stsp else {
2696 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2697 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2698 7c1452c1 2020-03-26 stsp &tog_mutex);
2699 5629093a 2022-07-11 stsp if (errcode) {
2700 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2701 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2702 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2703 5629093a 2022-07-11 stsp goto done;
2704 5629093a 2022-07-11 stsp }
2705 21355643 2020-12-06 stsp if (*a->quit)
2706 21355643 2020-12-06 stsp done = 1;
2707 7c1452c1 2020-03-26 stsp }
2708 1a76625f 2018-10-22 stsp }
2709 1a76625f 2018-10-22 stsp }
2710 3abe8080 2019-04-10 stsp a->log_complete = 1;
2711 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2712 5629093a 2022-07-11 stsp if (errcode)
2713 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2714 5629093a 2022-07-11 stsp done:
2715 5629093a 2022-07-11 stsp if (err) {
2716 5629093a 2022-07-11 stsp tog_thread_error = 1;
2717 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2718 5629093a 2022-07-11 stsp }
2719 1a76625f 2018-10-22 stsp return (void *)err;
2720 1a76625f 2018-10-22 stsp }
2721 1a76625f 2018-10-22 stsp
2722 1a76625f 2018-10-22 stsp static const struct got_error *
2723 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2724 1a76625f 2018-10-22 stsp {
2725 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2726 1a76625f 2018-10-22 stsp int errcode;
2727 1a76625f 2018-10-22 stsp
2728 1a76625f 2018-10-22 stsp if (s->thread) {
2729 1a76625f 2018-10-22 stsp s->quit = 1;
2730 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2731 1a76625f 2018-10-22 stsp if (errcode)
2732 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2733 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2734 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2735 1a76625f 2018-10-22 stsp if (errcode)
2736 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2737 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2738 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2739 1a76625f 2018-10-22 stsp if (errcode)
2740 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2741 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2742 1a76625f 2018-10-22 stsp if (errcode)
2743 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2744 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2745 1a76625f 2018-10-22 stsp s->thread = NULL;
2746 1a76625f 2018-10-22 stsp }
2747 1a76625f 2018-10-22 stsp
2748 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2749 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2750 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2751 74467cc8 2022-06-15 stsp }
2752 74467cc8 2022-06-15 stsp
2753 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2754 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2755 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2756 74467cc8 2022-06-15 stsp if (err == NULL)
2757 74467cc8 2022-06-15 stsp err = pack_err;
2758 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2759 1a76625f 2018-10-22 stsp }
2760 1a76625f 2018-10-22 stsp
2761 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2762 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2763 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2764 1a76625f 2018-10-22 stsp }
2765 1a76625f 2018-10-22 stsp
2766 5629093a 2022-07-11 stsp return err ? err : thread_err;
2767 9343a5fb 2018-06-23 stsp }
2768 9343a5fb 2018-06-23 stsp
2769 9343a5fb 2018-06-23 stsp static const struct got_error *
2770 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2771 1a76625f 2018-10-22 stsp {
2772 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2773 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2774 276b94a1 2020-11-13 naddy int errcode;
2775 1a76625f 2018-10-22 stsp
2776 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2777 276b94a1 2020-11-13 naddy
2778 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2779 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2780 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2781 276b94a1 2020-11-13 naddy
2782 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2783 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2784 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2785 276b94a1 2020-11-13 naddy
2786 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2787 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2788 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2789 1a76625f 2018-10-22 stsp free(s->start_id);
2790 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2791 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2792 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2793 1a76625f 2018-10-22 stsp return err;
2794 1a76625f 2018-10-22 stsp }
2795 1a76625f 2018-10-22 stsp
2796 1a76625f 2018-10-22 stsp static const struct got_error *
2797 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2798 60493ae3 2019-06-20 stsp {
2799 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2800 60493ae3 2019-06-20 stsp
2801 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2802 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2803 60493ae3 2019-06-20 stsp return NULL;
2804 60493ae3 2019-06-20 stsp }
2805 60493ae3 2019-06-20 stsp
2806 60493ae3 2019-06-20 stsp static const struct got_error *
2807 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2808 60493ae3 2019-06-20 stsp {
2809 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2810 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2811 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2812 60493ae3 2019-06-20 stsp
2813 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2814 f9686aa5 2020-03-27 stsp show_log_view(view);
2815 f9686aa5 2020-03-27 stsp update_panels();
2816 f9686aa5 2020-03-27 stsp doupdate();
2817 f9686aa5 2020-03-27 stsp
2818 96e2b566 2019-07-08 stsp if (s->search_entry) {
2819 13add988 2019-10-15 stsp int errcode, ch;
2820 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2821 13add988 2019-10-15 stsp if (errcode)
2822 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2823 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2824 13add988 2019-10-15 stsp ch = wgetch(view->window);
2825 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2826 13add988 2019-10-15 stsp if (errcode)
2827 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2828 13add988 2019-10-15 stsp "pthread_mutex_lock");
2829 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2830 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2831 678cbce5 2019-07-28 stsp return NULL;
2832 678cbce5 2019-07-28 stsp }
2833 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2834 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2835 96e2b566 2019-07-08 stsp else
2836 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2837 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2838 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2839 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2840 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2841 364ac6fd 2022-06-18 stsp
2842 364ac6fd 2022-06-18 stsp /*
2843 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2844 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2845 f704b35c 2022-06-18 stsp * might have changed.
2846 364ac6fd 2022-06-18 stsp */
2847 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2848 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2849 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2850 364ac6fd 2022-06-18 stsp else
2851 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2852 4bfe9f0a 2022-06-18 stsp } else {
2853 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2854 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2855 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2856 364ac6fd 2022-06-18 stsp else
2857 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2858 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2859 4bfe9f0a 2022-06-18 stsp }
2860 20be8d96 2019-06-21 stsp } else {
2861 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2862 20be8d96 2019-06-21 stsp }
2863 60493ae3 2019-06-20 stsp
2864 60493ae3 2019-06-20 stsp while (1) {
2865 13add988 2019-10-15 stsp int have_match = 0;
2866 13add988 2019-10-15 stsp
2867 60493ae3 2019-06-20 stsp if (entry == NULL) {
2868 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2869 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2870 f9967bca 2020-03-27 stsp view->search_next_done =
2871 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2872 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2873 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2874 f801134a 2019-06-25 stsp return NULL;
2875 60493ae3 2019-06-20 stsp }
2876 96e2b566 2019-07-08 stsp /*
2877 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2878 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2879 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2880 96e2b566 2019-07-08 stsp */
2881 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2882 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2883 60493ae3 2019-06-20 stsp }
2884 60493ae3 2019-06-20 stsp
2885 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2886 13add988 2019-10-15 stsp &view->regex);
2887 5943eee2 2019-08-13 stsp if (err)
2888 13add988 2019-10-15 stsp break;
2889 13add988 2019-10-15 stsp if (have_match) {
2890 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2891 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2892 60493ae3 2019-06-20 stsp break;
2893 60493ae3 2019-06-20 stsp }
2894 13add988 2019-10-15 stsp
2895 96e2b566 2019-07-08 stsp s->search_entry = entry;
2896 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2897 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2898 b1bf1435 2019-06-21 stsp else
2899 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2900 60493ae3 2019-06-20 stsp }
2901 60493ae3 2019-06-20 stsp
2902 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2903 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2904 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2905 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2906 60493ae3 2019-06-20 stsp if (err)
2907 60493ae3 2019-06-20 stsp return err;
2908 ead14cbe 2019-06-21 stsp cur++;
2909 ead14cbe 2019-06-21 stsp }
2910 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2911 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2912 60493ae3 2019-06-20 stsp if (err)
2913 60493ae3 2019-06-20 stsp return err;
2914 ead14cbe 2019-06-21 stsp cur--;
2915 60493ae3 2019-06-20 stsp }
2916 60493ae3 2019-06-20 stsp }
2917 60493ae3 2019-06-20 stsp
2918 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2919 96e2b566 2019-07-08 stsp
2920 60493ae3 2019-06-20 stsp return NULL;
2921 60493ae3 2019-06-20 stsp }
2922 60493ae3 2019-06-20 stsp
2923 60493ae3 2019-06-20 stsp static const struct got_error *
2924 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2925 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2926 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2927 80ddbec8 2018-04-29 stsp {
2928 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2929 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2930 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2931 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2932 1a76625f 2018-10-22 stsp int errcode;
2933 80ddbec8 2018-04-29 stsp
2934 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2935 f135c941 2020-02-20 stsp free(s->in_repo_path);
2936 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2937 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2938 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2939 f135c941 2020-02-20 stsp }
2940 ecb28ae0 2018-07-16 stsp
2941 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2942 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2943 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2944 78756c87 2020-11-24 stsp
2945 fb2756b9 2018-08-04 stsp s->repo = repo;
2946 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2947 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2948 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2949 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2950 9cd7cbd1 2020-12-07 stsp goto done;
2951 9cd7cbd1 2020-12-07 stsp }
2952 9cd7cbd1 2020-12-07 stsp }
2953 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2954 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2955 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2956 5036bf37 2018-09-24 stsp goto done;
2957 5036bf37 2018-09-24 stsp }
2958 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2959 e5a0f69f 2018-08-18 stsp
2960 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2961 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2962 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2963 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2964 11b20872 2019-11-08 stsp if (err)
2965 11b20872 2019-11-08 stsp goto done;
2966 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2967 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2968 11b20872 2019-11-08 stsp if (err) {
2969 11b20872 2019-11-08 stsp free_colors(&s->colors);
2970 11b20872 2019-11-08 stsp goto done;
2971 11b20872 2019-11-08 stsp }
2972 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2973 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2974 11b20872 2019-11-08 stsp if (err) {
2975 11b20872 2019-11-08 stsp free_colors(&s->colors);
2976 11b20872 2019-11-08 stsp goto done;
2977 11b20872 2019-11-08 stsp }
2978 11b20872 2019-11-08 stsp }
2979 11b20872 2019-11-08 stsp
2980 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2981 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2982 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2983 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2984 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2985 1a76625f 2018-10-22 stsp
2986 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2987 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2988 74467cc8 2022-06-15 stsp if (err)
2989 74467cc8 2022-06-15 stsp goto done;
2990 74467cc8 2022-06-15 stsp }
2991 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2992 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2993 0ae84acc 2022-06-15 tracey if (err)
2994 0ae84acc 2022-06-15 tracey goto done;
2995 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2996 b672a97a 2020-01-27 stsp !s->log_branches);
2997 1a76625f 2018-10-22 stsp if (err)
2998 1a76625f 2018-10-22 stsp goto done;
2999 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3000 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3001 c5b78334 2020-01-12 stsp if (err)
3002 c5b78334 2020-01-12 stsp goto done;
3003 1a76625f 2018-10-22 stsp
3004 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3005 1a76625f 2018-10-22 stsp if (errcode) {
3006 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3007 1a76625f 2018-10-22 stsp goto done;
3008 1a76625f 2018-10-22 stsp }
3009 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3010 7c1452c1 2020-03-26 stsp if (errcode) {
3011 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3012 7c1452c1 2020-03-26 stsp goto done;
3013 7c1452c1 2020-03-26 stsp }
3014 1a76625f 2018-10-22 stsp
3015 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3016 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3017 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3018 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3019 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3020 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3021 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3022 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3023 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3024 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3025 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3026 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3027 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3028 ba4f502b 2018-08-04 stsp done:
3029 1a76625f 2018-10-22 stsp if (err)
3030 1a76625f 2018-10-22 stsp close_log_view(view);
3031 ba4f502b 2018-08-04 stsp return err;
3032 ba4f502b 2018-08-04 stsp }
3033 ba4f502b 2018-08-04 stsp
3034 e5a0f69f 2018-08-18 stsp static const struct got_error *
3035 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3036 ba4f502b 2018-08-04 stsp {
3037 f2f6d207 2020-11-24 stsp const struct got_error *err;
3038 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3039 ba4f502b 2018-08-04 stsp
3040 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3041 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3042 2b380cc8 2018-10-24 stsp &s->thread_args);
3043 2b380cc8 2018-10-24 stsp if (errcode)
3044 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3045 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3046 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3047 f2f6d207 2020-11-24 stsp if (err)
3048 f2f6d207 2020-11-24 stsp return err;
3049 f2f6d207 2020-11-24 stsp }
3050 2b380cc8 2018-10-24 stsp }
3051 2b380cc8 2018-10-24 stsp
3052 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3053 b880cc75 2022-06-30 mark }
3054 b880cc75 2022-06-30 mark
3055 b880cc75 2022-06-30 mark static void
3056 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3057 b880cc75 2022-06-30 mark {
3058 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3059 b880cc75 2022-06-30 mark
3060 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3061 b880cc75 2022-06-30 mark view->count = 0;
3062 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3063 b880cc75 2022-06-30 mark return;
3064 b880cc75 2022-06-30 mark
3065 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3066 b880cc75 2022-06-30 mark || home)
3067 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3068 b880cc75 2022-06-30 mark
3069 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3070 b880cc75 2022-06-30 mark --s->selected;
3071 b880cc75 2022-06-30 mark else
3072 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3073 b880cc75 2022-06-30 mark
3074 b880cc75 2022-06-30 mark select_commit(s);
3075 b880cc75 2022-06-30 mark return;
3076 b880cc75 2022-06-30 mark }
3077 b880cc75 2022-06-30 mark
3078 b880cc75 2022-06-30 mark static const struct got_error *
3079 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3080 b880cc75 2022-06-30 mark {
3081 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3082 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3083 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3084 b880cc75 2022-06-30 mark
3085 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3086 b880cc75 2022-06-30 mark if (first == NULL) {
3087 b880cc75 2022-06-30 mark view->count = 0;
3088 b880cc75 2022-06-30 mark return NULL;
3089 b880cc75 2022-06-30 mark }
3090 b880cc75 2022-06-30 mark
3091 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3092 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3093 b880cc75 2022-06-30 mark return NULL;
3094 b880cc75 2022-06-30 mark
3095 b880cc75 2022-06-30 mark if (!page) {
3096 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3097 b880cc75 2022-06-30 mark
3098 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3099 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3100 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3101 b880cc75 2022-06-30 mark ++s->selected;
3102 b880cc75 2022-06-30 mark else
3103 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3104 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3105 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3106 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3107 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3108 b880cc75 2022-06-30 mark else
3109 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3110 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3111 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3112 b880cc75 2022-06-30 mark } else {
3113 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3114 b880cc75 2022-06-30 mark if (err)
3115 b880cc75 2022-06-30 mark return err;
3116 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3117 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3118 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3119 b880cc75 2022-06-30 mark }
3120 b880cc75 2022-06-30 mark }
3121 b880cc75 2022-06-30 mark if (err)
3122 b880cc75 2022-06-30 mark return err;
3123 b880cc75 2022-06-30 mark
3124 9b058f45 2022-06-30 mark /*
3125 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3126 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3127 9b058f45 2022-06-30 mark */
3128 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3129 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3130 9b058f45 2022-06-30 mark
3131 b880cc75 2022-06-30 mark select_commit(s);
3132 b880cc75 2022-06-30 mark
3133 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3134 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3135 b880cc75 2022-06-30 mark view->count = 0;
3136 b880cc75 2022-06-30 mark
3137 b880cc75 2022-06-30 mark return NULL;
3138 e5a0f69f 2018-08-18 stsp }
3139 04cc582a 2018-08-01 stsp
3140 9b058f45 2022-06-30 mark static void
3141 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3142 9b058f45 2022-06-30 mark {
3143 76364b2d 2022-07-02 mark *x = 0;
3144 76364b2d 2022-07-02 mark *y = 0;
3145 76364b2d 2022-07-02 mark
3146 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3147 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3148 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3149 7532ccda 2022-07-11 mark else if (view->resized_y)
3150 7532ccda 2022-07-11 mark *y = view->resized_y;
3151 3c1dfe12 2022-07-08 mark else
3152 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3153 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3154 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3155 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3156 7532ccda 2022-07-11 mark else if (view->resized_x)
3157 7532ccda 2022-07-11 mark *x = view->resized_x;
3158 3c1dfe12 2022-07-08 mark else
3159 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3160 3c1dfe12 2022-07-08 mark }
3161 9b058f45 2022-06-30 mark }
3162 9b058f45 2022-06-30 mark
3163 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3164 e5a0f69f 2018-08-18 stsp static const struct got_error *
3165 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3166 9b058f45 2022-06-30 mark {
3167 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3168 9b058f45 2022-06-30 mark
3169 9b058f45 2022-06-30 mark view->nlines = y;
3170 d2366e29 2022-07-07 mark view->ncols = COLS;
3171 9b058f45 2022-06-30 mark err = view_resize(view);
3172 9b058f45 2022-06-30 mark if (err)
3173 9b058f45 2022-06-30 mark return err;
3174 9b058f45 2022-06-30 mark
3175 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3176 9b058f45 2022-06-30 mark
3177 9b058f45 2022-06-30 mark return err;
3178 9b058f45 2022-06-30 mark }
3179 9b058f45 2022-06-30 mark
3180 9b058f45 2022-06-30 mark static const struct got_error *
3181 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3182 e5a0f69f 2018-08-18 stsp {
3183 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3184 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3185 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3186 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3187 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3188 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
3189 80ddbec8 2018-04-29 stsp
3190 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3191 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3192 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3193 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3194 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3195 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3196 fb280deb 2021-08-30 stsp }
3197 b880cc75 2022-06-30 mark return err;
3198 528dedf3 2021-08-30 stsp }
3199 0dca135e 2022-06-30 mark
3200 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3201 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3202 0dca135e 2022-06-30 mark --eos; /* border */
3203 0dca135e 2022-06-30 mark
3204 528dedf3 2021-08-30 stsp switch (ch) {
3205 1e37a5c2 2019-05-12 jcs case 'q':
3206 1e37a5c2 2019-05-12 jcs s->quit = 1;
3207 145b6838 2022-06-16 stsp break;
3208 145b6838 2022-06-16 stsp case '0':
3209 145b6838 2022-06-16 stsp view->x = 0;
3210 145b6838 2022-06-16 stsp break;
3211 145b6838 2022-06-16 stsp case '$':
3212 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3213 640cd7ff 2022-06-22 mark view->count = 0;
3214 145b6838 2022-06-16 stsp break;
3215 145b6838 2022-06-16 stsp case KEY_RIGHT:
3216 145b6838 2022-06-16 stsp case 'l':
3217 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3218 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3219 640cd7ff 2022-06-22 mark else
3220 640cd7ff 2022-06-22 mark view->count = 0;
3221 1e37a5c2 2019-05-12 jcs break;
3222 145b6838 2022-06-16 stsp case KEY_LEFT:
3223 145b6838 2022-06-16 stsp case 'h':
3224 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3225 640cd7ff 2022-06-22 mark if (view->x <= 0)
3226 640cd7ff 2022-06-22 mark view->count = 0;
3227 145b6838 2022-06-16 stsp break;
3228 1e37a5c2 2019-05-12 jcs case 'k':
3229 1e37a5c2 2019-05-12 jcs case KEY_UP:
3230 1e37a5c2 2019-05-12 jcs case '<':
3231 1e37a5c2 2019-05-12 jcs case ',':
3232 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3233 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3234 912a3f79 2021-08-30 j break;
3235 912a3f79 2021-08-30 j case 'g':
3236 912a3f79 2021-08-30 j case KEY_HOME:
3237 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3238 640cd7ff 2022-06-22 mark view->count = 0;
3239 1e37a5c2 2019-05-12 jcs break;
3240 83cc4199 2022-06-13 stsp case CTRL('u'):
3241 33c3719a 2022-06-15 stsp case 'u':
3242 83cc4199 2022-06-13 stsp nscroll /= 2;
3243 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3244 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3245 a4292ac5 2019-05-12 jcs case CTRL('b'):
3246 61417565 2022-06-20 mark case 'b':
3247 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3248 1e37a5c2 2019-05-12 jcs break;
3249 1e37a5c2 2019-05-12 jcs case 'j':
3250 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3251 1e37a5c2 2019-05-12 jcs case '>':
3252 1e37a5c2 2019-05-12 jcs case '.':
3253 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3254 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3255 912a3f79 2021-08-30 j break;
3256 912a3f79 2021-08-30 j case 'G':
3257 912a3f79 2021-08-30 j case KEY_END: {
3258 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3259 912a3f79 2021-08-30 j * traverse them all. */
3260 640cd7ff 2022-06-22 mark view->count = 0;
3261 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3262 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3263 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3264 80ddbec8 2018-04-29 stsp }
3265 912a3f79 2021-08-30 j
3266 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3267 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3268 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3269 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3270 f3bc9f1d 2021-09-05 naddy break;
3271 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3272 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3273 f3bc9f1d 2021-09-05 naddy }
3274 f3bc9f1d 2021-09-05 naddy if (n > 0)
3275 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3276 2b779855 2020-12-05 naddy select_commit(s);
3277 1e37a5c2 2019-05-12 jcs break;
3278 912a3f79 2021-08-30 j }
3279 80b7e8da 2022-06-11 stsp case CTRL('d'):
3280 33c3719a 2022-06-15 stsp case 'd':
3281 83cc4199 2022-06-13 stsp nscroll /= 2;
3282 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3283 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3284 61417565 2022-06-20 mark case CTRL('f'):
3285 48bb96f0 2022-06-20 naddy case 'f':
3286 b880cc75 2022-06-30 mark case ' ':
3287 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3288 1e37a5c2 2019-05-12 jcs break;
3289 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3290 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3291 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3292 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3293 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3294 2b779855 2020-12-05 naddy select_commit(s);
3295 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3296 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3297 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3298 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3299 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3300 0bf7f153 2020-12-02 naddy }
3301 1e37a5c2 2019-05-12 jcs break;
3302 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3303 49b24ee5 2022-07-03 mark case '\r':
3304 640cd7ff 2022-06-22 mark view->count = 0;
3305 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3306 e5a0f69f 2018-08-18 stsp break;
3307 9b058f45 2022-06-30 mark
3308 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3309 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3310 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3311 9b058f45 2022-06-30 mark
3312 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3313 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3314 78756c87 2020-11-24 stsp view, s->repo);
3315 1e37a5c2 2019-05-12 jcs if (err)
3316 1e37a5c2 2019-05-12 jcs break;
3317 9b058f45 2022-06-30 mark
3318 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3319 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3320 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3321 9b058f45 2022-06-30 mark if (err)
3322 9b058f45 2022-06-30 mark break;
3323 9b058f45 2022-06-30 mark }
3324 9b058f45 2022-06-30 mark
3325 e78dc838 2020-12-04 stsp view->focussed = 0;
3326 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3327 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3328 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3329 9b058f45 2022-06-30 mark
3330 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3331 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
3332 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3333 f7013a22 2018-10-24 stsp if (err)
3334 1e37a5c2 2019-05-12 jcs return err;
3335 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3336 0dbbbe90 2022-06-17 op if (err)
3337 0dbbbe90 2022-06-17 op return err;
3338 e78dc838 2020-12-04 stsp view->focus_child = 1;
3339 1e37a5c2 2019-05-12 jcs } else
3340 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3341 1e37a5c2 2019-05-12 jcs break;
3342 1e37a5c2 2019-05-12 jcs case 't':
3343 640cd7ff 2022-06-22 mark view->count = 0;
3344 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3345 5036bf37 2018-09-24 stsp break;
3346 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3347 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3348 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3349 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3350 4e97c21c 2020-12-06 stsp s->repo);
3351 1e37a5c2 2019-05-12 jcs if (err)
3352 e5a0f69f 2018-08-18 stsp break;
3353 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3354 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3355 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3356 49b24ee5 2022-07-03 mark if (err)
3357 49b24ee5 2022-07-03 mark break;
3358 49b24ee5 2022-07-03 mark }
3359 e78dc838 2020-12-04 stsp view->focussed = 0;
3360 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3361 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3362 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3363 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3364 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
3365 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3366 1e37a5c2 2019-05-12 jcs if (err)
3367 1e37a5c2 2019-05-12 jcs return err;
3368 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3369 0dbbbe90 2022-06-17 op if (err)
3370 0dbbbe90 2022-06-17 op return err;
3371 e78dc838 2020-12-04 stsp view->focus_child = 1;
3372 1e37a5c2 2019-05-12 jcs } else
3373 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3374 1e37a5c2 2019-05-12 jcs break;
3375 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3376 21355643 2020-12-06 stsp case CTRL('l'):
3377 21355643 2020-12-06 stsp case 'B':
3378 640cd7ff 2022-06-22 mark view->count = 0;
3379 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3380 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3381 1e37a5c2 2019-05-12 jcs break;
3382 21355643 2020-12-06 stsp err = stop_log_thread(s);
3383 74cfe85e 2020-10-20 stsp if (err)
3384 74cfe85e 2020-10-20 stsp return err;
3385 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3386 21355643 2020-12-06 stsp char *parent_path;
3387 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3388 21355643 2020-12-06 stsp if (err)
3389 21355643 2020-12-06 stsp return err;
3390 21355643 2020-12-06 stsp free(s->in_repo_path);
3391 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3392 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3393 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3394 21355643 2020-12-06 stsp struct got_object_id *start_id;
3395 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3396 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3397 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3398 21355643 2020-12-06 stsp if (err)
3399 21355643 2020-12-06 stsp return err;
3400 21355643 2020-12-06 stsp free(s->start_id);
3401 21355643 2020-12-06 stsp s->start_id = start_id;
3402 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3403 21355643 2020-12-06 stsp } else /* 'B' */
3404 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3405 21355643 2020-12-06 stsp
3406 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3407 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3408 b0dd8d27 2022-06-16 stsp if (err)
3409 b0dd8d27 2022-06-16 stsp return err;
3410 b0dd8d27 2022-06-16 stsp }
3411 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3412 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3413 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3414 74cfe85e 2020-10-20 stsp if (err)
3415 21355643 2020-12-06 stsp return err;
3416 51a10b52 2020-12-26 stsp tog_free_refs();
3417 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3418 ca51c541 2020-12-07 stsp if (err)
3419 ca51c541 2020-12-07 stsp return err;
3420 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3421 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3422 d01904d4 2019-06-25 stsp if (err)
3423 d01904d4 2019-06-25 stsp return err;
3424 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3425 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3426 b672a97a 2020-01-27 stsp if (err)
3427 b672a97a 2020-01-27 stsp return err;
3428 21355643 2020-12-06 stsp free_commits(&s->commits);
3429 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3430 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3431 21355643 2020-12-06 stsp s->selected_entry = NULL;
3432 21355643 2020-12-06 stsp s->selected = 0;
3433 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3434 21355643 2020-12-06 stsp s->quit = 0;
3435 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3436 dfee752e 2022-06-18 op s->matched_entry = NULL;
3437 dfee752e 2022-06-18 op s->search_entry = NULL;
3438 d01904d4 2019-06-25 stsp break;
3439 6458efa5 2020-11-24 stsp case 'r':
3440 640cd7ff 2022-06-22 mark view->count = 0;
3441 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3442 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3443 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3444 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3445 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3446 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3447 6458efa5 2020-11-24 stsp if (err) {
3448 6458efa5 2020-11-24 stsp view_close(ref_view);
3449 6458efa5 2020-11-24 stsp return err;
3450 6458efa5 2020-11-24 stsp }
3451 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3452 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3453 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3454 49b24ee5 2022-07-03 mark if (err)
3455 49b24ee5 2022-07-03 mark break;
3456 49b24ee5 2022-07-03 mark }
3457 e78dc838 2020-12-04 stsp view->focussed = 0;
3458 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3459 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3460 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3461 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3462 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
3463 6458efa5 2020-11-24 stsp err = view_close_child(view);
3464 6458efa5 2020-11-24 stsp if (err)
3465 6458efa5 2020-11-24 stsp return err;
3466 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3467 0dbbbe90 2022-06-17 op if (err)
3468 0dbbbe90 2022-06-17 op return err;
3469 e78dc838 2020-12-04 stsp view->focus_child = 1;
3470 6458efa5 2020-11-24 stsp } else
3471 6458efa5 2020-11-24 stsp *new_view = ref_view;
3472 6458efa5 2020-11-24 stsp break;
3473 1e37a5c2 2019-05-12 jcs default:
3474 640cd7ff 2022-06-22 mark view->count = 0;
3475 1e37a5c2 2019-05-12 jcs break;
3476 899d86c2 2018-05-10 stsp }
3477 e5a0f69f 2018-08-18 stsp
3478 80ddbec8 2018-04-29 stsp return err;
3479 80ddbec8 2018-04-29 stsp }
3480 80ddbec8 2018-04-29 stsp
3481 4ed7e80c 2018-05-20 stsp static const struct got_error *
3482 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3483 c2db6724 2019-01-04 stsp {
3484 c2db6724 2019-01-04 stsp const struct got_error *error;
3485 c2db6724 2019-01-04 stsp
3486 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3487 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3488 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3489 37c06ea4 2019-07-15 stsp #endif
3490 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3491 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3492 c2db6724 2019-01-04 stsp
3493 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3494 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3495 c2db6724 2019-01-04 stsp
3496 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3497 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3498 c2db6724 2019-01-04 stsp
3499 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3500 c2db6724 2019-01-04 stsp if (error != NULL)
3501 c2db6724 2019-01-04 stsp return error;
3502 c2db6724 2019-01-04 stsp
3503 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3504 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3505 c2db6724 2019-01-04 stsp
3506 c2db6724 2019-01-04 stsp return NULL;
3507 c2db6724 2019-01-04 stsp }
3508 c2db6724 2019-01-04 stsp
3509 a915003a 2019-02-05 stsp static void
3510 a915003a 2019-02-05 stsp init_curses(void)
3511 a915003a 2019-02-05 stsp {
3512 2497f032 2022-05-31 stsp /*
3513 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3514 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3515 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3516 2497f032 2022-05-31 stsp */
3517 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3518 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3519 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3520 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3521 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3522 2497f032 2022-05-31 stsp
3523 a915003a 2019-02-05 stsp initscr();
3524 a915003a 2019-02-05 stsp cbreak();
3525 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3526 a915003a 2019-02-05 stsp noecho();
3527 a915003a 2019-02-05 stsp nonl();
3528 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3529 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3530 a915003a 2019-02-05 stsp curs_set(0);
3531 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3532 6d17833f 2019-11-08 stsp start_color();
3533 6d17833f 2019-11-08 stsp use_default_colors();
3534 6d17833f 2019-11-08 stsp }
3535 a915003a 2019-02-05 stsp }
3536 a915003a 2019-02-05 stsp
3537 c2db6724 2019-01-04 stsp static const struct got_error *
3538 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3539 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3540 f135c941 2020-02-20 stsp {
3541 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3542 f135c941 2020-02-20 stsp
3543 f135c941 2020-02-20 stsp if (argc == 0) {
3544 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3545 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3546 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3547 f135c941 2020-02-20 stsp return NULL;
3548 f135c941 2020-02-20 stsp }
3549 f135c941 2020-02-20 stsp
3550 f135c941 2020-02-20 stsp if (worktree) {
3551 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3552 bfd61697 2020-11-14 stsp char *p;
3553 f135c941 2020-02-20 stsp
3554 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3555 f135c941 2020-02-20 stsp if (err)
3556 f135c941 2020-02-20 stsp return err;
3557 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3558 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3559 bfd61697 2020-11-14 stsp p) == -1) {
3560 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3561 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3562 f135c941 2020-02-20 stsp }
3563 f135c941 2020-02-20 stsp free(p);
3564 f135c941 2020-02-20 stsp } else
3565 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3566 f135c941 2020-02-20 stsp
3567 f135c941 2020-02-20 stsp return err;
3568 f135c941 2020-02-20 stsp }
3569 f135c941 2020-02-20 stsp
3570 f135c941 2020-02-20 stsp static const struct got_error *
3571 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3572 9f7d7167 2018-04-29 stsp {
3573 80ddbec8 2018-04-29 stsp const struct got_error *error;
3574 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3575 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3576 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3577 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3578 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3579 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3580 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3581 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3582 04cc582a 2018-08-01 stsp struct tog_view *view;
3583 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3584 80ddbec8 2018-04-29 stsp
3585 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3586 80ddbec8 2018-04-29 stsp switch (ch) {
3587 b672a97a 2020-01-27 stsp case 'b':
3588 b672a97a 2020-01-27 stsp log_branches = 1;
3589 b672a97a 2020-01-27 stsp break;
3590 80ddbec8 2018-04-29 stsp case 'c':
3591 80ddbec8 2018-04-29 stsp start_commit = optarg;
3592 80ddbec8 2018-04-29 stsp break;
3593 ecb28ae0 2018-07-16 stsp case 'r':
3594 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3595 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3596 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3597 9ba1d308 2019-10-21 stsp optarg);
3598 ecb28ae0 2018-07-16 stsp break;
3599 80ddbec8 2018-04-29 stsp default:
3600 17020d27 2019-03-07 stsp usage_log();
3601 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3602 80ddbec8 2018-04-29 stsp }
3603 80ddbec8 2018-04-29 stsp }
3604 80ddbec8 2018-04-29 stsp
3605 80ddbec8 2018-04-29 stsp argc -= optind;
3606 80ddbec8 2018-04-29 stsp argv += optind;
3607 80ddbec8 2018-04-29 stsp
3608 f135c941 2020-02-20 stsp if (argc > 1)
3609 f135c941 2020-02-20 stsp usage_log();
3610 963f97a1 2019-03-18 stsp
3611 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3612 0ae84acc 2022-06-15 tracey if (error != NULL)
3613 0ae84acc 2022-06-15 tracey goto done;
3614 0ae84acc 2022-06-15 tracey
3615 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3616 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3617 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3618 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3619 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3620 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3621 c156c7a4 2020-12-18 stsp goto done;
3622 a1fbf39a 2019-08-11 stsp if (worktree)
3623 6962eb72 2020-02-20 stsp repo_path =
3624 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3625 a1fbf39a 2019-08-11 stsp else
3626 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3627 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3628 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3629 c156c7a4 2020-12-18 stsp goto done;
3630 c156c7a4 2020-12-18 stsp }
3631 ecb28ae0 2018-07-16 stsp }
3632 ecb28ae0 2018-07-16 stsp
3633 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3634 80ddbec8 2018-04-29 stsp if (error != NULL)
3635 ecb28ae0 2018-07-16 stsp goto done;
3636 80ddbec8 2018-04-29 stsp
3637 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3638 f135c941 2020-02-20 stsp repo, worktree);
3639 f135c941 2020-02-20 stsp if (error)
3640 f135c941 2020-02-20 stsp goto done;
3641 f135c941 2020-02-20 stsp
3642 f135c941 2020-02-20 stsp init_curses();
3643 f135c941 2020-02-20 stsp
3644 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3645 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3646 c02c541e 2019-03-29 stsp if (error)
3647 c02c541e 2019-03-29 stsp goto done;
3648 c02c541e 2019-03-29 stsp
3649 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3650 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3651 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3652 87670572 2020-12-26 naddy if (error)
3653 87670572 2020-12-26 naddy goto done;
3654 87670572 2020-12-26 naddy }
3655 51a10b52 2020-12-26 stsp
3656 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3657 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3658 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3659 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3660 d8f38dc4 2020-12-05 stsp if (error)
3661 d8f38dc4 2020-12-05 stsp goto done;
3662 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3663 d8f38dc4 2020-12-05 stsp } else {
3664 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3665 d8f38dc4 2020-12-05 stsp if (error == NULL)
3666 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3667 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3668 d8f38dc4 2020-12-05 stsp goto done;
3669 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3670 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3671 d8f38dc4 2020-12-05 stsp if (error)
3672 d8f38dc4 2020-12-05 stsp goto done;
3673 d8f38dc4 2020-12-05 stsp }
3674 8b473291 2019-02-21 stsp
3675 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3676 04cc582a 2018-08-01 stsp if (view == NULL) {
3677 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3678 04cc582a 2018-08-01 stsp goto done;
3679 04cc582a 2018-08-01 stsp }
3680 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3681 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3682 ba4f502b 2018-08-04 stsp if (error)
3683 ba4f502b 2018-08-04 stsp goto done;
3684 2fc00ff4 2019-08-31 stsp if (worktree) {
3685 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3686 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3687 2fc00ff4 2019-08-31 stsp worktree = NULL;
3688 2fc00ff4 2019-08-31 stsp }
3689 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3690 ecb28ae0 2018-07-16 stsp done:
3691 f135c941 2020-02-20 stsp free(in_repo_path);
3692 ecb28ae0 2018-07-16 stsp free(repo_path);
3693 ecb28ae0 2018-07-16 stsp free(cwd);
3694 899d86c2 2018-05-10 stsp free(start_id);
3695 d8f38dc4 2020-12-05 stsp free(label);
3696 d8f38dc4 2020-12-05 stsp if (ref)
3697 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3698 1d0f4054 2021-06-17 stsp if (repo) {
3699 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3700 1d0f4054 2021-06-17 stsp if (error == NULL)
3701 1d0f4054 2021-06-17 stsp error = close_err;
3702 1d0f4054 2021-06-17 stsp }
3703 ec142235 2019-03-07 stsp if (worktree)
3704 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3705 0ae84acc 2022-06-15 tracey if (pack_fds) {
3706 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3707 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3708 0ae84acc 2022-06-15 tracey if (error == NULL)
3709 0ae84acc 2022-06-15 tracey error = pack_err;
3710 0ae84acc 2022-06-15 tracey }
3711 51a10b52 2020-12-26 stsp tog_free_refs();
3712 80ddbec8 2018-04-29 stsp return error;
3713 9f7d7167 2018-04-29 stsp }
3714 9f7d7167 2018-04-29 stsp
3715 4ed7e80c 2018-05-20 stsp __dead static void
3716 9f7d7167 2018-04-29 stsp usage_diff(void)
3717 9f7d7167 2018-04-29 stsp {
3718 80ddbec8 2018-04-29 stsp endwin();
3719 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3720 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3721 9f7d7167 2018-04-29 stsp exit(1);
3722 b304db33 2018-05-20 stsp }
3723 b304db33 2018-05-20 stsp
3724 6d17833f 2019-11-08 stsp static int
3725 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3726 41605754 2020-11-12 stsp regmatch_t *regmatch)
3727 6d17833f 2019-11-08 stsp {
3728 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3729 6d17833f 2019-11-08 stsp }
3730 6d17833f 2019-11-08 stsp
3731 336075a4 2022-06-25 op static struct tog_color *
3732 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3733 6d17833f 2019-11-08 stsp {
3734 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3735 6d17833f 2019-11-08 stsp
3736 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3737 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3738 6d17833f 2019-11-08 stsp return tc;
3739 6d17833f 2019-11-08 stsp }
3740 6d17833f 2019-11-08 stsp
3741 6d17833f 2019-11-08 stsp return NULL;
3742 6d17833f 2019-11-08 stsp }
3743 6d17833f 2019-11-08 stsp
3744 4ed7e80c 2018-05-20 stsp static const struct got_error *
3745 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3746 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3747 41605754 2020-11-12 stsp {
3748 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3749 44a87665 2022-06-16 stsp char *exstr = NULL;
3750 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3751 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3752 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3753 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3754 41605754 2020-11-12 stsp
3755 41605754 2020-11-12 stsp *wtotal = 0;
3756 1853e0f4 2022-06-16 stsp
3757 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3758 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3759 41605754 2020-11-12 stsp
3760 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3761 44a87665 2022-06-16 stsp if (err)
3762 44a87665 2022-06-16 stsp return err;
3763 44a87665 2022-06-16 stsp
3764 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3765 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3766 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3767 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3768 44a87665 2022-06-16 stsp goto done;
3769 44a87665 2022-06-16 stsp }
3770 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3771 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3772 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3773 1853e0f4 2022-06-16 stsp goto done;
3774 1853e0f4 2022-06-16 stsp }
3775 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3776 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3777 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3778 1853e0f4 2022-06-16 stsp goto done;
3779 1853e0f4 2022-06-16 stsp }
3780 145b6838 2022-06-16 stsp
3781 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3782 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3783 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3784 1853e0f4 2022-06-16 stsp if (err)
3785 1853e0f4 2022-06-16 stsp goto done;
3786 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3787 145b6838 2022-06-16 stsp if (n) {
3788 1853e0f4 2022-06-16 stsp free(wline);
3789 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3790 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3791 1853e0f4 2022-06-16 stsp if (err)
3792 1853e0f4 2022-06-16 stsp goto done;
3793 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3794 1853e0f4 2022-06-16 stsp wlimit -= width;
3795 1853e0f4 2022-06-16 stsp *wtotal += width;
3796 41605754 2020-11-12 stsp }
3797 41605754 2020-11-12 stsp
3798 41605754 2020-11-12 stsp if (wlimit > 0) {
3799 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3800 1853e0f4 2022-06-16 stsp size_t wlen;
3801 1853e0f4 2022-06-16 stsp
3802 1853e0f4 2022-06-16 stsp free(wline);
3803 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3804 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3805 1853e0f4 2022-06-16 stsp if (err)
3806 1853e0f4 2022-06-16 stsp goto done;
3807 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3808 1853e0f4 2022-06-16 stsp while (i < wlen) {
3809 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3810 1853e0f4 2022-06-16 stsp if (width == -1) {
3811 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3812 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3813 1853e0f4 2022-06-16 stsp goto done;
3814 1853e0f4 2022-06-16 stsp }
3815 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3816 1853e0f4 2022-06-16 stsp break;
3817 61417565 2022-06-20 mark w += width;
3818 1853e0f4 2022-06-16 stsp i++;
3819 41605754 2020-11-12 stsp }
3820 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3821 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3822 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3823 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3824 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3825 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3826 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3827 41605754 2020-11-12 stsp }
3828 41605754 2020-11-12 stsp }
3829 41605754 2020-11-12 stsp
3830 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3831 1853e0f4 2022-06-16 stsp free(wline);
3832 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3833 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3834 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3835 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3836 1853e0f4 2022-06-16 stsp if (err)
3837 1853e0f4 2022-06-16 stsp goto done;
3838 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3839 1853e0f4 2022-06-16 stsp } else {
3840 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3841 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3842 1853e0f4 2022-06-16 stsp if (err)
3843 1853e0f4 2022-06-16 stsp goto done;
3844 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3845 1853e0f4 2022-06-16 stsp }
3846 1853e0f4 2022-06-16 stsp *wtotal += width2;
3847 41605754 2020-11-12 stsp }
3848 1853e0f4 2022-06-16 stsp done:
3849 145b6838 2022-06-16 stsp free(wline);
3850 44a87665 2022-06-16 stsp free(exstr);
3851 1853e0f4 2022-06-16 stsp free(seg0);
3852 1853e0f4 2022-06-16 stsp free(seg1);
3853 1853e0f4 2022-06-16 stsp free(seg2);
3854 1853e0f4 2022-06-16 stsp return err;
3855 41605754 2020-11-12 stsp }
3856 41605754 2020-11-12 stsp
3857 41605754 2020-11-12 stsp static const struct got_error *
3858 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3859 26ed57b2 2018-05-19 stsp {
3860 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3861 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3862 61e69b96 2018-05-20 stsp const struct got_error *err;
3863 fe621944 2020-11-10 stsp int nprinted = 0;
3864 b304db33 2018-05-20 stsp char *line;
3865 826082fe 2020-12-10 stsp size_t linesize = 0;
3866 826082fe 2020-12-10 stsp ssize_t linelen;
3867 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3868 61e69b96 2018-05-20 stsp wchar_t *wline;
3869 e0b650dd 2018-05-20 stsp int width;
3870 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3871 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3872 fe621944 2020-11-10 stsp off_t line_offset;
3873 26ed57b2 2018-05-19 stsp
3874 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3875 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3876 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3877 fe621944 2020-11-10 stsp
3878 f7d12f7e 2018-08-01 stsp werase(view->window);
3879 a3404814 2018-09-02 stsp
3880 a3404814 2018-09-02 stsp if (header) {
3881 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3882 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3883 135a2da0 2020-11-11 stsp header) == -1)
3884 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3885 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3886 ccda2f4d 2022-06-16 stsp 0, 0);
3887 135a2da0 2020-11-11 stsp free(line);
3888 135a2da0 2020-11-11 stsp if (err)
3889 a3404814 2018-09-02 stsp return err;
3890 a3404814 2018-09-02 stsp
3891 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3892 a3404814 2018-09-02 stsp wstandout(view->window);
3893 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3894 e54cc94a 2020-11-11 stsp free(wline);
3895 e54cc94a 2020-11-11 stsp wline = NULL;
3896 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3897 a3404814 2018-09-02 stsp wstandend(view->window);
3898 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3899 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3900 26ed57b2 2018-05-19 stsp
3901 a3404814 2018-09-02 stsp if (max_lines <= 1)
3902 a3404814 2018-09-02 stsp return NULL;
3903 a3404814 2018-09-02 stsp max_lines--;
3904 a3404814 2018-09-02 stsp }
3905 a3404814 2018-09-02 stsp
3906 89f1a395 2020-12-01 naddy s->eof = 0;
3907 145b6838 2022-06-16 stsp view->maxx = 0;
3908 826082fe 2020-12-10 stsp line = NULL;
3909 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3910 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3911 826082fe 2020-12-10 stsp if (linelen == -1) {
3912 826082fe 2020-12-10 stsp if (feof(s->f)) {
3913 826082fe 2020-12-10 stsp s->eof = 1;
3914 826082fe 2020-12-10 stsp break;
3915 826082fe 2020-12-10 stsp }
3916 826082fe 2020-12-10 stsp free(line);
3917 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3918 61e69b96 2018-05-20 stsp }
3919 145b6838 2022-06-16 stsp
3920 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3921 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3922 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3923 1853e0f4 2022-06-16 stsp if (err) {
3924 1853e0f4 2022-06-16 stsp free(line);
3925 1853e0f4 2022-06-16 stsp return err;
3926 1853e0f4 2022-06-16 stsp }
3927 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3928 1853e0f4 2022-06-16 stsp free(wline);
3929 1853e0f4 2022-06-16 stsp wline = NULL;
3930 1853e0f4 2022-06-16 stsp
3931 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3932 f26dddb7 2019-11-08 stsp if (tc)
3933 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3934 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3935 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3936 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3937 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3938 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3939 41605754 2020-11-12 stsp if (err) {
3940 41605754 2020-11-12 stsp free(line);
3941 41605754 2020-11-12 stsp return err;
3942 41605754 2020-11-12 stsp }
3943 41605754 2020-11-12 stsp } else {
3944 969c159c 2022-06-16 stsp int skip;
3945 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3946 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3947 969c159c 2022-06-16 stsp if (err) {
3948 969c159c 2022-06-16 stsp free(line);
3949 969c159c 2022-06-16 stsp return err;
3950 969c159c 2022-06-16 stsp }
3951 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3952 969c159c 2022-06-16 stsp free(wline);
3953 41605754 2020-11-12 stsp wline = NULL;
3954 41605754 2020-11-12 stsp }
3955 f26dddb7 2019-11-08 stsp if (tc)
3956 6d17833f 2019-11-08 stsp wattr_off(view->window,
3957 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3958 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3959 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3960 fe621944 2020-11-10 stsp nprinted++;
3961 826082fe 2020-12-10 stsp }
3962 826082fe 2020-12-10 stsp free(line);
3963 fe621944 2020-11-10 stsp if (nprinted >= 1)
3964 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3965 89f1a395 2020-12-01 naddy (nprinted - 1);
3966 fe621944 2020-11-10 stsp else
3967 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3968 26ed57b2 2018-05-19 stsp
3969 9b058f45 2022-06-30 mark view_border(view);
3970 c3e9aa98 2019-05-13 jcs
3971 89f1a395 2020-12-01 naddy if (s->eof) {
3972 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3973 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3974 c3e9aa98 2019-05-13 jcs nprinted++;
3975 c3e9aa98 2019-05-13 jcs }
3976 c3e9aa98 2019-05-13 jcs
3977 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3978 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3979 c3e9aa98 2019-05-13 jcs if (err) {
3980 c3e9aa98 2019-05-13 jcs return err;
3981 c3e9aa98 2019-05-13 jcs }
3982 26ed57b2 2018-05-19 stsp
3983 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3984 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3985 e54cc94a 2020-11-11 stsp free(wline);
3986 e54cc94a 2020-11-11 stsp wline = NULL;
3987 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3988 c3e9aa98 2019-05-13 jcs }
3989 c3e9aa98 2019-05-13 jcs
3990 26ed57b2 2018-05-19 stsp return NULL;
3991 abd2672a 2018-12-23 stsp }
3992 abd2672a 2018-12-23 stsp
3993 abd2672a 2018-12-23 stsp static char *
3994 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3995 abd2672a 2018-12-23 stsp {
3996 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3997 09867e48 2019-08-13 stsp char *p, *s;
3998 09867e48 2019-08-13 stsp
3999 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4000 09867e48 2019-08-13 stsp if (tm == NULL)
4001 09867e48 2019-08-13 stsp return NULL;
4002 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4003 09867e48 2019-08-13 stsp if (s == NULL)
4004 09867e48 2019-08-13 stsp return NULL;
4005 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4006 abd2672a 2018-12-23 stsp if (p)
4007 abd2672a 2018-12-23 stsp *p = '\0';
4008 abd2672a 2018-12-23 stsp return s;
4009 9f7d7167 2018-04-29 stsp }
4010 9f7d7167 2018-04-29 stsp
4011 4ed7e80c 2018-05-20 stsp static const struct got_error *
4012 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4013 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4014 0208f208 2020-05-05 stsp {
4015 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4016 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4017 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4018 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4019 0208f208 2020-05-05 stsp
4020 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4021 0208f208 2020-05-05 stsp if (qid != NULL) {
4022 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4023 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4024 d7b5a0e8 2022-04-20 stsp &qid->id);
4025 0208f208 2020-05-05 stsp if (err)
4026 0208f208 2020-05-05 stsp return err;
4027 0208f208 2020-05-05 stsp
4028 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4029 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4030 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4031 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4032 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4033 aa8b5dd0 2021-08-01 stsp }
4034 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4035 0208f208 2020-05-05 stsp
4036 0208f208 2020-05-05 stsp }
4037 0208f208 2020-05-05 stsp
4038 0208f208 2020-05-05 stsp if (tree_id1) {
4039 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4040 0208f208 2020-05-05 stsp if (err)
4041 0208f208 2020-05-05 stsp goto done;
4042 0208f208 2020-05-05 stsp }
4043 0208f208 2020-05-05 stsp
4044 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4045 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4046 0208f208 2020-05-05 stsp if (err)
4047 0208f208 2020-05-05 stsp goto done;
4048 0208f208 2020-05-05 stsp
4049 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4050 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4051 0208f208 2020-05-05 stsp done:
4052 0208f208 2020-05-05 stsp if (tree1)
4053 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4054 0208f208 2020-05-05 stsp if (tree2)
4055 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4056 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4057 0208f208 2020-05-05 stsp return err;
4058 0208f208 2020-05-05 stsp }
4059 0208f208 2020-05-05 stsp
4060 0208f208 2020-05-05 stsp static const struct got_error *
4061 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4062 abd2672a 2018-12-23 stsp {
4063 fe621944 2020-11-10 stsp off_t *p;
4064 fe621944 2020-11-10 stsp
4065 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4066 fe621944 2020-11-10 stsp if (p == NULL)
4067 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4068 fe621944 2020-11-10 stsp *line_offsets = p;
4069 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4070 fe621944 2020-11-10 stsp (*nlines)++;
4071 fe621944 2020-11-10 stsp return NULL;
4072 fe621944 2020-11-10 stsp }
4073 fe621944 2020-11-10 stsp
4074 fe621944 2020-11-10 stsp static const struct got_error *
4075 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4076 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4077 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4078 fe621944 2020-11-10 stsp {
4079 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4080 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4081 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4082 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4083 45d799e2 2018-12-23 stsp time_t committer_time;
4084 45d799e2 2018-12-23 stsp const char *author, *committer;
4085 8b473291 2019-02-21 stsp char *refs_str = NULL;
4086 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4087 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4088 fe621944 2020-11-10 stsp off_t outoff = 0;
4089 fe621944 2020-11-10 stsp int n;
4090 abd2672a 2018-12-23 stsp
4091 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4092 0208f208 2020-05-05 stsp
4093 8b473291 2019-02-21 stsp if (refs) {
4094 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4095 8b473291 2019-02-21 stsp if (err)
4096 8b473291 2019-02-21 stsp return err;
4097 8b473291 2019-02-21 stsp }
4098 8b473291 2019-02-21 stsp
4099 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4100 abd2672a 2018-12-23 stsp if (err)
4101 abd2672a 2018-12-23 stsp return err;
4102 abd2672a 2018-12-23 stsp
4103 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4104 15a94983 2018-12-23 stsp if (err) {
4105 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4106 15a94983 2018-12-23 stsp goto done;
4107 15a94983 2018-12-23 stsp }
4108 abd2672a 2018-12-23 stsp
4109 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4110 fe621944 2020-11-10 stsp if (err)
4111 fe621944 2020-11-10 stsp goto done;
4112 fe621944 2020-11-10 stsp
4113 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4114 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4115 fe621944 2020-11-10 stsp if (n < 0) {
4116 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4117 abd2672a 2018-12-23 stsp goto done;
4118 abd2672a 2018-12-23 stsp }
4119 fe621944 2020-11-10 stsp outoff += n;
4120 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4121 fe621944 2020-11-10 stsp if (err)
4122 fe621944 2020-11-10 stsp goto done;
4123 fe621944 2020-11-10 stsp
4124 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4125 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4126 fe621944 2020-11-10 stsp if (n < 0) {
4127 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4128 abd2672a 2018-12-23 stsp goto done;
4129 abd2672a 2018-12-23 stsp }
4130 fe621944 2020-11-10 stsp outoff += n;
4131 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4132 fe621944 2020-11-10 stsp if (err)
4133 fe621944 2020-11-10 stsp goto done;
4134 fe621944 2020-11-10 stsp
4135 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4136 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4137 fe621944 2020-11-10 stsp if (datestr) {
4138 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4139 fe621944 2020-11-10 stsp if (n < 0) {
4140 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4141 fe621944 2020-11-10 stsp goto done;
4142 fe621944 2020-11-10 stsp }
4143 fe621944 2020-11-10 stsp outoff += n;
4144 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4145 fe621944 2020-11-10 stsp if (err)
4146 fe621944 2020-11-10 stsp goto done;
4147 abd2672a 2018-12-23 stsp }
4148 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4149 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4150 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4151 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4152 fe621944 2020-11-10 stsp if (n < 0) {
4153 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4154 fe621944 2020-11-10 stsp goto done;
4155 fe621944 2020-11-10 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 abd2672a 2018-12-23 stsp }
4161 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4162 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4163 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4164 9f98ca05 2021-09-24 stsp int pn = 1;
4165 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4166 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4167 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4168 9f98ca05 2021-09-24 stsp if (err)
4169 9f98ca05 2021-09-24 stsp goto done;
4170 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4171 9f98ca05 2021-09-24 stsp if (n < 0) {
4172 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4173 9f98ca05 2021-09-24 stsp goto done;
4174 9f98ca05 2021-09-24 stsp }
4175 9f98ca05 2021-09-24 stsp outoff += n;
4176 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4177 9f98ca05 2021-09-24 stsp if (err)
4178 9f98ca05 2021-09-24 stsp goto done;
4179 9f98ca05 2021-09-24 stsp free(id_str);
4180 9f98ca05 2021-09-24 stsp id_str = NULL;
4181 9f98ca05 2021-09-24 stsp }
4182 9f98ca05 2021-09-24 stsp }
4183 9f98ca05 2021-09-24 stsp
4184 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4185 5943eee2 2019-08-13 stsp if (err)
4186 5943eee2 2019-08-13 stsp goto done;
4187 fe621944 2020-11-10 stsp s = logmsg;
4188 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4189 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4190 fe621944 2020-11-10 stsp if (n < 0) {
4191 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4192 fe621944 2020-11-10 stsp goto done;
4193 fe621944 2020-11-10 stsp }
4194 fe621944 2020-11-10 stsp outoff += n;
4195 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4196 fe621944 2020-11-10 stsp if (err)
4197 fe621944 2020-11-10 stsp goto done;
4198 abd2672a 2018-12-23 stsp }
4199 fe621944 2020-11-10 stsp
4200 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4201 0208f208 2020-05-05 stsp if (err)
4202 0208f208 2020-05-05 stsp goto done;
4203 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4204 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4205 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4206 fe621944 2020-11-10 stsp if (n < 0) {
4207 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4208 fe621944 2020-11-10 stsp goto done;
4209 fe621944 2020-11-10 stsp }
4210 fe621944 2020-11-10 stsp outoff += n;
4211 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4212 fe621944 2020-11-10 stsp if (err)
4213 fe621944 2020-11-10 stsp goto done;
4214 0208f208 2020-05-05 stsp free((char *)pe->path);
4215 0208f208 2020-05-05 stsp free(pe->data);
4216 0208f208 2020-05-05 stsp }
4217 fe621944 2020-11-10 stsp
4218 0208f208 2020-05-05 stsp fputc('\n', outfile);
4219 fe621944 2020-11-10 stsp outoff++;
4220 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4221 abd2672a 2018-12-23 stsp done:
4222 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4223 abd2672a 2018-12-23 stsp free(id_str);
4224 5943eee2 2019-08-13 stsp free(logmsg);
4225 8b473291 2019-02-21 stsp free(refs_str);
4226 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4227 7510f233 2020-08-09 stsp if (err) {
4228 ae6a6978 2020-08-09 stsp free(*line_offsets);
4229 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4230 ae6a6978 2020-08-09 stsp *nlines = 0;
4231 7510f233 2020-08-09 stsp }
4232 fe621944 2020-11-10 stsp return err;
4233 abd2672a 2018-12-23 stsp }
4234 abd2672a 2018-12-23 stsp
4235 abd2672a 2018-12-23 stsp static const struct got_error *
4236 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4237 26ed57b2 2018-05-19 stsp {
4238 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4239 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4240 15a94983 2018-12-23 stsp int obj_type;
4241 fe621944 2020-11-10 stsp
4242 fe621944 2020-11-10 stsp free(s->line_offsets);
4243 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4244 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4245 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4246 fe621944 2020-11-10 stsp s->nlines = 0;
4247 26ed57b2 2018-05-19 stsp
4248 511a516b 2018-05-19 stsp f = got_opentemp();
4249 48ae06ee 2018-10-18 stsp if (f == NULL) {
4250 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4251 48ae06ee 2018-10-18 stsp goto done;
4252 48ae06ee 2018-10-18 stsp }
4253 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4254 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4255 fb43ecf1 2019-02-11 stsp goto done;
4256 fb43ecf1 2019-02-11 stsp }
4257 48ae06ee 2018-10-18 stsp s->f = f;
4258 26ed57b2 2018-05-19 stsp
4259 15a94983 2018-12-23 stsp if (s->id1)
4260 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4261 15a94983 2018-12-23 stsp else
4262 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4263 15a94983 2018-12-23 stsp if (err)
4264 15a94983 2018-12-23 stsp goto done;
4265 15a94983 2018-12-23 stsp
4266 15a94983 2018-12-23 stsp switch (obj_type) {
4267 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4268 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4269 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4270 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4271 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4272 26ed57b2 2018-05-19 stsp break;
4273 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4274 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4275 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4276 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4277 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4278 26ed57b2 2018-05-19 stsp break;
4279 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4280 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4281 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4282 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4283 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4284 abd2672a 2018-12-23 stsp
4285 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4286 abd2672a 2018-12-23 stsp if (err)
4287 3ffacbe1 2020-02-02 tracey goto done;
4288 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4289 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4290 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4291 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4292 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4293 f44b1f58 2020-02-02 tracey if (err)
4294 f44b1f58 2020-02-02 tracey goto done;
4295 f44b1f58 2020-02-02 tracey } else {
4296 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4297 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4298 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4299 fe621944 2020-11-10 stsp err = write_commit_info(
4300 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4301 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4302 f44b1f58 2020-02-02 tracey if (err)
4303 f44b1f58 2020-02-02 tracey goto done;
4304 f5404e4e 2020-02-02 tracey break;
4305 15a087fe 2019-02-21 stsp }
4306 abd2672a 2018-12-23 stsp }
4307 abd2672a 2018-12-23 stsp }
4308 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4309 abd2672a 2018-12-23 stsp
4310 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4311 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4312 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4313 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4314 26ed57b2 2018-05-19 stsp break;
4315 abd2672a 2018-12-23 stsp }
4316 26ed57b2 2018-05-19 stsp default:
4317 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4318 48ae06ee 2018-10-18 stsp break;
4319 26ed57b2 2018-05-19 stsp }
4320 f44b1f58 2020-02-02 tracey if (err)
4321 f44b1f58 2020-02-02 tracey goto done;
4322 48ae06ee 2018-10-18 stsp done:
4323 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4324 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4325 48ae06ee 2018-10-18 stsp return err;
4326 48ae06ee 2018-10-18 stsp }
4327 26ed57b2 2018-05-19 stsp
4328 f5215bb9 2019-02-22 stsp static void
4329 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4330 f5215bb9 2019-02-22 stsp {
4331 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4332 f5215bb9 2019-02-22 stsp update_panels();
4333 f5215bb9 2019-02-22 stsp doupdate();
4334 f44b1f58 2020-02-02 tracey }
4335 f44b1f58 2020-02-02 tracey
4336 f44b1f58 2020-02-02 tracey static const struct got_error *
4337 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4338 f44b1f58 2020-02-02 tracey {
4339 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4340 f44b1f58 2020-02-02 tracey
4341 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4342 f44b1f58 2020-02-02 tracey return NULL;
4343 f44b1f58 2020-02-02 tracey }
4344 f44b1f58 2020-02-02 tracey
4345 f44b1f58 2020-02-02 tracey static const struct got_error *
4346 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4347 f44b1f58 2020-02-02 tracey {
4348 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4349 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4350 f44b1f58 2020-02-02 tracey int lineno;
4351 cb713507 2022-06-16 stsp char *line = NULL;
4352 826082fe 2020-12-10 stsp size_t linesize = 0;
4353 826082fe 2020-12-10 stsp ssize_t linelen;
4354 f44b1f58 2020-02-02 tracey
4355 f44b1f58 2020-02-02 tracey if (!view->searching) {
4356 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4357 f44b1f58 2020-02-02 tracey return NULL;
4358 f44b1f58 2020-02-02 tracey }
4359 f44b1f58 2020-02-02 tracey
4360 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4361 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4362 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4363 f44b1f58 2020-02-02 tracey else
4364 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4365 487cd7d2 2021-12-17 stsp } else
4366 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4367 f44b1f58 2020-02-02 tracey
4368 f44b1f58 2020-02-02 tracey while (1) {
4369 f44b1f58 2020-02-02 tracey off_t offset;
4370 f44b1f58 2020-02-02 tracey
4371 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4372 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4373 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4374 f44b1f58 2020-02-02 tracey break;
4375 f44b1f58 2020-02-02 tracey }
4376 f44b1f58 2020-02-02 tracey
4377 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4378 f44b1f58 2020-02-02 tracey lineno = 1;
4379 f44b1f58 2020-02-02 tracey else
4380 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4381 f44b1f58 2020-02-02 tracey }
4382 f44b1f58 2020-02-02 tracey
4383 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4384 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4385 f44b1f58 2020-02-02 tracey free(line);
4386 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4387 f44b1f58 2020-02-02 tracey }
4388 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4389 cb713507 2022-06-16 stsp if (linelen != -1) {
4390 cb713507 2022-06-16 stsp char *exstr;
4391 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4392 cb713507 2022-06-16 stsp if (err)
4393 cb713507 2022-06-16 stsp break;
4394 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4395 cb713507 2022-06-16 stsp &view->regmatch)) {
4396 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4397 cb713507 2022-06-16 stsp s->matched_line = lineno;
4398 cb713507 2022-06-16 stsp free(exstr);
4399 cb713507 2022-06-16 stsp break;
4400 cb713507 2022-06-16 stsp }
4401 cb713507 2022-06-16 stsp free(exstr);
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++;
4405 f44b1f58 2020-02-02 tracey else
4406 f44b1f58 2020-02-02 tracey lineno--;
4407 f44b1f58 2020-02-02 tracey }
4408 826082fe 2020-12-10 stsp free(line);
4409 f44b1f58 2020-02-02 tracey
4410 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4411 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4412 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4413 f44b1f58 2020-02-02 tracey }
4414 f44b1f58 2020-02-02 tracey
4415 145b6838 2022-06-16 stsp return err;
4416 f5215bb9 2019-02-22 stsp }
4417 f5215bb9 2019-02-22 stsp
4418 48ae06ee 2018-10-18 stsp static const struct got_error *
4419 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4420 b72706c3 2022-06-01 stsp {
4421 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4422 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4423 b72706c3 2022-06-01 stsp
4424 b72706c3 2022-06-01 stsp free(s->id1);
4425 b72706c3 2022-06-01 stsp s->id1 = NULL;
4426 b72706c3 2022-06-01 stsp free(s->id2);
4427 b72706c3 2022-06-01 stsp s->id2 = NULL;
4428 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4429 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4430 b72706c3 2022-06-01 stsp s->f = NULL;
4431 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4432 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4433 b72706c3 2022-06-01 stsp s->f1 = NULL;
4434 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4435 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4436 b72706c3 2022-06-01 stsp s->f2 = NULL;
4437 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4438 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4439 f9d37699 2022-06-28 stsp s->fd1 = -1;
4440 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4441 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4442 f9d37699 2022-06-28 stsp s->fd2 = -1;
4443 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4444 b72706c3 2022-06-01 stsp free(s->line_offsets);
4445 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4446 b72706c3 2022-06-01 stsp s->nlines = 0;
4447 b72706c3 2022-06-01 stsp return err;
4448 b72706c3 2022-06-01 stsp }
4449 b72706c3 2022-06-01 stsp
4450 b72706c3 2022-06-01 stsp static const struct got_error *
4451 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4452 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4453 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4454 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4455 48ae06ee 2018-10-18 stsp {
4456 48ae06ee 2018-10-18 stsp const struct got_error *err;
4457 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4458 5dc9f4bc 2018-08-04 stsp
4459 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4460 f9d37699 2022-06-28 stsp s->fd1 = -1;
4461 f9d37699 2022-06-28 stsp s->fd2 = -1;
4462 b72706c3 2022-06-01 stsp
4463 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4464 15a94983 2018-12-23 stsp int type1, type2;
4465 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4466 15a94983 2018-12-23 stsp if (err)
4467 15a94983 2018-12-23 stsp return err;
4468 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4469 15a94983 2018-12-23 stsp if (err)
4470 15a94983 2018-12-23 stsp return err;
4471 15a94983 2018-12-23 stsp
4472 15a94983 2018-12-23 stsp if (type1 != type2)
4473 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4474 15a94983 2018-12-23 stsp }
4475 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4476 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4477 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4478 f44b1f58 2020-02-02 tracey s->repo = repo;
4479 f44b1f58 2020-02-02 tracey s->id1 = id1;
4480 f44b1f58 2020-02-02 tracey s->id2 = id2;
4481 3dbaef42 2020-11-24 stsp s->label1 = label1;
4482 3dbaef42 2020-11-24 stsp s->label2 = label2;
4483 48ae06ee 2018-10-18 stsp
4484 15a94983 2018-12-23 stsp if (id1) {
4485 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4486 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4487 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4488 48ae06ee 2018-10-18 stsp } else
4489 5465d566 2020-02-01 tracey s->id1 = NULL;
4490 48ae06ee 2018-10-18 stsp
4491 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4492 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4493 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4494 b72706c3 2022-06-01 stsp goto done;
4495 48ae06ee 2018-10-18 stsp }
4496 b72706c3 2022-06-01 stsp
4497 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4498 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4499 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4500 a00719e9 2022-06-17 stsp goto done;
4501 a00719e9 2022-06-17 stsp }
4502 a00719e9 2022-06-17 stsp
4503 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4504 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4505 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4506 b72706c3 2022-06-01 stsp goto done;
4507 b72706c3 2022-06-01 stsp }
4508 b72706c3 2022-06-01 stsp
4509 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4510 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4511 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4512 f9d37699 2022-06-28 stsp goto done;
4513 f9d37699 2022-06-28 stsp }
4514 f9d37699 2022-06-28 stsp
4515 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4516 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4517 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4518 f9d37699 2022-06-28 stsp goto done;
4519 f9d37699 2022-06-28 stsp }
4520 f9d37699 2022-06-28 stsp
4521 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4522 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4523 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4524 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4525 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4526 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4527 5465d566 2020-02-01 tracey s->repo = repo;
4528 6d17833f 2019-11-08 stsp
4529 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4530 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4531 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4532 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4533 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4534 6d17833f 2019-11-08 stsp if (err)
4535 b72706c3 2022-06-01 stsp goto done;
4536 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4537 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4538 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4539 b72706c3 2022-06-01 stsp if (err)
4540 b72706c3 2022-06-01 stsp goto done;
4541 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4542 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4543 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4544 b72706c3 2022-06-01 stsp if (err)
4545 b72706c3 2022-06-01 stsp goto done;
4546 6d17833f 2019-11-08 stsp
4547 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4548 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4549 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4550 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4551 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4552 b72706c3 2022-06-01 stsp if (err)
4553 b72706c3 2022-06-01 stsp goto done;
4554 11b20872 2019-11-08 stsp
4555 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4556 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4557 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4558 b72706c3 2022-06-01 stsp if (err)
4559 b72706c3 2022-06-01 stsp goto done;
4560 11b20872 2019-11-08 stsp
4561 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4562 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4563 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4564 b72706c3 2022-06-01 stsp if (err)
4565 b72706c3 2022-06-01 stsp goto done;
4566 6d17833f 2019-11-08 stsp }
4567 5dc9f4bc 2018-08-04 stsp
4568 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4569 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4570 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4571 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4572 f5215bb9 2019-02-22 stsp
4573 5465d566 2020-02-01 tracey err = create_diff(s);
4574 48ae06ee 2018-10-18 stsp
4575 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4576 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4577 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4578 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4579 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4580 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4581 b72706c3 2022-06-01 stsp done:
4582 b72706c3 2022-06-01 stsp if (err)
4583 b72706c3 2022-06-01 stsp close_diff_view(view);
4584 e5a0f69f 2018-08-18 stsp return err;
4585 5dc9f4bc 2018-08-04 stsp }
4586 5dc9f4bc 2018-08-04 stsp
4587 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4588 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4589 5dc9f4bc 2018-08-04 stsp {
4590 a3404814 2018-09-02 stsp const struct got_error *err;
4591 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4592 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4593 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4594 a3404814 2018-09-02 stsp
4595 a3404814 2018-09-02 stsp if (s->id1) {
4596 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4597 a3404814 2018-09-02 stsp if (err)
4598 a3404814 2018-09-02 stsp return err;
4599 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4600 3dbaef42 2020-11-24 stsp } else
4601 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4602 3dbaef42 2020-11-24 stsp
4603 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4604 a3404814 2018-09-02 stsp if (err)
4605 a3404814 2018-09-02 stsp return err;
4606 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4607 26ed57b2 2018-05-19 stsp
4608 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4609 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4610 a3404814 2018-09-02 stsp free(id_str1);
4611 a3404814 2018-09-02 stsp free(id_str2);
4612 a3404814 2018-09-02 stsp return err;
4613 a3404814 2018-09-02 stsp }
4614 a3404814 2018-09-02 stsp free(id_str1);
4615 a3404814 2018-09-02 stsp free(id_str2);
4616 a3404814 2018-09-02 stsp
4617 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4618 267bb3b8 2021-08-01 stsp free(header);
4619 267bb3b8 2021-08-01 stsp return err;
4620 15a087fe 2019-02-21 stsp }
4621 15a087fe 2019-02-21 stsp
4622 15a087fe 2019-02-21 stsp static const struct got_error *
4623 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4624 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4625 15a087fe 2019-02-21 stsp {
4626 d7a04538 2019-02-21 stsp const struct got_error *err;
4627 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4628 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4629 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4630 15a087fe 2019-02-21 stsp
4631 15a087fe 2019-02-21 stsp free(s->id2);
4632 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4633 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4634 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4635 15a087fe 2019-02-21 stsp
4636 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4637 d7a04538 2019-02-21 stsp if (err)
4638 d7a04538 2019-02-21 stsp return err;
4639 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4640 15a087fe 2019-02-21 stsp free(s->id1);
4641 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4642 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4643 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4644 15a087fe 2019-02-21 stsp return NULL;
4645 0cf4efb1 2018-09-29 stsp }
4646 0cf4efb1 2018-09-29 stsp
4647 0cf4efb1 2018-09-29 stsp static const struct got_error *
4648 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4649 917d79a7 2022-07-01 stsp {
4650 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4651 917d79a7 2022-07-01 stsp
4652 917d79a7 2022-07-01 stsp view->count = 0;
4653 917d79a7 2022-07-01 stsp wclear(view->window);
4654 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4655 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4656 917d79a7 2022-07-01 stsp s->matched_line = 0;
4657 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4658 917d79a7 2022-07-01 stsp return create_diff(s);
4659 917d79a7 2022-07-01 stsp }
4660 917d79a7 2022-07-01 stsp
4661 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4662 c0f61fa4 2022-07-11 mark int, int, int);
4663 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4664 c0f61fa4 2022-07-11 mark int, int);
4665 c0f61fa4 2022-07-11 mark
4666 917d79a7 2022-07-01 stsp static const struct got_error *
4667 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4668 e5a0f69f 2018-08-18 stsp {
4669 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4670 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4671 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4672 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4673 826082fe 2020-12-10 stsp char *line = NULL;
4674 826082fe 2020-12-10 stsp size_t linesize = 0;
4675 826082fe 2020-12-10 stsp ssize_t linelen;
4676 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4677 e5a0f69f 2018-08-18 stsp
4678 e5a0f69f 2018-08-18 stsp switch (ch) {
4679 145b6838 2022-06-16 stsp case '0':
4680 145b6838 2022-06-16 stsp view->x = 0;
4681 145b6838 2022-06-16 stsp break;
4682 145b6838 2022-06-16 stsp case '$':
4683 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4684 640cd7ff 2022-06-22 mark view->count = 0;
4685 145b6838 2022-06-16 stsp break;
4686 145b6838 2022-06-16 stsp case KEY_RIGHT:
4687 145b6838 2022-06-16 stsp case 'l':
4688 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4689 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4690 640cd7ff 2022-06-22 mark else
4691 640cd7ff 2022-06-22 mark view->count = 0;
4692 145b6838 2022-06-16 stsp break;
4693 145b6838 2022-06-16 stsp case KEY_LEFT:
4694 145b6838 2022-06-16 stsp case 'h':
4695 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4696 640cd7ff 2022-06-22 mark if (view->x <= 0)
4697 640cd7ff 2022-06-22 mark view->count = 0;
4698 145b6838 2022-06-16 stsp break;
4699 64453f7e 2020-11-21 stsp case 'a':
4700 3dbaef42 2020-11-24 stsp case 'w':
4701 3dbaef42 2020-11-24 stsp if (ch == 'a')
4702 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4703 3dbaef42 2020-11-24 stsp if (ch == 'w')
4704 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4705 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4706 912a3f79 2021-08-30 j break;
4707 912a3f79 2021-08-30 j case 'g':
4708 912a3f79 2021-08-30 j case KEY_HOME:
4709 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4710 640cd7ff 2022-06-22 mark view->count = 0;
4711 64453f7e 2020-11-21 stsp break;
4712 912a3f79 2021-08-30 j case 'G':
4713 912a3f79 2021-08-30 j case KEY_END:
4714 640cd7ff 2022-06-22 mark view->count = 0;
4715 912a3f79 2021-08-30 j if (s->eof)
4716 912a3f79 2021-08-30 j break;
4717 912a3f79 2021-08-30 j
4718 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4719 912a3f79 2021-08-30 j s->eof = 1;
4720 912a3f79 2021-08-30 j break;
4721 1e37a5c2 2019-05-12 jcs case 'k':
4722 1e37a5c2 2019-05-12 jcs case KEY_UP:
4723 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4724 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4725 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4726 640cd7ff 2022-06-22 mark else
4727 640cd7ff 2022-06-22 mark view->count = 0;
4728 1e37a5c2 2019-05-12 jcs break;
4729 83cc4199 2022-06-13 stsp case CTRL('u'):
4730 33c3719a 2022-06-15 stsp case 'u':
4731 83cc4199 2022-06-13 stsp nscroll /= 2;
4732 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4733 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4734 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4735 61417565 2022-06-20 mark case 'b':
4736 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4737 640cd7ff 2022-06-22 mark view->count = 0;
4738 26ed57b2 2018-05-19 stsp break;
4739 640cd7ff 2022-06-22 mark }
4740 1e37a5c2 2019-05-12 jcs i = 0;
4741 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4742 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4743 1e37a5c2 2019-05-12 jcs break;
4744 1e37a5c2 2019-05-12 jcs case 'j':
4745 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4746 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4747 1e37a5c2 2019-05-12 jcs if (!s->eof)
4748 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4749 640cd7ff 2022-06-22 mark else
4750 640cd7ff 2022-06-22 mark view->count = 0;
4751 1e37a5c2 2019-05-12 jcs break;
4752 83cc4199 2022-06-13 stsp case CTRL('d'):
4753 33c3719a 2022-06-15 stsp case 'd':
4754 83cc4199 2022-06-13 stsp nscroll /= 2;
4755 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4756 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4757 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4758 61417565 2022-06-20 mark case 'f':
4759 1e37a5c2 2019-05-12 jcs case ' ':
4760 640cd7ff 2022-06-22 mark if (s->eof) {
4761 640cd7ff 2022-06-22 mark view->count = 0;
4762 1e37a5c2 2019-05-12 jcs break;
4763 640cd7ff 2022-06-22 mark }
4764 1e37a5c2 2019-05-12 jcs i = 0;
4765 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4766 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4767 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4768 826082fe 2020-12-10 stsp if (linelen == -1) {
4769 826082fe 2020-12-10 stsp if (feof(s->f)) {
4770 826082fe 2020-12-10 stsp s->eof = 1;
4771 826082fe 2020-12-10 stsp } else
4772 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4773 34bc9ec9 2019-02-22 stsp break;
4774 826082fe 2020-12-10 stsp }
4775 1e37a5c2 2019-05-12 jcs }
4776 826082fe 2020-12-10 stsp free(line);
4777 1e37a5c2 2019-05-12 jcs break;
4778 1e37a5c2 2019-05-12 jcs case '[':
4779 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4780 1e37a5c2 2019-05-12 jcs s->diff_context--;
4781 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4782 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4783 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4784 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4785 27829c9e 2020-11-21 stsp s->nlines) {
4786 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4787 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4788 27829c9e 2020-11-21 stsp }
4789 640cd7ff 2022-06-22 mark } else
4790 640cd7ff 2022-06-22 mark view->count = 0;
4791 1e37a5c2 2019-05-12 jcs break;
4792 1e37a5c2 2019-05-12 jcs case ']':
4793 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4794 1e37a5c2 2019-05-12 jcs s->diff_context++;
4795 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4796 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4797 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4798 640cd7ff 2022-06-22 mark } else
4799 640cd7ff 2022-06-22 mark view->count = 0;
4800 1e37a5c2 2019-05-12 jcs break;
4801 1e37a5c2 2019-05-12 jcs case '<':
4802 1e37a5c2 2019-05-12 jcs case ',':
4803 c0f61fa4 2022-07-11 mark up = 1;
4804 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4805 c0f61fa4 2022-07-11 mark case '>':
4806 c0f61fa4 2022-07-11 mark case '.':
4807 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4808 640cd7ff 2022-06-22 mark view->count = 0;
4809 48ae06ee 2018-10-18 stsp break;
4810 640cd7ff 2022-06-22 mark }
4811 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4812 6524637e 2019-02-21 stsp
4813 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4814 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4815 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4816 15a087fe 2019-02-21 stsp
4817 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4818 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4819 c0f61fa4 2022-07-11 mark if (err)
4820 c0f61fa4 2022-07-11 mark break;
4821 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4822 15a087fe 2019-02-21 stsp
4823 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4824 c0f61fa4 2022-07-11 mark break;
4825 15a087fe 2019-02-21 stsp
4826 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4827 c0f61fa4 2022-07-11 mark if (err)
4828 c0f61fa4 2022-07-11 mark break;
4829 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4830 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4831 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4832 5e224a3e 2019-02-22 stsp
4833 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4834 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4835 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4836 c0f61fa4 2022-07-11 mark
4837 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4838 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4839 c0f61fa4 2022-07-11 mark if (err)
4840 c0f61fa4 2022-07-11 mark break;
4841 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4842 5a8b5076 2020-12-05 stsp
4843 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4844 c0f61fa4 2022-07-11 mark break;
4845 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4846 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4847 c0f61fa4 2022-07-11 mark bs->selected_line);
4848 c0f61fa4 2022-07-11 mark if (id == NULL)
4849 c0f61fa4 2022-07-11 mark break;
4850 15a087fe 2019-02-21 stsp
4851 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4852 c0f61fa4 2022-07-11 mark break;
4853 15a087fe 2019-02-21 stsp
4854 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4855 c0f61fa4 2022-07-11 mark if (err)
4856 c0f61fa4 2022-07-11 mark break;
4857 c0f61fa4 2022-07-11 mark }
4858 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4859 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4860 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4861 145b6838 2022-06-16 stsp view->x = 0;
4862 1e37a5c2 2019-05-12 jcs
4863 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4864 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4865 1e37a5c2 2019-05-12 jcs break;
4866 1e37a5c2 2019-05-12 jcs default:
4867 640cd7ff 2022-06-22 mark view->count = 0;
4868 1e37a5c2 2019-05-12 jcs break;
4869 26ed57b2 2018-05-19 stsp }
4870 e5a0f69f 2018-08-18 stsp
4871 bcbd79e2 2018-08-19 stsp return err;
4872 26ed57b2 2018-05-19 stsp }
4873 26ed57b2 2018-05-19 stsp
4874 4ed7e80c 2018-05-20 stsp static const struct got_error *
4875 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4876 9f7d7167 2018-04-29 stsp {
4877 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4878 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4879 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4880 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4881 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4882 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4883 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4884 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4885 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4886 3dbaef42 2020-11-24 stsp const char *errstr;
4887 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4888 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4889 70ac5f84 2019-03-28 stsp
4890 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4891 26ed57b2 2018-05-19 stsp switch (ch) {
4892 64453f7e 2020-11-21 stsp case 'a':
4893 64453f7e 2020-11-21 stsp force_text_diff = 1;
4894 3dbaef42 2020-11-24 stsp break;
4895 3dbaef42 2020-11-24 stsp case 'C':
4896 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4897 3dbaef42 2020-11-24 stsp &errstr);
4898 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4899 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4900 5a20d08d 2022-02-09 op errstr, errstr);
4901 64453f7e 2020-11-21 stsp break;
4902 09b5bff8 2020-02-23 naddy case 'r':
4903 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4904 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4905 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4906 09b5bff8 2020-02-23 naddy optarg);
4907 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4908 09b5bff8 2020-02-23 naddy break;
4909 3dbaef42 2020-11-24 stsp case 'w':
4910 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4911 3dbaef42 2020-11-24 stsp break;
4912 26ed57b2 2018-05-19 stsp default:
4913 17020d27 2019-03-07 stsp usage_diff();
4914 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4915 26ed57b2 2018-05-19 stsp }
4916 26ed57b2 2018-05-19 stsp }
4917 26ed57b2 2018-05-19 stsp
4918 26ed57b2 2018-05-19 stsp argc -= optind;
4919 26ed57b2 2018-05-19 stsp argv += optind;
4920 26ed57b2 2018-05-19 stsp
4921 26ed57b2 2018-05-19 stsp if (argc == 0) {
4922 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4923 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4924 15a94983 2018-12-23 stsp id_str1 = argv[0];
4925 15a94983 2018-12-23 stsp id_str2 = argv[1];
4926 26ed57b2 2018-05-19 stsp } else
4927 26ed57b2 2018-05-19 stsp usage_diff();
4928 eb6600df 2019-01-04 stsp
4929 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4930 0ae84acc 2022-06-15 tracey if (error)
4931 0ae84acc 2022-06-15 tracey goto done;
4932 0ae84acc 2022-06-15 tracey
4933 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4934 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4935 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4936 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4937 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4938 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4939 c156c7a4 2020-12-18 stsp goto done;
4940 a273ac94 2020-02-23 naddy if (worktree)
4941 a273ac94 2020-02-23 naddy repo_path =
4942 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4943 a273ac94 2020-02-23 naddy else
4944 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4945 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4946 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4947 c156c7a4 2020-12-18 stsp goto done;
4948 c156c7a4 2020-12-18 stsp }
4949 a273ac94 2020-02-23 naddy }
4950 a273ac94 2020-02-23 naddy
4951 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4952 eb6600df 2019-01-04 stsp if (error)
4953 eb6600df 2019-01-04 stsp goto done;
4954 26ed57b2 2018-05-19 stsp
4955 a273ac94 2020-02-23 naddy init_curses();
4956 a273ac94 2020-02-23 naddy
4957 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4958 51a10b52 2020-12-26 stsp if (error)
4959 51a10b52 2020-12-26 stsp goto done;
4960 51a10b52 2020-12-26 stsp
4961 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4962 26ed57b2 2018-05-19 stsp if (error)
4963 26ed57b2 2018-05-19 stsp goto done;
4964 26ed57b2 2018-05-19 stsp
4965 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4966 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4967 26ed57b2 2018-05-19 stsp if (error)
4968 26ed57b2 2018-05-19 stsp goto done;
4969 26ed57b2 2018-05-19 stsp
4970 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4971 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4972 26ed57b2 2018-05-19 stsp if (error)
4973 26ed57b2 2018-05-19 stsp goto done;
4974 26ed57b2 2018-05-19 stsp
4975 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4976 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4977 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4978 ea5e7bb5 2018-08-01 stsp goto done;
4979 ea5e7bb5 2018-08-01 stsp }
4980 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4981 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4982 5dc9f4bc 2018-08-04 stsp if (error)
4983 5dc9f4bc 2018-08-04 stsp goto done;
4984 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4985 26ed57b2 2018-05-19 stsp done:
4986 3dbaef42 2020-11-24 stsp free(label1);
4987 3dbaef42 2020-11-24 stsp free(label2);
4988 c02c541e 2019-03-29 stsp free(repo_path);
4989 a273ac94 2020-02-23 naddy free(cwd);
4990 1d0f4054 2021-06-17 stsp if (repo) {
4991 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4992 1d0f4054 2021-06-17 stsp if (error == NULL)
4993 1d0f4054 2021-06-17 stsp error = close_err;
4994 1d0f4054 2021-06-17 stsp }
4995 a273ac94 2020-02-23 naddy if (worktree)
4996 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4997 0ae84acc 2022-06-15 tracey if (pack_fds) {
4998 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4999 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5000 0ae84acc 2022-06-15 tracey if (error == NULL)
5001 0ae84acc 2022-06-15 tracey error = pack_err;
5002 0ae84acc 2022-06-15 tracey }
5003 51a10b52 2020-12-26 stsp tog_free_refs();
5004 26ed57b2 2018-05-19 stsp return error;
5005 9f7d7167 2018-04-29 stsp }
5006 9f7d7167 2018-04-29 stsp
5007 4ed7e80c 2018-05-20 stsp __dead static void
5008 9f7d7167 2018-04-29 stsp usage_blame(void)
5009 9f7d7167 2018-04-29 stsp {
5010 80ddbec8 2018-04-29 stsp endwin();
5011 87411fa9 2022-06-16 stsp fprintf(stderr,
5012 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5013 9f7d7167 2018-04-29 stsp getprogname());
5014 9f7d7167 2018-04-29 stsp exit(1);
5015 9f7d7167 2018-04-29 stsp }
5016 84451b3e 2018-07-10 stsp
5017 84451b3e 2018-07-10 stsp struct tog_blame_line {
5018 84451b3e 2018-07-10 stsp int annotated;
5019 84451b3e 2018-07-10 stsp struct got_object_id *id;
5020 84451b3e 2018-07-10 stsp };
5021 9f7d7167 2018-04-29 stsp
5022 4ed7e80c 2018-05-20 stsp static const struct got_error *
5023 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5024 84451b3e 2018-07-10 stsp {
5025 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5026 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5027 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5028 84451b3e 2018-07-10 stsp const struct got_error *err;
5029 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5030 826082fe 2020-12-10 stsp char *line = NULL;
5031 826082fe 2020-12-10 stsp size_t linesize = 0;
5032 826082fe 2020-12-10 stsp ssize_t linelen;
5033 84451b3e 2018-07-10 stsp wchar_t *wline;
5034 27a741e5 2019-09-11 stsp int width;
5035 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5036 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5037 ab089a2a 2018-07-12 stsp char *id_str;
5038 11b20872 2019-11-08 stsp struct tog_color *tc;
5039 ab089a2a 2018-07-12 stsp
5040 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5041 ab089a2a 2018-07-12 stsp if (err)
5042 ab089a2a 2018-07-12 stsp return err;
5043 84451b3e 2018-07-10 stsp
5044 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5045 f7d12f7e 2018-08-01 stsp werase(view->window);
5046 84451b3e 2018-07-10 stsp
5047 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5048 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5049 ab089a2a 2018-07-12 stsp free(id_str);
5050 ab089a2a 2018-07-12 stsp return err;
5051 ab089a2a 2018-07-12 stsp }
5052 ab089a2a 2018-07-12 stsp
5053 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5054 ab089a2a 2018-07-12 stsp free(line);
5055 2550e4c3 2018-07-13 stsp line = NULL;
5056 1cae65b4 2019-09-22 stsp if (err)
5057 1cae65b4 2019-09-22 stsp return err;
5058 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5059 a3404814 2018-09-02 stsp wstandout(view->window);
5060 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5061 11b20872 2019-11-08 stsp if (tc)
5062 11b20872 2019-11-08 stsp wattr_on(view->window,
5063 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5064 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5065 11b20872 2019-11-08 stsp if (tc)
5066 11b20872 2019-11-08 stsp wattr_off(view->window,
5067 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5068 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5069 a3404814 2018-09-02 stsp wstandend(view->window);
5070 2550e4c3 2018-07-13 stsp free(wline);
5071 2550e4c3 2018-07-13 stsp wline = NULL;
5072 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5073 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5074 ab089a2a 2018-07-12 stsp
5075 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5076 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5077 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5078 ab089a2a 2018-07-12 stsp free(id_str);
5079 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5080 ab089a2a 2018-07-12 stsp }
5081 ab089a2a 2018-07-12 stsp free(id_str);
5082 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5083 3f60a8ef 2018-07-10 stsp free(line);
5084 2550e4c3 2018-07-13 stsp line = NULL;
5085 3f60a8ef 2018-07-10 stsp if (err)
5086 3f60a8ef 2018-07-10 stsp return err;
5087 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5088 2550e4c3 2018-07-13 stsp free(wline);
5089 2550e4c3 2018-07-13 stsp wline = NULL;
5090 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5091 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5092 3f60a8ef 2018-07-10 stsp
5093 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5094 145b6838 2022-06-16 stsp view->maxx = 0;
5095 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5096 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5097 826082fe 2020-12-10 stsp if (linelen == -1) {
5098 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5099 826082fe 2020-12-10 stsp s->eof = 1;
5100 826082fe 2020-12-10 stsp break;
5101 826082fe 2020-12-10 stsp }
5102 84451b3e 2018-07-10 stsp free(line);
5103 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5104 84451b3e 2018-07-10 stsp }
5105 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5106 826082fe 2020-12-10 stsp continue;
5107 1853e0f4 2022-06-16 stsp
5108 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5109 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5110 1853e0f4 2022-06-16 stsp if (err) {
5111 1853e0f4 2022-06-16 stsp free(line);
5112 1853e0f4 2022-06-16 stsp return err;
5113 1853e0f4 2022-06-16 stsp }
5114 1853e0f4 2022-06-16 stsp free(wline);
5115 1853e0f4 2022-06-16 stsp wline = NULL;
5116 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5117 84451b3e 2018-07-10 stsp
5118 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5119 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5120 b700b5d6 2018-07-10 stsp
5121 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5122 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5123 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5124 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5125 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5126 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5127 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5128 8d0fe45a 2019-08-12 stsp char *id_str;
5129 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5130 87411fa9 2022-06-16 stsp blame_line->id);
5131 8d0fe45a 2019-08-12 stsp if (err) {
5132 8d0fe45a 2019-08-12 stsp free(line);
5133 8d0fe45a 2019-08-12 stsp return err;
5134 8d0fe45a 2019-08-12 stsp }
5135 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5136 11b20872 2019-11-08 stsp if (tc)
5137 11b20872 2019-11-08 stsp wattr_on(view->window,
5138 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5139 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5140 11b20872 2019-11-08 stsp if (tc)
5141 11b20872 2019-11-08 stsp wattr_off(view->window,
5142 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5143 8d0fe45a 2019-08-12 stsp free(id_str);
5144 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5145 8d0fe45a 2019-08-12 stsp } else {
5146 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5147 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5148 84451b3e 2018-07-10 stsp }
5149 ee41ec32 2018-07-10 stsp } else {
5150 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5151 ee41ec32 2018-07-10 stsp prev_id = NULL;
5152 ee41ec32 2018-07-10 stsp }
5153 84451b3e 2018-07-10 stsp
5154 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5155 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5156 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5157 27a741e5 2019-09-11 stsp
5158 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5159 41605754 2020-11-12 stsp width = 9;
5160 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5161 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5162 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5163 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5164 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5165 41605754 2020-11-12 stsp if (err) {
5166 41605754 2020-11-12 stsp free(line);
5167 41605754 2020-11-12 stsp return err;
5168 41605754 2020-11-12 stsp }
5169 41605754 2020-11-12 stsp width += 9;
5170 41605754 2020-11-12 stsp } else {
5171 4cb9d869 2022-06-16 stsp int skip;
5172 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5173 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5174 4cb9d869 2022-06-16 stsp if (err) {
5175 4cb9d869 2022-06-16 stsp free(line);
5176 4cb9d869 2022-06-16 stsp return err;
5177 145b6838 2022-06-16 stsp }
5178 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5179 a75b3e2d 2022-06-16 stsp width += 9;
5180 41605754 2020-11-12 stsp free(wline);
5181 41605754 2020-11-12 stsp wline = NULL;
5182 41605754 2020-11-12 stsp }
5183 41605754 2020-11-12 stsp
5184 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5185 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5186 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5187 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5188 84451b3e 2018-07-10 stsp }
5189 826082fe 2020-12-10 stsp free(line);
5190 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5191 84451b3e 2018-07-10 stsp
5192 9b058f45 2022-06-30 mark view_border(view);
5193 84451b3e 2018-07-10 stsp
5194 84451b3e 2018-07-10 stsp return NULL;
5195 84451b3e 2018-07-10 stsp }
5196 84451b3e 2018-07-10 stsp
5197 84451b3e 2018-07-10 stsp static const struct got_error *
5198 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5199 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5200 84451b3e 2018-07-10 stsp {
5201 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5202 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5203 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5204 1a76625f 2018-10-22 stsp int errcode;
5205 84451b3e 2018-07-10 stsp
5206 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5207 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5208 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5209 84451b3e 2018-07-10 stsp
5210 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5211 1a76625f 2018-10-22 stsp if (errcode)
5212 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5213 84451b3e 2018-07-10 stsp
5214 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5215 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5216 d68a0a7d 2018-07-10 stsp goto done;
5217 d68a0a7d 2018-07-10 stsp }
5218 d68a0a7d 2018-07-10 stsp
5219 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5220 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5221 d68a0a7d 2018-07-10 stsp
5222 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5223 d68a0a7d 2018-07-10 stsp if (line->annotated)
5224 d68a0a7d 2018-07-10 stsp goto done;
5225 d68a0a7d 2018-07-10 stsp
5226 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5227 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5228 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5229 84451b3e 2018-07-10 stsp goto done;
5230 84451b3e 2018-07-10 stsp }
5231 84451b3e 2018-07-10 stsp line->annotated = 1;
5232 84451b3e 2018-07-10 stsp done:
5233 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5234 1a76625f 2018-10-22 stsp if (errcode)
5235 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5236 84451b3e 2018-07-10 stsp return err;
5237 84451b3e 2018-07-10 stsp }
5238 84451b3e 2018-07-10 stsp
5239 84451b3e 2018-07-10 stsp static void *
5240 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5241 84451b3e 2018-07-10 stsp {
5242 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5243 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5244 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5245 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5246 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5247 1b484788 2022-06-28 tracey
5248 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5249 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5250 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5251 e6e73e55 2022-06-30 tracey
5252 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5253 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5254 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5255 e6e73e55 2022-06-30 tracey goto done;
5256 e6e73e55 2022-06-30 tracey }
5257 18430de3 2018-07-10 stsp
5258 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5259 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5260 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5261 e6e73e55 2022-06-30 tracey goto done;
5262 e6e73e55 2022-06-30 tracey }
5263 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5264 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5265 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5266 e6e73e55 2022-06-30 tracey goto done;
5267 e6e73e55 2022-06-30 tracey }
5268 e6e73e55 2022-06-30 tracey
5269 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5270 61266923 2020-01-14 stsp if (err)
5271 e6e73e55 2022-06-30 tracey goto done;
5272 61266923 2020-01-14 stsp
5273 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5274 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5275 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5276 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5277 fc06ba56 2019-08-22 stsp err = NULL;
5278 18430de3 2018-07-10 stsp
5279 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5280 e6e73e55 2022-06-30 tracey if (errcode) {
5281 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5282 e6e73e55 2022-06-30 tracey goto done;
5283 e6e73e55 2022-06-30 tracey }
5284 18430de3 2018-07-10 stsp
5285 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5286 1d0f4054 2021-06-17 stsp if (err == NULL)
5287 1d0f4054 2021-06-17 stsp err = close_err;
5288 c9beca56 2018-07-22 stsp ta->repo = NULL;
5289 c9beca56 2018-07-22 stsp *ta->complete = 1;
5290 18430de3 2018-07-10 stsp
5291 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5292 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5293 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5294 18430de3 2018-07-10 stsp
5295 e6e73e55 2022-06-30 tracey done:
5296 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5297 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5298 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5299 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5300 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5301 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5302 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5303 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5304 1b484788 2022-06-28 tracey
5305 18430de3 2018-07-10 stsp return (void *)err;
5306 84451b3e 2018-07-10 stsp }
5307 84451b3e 2018-07-10 stsp
5308 245d91c1 2018-07-12 stsp static struct got_object_id *
5309 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5310 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5311 245d91c1 2018-07-12 stsp {
5312 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5313 8d0fe45a 2019-08-12 stsp
5314 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5315 8d0fe45a 2019-08-12 stsp return NULL;
5316 b880a918 2018-07-10 stsp
5317 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5318 c0f61fa4 2022-07-11 mark if (!line->annotated)
5319 c0f61fa4 2022-07-11 mark return NULL;
5320 c0f61fa4 2022-07-11 mark
5321 c0f61fa4 2022-07-11 mark return line->id;
5322 c0f61fa4 2022-07-11 mark }
5323 c0f61fa4 2022-07-11 mark
5324 c0f61fa4 2022-07-11 mark static struct got_object_id *
5325 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5326 c0f61fa4 2022-07-11 mark int lineno)
5327 c0f61fa4 2022-07-11 mark {
5328 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5329 c0f61fa4 2022-07-11 mark
5330 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5331 c0f61fa4 2022-07-11 mark return NULL;
5332 c0f61fa4 2022-07-11 mark
5333 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5334 245d91c1 2018-07-12 stsp if (!line->annotated)
5335 245d91c1 2018-07-12 stsp return NULL;
5336 245d91c1 2018-07-12 stsp
5337 245d91c1 2018-07-12 stsp return line->id;
5338 b880a918 2018-07-10 stsp }
5339 245d91c1 2018-07-12 stsp
5340 b880a918 2018-07-10 stsp static const struct got_error *
5341 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5342 a70480e0 2018-06-23 stsp {
5343 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5344 245d91c1 2018-07-12 stsp int i;
5345 245d91c1 2018-07-12 stsp
5346 245d91c1 2018-07-12 stsp if (blame->thread) {
5347 1a76625f 2018-10-22 stsp int errcode;
5348 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5349 1a76625f 2018-10-22 stsp if (errcode)
5350 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5351 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5352 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5353 1a76625f 2018-10-22 stsp if (errcode)
5354 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5355 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5356 1a76625f 2018-10-22 stsp if (errcode)
5357 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5358 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5359 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5360 245d91c1 2018-07-12 stsp err = NULL;
5361 245d91c1 2018-07-12 stsp blame->thread = NULL;
5362 245d91c1 2018-07-12 stsp }
5363 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5364 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5365 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5366 1d0f4054 2021-06-17 stsp if (err == NULL)
5367 1d0f4054 2021-06-17 stsp err = close_err;
5368 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5369 245d91c1 2018-07-12 stsp }
5370 245d91c1 2018-07-12 stsp if (blame->f) {
5371 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5372 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5373 245d91c1 2018-07-12 stsp blame->f = NULL;
5374 245d91c1 2018-07-12 stsp }
5375 57670559 2018-12-23 stsp if (blame->lines) {
5376 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5377 57670559 2018-12-23 stsp free(blame->lines[i].id);
5378 57670559 2018-12-23 stsp free(blame->lines);
5379 57670559 2018-12-23 stsp blame->lines = NULL;
5380 57670559 2018-12-23 stsp }
5381 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5382 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5383 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5384 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5385 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5386 0ae84acc 2022-06-15 tracey if (err == NULL)
5387 0ae84acc 2022-06-15 tracey err = pack_err;
5388 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5389 0ae84acc 2022-06-15 tracey }
5390 245d91c1 2018-07-12 stsp return err;
5391 245d91c1 2018-07-12 stsp }
5392 245d91c1 2018-07-12 stsp
5393 245d91c1 2018-07-12 stsp static const struct got_error *
5394 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5395 fc06ba56 2019-08-22 stsp {
5396 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5397 fc06ba56 2019-08-22 stsp int *done = arg;
5398 fc06ba56 2019-08-22 stsp int errcode;
5399 fc06ba56 2019-08-22 stsp
5400 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5401 fc06ba56 2019-08-22 stsp if (errcode)
5402 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5403 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5404 fc06ba56 2019-08-22 stsp
5405 fc06ba56 2019-08-22 stsp if (*done)
5406 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5407 fc06ba56 2019-08-22 stsp
5408 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5409 fc06ba56 2019-08-22 stsp if (errcode)
5410 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5411 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5412 fc06ba56 2019-08-22 stsp
5413 fc06ba56 2019-08-22 stsp return err;
5414 fc06ba56 2019-08-22 stsp }
5415 fc06ba56 2019-08-22 stsp
5416 fc06ba56 2019-08-22 stsp static const struct got_error *
5417 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5418 245d91c1 2018-07-12 stsp {
5419 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5420 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5421 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5422 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5423 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5424 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5425 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5426 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5427 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5428 a70480e0 2018-06-23 stsp
5429 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5430 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5431 27d434c2 2018-09-15 stsp if (err)
5432 15a94983 2018-12-23 stsp return err;
5433 eb81bc23 2022-06-28 tracey
5434 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5435 eb81bc23 2022-06-28 tracey if (fd == -1) {
5436 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5437 eb81bc23 2022-06-28 tracey goto done;
5438 eb81bc23 2022-06-28 tracey }
5439 a44927cc 2022-04-07 stsp
5440 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5441 a44927cc 2022-04-07 stsp if (err)
5442 a44927cc 2022-04-07 stsp goto done;
5443 27d434c2 2018-09-15 stsp
5444 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5445 84451b3e 2018-07-10 stsp if (err)
5446 84451b3e 2018-07-10 stsp goto done;
5447 27d434c2 2018-09-15 stsp
5448 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5449 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5450 84451b3e 2018-07-10 stsp goto done;
5451 84451b3e 2018-07-10 stsp }
5452 a70480e0 2018-06-23 stsp
5453 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5454 a70480e0 2018-06-23 stsp if (err)
5455 a70480e0 2018-06-23 stsp goto done;
5456 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5457 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5458 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5459 84451b3e 2018-07-10 stsp goto done;
5460 84451b3e 2018-07-10 stsp }
5461 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5462 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5463 1fddf795 2021-01-20 stsp if (err)
5464 1fddf795 2021-01-20 stsp goto done;
5465 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5466 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5467 84451b3e 2018-07-10 stsp goto done;
5468 1fddf795 2021-01-20 stsp }
5469 b02560ec 2019-08-19 stsp
5470 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5471 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5472 b02560ec 2019-08-19 stsp blame->nlines--;
5473 a70480e0 2018-06-23 stsp
5474 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5475 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5476 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5477 84451b3e 2018-07-10 stsp goto done;
5478 84451b3e 2018-07-10 stsp }
5479 a70480e0 2018-06-23 stsp
5480 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5481 bd24772e 2018-07-11 stsp if (err)
5482 bd24772e 2018-07-11 stsp goto done;
5483 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5484 0ae84acc 2022-06-15 tracey pack_fds);
5485 0ae84acc 2022-06-15 tracey if (err)
5486 0ae84acc 2022-06-15 tracey goto done;
5487 bd24772e 2018-07-11 stsp
5488 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5489 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5490 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5491 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5492 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5493 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5494 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5495 245d91c1 2018-07-12 stsp goto done;
5496 245d91c1 2018-07-12 stsp }
5497 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5498 245d91c1 2018-07-12 stsp
5499 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5500 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5501 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5502 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5503 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5504 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5505 a5388363 2020-12-01 naddy s->blame_complete = 0;
5506 f5a09613 2020-12-13 naddy
5507 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5508 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5509 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5510 f5a09613 2020-12-13 naddy s->selected_line = 1;
5511 f5a09613 2020-12-13 naddy }
5512 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5513 245d91c1 2018-07-12 stsp
5514 245d91c1 2018-07-12 stsp done:
5515 a44927cc 2022-04-07 stsp if (commit)
5516 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5517 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5518 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5519 245d91c1 2018-07-12 stsp if (blob)
5520 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5521 27d434c2 2018-09-15 stsp free(obj_id);
5522 245d91c1 2018-07-12 stsp if (err)
5523 245d91c1 2018-07-12 stsp stop_blame(blame);
5524 245d91c1 2018-07-12 stsp return err;
5525 245d91c1 2018-07-12 stsp }
5526 245d91c1 2018-07-12 stsp
5527 245d91c1 2018-07-12 stsp static const struct got_error *
5528 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5529 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5530 245d91c1 2018-07-12 stsp {
5531 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5532 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5533 dbc6a6b6 2018-07-12 stsp
5534 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5535 245d91c1 2018-07-12 stsp
5536 c4843652 2019-08-12 stsp s->path = strdup(path);
5537 c4843652 2019-08-12 stsp if (s->path == NULL)
5538 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5539 c4843652 2019-08-12 stsp
5540 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5541 c4843652 2019-08-12 stsp if (err) {
5542 c4843652 2019-08-12 stsp free(s->path);
5543 7cbe629d 2018-08-04 stsp return err;
5544 c4843652 2019-08-12 stsp }
5545 245d91c1 2018-07-12 stsp
5546 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5547 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5548 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5549 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5550 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5551 fb2756b9 2018-08-04 stsp s->repo = repo;
5552 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5553 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5554 7cbe629d 2018-08-04 stsp
5555 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5556 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5557 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5558 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5559 11b20872 2019-11-08 stsp if (err)
5560 11b20872 2019-11-08 stsp return err;
5561 11b20872 2019-11-08 stsp }
5562 11b20872 2019-11-08 stsp
5563 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5564 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5565 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5566 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5567 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5568 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5569 e5a0f69f 2018-08-18 stsp
5570 a5388363 2020-12-01 naddy return run_blame(view);
5571 7cbe629d 2018-08-04 stsp }
5572 7cbe629d 2018-08-04 stsp
5573 e5a0f69f 2018-08-18 stsp static const struct got_error *
5574 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5575 7cbe629d 2018-08-04 stsp {
5576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5577 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5578 7cbe629d 2018-08-04 stsp
5579 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5580 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5581 e5a0f69f 2018-08-18 stsp
5582 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5583 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5584 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5585 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5586 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5587 7cbe629d 2018-08-04 stsp }
5588 e5a0f69f 2018-08-18 stsp
5589 e5a0f69f 2018-08-18 stsp free(s->path);
5590 11b20872 2019-11-08 stsp free_colors(&s->colors);
5591 e5a0f69f 2018-08-18 stsp return err;
5592 7cbe629d 2018-08-04 stsp }
5593 7cbe629d 2018-08-04 stsp
5594 7cbe629d 2018-08-04 stsp static const struct got_error *
5595 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5596 6c4c42e0 2019-06-24 stsp {
5597 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5598 6c4c42e0 2019-06-24 stsp
5599 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5600 6c4c42e0 2019-06-24 stsp return NULL;
5601 6c4c42e0 2019-06-24 stsp }
5602 6c4c42e0 2019-06-24 stsp
5603 6c4c42e0 2019-06-24 stsp static const struct got_error *
5604 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5605 6c4c42e0 2019-06-24 stsp {
5606 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5607 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5608 6c4c42e0 2019-06-24 stsp int lineno;
5609 cb713507 2022-06-16 stsp char *line = NULL;
5610 826082fe 2020-12-10 stsp size_t linesize = 0;
5611 826082fe 2020-12-10 stsp ssize_t linelen;
5612 6c4c42e0 2019-06-24 stsp
5613 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5614 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5615 6c4c42e0 2019-06-24 stsp return NULL;
5616 6c4c42e0 2019-06-24 stsp }
5617 6c4c42e0 2019-06-24 stsp
5618 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5619 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5620 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5621 6c4c42e0 2019-06-24 stsp else
5622 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5623 487cd7d2 2021-12-17 stsp } else
5624 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5625 6c4c42e0 2019-06-24 stsp
5626 6c4c42e0 2019-06-24 stsp while (1) {
5627 6c4c42e0 2019-06-24 stsp off_t offset;
5628 6c4c42e0 2019-06-24 stsp
5629 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5630 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5631 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5632 6c4c42e0 2019-06-24 stsp break;
5633 6c4c42e0 2019-06-24 stsp }
5634 2246482e 2019-06-25 stsp
5635 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5636 6c4c42e0 2019-06-24 stsp lineno = 1;
5637 6c4c42e0 2019-06-24 stsp else
5638 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5639 6c4c42e0 2019-06-24 stsp }
5640 6c4c42e0 2019-06-24 stsp
5641 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5642 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5643 6c4c42e0 2019-06-24 stsp free(line);
5644 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5645 6c4c42e0 2019-06-24 stsp }
5646 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5647 cb713507 2022-06-16 stsp if (linelen != -1) {
5648 cb713507 2022-06-16 stsp char *exstr;
5649 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5650 cb713507 2022-06-16 stsp if (err)
5651 cb713507 2022-06-16 stsp break;
5652 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5653 cb713507 2022-06-16 stsp &view->regmatch)) {
5654 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5655 cb713507 2022-06-16 stsp s->matched_line = lineno;
5656 cb713507 2022-06-16 stsp free(exstr);
5657 cb713507 2022-06-16 stsp break;
5658 cb713507 2022-06-16 stsp }
5659 cb713507 2022-06-16 stsp free(exstr);
5660 6c4c42e0 2019-06-24 stsp }
5661 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5662 6c4c42e0 2019-06-24 stsp lineno++;
5663 6c4c42e0 2019-06-24 stsp else
5664 6c4c42e0 2019-06-24 stsp lineno--;
5665 6c4c42e0 2019-06-24 stsp }
5666 826082fe 2020-12-10 stsp free(line);
5667 6c4c42e0 2019-06-24 stsp
5668 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5669 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5670 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5671 6c4c42e0 2019-06-24 stsp }
5672 6c4c42e0 2019-06-24 stsp
5673 145b6838 2022-06-16 stsp return err;
5674 6c4c42e0 2019-06-24 stsp }
5675 6c4c42e0 2019-06-24 stsp
5676 6c4c42e0 2019-06-24 stsp static const struct got_error *
5677 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5678 7cbe629d 2018-08-04 stsp {
5679 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5680 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5681 2b380cc8 2018-10-24 stsp int errcode;
5682 2b380cc8 2018-10-24 stsp
5683 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5684 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5685 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5686 2b380cc8 2018-10-24 stsp if (errcode)
5687 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5688 51fe7530 2019-08-19 stsp
5689 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5690 2b380cc8 2018-10-24 stsp }
5691 e5a0f69f 2018-08-18 stsp
5692 51fe7530 2019-08-19 stsp if (s->blame_complete)
5693 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5694 51fe7530 2019-08-19 stsp
5695 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5696 e5a0f69f 2018-08-18 stsp
5697 9b058f45 2022-06-30 mark view_border(view);
5698 e5a0f69f 2018-08-18 stsp return err;
5699 e5a0f69f 2018-08-18 stsp }
5700 e5a0f69f 2018-08-18 stsp
5701 e5a0f69f 2018-08-18 stsp static const struct got_error *
5702 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5703 e5a0f69f 2018-08-18 stsp {
5704 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5705 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5706 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5707 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5708 9b058f45 2022-06-30 mark
5709 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5710 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5711 9b058f45 2022-06-30 mark --eos; /* border */
5712 7cbe629d 2018-08-04 stsp
5713 e5a0f69f 2018-08-18 stsp switch (ch) {
5714 145b6838 2022-06-16 stsp case '0':
5715 145b6838 2022-06-16 stsp view->x = 0;
5716 145b6838 2022-06-16 stsp break;
5717 145b6838 2022-06-16 stsp case '$':
5718 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5719 640cd7ff 2022-06-22 mark view->count = 0;
5720 145b6838 2022-06-16 stsp break;
5721 145b6838 2022-06-16 stsp case KEY_RIGHT:
5722 145b6838 2022-06-16 stsp case 'l':
5723 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5724 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5725 640cd7ff 2022-06-22 mark else
5726 640cd7ff 2022-06-22 mark view->count = 0;
5727 145b6838 2022-06-16 stsp break;
5728 145b6838 2022-06-16 stsp case KEY_LEFT:
5729 145b6838 2022-06-16 stsp case 'h':
5730 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5731 640cd7ff 2022-06-22 mark if (view->x <= 0)
5732 640cd7ff 2022-06-22 mark view->count = 0;
5733 145b6838 2022-06-16 stsp break;
5734 1e37a5c2 2019-05-12 jcs case 'q':
5735 1e37a5c2 2019-05-12 jcs s->done = 1;
5736 4deef56f 2021-09-02 naddy break;
5737 4deef56f 2021-09-02 naddy case 'g':
5738 4deef56f 2021-09-02 naddy case KEY_HOME:
5739 4deef56f 2021-09-02 naddy s->selected_line = 1;
5740 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5741 640cd7ff 2022-06-22 mark view->count = 0;
5742 4deef56f 2021-09-02 naddy break;
5743 4deef56f 2021-09-02 naddy case 'G':
5744 4deef56f 2021-09-02 naddy case KEY_END:
5745 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5746 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5747 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5748 4deef56f 2021-09-02 naddy } else {
5749 9b058f45 2022-06-30 mark s->selected_line = eos;
5750 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5751 4deef56f 2021-09-02 naddy }
5752 640cd7ff 2022-06-22 mark view->count = 0;
5753 1e37a5c2 2019-05-12 jcs break;
5754 1e37a5c2 2019-05-12 jcs case 'k':
5755 1e37a5c2 2019-05-12 jcs case KEY_UP:
5756 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5757 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5758 1e37a5c2 2019-05-12 jcs s->selected_line--;
5759 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5760 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5761 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5762 640cd7ff 2022-06-22 mark else
5763 640cd7ff 2022-06-22 mark view->count = 0;
5764 1e37a5c2 2019-05-12 jcs break;
5765 83cc4199 2022-06-13 stsp case CTRL('u'):
5766 33c3719a 2022-06-15 stsp case 'u':
5767 83cc4199 2022-06-13 stsp nscroll /= 2;
5768 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5769 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5770 ea025d1d 2020-02-22 naddy case CTRL('b'):
5771 61417565 2022-06-20 mark case 'b':
5772 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5773 640cd7ff 2022-06-22 mark if (view->count > 1)
5774 640cd7ff 2022-06-22 mark nscroll += nscroll;
5775 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5776 640cd7ff 2022-06-22 mark view->count = 0;
5777 e5a0f69f 2018-08-18 stsp break;
5778 1e37a5c2 2019-05-12 jcs }
5779 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5780 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5781 1e37a5c2 2019-05-12 jcs else
5782 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5783 1e37a5c2 2019-05-12 jcs break;
5784 1e37a5c2 2019-05-12 jcs case 'j':
5785 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5786 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5787 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5788 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5789 1e37a5c2 2019-05-12 jcs s->selected_line++;
5790 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5791 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5792 640cd7ff 2022-06-22 mark else
5793 640cd7ff 2022-06-22 mark view->count = 0;
5794 1e37a5c2 2019-05-12 jcs break;
5795 61417565 2022-06-20 mark case 'c':
5796 1e37a5c2 2019-05-12 jcs case 'p': {
5797 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5798 640cd7ff 2022-06-22 mark
5799 640cd7ff 2022-06-22 mark view->count = 0;
5800 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5801 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5802 1e37a5c2 2019-05-12 jcs if (id == NULL)
5803 e5a0f69f 2018-08-18 stsp break;
5804 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5805 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5806 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5807 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5808 1e37a5c2 2019-05-12 jcs int obj_type;
5809 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5810 1e37a5c2 2019-05-12 jcs s->repo, id);
5811 e5a0f69f 2018-08-18 stsp if (err)
5812 e5a0f69f 2018-08-18 stsp break;
5813 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5814 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5815 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5816 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5817 e5a0f69f 2018-08-18 stsp break;
5818 e5a0f69f 2018-08-18 stsp }
5819 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5820 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5821 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5822 a44927cc 2022-04-07 stsp if (err)
5823 a44927cc 2022-04-07 stsp break;
5824 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5825 a44927cc 2022-04-07 stsp pcommit, s->path);
5826 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5827 e5a0f69f 2018-08-18 stsp if (err) {
5828 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5829 1e37a5c2 2019-05-12 jcs err = NULL;
5830 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5831 e5a0f69f 2018-08-18 stsp break;
5832 e5a0f69f 2018-08-18 stsp }
5833 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5834 1e37a5c2 2019-05-12 jcs blob_id);
5835 1e37a5c2 2019-05-12 jcs free(blob_id);
5836 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5837 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5838 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5839 e5a0f69f 2018-08-18 stsp break;
5840 1e37a5c2 2019-05-12 jcs }
5841 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5842 d7b5a0e8 2022-04-20 stsp &pid->id);
5843 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5844 1e37a5c2 2019-05-12 jcs } else {
5845 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5846 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5847 1e37a5c2 2019-05-12 jcs break;
5848 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5849 1e37a5c2 2019-05-12 jcs id);
5850 1e37a5c2 2019-05-12 jcs }
5851 1e37a5c2 2019-05-12 jcs if (err)
5852 e5a0f69f 2018-08-18 stsp break;
5853 1e37a5c2 2019-05-12 jcs s->done = 1;
5854 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5855 1e37a5c2 2019-05-12 jcs s->done = 0;
5856 1e37a5c2 2019-05-12 jcs if (thread_err)
5857 1e37a5c2 2019-05-12 jcs break;
5858 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5859 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5860 a5388363 2020-12-01 naddy err = run_blame(view);
5861 1e37a5c2 2019-05-12 jcs if (err)
5862 1e37a5c2 2019-05-12 jcs break;
5863 1e37a5c2 2019-05-12 jcs break;
5864 1e37a5c2 2019-05-12 jcs }
5865 61417565 2022-06-20 mark case 'C': {
5866 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5867 640cd7ff 2022-06-22 mark
5868 640cd7ff 2022-06-22 mark view->count = 0;
5869 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5870 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5871 1e37a5c2 2019-05-12 jcs break;
5872 1e37a5c2 2019-05-12 jcs s->done = 1;
5873 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5874 1e37a5c2 2019-05-12 jcs s->done = 0;
5875 1e37a5c2 2019-05-12 jcs if (thread_err)
5876 1e37a5c2 2019-05-12 jcs break;
5877 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5878 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5879 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5880 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5881 a5388363 2020-12-01 naddy err = run_blame(view);
5882 1e37a5c2 2019-05-12 jcs if (err)
5883 1e37a5c2 2019-05-12 jcs break;
5884 1e37a5c2 2019-05-12 jcs break;
5885 1e37a5c2 2019-05-12 jcs }
5886 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5887 1e37a5c2 2019-05-12 jcs case '\r': {
5888 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5889 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5890 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5891 640cd7ff 2022-06-22 mark
5892 640cd7ff 2022-06-22 mark view->count = 0;
5893 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5894 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5895 1e37a5c2 2019-05-12 jcs if (id == NULL)
5896 1e37a5c2 2019-05-12 jcs break;
5897 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5898 1e37a5c2 2019-05-12 jcs if (err)
5899 1e37a5c2 2019-05-12 jcs break;
5900 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5901 c0f61fa4 2022-07-11 mark if (*new_view) {
5902 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5903 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5904 c0f61fa4 2022-07-11 mark if (err)
5905 c0f61fa4 2022-07-11 mark break;
5906 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5907 c0f61fa4 2022-07-11 mark } else {
5908 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
5909 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
5910 9b058f45 2022-06-30 mark
5911 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
5912 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
5913 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
5914 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
5915 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
5916 c0f61fa4 2022-07-11 mark break;
5917 c0f61fa4 2022-07-11 mark }
5918 15a94983 2018-12-23 stsp }
5919 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5920 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
5921 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5922 1e37a5c2 2019-05-12 jcs if (err) {
5923 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5924 1e37a5c2 2019-05-12 jcs break;
5925 1e37a5c2 2019-05-12 jcs }
5926 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
5927 c0f61fa4 2022-07-11 mark s->selected_line;
5928 c0f61fa4 2022-07-11 mark if (*new_view)
5929 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
5930 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
5931 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5932 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5933 9b058f45 2022-06-30 mark if (err)
5934 9b058f45 2022-06-30 mark break;
5935 9b058f45 2022-06-30 mark }
5936 9b058f45 2022-06-30 mark
5937 e78dc838 2020-12-04 stsp view->focussed = 0;
5938 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5939 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5940 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5941 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5942 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
5943 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5944 1e37a5c2 2019-05-12 jcs if (err)
5945 34bc9ec9 2019-02-22 stsp break;
5946 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5947 0dbbbe90 2022-06-17 op if (err)
5948 0dbbbe90 2022-06-17 op break;
5949 e78dc838 2020-12-04 stsp view->focus_child = 1;
5950 1e37a5c2 2019-05-12 jcs } else
5951 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5952 1e37a5c2 2019-05-12 jcs if (err)
5953 e5a0f69f 2018-08-18 stsp break;
5954 1e37a5c2 2019-05-12 jcs break;
5955 1e37a5c2 2019-05-12 jcs }
5956 83cc4199 2022-06-13 stsp case CTRL('d'):
5957 33c3719a 2022-06-15 stsp case 'd':
5958 83cc4199 2022-06-13 stsp nscroll /= 2;
5959 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5960 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5961 ea025d1d 2020-02-22 naddy case CTRL('f'):
5962 61417565 2022-06-20 mark case 'f':
5963 1e37a5c2 2019-05-12 jcs case ' ':
5964 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5965 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5966 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5967 640cd7ff 2022-06-22 mark view->count = 0;
5968 e5a0f69f 2018-08-18 stsp break;
5969 1e37a5c2 2019-05-12 jcs }
5970 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5971 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5972 83cc4199 2022-06-13 stsp s->selected_line +=
5973 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5974 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5975 1e37a5c2 2019-05-12 jcs }
5976 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5977 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5978 1e37a5c2 2019-05-12 jcs else
5979 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5980 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5981 1e37a5c2 2019-05-12 jcs break;
5982 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5983 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5984 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5985 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5986 1e37a5c2 2019-05-12 jcs }
5987 1e37a5c2 2019-05-12 jcs break;
5988 1e37a5c2 2019-05-12 jcs default:
5989 640cd7ff 2022-06-22 mark view->count = 0;
5990 1e37a5c2 2019-05-12 jcs break;
5991 a70480e0 2018-06-23 stsp }
5992 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5993 917d79a7 2022-07-01 stsp }
5994 917d79a7 2022-07-01 stsp
5995 917d79a7 2022-07-01 stsp static const struct got_error *
5996 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
5997 917d79a7 2022-07-01 stsp {
5998 917d79a7 2022-07-01 stsp const struct got_error *err;
5999 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6000 917d79a7 2022-07-01 stsp
6001 917d79a7 2022-07-01 stsp view->count = 0;
6002 917d79a7 2022-07-01 stsp s->done = 1;
6003 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6004 917d79a7 2022-07-01 stsp s->done = 0;
6005 917d79a7 2022-07-01 stsp if (err)
6006 917d79a7 2022-07-01 stsp return err;
6007 917d79a7 2022-07-01 stsp return run_blame(view);
6008 a70480e0 2018-06-23 stsp }
6009 a70480e0 2018-06-23 stsp
6010 a70480e0 2018-06-23 stsp static const struct got_error *
6011 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6012 9f7d7167 2018-04-29 stsp {
6013 a70480e0 2018-06-23 stsp const struct got_error *error;
6014 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6015 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6016 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6017 0587e10c 2020-07-23 stsp char *link_target = NULL;
6018 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6019 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6020 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6021 a70480e0 2018-06-23 stsp int ch;
6022 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6023 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6024 a70480e0 2018-06-23 stsp
6025 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6026 a70480e0 2018-06-23 stsp switch (ch) {
6027 a70480e0 2018-06-23 stsp case 'c':
6028 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6029 a70480e0 2018-06-23 stsp break;
6030 69069811 2018-08-02 stsp case 'r':
6031 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6032 69069811 2018-08-02 stsp if (repo_path == NULL)
6033 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6034 9ba1d308 2019-10-21 stsp optarg);
6035 69069811 2018-08-02 stsp break;
6036 a70480e0 2018-06-23 stsp default:
6037 17020d27 2019-03-07 stsp usage_blame();
6038 a70480e0 2018-06-23 stsp /* NOTREACHED */
6039 a70480e0 2018-06-23 stsp }
6040 a70480e0 2018-06-23 stsp }
6041 a70480e0 2018-06-23 stsp
6042 a70480e0 2018-06-23 stsp argc -= optind;
6043 a70480e0 2018-06-23 stsp argv += optind;
6044 a70480e0 2018-06-23 stsp
6045 f135c941 2020-02-20 stsp if (argc != 1)
6046 a70480e0 2018-06-23 stsp usage_blame();
6047 6962eb72 2020-02-20 stsp
6048 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6049 0ae84acc 2022-06-15 tracey if (error != NULL)
6050 0ae84acc 2022-06-15 tracey goto done;
6051 0ae84acc 2022-06-15 tracey
6052 69069811 2018-08-02 stsp if (repo_path == NULL) {
6053 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6054 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6055 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6056 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6057 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6058 c156c7a4 2020-12-18 stsp goto done;
6059 f135c941 2020-02-20 stsp if (worktree)
6060 eb41ed75 2019-02-05 stsp repo_path =
6061 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6062 f135c941 2020-02-20 stsp else
6063 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6064 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6065 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6066 c156c7a4 2020-12-18 stsp goto done;
6067 c156c7a4 2020-12-18 stsp }
6068 f135c941 2020-02-20 stsp }
6069 a915003a 2019-02-05 stsp
6070 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6071 c02c541e 2019-03-29 stsp if (error != NULL)
6072 8e94dd5b 2019-01-04 stsp goto done;
6073 69069811 2018-08-02 stsp
6074 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6075 f135c941 2020-02-20 stsp worktree);
6076 c02c541e 2019-03-29 stsp if (error)
6077 92205607 2019-01-04 stsp goto done;
6078 69069811 2018-08-02 stsp
6079 f135c941 2020-02-20 stsp init_curses();
6080 f135c941 2020-02-20 stsp
6081 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6082 51a10b52 2020-12-26 stsp if (error)
6083 51a10b52 2020-12-26 stsp goto done;
6084 51a10b52 2020-12-26 stsp
6085 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6086 eb41ed75 2019-02-05 stsp if (error)
6087 69069811 2018-08-02 stsp goto done;
6088 a70480e0 2018-06-23 stsp
6089 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6090 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6091 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6092 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6093 a70480e0 2018-06-23 stsp if (error != NULL)
6094 66b4983c 2018-06-23 stsp goto done;
6095 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6096 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6097 a70480e0 2018-06-23 stsp } else {
6098 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6099 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6100 a70480e0 2018-06-23 stsp }
6101 a19e88aa 2018-06-23 stsp if (error != NULL)
6102 8b473291 2019-02-21 stsp goto done;
6103 8b473291 2019-02-21 stsp
6104 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6105 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6106 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6107 e1cd8fed 2018-08-01 stsp goto done;
6108 e1cd8fed 2018-08-01 stsp }
6109 0587e10c 2020-07-23 stsp
6110 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6111 a44927cc 2022-04-07 stsp if (error)
6112 a44927cc 2022-04-07 stsp goto done;
6113 a44927cc 2022-04-07 stsp
6114 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6115 a44927cc 2022-04-07 stsp commit, repo);
6116 7cbe629d 2018-08-04 stsp if (error)
6117 7cbe629d 2018-08-04 stsp goto done;
6118 0587e10c 2020-07-23 stsp
6119 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6120 78756c87 2020-11-24 stsp commit_id, repo);
6121 0587e10c 2020-07-23 stsp if (error)
6122 0587e10c 2020-07-23 stsp goto done;
6123 12314ad4 2019-08-31 stsp if (worktree) {
6124 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6125 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6126 12314ad4 2019-08-31 stsp worktree = NULL;
6127 12314ad4 2019-08-31 stsp }
6128 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6129 a70480e0 2018-06-23 stsp done:
6130 69069811 2018-08-02 stsp free(repo_path);
6131 f135c941 2020-02-20 stsp free(in_repo_path);
6132 0587e10c 2020-07-23 stsp free(link_target);
6133 69069811 2018-08-02 stsp free(cwd);
6134 a70480e0 2018-06-23 stsp free(commit_id);
6135 a44927cc 2022-04-07 stsp if (commit)
6136 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6137 eb41ed75 2019-02-05 stsp if (worktree)
6138 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6139 1d0f4054 2021-06-17 stsp if (repo) {
6140 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6141 1d0f4054 2021-06-17 stsp if (error == NULL)
6142 1d0f4054 2021-06-17 stsp error = close_err;
6143 1d0f4054 2021-06-17 stsp }
6144 0ae84acc 2022-06-15 tracey if (pack_fds) {
6145 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6146 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6147 0ae84acc 2022-06-15 tracey if (error == NULL)
6148 0ae84acc 2022-06-15 tracey error = pack_err;
6149 0ae84acc 2022-06-15 tracey }
6150 51a10b52 2020-12-26 stsp tog_free_refs();
6151 a70480e0 2018-06-23 stsp return error;
6152 ffd1d5e5 2018-06-23 stsp }
6153 ffd1d5e5 2018-06-23 stsp
6154 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6155 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6156 ffd1d5e5 2018-06-23 stsp {
6157 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6158 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6159 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6160 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6161 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6162 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6163 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6164 ffd1d5e5 2018-06-23 stsp
6165 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6166 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6167 9b058f45 2022-06-30 mark --limit; /* border */
6168 ffd1d5e5 2018-06-23 stsp
6169 f7d12f7e 2018-08-01 stsp werase(view->window);
6170 ffd1d5e5 2018-06-23 stsp
6171 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6172 ffd1d5e5 2018-06-23 stsp return NULL;
6173 ffd1d5e5 2018-06-23 stsp
6174 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6175 ccda2f4d 2022-06-16 stsp 0, 0);
6176 ffd1d5e5 2018-06-23 stsp if (err)
6177 ffd1d5e5 2018-06-23 stsp return err;
6178 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6179 a3404814 2018-09-02 stsp wstandout(view->window);
6180 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6181 11b20872 2019-11-08 stsp if (tc)
6182 11b20872 2019-11-08 stsp wattr_on(view->window,
6183 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6184 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6185 11b20872 2019-11-08 stsp if (tc)
6186 11b20872 2019-11-08 stsp wattr_off(view->window,
6187 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6188 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6189 a3404814 2018-09-02 stsp wstandend(view->window);
6190 2550e4c3 2018-07-13 stsp free(wline);
6191 2550e4c3 2018-07-13 stsp wline = NULL;
6192 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6193 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6194 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6195 ffd1d5e5 2018-06-23 stsp return NULL;
6196 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6197 ccda2f4d 2022-06-16 stsp 0, 0);
6198 ce52c690 2018-06-23 stsp if (err)
6199 ce52c690 2018-06-23 stsp return err;
6200 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6201 2550e4c3 2018-07-13 stsp free(wline);
6202 2550e4c3 2018-07-13 stsp wline = NULL;
6203 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6204 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6205 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6206 ffd1d5e5 2018-06-23 stsp return NULL;
6207 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6208 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6209 a1eca9bb 2018-06-23 stsp return NULL;
6210 ffd1d5e5 2018-06-23 stsp
6211 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6212 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6213 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6214 0cf4efb1 2018-09-29 stsp if (view->focussed)
6215 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6216 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6217 ffd1d5e5 2018-06-23 stsp }
6218 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6219 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6220 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6221 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6222 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6223 ffd1d5e5 2018-06-23 stsp return NULL;
6224 ffd1d5e5 2018-06-23 stsp n = 1;
6225 ffd1d5e5 2018-06-23 stsp } else {
6226 ffd1d5e5 2018-06-23 stsp n = 0;
6227 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6228 ffd1d5e5 2018-06-23 stsp }
6229 ffd1d5e5 2018-06-23 stsp
6230 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6231 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6232 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6233 848d6979 2019-08-12 stsp const char *modestr = "";
6234 56e0773d 2019-11-28 stsp mode_t mode;
6235 1d13200f 2018-07-12 stsp
6236 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6237 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6238 56e0773d 2019-11-28 stsp
6239 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6240 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6241 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6242 1d13200f 2018-07-12 stsp if (err)
6243 638f9024 2019-05-13 stsp return got_error_from_errno(
6244 230a42bd 2019-05-11 jcs "got_object_id_str");
6245 1d13200f 2018-07-12 stsp }
6246 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6247 63c5ca5d 2019-08-24 stsp modestr = "$";
6248 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6249 0d6c6ee3 2020-05-20 stsp int i;
6250 0d6c6ee3 2020-05-20 stsp
6251 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6252 d86d3b18 2020-12-01 naddy te, s->repo);
6253 0d6c6ee3 2020-05-20 stsp if (err) {
6254 0d6c6ee3 2020-05-20 stsp free(id_str);
6255 0d6c6ee3 2020-05-20 stsp return err;
6256 0d6c6ee3 2020-05-20 stsp }
6257 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6258 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6259 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6260 0d6c6ee3 2020-05-20 stsp }
6261 848d6979 2019-08-12 stsp modestr = "@";
6262 0d6c6ee3 2020-05-20 stsp }
6263 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6264 848d6979 2019-08-12 stsp modestr = "/";
6265 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6266 848d6979 2019-08-12 stsp modestr = "*";
6267 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6268 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6269 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6270 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6271 1d13200f 2018-07-12 stsp free(id_str);
6272 0d6c6ee3 2020-05-20 stsp free(link_target);
6273 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6274 1d13200f 2018-07-12 stsp }
6275 1d13200f 2018-07-12 stsp free(id_str);
6276 0d6c6ee3 2020-05-20 stsp free(link_target);
6277 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6278 ccda2f4d 2022-06-16 stsp 0, 0);
6279 ffd1d5e5 2018-06-23 stsp if (err) {
6280 ffd1d5e5 2018-06-23 stsp free(line);
6281 ffd1d5e5 2018-06-23 stsp break;
6282 ffd1d5e5 2018-06-23 stsp }
6283 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6284 0cf4efb1 2018-09-29 stsp if (view->focussed)
6285 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6286 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6287 ffd1d5e5 2018-06-23 stsp }
6288 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6289 f26dddb7 2019-11-08 stsp if (tc)
6290 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6291 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6292 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6293 f26dddb7 2019-11-08 stsp if (tc)
6294 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6295 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6296 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6297 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6298 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6299 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6300 ffd1d5e5 2018-06-23 stsp free(line);
6301 2550e4c3 2018-07-13 stsp free(wline);
6302 2550e4c3 2018-07-13 stsp wline = NULL;
6303 ffd1d5e5 2018-06-23 stsp n++;
6304 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6305 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6306 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6307 ffd1d5e5 2018-06-23 stsp break;
6308 ffd1d5e5 2018-06-23 stsp }
6309 ffd1d5e5 2018-06-23 stsp
6310 ffd1d5e5 2018-06-23 stsp return err;
6311 ffd1d5e5 2018-06-23 stsp }
6312 ffd1d5e5 2018-06-23 stsp
6313 ffd1d5e5 2018-06-23 stsp static void
6314 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6315 ffd1d5e5 2018-06-23 stsp {
6316 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6317 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6318 fa86c4bf 2020-11-29 stsp int i = 0;
6319 ffd1d5e5 2018-06-23 stsp
6320 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6321 ffd1d5e5 2018-06-23 stsp return;
6322 ffd1d5e5 2018-06-23 stsp
6323 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6324 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6325 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6326 fa86c4bf 2020-11-29 stsp if (!isroot)
6327 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6328 fa86c4bf 2020-11-29 stsp break;
6329 fa86c4bf 2020-11-29 stsp }
6330 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6331 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6332 ffd1d5e5 2018-06-23 stsp }
6333 ffd1d5e5 2018-06-23 stsp }
6334 ffd1d5e5 2018-06-23 stsp
6335 9b058f45 2022-06-30 mark static const struct got_error *
6336 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6337 ffd1d5e5 2018-06-23 stsp {
6338 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6339 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6340 ffd1d5e5 2018-06-23 stsp int n = 0;
6341 ffd1d5e5 2018-06-23 stsp
6342 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6343 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6344 694d3271 2020-12-01 naddy s->first_displayed_entry);
6345 694d3271 2020-12-01 naddy else
6346 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6347 56e0773d 2019-11-28 stsp
6348 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6349 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6350 9b058f45 2022-06-30 mark if (last)
6351 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6352 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6353 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6354 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6355 768394f3 2019-01-24 stsp }
6356 ffd1d5e5 2018-06-23 stsp }
6357 9b058f45 2022-06-30 mark
6358 9b058f45 2022-06-30 mark return NULL;
6359 ffd1d5e5 2018-06-23 stsp }
6360 ffd1d5e5 2018-06-23 stsp
6361 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6362 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6363 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6364 ffd1d5e5 2018-06-23 stsp {
6365 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6366 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6367 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6368 ffd1d5e5 2018-06-23 stsp
6369 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6370 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6371 56e0773d 2019-11-28 stsp + 1 /* slash */;
6372 ce52c690 2018-06-23 stsp if (te)
6373 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6374 ce52c690 2018-06-23 stsp
6375 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6376 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6377 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6378 ffd1d5e5 2018-06-23 stsp
6379 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6380 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6381 d9765a41 2018-06-23 stsp while (pt) {
6382 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6383 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6384 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6385 cb2ebc8a 2018-06-23 stsp goto done;
6386 cb2ebc8a 2018-06-23 stsp }
6387 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6388 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6389 cb2ebc8a 2018-06-23 stsp goto done;
6390 cb2ebc8a 2018-06-23 stsp }
6391 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6392 ffd1d5e5 2018-06-23 stsp }
6393 ce52c690 2018-06-23 stsp if (te) {
6394 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6395 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6396 ce52c690 2018-06-23 stsp goto done;
6397 ce52c690 2018-06-23 stsp }
6398 cb2ebc8a 2018-06-23 stsp }
6399 ce52c690 2018-06-23 stsp done:
6400 ce52c690 2018-06-23 stsp if (err) {
6401 ce52c690 2018-06-23 stsp free(*path);
6402 ce52c690 2018-06-23 stsp *path = NULL;
6403 ce52c690 2018-06-23 stsp }
6404 ce52c690 2018-06-23 stsp return err;
6405 ce52c690 2018-06-23 stsp }
6406 ce52c690 2018-06-23 stsp
6407 ce52c690 2018-06-23 stsp static const struct got_error *
6408 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6409 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6410 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6411 ce52c690 2018-06-23 stsp {
6412 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6413 ce52c690 2018-06-23 stsp char *path;
6414 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6415 a0de39f3 2019-08-09 stsp
6416 a0de39f3 2019-08-09 stsp *new_view = NULL;
6417 69efd4c4 2018-07-18 stsp
6418 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6419 ce52c690 2018-06-23 stsp if (err)
6420 ce52c690 2018-06-23 stsp return err;
6421 ffd1d5e5 2018-06-23 stsp
6422 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6423 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6424 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6425 83ce39e3 2019-08-12 stsp goto done;
6426 83ce39e3 2019-08-12 stsp }
6427 cdf1ee82 2018-08-01 stsp
6428 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6429 e5a0f69f 2018-08-18 stsp if (err) {
6430 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6431 fc06ba56 2019-08-22 stsp err = NULL;
6432 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6433 e5a0f69f 2018-08-18 stsp } else
6434 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6435 83ce39e3 2019-08-12 stsp done:
6436 83ce39e3 2019-08-12 stsp free(path);
6437 69efd4c4 2018-07-18 stsp return err;
6438 69efd4c4 2018-07-18 stsp }
6439 69efd4c4 2018-07-18 stsp
6440 69efd4c4 2018-07-18 stsp static const struct got_error *
6441 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6442 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6443 69efd4c4 2018-07-18 stsp {
6444 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6445 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6446 69efd4c4 2018-07-18 stsp char *path;
6447 a0de39f3 2019-08-09 stsp
6448 a0de39f3 2019-08-09 stsp *new_view = NULL;
6449 69efd4c4 2018-07-18 stsp
6450 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6451 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6452 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6453 e5a0f69f 2018-08-18 stsp
6454 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6455 69efd4c4 2018-07-18 stsp if (err)
6456 69efd4c4 2018-07-18 stsp return err;
6457 69efd4c4 2018-07-18 stsp
6458 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6459 4e97c21c 2020-12-06 stsp path, 0);
6460 ba4f502b 2018-08-04 stsp if (err)
6461 e5a0f69f 2018-08-18 stsp view_close(log_view);
6462 e5a0f69f 2018-08-18 stsp else
6463 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6464 cb2ebc8a 2018-06-23 stsp free(path);
6465 cb2ebc8a 2018-06-23 stsp return err;
6466 ffd1d5e5 2018-06-23 stsp }
6467 ffd1d5e5 2018-06-23 stsp
6468 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6469 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6470 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6471 ffd1d5e5 2018-06-23 stsp {
6472 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6473 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6474 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6475 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6476 ffd1d5e5 2018-06-23 stsp
6477 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6478 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6479 bc573f3b 2021-07-10 stsp
6480 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6481 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6482 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6483 ffd1d5e5 2018-06-23 stsp
6484 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6485 bc573f3b 2021-07-10 stsp if (err)
6486 bc573f3b 2021-07-10 stsp goto done;
6487 bc573f3b 2021-07-10 stsp
6488 bc573f3b 2021-07-10 stsp /*
6489 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6490 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6491 bc573f3b 2021-07-10 stsp * closed on demand.
6492 bc573f3b 2021-07-10 stsp */
6493 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6494 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6495 bc573f3b 2021-07-10 stsp if (err)
6496 bc573f3b 2021-07-10 stsp goto done;
6497 bc573f3b 2021-07-10 stsp s->tree = s->root;
6498 bc573f3b 2021-07-10 stsp
6499 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6500 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6501 ffd1d5e5 2018-06-23 stsp goto done;
6502 ffd1d5e5 2018-06-23 stsp
6503 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6504 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6505 ffd1d5e5 2018-06-23 stsp goto done;
6506 ffd1d5e5 2018-06-23 stsp }
6507 ffd1d5e5 2018-06-23 stsp
6508 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6509 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6510 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6511 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6512 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6513 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6514 9cd7cbd1 2020-12-07 stsp goto done;
6515 9cd7cbd1 2020-12-07 stsp }
6516 9cd7cbd1 2020-12-07 stsp }
6517 fb2756b9 2018-08-04 stsp s->repo = repo;
6518 c0b01bdb 2019-11-08 stsp
6519 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6520 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6521 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6522 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6523 c0b01bdb 2019-11-08 stsp if (err)
6524 c0b01bdb 2019-11-08 stsp goto done;
6525 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6526 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6527 bc573f3b 2021-07-10 stsp if (err)
6528 c0b01bdb 2019-11-08 stsp goto done;
6529 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6530 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6531 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6532 bc573f3b 2021-07-10 stsp if (err)
6533 c0b01bdb 2019-11-08 stsp goto done;
6534 e5a0f69f 2018-08-18 stsp
6535 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6536 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6537 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6538 bc573f3b 2021-07-10 stsp if (err)
6539 11b20872 2019-11-08 stsp goto done;
6540 11b20872 2019-11-08 stsp
6541 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6543 bc573f3b 2021-07-10 stsp if (err)
6544 c0b01bdb 2019-11-08 stsp goto done;
6545 c0b01bdb 2019-11-08 stsp }
6546 c0b01bdb 2019-11-08 stsp
6547 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6548 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6549 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6550 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6551 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6552 ad80ab7b 2018-08-04 stsp done:
6553 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6554 bc573f3b 2021-07-10 stsp if (commit)
6555 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6556 bc573f3b 2021-07-10 stsp if (err)
6557 bc573f3b 2021-07-10 stsp close_tree_view(view);
6558 ad80ab7b 2018-08-04 stsp return err;
6559 ad80ab7b 2018-08-04 stsp }
6560 ad80ab7b 2018-08-04 stsp
6561 e5a0f69f 2018-08-18 stsp static const struct got_error *
6562 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6563 ad80ab7b 2018-08-04 stsp {
6564 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6565 ad80ab7b 2018-08-04 stsp
6566 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6567 fb2756b9 2018-08-04 stsp free(s->tree_label);
6568 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6569 6484ec90 2018-09-29 stsp free(s->commit_id);
6570 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6571 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6572 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6573 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6574 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6575 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6576 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6577 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6578 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6579 ad80ab7b 2018-08-04 stsp free(parent);
6580 ad80ab7b 2018-08-04 stsp
6581 ad80ab7b 2018-08-04 stsp }
6582 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6583 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6584 bc573f3b 2021-07-10 stsp if (s->root)
6585 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6586 7c32bd05 2019-06-22 stsp return NULL;
6587 7c32bd05 2019-06-22 stsp }
6588 7c32bd05 2019-06-22 stsp
6589 7c32bd05 2019-06-22 stsp static const struct got_error *
6590 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6591 7c32bd05 2019-06-22 stsp {
6592 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6593 7c32bd05 2019-06-22 stsp
6594 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6595 7c32bd05 2019-06-22 stsp return NULL;
6596 7c32bd05 2019-06-22 stsp }
6597 7c32bd05 2019-06-22 stsp
6598 7c32bd05 2019-06-22 stsp static int
6599 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6600 7c32bd05 2019-06-22 stsp {
6601 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6602 7c32bd05 2019-06-22 stsp
6603 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6604 56e0773d 2019-11-28 stsp 0) == 0;
6605 7c32bd05 2019-06-22 stsp }
6606 7c32bd05 2019-06-22 stsp
6607 7c32bd05 2019-06-22 stsp static const struct got_error *
6608 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6609 7c32bd05 2019-06-22 stsp {
6610 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6611 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6612 7c32bd05 2019-06-22 stsp
6613 7c32bd05 2019-06-22 stsp if (!view->searching) {
6614 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6615 7c32bd05 2019-06-22 stsp return NULL;
6616 7c32bd05 2019-06-22 stsp }
6617 7c32bd05 2019-06-22 stsp
6618 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6619 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6620 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6621 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6622 56e0773d 2019-11-28 stsp s->selected_entry);
6623 7c32bd05 2019-06-22 stsp else
6624 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6625 56e0773d 2019-11-28 stsp } else {
6626 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6627 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6628 56e0773d 2019-11-28 stsp else
6629 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6630 56e0773d 2019-11-28 stsp s->selected_entry);
6631 7c32bd05 2019-06-22 stsp }
6632 7c32bd05 2019-06-22 stsp } else {
6633 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6634 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6635 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6636 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6637 56e0773d 2019-11-28 stsp else
6638 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6639 7c32bd05 2019-06-22 stsp }
6640 7c32bd05 2019-06-22 stsp
6641 7c32bd05 2019-06-22 stsp while (1) {
6642 56e0773d 2019-11-28 stsp if (te == NULL) {
6643 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6644 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6645 ac66afb8 2019-06-24 stsp return NULL;
6646 ac66afb8 2019-06-24 stsp }
6647 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6648 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6649 56e0773d 2019-11-28 stsp else
6650 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6651 7c32bd05 2019-06-22 stsp }
6652 7c32bd05 2019-06-22 stsp
6653 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6654 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6655 56e0773d 2019-11-28 stsp s->matched_entry = te;
6656 7c32bd05 2019-06-22 stsp break;
6657 7c32bd05 2019-06-22 stsp }
6658 7c32bd05 2019-06-22 stsp
6659 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6660 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6661 56e0773d 2019-11-28 stsp else
6662 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6663 7c32bd05 2019-06-22 stsp }
6664 e5a0f69f 2018-08-18 stsp
6665 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6666 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6667 7c32bd05 2019-06-22 stsp s->selected = 0;
6668 7c32bd05 2019-06-22 stsp }
6669 7c32bd05 2019-06-22 stsp
6670 e5a0f69f 2018-08-18 stsp return NULL;
6671 ad80ab7b 2018-08-04 stsp }
6672 ad80ab7b 2018-08-04 stsp
6673 ad80ab7b 2018-08-04 stsp static const struct got_error *
6674 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6675 ad80ab7b 2018-08-04 stsp {
6676 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6677 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6678 e5a0f69f 2018-08-18 stsp char *parent_path;
6679 ad80ab7b 2018-08-04 stsp
6680 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6681 e5a0f69f 2018-08-18 stsp if (err)
6682 e5a0f69f 2018-08-18 stsp return err;
6683 ffd1d5e5 2018-06-23 stsp
6684 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6685 e5a0f69f 2018-08-18 stsp free(parent_path);
6686 669b5ffa 2018-10-07 stsp
6687 9b058f45 2022-06-30 mark view_border(view);
6688 e5a0f69f 2018-08-18 stsp return err;
6689 e5a0f69f 2018-08-18 stsp }
6690 ce52c690 2018-06-23 stsp
6691 e5a0f69f 2018-08-18 stsp static const struct got_error *
6692 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6693 e5a0f69f 2018-08-18 stsp {
6694 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6695 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6696 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6697 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6698 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6699 ffd1d5e5 2018-06-23 stsp
6700 e5a0f69f 2018-08-18 stsp switch (ch) {
6701 1e37a5c2 2019-05-12 jcs case 'i':
6702 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6703 640cd7ff 2022-06-22 mark view->count = 0;
6704 1e37a5c2 2019-05-12 jcs break;
6705 1e37a5c2 2019-05-12 jcs case 'l':
6706 640cd7ff 2022-06-22 mark view->count = 0;
6707 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6708 ffd1d5e5 2018-06-23 stsp break;
6709 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6710 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6711 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6712 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6713 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6714 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6715 49b24ee5 2022-07-03 mark if (err)
6716 49b24ee5 2022-07-03 mark break;
6717 49b24ee5 2022-07-03 mark }
6718 e78dc838 2020-12-04 stsp view->focussed = 0;
6719 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6720 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6721 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6722 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6723 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
6724 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6725 1e37a5c2 2019-05-12 jcs if (err)
6726 1e37a5c2 2019-05-12 jcs return err;
6727 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6728 0dbbbe90 2022-06-17 op if (err)
6729 0dbbbe90 2022-06-17 op return err;
6730 e78dc838 2020-12-04 stsp view->focus_child = 1;
6731 1e37a5c2 2019-05-12 jcs } else
6732 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6733 152c1c93 2020-11-29 stsp break;
6734 152c1c93 2020-11-29 stsp case 'r':
6735 640cd7ff 2022-06-22 mark view->count = 0;
6736 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6737 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6738 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6739 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6740 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6741 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6742 152c1c93 2020-11-29 stsp if (err) {
6743 152c1c93 2020-11-29 stsp view_close(ref_view);
6744 152c1c93 2020-11-29 stsp return err;
6745 152c1c93 2020-11-29 stsp }
6746 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6747 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6748 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6749 49b24ee5 2022-07-03 mark if (err)
6750 49b24ee5 2022-07-03 mark break;
6751 49b24ee5 2022-07-03 mark }
6752 e78dc838 2020-12-04 stsp view->focussed = 0;
6753 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6754 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6755 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6756 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6757 3c1dfe12 2022-07-08 mark view_transfer_size(ref_view, view);
6758 152c1c93 2020-11-29 stsp err = view_close_child(view);
6759 152c1c93 2020-11-29 stsp if (err)
6760 152c1c93 2020-11-29 stsp return err;
6761 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6762 0dbbbe90 2022-06-17 op if (err)
6763 0dbbbe90 2022-06-17 op return err;
6764 e78dc838 2020-12-04 stsp view->focus_child = 1;
6765 152c1c93 2020-11-29 stsp } else
6766 152c1c93 2020-11-29 stsp *new_view = ref_view;
6767 e4526bf5 2021-09-03 naddy break;
6768 e4526bf5 2021-09-03 naddy case 'g':
6769 e4526bf5 2021-09-03 naddy case KEY_HOME:
6770 e4526bf5 2021-09-03 naddy s->selected = 0;
6771 640cd7ff 2022-06-22 mark view->count = 0;
6772 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6773 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6774 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6775 e4526bf5 2021-09-03 naddy else
6776 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6777 1e37a5c2 2019-05-12 jcs break;
6778 e4526bf5 2021-09-03 naddy case 'G':
6779 9b058f45 2022-06-30 mark case KEY_END: {
6780 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6781 9b058f45 2022-06-30 mark
6782 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6783 9b058f45 2022-06-30 mark --eos; /* border */
6784 e4526bf5 2021-09-03 naddy s->selected = 0;
6785 640cd7ff 2022-06-22 mark view->count = 0;
6786 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6787 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6788 e4526bf5 2021-09-03 naddy if (te == NULL) {
6789 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6790 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6791 e4526bf5 2021-09-03 naddy n++;
6792 e4526bf5 2021-09-03 naddy }
6793 e4526bf5 2021-09-03 naddy break;
6794 e4526bf5 2021-09-03 naddy }
6795 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6796 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6797 e4526bf5 2021-09-03 naddy }
6798 e4526bf5 2021-09-03 naddy if (n > 0)
6799 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6800 e4526bf5 2021-09-03 naddy break;
6801 9b058f45 2022-06-30 mark }
6802 1e37a5c2 2019-05-12 jcs case 'k':
6803 1e37a5c2 2019-05-12 jcs case KEY_UP:
6804 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6805 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6806 1e37a5c2 2019-05-12 jcs s->selected--;
6807 fa86c4bf 2020-11-29 stsp break;
6808 1e37a5c2 2019-05-12 jcs }
6809 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6810 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6811 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6812 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6813 640cd7ff 2022-06-22 mark view->count = 0;
6814 1e37a5c2 2019-05-12 jcs break;
6815 83cc4199 2022-06-13 stsp case CTRL('u'):
6816 33c3719a 2022-06-15 stsp case 'u':
6817 83cc4199 2022-06-13 stsp nscroll /= 2;
6818 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6819 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6820 ea025d1d 2020-02-22 naddy case CTRL('b'):
6821 61417565 2022-06-20 mark case 'b':
6822 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6823 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6824 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6825 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6826 fa86c4bf 2020-11-29 stsp } else {
6827 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6828 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6829 fa86c4bf 2020-11-29 stsp }
6830 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6831 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6832 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6833 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6834 640cd7ff 2022-06-22 mark view->count = 0;
6835 1e37a5c2 2019-05-12 jcs break;
6836 1e37a5c2 2019-05-12 jcs case 'j':
6837 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6838 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6839 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6840 1e37a5c2 2019-05-12 jcs s->selected++;
6841 1e37a5c2 2019-05-12 jcs break;
6842 1e37a5c2 2019-05-12 jcs }
6843 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6844 640cd7ff 2022-06-22 mark == NULL) {
6845 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6846 640cd7ff 2022-06-22 mark view->count = 0;
6847 1e37a5c2 2019-05-12 jcs break;
6848 640cd7ff 2022-06-22 mark }
6849 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6850 1e37a5c2 2019-05-12 jcs break;
6851 83cc4199 2022-06-13 stsp case CTRL('d'):
6852 33c3719a 2022-06-15 stsp case 'd':
6853 83cc4199 2022-06-13 stsp nscroll /= 2;
6854 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6855 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6856 ea025d1d 2020-02-22 naddy case CTRL('f'):
6857 61417565 2022-06-20 mark case 'f':
6858 48bb96f0 2022-06-20 naddy case ' ':
6859 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6860 1e37a5c2 2019-05-12 jcs == NULL) {
6861 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6862 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6863 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6864 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6865 640cd7ff 2022-06-22 mark else
6866 640cd7ff 2022-06-22 mark view->count = 0;
6867 1e37a5c2 2019-05-12 jcs break;
6868 1e37a5c2 2019-05-12 jcs }
6869 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6870 1e37a5c2 2019-05-12 jcs break;
6871 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6872 1e37a5c2 2019-05-12 jcs case '\r':
6873 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6874 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6875 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6876 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6877 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6878 640cd7ff 2022-06-22 mark view->count = 0;
6879 1e37a5c2 2019-05-12 jcs break;
6880 640cd7ff 2022-06-22 mark }
6881 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6882 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6883 1e37a5c2 2019-05-12 jcs entry);
6884 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6885 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6886 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6887 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6888 1e37a5c2 2019-05-12 jcs s->selected_entry =
6889 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6890 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6891 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6892 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6893 9b058f45 2022-06-30 mark if (err)
6894 9b058f45 2022-06-30 mark break;
6895 9b058f45 2022-06-30 mark }
6896 1e37a5c2 2019-05-12 jcs free(parent);
6897 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6898 56e0773d 2019-11-28 stsp s->selected_entry))) {
6899 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6900 640cd7ff 2022-06-22 mark view->count = 0;
6901 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6902 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6903 1e37a5c2 2019-05-12 jcs if (err)
6904 1e37a5c2 2019-05-12 jcs break;
6905 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6906 941e9f74 2019-05-21 stsp if (err) {
6907 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6908 1e37a5c2 2019-05-12 jcs break;
6909 1e37a5c2 2019-05-12 jcs }
6910 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6911 56e0773d 2019-11-28 stsp s->selected_entry))) {
6912 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6913 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6914 1e37a5c2 2019-05-12 jcs
6915 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6916 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6917 9b058f45 2022-06-30 mark
6918 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6919 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6920 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6921 1e37a5c2 2019-05-12 jcs if (err)
6922 1e37a5c2 2019-05-12 jcs break;
6923 9b058f45 2022-06-30 mark
6924 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6925 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6926 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6927 9b058f45 2022-06-30 mark if (err)
6928 9b058f45 2022-06-30 mark break;
6929 9b058f45 2022-06-30 mark }
6930 9b058f45 2022-06-30 mark
6931 640cd7ff 2022-06-22 mark view->count = 0;
6932 e78dc838 2020-12-04 stsp view->focussed = 0;
6933 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6934 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6935 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6936 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6937 3c1dfe12 2022-07-08 mark view_transfer_size(blame_view, view);
6938 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6939 669b5ffa 2018-10-07 stsp if (err)
6940 669b5ffa 2018-10-07 stsp return err;
6941 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6942 0dbbbe90 2022-06-17 op if (err)
6943 0dbbbe90 2022-06-17 op return err;
6944 e78dc838 2020-12-04 stsp view->focus_child = 1;
6945 669b5ffa 2018-10-07 stsp } else
6946 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6947 1e37a5c2 2019-05-12 jcs }
6948 1e37a5c2 2019-05-12 jcs break;
6949 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6950 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6951 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6952 640cd7ff 2022-06-22 mark view->count = 0;
6953 1e37a5c2 2019-05-12 jcs break;
6954 1e37a5c2 2019-05-12 jcs default:
6955 640cd7ff 2022-06-22 mark view->count = 0;
6956 1e37a5c2 2019-05-12 jcs break;
6957 ffd1d5e5 2018-06-23 stsp }
6958 e5a0f69f 2018-08-18 stsp
6959 ffd1d5e5 2018-06-23 stsp return err;
6960 9f7d7167 2018-04-29 stsp }
6961 9f7d7167 2018-04-29 stsp
6962 ffd1d5e5 2018-06-23 stsp __dead static void
6963 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6964 ffd1d5e5 2018-06-23 stsp {
6965 ffd1d5e5 2018-06-23 stsp endwin();
6966 87411fa9 2022-06-16 stsp fprintf(stderr,
6967 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6968 ffd1d5e5 2018-06-23 stsp getprogname());
6969 ffd1d5e5 2018-06-23 stsp exit(1);
6970 ffd1d5e5 2018-06-23 stsp }
6971 ffd1d5e5 2018-06-23 stsp
6972 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6973 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6974 ffd1d5e5 2018-06-23 stsp {
6975 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6976 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6977 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6978 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6979 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6980 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6981 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6982 4e97c21c 2020-12-06 stsp char *label = NULL;
6983 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6984 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6985 ffd1d5e5 2018-06-23 stsp int ch;
6986 5221c383 2018-08-01 stsp struct tog_view *view;
6987 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6988 70ac5f84 2019-03-28 stsp
6989 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6990 ffd1d5e5 2018-06-23 stsp switch (ch) {
6991 ffd1d5e5 2018-06-23 stsp case 'c':
6992 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6993 74283ab8 2020-02-07 stsp break;
6994 74283ab8 2020-02-07 stsp case 'r':
6995 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6996 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6997 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6998 74283ab8 2020-02-07 stsp optarg);
6999 ffd1d5e5 2018-06-23 stsp break;
7000 ffd1d5e5 2018-06-23 stsp default:
7001 e99e2d15 2020-11-24 naddy usage_tree();
7002 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7003 ffd1d5e5 2018-06-23 stsp }
7004 ffd1d5e5 2018-06-23 stsp }
7005 ffd1d5e5 2018-06-23 stsp
7006 ffd1d5e5 2018-06-23 stsp argc -= optind;
7007 ffd1d5e5 2018-06-23 stsp argv += optind;
7008 ffd1d5e5 2018-06-23 stsp
7009 55cccc34 2020-02-20 stsp if (argc > 1)
7010 e99e2d15 2020-11-24 naddy usage_tree();
7011 0ae84acc 2022-06-15 tracey
7012 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7013 0ae84acc 2022-06-15 tracey if (error != NULL)
7014 0ae84acc 2022-06-15 tracey goto done;
7015 74283ab8 2020-02-07 stsp
7016 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7017 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7018 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7019 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7020 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7021 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7022 c156c7a4 2020-12-18 stsp goto done;
7023 55cccc34 2020-02-20 stsp if (worktree)
7024 52185f70 2019-02-05 stsp repo_path =
7025 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7026 55cccc34 2020-02-20 stsp else
7027 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7028 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7029 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7030 c156c7a4 2020-12-18 stsp goto done;
7031 c156c7a4 2020-12-18 stsp }
7032 55cccc34 2020-02-20 stsp }
7033 a915003a 2019-02-05 stsp
7034 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7035 c02c541e 2019-03-29 stsp if (error != NULL)
7036 52185f70 2019-02-05 stsp goto done;
7037 d188b9a6 2019-01-04 stsp
7038 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7039 55cccc34 2020-02-20 stsp repo, worktree);
7040 55cccc34 2020-02-20 stsp if (error)
7041 55cccc34 2020-02-20 stsp goto done;
7042 55cccc34 2020-02-20 stsp
7043 55cccc34 2020-02-20 stsp init_curses();
7044 55cccc34 2020-02-20 stsp
7045 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7046 c02c541e 2019-03-29 stsp if (error)
7047 52185f70 2019-02-05 stsp goto done;
7048 ffd1d5e5 2018-06-23 stsp
7049 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7050 51a10b52 2020-12-26 stsp if (error)
7051 51a10b52 2020-12-26 stsp goto done;
7052 51a10b52 2020-12-26 stsp
7053 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7054 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7055 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7056 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7057 4e97c21c 2020-12-06 stsp if (error)
7058 4e97c21c 2020-12-06 stsp goto done;
7059 4e97c21c 2020-12-06 stsp head_ref_name = label;
7060 4e97c21c 2020-12-06 stsp } else {
7061 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7062 4e97c21c 2020-12-06 stsp if (error == NULL)
7063 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7064 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7065 4e97c21c 2020-12-06 stsp goto done;
7066 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7067 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7068 4e97c21c 2020-12-06 stsp if (error)
7069 4e97c21c 2020-12-06 stsp goto done;
7070 4e97c21c 2020-12-06 stsp }
7071 ffd1d5e5 2018-06-23 stsp
7072 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7073 a44927cc 2022-04-07 stsp if (error)
7074 a44927cc 2022-04-07 stsp goto done;
7075 a44927cc 2022-04-07 stsp
7076 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7077 5221c383 2018-08-01 stsp if (view == NULL) {
7078 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7079 5221c383 2018-08-01 stsp goto done;
7080 5221c383 2018-08-01 stsp }
7081 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7082 ad80ab7b 2018-08-04 stsp if (error)
7083 ad80ab7b 2018-08-04 stsp goto done;
7084 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7085 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7086 d91faf3b 2020-12-01 naddy in_repo_path);
7087 55cccc34 2020-02-20 stsp if (error)
7088 55cccc34 2020-02-20 stsp goto done;
7089 55cccc34 2020-02-20 stsp }
7090 55cccc34 2020-02-20 stsp
7091 55cccc34 2020-02-20 stsp if (worktree) {
7092 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7093 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7094 55cccc34 2020-02-20 stsp worktree = NULL;
7095 55cccc34 2020-02-20 stsp }
7096 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7097 ffd1d5e5 2018-06-23 stsp done:
7098 52185f70 2019-02-05 stsp free(repo_path);
7099 e4a0e26d 2020-02-20 stsp free(cwd);
7100 ffd1d5e5 2018-06-23 stsp free(commit_id);
7101 4e97c21c 2020-12-06 stsp free(label);
7102 486cd271 2020-12-06 stsp if (ref)
7103 486cd271 2020-12-06 stsp got_ref_close(ref);
7104 1d0f4054 2021-06-17 stsp if (repo) {
7105 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7106 1d0f4054 2021-06-17 stsp if (error == NULL)
7107 1d0f4054 2021-06-17 stsp error = close_err;
7108 1d0f4054 2021-06-17 stsp }
7109 0ae84acc 2022-06-15 tracey if (pack_fds) {
7110 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7111 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7112 0ae84acc 2022-06-15 tracey if (error == NULL)
7113 0ae84acc 2022-06-15 tracey error = pack_err;
7114 0ae84acc 2022-06-15 tracey }
7115 51a10b52 2020-12-26 stsp tog_free_refs();
7116 ffd1d5e5 2018-06-23 stsp return error;
7117 6458efa5 2020-11-24 stsp }
7118 6458efa5 2020-11-24 stsp
7119 6458efa5 2020-11-24 stsp static const struct got_error *
7120 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7121 6458efa5 2020-11-24 stsp {
7122 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7123 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7124 6458efa5 2020-11-24 stsp
7125 6458efa5 2020-11-24 stsp s->nrefs = 0;
7126 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7127 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7128 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7129 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7130 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7131 6458efa5 2020-11-24 stsp continue;
7132 6458efa5 2020-11-24 stsp
7133 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7134 6458efa5 2020-11-24 stsp if (re == NULL)
7135 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7136 6458efa5 2020-11-24 stsp
7137 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7138 8924d611 2020-12-26 stsp if (re->ref == NULL)
7139 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7140 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7141 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7142 6458efa5 2020-11-24 stsp }
7143 6458efa5 2020-11-24 stsp
7144 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7145 6458efa5 2020-11-24 stsp return NULL;
7146 6458efa5 2020-11-24 stsp }
7147 6458efa5 2020-11-24 stsp
7148 336075a4 2022-06-25 op static void
7149 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7150 6458efa5 2020-11-24 stsp {
7151 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7152 6458efa5 2020-11-24 stsp
7153 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7154 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7155 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7156 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7157 6458efa5 2020-11-24 stsp free(re);
7158 6458efa5 2020-11-24 stsp }
7159 6458efa5 2020-11-24 stsp }
7160 6458efa5 2020-11-24 stsp
7161 6458efa5 2020-11-24 stsp static const struct got_error *
7162 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7163 6458efa5 2020-11-24 stsp {
7164 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7165 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7166 6458efa5 2020-11-24 stsp
7167 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7168 6458efa5 2020-11-24 stsp s->repo = repo;
7169 6458efa5 2020-11-24 stsp
7170 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7171 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7172 6458efa5 2020-11-24 stsp
7173 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7174 6458efa5 2020-11-24 stsp if (err)
7175 6458efa5 2020-11-24 stsp return err;
7176 34ba6917 2020-11-30 stsp
7177 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7178 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7179 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7180 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7181 6458efa5 2020-11-24 stsp if (err)
7182 6458efa5 2020-11-24 stsp goto done;
7183 6458efa5 2020-11-24 stsp
7184 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7185 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7186 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7187 6458efa5 2020-11-24 stsp if (err)
7188 6458efa5 2020-11-24 stsp goto done;
7189 6458efa5 2020-11-24 stsp
7190 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7191 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7192 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7193 cc488aa7 2022-01-23 stsp if (err)
7194 cc488aa7 2022-01-23 stsp goto done;
7195 cc488aa7 2022-01-23 stsp
7196 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7197 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7198 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7199 6458efa5 2020-11-24 stsp if (err)
7200 6458efa5 2020-11-24 stsp goto done;
7201 6458efa5 2020-11-24 stsp }
7202 6458efa5 2020-11-24 stsp
7203 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7204 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7205 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7206 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7207 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7208 6458efa5 2020-11-24 stsp done:
7209 6458efa5 2020-11-24 stsp if (err)
7210 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7211 6458efa5 2020-11-24 stsp return err;
7212 6458efa5 2020-11-24 stsp }
7213 6458efa5 2020-11-24 stsp
7214 6458efa5 2020-11-24 stsp static const struct got_error *
7215 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7216 6458efa5 2020-11-24 stsp {
7217 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7218 6458efa5 2020-11-24 stsp
7219 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7220 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7221 6458efa5 2020-11-24 stsp
7222 6458efa5 2020-11-24 stsp return NULL;
7223 9f7d7167 2018-04-29 stsp }
7224 ce5b7c56 2019-07-09 stsp
7225 6458efa5 2020-11-24 stsp static const struct got_error *
7226 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7227 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7228 6458efa5 2020-11-24 stsp {
7229 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7230 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7231 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7232 6458efa5 2020-11-24 stsp int obj_type;
7233 6458efa5 2020-11-24 stsp
7234 c42c9805 2020-11-24 stsp *commit_id = NULL;
7235 6458efa5 2020-11-24 stsp
7236 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7237 6458efa5 2020-11-24 stsp if (err)
7238 6458efa5 2020-11-24 stsp return err;
7239 6458efa5 2020-11-24 stsp
7240 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7241 6458efa5 2020-11-24 stsp if (err)
7242 6458efa5 2020-11-24 stsp goto done;
7243 6458efa5 2020-11-24 stsp
7244 6458efa5 2020-11-24 stsp switch (obj_type) {
7245 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7246 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7247 6458efa5 2020-11-24 stsp break;
7248 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7249 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7250 6458efa5 2020-11-24 stsp if (err)
7251 6458efa5 2020-11-24 stsp goto done;
7252 c42c9805 2020-11-24 stsp free(obj_id);
7253 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7254 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7255 c42c9805 2020-11-24 stsp if (err)
7256 6458efa5 2020-11-24 stsp goto done;
7257 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7258 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7259 c42c9805 2020-11-24 stsp goto done;
7260 c42c9805 2020-11-24 stsp }
7261 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7262 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7263 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7264 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7265 c42c9805 2020-11-24 stsp goto done;
7266 c42c9805 2020-11-24 stsp }
7267 6458efa5 2020-11-24 stsp break;
7268 6458efa5 2020-11-24 stsp default:
7269 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7270 c42c9805 2020-11-24 stsp break;
7271 6458efa5 2020-11-24 stsp }
7272 6458efa5 2020-11-24 stsp
7273 c42c9805 2020-11-24 stsp done:
7274 c42c9805 2020-11-24 stsp if (tag)
7275 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7276 c42c9805 2020-11-24 stsp if (err) {
7277 c42c9805 2020-11-24 stsp free(*commit_id);
7278 c42c9805 2020-11-24 stsp *commit_id = NULL;
7279 c42c9805 2020-11-24 stsp }
7280 c42c9805 2020-11-24 stsp return err;
7281 c42c9805 2020-11-24 stsp }
7282 c42c9805 2020-11-24 stsp
7283 c42c9805 2020-11-24 stsp static const struct got_error *
7284 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7285 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7286 c42c9805 2020-11-24 stsp {
7287 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7288 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7289 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7290 c42c9805 2020-11-24 stsp
7291 c42c9805 2020-11-24 stsp *new_view = NULL;
7292 c42c9805 2020-11-24 stsp
7293 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7294 c42c9805 2020-11-24 stsp if (err) {
7295 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7296 c42c9805 2020-11-24 stsp return err;
7297 c42c9805 2020-11-24 stsp else
7298 c42c9805 2020-11-24 stsp return NULL;
7299 c42c9805 2020-11-24 stsp }
7300 c42c9805 2020-11-24 stsp
7301 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7302 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7303 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7304 6458efa5 2020-11-24 stsp goto done;
7305 6458efa5 2020-11-24 stsp }
7306 6458efa5 2020-11-24 stsp
7307 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7308 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7309 6458efa5 2020-11-24 stsp done:
7310 6458efa5 2020-11-24 stsp if (err)
7311 6458efa5 2020-11-24 stsp view_close(log_view);
7312 6458efa5 2020-11-24 stsp else
7313 6458efa5 2020-11-24 stsp *new_view = log_view;
7314 c42c9805 2020-11-24 stsp free(commit_id);
7315 6458efa5 2020-11-24 stsp return err;
7316 6458efa5 2020-11-24 stsp }
7317 6458efa5 2020-11-24 stsp
7318 ce5b7c56 2019-07-09 stsp static void
7319 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7320 6458efa5 2020-11-24 stsp {
7321 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7322 34ba6917 2020-11-30 stsp int i = 0;
7323 6458efa5 2020-11-24 stsp
7324 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7325 6458efa5 2020-11-24 stsp return;
7326 6458efa5 2020-11-24 stsp
7327 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7328 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7329 34ba6917 2020-11-30 stsp if (re == NULL)
7330 34ba6917 2020-11-30 stsp break;
7331 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7332 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7333 6458efa5 2020-11-24 stsp }
7334 6458efa5 2020-11-24 stsp }
7335 6458efa5 2020-11-24 stsp
7336 9b058f45 2022-06-30 mark static const struct got_error *
7337 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7338 6458efa5 2020-11-24 stsp {
7339 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7340 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7341 6458efa5 2020-11-24 stsp int n = 0;
7342 6458efa5 2020-11-24 stsp
7343 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7344 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7345 6458efa5 2020-11-24 stsp else
7346 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7347 6458efa5 2020-11-24 stsp
7348 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7349 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7350 9b058f45 2022-06-30 mark if (last)
7351 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7352 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7353 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7354 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7355 6458efa5 2020-11-24 stsp }
7356 6458efa5 2020-11-24 stsp }
7357 9b058f45 2022-06-30 mark
7358 9b058f45 2022-06-30 mark return NULL;
7359 6458efa5 2020-11-24 stsp }
7360 6458efa5 2020-11-24 stsp
7361 6458efa5 2020-11-24 stsp static const struct got_error *
7362 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7363 6458efa5 2020-11-24 stsp {
7364 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7365 6458efa5 2020-11-24 stsp
7366 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7367 6458efa5 2020-11-24 stsp return NULL;
7368 6458efa5 2020-11-24 stsp }
7369 6458efa5 2020-11-24 stsp
7370 6458efa5 2020-11-24 stsp static int
7371 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7372 6458efa5 2020-11-24 stsp {
7373 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7374 6458efa5 2020-11-24 stsp
7375 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7376 6458efa5 2020-11-24 stsp 0) == 0;
7377 6458efa5 2020-11-24 stsp }
7378 6458efa5 2020-11-24 stsp
7379 6458efa5 2020-11-24 stsp static const struct got_error *
7380 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7381 6458efa5 2020-11-24 stsp {
7382 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7383 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7384 6458efa5 2020-11-24 stsp
7385 6458efa5 2020-11-24 stsp if (!view->searching) {
7386 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7387 6458efa5 2020-11-24 stsp return NULL;
7388 6458efa5 2020-11-24 stsp }
7389 6458efa5 2020-11-24 stsp
7390 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7391 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7392 6458efa5 2020-11-24 stsp if (s->selected_entry)
7393 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7394 6458efa5 2020-11-24 stsp else
7395 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7396 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7397 6458efa5 2020-11-24 stsp } else {
7398 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7399 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7400 6458efa5 2020-11-24 stsp else
7401 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7402 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7403 6458efa5 2020-11-24 stsp }
7404 6458efa5 2020-11-24 stsp } else {
7405 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7406 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7407 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7408 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7409 6458efa5 2020-11-24 stsp else
7410 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7411 6458efa5 2020-11-24 stsp }
7412 6458efa5 2020-11-24 stsp
7413 6458efa5 2020-11-24 stsp while (1) {
7414 6458efa5 2020-11-24 stsp if (re == NULL) {
7415 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7416 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7417 6458efa5 2020-11-24 stsp return NULL;
7418 6458efa5 2020-11-24 stsp }
7419 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7420 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7421 6458efa5 2020-11-24 stsp else
7422 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7423 6458efa5 2020-11-24 stsp }
7424 6458efa5 2020-11-24 stsp
7425 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7426 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7427 6458efa5 2020-11-24 stsp s->matched_entry = re;
7428 6458efa5 2020-11-24 stsp break;
7429 6458efa5 2020-11-24 stsp }
7430 6458efa5 2020-11-24 stsp
7431 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7432 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7433 6458efa5 2020-11-24 stsp else
7434 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7435 6458efa5 2020-11-24 stsp }
7436 6458efa5 2020-11-24 stsp
7437 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7438 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7439 6458efa5 2020-11-24 stsp s->selected = 0;
7440 6458efa5 2020-11-24 stsp }
7441 6458efa5 2020-11-24 stsp
7442 6458efa5 2020-11-24 stsp return NULL;
7443 6458efa5 2020-11-24 stsp }
7444 6458efa5 2020-11-24 stsp
7445 6458efa5 2020-11-24 stsp static const struct got_error *
7446 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7447 6458efa5 2020-11-24 stsp {
7448 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7449 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7450 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7451 6458efa5 2020-11-24 stsp char *line = NULL;
7452 6458efa5 2020-11-24 stsp wchar_t *wline;
7453 6458efa5 2020-11-24 stsp struct tog_color *tc;
7454 6458efa5 2020-11-24 stsp int width, n;
7455 6458efa5 2020-11-24 stsp int limit = view->nlines;
7456 6458efa5 2020-11-24 stsp
7457 6458efa5 2020-11-24 stsp werase(view->window);
7458 6458efa5 2020-11-24 stsp
7459 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7460 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7461 9b058f45 2022-06-30 mark --limit; /* border */
7462 6458efa5 2020-11-24 stsp
7463 6458efa5 2020-11-24 stsp if (limit == 0)
7464 6458efa5 2020-11-24 stsp return NULL;
7465 6458efa5 2020-11-24 stsp
7466 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7467 6458efa5 2020-11-24 stsp
7468 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7469 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7470 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7471 6458efa5 2020-11-24 stsp
7472 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7473 6458efa5 2020-11-24 stsp if (err) {
7474 6458efa5 2020-11-24 stsp free(line);
7475 6458efa5 2020-11-24 stsp return err;
7476 6458efa5 2020-11-24 stsp }
7477 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7478 6458efa5 2020-11-24 stsp wstandout(view->window);
7479 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7480 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7481 6458efa5 2020-11-24 stsp wstandend(view->window);
7482 6458efa5 2020-11-24 stsp free(wline);
7483 6458efa5 2020-11-24 stsp wline = NULL;
7484 6458efa5 2020-11-24 stsp free(line);
7485 6458efa5 2020-11-24 stsp line = NULL;
7486 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7487 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7488 6458efa5 2020-11-24 stsp if (--limit <= 0)
7489 6458efa5 2020-11-24 stsp return NULL;
7490 6458efa5 2020-11-24 stsp
7491 6458efa5 2020-11-24 stsp n = 0;
7492 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7493 6458efa5 2020-11-24 stsp char *line = NULL;
7494 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7495 6458efa5 2020-11-24 stsp
7496 b4996bee 2022-06-16 stsp if (s->show_date) {
7497 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7498 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7499 b4996bee 2022-06-16 stsp struct got_object_id *id;
7500 b4996bee 2022-06-16 stsp struct tm tm;
7501 b4996bee 2022-06-16 stsp time_t t;
7502 b4996bee 2022-06-16 stsp
7503 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7504 b4996bee 2022-06-16 stsp if (err)
7505 b4996bee 2022-06-16 stsp return err;
7506 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7507 b4996bee 2022-06-16 stsp if (err) {
7508 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7509 b4996bee 2022-06-16 stsp free(id);
7510 b4996bee 2022-06-16 stsp return err;
7511 b4996bee 2022-06-16 stsp }
7512 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7513 b4996bee 2022-06-16 stsp id);
7514 b4996bee 2022-06-16 stsp if (err) {
7515 b4996bee 2022-06-16 stsp free(id);
7516 b4996bee 2022-06-16 stsp return err;
7517 b4996bee 2022-06-16 stsp }
7518 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7519 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7520 b4996bee 2022-06-16 stsp } else {
7521 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7522 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7523 b4996bee 2022-06-16 stsp }
7524 b4996bee 2022-06-16 stsp free(id);
7525 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7526 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7527 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7528 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7529 b4996bee 2022-06-16 stsp }
7530 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7531 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7532 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7533 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7534 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7535 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7536 6458efa5 2020-11-24 stsp struct got_object_id *id;
7537 6458efa5 2020-11-24 stsp char *id_str;
7538 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7539 6458efa5 2020-11-24 stsp if (err)
7540 6458efa5 2020-11-24 stsp return err;
7541 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7542 6458efa5 2020-11-24 stsp if (err) {
7543 6458efa5 2020-11-24 stsp free(id);
7544 6458efa5 2020-11-24 stsp return err;
7545 6458efa5 2020-11-24 stsp }
7546 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7547 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7548 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7549 6458efa5 2020-11-24 stsp free(id);
7550 6458efa5 2020-11-24 stsp free(id_str);
7551 6458efa5 2020-11-24 stsp return err;
7552 6458efa5 2020-11-24 stsp }
7553 6458efa5 2020-11-24 stsp free(id);
7554 6458efa5 2020-11-24 stsp free(id_str);
7555 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7556 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7557 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7558 6458efa5 2020-11-24 stsp
7559 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7560 ccda2f4d 2022-06-16 stsp 0, 0);
7561 6458efa5 2020-11-24 stsp if (err) {
7562 6458efa5 2020-11-24 stsp free(line);
7563 6458efa5 2020-11-24 stsp return err;
7564 6458efa5 2020-11-24 stsp }
7565 6458efa5 2020-11-24 stsp if (n == s->selected) {
7566 6458efa5 2020-11-24 stsp if (view->focussed)
7567 6458efa5 2020-11-24 stsp wstandout(view->window);
7568 6458efa5 2020-11-24 stsp s->selected_entry = re;
7569 6458efa5 2020-11-24 stsp }
7570 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7571 6458efa5 2020-11-24 stsp if (tc)
7572 6458efa5 2020-11-24 stsp wattr_on(view->window,
7573 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7574 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7575 6458efa5 2020-11-24 stsp if (tc)
7576 6458efa5 2020-11-24 stsp wattr_off(view->window,
7577 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7578 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7579 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7580 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7581 6458efa5 2020-11-24 stsp wstandend(view->window);
7582 6458efa5 2020-11-24 stsp free(line);
7583 6458efa5 2020-11-24 stsp free(wline);
7584 6458efa5 2020-11-24 stsp wline = NULL;
7585 6458efa5 2020-11-24 stsp n++;
7586 6458efa5 2020-11-24 stsp s->ndisplayed++;
7587 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7588 6458efa5 2020-11-24 stsp
7589 6458efa5 2020-11-24 stsp limit--;
7590 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7591 6458efa5 2020-11-24 stsp }
7592 6458efa5 2020-11-24 stsp
7593 9b058f45 2022-06-30 mark view_border(view);
7594 6458efa5 2020-11-24 stsp return err;
7595 6458efa5 2020-11-24 stsp }
7596 6458efa5 2020-11-24 stsp
7597 6458efa5 2020-11-24 stsp static const struct got_error *
7598 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7599 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7600 c42c9805 2020-11-24 stsp {
7601 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7602 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7603 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7604 c42c9805 2020-11-24 stsp
7605 c42c9805 2020-11-24 stsp *new_view = NULL;
7606 c42c9805 2020-11-24 stsp
7607 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7608 c42c9805 2020-11-24 stsp if (err) {
7609 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7610 c42c9805 2020-11-24 stsp return err;
7611 c42c9805 2020-11-24 stsp else
7612 c42c9805 2020-11-24 stsp return NULL;
7613 c42c9805 2020-11-24 stsp }
7614 c42c9805 2020-11-24 stsp
7615 c42c9805 2020-11-24 stsp
7616 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7617 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7618 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7619 c42c9805 2020-11-24 stsp goto done;
7620 c42c9805 2020-11-24 stsp }
7621 c42c9805 2020-11-24 stsp
7622 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7623 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7624 c42c9805 2020-11-24 stsp if (err)
7625 c42c9805 2020-11-24 stsp goto done;
7626 c42c9805 2020-11-24 stsp
7627 c42c9805 2020-11-24 stsp *new_view = tree_view;
7628 c42c9805 2020-11-24 stsp done:
7629 c42c9805 2020-11-24 stsp free(commit_id);
7630 c42c9805 2020-11-24 stsp return err;
7631 c42c9805 2020-11-24 stsp }
7632 c42c9805 2020-11-24 stsp static const struct got_error *
7633 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7634 6458efa5 2020-11-24 stsp {
7635 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7636 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7637 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7638 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7639 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7640 6458efa5 2020-11-24 stsp
7641 6458efa5 2020-11-24 stsp switch (ch) {
7642 6458efa5 2020-11-24 stsp case 'i':
7643 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7644 640cd7ff 2022-06-22 mark view->count = 0;
7645 7f66531d 2021-11-16 stsp break;
7646 b4996bee 2022-06-16 stsp case 'm':
7647 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7648 640cd7ff 2022-06-22 mark view->count = 0;
7649 b4996bee 2022-06-16 stsp break;
7650 07a065fe 2021-11-20 stsp case 'o':
7651 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7652 640cd7ff 2022-06-22 mark view->count = 0;
7653 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7654 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7655 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7656 50617b77 2021-11-20 stsp if (err)
7657 50617b77 2021-11-20 stsp break;
7658 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7659 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7660 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7661 7f66531d 2021-11-16 stsp if (err)
7662 7f66531d 2021-11-16 stsp break;
7663 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7664 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7665 6458efa5 2020-11-24 stsp break;
7666 6458efa5 2020-11-24 stsp case KEY_ENTER:
7667 6458efa5 2020-11-24 stsp case '\r':
7668 640cd7ff 2022-06-22 mark view->count = 0;
7669 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7670 6458efa5 2020-11-24 stsp break;
7671 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7672 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7673 9b058f45 2022-06-30 mark
7674 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7675 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7676 9b058f45 2022-06-30 mark if (err)
7677 9b058f45 2022-06-30 mark break;
7678 9b058f45 2022-06-30 mark
7679 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7680 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7681 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7682 9b058f45 2022-06-30 mark if (err)
7683 9b058f45 2022-06-30 mark break;
7684 9b058f45 2022-06-30 mark }
7685 9b058f45 2022-06-30 mark
7686 e78dc838 2020-12-04 stsp view->focussed = 0;
7687 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7688 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7689 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7690 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7691 3c1dfe12 2022-07-08 mark view_transfer_size(log_view, view);
7692 6458efa5 2020-11-24 stsp err = view_close_child(view);
7693 6458efa5 2020-11-24 stsp if (err)
7694 6458efa5 2020-11-24 stsp return err;
7695 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7696 0dbbbe90 2022-06-17 op if (err)
7697 0dbbbe90 2022-06-17 op return err;
7698 e78dc838 2020-12-04 stsp view->focus_child = 1;
7699 6458efa5 2020-11-24 stsp } else
7700 6458efa5 2020-11-24 stsp *new_view = log_view;
7701 6458efa5 2020-11-24 stsp break;
7702 c42c9805 2020-11-24 stsp case 't':
7703 640cd7ff 2022-06-22 mark view->count = 0;
7704 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7705 c42c9805 2020-11-24 stsp break;
7706 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7707 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7708 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7709 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7710 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7711 c42c9805 2020-11-24 stsp break;
7712 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7713 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7714 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7715 49b24ee5 2022-07-03 mark if (err)
7716 49b24ee5 2022-07-03 mark break;
7717 49b24ee5 2022-07-03 mark }
7718 e78dc838 2020-12-04 stsp view->focussed = 0;
7719 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7720 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7721 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7722 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7723 3c1dfe12 2022-07-08 mark view_transfer_size(tree_view, view);
7724 c42c9805 2020-11-24 stsp err = view_close_child(view);
7725 c42c9805 2020-11-24 stsp if (err)
7726 c42c9805 2020-11-24 stsp return err;
7727 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7728 0dbbbe90 2022-06-17 op if (err)
7729 0dbbbe90 2022-06-17 op return err;
7730 e78dc838 2020-12-04 stsp view->focus_child = 1;
7731 c42c9805 2020-11-24 stsp } else
7732 c42c9805 2020-11-24 stsp *new_view = tree_view;
7733 c42c9805 2020-11-24 stsp break;
7734 e4526bf5 2021-09-03 naddy case 'g':
7735 e4526bf5 2021-09-03 naddy case KEY_HOME:
7736 e4526bf5 2021-09-03 naddy s->selected = 0;
7737 640cd7ff 2022-06-22 mark view->count = 0;
7738 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7739 e4526bf5 2021-09-03 naddy break;
7740 e4526bf5 2021-09-03 naddy case 'G':
7741 9b058f45 2022-06-30 mark case KEY_END: {
7742 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7743 9b058f45 2022-06-30 mark
7744 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7745 9b058f45 2022-06-30 mark --eos; /* border */
7746 e4526bf5 2021-09-03 naddy s->selected = 0;
7747 640cd7ff 2022-06-22 mark view->count = 0;
7748 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7749 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7750 e4526bf5 2021-09-03 naddy if (re == NULL)
7751 e4526bf5 2021-09-03 naddy break;
7752 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7753 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7754 e4526bf5 2021-09-03 naddy }
7755 e4526bf5 2021-09-03 naddy if (n > 0)
7756 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7757 e4526bf5 2021-09-03 naddy break;
7758 9b058f45 2022-06-30 mark }
7759 6458efa5 2020-11-24 stsp case 'k':
7760 6458efa5 2020-11-24 stsp case KEY_UP:
7761 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7762 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7763 6458efa5 2020-11-24 stsp s->selected--;
7764 6458efa5 2020-11-24 stsp break;
7765 34ba6917 2020-11-30 stsp }
7766 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7767 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7768 640cd7ff 2022-06-22 mark view->count = 0;
7769 6458efa5 2020-11-24 stsp break;
7770 83cc4199 2022-06-13 stsp case CTRL('u'):
7771 33c3719a 2022-06-15 stsp case 'u':
7772 83cc4199 2022-06-13 stsp nscroll /= 2;
7773 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7774 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7775 6458efa5 2020-11-24 stsp case CTRL('b'):
7776 61417565 2022-06-20 mark case 'b':
7777 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7778 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7779 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7780 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7781 640cd7ff 2022-06-22 mark view->count = 0;
7782 6458efa5 2020-11-24 stsp break;
7783 6458efa5 2020-11-24 stsp case 'j':
7784 6458efa5 2020-11-24 stsp case KEY_DOWN:
7785 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7786 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7787 6458efa5 2020-11-24 stsp s->selected++;
7788 6458efa5 2020-11-24 stsp break;
7789 6458efa5 2020-11-24 stsp }
7790 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7791 6458efa5 2020-11-24 stsp /* can't scroll any further */
7792 640cd7ff 2022-06-22 mark view->count = 0;
7793 6458efa5 2020-11-24 stsp break;
7794 640cd7ff 2022-06-22 mark }
7795 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7796 6458efa5 2020-11-24 stsp break;
7797 83cc4199 2022-06-13 stsp case CTRL('d'):
7798 33c3719a 2022-06-15 stsp case 'd':
7799 83cc4199 2022-06-13 stsp nscroll /= 2;
7800 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7801 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7802 6458efa5 2020-11-24 stsp case CTRL('f'):
7803 61417565 2022-06-20 mark case 'f':
7804 48bb96f0 2022-06-20 naddy case ' ':
7805 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7806 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7807 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7808 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7809 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7810 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7811 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7812 640cd7ff 2022-06-22 mark view->count = 0;
7813 6458efa5 2020-11-24 stsp break;
7814 6458efa5 2020-11-24 stsp }
7815 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7816 6458efa5 2020-11-24 stsp break;
7817 6458efa5 2020-11-24 stsp case CTRL('l'):
7818 640cd7ff 2022-06-22 mark view->count = 0;
7819 8924d611 2020-12-26 stsp tog_free_refs();
7820 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7821 8924d611 2020-12-26 stsp if (err)
7822 8924d611 2020-12-26 stsp break;
7823 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7824 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7825 6458efa5 2020-11-24 stsp break;
7826 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7827 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7828 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7829 6458efa5 2020-11-24 stsp break;
7830 6458efa5 2020-11-24 stsp default:
7831 640cd7ff 2022-06-22 mark view->count = 0;
7832 6458efa5 2020-11-24 stsp break;
7833 6458efa5 2020-11-24 stsp }
7834 6458efa5 2020-11-24 stsp
7835 6458efa5 2020-11-24 stsp return err;
7836 6458efa5 2020-11-24 stsp }
7837 6458efa5 2020-11-24 stsp
7838 6458efa5 2020-11-24 stsp __dead static void
7839 6458efa5 2020-11-24 stsp usage_ref(void)
7840 6458efa5 2020-11-24 stsp {
7841 6458efa5 2020-11-24 stsp endwin();
7842 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7843 6458efa5 2020-11-24 stsp getprogname());
7844 6458efa5 2020-11-24 stsp exit(1);
7845 6458efa5 2020-11-24 stsp }
7846 6458efa5 2020-11-24 stsp
7847 6458efa5 2020-11-24 stsp static const struct got_error *
7848 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7849 6458efa5 2020-11-24 stsp {
7850 6458efa5 2020-11-24 stsp const struct got_error *error;
7851 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7852 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7853 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7854 6458efa5 2020-11-24 stsp int ch;
7855 6458efa5 2020-11-24 stsp struct tog_view *view;
7856 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7857 6458efa5 2020-11-24 stsp
7858 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7859 6458efa5 2020-11-24 stsp switch (ch) {
7860 6458efa5 2020-11-24 stsp case 'r':
7861 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7862 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7863 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7864 6458efa5 2020-11-24 stsp optarg);
7865 6458efa5 2020-11-24 stsp break;
7866 6458efa5 2020-11-24 stsp default:
7867 e99e2d15 2020-11-24 naddy usage_ref();
7868 6458efa5 2020-11-24 stsp /* NOTREACHED */
7869 6458efa5 2020-11-24 stsp }
7870 6458efa5 2020-11-24 stsp }
7871 6458efa5 2020-11-24 stsp
7872 6458efa5 2020-11-24 stsp argc -= optind;
7873 6458efa5 2020-11-24 stsp argv += optind;
7874 6458efa5 2020-11-24 stsp
7875 6458efa5 2020-11-24 stsp if (argc > 1)
7876 e99e2d15 2020-11-24 naddy usage_ref();
7877 0ae84acc 2022-06-15 tracey
7878 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7879 0ae84acc 2022-06-15 tracey if (error != NULL)
7880 0ae84acc 2022-06-15 tracey goto done;
7881 6458efa5 2020-11-24 stsp
7882 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7883 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7884 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7885 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7886 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7887 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7888 c156c7a4 2020-12-18 stsp goto done;
7889 6458efa5 2020-11-24 stsp if (worktree)
7890 6458efa5 2020-11-24 stsp repo_path =
7891 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7892 6458efa5 2020-11-24 stsp else
7893 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7894 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7895 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7896 c156c7a4 2020-12-18 stsp goto done;
7897 c156c7a4 2020-12-18 stsp }
7898 6458efa5 2020-11-24 stsp }
7899 6458efa5 2020-11-24 stsp
7900 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7901 6458efa5 2020-11-24 stsp if (error != NULL)
7902 6458efa5 2020-11-24 stsp goto done;
7903 6458efa5 2020-11-24 stsp
7904 6458efa5 2020-11-24 stsp init_curses();
7905 6458efa5 2020-11-24 stsp
7906 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7907 51a10b52 2020-12-26 stsp if (error)
7908 51a10b52 2020-12-26 stsp goto done;
7909 51a10b52 2020-12-26 stsp
7910 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7911 6458efa5 2020-11-24 stsp if (error)
7912 6458efa5 2020-11-24 stsp goto done;
7913 6458efa5 2020-11-24 stsp
7914 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7915 6458efa5 2020-11-24 stsp if (view == NULL) {
7916 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7917 6458efa5 2020-11-24 stsp goto done;
7918 6458efa5 2020-11-24 stsp }
7919 6458efa5 2020-11-24 stsp
7920 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7921 6458efa5 2020-11-24 stsp if (error)
7922 6458efa5 2020-11-24 stsp goto done;
7923 6458efa5 2020-11-24 stsp
7924 6458efa5 2020-11-24 stsp if (worktree) {
7925 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7926 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7927 6458efa5 2020-11-24 stsp worktree = NULL;
7928 6458efa5 2020-11-24 stsp }
7929 6458efa5 2020-11-24 stsp error = view_loop(view);
7930 6458efa5 2020-11-24 stsp done:
7931 6458efa5 2020-11-24 stsp free(repo_path);
7932 6458efa5 2020-11-24 stsp free(cwd);
7933 1d0f4054 2021-06-17 stsp if (repo) {
7934 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7935 1d0f4054 2021-06-17 stsp if (close_err)
7936 1d0f4054 2021-06-17 stsp error = close_err;
7937 1d0f4054 2021-06-17 stsp }
7938 0ae84acc 2022-06-15 tracey if (pack_fds) {
7939 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7940 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7941 0ae84acc 2022-06-15 tracey if (error == NULL)
7942 0ae84acc 2022-06-15 tracey error = pack_err;
7943 0ae84acc 2022-06-15 tracey }
7944 51a10b52 2020-12-26 stsp tog_free_refs();
7945 6458efa5 2020-11-24 stsp return error;
7946 6458efa5 2020-11-24 stsp }
7947 6458efa5 2020-11-24 stsp
7948 9b058f45 2022-06-30 mark /*
7949 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7950 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7951 9b058f45 2022-06-30 mark */
7952 6458efa5 2020-11-24 stsp static void
7953 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7954 9b058f45 2022-06-30 mark {
7955 9b058f45 2022-06-30 mark switch (view->type) {
7956 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7957 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7958 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7959 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7960 9b058f45 2022-06-30 mark 1);
7961 9b058f45 2022-06-30 mark break;
7962 9b058f45 2022-06-30 mark }
7963 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7964 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7965 9b058f45 2022-06-30 mark else
7966 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7967 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7968 9b058f45 2022-06-30 mark break;
7969 9b058f45 2022-06-30 mark }
7970 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7971 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7972 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7973 9b058f45 2022-06-30 mark break;
7974 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7975 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7976 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7977 9b058f45 2022-06-30 mark break;
7978 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7979 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7980 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7981 9b058f45 2022-06-30 mark break;
7982 9b058f45 2022-06-30 mark default:
7983 9b058f45 2022-06-30 mark break;
7984 9b058f45 2022-06-30 mark }
7985 9b058f45 2022-06-30 mark
7986 9b058f45 2022-06-30 mark view->offset = 0;
7987 9b058f45 2022-06-30 mark }
7988 9b058f45 2022-06-30 mark
7989 9b058f45 2022-06-30 mark /*
7990 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7991 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7992 9b058f45 2022-06-30 mark */
7993 9b058f45 2022-06-30 mark static const struct got_error *
7994 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7995 9b058f45 2022-06-30 mark {
7996 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7997 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7998 9b058f45 2022-06-30 mark int *selected = NULL;
7999 9b058f45 2022-06-30 mark int header, offset;
8000 9b058f45 2022-06-30 mark
8001 9b058f45 2022-06-30 mark switch (view->type) {
8002 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8003 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8004 9b058f45 2022-06-30 mark header = 3;
8005 9b058f45 2022-06-30 mark scrolld = NULL;
8006 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8007 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8008 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8009 9b058f45 2022-06-30 mark s->selected_line -= offset;
8010 9b058f45 2022-06-30 mark view->offset = offset;
8011 9b058f45 2022-06-30 mark }
8012 9b058f45 2022-06-30 mark break;
8013 9b058f45 2022-06-30 mark }
8014 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8015 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8016 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8017 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8018 9b058f45 2022-06-30 mark selected = &s->selected;
8019 9b058f45 2022-06-30 mark break;
8020 9b058f45 2022-06-30 mark }
8021 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8022 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8023 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8024 9b058f45 2022-06-30 mark header = 3;
8025 9b058f45 2022-06-30 mark selected = &s->selected;
8026 9b058f45 2022-06-30 mark break;
8027 9b058f45 2022-06-30 mark }
8028 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8029 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8030 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8031 9b058f45 2022-06-30 mark header = 5;
8032 9b058f45 2022-06-30 mark selected = &s->selected;
8033 9b058f45 2022-06-30 mark break;
8034 9b058f45 2022-06-30 mark }
8035 9b058f45 2022-06-30 mark default:
8036 9b058f45 2022-06-30 mark selected = NULL;
8037 9b058f45 2022-06-30 mark scrolld = NULL;
8038 9b058f45 2022-06-30 mark header = 0;
8039 9b058f45 2022-06-30 mark break;
8040 9b058f45 2022-06-30 mark }
8041 9b058f45 2022-06-30 mark
8042 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8043 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8044 9b058f45 2022-06-30 mark view->offset = offset;
8045 9b058f45 2022-06-30 mark if (scrolld && offset) {
8046 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8047 9b058f45 2022-06-30 mark *selected -= offset;
8048 9b058f45 2022-06-30 mark }
8049 9b058f45 2022-06-30 mark }
8050 9b058f45 2022-06-30 mark
8051 9b058f45 2022-06-30 mark return err;
8052 9b058f45 2022-06-30 mark }
8053 9b058f45 2022-06-30 mark
8054 9b058f45 2022-06-30 mark static void
8055 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8056 ce5b7c56 2019-07-09 stsp {
8057 6059809a 2020-12-17 stsp size_t i;
8058 9f7d7167 2018-04-29 stsp
8059 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8060 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8061 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8062 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8063 ce5b7c56 2019-07-09 stsp }
8064 6879ba42 2020-10-01 naddy fputc('\n', fp);
8065 ce5b7c56 2019-07-09 stsp }
8066 ce5b7c56 2019-07-09 stsp
8067 4ed7e80c 2018-05-20 stsp __dead static void
8068 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8069 9f7d7167 2018-04-29 stsp {
8070 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8071 6879ba42 2020-10-01 naddy
8072 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8073 6879ba42 2020-10-01 naddy getprogname());
8074 6879ba42 2020-10-01 naddy if (hflag) {
8075 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8076 6879ba42 2020-10-01 naddy list_commands(fp);
8077 ee85c5e8 2020-02-29 stsp }
8078 6879ba42 2020-10-01 naddy exit(status);
8079 9f7d7167 2018-04-29 stsp }
8080 9f7d7167 2018-04-29 stsp
8081 c2301be8 2018-04-30 stsp static char **
8082 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8083 c2301be8 2018-04-30 stsp {
8084 ee85c5e8 2020-02-29 stsp va_list ap;
8085 c2301be8 2018-04-30 stsp char **argv;
8086 ee85c5e8 2020-02-29 stsp int i;
8087 c2301be8 2018-04-30 stsp
8088 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8089 ee85c5e8 2020-02-29 stsp
8090 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8091 c2301be8 2018-04-30 stsp if (argv == NULL)
8092 c2301be8 2018-04-30 stsp err(1, "calloc");
8093 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8094 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8095 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8096 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8097 c2301be8 2018-04-30 stsp }
8098 c2301be8 2018-04-30 stsp
8099 ee85c5e8 2020-02-29 stsp va_end(ap);
8100 c2301be8 2018-04-30 stsp return argv;
8101 ee85c5e8 2020-02-29 stsp }
8102 ee85c5e8 2020-02-29 stsp
8103 ee85c5e8 2020-02-29 stsp /*
8104 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8105 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8106 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8107 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8108 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8109 ee85c5e8 2020-02-29 stsp */
8110 ee85c5e8 2020-02-29 stsp static const struct got_error *
8111 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8112 ee85c5e8 2020-02-29 stsp {
8113 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8114 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8115 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8116 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8117 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8118 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8119 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8120 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8121 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8122 84de9106 2020-12-26 stsp
8123 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8124 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8125 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8126 ee85c5e8 2020-02-29 stsp
8127 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8128 0ae84acc 2022-06-15 tracey if (error != NULL)
8129 0ae84acc 2022-06-15 tracey goto done;
8130 0ae84acc 2022-06-15 tracey
8131 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8132 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8133 ee85c5e8 2020-02-29 stsp goto done;
8134 ee85c5e8 2020-02-29 stsp
8135 ee85c5e8 2020-02-29 stsp if (worktree)
8136 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8137 ee85c5e8 2020-02-29 stsp else
8138 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8139 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8140 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8141 ee85c5e8 2020-02-29 stsp goto done;
8142 ee85c5e8 2020-02-29 stsp }
8143 ee85c5e8 2020-02-29 stsp
8144 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8145 ee85c5e8 2020-02-29 stsp if (error != NULL)
8146 ee85c5e8 2020-02-29 stsp goto done;
8147 ee85c5e8 2020-02-29 stsp
8148 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8149 ee85c5e8 2020-02-29 stsp repo, worktree);
8150 ee85c5e8 2020-02-29 stsp if (error)
8151 ee85c5e8 2020-02-29 stsp goto done;
8152 ee85c5e8 2020-02-29 stsp
8153 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8154 84de9106 2020-12-26 stsp if (error)
8155 84de9106 2020-12-26 stsp goto done;
8156 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8157 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8158 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8159 ee85c5e8 2020-02-29 stsp if (error)
8160 ee85c5e8 2020-02-29 stsp goto done;
8161 ee85c5e8 2020-02-29 stsp
8162 ee85c5e8 2020-02-29 stsp if (worktree) {
8163 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8164 ee85c5e8 2020-02-29 stsp worktree = NULL;
8165 ee85c5e8 2020-02-29 stsp }
8166 ee85c5e8 2020-02-29 stsp
8167 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8168 a44927cc 2022-04-07 stsp if (error)
8169 a44927cc 2022-04-07 stsp goto done;
8170 a44927cc 2022-04-07 stsp
8171 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8172 ee85c5e8 2020-02-29 stsp if (error) {
8173 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8174 ee85c5e8 2020-02-29 stsp goto done;
8175 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8176 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8177 6879ba42 2020-10-01 naddy usage(1, 1);
8178 ee85c5e8 2020-02-29 stsp /* not reached */
8179 ee85c5e8 2020-02-29 stsp }
8180 ee85c5e8 2020-02-29 stsp
8181 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8182 1d0f4054 2021-06-17 stsp if (error == NULL)
8183 1d0f4054 2021-06-17 stsp error = close_err;
8184 ee85c5e8 2020-02-29 stsp repo = NULL;
8185 ee85c5e8 2020-02-29 stsp
8186 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8187 ee85c5e8 2020-02-29 stsp if (error)
8188 ee85c5e8 2020-02-29 stsp goto done;
8189 ee85c5e8 2020-02-29 stsp
8190 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8191 ee85c5e8 2020-02-29 stsp argc = 4;
8192 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8193 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8194 ee85c5e8 2020-02-29 stsp done:
8195 1d0f4054 2021-06-17 stsp if (repo) {
8196 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8197 1d0f4054 2021-06-17 stsp if (error == NULL)
8198 1d0f4054 2021-06-17 stsp error = close_err;
8199 1d0f4054 2021-06-17 stsp }
8200 a44927cc 2022-04-07 stsp if (commit)
8201 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8202 ee85c5e8 2020-02-29 stsp if (worktree)
8203 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8204 0ae84acc 2022-06-15 tracey if (pack_fds) {
8205 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8206 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8207 0ae84acc 2022-06-15 tracey if (error == NULL)
8208 0ae84acc 2022-06-15 tracey error = pack_err;
8209 0ae84acc 2022-06-15 tracey }
8210 ee85c5e8 2020-02-29 stsp free(id);
8211 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8212 ee85c5e8 2020-02-29 stsp free(commit_id);
8213 ee85c5e8 2020-02-29 stsp free(cwd);
8214 ee85c5e8 2020-02-29 stsp free(repo_path);
8215 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8216 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8217 ee85c5e8 2020-02-29 stsp int i;
8218 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8219 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8220 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8221 ee85c5e8 2020-02-29 stsp }
8222 87670572 2020-12-26 naddy tog_free_refs();
8223 ee85c5e8 2020-02-29 stsp return error;
8224 c2301be8 2018-04-30 stsp }
8225 c2301be8 2018-04-30 stsp
8226 9f7d7167 2018-04-29 stsp int
8227 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8228 9f7d7167 2018-04-29 stsp {
8229 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8230 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8231 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8232 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8233 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8234 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8235 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8236 83cd27f8 2020-01-13 stsp };
8237 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8238 9f7d7167 2018-04-29 stsp
8239 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8240 9f7d7167 2018-04-29 stsp
8241 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8242 9f7d7167 2018-04-29 stsp switch (ch) {
8243 9f7d7167 2018-04-29 stsp case 'h':
8244 9f7d7167 2018-04-29 stsp hflag = 1;
8245 9f7d7167 2018-04-29 stsp break;
8246 53ccebc2 2019-07-30 stsp case 'V':
8247 53ccebc2 2019-07-30 stsp Vflag = 1;
8248 53ccebc2 2019-07-30 stsp break;
8249 9f7d7167 2018-04-29 stsp default:
8250 6879ba42 2020-10-01 naddy usage(hflag, 1);
8251 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8252 9f7d7167 2018-04-29 stsp }
8253 9f7d7167 2018-04-29 stsp }
8254 9f7d7167 2018-04-29 stsp
8255 9f7d7167 2018-04-29 stsp argc -= optind;
8256 9f7d7167 2018-04-29 stsp argv += optind;
8257 9814e6a3 2020-09-27 naddy optind = 1;
8258 c2301be8 2018-04-30 stsp optreset = 1;
8259 9f7d7167 2018-04-29 stsp
8260 53ccebc2 2019-07-30 stsp if (Vflag) {
8261 53ccebc2 2019-07-30 stsp got_version_print_str();
8262 6879ba42 2020-10-01 naddy return 0;
8263 53ccebc2 2019-07-30 stsp }
8264 4010e238 2020-12-04 stsp
8265 4010e238 2020-12-04 stsp #ifndef PROFILE
8266 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8267 4010e238 2020-12-04 stsp NULL) == -1)
8268 4010e238 2020-12-04 stsp err(1, "pledge");
8269 4010e238 2020-12-04 stsp #endif
8270 53ccebc2 2019-07-30 stsp
8271 c2301be8 2018-04-30 stsp if (argc == 0) {
8272 f29d3e89 2018-06-23 stsp if (hflag)
8273 6879ba42 2020-10-01 naddy usage(hflag, 0);
8274 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8275 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8276 c2301be8 2018-04-30 stsp argc = 1;
8277 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8278 c2301be8 2018-04-30 stsp } else {
8279 6059809a 2020-12-17 stsp size_t i;
8280 9f7d7167 2018-04-29 stsp
8281 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8282 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8283 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8284 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8285 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8286 9f7d7167 2018-04-29 stsp break;
8287 9f7d7167 2018-04-29 stsp }
8288 9f7d7167 2018-04-29 stsp }
8289 ee85c5e8 2020-02-29 stsp }
8290 3642c4c6 2019-07-09 stsp
8291 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8292 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8293 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8294 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8295 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8296 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8297 917d79a7 2022-07-01 stsp }
8298 917d79a7 2022-07-01 stsp
8299 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8300 ee85c5e8 2020-02-29 stsp if (argc != 1)
8301 6879ba42 2020-10-01 naddy usage(0, 1);
8302 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8303 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8304 ee85c5e8 2020-02-29 stsp } else {
8305 ee85c5e8 2020-02-29 stsp if (hflag)
8306 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8307 ee85c5e8 2020-02-29 stsp else
8308 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8309 9f7d7167 2018-04-29 stsp }
8310 9f7d7167 2018-04-29 stsp
8311 9f7d7167 2018-04-29 stsp endwin();
8312 b46c1e04 2020-09-20 naddy putchar('\n');
8313 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8314 a2f4a359 2020-02-28 stsp int i;
8315 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8316 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8317 a2f4a359 2020-02-28 stsp free(cmd_argv);
8318 a2f4a359 2020-02-28 stsp }
8319 a2f4a359 2020-02-28 stsp
8320 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8321 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8322 9f7d7167 2018-04-29 stsp return 0;
8323 9f7d7167 2018-04-29 stsp }