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 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
317 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
318 3dbaef42 2020-11-24 stsp const char *label1, *label2;
319 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
320 f9d37699 2022-06-28 stsp int fd1, fd2;
321 c7d5c43c 2022-08-04 mark int lineno;
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 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey int matched_line;
332 f44b1f58 2020-02-02 tracey int selected_line;
333 15a087fe 2019-02-21 stsp
334 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
335 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
336 b01e7d3b 2018-08-04 stsp };
337 b01e7d3b 2018-08-04 stsp
338 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
340 1a76625f 2018-10-22 stsp
341 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
342 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
343 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
344 1a76625f 2018-10-22 stsp int commits_needed;
345 fb280deb 2021-08-30 stsp int load_all;
346 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
347 568eae95 2022-09-11 mark struct commit_queue *real_commits;
348 1a76625f 2018-10-22 stsp const char *in_repo_path;
349 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
350 1a76625f 2018-10-22 stsp struct got_repository *repo;
351 74467cc8 2022-06-15 stsp int *pack_fds;
352 1a76625f 2018-10-22 stsp int log_complete;
353 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
356 13add988 2019-10-15 stsp int *searching;
357 13add988 2019-10-15 stsp int *search_next_done;
358 13add988 2019-10-15 stsp regex_t *regex;
359 568eae95 2022-09-11 mark int *limiting;
360 568eae95 2022-09-11 mark int limit_match;
361 568eae95 2022-09-11 mark regex_t *limit_regex;
362 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
363 1a76625f 2018-10-22 stsp };
364 1a76625f 2018-10-22 stsp
365 1a76625f 2018-10-22 stsp struct tog_log_view_state {
366 568eae95 2022-09-11 mark struct commit_queue *commits;
367 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
370 568eae95 2022-09-11 mark struct commit_queue real_commits;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 10aab77f 2022-07-19 op int use_committer;
384 568eae95 2022-09-11 mark int limit_view;
385 568eae95 2022-09-11 mark regex_t limit_regex;
386 568eae95 2022-09-11 mark struct commit_queue limit_commits;
387 ba4f502b 2018-08-04 stsp };
388 11b20872 2019-11-08 stsp
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
391 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
392 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
395 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
396 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
397 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
398 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
399 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
401 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
402 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
403 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
404 ba4f502b 2018-08-04 stsp
405 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
406 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
407 e9424729 2018-08-04 stsp int nlines;
408 e9424729 2018-08-04 stsp
409 e9424729 2018-08-04 stsp struct tog_view *view;
410 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
411 e9424729 2018-08-04 stsp int *quit;
412 e9424729 2018-08-04 stsp };
413 e9424729 2018-08-04 stsp
414 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
415 e9424729 2018-08-04 stsp const char *path;
416 e9424729 2018-08-04 stsp struct got_repository *repo;
417 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
418 e9424729 2018-08-04 stsp int *complete;
419 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
420 fc06ba56 2019-08-22 stsp void *cancel_arg;
421 e9424729 2018-08-04 stsp };
422 e9424729 2018-08-04 stsp
423 e9424729 2018-08-04 stsp struct tog_blame {
424 e9424729 2018-08-04 stsp FILE *f;
425 be659d10 2020-11-18 stsp off_t filesize;
426 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
427 6fcac457 2018-11-19 stsp int nlines;
428 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
429 e9424729 2018-08-04 stsp pthread_t thread;
430 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
431 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
432 e9424729 2018-08-04 stsp const char *path;
433 0ae84acc 2022-06-15 tracey int *pack_fds;
434 e9424729 2018-08-04 stsp };
435 e9424729 2018-08-04 stsp
436 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
437 7cbe629d 2018-08-04 stsp int first_displayed_line;
438 7cbe629d 2018-08-04 stsp int last_displayed_line;
439 7cbe629d 2018-08-04 stsp int selected_line;
440 c0f61fa4 2022-07-11 mark int last_diffed_line;
441 7cbe629d 2018-08-04 stsp int blame_complete;
442 e5a0f69f 2018-08-18 stsp int eof;
443 e5a0f69f 2018-08-18 stsp int done;
444 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
445 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
446 e5a0f69f 2018-08-18 stsp char *path;
447 7cbe629d 2018-08-04 stsp struct got_repository *repo;
448 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
449 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
450 e9424729 2018-08-04 stsp struct tog_blame blame;
451 6c4c42e0 2019-06-24 stsp int matched_line;
452 11b20872 2019-11-08 stsp struct tog_colors colors;
453 ad80ab7b 2018-08-04 stsp };
454 ad80ab7b 2018-08-04 stsp
455 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
456 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
460 ad80ab7b 2018-08-04 stsp int selected;
461 ad80ab7b 2018-08-04 stsp };
462 ad80ab7b 2018-08-04 stsp
463 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
464 ad80ab7b 2018-08-04 stsp
465 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
466 ad80ab7b 2018-08-04 stsp char *tree_label;
467 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
468 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
469 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
470 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
471 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
473 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
474 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
475 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
476 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
477 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
478 6458efa5 2020-11-24 stsp struct tog_colors colors;
479 6458efa5 2020-11-24 stsp };
480 6458efa5 2020-11-24 stsp
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
482 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
483 6458efa5 2020-11-24 stsp struct got_reference *ref;
484 6458efa5 2020-11-24 stsp int idx;
485 6458efa5 2020-11-24 stsp };
486 6458efa5 2020-11-24 stsp
487 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
488 6458efa5 2020-11-24 stsp
489 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
490 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
491 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
492 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
494 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
495 6458efa5 2020-11-24 stsp struct got_repository *repo;
496 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
497 bddb1296 2019-11-08 stsp struct tog_colors colors;
498 7cbe629d 2018-08-04 stsp };
499 7cbe629d 2018-08-04 stsp
500 669b5ffa 2018-10-07 stsp /*
501 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
502 669b5ffa 2018-10-07 stsp *
503 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
504 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
505 669b5ffa 2018-10-07 stsp * there is enough screen estate.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
509 669b5ffa 2018-10-07 stsp *
510 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
511 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
512 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
513 669b5ffa 2018-10-07 stsp *
514 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
515 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
516 669b5ffa 2018-10-07 stsp */
517 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
518 669b5ffa 2018-10-07 stsp
519 cc3c9aac 2018-08-01 stsp struct tog_view {
520 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
521 26ed57b2 2018-05-19 stsp WINDOW *window;
522 26ed57b2 2018-05-19 stsp PANEL *panel;
523 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
524 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
525 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
526 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
527 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
528 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
529 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
530 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
531 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
532 9970f7fc 2020-12-03 stsp int dying;
533 669b5ffa 2018-10-07 stsp struct tog_view *parent;
534 669b5ffa 2018-10-07 stsp struct tog_view *child;
535 5dc9f4bc 2018-08-04 stsp
536 e78dc838 2020-12-04 stsp /*
537 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
538 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
539 e78dc838 2020-12-04 stsp * between parent and child.
540 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
541 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
542 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
543 e78dc838 2020-12-04 stsp * situations.
544 e78dc838 2020-12-04 stsp */
545 e78dc838 2020-12-04 stsp int focus_child;
546 e78dc838 2020-12-04 stsp
547 9b058f45 2022-06-30 mark enum tog_view_mode mode;
548 5dc9f4bc 2018-08-04 stsp /* type-specific state */
549 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
550 5dc9f4bc 2018-08-04 stsp union {
551 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
552 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
553 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
554 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
555 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
556 5dc9f4bc 2018-08-04 stsp } state;
557 e5a0f69f 2018-08-18 stsp
558 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
562 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
563 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
564 60493ae3 2019-06-20 stsp
565 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
566 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
567 c0c4acc8 2021-01-24 stsp int search_started;
568 60493ae3 2019-06-20 stsp int searching;
569 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
570 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
571 60493ae3 2019-06-20 stsp int search_next_done;
572 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
573 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
574 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
575 1803e47f 2019-06-22 stsp regex_t regex;
576 41605754 2020-11-12 stsp regmatch_t regmatch;
577 cc3c9aac 2018-08-01 stsp };
578 cd0acaa7 2018-05-20 stsp
579 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
580 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
581 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
582 78756c87 2020-11-24 stsp struct got_repository *);
583 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
585 e78dc838 2020-12-04 stsp struct tog_view *, int);
586 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
588 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
589 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
590 e5a0f69f 2018-08-18 stsp
591 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
592 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
593 78756c87 2020-11-24 stsp const char *, const char *, int);
594 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
596 e78dc838 2020-12-04 stsp struct tog_view *, int);
597 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
598 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
599 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
600 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
601 e5a0f69f 2018-08-18 stsp
602 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
603 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
604 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
606 e78dc838 2020-12-04 stsp struct tog_view *, int);
607 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
609 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
610 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
611 e5a0f69f 2018-08-18 stsp
612 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
613 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
614 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
615 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
616 e78dc838 2020-12-04 stsp struct tog_view *, int);
617 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
618 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
619 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp
621 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
622 6458efa5 2020-11-24 stsp struct got_repository *);
623 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
625 e78dc838 2020-12-04 stsp struct tog_view *, int);
626 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
627 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
628 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
629 25791caa 2018-10-24 stsp
630 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
631 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
632 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
633 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
634 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
635 25791caa 2018-10-24 stsp
636 25791caa 2018-10-24 stsp static void
637 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
638 25791caa 2018-10-24 stsp {
639 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
640 83baff54 2019-08-12 stsp }
641 83baff54 2019-08-12 stsp
642 83baff54 2019-08-12 stsp static void
643 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
644 83baff54 2019-08-12 stsp {
645 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
646 25791caa 2018-10-24 stsp }
647 26ed57b2 2018-05-19 stsp
648 61266923 2020-01-14 stsp static void
649 61266923 2020-01-14 stsp tog_sigcont(int signo)
650 61266923 2020-01-14 stsp {
651 61266923 2020-01-14 stsp tog_sigcont_received = 1;
652 61266923 2020-01-14 stsp }
653 61266923 2020-01-14 stsp
654 2497f032 2022-05-31 stsp static void
655 2497f032 2022-05-31 stsp tog_sigint(int signo)
656 2497f032 2022-05-31 stsp {
657 2497f032 2022-05-31 stsp tog_sigint_received = 1;
658 2497f032 2022-05-31 stsp }
659 2497f032 2022-05-31 stsp
660 2497f032 2022-05-31 stsp static void
661 2497f032 2022-05-31 stsp tog_sigterm(int signo)
662 2497f032 2022-05-31 stsp {
663 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
664 2497f032 2022-05-31 stsp }
665 2497f032 2022-05-31 stsp
666 2497f032 2022-05-31 stsp static int
667 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
668 2497f032 2022-05-31 stsp {
669 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
670 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
671 2497f032 2022-05-31 stsp }
672 2497f032 2022-05-31 stsp
673 e5a0f69f 2018-08-18 stsp static const struct got_error *
674 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
675 ea5e7bb5 2018-08-01 stsp {
676 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
677 e5a0f69f 2018-08-18 stsp
678 669b5ffa 2018-10-07 stsp if (view->child) {
679 5629093a 2022-07-11 stsp child_err = view_close(view->child);
680 669b5ffa 2018-10-07 stsp view->child = NULL;
681 669b5ffa 2018-10-07 stsp }
682 e5a0f69f 2018-08-18 stsp if (view->close)
683 e5a0f69f 2018-08-18 stsp err = view->close(view);
684 ea5e7bb5 2018-08-01 stsp if (view->panel)
685 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
686 ea5e7bb5 2018-08-01 stsp if (view->window)
687 ea5e7bb5 2018-08-01 stsp delwin(view->window);
688 ea5e7bb5 2018-08-01 stsp free(view);
689 5629093a 2022-07-11 stsp return err ? err : child_err;
690 ea5e7bb5 2018-08-01 stsp }
691 ea5e7bb5 2018-08-01 stsp
692 ea5e7bb5 2018-08-01 stsp static struct tog_view *
693 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
694 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
695 ea5e7bb5 2018-08-01 stsp {
696 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
697 ea5e7bb5 2018-08-01 stsp
698 ea5e7bb5 2018-08-01 stsp if (view == NULL)
699 ea5e7bb5 2018-08-01 stsp return NULL;
700 ea5e7bb5 2018-08-01 stsp
701 d6b05b5b 2018-08-04 stsp view->type = type;
702 f7d12f7e 2018-08-01 stsp view->lines = LINES;
703 f7d12f7e 2018-08-01 stsp view->cols = COLS;
704 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
705 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
706 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
707 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
708 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
709 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
710 96a765a8 2018-08-04 stsp view_close(view);
711 ea5e7bb5 2018-08-01 stsp return NULL;
712 ea5e7bb5 2018-08-01 stsp }
713 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
714 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
715 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
716 96a765a8 2018-08-04 stsp view_close(view);
717 ea5e7bb5 2018-08-01 stsp return NULL;
718 ea5e7bb5 2018-08-01 stsp }
719 ea5e7bb5 2018-08-01 stsp
720 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
721 ea5e7bb5 2018-08-01 stsp return view;
722 cdf1ee82 2018-08-01 stsp }
723 cdf1ee82 2018-08-01 stsp
724 0cf4efb1 2018-09-29 stsp static int
725 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
726 0cf4efb1 2018-09-29 stsp {
727 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
728 0cf4efb1 2018-09-29 stsp return 0;
729 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
730 5c60c32a 2018-10-18 stsp }
731 5c60c32a 2018-10-18 stsp
732 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
733 9b058f45 2022-06-30 mark static int
734 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
735 9b058f45 2022-06-30 mark {
736 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
737 9b058f45 2022-06-30 mark }
738 9b058f45 2022-06-30 mark
739 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
740 5c60c32a 2018-10-18 stsp
741 5c60c32a 2018-10-18 stsp static const struct got_error *
742 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
743 5c60c32a 2018-10-18 stsp {
744 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
745 5c60c32a 2018-10-18 stsp
746 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
747 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
748 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
749 3c1dfe12 2022-07-08 mark else
750 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
751 9b058f45 2022-06-30 mark view->begin_x = 0;
752 571ccd73 2022-07-19 mark } else if (!view->resized) {
753 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
754 3c1dfe12 2022-07-08 mark view->cols > 119)
755 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
756 3c1dfe12 2022-07-08 mark else
757 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
758 9b058f45 2022-06-30 mark view->begin_y = 0;
759 9b058f45 2022-06-30 mark }
760 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
761 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
762 5c60c32a 2018-10-18 stsp view->lines = LINES;
763 5c60c32a 2018-10-18 stsp view->cols = COLS;
764 5c60c32a 2018-10-18 stsp err = view_resize(view);
765 5c60c32a 2018-10-18 stsp if (err)
766 5c60c32a 2018-10-18 stsp return err;
767 5c60c32a 2018-10-18 stsp
768 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
769 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
770 9b058f45 2022-06-30 mark
771 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
772 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp return NULL;
775 5c60c32a 2018-10-18 stsp }
776 5c60c32a 2018-10-18 stsp
777 5c60c32a 2018-10-18 stsp static const struct got_error *
778 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
779 5c60c32a 2018-10-18 stsp {
780 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
781 5c60c32a 2018-10-18 stsp
782 5c60c32a 2018-10-18 stsp view->begin_x = 0;
783 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
784 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
785 5c60c32a 2018-10-18 stsp view->ncols = COLS;
786 5c60c32a 2018-10-18 stsp view->lines = LINES;
787 5c60c32a 2018-10-18 stsp view->cols = COLS;
788 5c60c32a 2018-10-18 stsp err = view_resize(view);
789 5c60c32a 2018-10-18 stsp if (err)
790 5c60c32a 2018-10-18 stsp return err;
791 5c60c32a 2018-10-18 stsp
792 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
793 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
794 5c60c32a 2018-10-18 stsp
795 5c60c32a 2018-10-18 stsp return NULL;
796 0cf4efb1 2018-09-29 stsp }
797 0cf4efb1 2018-09-29 stsp
798 5c60c32a 2018-10-18 stsp static int
799 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
800 5c60c32a 2018-10-18 stsp {
801 5c60c32a 2018-10-18 stsp return view->parent == NULL;
802 5c60c32a 2018-10-18 stsp }
803 5c60c32a 2018-10-18 stsp
804 6131ff18 2022-06-20 mark static int
805 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
806 6131ff18 2022-06-20 mark {
807 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
808 24b9cfdc 2022-06-30 stsp }
809 24b9cfdc 2022-06-30 stsp
810 24b9cfdc 2022-06-30 stsp static int
811 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
812 24b9cfdc 2022-06-30 stsp {
813 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
814 6131ff18 2022-06-20 mark }
815 6131ff18 2022-06-20 mark
816 49b24ee5 2022-07-03 mark static int
817 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
818 49b24ee5 2022-07-03 mark {
819 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
820 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
821 49b24ee5 2022-07-03 mark }
822 49b24ee5 2022-07-03 mark
823 9b058f45 2022-06-30 mark static void
824 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
825 9b058f45 2022-06-30 mark {
826 9b058f45 2022-06-30 mark PANEL *panel;
827 9b058f45 2022-06-30 mark const struct tog_view *view_above;
828 6131ff18 2022-06-20 mark
829 9b058f45 2022-06-30 mark if (view->parent)
830 9b058f45 2022-06-30 mark return view_border(view->parent);
831 9b058f45 2022-06-30 mark
832 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
833 9b058f45 2022-06-30 mark if (panel == NULL)
834 9b058f45 2022-06-30 mark return;
835 9b058f45 2022-06-30 mark
836 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
837 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
838 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
839 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
840 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
841 9b058f45 2022-06-30 mark else
842 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
843 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
844 9b058f45 2022-06-30 mark }
845 9b058f45 2022-06-30 mark
846 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
847 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
848 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
849 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
850 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
851 9b058f45 2022-06-30 mark
852 4d8c2215 2018-08-19 stsp static const struct got_error *
853 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
854 f7d12f7e 2018-08-01 stsp {
855 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
856 9b058f45 2022-06-30 mark int dif, nlines, ncols;
857 f7d12f7e 2018-08-01 stsp
858 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
859 9b058f45 2022-06-30 mark
860 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
861 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
862 0cf4efb1 2018-09-29 stsp else
863 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
864 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
865 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
866 0cf4efb1 2018-09-29 stsp else
867 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
868 6d0fee91 2018-08-01 stsp
869 4dd27a72 2022-06-29 stsp if (view->child) {
870 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
871 9b058f45 2022-06-30 mark
872 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
873 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
874 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
875 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
876 0dbbbe90 2022-06-17 op ncols = COLS;
877 0dbbbe90 2022-06-17 op
878 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
879 5c60c32a 2018-10-18 stsp if (view->child->focussed)
880 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
881 5c60c32a 2018-10-18 stsp else
882 5c60c32a 2018-10-18 stsp show_panel(view->panel);
883 5c60c32a 2018-10-18 stsp } else {
884 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
885 0dbbbe90 2022-06-17 op
886 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
887 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
888 9b058f45 2022-06-30 mark }
889 9b058f45 2022-06-30 mark /*
890 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
891 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
892 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
893 9b058f45 2022-06-30 mark */
894 9b058f45 2022-06-30 mark if (hs) {
895 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
896 9b058f45 2022-06-30 mark if (err)
897 9b058f45 2022-06-30 mark return err;
898 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
899 9b058f45 2022-06-30 mark err = offset_selection_down(view);
900 9b058f45 2022-06-30 mark if (err)
901 9b058f45 2022-06-30 mark return err;
902 9b058f45 2022-06-30 mark }
903 9b058f45 2022-06-30 mark view_border(view);
904 9b058f45 2022-06-30 mark update_panels();
905 9b058f45 2022-06-30 mark doupdate();
906 9b058f45 2022-06-30 mark show_panel(view->child->panel);
907 9b058f45 2022-06-30 mark nlines = view->nlines;
908 9b058f45 2022-06-30 mark }
909 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
910 0dbbbe90 2022-06-17 op ncols = COLS;
911 669b5ffa 2018-10-07 stsp
912 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
913 571ccd73 2022-07-19 mark err = view->resize(view, dif);
914 571ccd73 2022-07-19 mark if (err)
915 571ccd73 2022-07-19 mark return err;
916 571ccd73 2022-07-19 mark }
917 571ccd73 2022-07-19 mark
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 571ccd73 2022-07-19 mark }
931 571ccd73 2022-07-19 mark
932 571ccd73 2022-07-19 mark static const struct got_error *
933 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
934 571ccd73 2022-07-19 mark {
935 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
936 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
937 6fe51fee 2022-07-22 mark int n = 0;
938 571ccd73 2022-07-19 mark
939 6fe51fee 2022-07-22 mark if (s->selected_entry)
940 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
941 6fe51fee 2022-07-22 mark
942 571ccd73 2022-07-19 mark /*
943 571ccd73 2022-07-19 mark * Request commits to account for the increased
944 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
945 571ccd73 2022-07-19 mark */
946 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
947 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
948 571ccd73 2022-07-19 mark err = request_log_commits(view);
949 571ccd73 2022-07-19 mark }
950 571ccd73 2022-07-19 mark
951 571ccd73 2022-07-19 mark return err;
952 d9a7ab53 2022-07-11 mark }
953 d9a7ab53 2022-07-11 mark
954 d9a7ab53 2022-07-11 mark static void
955 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
956 d9a7ab53 2022-07-11 mark {
957 d9a7ab53 2022-07-11 mark if (n == 0)
958 d9a7ab53 2022-07-11 mark return;
959 d9a7ab53 2022-07-11 mark
960 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
961 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
962 d9a7ab53 2022-07-11 mark view->parent->offset += n;
963 d9a7ab53 2022-07-11 mark else
964 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
965 d9a7ab53 2022-07-11 mark } else if (view->offset) {
966 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
967 d9a7ab53 2022-07-11 mark view->offset -= n;
968 d9a7ab53 2022-07-11 mark else
969 d9a7ab53 2022-07-11 mark view->offset = 0;
970 d9a7ab53 2022-07-11 mark }
971 3c1dfe12 2022-07-08 mark }
972 3c1dfe12 2022-07-08 mark
973 3c1dfe12 2022-07-08 mark static const struct got_error *
974 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
975 3c1dfe12 2022-07-08 mark {
976 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
977 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
978 3c1dfe12 2022-07-08 mark
979 3c1dfe12 2022-07-08 mark if (view->parent)
980 3c1dfe12 2022-07-08 mark v = view->parent;
981 3c1dfe12 2022-07-08 mark else
982 3c1dfe12 2022-07-08 mark v = view;
983 3c1dfe12 2022-07-08 mark
984 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
985 3c1dfe12 2022-07-08 mark return NULL;
986 3c1dfe12 2022-07-08 mark
987 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
988 3c1dfe12 2022-07-08 mark
989 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
990 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
991 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
992 3c1dfe12 2022-07-08 mark if (view->parent)
993 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
994 3c1dfe12 2022-07-08 mark else
995 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
996 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
997 3c1dfe12 2022-07-08 mark view->count = 0;
998 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
999 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1000 3c1dfe12 2022-07-08 mark view->count = 0;
1001 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1002 3c1dfe12 2022-07-08 mark }
1003 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1004 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1005 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1006 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1007 3c1dfe12 2022-07-08 mark if (err)
1008 3c1dfe12 2022-07-08 mark return err;
1009 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1010 3c1dfe12 2022-07-08 mark } else {
1011 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1012 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1013 3c1dfe12 2022-07-08 mark if (view->parent)
1014 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1015 3c1dfe12 2022-07-08 mark else
1016 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1017 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1018 3c1dfe12 2022-07-08 mark view->count = 0;
1019 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1020 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1021 3c1dfe12 2022-07-08 mark view->count = 0;
1022 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1023 3c1dfe12 2022-07-08 mark }
1024 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1025 3c1dfe12 2022-07-08 mark }
1026 3c1dfe12 2022-07-08 mark
1027 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1028 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1029 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1030 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1031 3c1dfe12 2022-07-08 mark
1032 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1033 3c1dfe12 2022-07-08 mark if (err)
1034 3c1dfe12 2022-07-08 mark return err;
1035 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1036 3c1dfe12 2022-07-08 mark if (err)
1037 3c1dfe12 2022-07-08 mark return err;
1038 3c1dfe12 2022-07-08 mark
1039 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1040 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1041 3c1dfe12 2022-07-08 mark if (err)
1042 3c1dfe12 2022-07-08 mark return err;
1043 3c1dfe12 2022-07-08 mark }
1044 3c1dfe12 2022-07-08 mark
1045 6fe51fee 2022-07-22 mark if (v->resize)
1046 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1047 6fe51fee 2022-07-22 mark else if (v->child->resize)
1048 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1049 3c1dfe12 2022-07-08 mark
1050 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1051 3c1dfe12 2022-07-08 mark
1052 3c1dfe12 2022-07-08 mark return err;
1053 3c1dfe12 2022-07-08 mark }
1054 3c1dfe12 2022-07-08 mark
1055 3c1dfe12 2022-07-08 mark static void
1056 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 3c1dfe12 2022-07-08 mark {
1058 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1059 3c1dfe12 2022-07-08 mark
1060 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1061 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1062 669b5ffa 2018-10-07 stsp }
1063 669b5ffa 2018-10-07 stsp
1064 669b5ffa 2018-10-07 stsp static const struct got_error *
1065 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1066 669b5ffa 2018-10-07 stsp {
1067 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1070 669b5ffa 2018-10-07 stsp return NULL;
1071 669b5ffa 2018-10-07 stsp
1072 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1073 669b5ffa 2018-10-07 stsp view->child = NULL;
1074 669b5ffa 2018-10-07 stsp return err;
1075 669b5ffa 2018-10-07 stsp }
1076 669b5ffa 2018-10-07 stsp
1077 0dbbbe90 2022-06-17 op static const struct got_error *
1078 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1079 669b5ffa 2018-10-07 stsp {
1080 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1081 3c1dfe12 2022-07-08 mark
1082 669b5ffa 2018-10-07 stsp view->child = child;
1083 669b5ffa 2018-10-07 stsp child->parent = view;
1084 0dbbbe90 2022-06-17 op
1085 3c1dfe12 2022-07-08 mark err = view_resize(view);
1086 3c1dfe12 2022-07-08 mark if (err)
1087 3c1dfe12 2022-07-08 mark return err;
1088 3c1dfe12 2022-07-08 mark
1089 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1090 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1091 3c1dfe12 2022-07-08 mark
1092 3c1dfe12 2022-07-08 mark return err;
1093 bfddd0d9 2018-09-29 stsp }
1094 136e2bd4 2022-07-23 mark
1095 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1096 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1097 136e2bd4 2022-07-23 mark
1098 136e2bd4 2022-07-23 mark static const struct got_error *
1099 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1100 136e2bd4 2022-07-23 mark enum tog_view_type request)
1101 136e2bd4 2022-07-23 mark {
1102 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1103 136e2bd4 2022-07-23 mark const struct got_error *err;
1104 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1105 136e2bd4 2022-07-23 mark
1106 136e2bd4 2022-07-23 mark *requested = NULL;
1107 136e2bd4 2022-07-23 mark
1108 136e2bd4 2022-07-23 mark if (view_is_parent_view(view))
1109 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1110 bfddd0d9 2018-09-29 stsp
1111 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1112 136e2bd4 2022-07-23 mark if (err)
1113 136e2bd4 2022-07-23 mark return err;
1114 136e2bd4 2022-07-23 mark
1115 136e2bd4 2022-07-23 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1116 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1117 136e2bd4 2022-07-23 mark if (err)
1118 136e2bd4 2022-07-23 mark return err;
1119 136e2bd4 2022-07-23 mark }
1120 136e2bd4 2022-07-23 mark
1121 136e2bd4 2022-07-23 mark view->focussed = 0;
1122 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1123 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1124 136e2bd4 2022-07-23 mark new_view->nlines = view->lines - y;
1125 136e2bd4 2022-07-23 mark
1126 136e2bd4 2022-07-23 mark if (view_is_parent_view(view)) {
1127 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1128 136e2bd4 2022-07-23 mark err = view_close_child(view);
1129 136e2bd4 2022-07-23 mark if (err)
1130 136e2bd4 2022-07-23 mark return err;
1131 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1132 136e2bd4 2022-07-23 mark if (err)
1133 136e2bd4 2022-07-23 mark return err;
1134 136e2bd4 2022-07-23 mark view->focus_child = 1;
1135 136e2bd4 2022-07-23 mark } else
1136 136e2bd4 2022-07-23 mark *requested = new_view;
1137 136e2bd4 2022-07-23 mark
1138 136e2bd4 2022-07-23 mark return NULL;
1139 136e2bd4 2022-07-23 mark }
1140 136e2bd4 2022-07-23 mark
1141 34bc9ec9 2019-02-22 stsp static void
1142 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1143 25791caa 2018-10-24 stsp {
1144 25791caa 2018-10-24 stsp int cols, lines;
1145 25791caa 2018-10-24 stsp struct winsize size;
1146 25791caa 2018-10-24 stsp
1147 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1148 25791caa 2018-10-24 stsp cols = 80; /* Default */
1149 25791caa 2018-10-24 stsp lines = 24;
1150 25791caa 2018-10-24 stsp } else {
1151 25791caa 2018-10-24 stsp cols = size.ws_col;
1152 25791caa 2018-10-24 stsp lines = size.ws_row;
1153 25791caa 2018-10-24 stsp }
1154 25791caa 2018-10-24 stsp resize_term(lines, cols);
1155 2b49a8ae 2019-06-22 stsp }
1156 2b49a8ae 2019-06-22 stsp
1157 2b49a8ae 2019-06-22 stsp static const struct got_error *
1158 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1159 2b49a8ae 2019-06-22 stsp {
1160 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1161 9b058f45 2022-06-30 mark struct tog_view *v = view;
1162 2b49a8ae 2019-06-22 stsp char pattern[1024];
1163 2b49a8ae 2019-06-22 stsp int ret;
1164 c0c4acc8 2021-01-24 stsp
1165 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1166 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1167 c0c4acc8 2021-01-24 stsp view->searching = 0;
1168 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1169 c0c4acc8 2021-01-24 stsp }
1170 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1171 2b49a8ae 2019-06-22 stsp
1172 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1173 2b49a8ae 2019-06-22 stsp return NULL;
1174 2b49a8ae 2019-06-22 stsp
1175 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1176 9b058f45 2022-06-30 mark v = view->child;
1177 2b49a8ae 2019-06-22 stsp
1178 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1179 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1180 9b058f45 2022-06-30 mark
1181 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1182 2b49a8ae 2019-06-22 stsp nocbreak();
1183 2b49a8ae 2019-06-22 stsp echo();
1184 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1185 9b058f45 2022-06-30 mark wrefresh(v->window);
1186 2b49a8ae 2019-06-22 stsp cbreak();
1187 2b49a8ae 2019-06-22 stsp noecho();
1188 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1189 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1190 2b49a8ae 2019-06-22 stsp return NULL;
1191 2b49a8ae 2019-06-22 stsp
1192 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1193 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1194 7c32bd05 2019-06-22 stsp if (err) {
1195 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1196 7c32bd05 2019-06-22 stsp return err;
1197 7c32bd05 2019-06-22 stsp }
1198 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1199 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1200 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1201 2b49a8ae 2019-06-22 stsp view->search_next(view);
1202 2b49a8ae 2019-06-22 stsp }
1203 2b49a8ae 2019-06-22 stsp
1204 2b49a8ae 2019-06-22 stsp return NULL;
1205 d2366e29 2022-07-07 mark }
1206 d2366e29 2022-07-07 mark
1207 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1208 d2366e29 2022-07-07 mark static const struct got_error *
1209 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1210 d2366e29 2022-07-07 mark {
1211 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1212 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1213 d2366e29 2022-07-07 mark
1214 d2366e29 2022-07-07 mark if (view->parent)
1215 d2366e29 2022-07-07 mark v = view->parent;
1216 d2366e29 2022-07-07 mark else
1217 d2366e29 2022-07-07 mark v = view;
1218 d2366e29 2022-07-07 mark
1219 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1220 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1221 7532ccda 2022-07-11 mark else
1222 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1223 d2366e29 2022-07-07 mark
1224 7532ccda 2022-07-11 mark if (!v->child)
1225 7532ccda 2022-07-11 mark return NULL;
1226 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1227 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1228 7532ccda 2022-07-11 mark
1229 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1230 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1231 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1232 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1233 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1234 d2366e29 2022-07-07 mark
1235 7532ccda 2022-07-11 mark
1236 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1237 d2366e29 2022-07-07 mark v->ncols = COLS;
1238 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1239 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1240 d2366e29 2022-07-07 mark
1241 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1242 d2366e29 2022-07-07 mark if (err)
1243 d2366e29 2022-07-07 mark return err;
1244 d2366e29 2022-07-07 mark }
1245 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1246 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1247 d2366e29 2022-07-07 mark v->focus_child = 1;
1248 d2366e29 2022-07-07 mark
1249 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1250 d2366e29 2022-07-07 mark if (err)
1251 d2366e29 2022-07-07 mark return err;
1252 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1253 d2366e29 2022-07-07 mark if (err)
1254 d2366e29 2022-07-07 mark return err;
1255 d2366e29 2022-07-07 mark
1256 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1257 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1258 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1259 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1260 279d2047 2022-07-29 stsp if (err)
1261 279d2047 2022-07-29 stsp return err;
1262 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1263 279d2047 2022-07-29 stsp if (err)
1264 279d2047 2022-07-29 stsp return err;
1265 7532ccda 2022-07-11 mark } else {
1266 7532ccda 2022-07-11 mark offset_selection_up(v);
1267 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1268 d2366e29 2022-07-07 mark }
1269 dff91825 2022-07-22 mark if (v->resize)
1270 dff91825 2022-07-22 mark err = v->resize(v, 0);
1271 dff91825 2022-07-22 mark else if (v->child->resize)
1272 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1273 d2366e29 2022-07-07 mark
1274 d2366e29 2022-07-07 mark return err;
1275 0cf4efb1 2018-09-29 stsp }
1276 6d0fee91 2018-08-01 stsp
1277 640cd7ff 2022-06-22 mark /*
1278 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1279 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1280 640cd7ff 2022-06-22 mark */
1281 640cd7ff 2022-06-22 mark static int
1282 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1283 640cd7ff 2022-06-22 mark {
1284 9b058f45 2022-06-30 mark struct tog_view *v = view;
1285 9b058f45 2022-06-30 mark int x, n = 0;
1286 640cd7ff 2022-06-22 mark
1287 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1288 9b058f45 2022-06-30 mark v = view->child;
1289 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1290 9b058f45 2022-06-30 mark v = view->parent;
1291 9b058f45 2022-06-30 mark
1292 640cd7ff 2022-06-22 mark view->count = 0;
1293 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1294 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1295 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1296 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1297 9b058f45 2022-06-30 mark waddch(v->window, ':');
1298 640cd7ff 2022-06-22 mark
1299 640cd7ff 2022-06-22 mark do {
1300 9b058f45 2022-06-30 mark x = getcurx(v->window);
1301 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1302 9b058f45 2022-06-30 mark waddch(v->window, c);
1303 9b058f45 2022-06-30 mark wrefresh(v->window);
1304 9b058f45 2022-06-30 mark }
1305 9b058f45 2022-06-30 mark
1306 640cd7ff 2022-06-22 mark /*
1307 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1308 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1309 640cd7ff 2022-06-22 mark */
1310 640cd7ff 2022-06-22 mark if (n >= 9999999)
1311 640cd7ff 2022-06-22 mark n = 9999999;
1312 640cd7ff 2022-06-22 mark else
1313 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1314 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1315 640cd7ff 2022-06-22 mark
1316 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1317 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1318 94b80cfa 2022-08-01 mark n = 0;
1319 94b80cfa 2022-08-01 mark c = 0;
1320 94b80cfa 2022-08-01 mark }
1321 94b80cfa 2022-08-01 mark
1322 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1323 640cd7ff 2022-06-22 mark view->count = n;
1324 640cd7ff 2022-06-22 mark
1325 640cd7ff 2022-06-22 mark return c;
1326 640cd7ff 2022-06-22 mark }
1327 640cd7ff 2022-06-22 mark
1328 0cf4efb1 2018-09-29 stsp static const struct got_error *
1329 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1330 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1331 e5a0f69f 2018-08-18 stsp {
1332 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1333 669b5ffa 2018-10-07 stsp struct tog_view *v;
1334 1a76625f 2018-10-22 stsp int ch, errcode;
1335 e5a0f69f 2018-08-18 stsp
1336 e5a0f69f 2018-08-18 stsp *new = NULL;
1337 8f4ed634 2020-03-26 stsp
1338 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1339 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1340 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1341 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1342 640cd7ff 2022-06-22 mark view->count = 0;
1343 640cd7ff 2022-06-22 mark }
1344 e5a0f69f 2018-08-18 stsp
1345 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1346 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1347 82954512 2020-02-03 stsp if (errcode)
1348 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1349 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1350 3da8ef85 2021-09-21 stsp sched_yield();
1351 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1352 82954512 2020-02-03 stsp if (errcode)
1353 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1354 82954512 2020-02-03 stsp "pthread_mutex_lock");
1355 60493ae3 2019-06-20 stsp view->search_next(view);
1356 60493ae3 2019-06-20 stsp return NULL;
1357 60493ae3 2019-06-20 stsp }
1358 60493ae3 2019-06-20 stsp
1359 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1360 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1361 1a76625f 2018-10-22 stsp if (errcode)
1362 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1363 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1364 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1365 a6d37fac 2022-07-03 mark cbreak();
1366 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1367 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1368 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1369 a6d37fac 2022-07-03 mark view->count = 0;
1370 a6d37fac 2022-07-03 mark else
1371 a6d37fac 2022-07-03 mark ch = view->ch;
1372 a6d37fac 2022-07-03 mark } else {
1373 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1374 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1375 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1376 640cd7ff 2022-06-22 mark }
1377 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1378 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1379 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1380 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1381 1a76625f 2018-10-22 stsp if (errcode)
1382 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1383 25791caa 2018-10-24 stsp
1384 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1385 25791caa 2018-10-24 stsp tog_resizeterm();
1386 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1387 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1388 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1389 25791caa 2018-10-24 stsp err = view_resize(v);
1390 25791caa 2018-10-24 stsp if (err)
1391 25791caa 2018-10-24 stsp return err;
1392 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1393 25791caa 2018-10-24 stsp if (err)
1394 25791caa 2018-10-24 stsp return err;
1395 cdfcfb03 2020-12-06 stsp if (v->child) {
1396 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1397 cdfcfb03 2020-12-06 stsp if (err)
1398 cdfcfb03 2020-12-06 stsp return err;
1399 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1400 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1401 cdfcfb03 2020-12-06 stsp if (err)
1402 cdfcfb03 2020-12-06 stsp return err;
1403 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1404 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1405 3c1dfe12 2022-07-08 mark if (err)
1406 3c1dfe12 2022-07-08 mark return err;
1407 3c1dfe12 2022-07-08 mark }
1408 cdfcfb03 2020-12-06 stsp }
1409 25791caa 2018-10-24 stsp }
1410 25791caa 2018-10-24 stsp }
1411 25791caa 2018-10-24 stsp
1412 e5a0f69f 2018-08-18 stsp switch (ch) {
1413 1e37a5c2 2019-05-12 jcs case '\t':
1414 640cd7ff 2022-06-22 mark view->count = 0;
1415 1e37a5c2 2019-05-12 jcs if (view->child) {
1416 e78dc838 2020-12-04 stsp view->focussed = 0;
1417 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1418 e78dc838 2020-12-04 stsp view->focus_child = 1;
1419 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1420 e78dc838 2020-12-04 stsp view->focussed = 0;
1421 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1422 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1423 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1424 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1425 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1426 6fe51fee 2022-07-22 mark 0);
1427 9b058f45 2022-06-30 mark if (err)
1428 9b058f45 2022-06-30 mark return err;
1429 9b058f45 2022-06-30 mark }
1430 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1431 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1432 9b058f45 2022-06-30 mark if (err)
1433 9b058f45 2022-06-30 mark return err;
1434 9b058f45 2022-06-30 mark }
1435 1e37a5c2 2019-05-12 jcs }
1436 1e37a5c2 2019-05-12 jcs break;
1437 1e37a5c2 2019-05-12 jcs case 'q':
1438 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1439 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1440 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1441 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1442 9b058f45 2022-06-30 mark if (err)
1443 9b058f45 2022-06-30 mark break;
1444 9b058f45 2022-06-30 mark }
1445 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1446 9b058f45 2022-06-30 mark }
1447 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1448 9970f7fc 2020-12-03 stsp view->dying = 1;
1449 1e37a5c2 2019-05-12 jcs break;
1450 1e37a5c2 2019-05-12 jcs case 'Q':
1451 1e37a5c2 2019-05-12 jcs *done = 1;
1452 1e37a5c2 2019-05-12 jcs break;
1453 61417565 2022-06-20 mark case 'F':
1454 640cd7ff 2022-06-22 mark view->count = 0;
1455 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1456 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1457 1e37a5c2 2019-05-12 jcs break;
1458 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1459 e78dc838 2020-12-04 stsp view->focussed = 0;
1460 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1461 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1462 3c1dfe12 2022-07-08 mark } else {
1463 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1464 3c1dfe12 2022-07-08 mark if (!err)
1465 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1466 3c1dfe12 2022-07-08 mark }
1467 1e37a5c2 2019-05-12 jcs if (err)
1468 1e37a5c2 2019-05-12 jcs break;
1469 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1470 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1471 1e37a5c2 2019-05-12 jcs } else {
1472 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1473 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1474 e78dc838 2020-12-04 stsp view->focussed = 1;
1475 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1476 1e37a5c2 2019-05-12 jcs } else {
1477 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1478 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1479 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1480 3c1dfe12 2022-07-08 mark if (!err)
1481 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1482 669b5ffa 2018-10-07 stsp }
1483 1e37a5c2 2019-05-12 jcs if (err)
1484 1e37a5c2 2019-05-12 jcs break;
1485 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1486 1e37a5c2 2019-05-12 jcs }
1487 9b058f45 2022-06-30 mark if (err)
1488 9b058f45 2022-06-30 mark break;
1489 6fe51fee 2022-07-22 mark if (view->resize) {
1490 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1491 9b058f45 2022-06-30 mark if (err)
1492 9b058f45 2022-06-30 mark break;
1493 9b058f45 2022-06-30 mark }
1494 9b058f45 2022-06-30 mark if (view->parent)
1495 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1496 9b058f45 2022-06-30 mark if (!err)
1497 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1498 1e37a5c2 2019-05-12 jcs break;
1499 d2366e29 2022-07-07 mark case 'S':
1500 3c1dfe12 2022-07-08 mark view->count = 0;
1501 d2366e29 2022-07-07 mark err = switch_split(view);
1502 3c1dfe12 2022-07-08 mark break;
1503 3c1dfe12 2022-07-08 mark case '-':
1504 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1505 d2366e29 2022-07-07 mark break;
1506 3c1dfe12 2022-07-08 mark case '+':
1507 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1508 3c1dfe12 2022-07-08 mark break;
1509 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1510 60493ae3 2019-06-20 stsp break;
1511 60493ae3 2019-06-20 stsp case '/':
1512 640cd7ff 2022-06-22 mark view->count = 0;
1513 60493ae3 2019-06-20 stsp if (view->search_start)
1514 2b49a8ae 2019-06-22 stsp view_search_start(view);
1515 60493ae3 2019-06-20 stsp else
1516 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1517 1e37a5c2 2019-05-12 jcs break;
1518 b1bf1435 2019-06-21 stsp case 'N':
1519 60493ae3 2019-06-20 stsp case 'n':
1520 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1521 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1522 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1523 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1524 60493ae3 2019-06-20 stsp view->search_next(view);
1525 60493ae3 2019-06-20 stsp } else
1526 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1527 917d79a7 2022-07-01 stsp break;
1528 917d79a7 2022-07-01 stsp case 'A':
1529 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1530 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1531 917d79a7 2022-07-01 stsp else
1532 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1533 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1534 917d79a7 2022-07-01 stsp if (v->reset) {
1535 917d79a7 2022-07-01 stsp err = v->reset(v);
1536 917d79a7 2022-07-01 stsp if (err)
1537 917d79a7 2022-07-01 stsp return err;
1538 917d79a7 2022-07-01 stsp }
1539 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1540 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1541 917d79a7 2022-07-01 stsp if (err)
1542 917d79a7 2022-07-01 stsp return err;
1543 917d79a7 2022-07-01 stsp }
1544 917d79a7 2022-07-01 stsp }
1545 60493ae3 2019-06-20 stsp break;
1546 1e37a5c2 2019-05-12 jcs default:
1547 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1548 1e37a5c2 2019-05-12 jcs break;
1549 e5a0f69f 2018-08-18 stsp }
1550 e5a0f69f 2018-08-18 stsp
1551 e5a0f69f 2018-08-18 stsp return err;
1552 e5a0f69f 2018-08-18 stsp }
1553 e5a0f69f 2018-08-18 stsp
1554 336075a4 2022-06-25 op static int
1555 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1556 a3404814 2018-09-02 stsp {
1557 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1558 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1559 669b5ffa 2018-10-07 stsp return 0;
1560 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1561 669b5ffa 2018-10-07 stsp return 0;
1562 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1563 a3404814 2018-09-02 stsp return 0;
1564 a3404814 2018-09-02 stsp
1565 669b5ffa 2018-10-07 stsp return view->focussed;
1566 a3404814 2018-09-02 stsp }
1567 a3404814 2018-09-02 stsp
1568 bcbd79e2 2018-08-19 stsp static const struct got_error *
1569 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1570 e5a0f69f 2018-08-18 stsp {
1571 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1572 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1573 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1574 d2366e29 2022-07-07 mark char *mode;
1575 fd823528 2018-10-22 stsp int fast_refresh = 10;
1576 1a76625f 2018-10-22 stsp int done = 0, errcode;
1577 e5a0f69f 2018-08-18 stsp
1578 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1579 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1580 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1581 d2366e29 2022-07-07 mark else
1582 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1583 d2366e29 2022-07-07 mark
1584 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1585 1a76625f 2018-10-22 stsp if (errcode)
1586 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1587 1a76625f 2018-10-22 stsp
1588 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1589 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1590 e5a0f69f 2018-08-18 stsp
1591 1004088d 2018-09-29 stsp view->focussed = 1;
1592 878940b7 2018-09-29 stsp err = view->show(view);
1593 0cf4efb1 2018-09-29 stsp if (err)
1594 0cf4efb1 2018-09-29 stsp return err;
1595 0cf4efb1 2018-09-29 stsp update_panels();
1596 0cf4efb1 2018-09-29 stsp doupdate();
1597 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1598 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1599 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1600 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1601 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1602 fd823528 2018-10-22 stsp
1603 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1604 e5a0f69f 2018-08-18 stsp if (err)
1605 e5a0f69f 2018-08-18 stsp break;
1606 9970f7fc 2020-12-03 stsp if (view->dying) {
1607 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1608 669b5ffa 2018-10-07 stsp
1609 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1610 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1611 9970f7fc 2020-12-03 stsp entry);
1612 e78dc838 2020-12-04 stsp else if (view->parent)
1613 669b5ffa 2018-10-07 stsp prev = view->parent;
1614 669b5ffa 2018-10-07 stsp
1615 e78dc838 2020-12-04 stsp if (view->parent) {
1616 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1617 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1618 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1619 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1620 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1621 0dbbbe90 2022-06-17 op if (err)
1622 0dbbbe90 2022-06-17 op break;
1623 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1624 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1625 e78dc838 2020-12-04 stsp } else
1626 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1627 669b5ffa 2018-10-07 stsp
1628 9970f7fc 2020-12-03 stsp err = view_close(view);
1629 fb59748f 2020-12-05 stsp if (err)
1630 e5a0f69f 2018-08-18 stsp goto done;
1631 669b5ffa 2018-10-07 stsp
1632 e78dc838 2020-12-04 stsp view = NULL;
1633 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1634 e78dc838 2020-12-04 stsp if (v->focussed)
1635 e78dc838 2020-12-04 stsp break;
1636 0cf4efb1 2018-09-29 stsp }
1637 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1638 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1639 e78dc838 2020-12-04 stsp if (prev)
1640 e78dc838 2020-12-04 stsp view = prev;
1641 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1642 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1643 e78dc838 2020-12-04 stsp tog_view_list_head);
1644 e78dc838 2020-12-04 stsp }
1645 e78dc838 2020-12-04 stsp if (view) {
1646 e78dc838 2020-12-04 stsp if (view->focus_child) {
1647 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1648 e78dc838 2020-12-04 stsp view = view->child;
1649 e78dc838 2020-12-04 stsp } else
1650 e78dc838 2020-12-04 stsp view->focussed = 1;
1651 e78dc838 2020-12-04 stsp }
1652 e78dc838 2020-12-04 stsp }
1653 e5a0f69f 2018-08-18 stsp }
1654 bcbd79e2 2018-08-19 stsp if (new_view) {
1655 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1656 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1657 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1658 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1659 86c66b02 2018-10-18 stsp continue;
1660 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1661 86c66b02 2018-10-18 stsp err = view_close(v);
1662 86c66b02 2018-10-18 stsp if (err)
1663 86c66b02 2018-10-18 stsp goto done;
1664 86c66b02 2018-10-18 stsp break;
1665 86c66b02 2018-10-18 stsp }
1666 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1667 fed7eaa8 2018-10-24 stsp view = new_view;
1668 0ae84acc 2022-06-15 tracey }
1669 669b5ffa 2018-10-07 stsp if (view) {
1670 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1671 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1672 e78dc838 2020-12-04 stsp view = view->child;
1673 e78dc838 2020-12-04 stsp } else {
1674 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1675 e78dc838 2020-12-04 stsp view = view->parent;
1676 1a76625f 2018-10-22 stsp }
1677 e78dc838 2020-12-04 stsp show_panel(view->panel);
1678 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1679 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1680 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1681 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1682 669b5ffa 2018-10-07 stsp if (err)
1683 1a76625f 2018-10-22 stsp goto done;
1684 669b5ffa 2018-10-07 stsp }
1685 669b5ffa 2018-10-07 stsp err = view->show(view);
1686 0cf4efb1 2018-09-29 stsp if (err)
1687 1a76625f 2018-10-22 stsp goto done;
1688 669b5ffa 2018-10-07 stsp if (view->child) {
1689 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1690 669b5ffa 2018-10-07 stsp if (err)
1691 1a76625f 2018-10-22 stsp goto done;
1692 669b5ffa 2018-10-07 stsp }
1693 1a76625f 2018-10-22 stsp update_panels();
1694 1a76625f 2018-10-22 stsp doupdate();
1695 0cf4efb1 2018-09-29 stsp }
1696 e5a0f69f 2018-08-18 stsp }
1697 e5a0f69f 2018-08-18 stsp done:
1698 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1699 5629093a 2022-07-11 stsp const struct got_error *close_err;
1700 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1701 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1702 5629093a 2022-07-11 stsp close_err = view_close(view);
1703 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1704 5629093a 2022-07-11 stsp err = close_err;
1705 e5a0f69f 2018-08-18 stsp }
1706 1a76625f 2018-10-22 stsp
1707 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1708 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1709 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1710 1a76625f 2018-10-22 stsp
1711 e5a0f69f 2018-08-18 stsp return err;
1712 ea5e7bb5 2018-08-01 stsp }
1713 ea5e7bb5 2018-08-01 stsp
1714 4ed7e80c 2018-05-20 stsp __dead static void
1715 9f7d7167 2018-04-29 stsp usage_log(void)
1716 9f7d7167 2018-04-29 stsp {
1717 80ddbec8 2018-04-29 stsp endwin();
1718 c70c5802 2018-08-01 stsp fprintf(stderr,
1719 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1720 9f7d7167 2018-04-29 stsp getprogname());
1721 9f7d7167 2018-04-29 stsp exit(1);
1722 80ddbec8 2018-04-29 stsp }
1723 80ddbec8 2018-04-29 stsp
1724 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1725 80ddbec8 2018-04-29 stsp static const struct got_error *
1726 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1727 963b370f 2018-05-20 stsp {
1728 00dfcb92 2018-06-11 stsp char *vis = NULL;
1729 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1730 963b370f 2018-05-20 stsp
1731 963b370f 2018-05-20 stsp *ws = NULL;
1732 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1733 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1734 00dfcb92 2018-06-11 stsp int vislen;
1735 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1736 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1737 00dfcb92 2018-06-11 stsp
1738 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1739 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1740 00dfcb92 2018-06-11 stsp if (err)
1741 00dfcb92 2018-06-11 stsp return err;
1742 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1743 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1744 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1745 a7f50699 2018-06-11 stsp goto done;
1746 a7f50699 2018-06-11 stsp }
1747 00dfcb92 2018-06-11 stsp }
1748 963b370f 2018-05-20 stsp
1749 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1750 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1751 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1752 a7f50699 2018-06-11 stsp goto done;
1753 a7f50699 2018-06-11 stsp }
1754 963b370f 2018-05-20 stsp
1755 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1757 a7f50699 2018-06-11 stsp done:
1758 00dfcb92 2018-06-11 stsp free(vis);
1759 963b370f 2018-05-20 stsp if (err) {
1760 963b370f 2018-05-20 stsp free(*ws);
1761 963b370f 2018-05-20 stsp *ws = NULL;
1762 963b370f 2018-05-20 stsp *wlen = 0;
1763 963b370f 2018-05-20 stsp }
1764 963b370f 2018-05-20 stsp return err;
1765 145b6838 2022-06-16 stsp }
1766 145b6838 2022-06-16 stsp
1767 145b6838 2022-06-16 stsp static const struct got_error *
1768 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1769 145b6838 2022-06-16 stsp {
1770 145b6838 2022-06-16 stsp char *dst;
1771 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1772 145b6838 2022-06-16 stsp
1773 145b6838 2022-06-16 stsp *ptr = NULL;
1774 145b6838 2022-06-16 stsp n = len = strlen(src);
1775 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1776 145b6838 2022-06-16 stsp if (dst == NULL)
1777 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1778 145b6838 2022-06-16 stsp
1779 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1780 145b6838 2022-06-16 stsp const char c = src[idx];
1781 145b6838 2022-06-16 stsp
1782 145b6838 2022-06-16 stsp if (c == '\t') {
1783 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1784 367ddf28 2022-06-16 mark char *p;
1785 367ddf28 2022-06-16 mark
1786 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1787 6e1c41ad 2022-06-16 mark if (p == NULL) {
1788 6e1c41ad 2022-06-16 mark free(dst);
1789 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1790 6e1c41ad 2022-06-16 mark
1791 6e1c41ad 2022-06-16 mark }
1792 6e1c41ad 2022-06-16 mark dst = p;
1793 145b6838 2022-06-16 stsp n += nb;
1794 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1795 145b6838 2022-06-16 stsp sz += nb;
1796 145b6838 2022-06-16 stsp } else
1797 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1798 145b6838 2022-06-16 stsp ++idx;
1799 145b6838 2022-06-16 stsp }
1800 145b6838 2022-06-16 stsp
1801 145b6838 2022-06-16 stsp dst[sz] = '\0';
1802 145b6838 2022-06-16 stsp *ptr = dst;
1803 145b6838 2022-06-16 stsp return NULL;
1804 963b370f 2018-05-20 stsp }
1805 963b370f 2018-05-20 stsp
1806 4e4a9ac8 2022-06-17 op /*
1807 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1808 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1809 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1810 4e4a9ac8 2022-06-17 op * *rcol.
1811 ccda2f4d 2022-06-16 stsp */
1812 4e4a9ac8 2022-06-17 op static int
1813 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1814 4e4a9ac8 2022-06-17 op {
1815 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1816 ccda2f4d 2022-06-16 stsp
1817 4e4a9ac8 2022-06-17 op if (n == 0) {
1818 4e4a9ac8 2022-06-17 op *rcol = cols;
1819 4e4a9ac8 2022-06-17 op return off;
1820 4e4a9ac8 2022-06-17 op }
1821 ccda2f4d 2022-06-16 stsp
1822 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1823 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1824 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1825 4e4a9ac8 2022-06-17 op else
1826 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1827 ccda2f4d 2022-06-16 stsp
1828 4e4a9ac8 2022-06-17 op if (width == -1) {
1829 4e4a9ac8 2022-06-17 op width = 1;
1830 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1831 ccda2f4d 2022-06-16 stsp }
1832 ccda2f4d 2022-06-16 stsp
1833 4e4a9ac8 2022-06-17 op if (cols + width > n)
1834 4e4a9ac8 2022-06-17 op break;
1835 4e4a9ac8 2022-06-17 op cols += width;
1836 ccda2f4d 2022-06-16 stsp }
1837 ccda2f4d 2022-06-16 stsp
1838 4e4a9ac8 2022-06-17 op *rcol = cols;
1839 4e4a9ac8 2022-06-17 op return i;
1840 ccda2f4d 2022-06-16 stsp }
1841 ccda2f4d 2022-06-16 stsp
1842 ccda2f4d 2022-06-16 stsp /*
1843 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1844 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1845 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1846 ccda2f4d 2022-06-16 stsp */
1847 ccda2f4d 2022-06-16 stsp static const struct got_error *
1848 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1849 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1850 ccda2f4d 2022-06-16 stsp {
1851 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1852 4e4a9ac8 2022-06-17 op int cols;
1853 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1854 145b6838 2022-06-16 stsp char *exstr = NULL;
1855 963b370f 2018-05-20 stsp size_t wlen;
1856 4e4a9ac8 2022-06-17 op int i, scrollx;
1857 963b370f 2018-05-20 stsp
1858 963b370f 2018-05-20 stsp *wlinep = NULL;
1859 b700b5d6 2018-07-10 stsp *widthp = 0;
1860 963b370f 2018-05-20 stsp
1861 145b6838 2022-06-16 stsp if (expand) {
1862 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1863 145b6838 2022-06-16 stsp if (err)
1864 145b6838 2022-06-16 stsp return err;
1865 145b6838 2022-06-16 stsp }
1866 145b6838 2022-06-16 stsp
1867 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1868 145b6838 2022-06-16 stsp free(exstr);
1869 963b370f 2018-05-20 stsp if (err)
1870 963b370f 2018-05-20 stsp return err;
1871 963b370f 2018-05-20 stsp
1872 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1873 ccda2f4d 2022-06-16 stsp
1874 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1875 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1876 3f670bfb 2020-12-10 stsp wlen--;
1877 3f670bfb 2020-12-10 stsp }
1878 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1879 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1880 3f670bfb 2020-12-10 stsp wlen--;
1881 3f670bfb 2020-12-10 stsp }
1882 3f670bfb 2020-12-10 stsp
1883 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1884 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1885 27a741e5 2019-09-11 stsp
1886 b700b5d6 2018-07-10 stsp if (widthp)
1887 b700b5d6 2018-07-10 stsp *widthp = cols;
1888 ccda2f4d 2022-06-16 stsp if (scrollxp)
1889 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1890 963b370f 2018-05-20 stsp if (err)
1891 963b370f 2018-05-20 stsp free(wline);
1892 963b370f 2018-05-20 stsp else
1893 963b370f 2018-05-20 stsp *wlinep = wline;
1894 963b370f 2018-05-20 stsp return err;
1895 963b370f 2018-05-20 stsp }
1896 963b370f 2018-05-20 stsp
1897 8b473291 2019-02-21 stsp static const struct got_error*
1898 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1899 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1900 8b473291 2019-02-21 stsp {
1901 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1902 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1903 8b473291 2019-02-21 stsp char *s;
1904 8b473291 2019-02-21 stsp const char *name;
1905 8b473291 2019-02-21 stsp
1906 8b473291 2019-02-21 stsp *refs_str = NULL;
1907 8b473291 2019-02-21 stsp
1908 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1909 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1910 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1911 52b5abe1 2019-08-13 stsp int cmp;
1912 52b5abe1 2019-08-13 stsp
1913 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1914 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1915 8b473291 2019-02-21 stsp continue;
1916 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1917 8b473291 2019-02-21 stsp name += 5;
1918 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1919 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1920 7143d404 2019-03-12 stsp continue;
1921 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1922 8b473291 2019-02-21 stsp name += 6;
1923 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1924 8b473291 2019-02-21 stsp name += 8;
1925 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1926 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1927 79cc719f 2020-04-24 stsp continue;
1928 79cc719f 2020-04-24 stsp }
1929 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1930 48cae60d 2020-09-22 stsp if (err)
1931 48cae60d 2020-09-22 stsp break;
1932 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1933 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1934 5d844a1e 2019-08-13 stsp if (err) {
1935 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1936 48cae60d 2020-09-22 stsp free(ref_id);
1937 5d844a1e 2019-08-13 stsp break;
1938 48cae60d 2020-09-22 stsp }
1939 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1940 5d844a1e 2019-08-13 stsp err = NULL;
1941 5d844a1e 2019-08-13 stsp tag = NULL;
1942 5d844a1e 2019-08-13 stsp }
1943 52b5abe1 2019-08-13 stsp }
1944 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1945 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1946 48cae60d 2020-09-22 stsp free(ref_id);
1947 52b5abe1 2019-08-13 stsp if (tag)
1948 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1949 52b5abe1 2019-08-13 stsp if (cmp != 0)
1950 52b5abe1 2019-08-13 stsp continue;
1951 8b473291 2019-02-21 stsp s = *refs_str;
1952 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1953 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1954 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1955 8b473291 2019-02-21 stsp free(s);
1956 8b473291 2019-02-21 stsp *refs_str = NULL;
1957 8b473291 2019-02-21 stsp break;
1958 8b473291 2019-02-21 stsp }
1959 8b473291 2019-02-21 stsp free(s);
1960 8b473291 2019-02-21 stsp }
1961 8b473291 2019-02-21 stsp
1962 8b473291 2019-02-21 stsp return err;
1963 8b473291 2019-02-21 stsp }
1964 8b473291 2019-02-21 stsp
1965 963b370f 2018-05-20 stsp static const struct got_error *
1966 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1967 27a741e5 2019-09-11 stsp int col_tab_align)
1968 5813d178 2019-03-09 stsp {
1969 e6b8b890 2020-12-29 naddy char *smallerthan;
1970 5813d178 2019-03-09 stsp
1971 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1972 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1973 5813d178 2019-03-09 stsp author = smallerthan + 1;
1974 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1975 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1976 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1977 5813d178 2019-03-09 stsp }
1978 5813d178 2019-03-09 stsp
1979 5813d178 2019-03-09 stsp static const struct got_error *
1980 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1981 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1982 8fdc79fe 2020-12-01 naddy int author_display_cols)
1983 80ddbec8 2018-04-29 stsp {
1984 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1985 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1986 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1987 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1988 5813d178 2019-03-09 stsp char *author = NULL;
1989 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1990 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1991 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1992 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1993 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1994 ccb26ccd 2018-11-05 stsp struct tm tm;
1995 45d799e2 2018-12-23 stsp time_t committer_time;
1996 11b20872 2019-11-08 stsp struct tog_color *tc;
1997 80ddbec8 2018-04-29 stsp
1998 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1999 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2000 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2001 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2002 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2003 b39d25c7 2018-07-10 stsp
2004 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2005 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2006 b39d25c7 2018-07-10 stsp else
2007 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2008 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2009 11b20872 2019-11-08 stsp if (tc)
2010 11b20872 2019-11-08 stsp wattr_on(view->window,
2011 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2012 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2013 11b20872 2019-11-08 stsp if (tc)
2014 11b20872 2019-11-08 stsp wattr_off(view->window,
2015 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2016 27a741e5 2019-09-11 stsp col = limit;
2017 b39d25c7 2018-07-10 stsp if (col > avail)
2018 b39d25c7 2018-07-10 stsp goto done;
2019 6570a66d 2019-11-08 stsp
2020 6570a66d 2019-11-08 stsp if (avail >= 120) {
2021 6570a66d 2019-11-08 stsp char *id_str;
2022 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2023 6570a66d 2019-11-08 stsp if (err)
2024 6570a66d 2019-11-08 stsp goto done;
2025 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2026 11b20872 2019-11-08 stsp if (tc)
2027 11b20872 2019-11-08 stsp wattr_on(view->window,
2028 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2029 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2030 11b20872 2019-11-08 stsp if (tc)
2031 11b20872 2019-11-08 stsp wattr_off(view->window,
2032 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2033 6570a66d 2019-11-08 stsp free(id_str);
2034 6570a66d 2019-11-08 stsp col += 9;
2035 6570a66d 2019-11-08 stsp if (col > avail)
2036 6570a66d 2019-11-08 stsp goto done;
2037 6570a66d 2019-11-08 stsp }
2038 b39d25c7 2018-07-10 stsp
2039 10aab77f 2022-07-19 op if (s->use_committer)
2040 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2041 10aab77f 2022-07-19 op else
2042 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2043 5813d178 2019-03-09 stsp if (author == NULL) {
2044 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2045 80ddbec8 2018-04-29 stsp goto done;
2046 80ddbec8 2018-04-29 stsp }
2047 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2048 bb737323 2018-05-20 stsp if (err)
2049 bb737323 2018-05-20 stsp goto done;
2050 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2051 11b20872 2019-11-08 stsp if (tc)
2052 11b20872 2019-11-08 stsp wattr_on(view->window,
2053 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2054 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2055 bb737323 2018-05-20 stsp col += author_width;
2056 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2057 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2058 bb737323 2018-05-20 stsp col++;
2059 bb737323 2018-05-20 stsp author_width++;
2060 bb737323 2018-05-20 stsp }
2061 f0f62654 2022-09-08 mark if (tc)
2062 f0f62654 2022-09-08 mark wattr_off(view->window,
2063 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2064 9c2eaf34 2018-05-20 stsp if (col > avail)
2065 9c2eaf34 2018-05-20 stsp goto done;
2066 80ddbec8 2018-04-29 stsp
2067 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2068 5943eee2 2019-08-13 stsp if (err)
2069 6d9fbc00 2018-04-29 stsp goto done;
2070 bb737323 2018-05-20 stsp logmsg = logmsg0;
2071 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2072 bb737323 2018-05-20 stsp logmsg++;
2073 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2074 bb737323 2018-05-20 stsp if (newline)
2075 bb737323 2018-05-20 stsp *newline = '\0';
2076 ccda2f4d 2022-06-16 stsp limit = avail - col;
2077 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2078 4d1f6af3 2022-06-17 op limit--; /* for the border */
2079 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2080 ccda2f4d 2022-06-16 stsp limit, col, 1);
2081 29688b02 2022-06-16 stsp if (err)
2082 29688b02 2022-06-16 stsp goto done;
2083 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2084 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2085 27a741e5 2019-09-11 stsp while (col < avail) {
2086 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2087 bb737323 2018-05-20 stsp col++;
2088 881b2d3e 2018-04-30 stsp }
2089 80ddbec8 2018-04-29 stsp done:
2090 80ddbec8 2018-04-29 stsp free(logmsg0);
2091 bb737323 2018-05-20 stsp free(wlogmsg);
2092 5813d178 2019-03-09 stsp free(author);
2093 bb737323 2018-05-20 stsp free(wauthor);
2094 80ddbec8 2018-04-29 stsp free(line);
2095 80ddbec8 2018-04-29 stsp return err;
2096 80ddbec8 2018-04-29 stsp }
2097 26ed57b2 2018-05-19 stsp
2098 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2099 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2100 899d86c2 2018-05-10 stsp struct got_object_id *id)
2101 80ddbec8 2018-04-29 stsp {
2102 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2103 e15c42de 2022-09-05 op struct got_object_id *dup;
2104 80ddbec8 2018-04-29 stsp
2105 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2106 80ddbec8 2018-04-29 stsp if (entry == NULL)
2107 899d86c2 2018-05-10 stsp return NULL;
2108 99db9666 2018-05-07 stsp
2109 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2110 e15c42de 2022-09-05 op if (dup == NULL) {
2111 e15c42de 2022-09-05 op free(entry);
2112 e15c42de 2022-09-05 op return NULL;
2113 e15c42de 2022-09-05 op }
2114 e15c42de 2022-09-05 op
2115 e15c42de 2022-09-05 op entry->id = dup;
2116 99db9666 2018-05-07 stsp entry->commit = commit;
2117 899d86c2 2018-05-10 stsp return entry;
2118 99db9666 2018-05-07 stsp }
2119 80ddbec8 2018-04-29 stsp
2120 99db9666 2018-05-07 stsp static void
2121 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2122 99db9666 2018-05-07 stsp {
2123 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2124 99db9666 2018-05-07 stsp
2125 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2126 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2127 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2128 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2129 e15c42de 2022-09-05 op free(entry->id);
2130 99db9666 2018-05-07 stsp free(entry);
2131 99db9666 2018-05-07 stsp }
2132 99db9666 2018-05-07 stsp
2133 99db9666 2018-05-07 stsp static void
2134 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2135 99db9666 2018-05-07 stsp {
2136 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2137 99db9666 2018-05-07 stsp pop_commit(commits);
2138 c4972b91 2018-05-07 stsp }
2139 c4972b91 2018-05-07 stsp
2140 c4972b91 2018-05-07 stsp static const struct got_error *
2141 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2142 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2143 13add988 2019-10-15 stsp {
2144 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2145 13add988 2019-10-15 stsp regmatch_t regmatch;
2146 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2147 13add988 2019-10-15 stsp
2148 13add988 2019-10-15 stsp *have_match = 0;
2149 13add988 2019-10-15 stsp
2150 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2151 13add988 2019-10-15 stsp if (err)
2152 13add988 2019-10-15 stsp return err;
2153 13add988 2019-10-15 stsp
2154 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2155 13add988 2019-10-15 stsp if (err)
2156 13add988 2019-10-15 stsp goto done;
2157 13add988 2019-10-15 stsp
2158 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2159 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2160 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2161 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2162 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2163 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2164 13add988 2019-10-15 stsp *have_match = 1;
2165 13add988 2019-10-15 stsp done:
2166 13add988 2019-10-15 stsp free(id_str);
2167 13add988 2019-10-15 stsp free(logmsg);
2168 13add988 2019-10-15 stsp return err;
2169 13add988 2019-10-15 stsp }
2170 13add988 2019-10-15 stsp
2171 13add988 2019-10-15 stsp static const struct got_error *
2172 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2173 c4972b91 2018-05-07 stsp {
2174 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2175 9ba79e04 2018-06-11 stsp
2176 1a76625f 2018-10-22 stsp /*
2177 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2178 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2179 1a76625f 2018-10-22 stsp * while updating the display.
2180 1a76625f 2018-10-22 stsp */
2181 4e0d2870 2020-12-07 naddy do {
2182 d9787ed8 2022-09-10 op struct got_object_id id;
2183 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2184 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2185 568eae95 2022-09-11 mark int limit_match = 0;
2186 1a76625f 2018-10-22 stsp int errcode;
2187 899d86c2 2018-05-10 stsp
2188 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2189 4e0d2870 2020-12-07 naddy NULL, NULL);
2190 d9787ed8 2022-09-10 op if (err)
2191 ecb28ae0 2018-07-16 stsp break;
2192 899d86c2 2018-05-10 stsp
2193 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2194 9ba79e04 2018-06-11 stsp if (err)
2195 9ba79e04 2018-06-11 stsp break;
2196 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2197 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2198 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2199 9ba79e04 2018-06-11 stsp break;
2200 9ba79e04 2018-06-11 stsp }
2201 93e45b7c 2018-09-24 stsp
2202 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2203 1a76625f 2018-10-22 stsp if (errcode) {
2204 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2205 13add988 2019-10-15 stsp "pthread_mutex_lock");
2206 1a76625f 2018-10-22 stsp break;
2207 1a76625f 2018-10-22 stsp }
2208 1a76625f 2018-10-22 stsp
2209 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2210 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2211 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2212 1a76625f 2018-10-22 stsp
2213 568eae95 2022-09-11 mark if (*a->limiting) {
2214 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2215 568eae95 2022-09-11 mark a->limit_regex);
2216 568eae95 2022-09-11 mark if (err)
2217 568eae95 2022-09-11 mark break;
2218 568eae95 2022-09-11 mark
2219 568eae95 2022-09-11 mark if (limit_match) {
2220 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2221 568eae95 2022-09-11 mark
2222 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2223 568eae95 2022-09-11 mark entry->commit, entry->id);
2224 568eae95 2022-09-11 mark if (matched == NULL) {
2225 568eae95 2022-09-11 mark err = got_error_from_errno(
2226 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2227 568eae95 2022-09-11 mark break;
2228 568eae95 2022-09-11 mark }
2229 568eae95 2022-09-11 mark
2230 568eae95 2022-09-11 mark err = got_object_commit_dup(&matched->commit,
2231 568eae95 2022-09-11 mark entry->commit);
2232 568eae95 2022-09-11 mark if (err)
2233 568eae95 2022-09-11 mark break;
2234 568eae95 2022-09-11 mark
2235 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2236 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2237 568eae95 2022-09-11 mark matched, entry);
2238 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2239 568eae95 2022-09-11 mark }
2240 568eae95 2022-09-11 mark
2241 568eae95 2022-09-11 mark /*
2242 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2243 568eae95 2022-09-11 mark * have found a match, and that it should be
2244 568eae95 2022-09-11 mark * counted as a new entry for the view.
2245 568eae95 2022-09-11 mark */
2246 568eae95 2022-09-11 mark a->limit_match = limit_match;
2247 568eae95 2022-09-11 mark }
2248 568eae95 2022-09-11 mark
2249 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2250 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2251 7c1452c1 2020-03-26 stsp int have_match;
2252 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2253 7c1452c1 2020-03-26 stsp if (err)
2254 7c1452c1 2020-03-26 stsp break;
2255 568eae95 2022-09-11 mark
2256 568eae95 2022-09-11 mark if (*a->limiting) {
2257 568eae95 2022-09-11 mark if (limit_match && have_match)
2258 568eae95 2022-09-11 mark *a->search_next_done =
2259 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2260 568eae95 2022-09-11 mark } else if (have_match)
2261 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2262 13add988 2019-10-15 stsp }
2263 13add988 2019-10-15 stsp
2264 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2265 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2266 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2267 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2268 7c1452c1 2020-03-26 stsp if (err)
2269 13add988 2019-10-15 stsp break;
2270 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2271 899d86c2 2018-05-10 stsp
2272 9ba79e04 2018-06-11 stsp return err;
2273 0553a4e3 2018-04-30 stsp }
2274 0553a4e3 2018-04-30 stsp
2275 2b779855 2020-12-05 naddy static void
2276 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2277 2b779855 2020-12-05 naddy {
2278 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2279 2b779855 2020-12-05 naddy int ncommits = 0;
2280 2b779855 2020-12-05 naddy
2281 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2282 2b779855 2020-12-05 naddy while (entry) {
2283 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2284 2b779855 2020-12-05 naddy s->selected_entry = entry;
2285 2b779855 2020-12-05 naddy break;
2286 2b779855 2020-12-05 naddy }
2287 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2288 2b779855 2020-12-05 naddy ncommits++;
2289 2b779855 2020-12-05 naddy }
2290 2b779855 2020-12-05 naddy }
2291 2b779855 2020-12-05 naddy
2292 0553a4e3 2018-04-30 stsp static const struct got_error *
2293 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2294 0553a4e3 2018-04-30 stsp {
2295 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2296 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2297 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2298 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2299 60493ae3 2019-06-20 stsp int width;
2300 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2301 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2302 8b473291 2019-02-21 stsp char *refs_str = NULL;
2303 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2304 11b20872 2019-11-08 stsp struct tog_color *tc;
2305 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2306 cbb0c8d7 2022-08-12 mark
2307 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2308 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2309 0553a4e3 2018-04-30 stsp
2310 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2311 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2312 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2313 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2314 1a76625f 2018-10-22 stsp if (err)
2315 ecb28ae0 2018-07-16 stsp return err;
2316 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2317 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2318 d2075bf3 2020-12-25 stsp if (refs) {
2319 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2320 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2321 d2075bf3 2020-12-25 stsp if (err)
2322 d2075bf3 2020-12-25 stsp goto done;
2323 d2075bf3 2020-12-25 stsp }
2324 867c6645 2018-07-10 stsp }
2325 359bfafd 2019-02-22 stsp
2326 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2327 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2328 1a76625f 2018-10-22 stsp
2329 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2330 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2331 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2332 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2333 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2334 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2335 8f4ed634 2020-03-26 stsp goto done;
2336 8f4ed634 2020-03-26 stsp }
2337 8f4ed634 2020-03-26 stsp } else {
2338 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2339 568eae95 2022-09-11 mark const char *limit_str = NULL;
2340 f9686aa5 2020-03-27 stsp
2341 f9686aa5 2020-03-27 stsp if (view->searching) {
2342 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2343 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2344 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2345 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2346 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2347 f9686aa5 2020-03-27 stsp search_str = "searching...";
2348 f9686aa5 2020-03-27 stsp }
2349 f9686aa5 2020-03-27 stsp
2350 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2351 568eae95 2022-09-11 mark limit_str = "no matches found";
2352 568eae95 2022-09-11 mark
2353 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2354 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2355 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2356 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2357 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2358 8f4ed634 2020-03-26 stsp goto done;
2359 8f4ed634 2020-03-26 stsp }
2360 8b473291 2019-02-21 stsp }
2361 1a76625f 2018-10-22 stsp
2362 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2363 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2364 87411fa9 2022-06-16 stsp "........................................",
2365 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2366 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2367 1a76625f 2018-10-22 stsp header = NULL;
2368 1a76625f 2018-10-22 stsp goto done;
2369 1a76625f 2018-10-22 stsp }
2370 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2371 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2372 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2373 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2374 1a76625f 2018-10-22 stsp header = NULL;
2375 1a76625f 2018-10-22 stsp goto done;
2376 ecb28ae0 2018-07-16 stsp }
2377 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2378 1a76625f 2018-10-22 stsp if (err)
2379 1a76625f 2018-10-22 stsp goto done;
2380 867c6645 2018-07-10 stsp
2381 2814baeb 2018-08-01 stsp werase(view->window);
2382 867c6645 2018-07-10 stsp
2383 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2384 a3404814 2018-09-02 stsp wstandout(view->window);
2385 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2386 11b20872 2019-11-08 stsp if (tc)
2387 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2388 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2389 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2390 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2391 1a76625f 2018-10-22 stsp width++;
2392 1a76625f 2018-10-22 stsp }
2393 0c6ad1bc 2022-09-09 mark if (tc)
2394 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2395 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2396 a3404814 2018-09-02 stsp wstandend(view->window);
2397 ecb28ae0 2018-07-16 stsp free(wline);
2398 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2399 1a76625f 2018-10-22 stsp goto done;
2400 0553a4e3 2018-04-30 stsp
2401 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2402 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2403 5813d178 2019-03-09 stsp ncommits = 0;
2404 145b6838 2022-06-16 stsp view->maxx = 0;
2405 5813d178 2019-03-09 stsp while (entry) {
2406 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2407 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2408 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2409 5813d178 2019-03-09 stsp int width;
2410 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2411 5813d178 2019-03-09 stsp break;
2412 10aab77f 2022-07-19 op if (s->use_committer)
2413 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2414 10aab77f 2022-07-19 op else
2415 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2416 5813d178 2019-03-09 stsp if (author == NULL) {
2417 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2418 5813d178 2019-03-09 stsp goto done;
2419 5813d178 2019-03-09 stsp }
2420 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2421 27a741e5 2019-09-11 stsp date_display_cols);
2422 5813d178 2019-03-09 stsp if (author_cols < width)
2423 5813d178 2019-03-09 stsp author_cols = width;
2424 5813d178 2019-03-09 stsp free(wauthor);
2425 5813d178 2019-03-09 stsp free(author);
2426 a310d9c3 2022-07-21 florian if (err)
2427 a310d9c3 2022-07-21 florian goto done;
2428 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2429 145b6838 2022-06-16 stsp if (err)
2430 145b6838 2022-06-16 stsp goto done;
2431 145b6838 2022-06-16 stsp msg = msg0;
2432 145b6838 2022-06-16 stsp while (*msg == '\n')
2433 145b6838 2022-06-16 stsp ++msg;
2434 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2435 29688b02 2022-06-16 stsp *eol = '\0';
2436 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2437 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2438 29688b02 2022-06-16 stsp if (err)
2439 29688b02 2022-06-16 stsp goto done;
2440 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2441 145b6838 2022-06-16 stsp free(msg0);
2442 29688b02 2022-06-16 stsp free(wmsg);
2443 7ca04879 2019-10-19 stsp ncommits++;
2444 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2445 5813d178 2019-03-09 stsp }
2446 5813d178 2019-03-09 stsp
2447 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2448 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2449 867c6645 2018-07-10 stsp ncommits = 0;
2450 899d86c2 2018-05-10 stsp while (entry) {
2451 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2452 899d86c2 2018-05-10 stsp break;
2453 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2454 2814baeb 2018-08-01 stsp wstandout(view->window);
2455 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2456 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2457 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2458 2814baeb 2018-08-01 stsp wstandend(view->window);
2459 0553a4e3 2018-04-30 stsp if (err)
2460 60493ae3 2019-06-20 stsp goto done;
2461 0553a4e3 2018-04-30 stsp ncommits++;
2462 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2463 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2464 80ddbec8 2018-04-29 stsp }
2465 80ddbec8 2018-04-29 stsp
2466 9b058f45 2022-06-30 mark view_border(view);
2467 1a76625f 2018-10-22 stsp done:
2468 1a76625f 2018-10-22 stsp free(id_str);
2469 8b473291 2019-02-21 stsp free(refs_str);
2470 1a76625f 2018-10-22 stsp free(ncommits_str);
2471 1a76625f 2018-10-22 stsp free(header);
2472 80ddbec8 2018-04-29 stsp return err;
2473 9f7d7167 2018-04-29 stsp }
2474 07b55e75 2018-05-10 stsp
2475 07b55e75 2018-05-10 stsp static void
2476 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2477 07b55e75 2018-05-10 stsp {
2478 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2479 07b55e75 2018-05-10 stsp int nscrolled = 0;
2480 07b55e75 2018-05-10 stsp
2481 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2482 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2483 07b55e75 2018-05-10 stsp return;
2484 9f7d7167 2018-04-29 stsp
2485 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2486 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2487 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2488 07b55e75 2018-05-10 stsp if (entry) {
2489 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2490 07b55e75 2018-05-10 stsp nscrolled++;
2491 07b55e75 2018-05-10 stsp }
2492 07b55e75 2018-05-10 stsp }
2493 aa075928 2018-05-10 stsp }
2494 aa075928 2018-05-10 stsp
2495 aa075928 2018-05-10 stsp static const struct got_error *
2496 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2497 aa075928 2018-05-10 stsp {
2498 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2499 5e224a3e 2019-02-22 stsp int errcode;
2500 8a42fca8 2019-02-22 stsp
2501 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2502 aa075928 2018-05-10 stsp
2503 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2504 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2505 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2506 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2507 7aafa0d1 2019-02-22 stsp if (errcode)
2508 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2509 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2510 7c1452c1 2020-03-26 stsp
2511 7c1452c1 2020-03-26 stsp /*
2512 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2513 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2514 7c1452c1 2020-03-26 stsp */
2515 7c1452c1 2020-03-26 stsp if (!wait)
2516 7c1452c1 2020-03-26 stsp break;
2517 7c1452c1 2020-03-26 stsp
2518 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2519 ffe38506 2020-12-01 naddy show_log_view(view);
2520 7c1452c1 2020-03-26 stsp update_panels();
2521 7c1452c1 2020-03-26 stsp doupdate();
2522 7c1452c1 2020-03-26 stsp
2523 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2524 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2525 82954512 2020-02-03 stsp if (errcode)
2526 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2527 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2528 82954512 2020-02-03 stsp
2529 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2530 ffe38506 2020-12-01 naddy show_log_view(view);
2531 7c1452c1 2020-03-26 stsp update_panels();
2532 7c1452c1 2020-03-26 stsp doupdate();
2533 5e224a3e 2019-02-22 stsp }
2534 5e224a3e 2019-02-22 stsp
2535 5e224a3e 2019-02-22 stsp return NULL;
2536 5e224a3e 2019-02-22 stsp }
2537 5e224a3e 2019-02-22 stsp
2538 5e224a3e 2019-02-22 stsp static const struct got_error *
2539 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2540 9b058f45 2022-06-30 mark {
2541 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2542 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2543 2525dccb 2022-07-13 mark
2544 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2545 2525dccb 2022-07-13 mark return NULL;
2546 9b058f45 2022-06-30 mark
2547 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2548 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2549 9b058f45 2022-06-30 mark view->nscrolled = 0;
2550 9b058f45 2022-06-30 mark
2551 9b058f45 2022-06-30 mark return err;
2552 9b058f45 2022-06-30 mark }
2553 9b058f45 2022-06-30 mark
2554 9b058f45 2022-06-30 mark static const struct got_error *
2555 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2556 5e224a3e 2019-02-22 stsp {
2557 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2558 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2559 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2560 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2561 5e224a3e 2019-02-22 stsp
2562 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2563 5e224a3e 2019-02-22 stsp return NULL;
2564 5e224a3e 2019-02-22 stsp
2565 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2566 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2567 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2568 08ebd0a9 2019-02-22 stsp /*
2569 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2570 08ebd0a9 2019-02-22 stsp */
2571 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2572 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2573 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2574 5e224a3e 2019-02-22 stsp if (err)
2575 5e224a3e 2019-02-22 stsp return err;
2576 7aafa0d1 2019-02-22 stsp }
2577 b295e71b 2019-02-22 stsp
2578 7aafa0d1 2019-02-22 stsp do {
2579 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2580 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2581 88048b54 2019-02-21 stsp break;
2582 88048b54 2019-02-21 stsp
2583 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2584 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2585 aa075928 2018-05-10 stsp
2586 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2587 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2588 dd0a52c1 2018-05-20 stsp break;
2589 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2590 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2591 aa075928 2018-05-10 stsp
2592 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2593 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2594 9b058f45 2022-06-30 mark else
2595 9b058f45 2022-06-30 mark view->nscrolled = 0;
2596 9b058f45 2022-06-30 mark
2597 dd0a52c1 2018-05-20 stsp return err;
2598 07b55e75 2018-05-10 stsp }
2599 4a7f7875 2018-05-10 stsp
2600 cd0acaa7 2018-05-20 stsp static const struct got_error *
2601 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2602 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2603 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2604 cd0acaa7 2018-05-20 stsp {
2605 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2606 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2607 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2608 cd0acaa7 2018-05-20 stsp
2609 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2610 15a94983 2018-12-23 stsp if (diff_view == NULL)
2611 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2612 ea5e7bb5 2018-08-01 stsp
2613 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2614 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2615 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2616 e5a0f69f 2018-08-18 stsp if (err == NULL)
2617 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2618 cd0acaa7 2018-05-20 stsp return err;
2619 4a7f7875 2018-05-10 stsp }
2620 4a7f7875 2018-05-10 stsp
2621 80ddbec8 2018-04-29 stsp static const struct got_error *
2622 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2623 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2624 9343a5fb 2018-06-23 stsp {
2625 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2626 941e9f74 2019-05-21 stsp
2627 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2628 941e9f74 2019-05-21 stsp if (parent == NULL)
2629 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2630 941e9f74 2019-05-21 stsp
2631 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2632 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2633 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2634 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2635 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2636 941e9f74 2019-05-21 stsp s->tree = subtree;
2637 941e9f74 2019-05-21 stsp s->selected = 0;
2638 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2639 941e9f74 2019-05-21 stsp return NULL;
2640 941e9f74 2019-05-21 stsp }
2641 941e9f74 2019-05-21 stsp
2642 941e9f74 2019-05-21 stsp static const struct got_error *
2643 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2644 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2645 941e9f74 2019-05-21 stsp {
2646 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2647 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2648 941e9f74 2019-05-21 stsp const char *p;
2649 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2650 9343a5fb 2018-06-23 stsp
2651 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2652 941e9f74 2019-05-21 stsp p = path;
2653 941e9f74 2019-05-21 stsp while (*p) {
2654 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2655 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2656 56e0773d 2019-11-28 stsp char *te_name;
2657 33cbf02b 2020-01-12 stsp
2658 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2659 33cbf02b 2020-01-12 stsp p++;
2660 941e9f74 2019-05-21 stsp
2661 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2662 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2663 941e9f74 2019-05-21 stsp if (slash == NULL)
2664 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2665 33cbf02b 2020-01-12 stsp else
2666 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2667 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2668 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2669 56e0773d 2019-11-28 stsp break;
2670 941e9f74 2019-05-21 stsp }
2671 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2672 56e0773d 2019-11-28 stsp if (te == NULL) {
2673 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2674 56e0773d 2019-11-28 stsp free(te_name);
2675 941e9f74 2019-05-21 stsp break;
2676 941e9f74 2019-05-21 stsp }
2677 56e0773d 2019-11-28 stsp free(te_name);
2678 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2679 941e9f74 2019-05-21 stsp
2680 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2681 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2682 b03c880f 2019-05-21 stsp
2683 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2684 941e9f74 2019-05-21 stsp if (slash)
2685 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2686 941e9f74 2019-05-21 stsp else
2687 941e9f74 2019-05-21 stsp subpath = strdup(path);
2688 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2689 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2690 941e9f74 2019-05-21 stsp break;
2691 941e9f74 2019-05-21 stsp }
2692 941e9f74 2019-05-21 stsp
2693 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2694 941e9f74 2019-05-21 stsp subpath);
2695 941e9f74 2019-05-21 stsp if (err)
2696 941e9f74 2019-05-21 stsp break;
2697 941e9f74 2019-05-21 stsp
2698 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2699 941e9f74 2019-05-21 stsp free(tree_id);
2700 941e9f74 2019-05-21 stsp if (err)
2701 941e9f74 2019-05-21 stsp break;
2702 941e9f74 2019-05-21 stsp
2703 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2704 941e9f74 2019-05-21 stsp if (err) {
2705 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2706 941e9f74 2019-05-21 stsp break;
2707 941e9f74 2019-05-21 stsp }
2708 941e9f74 2019-05-21 stsp if (slash == NULL)
2709 941e9f74 2019-05-21 stsp break;
2710 941e9f74 2019-05-21 stsp free(subpath);
2711 941e9f74 2019-05-21 stsp subpath = NULL;
2712 941e9f74 2019-05-21 stsp p = slash;
2713 941e9f74 2019-05-21 stsp }
2714 941e9f74 2019-05-21 stsp
2715 941e9f74 2019-05-21 stsp free(subpath);
2716 1a76625f 2018-10-22 stsp return err;
2717 61266923 2020-01-14 stsp }
2718 61266923 2020-01-14 stsp
2719 61266923 2020-01-14 stsp static const struct got_error *
2720 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2721 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2722 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2723 55cccc34 2020-02-20 stsp {
2724 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2725 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2726 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2727 55cccc34 2020-02-20 stsp
2728 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2729 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2730 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2731 55cccc34 2020-02-20 stsp
2732 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2733 bc573f3b 2021-07-10 stsp if (err)
2734 55cccc34 2020-02-20 stsp return err;
2735 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2736 55cccc34 2020-02-20 stsp
2737 55cccc34 2020-02-20 stsp *new_view = tree_view;
2738 55cccc34 2020-02-20 stsp
2739 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2740 55cccc34 2020-02-20 stsp return NULL;
2741 55cccc34 2020-02-20 stsp
2742 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2743 55cccc34 2020-02-20 stsp }
2744 55cccc34 2020-02-20 stsp
2745 55cccc34 2020-02-20 stsp static const struct got_error *
2746 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2747 61266923 2020-01-14 stsp {
2748 61266923 2020-01-14 stsp sigset_t sigset;
2749 61266923 2020-01-14 stsp int errcode;
2750 61266923 2020-01-14 stsp
2751 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2752 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2753 61266923 2020-01-14 stsp
2754 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2755 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2756 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2757 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2758 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2759 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2760 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2761 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2762 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2763 61266923 2020-01-14 stsp
2764 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2765 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2766 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2767 61266923 2020-01-14 stsp
2768 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2769 61266923 2020-01-14 stsp if (errcode)
2770 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2771 61266923 2020-01-14 stsp
2772 61266923 2020-01-14 stsp return NULL;
2773 1a76625f 2018-10-22 stsp }
2774 1a76625f 2018-10-22 stsp
2775 1a76625f 2018-10-22 stsp static void *
2776 1a76625f 2018-10-22 stsp log_thread(void *arg)
2777 1a76625f 2018-10-22 stsp {
2778 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2779 1a76625f 2018-10-22 stsp int errcode = 0;
2780 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2781 1a76625f 2018-10-22 stsp int done = 0;
2782 61266923 2020-01-14 stsp
2783 5629093a 2022-07-11 stsp /*
2784 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2785 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2786 5629093a 2022-07-11 stsp */
2787 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2788 5629093a 2022-07-11 stsp if (errcode) {
2789 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2790 61266923 2020-01-14 stsp return (void *)err;
2791 5629093a 2022-07-11 stsp }
2792 1a76625f 2018-10-22 stsp
2793 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2794 5629093a 2022-07-11 stsp if (err) {
2795 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2796 5629093a 2022-07-11 stsp goto done;
2797 5629093a 2022-07-11 stsp }
2798 5629093a 2022-07-11 stsp
2799 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2800 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2801 5629093a 2022-07-11 stsp if (errcode) {
2802 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2803 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2804 5629093a 2022-07-11 stsp goto done;
2805 5629093a 2022-07-11 stsp }
2806 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2807 1a76625f 2018-10-22 stsp if (err) {
2808 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2809 5629093a 2022-07-11 stsp goto done;
2810 1a76625f 2018-10-22 stsp err = NULL;
2811 1a76625f 2018-10-22 stsp done = 1;
2812 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
2813 568eae95 2022-09-11 mark if (*a->limiting) {
2814 568eae95 2022-09-11 mark if (a->limit_match)
2815 568eae95 2022-09-11 mark a->commits_needed--;
2816 568eae95 2022-09-11 mark } else
2817 568eae95 2022-09-11 mark a->commits_needed--;
2818 568eae95 2022-09-11 mark }
2819 1a76625f 2018-10-22 stsp
2820 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2821 3abe8080 2019-04-10 stsp if (errcode) {
2822 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2823 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2824 5629093a 2022-07-11 stsp goto done;
2825 3abe8080 2019-04-10 stsp } else if (*a->quit)
2826 1a76625f 2018-10-22 stsp done = 1;
2827 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
2828 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2829 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
2830 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2831 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
2832 568eae95 2022-09-11 mark *a->first_displayed_entry =
2833 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
2834 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
2835 1a76625f 2018-10-22 stsp }
2836 1a76625f 2018-10-22 stsp
2837 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2838 7c1452c1 2020-03-26 stsp if (errcode) {
2839 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2840 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2841 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2842 5629093a 2022-07-11 stsp goto done;
2843 7c1452c1 2020-03-26 stsp }
2844 7c1452c1 2020-03-26 stsp
2845 1a76625f 2018-10-22 stsp if (done)
2846 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2847 7c1452c1 2020-03-26 stsp else {
2848 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2849 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2850 7c1452c1 2020-03-26 stsp &tog_mutex);
2851 5629093a 2022-07-11 stsp if (errcode) {
2852 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2853 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2854 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2855 5629093a 2022-07-11 stsp goto done;
2856 5629093a 2022-07-11 stsp }
2857 21355643 2020-12-06 stsp if (*a->quit)
2858 21355643 2020-12-06 stsp done = 1;
2859 7c1452c1 2020-03-26 stsp }
2860 1a76625f 2018-10-22 stsp }
2861 1a76625f 2018-10-22 stsp }
2862 3abe8080 2019-04-10 stsp a->log_complete = 1;
2863 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2864 5629093a 2022-07-11 stsp if (errcode)
2865 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2866 5629093a 2022-07-11 stsp done:
2867 5629093a 2022-07-11 stsp if (err) {
2868 5629093a 2022-07-11 stsp tog_thread_error = 1;
2869 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2870 5629093a 2022-07-11 stsp }
2871 1a76625f 2018-10-22 stsp return (void *)err;
2872 1a76625f 2018-10-22 stsp }
2873 1a76625f 2018-10-22 stsp
2874 1a76625f 2018-10-22 stsp static const struct got_error *
2875 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2876 1a76625f 2018-10-22 stsp {
2877 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2878 1a76625f 2018-10-22 stsp int errcode;
2879 1a76625f 2018-10-22 stsp
2880 1a76625f 2018-10-22 stsp if (s->thread) {
2881 1a76625f 2018-10-22 stsp s->quit = 1;
2882 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2883 1a76625f 2018-10-22 stsp if (errcode)
2884 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2885 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2886 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2887 1a76625f 2018-10-22 stsp if (errcode)
2888 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2889 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2890 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2891 1a76625f 2018-10-22 stsp if (errcode)
2892 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2893 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2894 1a76625f 2018-10-22 stsp if (errcode)
2895 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2896 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2897 1a76625f 2018-10-22 stsp s->thread = NULL;
2898 1a76625f 2018-10-22 stsp }
2899 1a76625f 2018-10-22 stsp
2900 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2901 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2902 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2903 74467cc8 2022-06-15 stsp }
2904 74467cc8 2022-06-15 stsp
2905 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2906 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2907 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2908 74467cc8 2022-06-15 stsp if (err == NULL)
2909 74467cc8 2022-06-15 stsp err = pack_err;
2910 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2911 1a76625f 2018-10-22 stsp }
2912 1a76625f 2018-10-22 stsp
2913 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2914 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2915 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2916 1a76625f 2018-10-22 stsp }
2917 1a76625f 2018-10-22 stsp
2918 5629093a 2022-07-11 stsp return err ? err : thread_err;
2919 9343a5fb 2018-06-23 stsp }
2920 9343a5fb 2018-06-23 stsp
2921 9343a5fb 2018-06-23 stsp static const struct got_error *
2922 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2923 1a76625f 2018-10-22 stsp {
2924 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2925 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2926 276b94a1 2020-11-13 naddy int errcode;
2927 1a76625f 2018-10-22 stsp
2928 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2929 276b94a1 2020-11-13 naddy
2930 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2931 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2932 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2933 276b94a1 2020-11-13 naddy
2934 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2935 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2936 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2937 276b94a1 2020-11-13 naddy
2938 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
2939 568eae95 2022-09-11 mark free_commits(&s->real_commits);
2940 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2941 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2942 1a76625f 2018-10-22 stsp free(s->start_id);
2943 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2944 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2945 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2946 1a76625f 2018-10-22 stsp return err;
2947 1a76625f 2018-10-22 stsp }
2948 1a76625f 2018-10-22 stsp
2949 568eae95 2022-09-11 mark /*
2950 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
2951 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
2952 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
2953 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
2954 568eae95 2022-09-11 mark * works with very slight change.
2955 568eae95 2022-09-11 mark */
2956 1a76625f 2018-10-22 stsp static const struct got_error *
2957 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
2958 568eae95 2022-09-11 mark {
2959 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
2960 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
2961 568eae95 2022-09-11 mark struct tog_view *v = view;
2962 568eae95 2022-09-11 mark const struct got_error *err = NULL;
2963 568eae95 2022-09-11 mark char pattern[1024];
2964 568eae95 2022-09-11 mark int ret;
2965 568eae95 2022-09-11 mark
2966 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
2967 568eae95 2022-09-11 mark v = view->child;
2968 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2969 568eae95 2022-09-11 mark v = view->parent;
2970 568eae95 2022-09-11 mark
2971 568eae95 2022-09-11 mark /* Get the pattern */
2972 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
2973 568eae95 2022-09-11 mark wclrtoeol(v->window);
2974 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2975 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
2976 568eae95 2022-09-11 mark nocbreak();
2977 568eae95 2022-09-11 mark echo();
2978 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
2979 568eae95 2022-09-11 mark cbreak();
2980 568eae95 2022-09-11 mark noecho();
2981 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
2982 568eae95 2022-09-11 mark if (ret == ERR)
2983 568eae95 2022-09-11 mark return NULL;
2984 568eae95 2022-09-11 mark
2985 568eae95 2022-09-11 mark if (*pattern == '\0') {
2986 568eae95 2022-09-11 mark /*
2987 568eae95 2022-09-11 mark * Safety measure for the situation where the user
2988 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
2989 568eae95 2022-09-11 mark */
2990 568eae95 2022-09-11 mark if (!s->limit_view)
2991 568eae95 2022-09-11 mark return NULL;
2992 568eae95 2022-09-11 mark
2993 568eae95 2022-09-11 mark /*
2994 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
2995 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
2996 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
2997 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
2998 568eae95 2022-09-11 mark * so we are forced to always select first entry of
2999 568eae95 2022-09-11 mark * the queue.
3000 568eae95 2022-09-11 mark */
3001 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3002 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3003 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3004 568eae95 2022-09-11 mark s->selected = 0;
3005 568eae95 2022-09-11 mark s->limit_view = 0;
3006 568eae95 2022-09-11 mark
3007 568eae95 2022-09-11 mark return NULL;
3008 568eae95 2022-09-11 mark }
3009 568eae95 2022-09-11 mark
3010 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3011 568eae95 2022-09-11 mark return NULL;
3012 568eae95 2022-09-11 mark
3013 568eae95 2022-09-11 mark s->limit_view = 1;
3014 568eae95 2022-09-11 mark
3015 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3016 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3017 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3018 568eae95 2022-09-11 mark s->selected_entry = NULL;
3019 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3020 568eae95 2022-09-11 mark
3021 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3022 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3023 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3024 568eae95 2022-09-11 mark
3025 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3026 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3027 568eae95 2022-09-11 mark int have_match = 0;
3028 568eae95 2022-09-11 mark
3029 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3030 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3031 568eae95 2022-09-11 mark if (err)
3032 568eae95 2022-09-11 mark return err;
3033 568eae95 2022-09-11 mark
3034 568eae95 2022-09-11 mark if (have_match) {
3035 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3036 568eae95 2022-09-11 mark
3037 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3038 568eae95 2022-09-11 mark entry->id);
3039 568eae95 2022-09-11 mark if (matched == NULL) {
3040 568eae95 2022-09-11 mark err = got_error_from_errno(
3041 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3042 568eae95 2022-09-11 mark break;
3043 568eae95 2022-09-11 mark }
3044 568eae95 2022-09-11 mark
3045 568eae95 2022-09-11 mark err = got_object_commit_dup(&matched->commit,
3046 568eae95 2022-09-11 mark entry->commit);
3047 568eae95 2022-09-11 mark if (err) {
3048 568eae95 2022-09-11 mark free(matched);
3049 568eae95 2022-09-11 mark return err;
3050 568eae95 2022-09-11 mark }
3051 568eae95 2022-09-11 mark
3052 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3053 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3054 568eae95 2022-09-11 mark matched, entry);
3055 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3056 568eae95 2022-09-11 mark }
3057 568eae95 2022-09-11 mark }
3058 568eae95 2022-09-11 mark
3059 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3060 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3061 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3062 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3063 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3064 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3065 568eae95 2022-09-11 mark if (err)
3066 568eae95 2022-09-11 mark return err;
3067 568eae95 2022-09-11 mark }
3068 568eae95 2022-09-11 mark
3069 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3070 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3071 568eae95 2022-09-11 mark s->selected = 0;
3072 568eae95 2022-09-11 mark
3073 568eae95 2022-09-11 mark return NULL;
3074 568eae95 2022-09-11 mark }
3075 568eae95 2022-09-11 mark
3076 568eae95 2022-09-11 mark static const struct got_error *
3077 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3078 60493ae3 2019-06-20 stsp {
3079 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3080 60493ae3 2019-06-20 stsp
3081 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3082 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3083 60493ae3 2019-06-20 stsp return NULL;
3084 60493ae3 2019-06-20 stsp }
3085 60493ae3 2019-06-20 stsp
3086 60493ae3 2019-06-20 stsp static const struct got_error *
3087 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3088 60493ae3 2019-06-20 stsp {
3089 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3090 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3091 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3092 60493ae3 2019-06-20 stsp
3093 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3094 f9686aa5 2020-03-27 stsp show_log_view(view);
3095 f9686aa5 2020-03-27 stsp update_panels();
3096 f9686aa5 2020-03-27 stsp doupdate();
3097 f9686aa5 2020-03-27 stsp
3098 96e2b566 2019-07-08 stsp if (s->search_entry) {
3099 13add988 2019-10-15 stsp int errcode, ch;
3100 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3101 13add988 2019-10-15 stsp if (errcode)
3102 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3103 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3104 13add988 2019-10-15 stsp ch = wgetch(view->window);
3105 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3106 13add988 2019-10-15 stsp if (errcode)
3107 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3108 13add988 2019-10-15 stsp "pthread_mutex_lock");
3109 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3110 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3111 678cbce5 2019-07-28 stsp return NULL;
3112 678cbce5 2019-07-28 stsp }
3113 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3114 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3115 96e2b566 2019-07-08 stsp else
3116 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3117 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3118 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3119 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
3120 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
3121 364ac6fd 2022-06-18 stsp
3122 364ac6fd 2022-06-18 stsp /*
3123 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3124 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3125 f704b35c 2022-06-18 stsp * might have changed.
3126 364ac6fd 2022-06-18 stsp */
3127 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
3128 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
3129 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
3130 364ac6fd 2022-06-18 stsp else
3131 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
3132 4bfe9f0a 2022-06-18 stsp } else {
3133 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
3134 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
3135 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
3136 364ac6fd 2022-06-18 stsp else
3137 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
3138 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
3139 4bfe9f0a 2022-06-18 stsp }
3140 20be8d96 2019-06-21 stsp } else {
3141 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3142 20be8d96 2019-06-21 stsp }
3143 60493ae3 2019-06-20 stsp
3144 60493ae3 2019-06-20 stsp while (1) {
3145 13add988 2019-10-15 stsp int have_match = 0;
3146 13add988 2019-10-15 stsp
3147 60493ae3 2019-06-20 stsp if (entry == NULL) {
3148 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3149 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3150 f9967bca 2020-03-27 stsp view->search_next_done =
3151 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3152 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3153 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3154 f801134a 2019-06-25 stsp return NULL;
3155 60493ae3 2019-06-20 stsp }
3156 96e2b566 2019-07-08 stsp /*
3157 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3158 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3159 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3160 96e2b566 2019-07-08 stsp */
3161 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3162 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3163 60493ae3 2019-06-20 stsp }
3164 60493ae3 2019-06-20 stsp
3165 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3166 13add988 2019-10-15 stsp &view->regex);
3167 5943eee2 2019-08-13 stsp if (err)
3168 13add988 2019-10-15 stsp break;
3169 13add988 2019-10-15 stsp if (have_match) {
3170 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3171 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3172 60493ae3 2019-06-20 stsp break;
3173 60493ae3 2019-06-20 stsp }
3174 13add988 2019-10-15 stsp
3175 96e2b566 2019-07-08 stsp s->search_entry = entry;
3176 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3177 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3178 b1bf1435 2019-06-21 stsp else
3179 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3180 60493ae3 2019-06-20 stsp }
3181 60493ae3 2019-06-20 stsp
3182 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3183 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3184 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3185 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3186 60493ae3 2019-06-20 stsp if (err)
3187 60493ae3 2019-06-20 stsp return err;
3188 ead14cbe 2019-06-21 stsp cur++;
3189 ead14cbe 2019-06-21 stsp }
3190 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3191 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3192 60493ae3 2019-06-20 stsp if (err)
3193 60493ae3 2019-06-20 stsp return err;
3194 ead14cbe 2019-06-21 stsp cur--;
3195 60493ae3 2019-06-20 stsp }
3196 60493ae3 2019-06-20 stsp }
3197 60493ae3 2019-06-20 stsp
3198 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3199 96e2b566 2019-07-08 stsp
3200 60493ae3 2019-06-20 stsp return NULL;
3201 60493ae3 2019-06-20 stsp }
3202 60493ae3 2019-06-20 stsp
3203 60493ae3 2019-06-20 stsp static const struct got_error *
3204 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3205 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3206 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3207 80ddbec8 2018-04-29 stsp {
3208 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3209 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3210 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3211 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3212 1a76625f 2018-10-22 stsp int errcode;
3213 80ddbec8 2018-04-29 stsp
3214 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3215 f135c941 2020-02-20 stsp free(s->in_repo_path);
3216 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3217 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3218 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3219 f135c941 2020-02-20 stsp }
3220 ecb28ae0 2018-07-16 stsp
3221 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3222 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3223 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3224 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3225 78756c87 2020-11-24 stsp
3226 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3227 568eae95 2022-09-11 mark s->limit_view = 0;
3228 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3229 568eae95 2022-09-11 mark
3230 fb2756b9 2018-08-04 stsp s->repo = repo;
3231 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3232 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3233 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3234 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3235 9cd7cbd1 2020-12-07 stsp goto done;
3236 9cd7cbd1 2020-12-07 stsp }
3237 9cd7cbd1 2020-12-07 stsp }
3238 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3239 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3240 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3241 5036bf37 2018-09-24 stsp goto done;
3242 5036bf37 2018-09-24 stsp }
3243 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3244 e5a0f69f 2018-08-18 stsp
3245 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3246 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3247 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3248 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3249 11b20872 2019-11-08 stsp if (err)
3250 11b20872 2019-11-08 stsp goto done;
3251 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3252 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3253 11b20872 2019-11-08 stsp if (err) {
3254 11b20872 2019-11-08 stsp free_colors(&s->colors);
3255 11b20872 2019-11-08 stsp goto done;
3256 11b20872 2019-11-08 stsp }
3257 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3258 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3259 11b20872 2019-11-08 stsp if (err) {
3260 11b20872 2019-11-08 stsp free_colors(&s->colors);
3261 11b20872 2019-11-08 stsp goto done;
3262 11b20872 2019-11-08 stsp }
3263 11b20872 2019-11-08 stsp }
3264 11b20872 2019-11-08 stsp
3265 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3266 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3267 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3268 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3269 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3270 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3271 1a76625f 2018-10-22 stsp
3272 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3273 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3274 74467cc8 2022-06-15 stsp if (err)
3275 74467cc8 2022-06-15 stsp goto done;
3276 74467cc8 2022-06-15 stsp }
3277 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3278 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3279 0ae84acc 2022-06-15 tracey if (err)
3280 0ae84acc 2022-06-15 tracey goto done;
3281 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3282 b672a97a 2020-01-27 stsp !s->log_branches);
3283 1a76625f 2018-10-22 stsp if (err)
3284 1a76625f 2018-10-22 stsp goto done;
3285 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3286 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3287 c5b78334 2020-01-12 stsp if (err)
3288 c5b78334 2020-01-12 stsp goto done;
3289 1a76625f 2018-10-22 stsp
3290 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3291 1a76625f 2018-10-22 stsp if (errcode) {
3292 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3293 1a76625f 2018-10-22 stsp goto done;
3294 1a76625f 2018-10-22 stsp }
3295 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3296 7c1452c1 2020-03-26 stsp if (errcode) {
3297 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3298 7c1452c1 2020-03-26 stsp goto done;
3299 7c1452c1 2020-03-26 stsp }
3300 1a76625f 2018-10-22 stsp
3301 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3302 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3303 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3304 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3305 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3306 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3307 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3308 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3309 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3310 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3311 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3312 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3313 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3314 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3315 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3316 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3317 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3318 ba4f502b 2018-08-04 stsp done:
3319 1a76625f 2018-10-22 stsp if (err)
3320 1a76625f 2018-10-22 stsp close_log_view(view);
3321 ba4f502b 2018-08-04 stsp return err;
3322 ba4f502b 2018-08-04 stsp }
3323 ba4f502b 2018-08-04 stsp
3324 e5a0f69f 2018-08-18 stsp static const struct got_error *
3325 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3326 ba4f502b 2018-08-04 stsp {
3327 f2f6d207 2020-11-24 stsp const struct got_error *err;
3328 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3329 ba4f502b 2018-08-04 stsp
3330 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3331 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3332 2b380cc8 2018-10-24 stsp &s->thread_args);
3333 2b380cc8 2018-10-24 stsp if (errcode)
3334 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3335 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3336 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3337 f2f6d207 2020-11-24 stsp if (err)
3338 f2f6d207 2020-11-24 stsp return err;
3339 f2f6d207 2020-11-24 stsp }
3340 2b380cc8 2018-10-24 stsp }
3341 2b380cc8 2018-10-24 stsp
3342 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3343 b880cc75 2022-06-30 mark }
3344 b880cc75 2022-06-30 mark
3345 b880cc75 2022-06-30 mark static void
3346 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3347 b880cc75 2022-06-30 mark {
3348 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3349 b880cc75 2022-06-30 mark
3350 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3351 b880cc75 2022-06-30 mark return;
3352 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3353 568eae95 2022-09-11 mark view->count = 0;
3354 b880cc75 2022-06-30 mark
3355 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3356 b880cc75 2022-06-30 mark || home)
3357 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3358 b880cc75 2022-06-30 mark
3359 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3360 b880cc75 2022-06-30 mark --s->selected;
3361 b880cc75 2022-06-30 mark else
3362 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3363 b880cc75 2022-06-30 mark
3364 b880cc75 2022-06-30 mark select_commit(s);
3365 b880cc75 2022-06-30 mark return;
3366 b880cc75 2022-06-30 mark }
3367 b880cc75 2022-06-30 mark
3368 b880cc75 2022-06-30 mark static const struct got_error *
3369 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3370 b880cc75 2022-06-30 mark {
3371 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3372 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3373 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3374 b880cc75 2022-06-30 mark
3375 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3376 568eae95 2022-09-11 mark return NULL;
3377 568eae95 2022-09-11 mark
3378 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3379 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3380 b880cc75 2022-06-30 mark return NULL;
3381 b880cc75 2022-06-30 mark
3382 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3383 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3384 b880cc75 2022-06-30 mark
3385 631e7531 2022-08-12 mark if (!page) {
3386 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3387 b880cc75 2022-06-30 mark ++s->selected;
3388 b880cc75 2022-06-30 mark else
3389 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3390 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3391 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3392 631e7531 2022-08-12 mark int n;
3393 631e7531 2022-08-12 mark
3394 631e7531 2022-08-12 mark s->selected = 0;
3395 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3396 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3397 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3398 631e7531 2022-08-12 mark if (entry == NULL)
3399 631e7531 2022-08-12 mark break;
3400 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3401 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3402 631e7531 2022-08-12 mark }
3403 631e7531 2022-08-12 mark if (n > 0)
3404 631e7531 2022-08-12 mark s->selected = n - 1;
3405 b880cc75 2022-06-30 mark } else {
3406 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3407 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3408 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3409 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3410 cbb0c8d7 2022-08-12 mark else
3411 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3412 b880cc75 2022-06-30 mark }
3413 b880cc75 2022-06-30 mark if (err)
3414 b880cc75 2022-06-30 mark return err;
3415 b880cc75 2022-06-30 mark
3416 9b058f45 2022-06-30 mark /*
3417 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3418 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3419 9b058f45 2022-06-30 mark */
3420 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3421 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3422 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3423 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3424 374f69dd 2022-08-13 stsp }
3425 9b058f45 2022-06-30 mark
3426 b880cc75 2022-06-30 mark select_commit(s);
3427 b880cc75 2022-06-30 mark
3428 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3429 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3430 b880cc75 2022-06-30 mark view->count = 0;
3431 b880cc75 2022-06-30 mark
3432 b880cc75 2022-06-30 mark return NULL;
3433 e5a0f69f 2018-08-18 stsp }
3434 04cc582a 2018-08-01 stsp
3435 9b058f45 2022-06-30 mark static void
3436 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3437 9b058f45 2022-06-30 mark {
3438 76364b2d 2022-07-02 mark *x = 0;
3439 76364b2d 2022-07-02 mark *y = 0;
3440 76364b2d 2022-07-02 mark
3441 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3442 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3443 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3444 7532ccda 2022-07-11 mark else if (view->resized_y)
3445 7532ccda 2022-07-11 mark *y = view->resized_y;
3446 3c1dfe12 2022-07-08 mark else
3447 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3448 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3449 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3450 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3451 7532ccda 2022-07-11 mark else if (view->resized_x)
3452 7532ccda 2022-07-11 mark *x = view->resized_x;
3453 3c1dfe12 2022-07-08 mark else
3454 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3455 3c1dfe12 2022-07-08 mark }
3456 9b058f45 2022-06-30 mark }
3457 9b058f45 2022-06-30 mark
3458 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3459 e5a0f69f 2018-08-18 stsp static const struct got_error *
3460 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3461 9b058f45 2022-06-30 mark {
3462 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3463 9b058f45 2022-06-30 mark
3464 9b058f45 2022-06-30 mark view->nlines = y;
3465 d2366e29 2022-07-07 mark view->ncols = COLS;
3466 9b058f45 2022-06-30 mark err = view_resize(view);
3467 9b058f45 2022-06-30 mark if (err)
3468 9b058f45 2022-06-30 mark return err;
3469 9b058f45 2022-06-30 mark
3470 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3471 9b058f45 2022-06-30 mark
3472 9b058f45 2022-06-30 mark return err;
3473 94b80cfa 2022-08-01 mark }
3474 94b80cfa 2022-08-01 mark
3475 94b80cfa 2022-08-01 mark static const struct got_error *
3476 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3477 94b80cfa 2022-08-01 mark {
3478 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3479 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3480 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3481 374f69dd 2022-08-13 stsp
3482 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3483 374f69dd 2022-08-13 stsp return NULL;
3484 94b80cfa 2022-08-01 mark
3485 94b80cfa 2022-08-01 mark g = view->gline;
3486 94b80cfa 2022-08-01 mark view->gline = 0;
3487 94b80cfa 2022-08-01 mark
3488 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3489 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3490 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3491 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3492 94b80cfa 2022-08-01 mark select_commit(s);
3493 94b80cfa 2022-08-01 mark return NULL;
3494 94b80cfa 2022-08-01 mark }
3495 94b80cfa 2022-08-01 mark
3496 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3497 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3498 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3499 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3500 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3501 94b80cfa 2022-08-01 mark if (err)
3502 94b80cfa 2022-08-01 mark return err;
3503 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3504 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3505 94b80cfa 2022-08-01 mark
3506 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3507 94b80cfa 2022-08-01 mark s->selected = g - 1;
3508 94b80cfa 2022-08-01 mark
3509 94b80cfa 2022-08-01 mark select_commit(s);
3510 94b80cfa 2022-08-01 mark return NULL;
3511 94b80cfa 2022-08-01 mark
3512 9b058f45 2022-06-30 mark }
3513 9b058f45 2022-06-30 mark
3514 9b058f45 2022-06-30 mark static const struct got_error *
3515 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3516 e5a0f69f 2018-08-18 stsp {
3517 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3518 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3519 631e7531 2022-08-12 mark int eos, nscroll;
3520 80ddbec8 2018-04-29 stsp
3521 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3522 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3523 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3524 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3525 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3526 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3527 fb280deb 2021-08-30 stsp }
3528 11edf34c 2022-08-12 mark if (err)
3529 11edf34c 2022-08-12 mark return err;
3530 528dedf3 2021-08-30 stsp }
3531 0dca135e 2022-06-30 mark
3532 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3533 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3534 0dca135e 2022-06-30 mark --eos; /* border */
3535 94b80cfa 2022-08-01 mark
3536 94b80cfa 2022-08-01 mark if (view->gline)
3537 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3538 0dca135e 2022-06-30 mark
3539 528dedf3 2021-08-30 stsp switch (ch) {
3540 568eae95 2022-09-11 mark case '&':
3541 568eae95 2022-09-11 mark err = limit_log_view(view);
3542 568eae95 2022-09-11 mark break;
3543 1e37a5c2 2019-05-12 jcs case 'q':
3544 1e37a5c2 2019-05-12 jcs s->quit = 1;
3545 145b6838 2022-06-16 stsp break;
3546 145b6838 2022-06-16 stsp case '0':
3547 145b6838 2022-06-16 stsp view->x = 0;
3548 145b6838 2022-06-16 stsp break;
3549 145b6838 2022-06-16 stsp case '$':
3550 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3551 640cd7ff 2022-06-22 mark view->count = 0;
3552 145b6838 2022-06-16 stsp break;
3553 145b6838 2022-06-16 stsp case KEY_RIGHT:
3554 145b6838 2022-06-16 stsp case 'l':
3555 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3556 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3557 640cd7ff 2022-06-22 mark else
3558 640cd7ff 2022-06-22 mark view->count = 0;
3559 1e37a5c2 2019-05-12 jcs break;
3560 145b6838 2022-06-16 stsp case KEY_LEFT:
3561 145b6838 2022-06-16 stsp case 'h':
3562 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3563 640cd7ff 2022-06-22 mark if (view->x <= 0)
3564 640cd7ff 2022-06-22 mark view->count = 0;
3565 145b6838 2022-06-16 stsp break;
3566 1e37a5c2 2019-05-12 jcs case 'k':
3567 1e37a5c2 2019-05-12 jcs case KEY_UP:
3568 1e37a5c2 2019-05-12 jcs case '<':
3569 1e37a5c2 2019-05-12 jcs case ',':
3570 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3571 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3572 912a3f79 2021-08-30 j break;
3573 912a3f79 2021-08-30 j case 'g':
3574 912a3f79 2021-08-30 j case KEY_HOME:
3575 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3576 640cd7ff 2022-06-22 mark view->count = 0;
3577 1e37a5c2 2019-05-12 jcs break;
3578 83cc4199 2022-06-13 stsp case CTRL('u'):
3579 33c3719a 2022-06-15 stsp case 'u':
3580 83cc4199 2022-06-13 stsp nscroll /= 2;
3581 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3582 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3583 a4292ac5 2019-05-12 jcs case CTRL('b'):
3584 61417565 2022-06-20 mark case 'b':
3585 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3586 1e37a5c2 2019-05-12 jcs break;
3587 1e37a5c2 2019-05-12 jcs case 'j':
3588 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3589 1e37a5c2 2019-05-12 jcs case '>':
3590 1e37a5c2 2019-05-12 jcs case '.':
3591 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3592 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3593 912a3f79 2021-08-30 j break;
3594 10aab77f 2022-07-19 op case '@':
3595 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3596 10aab77f 2022-07-19 op break;
3597 912a3f79 2021-08-30 j case 'G':
3598 912a3f79 2021-08-30 j case KEY_END: {
3599 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3600 912a3f79 2021-08-30 j * traverse them all. */
3601 640cd7ff 2022-06-22 mark view->count = 0;
3602 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3603 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3604 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3605 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3606 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3607 1e37a5c2 2019-05-12 jcs break;
3608 912a3f79 2021-08-30 j }
3609 80b7e8da 2022-06-11 stsp case CTRL('d'):
3610 33c3719a 2022-06-15 stsp case 'd':
3611 83cc4199 2022-06-13 stsp nscroll /= 2;
3612 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3613 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3614 61417565 2022-06-20 mark case CTRL('f'):
3615 48bb96f0 2022-06-20 naddy case 'f':
3616 b880cc75 2022-06-30 mark case ' ':
3617 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3618 1e37a5c2 2019-05-12 jcs break;
3619 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3620 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3621 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3622 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3623 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3624 2b779855 2020-12-05 naddy select_commit(s);
3625 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3626 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3627 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3628 568eae95 2022-09-11 mark s->commits->ncommits;
3629 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3630 0bf7f153 2020-12-02 naddy }
3631 1e37a5c2 2019-05-12 jcs break;
3632 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3633 49b24ee5 2022-07-03 mark case '\r':
3634 640cd7ff 2022-06-22 mark view->count = 0;
3635 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3636 e5a0f69f 2018-08-18 stsp break;
3637 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3638 1e37a5c2 2019-05-12 jcs break;
3639 5e98fb33 2022-07-22 mark case 'T':
3640 640cd7ff 2022-06-22 mark view->count = 0;
3641 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3642 5036bf37 2018-09-24 stsp break;
3643 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3644 1e37a5c2 2019-05-12 jcs break;
3645 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3646 21355643 2020-12-06 stsp case CTRL('l'):
3647 21355643 2020-12-06 stsp case 'B':
3648 640cd7ff 2022-06-22 mark view->count = 0;
3649 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3650 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3651 1e37a5c2 2019-05-12 jcs break;
3652 21355643 2020-12-06 stsp err = stop_log_thread(s);
3653 74cfe85e 2020-10-20 stsp if (err)
3654 74cfe85e 2020-10-20 stsp return err;
3655 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3656 21355643 2020-12-06 stsp char *parent_path;
3657 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3658 21355643 2020-12-06 stsp if (err)
3659 21355643 2020-12-06 stsp return err;
3660 21355643 2020-12-06 stsp free(s->in_repo_path);
3661 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3662 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3663 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3664 21355643 2020-12-06 stsp struct got_object_id *start_id;
3665 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3666 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3667 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3668 21355643 2020-12-06 stsp if (err)
3669 21355643 2020-12-06 stsp return err;
3670 21355643 2020-12-06 stsp free(s->start_id);
3671 21355643 2020-12-06 stsp s->start_id = start_id;
3672 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3673 21355643 2020-12-06 stsp } else /* 'B' */
3674 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3675 21355643 2020-12-06 stsp
3676 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3677 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3678 b0dd8d27 2022-06-16 stsp if (err)
3679 b0dd8d27 2022-06-16 stsp return err;
3680 b0dd8d27 2022-06-16 stsp }
3681 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3682 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3683 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3684 74cfe85e 2020-10-20 stsp if (err)
3685 21355643 2020-12-06 stsp return err;
3686 51a10b52 2020-12-26 stsp tog_free_refs();
3687 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3688 ca51c541 2020-12-07 stsp if (err)
3689 ca51c541 2020-12-07 stsp return err;
3690 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3691 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3692 d01904d4 2019-06-25 stsp if (err)
3693 d01904d4 2019-06-25 stsp return err;
3694 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3695 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3696 b672a97a 2020-01-27 stsp if (err)
3697 b672a97a 2020-01-27 stsp return err;
3698 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3699 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3700 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3701 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3702 21355643 2020-12-06 stsp s->selected_entry = NULL;
3703 21355643 2020-12-06 stsp s->selected = 0;
3704 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3705 21355643 2020-12-06 stsp s->quit = 0;
3706 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3707 dfee752e 2022-06-18 op s->matched_entry = NULL;
3708 dfee752e 2022-06-18 op s->search_entry = NULL;
3709 01a7bcaf 2022-07-22 mark view->offset = 0;
3710 d01904d4 2019-06-25 stsp break;
3711 5e98fb33 2022-07-22 mark case 'R':
3712 640cd7ff 2022-06-22 mark view->count = 0;
3713 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3714 6458efa5 2020-11-24 stsp break;
3715 1e37a5c2 2019-05-12 jcs default:
3716 640cd7ff 2022-06-22 mark view->count = 0;
3717 1e37a5c2 2019-05-12 jcs break;
3718 899d86c2 2018-05-10 stsp }
3719 e5a0f69f 2018-08-18 stsp
3720 80ddbec8 2018-04-29 stsp return err;
3721 80ddbec8 2018-04-29 stsp }
3722 80ddbec8 2018-04-29 stsp
3723 4ed7e80c 2018-05-20 stsp static const struct got_error *
3724 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3725 c2db6724 2019-01-04 stsp {
3726 c2db6724 2019-01-04 stsp const struct got_error *error;
3727 c2db6724 2019-01-04 stsp
3728 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3729 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3730 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3731 37c06ea4 2019-07-15 stsp #endif
3732 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3733 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3734 c2db6724 2019-01-04 stsp
3735 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3736 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3737 c2db6724 2019-01-04 stsp
3738 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3739 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3740 c2db6724 2019-01-04 stsp
3741 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3742 c2db6724 2019-01-04 stsp if (error != NULL)
3743 c2db6724 2019-01-04 stsp return error;
3744 c2db6724 2019-01-04 stsp
3745 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3746 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3747 c2db6724 2019-01-04 stsp
3748 c2db6724 2019-01-04 stsp return NULL;
3749 c2db6724 2019-01-04 stsp }
3750 c2db6724 2019-01-04 stsp
3751 a915003a 2019-02-05 stsp static void
3752 a915003a 2019-02-05 stsp init_curses(void)
3753 a915003a 2019-02-05 stsp {
3754 2497f032 2022-05-31 stsp /*
3755 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3756 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3757 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3758 2497f032 2022-05-31 stsp */
3759 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3760 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3761 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3762 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3763 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3764 2497f032 2022-05-31 stsp
3765 a915003a 2019-02-05 stsp initscr();
3766 a915003a 2019-02-05 stsp cbreak();
3767 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3768 a915003a 2019-02-05 stsp noecho();
3769 a915003a 2019-02-05 stsp nonl();
3770 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3771 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3772 a915003a 2019-02-05 stsp curs_set(0);
3773 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3774 6d17833f 2019-11-08 stsp start_color();
3775 6d17833f 2019-11-08 stsp use_default_colors();
3776 6d17833f 2019-11-08 stsp }
3777 a915003a 2019-02-05 stsp }
3778 a915003a 2019-02-05 stsp
3779 c2db6724 2019-01-04 stsp static const struct got_error *
3780 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3781 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3782 f135c941 2020-02-20 stsp {
3783 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3784 f135c941 2020-02-20 stsp
3785 f135c941 2020-02-20 stsp if (argc == 0) {
3786 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3787 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3788 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3789 f135c941 2020-02-20 stsp return NULL;
3790 f135c941 2020-02-20 stsp }
3791 f135c941 2020-02-20 stsp
3792 f135c941 2020-02-20 stsp if (worktree) {
3793 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3794 bfd61697 2020-11-14 stsp char *p;
3795 f135c941 2020-02-20 stsp
3796 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3797 f135c941 2020-02-20 stsp if (err)
3798 f135c941 2020-02-20 stsp return err;
3799 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3800 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3801 bfd61697 2020-11-14 stsp p) == -1) {
3802 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3803 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3804 f135c941 2020-02-20 stsp }
3805 f135c941 2020-02-20 stsp free(p);
3806 f135c941 2020-02-20 stsp } else
3807 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3808 f135c941 2020-02-20 stsp
3809 f135c941 2020-02-20 stsp return err;
3810 f135c941 2020-02-20 stsp }
3811 f135c941 2020-02-20 stsp
3812 f135c941 2020-02-20 stsp static const struct got_error *
3813 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3814 9f7d7167 2018-04-29 stsp {
3815 80ddbec8 2018-04-29 stsp const struct got_error *error;
3816 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3817 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3818 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3819 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3820 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3821 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3822 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3823 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3824 04cc582a 2018-08-01 stsp struct tog_view *view;
3825 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3826 80ddbec8 2018-04-29 stsp
3827 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3828 80ddbec8 2018-04-29 stsp switch (ch) {
3829 b672a97a 2020-01-27 stsp case 'b':
3830 b672a97a 2020-01-27 stsp log_branches = 1;
3831 b672a97a 2020-01-27 stsp break;
3832 80ddbec8 2018-04-29 stsp case 'c':
3833 80ddbec8 2018-04-29 stsp start_commit = optarg;
3834 80ddbec8 2018-04-29 stsp break;
3835 ecb28ae0 2018-07-16 stsp case 'r':
3836 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3837 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3838 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3839 9ba1d308 2019-10-21 stsp optarg);
3840 ecb28ae0 2018-07-16 stsp break;
3841 80ddbec8 2018-04-29 stsp default:
3842 17020d27 2019-03-07 stsp usage_log();
3843 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3844 80ddbec8 2018-04-29 stsp }
3845 80ddbec8 2018-04-29 stsp }
3846 80ddbec8 2018-04-29 stsp
3847 80ddbec8 2018-04-29 stsp argc -= optind;
3848 80ddbec8 2018-04-29 stsp argv += optind;
3849 80ddbec8 2018-04-29 stsp
3850 f135c941 2020-02-20 stsp if (argc > 1)
3851 f135c941 2020-02-20 stsp usage_log();
3852 963f97a1 2019-03-18 stsp
3853 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3854 0ae84acc 2022-06-15 tracey if (error != NULL)
3855 0ae84acc 2022-06-15 tracey goto done;
3856 0ae84acc 2022-06-15 tracey
3857 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3858 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3859 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3860 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3861 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3862 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3863 c156c7a4 2020-12-18 stsp goto done;
3864 a1fbf39a 2019-08-11 stsp if (worktree)
3865 6962eb72 2020-02-20 stsp repo_path =
3866 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3867 a1fbf39a 2019-08-11 stsp else
3868 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3869 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3870 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3871 c156c7a4 2020-12-18 stsp goto done;
3872 c156c7a4 2020-12-18 stsp }
3873 ecb28ae0 2018-07-16 stsp }
3874 ecb28ae0 2018-07-16 stsp
3875 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3876 80ddbec8 2018-04-29 stsp if (error != NULL)
3877 ecb28ae0 2018-07-16 stsp goto done;
3878 80ddbec8 2018-04-29 stsp
3879 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3880 f135c941 2020-02-20 stsp repo, worktree);
3881 f135c941 2020-02-20 stsp if (error)
3882 f135c941 2020-02-20 stsp goto done;
3883 f135c941 2020-02-20 stsp
3884 f135c941 2020-02-20 stsp init_curses();
3885 f135c941 2020-02-20 stsp
3886 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3887 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3888 c02c541e 2019-03-29 stsp if (error)
3889 c02c541e 2019-03-29 stsp goto done;
3890 c02c541e 2019-03-29 stsp
3891 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3892 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3893 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3894 87670572 2020-12-26 naddy if (error)
3895 87670572 2020-12-26 naddy goto done;
3896 87670572 2020-12-26 naddy }
3897 51a10b52 2020-12-26 stsp
3898 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3899 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3900 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3901 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3902 d8f38dc4 2020-12-05 stsp if (error)
3903 d8f38dc4 2020-12-05 stsp goto done;
3904 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3905 d8f38dc4 2020-12-05 stsp } else {
3906 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3907 d8f38dc4 2020-12-05 stsp if (error == NULL)
3908 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3909 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3910 d8f38dc4 2020-12-05 stsp goto done;
3911 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3912 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3913 d8f38dc4 2020-12-05 stsp if (error)
3914 d8f38dc4 2020-12-05 stsp goto done;
3915 d8f38dc4 2020-12-05 stsp }
3916 8b473291 2019-02-21 stsp
3917 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3918 04cc582a 2018-08-01 stsp if (view == NULL) {
3919 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3920 04cc582a 2018-08-01 stsp goto done;
3921 04cc582a 2018-08-01 stsp }
3922 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3923 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3924 ba4f502b 2018-08-04 stsp if (error)
3925 ba4f502b 2018-08-04 stsp goto done;
3926 2fc00ff4 2019-08-31 stsp if (worktree) {
3927 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3928 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3929 2fc00ff4 2019-08-31 stsp worktree = NULL;
3930 2fc00ff4 2019-08-31 stsp }
3931 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3932 ecb28ae0 2018-07-16 stsp done:
3933 f135c941 2020-02-20 stsp free(in_repo_path);
3934 ecb28ae0 2018-07-16 stsp free(repo_path);
3935 ecb28ae0 2018-07-16 stsp free(cwd);
3936 899d86c2 2018-05-10 stsp free(start_id);
3937 d8f38dc4 2020-12-05 stsp free(label);
3938 d8f38dc4 2020-12-05 stsp if (ref)
3939 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3940 1d0f4054 2021-06-17 stsp if (repo) {
3941 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3942 1d0f4054 2021-06-17 stsp if (error == NULL)
3943 1d0f4054 2021-06-17 stsp error = close_err;
3944 1d0f4054 2021-06-17 stsp }
3945 ec142235 2019-03-07 stsp if (worktree)
3946 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3947 0ae84acc 2022-06-15 tracey if (pack_fds) {
3948 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3949 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3950 0ae84acc 2022-06-15 tracey if (error == NULL)
3951 0ae84acc 2022-06-15 tracey error = pack_err;
3952 0ae84acc 2022-06-15 tracey }
3953 51a10b52 2020-12-26 stsp tog_free_refs();
3954 80ddbec8 2018-04-29 stsp return error;
3955 9f7d7167 2018-04-29 stsp }
3956 9f7d7167 2018-04-29 stsp
3957 4ed7e80c 2018-05-20 stsp __dead static void
3958 9f7d7167 2018-04-29 stsp usage_diff(void)
3959 9f7d7167 2018-04-29 stsp {
3960 80ddbec8 2018-04-29 stsp endwin();
3961 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3962 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
3963 9f7d7167 2018-04-29 stsp exit(1);
3964 b304db33 2018-05-20 stsp }
3965 b304db33 2018-05-20 stsp
3966 6d17833f 2019-11-08 stsp static int
3967 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3968 41605754 2020-11-12 stsp regmatch_t *regmatch)
3969 6d17833f 2019-11-08 stsp {
3970 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3971 6d17833f 2019-11-08 stsp }
3972 6d17833f 2019-11-08 stsp
3973 336075a4 2022-06-25 op static struct tog_color *
3974 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3975 6d17833f 2019-11-08 stsp {
3976 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3977 6d17833f 2019-11-08 stsp
3978 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3979 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3980 6d17833f 2019-11-08 stsp return tc;
3981 6d17833f 2019-11-08 stsp }
3982 6d17833f 2019-11-08 stsp
3983 6d17833f 2019-11-08 stsp return NULL;
3984 6d17833f 2019-11-08 stsp }
3985 6d17833f 2019-11-08 stsp
3986 4ed7e80c 2018-05-20 stsp static const struct got_error *
3987 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3988 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3989 41605754 2020-11-12 stsp {
3990 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3991 44a87665 2022-06-16 stsp char *exstr = NULL;
3992 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3993 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3994 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3995 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3996 41605754 2020-11-12 stsp
3997 41605754 2020-11-12 stsp *wtotal = 0;
3998 1853e0f4 2022-06-16 stsp
3999 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4000 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4001 41605754 2020-11-12 stsp
4002 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4003 44a87665 2022-06-16 stsp if (err)
4004 44a87665 2022-06-16 stsp return err;
4005 44a87665 2022-06-16 stsp
4006 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4007 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4008 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4009 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4010 44a87665 2022-06-16 stsp goto done;
4011 44a87665 2022-06-16 stsp }
4012 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4013 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4014 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4015 1853e0f4 2022-06-16 stsp goto done;
4016 1853e0f4 2022-06-16 stsp }
4017 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4018 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4019 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4020 1853e0f4 2022-06-16 stsp goto done;
4021 1853e0f4 2022-06-16 stsp }
4022 145b6838 2022-06-16 stsp
4023 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4024 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4025 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4026 1853e0f4 2022-06-16 stsp if (err)
4027 1853e0f4 2022-06-16 stsp goto done;
4028 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4029 145b6838 2022-06-16 stsp if (n) {
4030 1853e0f4 2022-06-16 stsp free(wline);
4031 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4032 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4033 1853e0f4 2022-06-16 stsp if (err)
4034 1853e0f4 2022-06-16 stsp goto done;
4035 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4036 1853e0f4 2022-06-16 stsp wlimit -= width;
4037 1853e0f4 2022-06-16 stsp *wtotal += width;
4038 41605754 2020-11-12 stsp }
4039 41605754 2020-11-12 stsp
4040 41605754 2020-11-12 stsp if (wlimit > 0) {
4041 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4042 1853e0f4 2022-06-16 stsp size_t wlen;
4043 1853e0f4 2022-06-16 stsp
4044 1853e0f4 2022-06-16 stsp free(wline);
4045 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4046 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4047 1853e0f4 2022-06-16 stsp if (err)
4048 1853e0f4 2022-06-16 stsp goto done;
4049 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4050 1853e0f4 2022-06-16 stsp while (i < wlen) {
4051 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4052 1853e0f4 2022-06-16 stsp if (width == -1) {
4053 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4054 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4055 1853e0f4 2022-06-16 stsp goto done;
4056 1853e0f4 2022-06-16 stsp }
4057 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4058 1853e0f4 2022-06-16 stsp break;
4059 61417565 2022-06-20 mark w += width;
4060 1853e0f4 2022-06-16 stsp i++;
4061 41605754 2020-11-12 stsp }
4062 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4063 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4064 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4065 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4066 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4067 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4068 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4069 41605754 2020-11-12 stsp }
4070 41605754 2020-11-12 stsp }
4071 41605754 2020-11-12 stsp
4072 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4073 1853e0f4 2022-06-16 stsp free(wline);
4074 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4075 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4076 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4077 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4078 1853e0f4 2022-06-16 stsp if (err)
4079 1853e0f4 2022-06-16 stsp goto done;
4080 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4081 1853e0f4 2022-06-16 stsp } else {
4082 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4083 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4084 1853e0f4 2022-06-16 stsp if (err)
4085 1853e0f4 2022-06-16 stsp goto done;
4086 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4087 1853e0f4 2022-06-16 stsp }
4088 1853e0f4 2022-06-16 stsp *wtotal += width2;
4089 41605754 2020-11-12 stsp }
4090 1853e0f4 2022-06-16 stsp done:
4091 145b6838 2022-06-16 stsp free(wline);
4092 44a87665 2022-06-16 stsp free(exstr);
4093 1853e0f4 2022-06-16 stsp free(seg0);
4094 1853e0f4 2022-06-16 stsp free(seg1);
4095 1853e0f4 2022-06-16 stsp free(seg2);
4096 1853e0f4 2022-06-16 stsp return err;
4097 41605754 2020-11-12 stsp }
4098 94b80cfa 2022-08-01 mark
4099 94b80cfa 2022-08-01 mark static int
4100 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4101 94b80cfa 2022-08-01 mark {
4102 94b80cfa 2022-08-01 mark FILE *f = NULL;
4103 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4104 94b80cfa 2022-08-01 mark
4105 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4106 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4107 41605754 2020-11-12 stsp
4108 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4109 94b80cfa 2022-08-01 mark selected = first;
4110 94b80cfa 2022-08-01 mark eof = &s->eof;
4111 94b80cfa 2022-08-01 mark f = s->f;
4112 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4113 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4114 94b80cfa 2022-08-01 mark
4115 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4116 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4117 94b80cfa 2022-08-01 mark eof = &s->eof;
4118 94b80cfa 2022-08-01 mark f = s->blame.f;
4119 94b80cfa 2022-08-01 mark } else
4120 94b80cfa 2022-08-01 mark return 0;
4121 94b80cfa 2022-08-01 mark
4122 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4123 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4124 94b80cfa 2022-08-01 mark return 0;
4125 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4126 94b80cfa 2022-08-01 mark rewind(f);
4127 94b80cfa 2022-08-01 mark *eof = 0;
4128 94b80cfa 2022-08-01 mark *first = 1;
4129 94b80cfa 2022-08-01 mark *lineno = 0;
4130 94b80cfa 2022-08-01 mark *nprinted = 0;
4131 94b80cfa 2022-08-01 mark return 0;
4132 94b80cfa 2022-08-01 mark }
4133 94b80cfa 2022-08-01 mark
4134 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4135 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4136 94b80cfa 2022-08-01 mark view->gline = 0;
4137 94b80cfa 2022-08-01 mark
4138 94b80cfa 2022-08-01 mark return 1;
4139 94b80cfa 2022-08-01 mark }
4140 94b80cfa 2022-08-01 mark
4141 41605754 2020-11-12 stsp static const struct got_error *
4142 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4143 26ed57b2 2018-05-19 stsp {
4144 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4145 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4146 61e69b96 2018-05-20 stsp const struct got_error *err;
4147 c7d5c43c 2022-08-04 mark int nprinted = 0;
4148 b304db33 2018-05-20 stsp char *line;
4149 826082fe 2020-12-10 stsp size_t linesize = 0;
4150 826082fe 2020-12-10 stsp ssize_t linelen;
4151 61e69b96 2018-05-20 stsp wchar_t *wline;
4152 e0b650dd 2018-05-20 stsp int width;
4153 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4154 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4155 fe621944 2020-11-10 stsp off_t line_offset;
4156 26ed57b2 2018-05-19 stsp
4157 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4158 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4159 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4160 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4161 fe621944 2020-11-10 stsp
4162 f7d12f7e 2018-08-01 stsp werase(view->window);
4163 a3404814 2018-09-02 stsp
4164 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4165 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4166 94b80cfa 2022-08-01 mark
4167 a3404814 2018-09-02 stsp if (header) {
4168 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4169 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4170 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4171 94b80cfa 2022-08-01 mark
4172 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4173 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4174 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4175 ccda2f4d 2022-06-16 stsp 0, 0);
4176 135a2da0 2020-11-11 stsp free(line);
4177 135a2da0 2020-11-11 stsp if (err)
4178 a3404814 2018-09-02 stsp return err;
4179 a3404814 2018-09-02 stsp
4180 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4181 a3404814 2018-09-02 stsp wstandout(view->window);
4182 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4183 e54cc94a 2020-11-11 stsp free(wline);
4184 e54cc94a 2020-11-11 stsp wline = NULL;
4185 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4186 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4187 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4188 a3404814 2018-09-02 stsp wstandend(view->window);
4189 26ed57b2 2018-05-19 stsp
4190 a3404814 2018-09-02 stsp if (max_lines <= 1)
4191 a3404814 2018-09-02 stsp return NULL;
4192 a3404814 2018-09-02 stsp max_lines--;
4193 a3404814 2018-09-02 stsp }
4194 a3404814 2018-09-02 stsp
4195 89f1a395 2020-12-01 naddy s->eof = 0;
4196 145b6838 2022-06-16 stsp view->maxx = 0;
4197 826082fe 2020-12-10 stsp line = NULL;
4198 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4199 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4200 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4201 6a5ff2d4 2022-08-06 mark
4202 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4203 826082fe 2020-12-10 stsp if (linelen == -1) {
4204 826082fe 2020-12-10 stsp if (feof(s->f)) {
4205 826082fe 2020-12-10 stsp s->eof = 1;
4206 826082fe 2020-12-10 stsp break;
4207 826082fe 2020-12-10 stsp }
4208 826082fe 2020-12-10 stsp free(line);
4209 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4210 61e69b96 2018-05-20 stsp }
4211 145b6838 2022-06-16 stsp
4212 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4213 94b80cfa 2022-08-01 mark continue;
4214 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4215 94b80cfa 2022-08-01 mark continue;
4216 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4217 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4218 94b80cfa 2022-08-01 mark
4219 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4220 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4221 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4222 1853e0f4 2022-06-16 stsp if (err) {
4223 1853e0f4 2022-06-16 stsp free(line);
4224 1853e0f4 2022-06-16 stsp return err;
4225 1853e0f4 2022-06-16 stsp }
4226 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4227 1853e0f4 2022-06-16 stsp free(wline);
4228 1853e0f4 2022-06-16 stsp wline = NULL;
4229 1853e0f4 2022-06-16 stsp
4230 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4231 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4232 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4233 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4234 94b80cfa 2022-08-01 mark if (attr)
4235 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4236 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4237 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4238 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4239 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4240 41605754 2020-11-12 stsp if (err) {
4241 41605754 2020-11-12 stsp free(line);
4242 41605754 2020-11-12 stsp return err;
4243 41605754 2020-11-12 stsp }
4244 41605754 2020-11-12 stsp } else {
4245 969c159c 2022-06-16 stsp int skip;
4246 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4247 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4248 969c159c 2022-06-16 stsp if (err) {
4249 969c159c 2022-06-16 stsp free(line);
4250 969c159c 2022-06-16 stsp return err;
4251 969c159c 2022-06-16 stsp }
4252 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4253 969c159c 2022-06-16 stsp free(wline);
4254 41605754 2020-11-12 stsp wline = NULL;
4255 41605754 2020-11-12 stsp }
4256 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4257 94b80cfa 2022-08-01 mark /* highlight full gline length */
4258 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4259 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4260 94b80cfa 2022-08-01 mark } else {
4261 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4262 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4263 94b80cfa 2022-08-01 mark }
4264 94b80cfa 2022-08-01 mark if (attr)
4265 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4266 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4267 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4268 826082fe 2020-12-10 stsp }
4269 826082fe 2020-12-10 stsp free(line);
4270 fe621944 2020-11-10 stsp if (nprinted >= 1)
4271 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4272 89f1a395 2020-12-01 naddy (nprinted - 1);
4273 fe621944 2020-11-10 stsp else
4274 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4275 26ed57b2 2018-05-19 stsp
4276 9b058f45 2022-06-30 mark view_border(view);
4277 c3e9aa98 2019-05-13 jcs
4278 89f1a395 2020-12-01 naddy if (s->eof) {
4279 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4280 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4281 c3e9aa98 2019-05-13 jcs nprinted++;
4282 c3e9aa98 2019-05-13 jcs }
4283 c3e9aa98 2019-05-13 jcs
4284 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4285 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4286 c3e9aa98 2019-05-13 jcs if (err) {
4287 c3e9aa98 2019-05-13 jcs return err;
4288 c3e9aa98 2019-05-13 jcs }
4289 26ed57b2 2018-05-19 stsp
4290 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4291 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4292 e54cc94a 2020-11-11 stsp free(wline);
4293 e54cc94a 2020-11-11 stsp wline = NULL;
4294 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4295 c3e9aa98 2019-05-13 jcs }
4296 c3e9aa98 2019-05-13 jcs
4297 26ed57b2 2018-05-19 stsp return NULL;
4298 abd2672a 2018-12-23 stsp }
4299 abd2672a 2018-12-23 stsp
4300 abd2672a 2018-12-23 stsp static char *
4301 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4302 abd2672a 2018-12-23 stsp {
4303 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4304 09867e48 2019-08-13 stsp char *p, *s;
4305 09867e48 2019-08-13 stsp
4306 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4307 09867e48 2019-08-13 stsp if (tm == NULL)
4308 09867e48 2019-08-13 stsp return NULL;
4309 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4310 09867e48 2019-08-13 stsp if (s == NULL)
4311 09867e48 2019-08-13 stsp return NULL;
4312 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4313 abd2672a 2018-12-23 stsp if (p)
4314 abd2672a 2018-12-23 stsp *p = '\0';
4315 abd2672a 2018-12-23 stsp return s;
4316 9f7d7167 2018-04-29 stsp }
4317 9f7d7167 2018-04-29 stsp
4318 4ed7e80c 2018-05-20 stsp static const struct got_error *
4319 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4320 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4321 0208f208 2020-05-05 stsp {
4322 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4323 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4324 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4325 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4326 0208f208 2020-05-05 stsp
4327 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4328 0208f208 2020-05-05 stsp if (qid != NULL) {
4329 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4330 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4331 d7b5a0e8 2022-04-20 stsp &qid->id);
4332 0208f208 2020-05-05 stsp if (err)
4333 0208f208 2020-05-05 stsp return err;
4334 0208f208 2020-05-05 stsp
4335 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4336 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4337 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4338 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4339 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4340 aa8b5dd0 2021-08-01 stsp }
4341 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4342 0208f208 2020-05-05 stsp
4343 0208f208 2020-05-05 stsp }
4344 0208f208 2020-05-05 stsp
4345 0208f208 2020-05-05 stsp if (tree_id1) {
4346 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4347 0208f208 2020-05-05 stsp if (err)
4348 0208f208 2020-05-05 stsp goto done;
4349 0208f208 2020-05-05 stsp }
4350 0208f208 2020-05-05 stsp
4351 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4352 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4353 0208f208 2020-05-05 stsp if (err)
4354 0208f208 2020-05-05 stsp goto done;
4355 0208f208 2020-05-05 stsp
4356 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4357 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4358 0208f208 2020-05-05 stsp done:
4359 0208f208 2020-05-05 stsp if (tree1)
4360 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4361 0208f208 2020-05-05 stsp if (tree2)
4362 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4363 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4364 0208f208 2020-05-05 stsp return err;
4365 0208f208 2020-05-05 stsp }
4366 0208f208 2020-05-05 stsp
4367 0208f208 2020-05-05 stsp static const struct got_error *
4368 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4369 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4370 abd2672a 2018-12-23 stsp {
4371 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4372 fe621944 2020-11-10 stsp
4373 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4374 fe621944 2020-11-10 stsp if (p == NULL)
4375 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4376 c7d5c43c 2022-08-04 mark *lines = p;
4377 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4378 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4379 fe621944 2020-11-10 stsp (*nlines)++;
4380 c7d5c43c 2022-08-04 mark
4381 fe621944 2020-11-10 stsp return NULL;
4382 fe621944 2020-11-10 stsp }
4383 fe621944 2020-11-10 stsp
4384 fe621944 2020-11-10 stsp static const struct got_error *
4385 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4386 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4387 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4388 fe621944 2020-11-10 stsp {
4389 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4390 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4391 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4392 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4393 45d799e2 2018-12-23 stsp time_t committer_time;
4394 45d799e2 2018-12-23 stsp const char *author, *committer;
4395 8b473291 2019-02-21 stsp char *refs_str = NULL;
4396 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4397 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4398 fe621944 2020-11-10 stsp off_t outoff = 0;
4399 fe621944 2020-11-10 stsp int n;
4400 abd2672a 2018-12-23 stsp
4401 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4402 0208f208 2020-05-05 stsp
4403 8b473291 2019-02-21 stsp if (refs) {
4404 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4405 8b473291 2019-02-21 stsp if (err)
4406 8b473291 2019-02-21 stsp return err;
4407 8b473291 2019-02-21 stsp }
4408 8b473291 2019-02-21 stsp
4409 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4410 abd2672a 2018-12-23 stsp if (err)
4411 abd2672a 2018-12-23 stsp return err;
4412 abd2672a 2018-12-23 stsp
4413 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4414 15a94983 2018-12-23 stsp if (err) {
4415 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4416 15a94983 2018-12-23 stsp goto done;
4417 15a94983 2018-12-23 stsp }
4418 abd2672a 2018-12-23 stsp
4419 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4420 fe621944 2020-11-10 stsp if (err)
4421 fe621944 2020-11-10 stsp goto done;
4422 fe621944 2020-11-10 stsp
4423 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4424 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4425 fe621944 2020-11-10 stsp if (n < 0) {
4426 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4427 abd2672a 2018-12-23 stsp goto done;
4428 abd2672a 2018-12-23 stsp }
4429 fe621944 2020-11-10 stsp outoff += n;
4430 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4431 fe621944 2020-11-10 stsp if (err)
4432 fe621944 2020-11-10 stsp goto done;
4433 fe621944 2020-11-10 stsp
4434 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4435 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4436 fe621944 2020-11-10 stsp if (n < 0) {
4437 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4438 abd2672a 2018-12-23 stsp goto done;
4439 abd2672a 2018-12-23 stsp }
4440 fe621944 2020-11-10 stsp outoff += n;
4441 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4442 fe621944 2020-11-10 stsp if (err)
4443 fe621944 2020-11-10 stsp goto done;
4444 fe621944 2020-11-10 stsp
4445 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4446 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4447 fe621944 2020-11-10 stsp if (datestr) {
4448 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4449 fe621944 2020-11-10 stsp if (n < 0) {
4450 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4451 fe621944 2020-11-10 stsp goto done;
4452 fe621944 2020-11-10 stsp }
4453 fe621944 2020-11-10 stsp outoff += n;
4454 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4455 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_DATE);
4456 fe621944 2020-11-10 stsp if (err)
4457 fe621944 2020-11-10 stsp goto done;
4458 abd2672a 2018-12-23 stsp }
4459 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4460 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4461 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4462 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4463 fe621944 2020-11-10 stsp if (n < 0) {
4464 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4465 fe621944 2020-11-10 stsp goto done;
4466 fe621944 2020-11-10 stsp }
4467 fe621944 2020-11-10 stsp outoff += n;
4468 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4469 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_AUTHOR);
4470 fe621944 2020-11-10 stsp if (err)
4471 fe621944 2020-11-10 stsp goto done;
4472 abd2672a 2018-12-23 stsp }
4473 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4474 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4475 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4476 9f98ca05 2021-09-24 stsp int pn = 1;
4477 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4478 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4479 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4480 9f98ca05 2021-09-24 stsp if (err)
4481 9f98ca05 2021-09-24 stsp goto done;
4482 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4483 9f98ca05 2021-09-24 stsp if (n < 0) {
4484 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4485 9f98ca05 2021-09-24 stsp goto done;
4486 9f98ca05 2021-09-24 stsp }
4487 9f98ca05 2021-09-24 stsp outoff += n;
4488 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4489 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4490 9f98ca05 2021-09-24 stsp if (err)
4491 9f98ca05 2021-09-24 stsp goto done;
4492 9f98ca05 2021-09-24 stsp free(id_str);
4493 9f98ca05 2021-09-24 stsp id_str = NULL;
4494 9f98ca05 2021-09-24 stsp }
4495 9f98ca05 2021-09-24 stsp }
4496 9f98ca05 2021-09-24 stsp
4497 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4498 5943eee2 2019-08-13 stsp if (err)
4499 5943eee2 2019-08-13 stsp goto done;
4500 fe621944 2020-11-10 stsp s = logmsg;
4501 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4502 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4503 fe621944 2020-11-10 stsp if (n < 0) {
4504 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4505 fe621944 2020-11-10 stsp goto done;
4506 fe621944 2020-11-10 stsp }
4507 fe621944 2020-11-10 stsp outoff += n;
4508 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4509 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4510 fe621944 2020-11-10 stsp if (err)
4511 fe621944 2020-11-10 stsp goto done;
4512 abd2672a 2018-12-23 stsp }
4513 fe621944 2020-11-10 stsp
4514 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4515 0208f208 2020-05-05 stsp if (err)
4516 0208f208 2020-05-05 stsp goto done;
4517 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4518 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4519 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4520 fe621944 2020-11-10 stsp if (n < 0) {
4521 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4522 fe621944 2020-11-10 stsp goto done;
4523 fe621944 2020-11-10 stsp }
4524 fe621944 2020-11-10 stsp outoff += n;
4525 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4526 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4527 fe621944 2020-11-10 stsp if (err)
4528 fe621944 2020-11-10 stsp goto done;
4529 0208f208 2020-05-05 stsp free((char *)pe->path);
4530 0208f208 2020-05-05 stsp free(pe->data);
4531 0208f208 2020-05-05 stsp }
4532 fe621944 2020-11-10 stsp
4533 0208f208 2020-05-05 stsp fputc('\n', outfile);
4534 fe621944 2020-11-10 stsp outoff++;
4535 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4536 abd2672a 2018-12-23 stsp done:
4537 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4538 abd2672a 2018-12-23 stsp free(id_str);
4539 5943eee2 2019-08-13 stsp free(logmsg);
4540 8b473291 2019-02-21 stsp free(refs_str);
4541 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4542 7510f233 2020-08-09 stsp if (err) {
4543 c7d5c43c 2022-08-04 mark free(*lines);
4544 c7d5c43c 2022-08-04 mark *lines = NULL;
4545 ae6a6978 2020-08-09 stsp *nlines = 0;
4546 7510f233 2020-08-09 stsp }
4547 fe621944 2020-11-10 stsp return err;
4548 abd2672a 2018-12-23 stsp }
4549 abd2672a 2018-12-23 stsp
4550 abd2672a 2018-12-23 stsp static const struct got_error *
4551 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4552 26ed57b2 2018-05-19 stsp {
4553 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4554 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4555 15a94983 2018-12-23 stsp int obj_type;
4556 fe621944 2020-11-10 stsp
4557 c7d5c43c 2022-08-04 mark free(s->lines);
4558 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4559 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4560 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4561 fe621944 2020-11-10 stsp s->nlines = 0;
4562 26ed57b2 2018-05-19 stsp
4563 511a516b 2018-05-19 stsp f = got_opentemp();
4564 48ae06ee 2018-10-18 stsp if (f == NULL) {
4565 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4566 48ae06ee 2018-10-18 stsp goto done;
4567 48ae06ee 2018-10-18 stsp }
4568 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4569 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4570 fb43ecf1 2019-02-11 stsp goto done;
4571 fb43ecf1 2019-02-11 stsp }
4572 48ae06ee 2018-10-18 stsp s->f = f;
4573 26ed57b2 2018-05-19 stsp
4574 15a94983 2018-12-23 stsp if (s->id1)
4575 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4576 15a94983 2018-12-23 stsp else
4577 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4578 15a94983 2018-12-23 stsp if (err)
4579 15a94983 2018-12-23 stsp goto done;
4580 15a94983 2018-12-23 stsp
4581 15a94983 2018-12-23 stsp switch (obj_type) {
4582 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4583 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4584 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4585 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4586 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4587 26ed57b2 2018-05-19 stsp break;
4588 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4589 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4590 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4591 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4592 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4593 26ed57b2 2018-05-19 stsp break;
4594 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4595 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4596 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4597 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4598 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4599 abd2672a 2018-12-23 stsp
4600 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4601 abd2672a 2018-12-23 stsp if (err)
4602 3ffacbe1 2020-02-02 tracey goto done;
4603 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4604 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4605 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4606 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4607 c7d5c43c 2022-08-04 mark refs, s->repo, s->f);
4608 f44b1f58 2020-02-02 tracey if (err)
4609 f44b1f58 2020-02-02 tracey goto done;
4610 f44b1f58 2020-02-02 tracey } else {
4611 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4612 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4613 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4614 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4615 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4616 c7d5c43c 2022-08-04 mark s->f);
4617 f44b1f58 2020-02-02 tracey if (err)
4618 f44b1f58 2020-02-02 tracey goto done;
4619 f5404e4e 2020-02-02 tracey break;
4620 15a087fe 2019-02-21 stsp }
4621 abd2672a 2018-12-23 stsp }
4622 abd2672a 2018-12-23 stsp }
4623 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4624 abd2672a 2018-12-23 stsp
4625 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4626 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4627 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4628 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4629 26ed57b2 2018-05-19 stsp break;
4630 abd2672a 2018-12-23 stsp }
4631 26ed57b2 2018-05-19 stsp default:
4632 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4633 48ae06ee 2018-10-18 stsp break;
4634 26ed57b2 2018-05-19 stsp }
4635 48ae06ee 2018-10-18 stsp done:
4636 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4637 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4638 48ae06ee 2018-10-18 stsp return err;
4639 48ae06ee 2018-10-18 stsp }
4640 26ed57b2 2018-05-19 stsp
4641 f5215bb9 2019-02-22 stsp static void
4642 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4643 f5215bb9 2019-02-22 stsp {
4644 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4645 f5215bb9 2019-02-22 stsp update_panels();
4646 f5215bb9 2019-02-22 stsp doupdate();
4647 f44b1f58 2020-02-02 tracey }
4648 f44b1f58 2020-02-02 tracey
4649 f44b1f58 2020-02-02 tracey static const struct got_error *
4650 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4651 f44b1f58 2020-02-02 tracey {
4652 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4653 f44b1f58 2020-02-02 tracey
4654 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4655 f44b1f58 2020-02-02 tracey return NULL;
4656 f44b1f58 2020-02-02 tracey }
4657 f44b1f58 2020-02-02 tracey
4658 f44b1f58 2020-02-02 tracey static const struct got_error *
4659 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4660 f44b1f58 2020-02-02 tracey {
4661 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4662 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4663 f44b1f58 2020-02-02 tracey int lineno;
4664 cb713507 2022-06-16 stsp char *line = NULL;
4665 826082fe 2020-12-10 stsp size_t linesize = 0;
4666 826082fe 2020-12-10 stsp ssize_t linelen;
4667 f44b1f58 2020-02-02 tracey
4668 f44b1f58 2020-02-02 tracey if (!view->searching) {
4669 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4670 f44b1f58 2020-02-02 tracey return NULL;
4671 f44b1f58 2020-02-02 tracey }
4672 f44b1f58 2020-02-02 tracey
4673 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4674 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4675 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4676 f44b1f58 2020-02-02 tracey else
4677 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4678 487cd7d2 2021-12-17 stsp } else
4679 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4680 f44b1f58 2020-02-02 tracey
4681 f44b1f58 2020-02-02 tracey while (1) {
4682 f44b1f58 2020-02-02 tracey off_t offset;
4683 f44b1f58 2020-02-02 tracey
4684 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4685 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4686 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4687 f44b1f58 2020-02-02 tracey break;
4688 f44b1f58 2020-02-02 tracey }
4689 f44b1f58 2020-02-02 tracey
4690 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4691 f44b1f58 2020-02-02 tracey lineno = 1;
4692 f44b1f58 2020-02-02 tracey else
4693 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4694 f44b1f58 2020-02-02 tracey }
4695 f44b1f58 2020-02-02 tracey
4696 c7d5c43c 2022-08-04 mark offset = s->lines[lineno - 1].offset;
4697 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4698 f44b1f58 2020-02-02 tracey free(line);
4699 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4700 f44b1f58 2020-02-02 tracey }
4701 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4702 cb713507 2022-06-16 stsp if (linelen != -1) {
4703 cb713507 2022-06-16 stsp char *exstr;
4704 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4705 cb713507 2022-06-16 stsp if (err)
4706 cb713507 2022-06-16 stsp break;
4707 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4708 cb713507 2022-06-16 stsp &view->regmatch)) {
4709 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4710 cb713507 2022-06-16 stsp s->matched_line = lineno;
4711 cb713507 2022-06-16 stsp free(exstr);
4712 cb713507 2022-06-16 stsp break;
4713 cb713507 2022-06-16 stsp }
4714 cb713507 2022-06-16 stsp free(exstr);
4715 f44b1f58 2020-02-02 tracey }
4716 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4717 f44b1f58 2020-02-02 tracey lineno++;
4718 f44b1f58 2020-02-02 tracey else
4719 f44b1f58 2020-02-02 tracey lineno--;
4720 f44b1f58 2020-02-02 tracey }
4721 826082fe 2020-12-10 stsp free(line);
4722 f44b1f58 2020-02-02 tracey
4723 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4724 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4725 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4726 f44b1f58 2020-02-02 tracey }
4727 f44b1f58 2020-02-02 tracey
4728 145b6838 2022-06-16 stsp return err;
4729 f5215bb9 2019-02-22 stsp }
4730 f5215bb9 2019-02-22 stsp
4731 48ae06ee 2018-10-18 stsp static const struct got_error *
4732 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4733 b72706c3 2022-06-01 stsp {
4734 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4735 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4736 b72706c3 2022-06-01 stsp
4737 b72706c3 2022-06-01 stsp free(s->id1);
4738 b72706c3 2022-06-01 stsp s->id1 = NULL;
4739 b72706c3 2022-06-01 stsp free(s->id2);
4740 b72706c3 2022-06-01 stsp s->id2 = NULL;
4741 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4742 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4743 b72706c3 2022-06-01 stsp s->f = NULL;
4744 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4745 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4746 b72706c3 2022-06-01 stsp s->f1 = NULL;
4747 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4748 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4749 b72706c3 2022-06-01 stsp s->f2 = NULL;
4750 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4751 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4752 f9d37699 2022-06-28 stsp s->fd1 = -1;
4753 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4754 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4755 f9d37699 2022-06-28 stsp s->fd2 = -1;
4756 c7d5c43c 2022-08-04 mark free(s->lines);
4757 c7d5c43c 2022-08-04 mark s->lines = NULL;
4758 b72706c3 2022-06-01 stsp s->nlines = 0;
4759 b72706c3 2022-06-01 stsp return err;
4760 b72706c3 2022-06-01 stsp }
4761 b72706c3 2022-06-01 stsp
4762 b72706c3 2022-06-01 stsp static const struct got_error *
4763 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4764 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4765 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4766 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4767 48ae06ee 2018-10-18 stsp {
4768 48ae06ee 2018-10-18 stsp const struct got_error *err;
4769 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4770 5dc9f4bc 2018-08-04 stsp
4771 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4772 f9d37699 2022-06-28 stsp s->fd1 = -1;
4773 f9d37699 2022-06-28 stsp s->fd2 = -1;
4774 b72706c3 2022-06-01 stsp
4775 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4776 15a94983 2018-12-23 stsp int type1, type2;
4777 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4778 15a94983 2018-12-23 stsp if (err)
4779 15a94983 2018-12-23 stsp return err;
4780 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4781 15a94983 2018-12-23 stsp if (err)
4782 15a94983 2018-12-23 stsp return err;
4783 15a94983 2018-12-23 stsp
4784 15a94983 2018-12-23 stsp if (type1 != type2)
4785 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4786 15a94983 2018-12-23 stsp }
4787 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4788 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4789 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4790 f44b1f58 2020-02-02 tracey s->repo = repo;
4791 f44b1f58 2020-02-02 tracey s->id1 = id1;
4792 f44b1f58 2020-02-02 tracey s->id2 = id2;
4793 3dbaef42 2020-11-24 stsp s->label1 = label1;
4794 3dbaef42 2020-11-24 stsp s->label2 = label2;
4795 48ae06ee 2018-10-18 stsp
4796 15a94983 2018-12-23 stsp if (id1) {
4797 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4798 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4799 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4800 48ae06ee 2018-10-18 stsp } else
4801 5465d566 2020-02-01 tracey s->id1 = NULL;
4802 48ae06ee 2018-10-18 stsp
4803 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4804 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4805 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4806 b72706c3 2022-06-01 stsp goto done;
4807 48ae06ee 2018-10-18 stsp }
4808 b72706c3 2022-06-01 stsp
4809 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4810 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4811 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4812 a00719e9 2022-06-17 stsp goto done;
4813 a00719e9 2022-06-17 stsp }
4814 a00719e9 2022-06-17 stsp
4815 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4816 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4817 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4818 b72706c3 2022-06-01 stsp goto done;
4819 b72706c3 2022-06-01 stsp }
4820 b72706c3 2022-06-01 stsp
4821 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4822 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4823 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4824 f9d37699 2022-06-28 stsp goto done;
4825 f9d37699 2022-06-28 stsp }
4826 f9d37699 2022-06-28 stsp
4827 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4828 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4829 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4830 f9d37699 2022-06-28 stsp goto done;
4831 f9d37699 2022-06-28 stsp }
4832 f9d37699 2022-06-28 stsp
4833 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4834 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4835 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4836 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4837 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4838 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4839 5465d566 2020-02-01 tracey s->repo = repo;
4840 6d17833f 2019-11-08 stsp
4841 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4842 6a5ff2d4 2022-08-06 mark int rc;
4843 6d17833f 2019-11-08 stsp
4844 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
4845 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4846 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4847 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
4848 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4849 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4850 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
4851 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4852 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4853 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
4854 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4855 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4856 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
4857 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4858 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4859 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4860 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4861 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4862 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4863 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4864 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4865 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4866 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
4867 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4868 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
4869 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
4870 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
4871 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
4872 b72706c3 2022-06-01 stsp goto done;
4873 6a5ff2d4 2022-08-06 mark }
4874 6d17833f 2019-11-08 stsp }
4875 5dc9f4bc 2018-08-04 stsp
4876 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4877 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4878 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4879 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4880 f5215bb9 2019-02-22 stsp
4881 5465d566 2020-02-01 tracey err = create_diff(s);
4882 48ae06ee 2018-10-18 stsp
4883 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4884 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4885 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4886 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4887 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4888 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4889 b72706c3 2022-06-01 stsp done:
4890 b72706c3 2022-06-01 stsp if (err)
4891 b72706c3 2022-06-01 stsp close_diff_view(view);
4892 e5a0f69f 2018-08-18 stsp return err;
4893 5dc9f4bc 2018-08-04 stsp }
4894 5dc9f4bc 2018-08-04 stsp
4895 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4896 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4897 5dc9f4bc 2018-08-04 stsp {
4898 a3404814 2018-09-02 stsp const struct got_error *err;
4899 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4900 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4901 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4902 a3404814 2018-09-02 stsp
4903 a3404814 2018-09-02 stsp if (s->id1) {
4904 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4905 a3404814 2018-09-02 stsp if (err)
4906 a3404814 2018-09-02 stsp return err;
4907 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
4908 3dbaef42 2020-11-24 stsp } else
4909 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4910 3dbaef42 2020-11-24 stsp
4911 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4912 a3404814 2018-09-02 stsp if (err)
4913 a3404814 2018-09-02 stsp return err;
4914 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
4915 26ed57b2 2018-05-19 stsp
4916 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4917 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4918 a3404814 2018-09-02 stsp free(id_str1);
4919 a3404814 2018-09-02 stsp free(id_str2);
4920 a3404814 2018-09-02 stsp return err;
4921 a3404814 2018-09-02 stsp }
4922 a3404814 2018-09-02 stsp free(id_str1);
4923 a3404814 2018-09-02 stsp free(id_str2);
4924 a3404814 2018-09-02 stsp
4925 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4926 267bb3b8 2021-08-01 stsp free(header);
4927 267bb3b8 2021-08-01 stsp return err;
4928 15a087fe 2019-02-21 stsp }
4929 15a087fe 2019-02-21 stsp
4930 15a087fe 2019-02-21 stsp static const struct got_error *
4931 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4932 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4933 15a087fe 2019-02-21 stsp {
4934 d7a04538 2019-02-21 stsp const struct got_error *err;
4935 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4936 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4937 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4938 15a087fe 2019-02-21 stsp
4939 15a087fe 2019-02-21 stsp free(s->id2);
4940 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4941 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4942 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4943 15a087fe 2019-02-21 stsp
4944 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4945 d7a04538 2019-02-21 stsp if (err)
4946 d7a04538 2019-02-21 stsp return err;
4947 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4948 15a087fe 2019-02-21 stsp free(s->id1);
4949 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4950 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4951 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4952 15a087fe 2019-02-21 stsp return NULL;
4953 0cf4efb1 2018-09-29 stsp }
4954 0cf4efb1 2018-09-29 stsp
4955 0cf4efb1 2018-09-29 stsp static const struct got_error *
4956 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4957 917d79a7 2022-07-01 stsp {
4958 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4959 917d79a7 2022-07-01 stsp
4960 917d79a7 2022-07-01 stsp view->count = 0;
4961 917d79a7 2022-07-01 stsp wclear(view->window);
4962 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4963 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4964 917d79a7 2022-07-01 stsp s->matched_line = 0;
4965 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4966 917d79a7 2022-07-01 stsp return create_diff(s);
4967 c7d5c43c 2022-08-04 mark }
4968 c7d5c43c 2022-08-04 mark
4969 c7d5c43c 2022-08-04 mark static void
4970 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4971 c7d5c43c 2022-08-04 mark {
4972 c7d5c43c 2022-08-04 mark int start, i;
4973 c7d5c43c 2022-08-04 mark
4974 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
4975 c7d5c43c 2022-08-04 mark
4976 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
4977 c7d5c43c 2022-08-04 mark if (i == 0)
4978 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
4979 c7d5c43c 2022-08-04 mark if (--i == start)
4980 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
4981 c7d5c43c 2022-08-04 mark }
4982 c7d5c43c 2022-08-04 mark
4983 c7d5c43c 2022-08-04 mark s->selected_line = 1;
4984 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
4985 917d79a7 2022-07-01 stsp }
4986 917d79a7 2022-07-01 stsp
4987 c7d5c43c 2022-08-04 mark static void
4988 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4989 c7d5c43c 2022-08-04 mark {
4990 c7d5c43c 2022-08-04 mark int start, i;
4991 c7d5c43c 2022-08-04 mark
4992 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
4993 c7d5c43c 2022-08-04 mark
4994 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
4995 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
4996 c7d5c43c 2022-08-04 mark i = 0;
4997 c7d5c43c 2022-08-04 mark if (++i == start)
4998 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
4999 c7d5c43c 2022-08-04 mark }
5000 c7d5c43c 2022-08-04 mark
5001 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5002 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5003 c7d5c43c 2022-08-04 mark }
5004 c7d5c43c 2022-08-04 mark
5005 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5006 c0f61fa4 2022-07-11 mark int, int, int);
5007 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5008 c0f61fa4 2022-07-11 mark int, int);
5009 c0f61fa4 2022-07-11 mark
5010 917d79a7 2022-07-01 stsp static const struct got_error *
5011 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5012 e5a0f69f 2018-08-18 stsp {
5013 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5014 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5015 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5016 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5017 826082fe 2020-12-10 stsp char *line = NULL;
5018 826082fe 2020-12-10 stsp size_t linesize = 0;
5019 826082fe 2020-12-10 stsp ssize_t linelen;
5020 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5021 e5a0f69f 2018-08-18 stsp
5022 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5023 c7d5c43c 2022-08-04 mark
5024 e5a0f69f 2018-08-18 stsp switch (ch) {
5025 145b6838 2022-06-16 stsp case '0':
5026 145b6838 2022-06-16 stsp view->x = 0;
5027 145b6838 2022-06-16 stsp break;
5028 145b6838 2022-06-16 stsp case '$':
5029 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5030 640cd7ff 2022-06-22 mark view->count = 0;
5031 145b6838 2022-06-16 stsp break;
5032 145b6838 2022-06-16 stsp case KEY_RIGHT:
5033 145b6838 2022-06-16 stsp case 'l':
5034 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5035 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5036 640cd7ff 2022-06-22 mark else
5037 640cd7ff 2022-06-22 mark view->count = 0;
5038 145b6838 2022-06-16 stsp break;
5039 145b6838 2022-06-16 stsp case KEY_LEFT:
5040 145b6838 2022-06-16 stsp case 'h':
5041 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5042 640cd7ff 2022-06-22 mark if (view->x <= 0)
5043 640cd7ff 2022-06-22 mark view->count = 0;
5044 145b6838 2022-06-16 stsp break;
5045 64453f7e 2020-11-21 stsp case 'a':
5046 3dbaef42 2020-11-24 stsp case 'w':
5047 3dbaef42 2020-11-24 stsp if (ch == 'a')
5048 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5049 3dbaef42 2020-11-24 stsp if (ch == 'w')
5050 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5051 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5052 912a3f79 2021-08-30 j break;
5053 912a3f79 2021-08-30 j case 'g':
5054 912a3f79 2021-08-30 j case KEY_HOME:
5055 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5056 640cd7ff 2022-06-22 mark view->count = 0;
5057 64453f7e 2020-11-21 stsp break;
5058 912a3f79 2021-08-30 j case 'G':
5059 912a3f79 2021-08-30 j case KEY_END:
5060 640cd7ff 2022-06-22 mark view->count = 0;
5061 912a3f79 2021-08-30 j if (s->eof)
5062 912a3f79 2021-08-30 j break;
5063 912a3f79 2021-08-30 j
5064 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5065 912a3f79 2021-08-30 j s->eof = 1;
5066 912a3f79 2021-08-30 j break;
5067 1e37a5c2 2019-05-12 jcs case 'k':
5068 1e37a5c2 2019-05-12 jcs case KEY_UP:
5069 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5070 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5071 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5072 640cd7ff 2022-06-22 mark else
5073 640cd7ff 2022-06-22 mark view->count = 0;
5074 1e37a5c2 2019-05-12 jcs break;
5075 83cc4199 2022-06-13 stsp case CTRL('u'):
5076 33c3719a 2022-06-15 stsp case 'u':
5077 83cc4199 2022-06-13 stsp nscroll /= 2;
5078 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5079 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5080 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5081 61417565 2022-06-20 mark case 'b':
5082 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5083 640cd7ff 2022-06-22 mark view->count = 0;
5084 26ed57b2 2018-05-19 stsp break;
5085 640cd7ff 2022-06-22 mark }
5086 1e37a5c2 2019-05-12 jcs i = 0;
5087 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5088 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5089 1e37a5c2 2019-05-12 jcs break;
5090 1e37a5c2 2019-05-12 jcs case 'j':
5091 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5092 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5093 1e37a5c2 2019-05-12 jcs if (!s->eof)
5094 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5095 640cd7ff 2022-06-22 mark else
5096 640cd7ff 2022-06-22 mark view->count = 0;
5097 1e37a5c2 2019-05-12 jcs break;
5098 83cc4199 2022-06-13 stsp case CTRL('d'):
5099 33c3719a 2022-06-15 stsp case 'd':
5100 83cc4199 2022-06-13 stsp nscroll /= 2;
5101 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5102 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5103 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5104 61417565 2022-06-20 mark case 'f':
5105 1e37a5c2 2019-05-12 jcs case ' ':
5106 640cd7ff 2022-06-22 mark if (s->eof) {
5107 640cd7ff 2022-06-22 mark view->count = 0;
5108 1e37a5c2 2019-05-12 jcs break;
5109 640cd7ff 2022-06-22 mark }
5110 1e37a5c2 2019-05-12 jcs i = 0;
5111 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5112 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5113 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5114 826082fe 2020-12-10 stsp if (linelen == -1) {
5115 826082fe 2020-12-10 stsp if (feof(s->f)) {
5116 826082fe 2020-12-10 stsp s->eof = 1;
5117 826082fe 2020-12-10 stsp } else
5118 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5119 34bc9ec9 2019-02-22 stsp break;
5120 826082fe 2020-12-10 stsp }
5121 1e37a5c2 2019-05-12 jcs }
5122 826082fe 2020-12-10 stsp free(line);
5123 1e37a5c2 2019-05-12 jcs break;
5124 c7d5c43c 2022-08-04 mark case '(':
5125 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5126 c7d5c43c 2022-08-04 mark break;
5127 c7d5c43c 2022-08-04 mark case ')':
5128 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5129 c7d5c43c 2022-08-04 mark break;
5130 c7d5c43c 2022-08-04 mark case '{':
5131 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5132 c7d5c43c 2022-08-04 mark break;
5133 c7d5c43c 2022-08-04 mark case '}':
5134 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5135 c7d5c43c 2022-08-04 mark break;
5136 1e37a5c2 2019-05-12 jcs case '[':
5137 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5138 1e37a5c2 2019-05-12 jcs s->diff_context--;
5139 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5140 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5141 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5142 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5143 27829c9e 2020-11-21 stsp s->nlines) {
5144 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5145 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5146 27829c9e 2020-11-21 stsp }
5147 640cd7ff 2022-06-22 mark } else
5148 640cd7ff 2022-06-22 mark view->count = 0;
5149 1e37a5c2 2019-05-12 jcs break;
5150 1e37a5c2 2019-05-12 jcs case ']':
5151 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5152 1e37a5c2 2019-05-12 jcs s->diff_context++;
5153 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5154 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5155 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5156 640cd7ff 2022-06-22 mark } else
5157 640cd7ff 2022-06-22 mark view->count = 0;
5158 1e37a5c2 2019-05-12 jcs break;
5159 1e37a5c2 2019-05-12 jcs case '<':
5160 1e37a5c2 2019-05-12 jcs case ',':
5161 2b3e6702 2022-07-20 mark case 'K':
5162 c0f61fa4 2022-07-11 mark up = 1;
5163 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5164 c0f61fa4 2022-07-11 mark case '>':
5165 c0f61fa4 2022-07-11 mark case '.':
5166 2b3e6702 2022-07-20 mark case 'J':
5167 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5168 640cd7ff 2022-06-22 mark view->count = 0;
5169 48ae06ee 2018-10-18 stsp break;
5170 640cd7ff 2022-06-22 mark }
5171 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5172 6524637e 2019-02-21 stsp
5173 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5174 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5175 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5176 15a087fe 2019-02-21 stsp
5177 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5178 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5179 c0f61fa4 2022-07-11 mark if (err)
5180 c0f61fa4 2022-07-11 mark break;
5181 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5182 15a087fe 2019-02-21 stsp
5183 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5184 c0f61fa4 2022-07-11 mark break;
5185 15a087fe 2019-02-21 stsp
5186 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5187 c0f61fa4 2022-07-11 mark if (err)
5188 c0f61fa4 2022-07-11 mark break;
5189 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5190 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5191 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5192 5e224a3e 2019-02-22 stsp
5193 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5194 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5195 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5196 c0f61fa4 2022-07-11 mark
5197 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5198 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5199 c0f61fa4 2022-07-11 mark if (err)
5200 c0f61fa4 2022-07-11 mark break;
5201 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5202 5a8b5076 2020-12-05 stsp
5203 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5204 c0f61fa4 2022-07-11 mark break;
5205 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5206 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5207 c0f61fa4 2022-07-11 mark bs->selected_line);
5208 c0f61fa4 2022-07-11 mark if (id == NULL)
5209 c0f61fa4 2022-07-11 mark break;
5210 15a087fe 2019-02-21 stsp
5211 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5212 c0f61fa4 2022-07-11 mark break;
5213 15a087fe 2019-02-21 stsp
5214 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5215 c0f61fa4 2022-07-11 mark if (err)
5216 c0f61fa4 2022-07-11 mark break;
5217 c0f61fa4 2022-07-11 mark }
5218 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5219 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5220 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5221 145b6838 2022-06-16 stsp view->x = 0;
5222 1e37a5c2 2019-05-12 jcs
5223 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5224 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5225 1e37a5c2 2019-05-12 jcs break;
5226 1e37a5c2 2019-05-12 jcs default:
5227 640cd7ff 2022-06-22 mark view->count = 0;
5228 1e37a5c2 2019-05-12 jcs break;
5229 26ed57b2 2018-05-19 stsp }
5230 e5a0f69f 2018-08-18 stsp
5231 bcbd79e2 2018-08-19 stsp return err;
5232 26ed57b2 2018-05-19 stsp }
5233 26ed57b2 2018-05-19 stsp
5234 4ed7e80c 2018-05-20 stsp static const struct got_error *
5235 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5236 9f7d7167 2018-04-29 stsp {
5237 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5238 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5239 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5240 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5241 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5242 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5243 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5244 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5245 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5246 3dbaef42 2020-11-24 stsp const char *errstr;
5247 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5248 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5249 70ac5f84 2019-03-28 stsp
5250 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5251 26ed57b2 2018-05-19 stsp switch (ch) {
5252 64453f7e 2020-11-21 stsp case 'a':
5253 64453f7e 2020-11-21 stsp force_text_diff = 1;
5254 3dbaef42 2020-11-24 stsp break;
5255 3dbaef42 2020-11-24 stsp case 'C':
5256 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5257 3dbaef42 2020-11-24 stsp &errstr);
5258 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5259 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5260 5a20d08d 2022-02-09 op errstr, errstr);
5261 64453f7e 2020-11-21 stsp break;
5262 09b5bff8 2020-02-23 naddy case 'r':
5263 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5264 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5265 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5266 09b5bff8 2020-02-23 naddy optarg);
5267 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5268 09b5bff8 2020-02-23 naddy break;
5269 3dbaef42 2020-11-24 stsp case 'w':
5270 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5271 3dbaef42 2020-11-24 stsp break;
5272 26ed57b2 2018-05-19 stsp default:
5273 17020d27 2019-03-07 stsp usage_diff();
5274 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5275 26ed57b2 2018-05-19 stsp }
5276 26ed57b2 2018-05-19 stsp }
5277 26ed57b2 2018-05-19 stsp
5278 26ed57b2 2018-05-19 stsp argc -= optind;
5279 26ed57b2 2018-05-19 stsp argv += optind;
5280 26ed57b2 2018-05-19 stsp
5281 26ed57b2 2018-05-19 stsp if (argc == 0) {
5282 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5283 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5284 15a94983 2018-12-23 stsp id_str1 = argv[0];
5285 15a94983 2018-12-23 stsp id_str2 = argv[1];
5286 26ed57b2 2018-05-19 stsp } else
5287 26ed57b2 2018-05-19 stsp usage_diff();
5288 eb6600df 2019-01-04 stsp
5289 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5290 0ae84acc 2022-06-15 tracey if (error)
5291 0ae84acc 2022-06-15 tracey goto done;
5292 0ae84acc 2022-06-15 tracey
5293 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5294 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5295 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5296 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5297 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5298 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5299 c156c7a4 2020-12-18 stsp goto done;
5300 a273ac94 2020-02-23 naddy if (worktree)
5301 a273ac94 2020-02-23 naddy repo_path =
5302 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5303 a273ac94 2020-02-23 naddy else
5304 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5305 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5306 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5307 c156c7a4 2020-12-18 stsp goto done;
5308 c156c7a4 2020-12-18 stsp }
5309 a273ac94 2020-02-23 naddy }
5310 a273ac94 2020-02-23 naddy
5311 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5312 eb6600df 2019-01-04 stsp if (error)
5313 eb6600df 2019-01-04 stsp goto done;
5314 26ed57b2 2018-05-19 stsp
5315 a273ac94 2020-02-23 naddy init_curses();
5316 a273ac94 2020-02-23 naddy
5317 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5318 51a10b52 2020-12-26 stsp if (error)
5319 51a10b52 2020-12-26 stsp goto done;
5320 51a10b52 2020-12-26 stsp
5321 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5322 26ed57b2 2018-05-19 stsp if (error)
5323 26ed57b2 2018-05-19 stsp goto done;
5324 26ed57b2 2018-05-19 stsp
5325 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5326 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5327 26ed57b2 2018-05-19 stsp if (error)
5328 26ed57b2 2018-05-19 stsp goto done;
5329 26ed57b2 2018-05-19 stsp
5330 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5331 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5332 26ed57b2 2018-05-19 stsp if (error)
5333 26ed57b2 2018-05-19 stsp goto done;
5334 26ed57b2 2018-05-19 stsp
5335 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5336 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5337 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5338 ea5e7bb5 2018-08-01 stsp goto done;
5339 ea5e7bb5 2018-08-01 stsp }
5340 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5341 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5342 5dc9f4bc 2018-08-04 stsp if (error)
5343 5dc9f4bc 2018-08-04 stsp goto done;
5344 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5345 26ed57b2 2018-05-19 stsp done:
5346 3dbaef42 2020-11-24 stsp free(label1);
5347 3dbaef42 2020-11-24 stsp free(label2);
5348 c02c541e 2019-03-29 stsp free(repo_path);
5349 a273ac94 2020-02-23 naddy free(cwd);
5350 1d0f4054 2021-06-17 stsp if (repo) {
5351 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5352 1d0f4054 2021-06-17 stsp if (error == NULL)
5353 1d0f4054 2021-06-17 stsp error = close_err;
5354 1d0f4054 2021-06-17 stsp }
5355 a273ac94 2020-02-23 naddy if (worktree)
5356 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5357 0ae84acc 2022-06-15 tracey if (pack_fds) {
5358 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5359 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5360 0ae84acc 2022-06-15 tracey if (error == NULL)
5361 0ae84acc 2022-06-15 tracey error = pack_err;
5362 0ae84acc 2022-06-15 tracey }
5363 51a10b52 2020-12-26 stsp tog_free_refs();
5364 26ed57b2 2018-05-19 stsp return error;
5365 9f7d7167 2018-04-29 stsp }
5366 9f7d7167 2018-04-29 stsp
5367 4ed7e80c 2018-05-20 stsp __dead static void
5368 9f7d7167 2018-04-29 stsp usage_blame(void)
5369 9f7d7167 2018-04-29 stsp {
5370 80ddbec8 2018-04-29 stsp endwin();
5371 87411fa9 2022-06-16 stsp fprintf(stderr,
5372 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5373 9f7d7167 2018-04-29 stsp getprogname());
5374 9f7d7167 2018-04-29 stsp exit(1);
5375 9f7d7167 2018-04-29 stsp }
5376 84451b3e 2018-07-10 stsp
5377 84451b3e 2018-07-10 stsp struct tog_blame_line {
5378 84451b3e 2018-07-10 stsp int annotated;
5379 84451b3e 2018-07-10 stsp struct got_object_id *id;
5380 84451b3e 2018-07-10 stsp };
5381 9f7d7167 2018-04-29 stsp
5382 4ed7e80c 2018-05-20 stsp static const struct got_error *
5383 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5384 84451b3e 2018-07-10 stsp {
5385 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5386 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5387 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5388 84451b3e 2018-07-10 stsp const struct got_error *err;
5389 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5390 826082fe 2020-12-10 stsp char *line = NULL;
5391 826082fe 2020-12-10 stsp size_t linesize = 0;
5392 826082fe 2020-12-10 stsp ssize_t linelen;
5393 84451b3e 2018-07-10 stsp wchar_t *wline;
5394 27a741e5 2019-09-11 stsp int width;
5395 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5396 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5397 ab089a2a 2018-07-12 stsp char *id_str;
5398 11b20872 2019-11-08 stsp struct tog_color *tc;
5399 ab089a2a 2018-07-12 stsp
5400 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5401 ab089a2a 2018-07-12 stsp if (err)
5402 ab089a2a 2018-07-12 stsp return err;
5403 84451b3e 2018-07-10 stsp
5404 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5405 f7d12f7e 2018-08-01 stsp werase(view->window);
5406 84451b3e 2018-07-10 stsp
5407 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5408 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5409 ab089a2a 2018-07-12 stsp free(id_str);
5410 ab089a2a 2018-07-12 stsp return err;
5411 ab089a2a 2018-07-12 stsp }
5412 ab089a2a 2018-07-12 stsp
5413 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5414 ab089a2a 2018-07-12 stsp free(line);
5415 2550e4c3 2018-07-13 stsp line = NULL;
5416 1cae65b4 2019-09-22 stsp if (err)
5417 1cae65b4 2019-09-22 stsp return err;
5418 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5419 a3404814 2018-09-02 stsp wstandout(view->window);
5420 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5421 11b20872 2019-11-08 stsp if (tc)
5422 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5423 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5424 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5425 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5426 11b20872 2019-11-08 stsp if (tc)
5427 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5428 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5429 a3404814 2018-09-02 stsp wstandend(view->window);
5430 2550e4c3 2018-07-13 stsp free(wline);
5431 2550e4c3 2018-07-13 stsp wline = NULL;
5432 ab089a2a 2018-07-12 stsp
5433 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5434 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5435 94b80cfa 2022-08-01 mark
5436 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5437 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5438 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5439 ab089a2a 2018-07-12 stsp free(id_str);
5440 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5441 ab089a2a 2018-07-12 stsp }
5442 ab089a2a 2018-07-12 stsp free(id_str);
5443 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5444 3f60a8ef 2018-07-10 stsp free(line);
5445 2550e4c3 2018-07-13 stsp line = NULL;
5446 3f60a8ef 2018-07-10 stsp if (err)
5447 3f60a8ef 2018-07-10 stsp return err;
5448 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5449 2550e4c3 2018-07-13 stsp free(wline);
5450 2550e4c3 2018-07-13 stsp wline = NULL;
5451 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5452 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5453 3f60a8ef 2018-07-10 stsp
5454 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5455 145b6838 2022-06-16 stsp view->maxx = 0;
5456 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5457 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5458 826082fe 2020-12-10 stsp if (linelen == -1) {
5459 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5460 826082fe 2020-12-10 stsp s->eof = 1;
5461 826082fe 2020-12-10 stsp break;
5462 826082fe 2020-12-10 stsp }
5463 84451b3e 2018-07-10 stsp free(line);
5464 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5465 84451b3e 2018-07-10 stsp }
5466 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5467 94b80cfa 2022-08-01 mark continue;
5468 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5469 826082fe 2020-12-10 stsp continue;
5470 1853e0f4 2022-06-16 stsp
5471 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5472 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5473 1853e0f4 2022-06-16 stsp if (err) {
5474 1853e0f4 2022-06-16 stsp free(line);
5475 1853e0f4 2022-06-16 stsp return err;
5476 1853e0f4 2022-06-16 stsp }
5477 1853e0f4 2022-06-16 stsp free(wline);
5478 1853e0f4 2022-06-16 stsp wline = NULL;
5479 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5480 84451b3e 2018-07-10 stsp
5481 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5482 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5483 b700b5d6 2018-07-10 stsp
5484 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5485 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5486 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5487 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5488 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5489 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5490 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5491 8d0fe45a 2019-08-12 stsp char *id_str;
5492 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5493 87411fa9 2022-06-16 stsp blame_line->id);
5494 8d0fe45a 2019-08-12 stsp if (err) {
5495 8d0fe45a 2019-08-12 stsp free(line);
5496 8d0fe45a 2019-08-12 stsp return err;
5497 8d0fe45a 2019-08-12 stsp }
5498 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5499 11b20872 2019-11-08 stsp if (tc)
5500 11b20872 2019-11-08 stsp wattr_on(view->window,
5501 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5502 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5503 11b20872 2019-11-08 stsp if (tc)
5504 11b20872 2019-11-08 stsp wattr_off(view->window,
5505 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5506 8d0fe45a 2019-08-12 stsp free(id_str);
5507 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5508 8d0fe45a 2019-08-12 stsp } else {
5509 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5510 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5511 84451b3e 2018-07-10 stsp }
5512 ee41ec32 2018-07-10 stsp } else {
5513 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5514 ee41ec32 2018-07-10 stsp prev_id = NULL;
5515 ee41ec32 2018-07-10 stsp }
5516 84451b3e 2018-07-10 stsp
5517 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5518 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5519 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5520 27a741e5 2019-09-11 stsp
5521 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5522 41605754 2020-11-12 stsp width = 9;
5523 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5524 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5525 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5526 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5527 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5528 41605754 2020-11-12 stsp if (err) {
5529 41605754 2020-11-12 stsp free(line);
5530 41605754 2020-11-12 stsp return err;
5531 41605754 2020-11-12 stsp }
5532 41605754 2020-11-12 stsp width += 9;
5533 41605754 2020-11-12 stsp } else {
5534 4cb9d869 2022-06-16 stsp int skip;
5535 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5536 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5537 4cb9d869 2022-06-16 stsp if (err) {
5538 4cb9d869 2022-06-16 stsp free(line);
5539 4cb9d869 2022-06-16 stsp return err;
5540 145b6838 2022-06-16 stsp }
5541 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5542 a75b3e2d 2022-06-16 stsp width += 9;
5543 41605754 2020-11-12 stsp free(wline);
5544 41605754 2020-11-12 stsp wline = NULL;
5545 41605754 2020-11-12 stsp }
5546 41605754 2020-11-12 stsp
5547 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5548 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5549 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5550 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5551 84451b3e 2018-07-10 stsp }
5552 826082fe 2020-12-10 stsp free(line);
5553 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5554 84451b3e 2018-07-10 stsp
5555 9b058f45 2022-06-30 mark view_border(view);
5556 84451b3e 2018-07-10 stsp
5557 84451b3e 2018-07-10 stsp return NULL;
5558 84451b3e 2018-07-10 stsp }
5559 84451b3e 2018-07-10 stsp
5560 84451b3e 2018-07-10 stsp static const struct got_error *
5561 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5562 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5563 84451b3e 2018-07-10 stsp {
5564 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5565 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5566 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5567 1a76625f 2018-10-22 stsp int errcode;
5568 84451b3e 2018-07-10 stsp
5569 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5570 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5571 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5572 84451b3e 2018-07-10 stsp
5573 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5574 1a76625f 2018-10-22 stsp if (errcode)
5575 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5576 84451b3e 2018-07-10 stsp
5577 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5578 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5579 d68a0a7d 2018-07-10 stsp goto done;
5580 d68a0a7d 2018-07-10 stsp }
5581 d68a0a7d 2018-07-10 stsp
5582 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5583 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5584 d68a0a7d 2018-07-10 stsp
5585 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5586 d68a0a7d 2018-07-10 stsp if (line->annotated)
5587 d68a0a7d 2018-07-10 stsp goto done;
5588 d68a0a7d 2018-07-10 stsp
5589 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5590 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5591 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5592 84451b3e 2018-07-10 stsp goto done;
5593 84451b3e 2018-07-10 stsp }
5594 84451b3e 2018-07-10 stsp line->annotated = 1;
5595 84451b3e 2018-07-10 stsp done:
5596 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5597 1a76625f 2018-10-22 stsp if (errcode)
5598 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5599 84451b3e 2018-07-10 stsp return err;
5600 84451b3e 2018-07-10 stsp }
5601 84451b3e 2018-07-10 stsp
5602 84451b3e 2018-07-10 stsp static void *
5603 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5604 84451b3e 2018-07-10 stsp {
5605 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5606 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5607 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5608 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5609 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5610 1b484788 2022-06-28 tracey
5611 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5612 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5613 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5614 e6e73e55 2022-06-30 tracey
5615 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5616 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5617 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5618 e6e73e55 2022-06-30 tracey goto done;
5619 e6e73e55 2022-06-30 tracey }
5620 18430de3 2018-07-10 stsp
5621 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5622 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5623 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5624 e6e73e55 2022-06-30 tracey goto done;
5625 e6e73e55 2022-06-30 tracey }
5626 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5627 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5628 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5629 e6e73e55 2022-06-30 tracey goto done;
5630 e6e73e55 2022-06-30 tracey }
5631 e6e73e55 2022-06-30 tracey
5632 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5633 61266923 2020-01-14 stsp if (err)
5634 e6e73e55 2022-06-30 tracey goto done;
5635 61266923 2020-01-14 stsp
5636 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5637 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5638 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5639 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5640 fc06ba56 2019-08-22 stsp err = NULL;
5641 18430de3 2018-07-10 stsp
5642 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5643 e6e73e55 2022-06-30 tracey if (errcode) {
5644 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5645 e6e73e55 2022-06-30 tracey goto done;
5646 e6e73e55 2022-06-30 tracey }
5647 18430de3 2018-07-10 stsp
5648 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5649 1d0f4054 2021-06-17 stsp if (err == NULL)
5650 1d0f4054 2021-06-17 stsp err = close_err;
5651 c9beca56 2018-07-22 stsp ta->repo = NULL;
5652 c9beca56 2018-07-22 stsp *ta->complete = 1;
5653 18430de3 2018-07-10 stsp
5654 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5655 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5656 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5657 18430de3 2018-07-10 stsp
5658 e6e73e55 2022-06-30 tracey done:
5659 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5660 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5661 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5662 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5663 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5664 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5665 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5666 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5667 1b484788 2022-06-28 tracey
5668 18430de3 2018-07-10 stsp return (void *)err;
5669 84451b3e 2018-07-10 stsp }
5670 84451b3e 2018-07-10 stsp
5671 245d91c1 2018-07-12 stsp static struct got_object_id *
5672 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5673 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5674 245d91c1 2018-07-12 stsp {
5675 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5676 8d0fe45a 2019-08-12 stsp
5677 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5678 8d0fe45a 2019-08-12 stsp return NULL;
5679 b880a918 2018-07-10 stsp
5680 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5681 c0f61fa4 2022-07-11 mark if (!line->annotated)
5682 c0f61fa4 2022-07-11 mark return NULL;
5683 c0f61fa4 2022-07-11 mark
5684 c0f61fa4 2022-07-11 mark return line->id;
5685 c0f61fa4 2022-07-11 mark }
5686 c0f61fa4 2022-07-11 mark
5687 c0f61fa4 2022-07-11 mark static struct got_object_id *
5688 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5689 c0f61fa4 2022-07-11 mark int lineno)
5690 c0f61fa4 2022-07-11 mark {
5691 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5692 c0f61fa4 2022-07-11 mark
5693 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5694 c0f61fa4 2022-07-11 mark return NULL;
5695 c0f61fa4 2022-07-11 mark
5696 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5697 245d91c1 2018-07-12 stsp if (!line->annotated)
5698 245d91c1 2018-07-12 stsp return NULL;
5699 245d91c1 2018-07-12 stsp
5700 245d91c1 2018-07-12 stsp return line->id;
5701 b880a918 2018-07-10 stsp }
5702 245d91c1 2018-07-12 stsp
5703 b880a918 2018-07-10 stsp static const struct got_error *
5704 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5705 a70480e0 2018-06-23 stsp {
5706 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5707 245d91c1 2018-07-12 stsp int i;
5708 245d91c1 2018-07-12 stsp
5709 245d91c1 2018-07-12 stsp if (blame->thread) {
5710 1a76625f 2018-10-22 stsp int errcode;
5711 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5712 1a76625f 2018-10-22 stsp if (errcode)
5713 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5714 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5715 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5716 1a76625f 2018-10-22 stsp if (errcode)
5717 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5718 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5719 1a76625f 2018-10-22 stsp if (errcode)
5720 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5721 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5722 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5723 245d91c1 2018-07-12 stsp err = NULL;
5724 245d91c1 2018-07-12 stsp blame->thread = NULL;
5725 245d91c1 2018-07-12 stsp }
5726 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5727 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5728 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5729 1d0f4054 2021-06-17 stsp if (err == NULL)
5730 1d0f4054 2021-06-17 stsp err = close_err;
5731 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5732 245d91c1 2018-07-12 stsp }
5733 245d91c1 2018-07-12 stsp if (blame->f) {
5734 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5735 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5736 245d91c1 2018-07-12 stsp blame->f = NULL;
5737 245d91c1 2018-07-12 stsp }
5738 57670559 2018-12-23 stsp if (blame->lines) {
5739 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5740 57670559 2018-12-23 stsp free(blame->lines[i].id);
5741 57670559 2018-12-23 stsp free(blame->lines);
5742 57670559 2018-12-23 stsp blame->lines = NULL;
5743 57670559 2018-12-23 stsp }
5744 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5745 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5746 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5747 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5748 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5749 0ae84acc 2022-06-15 tracey if (err == NULL)
5750 0ae84acc 2022-06-15 tracey err = pack_err;
5751 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5752 0ae84acc 2022-06-15 tracey }
5753 245d91c1 2018-07-12 stsp return err;
5754 245d91c1 2018-07-12 stsp }
5755 245d91c1 2018-07-12 stsp
5756 245d91c1 2018-07-12 stsp static const struct got_error *
5757 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5758 fc06ba56 2019-08-22 stsp {
5759 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5760 fc06ba56 2019-08-22 stsp int *done = arg;
5761 fc06ba56 2019-08-22 stsp int errcode;
5762 fc06ba56 2019-08-22 stsp
5763 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5764 fc06ba56 2019-08-22 stsp if (errcode)
5765 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5766 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5767 fc06ba56 2019-08-22 stsp
5768 fc06ba56 2019-08-22 stsp if (*done)
5769 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5770 fc06ba56 2019-08-22 stsp
5771 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5772 fc06ba56 2019-08-22 stsp if (errcode)
5773 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5774 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5775 fc06ba56 2019-08-22 stsp
5776 fc06ba56 2019-08-22 stsp return err;
5777 fc06ba56 2019-08-22 stsp }
5778 fc06ba56 2019-08-22 stsp
5779 fc06ba56 2019-08-22 stsp static const struct got_error *
5780 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5781 245d91c1 2018-07-12 stsp {
5782 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5783 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5784 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5785 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5786 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5787 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5788 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5789 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5790 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5791 a70480e0 2018-06-23 stsp
5792 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5793 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5794 27d434c2 2018-09-15 stsp if (err)
5795 15a94983 2018-12-23 stsp return err;
5796 eb81bc23 2022-06-28 tracey
5797 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5798 eb81bc23 2022-06-28 tracey if (fd == -1) {
5799 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5800 eb81bc23 2022-06-28 tracey goto done;
5801 eb81bc23 2022-06-28 tracey }
5802 a44927cc 2022-04-07 stsp
5803 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5804 a44927cc 2022-04-07 stsp if (err)
5805 a44927cc 2022-04-07 stsp goto done;
5806 27d434c2 2018-09-15 stsp
5807 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5808 84451b3e 2018-07-10 stsp if (err)
5809 84451b3e 2018-07-10 stsp goto done;
5810 27d434c2 2018-09-15 stsp
5811 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5812 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5813 84451b3e 2018-07-10 stsp goto done;
5814 84451b3e 2018-07-10 stsp }
5815 a70480e0 2018-06-23 stsp
5816 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5817 a70480e0 2018-06-23 stsp if (err)
5818 a70480e0 2018-06-23 stsp goto done;
5819 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5820 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5821 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5822 84451b3e 2018-07-10 stsp goto done;
5823 84451b3e 2018-07-10 stsp }
5824 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5825 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5826 1fddf795 2021-01-20 stsp if (err)
5827 1fddf795 2021-01-20 stsp goto done;
5828 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5829 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5830 84451b3e 2018-07-10 stsp goto done;
5831 1fddf795 2021-01-20 stsp }
5832 b02560ec 2019-08-19 stsp
5833 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5834 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5835 b02560ec 2019-08-19 stsp blame->nlines--;
5836 a70480e0 2018-06-23 stsp
5837 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5838 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5839 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5840 84451b3e 2018-07-10 stsp goto done;
5841 84451b3e 2018-07-10 stsp }
5842 a70480e0 2018-06-23 stsp
5843 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5844 bd24772e 2018-07-11 stsp if (err)
5845 bd24772e 2018-07-11 stsp goto done;
5846 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5847 0ae84acc 2022-06-15 tracey pack_fds);
5848 0ae84acc 2022-06-15 tracey if (err)
5849 0ae84acc 2022-06-15 tracey goto done;
5850 bd24772e 2018-07-11 stsp
5851 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5852 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5853 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5854 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5855 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5856 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5857 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5858 245d91c1 2018-07-12 stsp goto done;
5859 245d91c1 2018-07-12 stsp }
5860 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5861 245d91c1 2018-07-12 stsp
5862 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5863 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5864 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5865 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5866 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5867 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5868 a5388363 2020-12-01 naddy s->blame_complete = 0;
5869 f5a09613 2020-12-13 naddy
5870 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5871 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5872 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5873 f5a09613 2020-12-13 naddy s->selected_line = 1;
5874 f5a09613 2020-12-13 naddy }
5875 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5876 245d91c1 2018-07-12 stsp
5877 245d91c1 2018-07-12 stsp done:
5878 a44927cc 2022-04-07 stsp if (commit)
5879 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5880 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5881 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5882 245d91c1 2018-07-12 stsp if (blob)
5883 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5884 27d434c2 2018-09-15 stsp free(obj_id);
5885 245d91c1 2018-07-12 stsp if (err)
5886 245d91c1 2018-07-12 stsp stop_blame(blame);
5887 245d91c1 2018-07-12 stsp return err;
5888 245d91c1 2018-07-12 stsp }
5889 245d91c1 2018-07-12 stsp
5890 245d91c1 2018-07-12 stsp static const struct got_error *
5891 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5892 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5893 245d91c1 2018-07-12 stsp {
5894 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5895 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5896 dbc6a6b6 2018-07-12 stsp
5897 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5898 245d91c1 2018-07-12 stsp
5899 c4843652 2019-08-12 stsp s->path = strdup(path);
5900 c4843652 2019-08-12 stsp if (s->path == NULL)
5901 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5902 c4843652 2019-08-12 stsp
5903 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5904 c4843652 2019-08-12 stsp if (err) {
5905 c4843652 2019-08-12 stsp free(s->path);
5906 7cbe629d 2018-08-04 stsp return err;
5907 c4843652 2019-08-12 stsp }
5908 245d91c1 2018-07-12 stsp
5909 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5910 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5911 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5912 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5913 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5914 fb2756b9 2018-08-04 stsp s->repo = repo;
5915 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5916 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5917 7cbe629d 2018-08-04 stsp
5918 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5919 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5920 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5921 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5922 11b20872 2019-11-08 stsp if (err)
5923 11b20872 2019-11-08 stsp return err;
5924 11b20872 2019-11-08 stsp }
5925 11b20872 2019-11-08 stsp
5926 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5927 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5928 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5929 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5930 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5931 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5932 e5a0f69f 2018-08-18 stsp
5933 a5388363 2020-12-01 naddy return run_blame(view);
5934 7cbe629d 2018-08-04 stsp }
5935 7cbe629d 2018-08-04 stsp
5936 e5a0f69f 2018-08-18 stsp static const struct got_error *
5937 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5938 7cbe629d 2018-08-04 stsp {
5939 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5940 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5941 7cbe629d 2018-08-04 stsp
5942 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5943 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5944 e5a0f69f 2018-08-18 stsp
5945 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5946 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5947 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5948 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5949 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5950 7cbe629d 2018-08-04 stsp }
5951 e5a0f69f 2018-08-18 stsp
5952 e5a0f69f 2018-08-18 stsp free(s->path);
5953 11b20872 2019-11-08 stsp free_colors(&s->colors);
5954 e5a0f69f 2018-08-18 stsp return err;
5955 7cbe629d 2018-08-04 stsp }
5956 7cbe629d 2018-08-04 stsp
5957 7cbe629d 2018-08-04 stsp static const struct got_error *
5958 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5959 6c4c42e0 2019-06-24 stsp {
5960 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5961 6c4c42e0 2019-06-24 stsp
5962 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5963 6c4c42e0 2019-06-24 stsp return NULL;
5964 6c4c42e0 2019-06-24 stsp }
5965 6c4c42e0 2019-06-24 stsp
5966 6c4c42e0 2019-06-24 stsp static const struct got_error *
5967 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5968 6c4c42e0 2019-06-24 stsp {
5969 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5970 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5971 6c4c42e0 2019-06-24 stsp int lineno;
5972 cb713507 2022-06-16 stsp char *line = NULL;
5973 826082fe 2020-12-10 stsp size_t linesize = 0;
5974 826082fe 2020-12-10 stsp ssize_t linelen;
5975 6c4c42e0 2019-06-24 stsp
5976 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5977 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5978 6c4c42e0 2019-06-24 stsp return NULL;
5979 6c4c42e0 2019-06-24 stsp }
5980 6c4c42e0 2019-06-24 stsp
5981 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5982 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5983 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5984 6c4c42e0 2019-06-24 stsp else
5985 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5986 487cd7d2 2021-12-17 stsp } else
5987 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5988 6c4c42e0 2019-06-24 stsp
5989 6c4c42e0 2019-06-24 stsp while (1) {
5990 6c4c42e0 2019-06-24 stsp off_t offset;
5991 6c4c42e0 2019-06-24 stsp
5992 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5993 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5994 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5995 6c4c42e0 2019-06-24 stsp break;
5996 6c4c42e0 2019-06-24 stsp }
5997 2246482e 2019-06-25 stsp
5998 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5999 6c4c42e0 2019-06-24 stsp lineno = 1;
6000 6c4c42e0 2019-06-24 stsp else
6001 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
6002 6c4c42e0 2019-06-24 stsp }
6003 6c4c42e0 2019-06-24 stsp
6004 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
6005 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6006 6c4c42e0 2019-06-24 stsp free(line);
6007 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
6008 6c4c42e0 2019-06-24 stsp }
6009 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
6010 cb713507 2022-06-16 stsp if (linelen != -1) {
6011 cb713507 2022-06-16 stsp char *exstr;
6012 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
6013 cb713507 2022-06-16 stsp if (err)
6014 cb713507 2022-06-16 stsp break;
6015 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
6016 cb713507 2022-06-16 stsp &view->regmatch)) {
6017 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6018 cb713507 2022-06-16 stsp s->matched_line = lineno;
6019 cb713507 2022-06-16 stsp free(exstr);
6020 cb713507 2022-06-16 stsp break;
6021 cb713507 2022-06-16 stsp }
6022 cb713507 2022-06-16 stsp free(exstr);
6023 6c4c42e0 2019-06-24 stsp }
6024 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6025 6c4c42e0 2019-06-24 stsp lineno++;
6026 6c4c42e0 2019-06-24 stsp else
6027 6c4c42e0 2019-06-24 stsp lineno--;
6028 6c4c42e0 2019-06-24 stsp }
6029 826082fe 2020-12-10 stsp free(line);
6030 6c4c42e0 2019-06-24 stsp
6031 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
6032 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
6033 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
6034 6c4c42e0 2019-06-24 stsp }
6035 6c4c42e0 2019-06-24 stsp
6036 145b6838 2022-06-16 stsp return err;
6037 6c4c42e0 2019-06-24 stsp }
6038 6c4c42e0 2019-06-24 stsp
6039 6c4c42e0 2019-06-24 stsp static const struct got_error *
6040 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6041 7cbe629d 2018-08-04 stsp {
6042 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6043 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6044 2b380cc8 2018-10-24 stsp int errcode;
6045 2b380cc8 2018-10-24 stsp
6046 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6047 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6048 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6049 2b380cc8 2018-10-24 stsp if (errcode)
6050 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6051 51fe7530 2019-08-19 stsp
6052 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6053 2b380cc8 2018-10-24 stsp }
6054 e5a0f69f 2018-08-18 stsp
6055 51fe7530 2019-08-19 stsp if (s->blame_complete)
6056 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6057 51fe7530 2019-08-19 stsp
6058 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6059 e5a0f69f 2018-08-18 stsp
6060 9b058f45 2022-06-30 mark view_border(view);
6061 e5a0f69f 2018-08-18 stsp return err;
6062 e5a0f69f 2018-08-18 stsp }
6063 e5a0f69f 2018-08-18 stsp
6064 e5a0f69f 2018-08-18 stsp static const struct got_error *
6065 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6066 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6067 05f04cdf 2022-07-20 mark {
6068 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6069 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6070 05f04cdf 2022-07-20 mark
6071 05f04cdf 2022-07-20 mark *new_view = NULL;
6072 05f04cdf 2022-07-20 mark
6073 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6074 05f04cdf 2022-07-20 mark if (log_view == NULL)
6075 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6076 05f04cdf 2022-07-20 mark
6077 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6078 05f04cdf 2022-07-20 mark if (err)
6079 05f04cdf 2022-07-20 mark view_close(log_view);
6080 05f04cdf 2022-07-20 mark else
6081 05f04cdf 2022-07-20 mark *new_view = log_view;
6082 05f04cdf 2022-07-20 mark
6083 05f04cdf 2022-07-20 mark return err;
6084 05f04cdf 2022-07-20 mark }
6085 05f04cdf 2022-07-20 mark
6086 05f04cdf 2022-07-20 mark static const struct got_error *
6087 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6088 e5a0f69f 2018-08-18 stsp {
6089 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6090 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6091 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6092 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6093 9b058f45 2022-06-30 mark
6094 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6095 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6096 9b058f45 2022-06-30 mark --eos; /* border */
6097 7cbe629d 2018-08-04 stsp
6098 e5a0f69f 2018-08-18 stsp switch (ch) {
6099 145b6838 2022-06-16 stsp case '0':
6100 145b6838 2022-06-16 stsp view->x = 0;
6101 145b6838 2022-06-16 stsp break;
6102 145b6838 2022-06-16 stsp case '$':
6103 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6104 640cd7ff 2022-06-22 mark view->count = 0;
6105 145b6838 2022-06-16 stsp break;
6106 145b6838 2022-06-16 stsp case KEY_RIGHT:
6107 145b6838 2022-06-16 stsp case 'l':
6108 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6109 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6110 640cd7ff 2022-06-22 mark else
6111 640cd7ff 2022-06-22 mark view->count = 0;
6112 145b6838 2022-06-16 stsp break;
6113 145b6838 2022-06-16 stsp case KEY_LEFT:
6114 145b6838 2022-06-16 stsp case 'h':
6115 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6116 640cd7ff 2022-06-22 mark if (view->x <= 0)
6117 640cd7ff 2022-06-22 mark view->count = 0;
6118 145b6838 2022-06-16 stsp break;
6119 1e37a5c2 2019-05-12 jcs case 'q':
6120 1e37a5c2 2019-05-12 jcs s->done = 1;
6121 4deef56f 2021-09-02 naddy break;
6122 4deef56f 2021-09-02 naddy case 'g':
6123 4deef56f 2021-09-02 naddy case KEY_HOME:
6124 4deef56f 2021-09-02 naddy s->selected_line = 1;
6125 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6126 640cd7ff 2022-06-22 mark view->count = 0;
6127 4deef56f 2021-09-02 naddy break;
6128 4deef56f 2021-09-02 naddy case 'G':
6129 4deef56f 2021-09-02 naddy case KEY_END:
6130 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6131 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6132 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6133 4deef56f 2021-09-02 naddy } else {
6134 9b058f45 2022-06-30 mark s->selected_line = eos;
6135 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6136 4deef56f 2021-09-02 naddy }
6137 640cd7ff 2022-06-22 mark view->count = 0;
6138 1e37a5c2 2019-05-12 jcs break;
6139 1e37a5c2 2019-05-12 jcs case 'k':
6140 1e37a5c2 2019-05-12 jcs case KEY_UP:
6141 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6142 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6143 1e37a5c2 2019-05-12 jcs s->selected_line--;
6144 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6145 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6146 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6147 640cd7ff 2022-06-22 mark else
6148 640cd7ff 2022-06-22 mark view->count = 0;
6149 1e37a5c2 2019-05-12 jcs break;
6150 83cc4199 2022-06-13 stsp case CTRL('u'):
6151 33c3719a 2022-06-15 stsp case 'u':
6152 83cc4199 2022-06-13 stsp nscroll /= 2;
6153 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6154 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6155 ea025d1d 2020-02-22 naddy case CTRL('b'):
6156 61417565 2022-06-20 mark case 'b':
6157 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6158 640cd7ff 2022-06-22 mark if (view->count > 1)
6159 640cd7ff 2022-06-22 mark nscroll += nscroll;
6160 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6161 640cd7ff 2022-06-22 mark view->count = 0;
6162 e5a0f69f 2018-08-18 stsp break;
6163 1e37a5c2 2019-05-12 jcs }
6164 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6165 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6166 1e37a5c2 2019-05-12 jcs else
6167 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6168 1e37a5c2 2019-05-12 jcs break;
6169 1e37a5c2 2019-05-12 jcs case 'j':
6170 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6171 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6172 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6173 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6174 1e37a5c2 2019-05-12 jcs s->selected_line++;
6175 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6176 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6177 640cd7ff 2022-06-22 mark else
6178 640cd7ff 2022-06-22 mark view->count = 0;
6179 1e37a5c2 2019-05-12 jcs break;
6180 61417565 2022-06-20 mark case 'c':
6181 1e37a5c2 2019-05-12 jcs case 'p': {
6182 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6183 640cd7ff 2022-06-22 mark
6184 640cd7ff 2022-06-22 mark view->count = 0;
6185 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6186 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6187 1e37a5c2 2019-05-12 jcs if (id == NULL)
6188 e5a0f69f 2018-08-18 stsp break;
6189 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6190 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6191 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6192 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6193 1e37a5c2 2019-05-12 jcs int obj_type;
6194 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6195 1e37a5c2 2019-05-12 jcs s->repo, id);
6196 e5a0f69f 2018-08-18 stsp if (err)
6197 e5a0f69f 2018-08-18 stsp break;
6198 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6199 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6200 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6201 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6202 e5a0f69f 2018-08-18 stsp break;
6203 e5a0f69f 2018-08-18 stsp }
6204 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6205 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6206 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6207 a44927cc 2022-04-07 stsp if (err)
6208 a44927cc 2022-04-07 stsp break;
6209 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6210 a44927cc 2022-04-07 stsp pcommit, s->path);
6211 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6212 e5a0f69f 2018-08-18 stsp if (err) {
6213 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6214 1e37a5c2 2019-05-12 jcs err = NULL;
6215 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6216 e5a0f69f 2018-08-18 stsp break;
6217 e5a0f69f 2018-08-18 stsp }
6218 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6219 1e37a5c2 2019-05-12 jcs blob_id);
6220 1e37a5c2 2019-05-12 jcs free(blob_id);
6221 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6222 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6223 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6224 e5a0f69f 2018-08-18 stsp break;
6225 1e37a5c2 2019-05-12 jcs }
6226 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6227 d7b5a0e8 2022-04-20 stsp &pid->id);
6228 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6229 1e37a5c2 2019-05-12 jcs } else {
6230 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6231 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6232 1e37a5c2 2019-05-12 jcs break;
6233 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6234 1e37a5c2 2019-05-12 jcs id);
6235 1e37a5c2 2019-05-12 jcs }
6236 1e37a5c2 2019-05-12 jcs if (err)
6237 e5a0f69f 2018-08-18 stsp break;
6238 1e37a5c2 2019-05-12 jcs s->done = 1;
6239 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6240 1e37a5c2 2019-05-12 jcs s->done = 0;
6241 1e37a5c2 2019-05-12 jcs if (thread_err)
6242 1e37a5c2 2019-05-12 jcs break;
6243 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6244 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6245 a5388363 2020-12-01 naddy err = run_blame(view);
6246 1e37a5c2 2019-05-12 jcs if (err)
6247 1e37a5c2 2019-05-12 jcs break;
6248 1e37a5c2 2019-05-12 jcs break;
6249 1e37a5c2 2019-05-12 jcs }
6250 61417565 2022-06-20 mark case 'C': {
6251 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6252 640cd7ff 2022-06-22 mark
6253 640cd7ff 2022-06-22 mark view->count = 0;
6254 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6255 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6256 1e37a5c2 2019-05-12 jcs break;
6257 1e37a5c2 2019-05-12 jcs s->done = 1;
6258 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6259 1e37a5c2 2019-05-12 jcs s->done = 0;
6260 1e37a5c2 2019-05-12 jcs if (thread_err)
6261 1e37a5c2 2019-05-12 jcs break;
6262 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6263 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6264 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6265 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6266 a5388363 2020-12-01 naddy err = run_blame(view);
6267 1e37a5c2 2019-05-12 jcs if (err)
6268 1e37a5c2 2019-05-12 jcs break;
6269 1e37a5c2 2019-05-12 jcs break;
6270 1e37a5c2 2019-05-12 jcs }
6271 136e2bd4 2022-07-23 mark case 'L':
6272 05f04cdf 2022-07-20 mark view->count = 0;
6273 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6274 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6275 136e2bd4 2022-07-23 mark if (s->id_to_log)
6276 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6277 05f04cdf 2022-07-20 mark break;
6278 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6279 1e37a5c2 2019-05-12 jcs case '\r': {
6280 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6281 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6282 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6283 640cd7ff 2022-06-22 mark
6284 640cd7ff 2022-06-22 mark view->count = 0;
6285 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6286 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6287 1e37a5c2 2019-05-12 jcs if (id == NULL)
6288 1e37a5c2 2019-05-12 jcs break;
6289 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6290 1e37a5c2 2019-05-12 jcs if (err)
6291 1e37a5c2 2019-05-12 jcs break;
6292 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6293 c0f61fa4 2022-07-11 mark if (*new_view) {
6294 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6295 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6296 c0f61fa4 2022-07-11 mark if (err)
6297 c0f61fa4 2022-07-11 mark break;
6298 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6299 c0f61fa4 2022-07-11 mark } else {
6300 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6301 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6302 9b058f45 2022-06-30 mark
6303 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6304 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6305 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6306 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6307 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6308 c0f61fa4 2022-07-11 mark break;
6309 c0f61fa4 2022-07-11 mark }
6310 15a94983 2018-12-23 stsp }
6311 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6312 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6313 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6314 1e37a5c2 2019-05-12 jcs if (err) {
6315 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6316 1e37a5c2 2019-05-12 jcs break;
6317 1e37a5c2 2019-05-12 jcs }
6318 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6319 c0f61fa4 2022-07-11 mark s->selected_line;
6320 c0f61fa4 2022-07-11 mark if (*new_view)
6321 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6322 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6323 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6324 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6325 9b058f45 2022-06-30 mark if (err)
6326 9b058f45 2022-06-30 mark break;
6327 9b058f45 2022-06-30 mark }
6328 9b058f45 2022-06-30 mark
6329 e78dc838 2020-12-04 stsp view->focussed = 0;
6330 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6331 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6332 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6333 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6334 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6335 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6336 1e37a5c2 2019-05-12 jcs if (err)
6337 34bc9ec9 2019-02-22 stsp break;
6338 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6339 0dbbbe90 2022-06-17 op if (err)
6340 0dbbbe90 2022-06-17 op break;
6341 e78dc838 2020-12-04 stsp view->focus_child = 1;
6342 1e37a5c2 2019-05-12 jcs } else
6343 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6344 1e37a5c2 2019-05-12 jcs if (err)
6345 e5a0f69f 2018-08-18 stsp break;
6346 1e37a5c2 2019-05-12 jcs break;
6347 1e37a5c2 2019-05-12 jcs }
6348 83cc4199 2022-06-13 stsp case CTRL('d'):
6349 33c3719a 2022-06-15 stsp case 'd':
6350 83cc4199 2022-06-13 stsp nscroll /= 2;
6351 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6352 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6353 ea025d1d 2020-02-22 naddy case CTRL('f'):
6354 61417565 2022-06-20 mark case 'f':
6355 1e37a5c2 2019-05-12 jcs case ' ':
6356 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6357 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6358 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6359 640cd7ff 2022-06-22 mark view->count = 0;
6360 e5a0f69f 2018-08-18 stsp break;
6361 1e37a5c2 2019-05-12 jcs }
6362 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6363 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6364 83cc4199 2022-06-13 stsp s->selected_line +=
6365 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6366 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6367 1e37a5c2 2019-05-12 jcs }
6368 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6369 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6370 1e37a5c2 2019-05-12 jcs else
6371 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6372 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6373 1e37a5c2 2019-05-12 jcs break;
6374 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6375 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6376 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6377 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6378 1e37a5c2 2019-05-12 jcs }
6379 1e37a5c2 2019-05-12 jcs break;
6380 1e37a5c2 2019-05-12 jcs default:
6381 640cd7ff 2022-06-22 mark view->count = 0;
6382 1e37a5c2 2019-05-12 jcs break;
6383 a70480e0 2018-06-23 stsp }
6384 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6385 917d79a7 2022-07-01 stsp }
6386 917d79a7 2022-07-01 stsp
6387 917d79a7 2022-07-01 stsp static const struct got_error *
6388 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6389 917d79a7 2022-07-01 stsp {
6390 917d79a7 2022-07-01 stsp const struct got_error *err;
6391 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6392 917d79a7 2022-07-01 stsp
6393 917d79a7 2022-07-01 stsp view->count = 0;
6394 917d79a7 2022-07-01 stsp s->done = 1;
6395 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6396 917d79a7 2022-07-01 stsp s->done = 0;
6397 917d79a7 2022-07-01 stsp if (err)
6398 917d79a7 2022-07-01 stsp return err;
6399 917d79a7 2022-07-01 stsp return run_blame(view);
6400 a70480e0 2018-06-23 stsp }
6401 a70480e0 2018-06-23 stsp
6402 a70480e0 2018-06-23 stsp static const struct got_error *
6403 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6404 9f7d7167 2018-04-29 stsp {
6405 a70480e0 2018-06-23 stsp const struct got_error *error;
6406 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6407 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6408 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6409 0587e10c 2020-07-23 stsp char *link_target = NULL;
6410 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6411 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6412 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6413 a70480e0 2018-06-23 stsp int ch;
6414 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6415 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6416 a70480e0 2018-06-23 stsp
6417 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6418 a70480e0 2018-06-23 stsp switch (ch) {
6419 a70480e0 2018-06-23 stsp case 'c':
6420 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6421 a70480e0 2018-06-23 stsp break;
6422 69069811 2018-08-02 stsp case 'r':
6423 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6424 69069811 2018-08-02 stsp if (repo_path == NULL)
6425 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6426 9ba1d308 2019-10-21 stsp optarg);
6427 69069811 2018-08-02 stsp break;
6428 a70480e0 2018-06-23 stsp default:
6429 17020d27 2019-03-07 stsp usage_blame();
6430 a70480e0 2018-06-23 stsp /* NOTREACHED */
6431 a70480e0 2018-06-23 stsp }
6432 a70480e0 2018-06-23 stsp }
6433 a70480e0 2018-06-23 stsp
6434 a70480e0 2018-06-23 stsp argc -= optind;
6435 a70480e0 2018-06-23 stsp argv += optind;
6436 a70480e0 2018-06-23 stsp
6437 f135c941 2020-02-20 stsp if (argc != 1)
6438 a70480e0 2018-06-23 stsp usage_blame();
6439 6962eb72 2020-02-20 stsp
6440 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6441 0ae84acc 2022-06-15 tracey if (error != NULL)
6442 0ae84acc 2022-06-15 tracey goto done;
6443 0ae84acc 2022-06-15 tracey
6444 69069811 2018-08-02 stsp if (repo_path == NULL) {
6445 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6446 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6447 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6448 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6449 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6450 c156c7a4 2020-12-18 stsp goto done;
6451 f135c941 2020-02-20 stsp if (worktree)
6452 eb41ed75 2019-02-05 stsp repo_path =
6453 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6454 f135c941 2020-02-20 stsp else
6455 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6456 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6457 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6458 c156c7a4 2020-12-18 stsp goto done;
6459 c156c7a4 2020-12-18 stsp }
6460 f135c941 2020-02-20 stsp }
6461 a915003a 2019-02-05 stsp
6462 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6463 c02c541e 2019-03-29 stsp if (error != NULL)
6464 8e94dd5b 2019-01-04 stsp goto done;
6465 69069811 2018-08-02 stsp
6466 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6467 f135c941 2020-02-20 stsp worktree);
6468 c02c541e 2019-03-29 stsp if (error)
6469 92205607 2019-01-04 stsp goto done;
6470 69069811 2018-08-02 stsp
6471 f135c941 2020-02-20 stsp init_curses();
6472 f135c941 2020-02-20 stsp
6473 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6474 51a10b52 2020-12-26 stsp if (error)
6475 51a10b52 2020-12-26 stsp goto done;
6476 51a10b52 2020-12-26 stsp
6477 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6478 eb41ed75 2019-02-05 stsp if (error)
6479 69069811 2018-08-02 stsp goto done;
6480 a70480e0 2018-06-23 stsp
6481 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6482 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6483 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6484 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6485 a70480e0 2018-06-23 stsp if (error != NULL)
6486 66b4983c 2018-06-23 stsp goto done;
6487 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6488 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6489 a70480e0 2018-06-23 stsp } else {
6490 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6491 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6492 a70480e0 2018-06-23 stsp }
6493 a19e88aa 2018-06-23 stsp if (error != NULL)
6494 8b473291 2019-02-21 stsp goto done;
6495 8b473291 2019-02-21 stsp
6496 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6497 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6498 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6499 e1cd8fed 2018-08-01 stsp goto done;
6500 e1cd8fed 2018-08-01 stsp }
6501 0587e10c 2020-07-23 stsp
6502 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6503 a44927cc 2022-04-07 stsp if (error)
6504 a44927cc 2022-04-07 stsp goto done;
6505 a44927cc 2022-04-07 stsp
6506 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6507 a44927cc 2022-04-07 stsp commit, repo);
6508 7cbe629d 2018-08-04 stsp if (error)
6509 7cbe629d 2018-08-04 stsp goto done;
6510 0587e10c 2020-07-23 stsp
6511 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6512 78756c87 2020-11-24 stsp commit_id, repo);
6513 0587e10c 2020-07-23 stsp if (error)
6514 0587e10c 2020-07-23 stsp goto done;
6515 12314ad4 2019-08-31 stsp if (worktree) {
6516 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6517 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6518 12314ad4 2019-08-31 stsp worktree = NULL;
6519 12314ad4 2019-08-31 stsp }
6520 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6521 a70480e0 2018-06-23 stsp done:
6522 69069811 2018-08-02 stsp free(repo_path);
6523 f135c941 2020-02-20 stsp free(in_repo_path);
6524 0587e10c 2020-07-23 stsp free(link_target);
6525 69069811 2018-08-02 stsp free(cwd);
6526 a70480e0 2018-06-23 stsp free(commit_id);
6527 a44927cc 2022-04-07 stsp if (commit)
6528 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6529 eb41ed75 2019-02-05 stsp if (worktree)
6530 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6531 1d0f4054 2021-06-17 stsp if (repo) {
6532 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6533 1d0f4054 2021-06-17 stsp if (error == NULL)
6534 1d0f4054 2021-06-17 stsp error = close_err;
6535 1d0f4054 2021-06-17 stsp }
6536 0ae84acc 2022-06-15 tracey if (pack_fds) {
6537 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6538 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6539 0ae84acc 2022-06-15 tracey if (error == NULL)
6540 0ae84acc 2022-06-15 tracey error = pack_err;
6541 0ae84acc 2022-06-15 tracey }
6542 51a10b52 2020-12-26 stsp tog_free_refs();
6543 a70480e0 2018-06-23 stsp return error;
6544 ffd1d5e5 2018-06-23 stsp }
6545 ffd1d5e5 2018-06-23 stsp
6546 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6547 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6548 ffd1d5e5 2018-06-23 stsp {
6549 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6550 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6551 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6552 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6553 0c6ad1bc 2022-09-09 mark char *index = NULL;
6554 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6555 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6556 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6557 ffd1d5e5 2018-06-23 stsp
6558 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6559 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6560 9b058f45 2022-06-30 mark --limit; /* border */
6561 ffd1d5e5 2018-06-23 stsp
6562 f7d12f7e 2018-08-01 stsp werase(view->window);
6563 ffd1d5e5 2018-06-23 stsp
6564 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6565 ffd1d5e5 2018-06-23 stsp return NULL;
6566 ffd1d5e5 2018-06-23 stsp
6567 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6568 ccda2f4d 2022-06-16 stsp 0, 0);
6569 ffd1d5e5 2018-06-23 stsp if (err)
6570 ffd1d5e5 2018-06-23 stsp return err;
6571 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6572 a3404814 2018-09-02 stsp wstandout(view->window);
6573 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6574 11b20872 2019-11-08 stsp if (tc)
6575 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6576 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6577 0c6ad1bc 2022-09-09 mark free(wline);
6578 0c6ad1bc 2022-09-09 mark wline = NULL;
6579 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6580 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6581 11b20872 2019-11-08 stsp if (tc)
6582 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6583 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6584 a3404814 2018-09-02 stsp wstandend(view->window);
6585 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6586 0c6ad1bc 2022-09-09 mark return NULL;
6587 94b80cfa 2022-08-01 mark
6588 df68a56b 2022-08-12 mark i += s->selected;
6589 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6590 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6591 df68a56b 2022-08-12 mark if (s->tree != s->root)
6592 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6593 94b80cfa 2022-08-01 mark }
6594 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6595 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6596 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6597 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6598 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6599 0c6ad1bc 2022-09-09 mark free(index);
6600 ce52c690 2018-06-23 stsp if (err)
6601 ce52c690 2018-06-23 stsp return err;
6602 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6603 2550e4c3 2018-07-13 stsp free(wline);
6604 2550e4c3 2018-07-13 stsp wline = NULL;
6605 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6606 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6607 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6608 ffd1d5e5 2018-06-23 stsp return NULL;
6609 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6610 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6611 a1eca9bb 2018-06-23 stsp return NULL;
6612 ffd1d5e5 2018-06-23 stsp
6613 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6614 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6615 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6616 0cf4efb1 2018-09-29 stsp if (view->focussed)
6617 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6618 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6619 ffd1d5e5 2018-06-23 stsp }
6620 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6621 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6622 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6623 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6624 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6625 ffd1d5e5 2018-06-23 stsp return NULL;
6626 ffd1d5e5 2018-06-23 stsp n = 1;
6627 ffd1d5e5 2018-06-23 stsp } else {
6628 ffd1d5e5 2018-06-23 stsp n = 0;
6629 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6630 ffd1d5e5 2018-06-23 stsp }
6631 ffd1d5e5 2018-06-23 stsp
6632 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6633 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6634 848d6979 2019-08-12 stsp const char *modestr = "";
6635 56e0773d 2019-11-28 stsp mode_t mode;
6636 1d13200f 2018-07-12 stsp
6637 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6638 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6639 56e0773d 2019-11-28 stsp
6640 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6641 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6642 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6643 1d13200f 2018-07-12 stsp if (err)
6644 638f9024 2019-05-13 stsp return got_error_from_errno(
6645 230a42bd 2019-05-11 jcs "got_object_id_str");
6646 1d13200f 2018-07-12 stsp }
6647 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6648 63c5ca5d 2019-08-24 stsp modestr = "$";
6649 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6650 0d6c6ee3 2020-05-20 stsp int i;
6651 0d6c6ee3 2020-05-20 stsp
6652 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6653 d86d3b18 2020-12-01 naddy te, s->repo);
6654 0d6c6ee3 2020-05-20 stsp if (err) {
6655 0d6c6ee3 2020-05-20 stsp free(id_str);
6656 0d6c6ee3 2020-05-20 stsp return err;
6657 0d6c6ee3 2020-05-20 stsp }
6658 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6659 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6660 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6661 0d6c6ee3 2020-05-20 stsp }
6662 848d6979 2019-08-12 stsp modestr = "@";
6663 0d6c6ee3 2020-05-20 stsp }
6664 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6665 848d6979 2019-08-12 stsp modestr = "/";
6666 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6667 848d6979 2019-08-12 stsp modestr = "*";
6668 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6669 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6670 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6671 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6672 1d13200f 2018-07-12 stsp free(id_str);
6673 0d6c6ee3 2020-05-20 stsp free(link_target);
6674 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6675 1d13200f 2018-07-12 stsp }
6676 1d13200f 2018-07-12 stsp free(id_str);
6677 0d6c6ee3 2020-05-20 stsp free(link_target);
6678 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6679 ccda2f4d 2022-06-16 stsp 0, 0);
6680 ffd1d5e5 2018-06-23 stsp if (err) {
6681 ffd1d5e5 2018-06-23 stsp free(line);
6682 ffd1d5e5 2018-06-23 stsp break;
6683 ffd1d5e5 2018-06-23 stsp }
6684 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6685 0cf4efb1 2018-09-29 stsp if (view->focussed)
6686 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6687 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6688 ffd1d5e5 2018-06-23 stsp }
6689 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6690 f26dddb7 2019-11-08 stsp if (tc)
6691 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6692 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6693 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6694 f26dddb7 2019-11-08 stsp if (tc)
6695 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6696 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6697 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6698 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6699 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6700 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6701 ffd1d5e5 2018-06-23 stsp free(line);
6702 2550e4c3 2018-07-13 stsp free(wline);
6703 2550e4c3 2018-07-13 stsp wline = NULL;
6704 ffd1d5e5 2018-06-23 stsp n++;
6705 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6706 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6707 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6708 ffd1d5e5 2018-06-23 stsp break;
6709 ffd1d5e5 2018-06-23 stsp }
6710 ffd1d5e5 2018-06-23 stsp
6711 ffd1d5e5 2018-06-23 stsp return err;
6712 ffd1d5e5 2018-06-23 stsp }
6713 ffd1d5e5 2018-06-23 stsp
6714 ffd1d5e5 2018-06-23 stsp static void
6715 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6716 ffd1d5e5 2018-06-23 stsp {
6717 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6718 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6719 fa86c4bf 2020-11-29 stsp int i = 0;
6720 ffd1d5e5 2018-06-23 stsp
6721 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6722 ffd1d5e5 2018-06-23 stsp return;
6723 ffd1d5e5 2018-06-23 stsp
6724 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6725 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6726 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6727 fa86c4bf 2020-11-29 stsp if (!isroot)
6728 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6729 fa86c4bf 2020-11-29 stsp break;
6730 fa86c4bf 2020-11-29 stsp }
6731 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6732 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6733 ffd1d5e5 2018-06-23 stsp }
6734 ffd1d5e5 2018-06-23 stsp }
6735 ffd1d5e5 2018-06-23 stsp
6736 9b058f45 2022-06-30 mark static const struct got_error *
6737 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6738 ffd1d5e5 2018-06-23 stsp {
6739 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6740 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6741 ffd1d5e5 2018-06-23 stsp int n = 0;
6742 ffd1d5e5 2018-06-23 stsp
6743 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6744 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6745 694d3271 2020-12-01 naddy s->first_displayed_entry);
6746 694d3271 2020-12-01 naddy else
6747 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6748 56e0773d 2019-11-28 stsp
6749 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6750 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6751 94b80cfa 2022-08-01 mark if (last) {
6752 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6753 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6754 94b80cfa 2022-08-01 mark }
6755 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6756 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6757 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6758 768394f3 2019-01-24 stsp }
6759 ffd1d5e5 2018-06-23 stsp }
6760 9b058f45 2022-06-30 mark
6761 9b058f45 2022-06-30 mark return NULL;
6762 ffd1d5e5 2018-06-23 stsp }
6763 ffd1d5e5 2018-06-23 stsp
6764 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6765 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6766 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6767 ffd1d5e5 2018-06-23 stsp {
6768 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6769 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6770 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6771 ffd1d5e5 2018-06-23 stsp
6772 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6773 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6774 56e0773d 2019-11-28 stsp + 1 /* slash */;
6775 ce52c690 2018-06-23 stsp if (te)
6776 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6777 ce52c690 2018-06-23 stsp
6778 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6779 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6780 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6781 ffd1d5e5 2018-06-23 stsp
6782 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6783 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6784 d9765a41 2018-06-23 stsp while (pt) {
6785 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6786 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6787 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6788 cb2ebc8a 2018-06-23 stsp goto done;
6789 cb2ebc8a 2018-06-23 stsp }
6790 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6791 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6792 cb2ebc8a 2018-06-23 stsp goto done;
6793 cb2ebc8a 2018-06-23 stsp }
6794 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6795 ffd1d5e5 2018-06-23 stsp }
6796 ce52c690 2018-06-23 stsp if (te) {
6797 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6798 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6799 ce52c690 2018-06-23 stsp goto done;
6800 ce52c690 2018-06-23 stsp }
6801 cb2ebc8a 2018-06-23 stsp }
6802 ce52c690 2018-06-23 stsp done:
6803 ce52c690 2018-06-23 stsp if (err) {
6804 ce52c690 2018-06-23 stsp free(*path);
6805 ce52c690 2018-06-23 stsp *path = NULL;
6806 ce52c690 2018-06-23 stsp }
6807 ce52c690 2018-06-23 stsp return err;
6808 ce52c690 2018-06-23 stsp }
6809 ce52c690 2018-06-23 stsp
6810 ce52c690 2018-06-23 stsp static const struct got_error *
6811 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6812 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6813 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6814 ce52c690 2018-06-23 stsp {
6815 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6816 ce52c690 2018-06-23 stsp char *path;
6817 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6818 a0de39f3 2019-08-09 stsp
6819 a0de39f3 2019-08-09 stsp *new_view = NULL;
6820 69efd4c4 2018-07-18 stsp
6821 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6822 ce52c690 2018-06-23 stsp if (err)
6823 ce52c690 2018-06-23 stsp return err;
6824 ffd1d5e5 2018-06-23 stsp
6825 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6826 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6827 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6828 83ce39e3 2019-08-12 stsp goto done;
6829 83ce39e3 2019-08-12 stsp }
6830 cdf1ee82 2018-08-01 stsp
6831 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6832 e5a0f69f 2018-08-18 stsp if (err) {
6833 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6834 fc06ba56 2019-08-22 stsp err = NULL;
6835 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6836 e5a0f69f 2018-08-18 stsp } else
6837 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6838 83ce39e3 2019-08-12 stsp done:
6839 83ce39e3 2019-08-12 stsp free(path);
6840 69efd4c4 2018-07-18 stsp return err;
6841 69efd4c4 2018-07-18 stsp }
6842 69efd4c4 2018-07-18 stsp
6843 69efd4c4 2018-07-18 stsp static const struct got_error *
6844 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6845 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6846 69efd4c4 2018-07-18 stsp {
6847 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6848 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6849 69efd4c4 2018-07-18 stsp char *path;
6850 a0de39f3 2019-08-09 stsp
6851 a0de39f3 2019-08-09 stsp *new_view = NULL;
6852 69efd4c4 2018-07-18 stsp
6853 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6854 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6855 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6856 e5a0f69f 2018-08-18 stsp
6857 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6858 69efd4c4 2018-07-18 stsp if (err)
6859 69efd4c4 2018-07-18 stsp return err;
6860 69efd4c4 2018-07-18 stsp
6861 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6862 4e97c21c 2020-12-06 stsp path, 0);
6863 ba4f502b 2018-08-04 stsp if (err)
6864 e5a0f69f 2018-08-18 stsp view_close(log_view);
6865 e5a0f69f 2018-08-18 stsp else
6866 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6867 cb2ebc8a 2018-06-23 stsp free(path);
6868 cb2ebc8a 2018-06-23 stsp return err;
6869 ffd1d5e5 2018-06-23 stsp }
6870 ffd1d5e5 2018-06-23 stsp
6871 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6872 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6873 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6874 ffd1d5e5 2018-06-23 stsp {
6875 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6876 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6877 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6878 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6879 ffd1d5e5 2018-06-23 stsp
6880 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6881 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6882 bc573f3b 2021-07-10 stsp
6883 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6884 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6885 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6886 ffd1d5e5 2018-06-23 stsp
6887 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6888 bc573f3b 2021-07-10 stsp if (err)
6889 bc573f3b 2021-07-10 stsp goto done;
6890 bc573f3b 2021-07-10 stsp
6891 bc573f3b 2021-07-10 stsp /*
6892 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6893 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6894 bc573f3b 2021-07-10 stsp * closed on demand.
6895 bc573f3b 2021-07-10 stsp */
6896 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6897 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6898 bc573f3b 2021-07-10 stsp if (err)
6899 bc573f3b 2021-07-10 stsp goto done;
6900 bc573f3b 2021-07-10 stsp s->tree = s->root;
6901 bc573f3b 2021-07-10 stsp
6902 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6903 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6904 ffd1d5e5 2018-06-23 stsp goto done;
6905 ffd1d5e5 2018-06-23 stsp
6906 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6907 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6908 ffd1d5e5 2018-06-23 stsp goto done;
6909 ffd1d5e5 2018-06-23 stsp }
6910 ffd1d5e5 2018-06-23 stsp
6911 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6912 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6913 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6914 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6915 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6916 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6917 9cd7cbd1 2020-12-07 stsp goto done;
6918 9cd7cbd1 2020-12-07 stsp }
6919 9cd7cbd1 2020-12-07 stsp }
6920 fb2756b9 2018-08-04 stsp s->repo = repo;
6921 c0b01bdb 2019-11-08 stsp
6922 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6923 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6924 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6925 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6926 c0b01bdb 2019-11-08 stsp if (err)
6927 c0b01bdb 2019-11-08 stsp goto done;
6928 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6929 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6930 bc573f3b 2021-07-10 stsp if (err)
6931 c0b01bdb 2019-11-08 stsp goto done;
6932 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6933 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6934 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6935 bc573f3b 2021-07-10 stsp if (err)
6936 c0b01bdb 2019-11-08 stsp goto done;
6937 e5a0f69f 2018-08-18 stsp
6938 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6939 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6940 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6941 bc573f3b 2021-07-10 stsp if (err)
6942 11b20872 2019-11-08 stsp goto done;
6943 11b20872 2019-11-08 stsp
6944 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6945 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6946 bc573f3b 2021-07-10 stsp if (err)
6947 c0b01bdb 2019-11-08 stsp goto done;
6948 c0b01bdb 2019-11-08 stsp }
6949 c0b01bdb 2019-11-08 stsp
6950 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6951 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6952 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6953 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6954 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6955 ad80ab7b 2018-08-04 stsp done:
6956 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6957 bc573f3b 2021-07-10 stsp if (commit)
6958 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6959 bc573f3b 2021-07-10 stsp if (err)
6960 bc573f3b 2021-07-10 stsp close_tree_view(view);
6961 ad80ab7b 2018-08-04 stsp return err;
6962 ad80ab7b 2018-08-04 stsp }
6963 ad80ab7b 2018-08-04 stsp
6964 e5a0f69f 2018-08-18 stsp static const struct got_error *
6965 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6966 ad80ab7b 2018-08-04 stsp {
6967 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6968 ad80ab7b 2018-08-04 stsp
6969 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6970 fb2756b9 2018-08-04 stsp free(s->tree_label);
6971 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6972 6484ec90 2018-09-29 stsp free(s->commit_id);
6973 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6974 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6975 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6976 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6977 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6978 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6979 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6980 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6981 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6982 ad80ab7b 2018-08-04 stsp free(parent);
6983 ad80ab7b 2018-08-04 stsp
6984 ad80ab7b 2018-08-04 stsp }
6985 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6986 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6987 bc573f3b 2021-07-10 stsp if (s->root)
6988 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6989 7c32bd05 2019-06-22 stsp return NULL;
6990 7c32bd05 2019-06-22 stsp }
6991 7c32bd05 2019-06-22 stsp
6992 7c32bd05 2019-06-22 stsp static const struct got_error *
6993 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6994 7c32bd05 2019-06-22 stsp {
6995 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6996 7c32bd05 2019-06-22 stsp
6997 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6998 7c32bd05 2019-06-22 stsp return NULL;
6999 7c32bd05 2019-06-22 stsp }
7000 7c32bd05 2019-06-22 stsp
7001 7c32bd05 2019-06-22 stsp static int
7002 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7003 7c32bd05 2019-06-22 stsp {
7004 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7005 7c32bd05 2019-06-22 stsp
7006 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7007 56e0773d 2019-11-28 stsp 0) == 0;
7008 7c32bd05 2019-06-22 stsp }
7009 7c32bd05 2019-06-22 stsp
7010 7c32bd05 2019-06-22 stsp static const struct got_error *
7011 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7012 7c32bd05 2019-06-22 stsp {
7013 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7014 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7015 7c32bd05 2019-06-22 stsp
7016 7c32bd05 2019-06-22 stsp if (!view->searching) {
7017 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7018 7c32bd05 2019-06-22 stsp return NULL;
7019 7c32bd05 2019-06-22 stsp }
7020 7c32bd05 2019-06-22 stsp
7021 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7022 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7023 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7024 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7025 56e0773d 2019-11-28 stsp s->selected_entry);
7026 7c32bd05 2019-06-22 stsp else
7027 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7028 56e0773d 2019-11-28 stsp } else {
7029 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7030 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7031 56e0773d 2019-11-28 stsp else
7032 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7033 56e0773d 2019-11-28 stsp s->selected_entry);
7034 7c32bd05 2019-06-22 stsp }
7035 7c32bd05 2019-06-22 stsp } else {
7036 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7037 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7038 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7039 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7040 56e0773d 2019-11-28 stsp else
7041 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7042 7c32bd05 2019-06-22 stsp }
7043 7c32bd05 2019-06-22 stsp
7044 7c32bd05 2019-06-22 stsp while (1) {
7045 56e0773d 2019-11-28 stsp if (te == NULL) {
7046 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7047 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7048 ac66afb8 2019-06-24 stsp return NULL;
7049 ac66afb8 2019-06-24 stsp }
7050 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7051 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7052 56e0773d 2019-11-28 stsp else
7053 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7054 7c32bd05 2019-06-22 stsp }
7055 7c32bd05 2019-06-22 stsp
7056 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7057 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7058 56e0773d 2019-11-28 stsp s->matched_entry = te;
7059 7c32bd05 2019-06-22 stsp break;
7060 7c32bd05 2019-06-22 stsp }
7061 7c32bd05 2019-06-22 stsp
7062 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7063 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7064 56e0773d 2019-11-28 stsp else
7065 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7066 7c32bd05 2019-06-22 stsp }
7067 e5a0f69f 2018-08-18 stsp
7068 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7069 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7070 7c32bd05 2019-06-22 stsp s->selected = 0;
7071 7c32bd05 2019-06-22 stsp }
7072 7c32bd05 2019-06-22 stsp
7073 e5a0f69f 2018-08-18 stsp return NULL;
7074 ad80ab7b 2018-08-04 stsp }
7075 ad80ab7b 2018-08-04 stsp
7076 ad80ab7b 2018-08-04 stsp static const struct got_error *
7077 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7078 ad80ab7b 2018-08-04 stsp {
7079 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7080 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7081 e5a0f69f 2018-08-18 stsp char *parent_path;
7082 ad80ab7b 2018-08-04 stsp
7083 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7084 e5a0f69f 2018-08-18 stsp if (err)
7085 e5a0f69f 2018-08-18 stsp return err;
7086 ffd1d5e5 2018-06-23 stsp
7087 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7088 e5a0f69f 2018-08-18 stsp free(parent_path);
7089 669b5ffa 2018-10-07 stsp
7090 9b058f45 2022-06-30 mark view_border(view);
7091 e5a0f69f 2018-08-18 stsp return err;
7092 94b80cfa 2022-08-01 mark }
7093 94b80cfa 2022-08-01 mark
7094 94b80cfa 2022-08-01 mark static const struct got_error *
7095 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7096 94b80cfa 2022-08-01 mark {
7097 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7098 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7099 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7100 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7101 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7102 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7103 94b80cfa 2022-08-01 mark
7104 94b80cfa 2022-08-01 mark g = view->gline;
7105 94b80cfa 2022-08-01 mark view->gline = 0;
7106 94b80cfa 2022-08-01 mark
7107 94b80cfa 2022-08-01 mark if (g == 0)
7108 94b80cfa 2022-08-01 mark g = 1;
7109 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7110 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7111 94b80cfa 2022-08-01 mark
7112 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7113 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7114 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7115 94b80cfa 2022-08-01 mark
7116 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7117 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7118 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7119 94b80cfa 2022-08-01 mark }
7120 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7121 94b80cfa 2022-08-01 mark last += off;
7122 94b80cfa 2022-08-01 mark
7123 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7124 94b80cfa 2022-08-01 mark s->selected = g - first;
7125 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7126 94b80cfa 2022-08-01 mark }
7127 94b80cfa 2022-08-01 mark
7128 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7129 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7130 94b80cfa 2022-08-01 mark i += off;
7131 94b80cfa 2022-08-01 mark }
7132 94b80cfa 2022-08-01 mark
7133 94b80cfa 2022-08-01 mark if (i < g) {
7134 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7135 94b80cfa 2022-08-01 mark if (err)
7136 94b80cfa 2022-08-01 mark return err;
7137 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7138 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7139 94b80cfa 2022-08-01 mark first + s->selected < g &&
7140 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7141 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7142 94b80cfa 2022-08-01 mark first += off;
7143 94b80cfa 2022-08-01 mark s->selected = g - first;
7144 94b80cfa 2022-08-01 mark }
7145 94b80cfa 2022-08-01 mark } else if (i > g)
7146 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7147 94b80cfa 2022-08-01 mark
7148 94b80cfa 2022-08-01 mark if (g < nlines &&
7149 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7150 94b80cfa 2022-08-01 mark s->selected = g - 1;
7151 94b80cfa 2022-08-01 mark
7152 94b80cfa 2022-08-01 mark return NULL;
7153 e5a0f69f 2018-08-18 stsp }
7154 ce52c690 2018-06-23 stsp
7155 e5a0f69f 2018-08-18 stsp static const struct got_error *
7156 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7157 e5a0f69f 2018-08-18 stsp {
7158 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7159 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7160 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7161 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7162 ffd1d5e5 2018-06-23 stsp
7163 94b80cfa 2022-08-01 mark if (view->gline)
7164 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7165 94b80cfa 2022-08-01 mark
7166 e5a0f69f 2018-08-18 stsp switch (ch) {
7167 1e37a5c2 2019-05-12 jcs case 'i':
7168 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7169 640cd7ff 2022-06-22 mark view->count = 0;
7170 1e37a5c2 2019-05-12 jcs break;
7171 5e98fb33 2022-07-22 mark case 'L':
7172 640cd7ff 2022-06-22 mark view->count = 0;
7173 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7174 ffd1d5e5 2018-06-23 stsp break;
7175 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7176 152c1c93 2020-11-29 stsp break;
7177 5e98fb33 2022-07-22 mark case 'R':
7178 640cd7ff 2022-06-22 mark view->count = 0;
7179 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7180 e4526bf5 2021-09-03 naddy break;
7181 e4526bf5 2021-09-03 naddy case 'g':
7182 e4526bf5 2021-09-03 naddy case KEY_HOME:
7183 e4526bf5 2021-09-03 naddy s->selected = 0;
7184 640cd7ff 2022-06-22 mark view->count = 0;
7185 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7186 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7187 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7188 e4526bf5 2021-09-03 naddy else
7189 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7190 1e37a5c2 2019-05-12 jcs break;
7191 e4526bf5 2021-09-03 naddy case 'G':
7192 9b058f45 2022-06-30 mark case KEY_END: {
7193 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7194 9b058f45 2022-06-30 mark
7195 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7196 9b058f45 2022-06-30 mark --eos; /* border */
7197 e4526bf5 2021-09-03 naddy s->selected = 0;
7198 640cd7ff 2022-06-22 mark view->count = 0;
7199 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7200 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7201 e4526bf5 2021-09-03 naddy if (te == NULL) {
7202 ad055527 2022-07-16 mark if (s->tree != s->root) {
7203 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7204 e4526bf5 2021-09-03 naddy n++;
7205 e4526bf5 2021-09-03 naddy }
7206 e4526bf5 2021-09-03 naddy break;
7207 e4526bf5 2021-09-03 naddy }
7208 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7209 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7210 e4526bf5 2021-09-03 naddy }
7211 e4526bf5 2021-09-03 naddy if (n > 0)
7212 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7213 e4526bf5 2021-09-03 naddy break;
7214 9b058f45 2022-06-30 mark }
7215 1e37a5c2 2019-05-12 jcs case 'k':
7216 1e37a5c2 2019-05-12 jcs case KEY_UP:
7217 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7218 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7219 1e37a5c2 2019-05-12 jcs s->selected--;
7220 fa86c4bf 2020-11-29 stsp break;
7221 1e37a5c2 2019-05-12 jcs }
7222 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7223 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7224 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7225 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7226 640cd7ff 2022-06-22 mark view->count = 0;
7227 1e37a5c2 2019-05-12 jcs break;
7228 83cc4199 2022-06-13 stsp case CTRL('u'):
7229 33c3719a 2022-06-15 stsp case 'u':
7230 83cc4199 2022-06-13 stsp nscroll /= 2;
7231 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7232 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7233 ea025d1d 2020-02-22 naddy case CTRL('b'):
7234 61417565 2022-06-20 mark case 'b':
7235 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7236 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7237 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7238 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7239 fa86c4bf 2020-11-29 stsp } else {
7240 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7241 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7242 fa86c4bf 2020-11-29 stsp }
7243 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7244 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7245 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7246 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7247 640cd7ff 2022-06-22 mark view->count = 0;
7248 1e37a5c2 2019-05-12 jcs break;
7249 1e37a5c2 2019-05-12 jcs case 'j':
7250 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7251 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7252 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7253 1e37a5c2 2019-05-12 jcs s->selected++;
7254 1e37a5c2 2019-05-12 jcs break;
7255 1e37a5c2 2019-05-12 jcs }
7256 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7257 640cd7ff 2022-06-22 mark == NULL) {
7258 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7259 640cd7ff 2022-06-22 mark view->count = 0;
7260 1e37a5c2 2019-05-12 jcs break;
7261 640cd7ff 2022-06-22 mark }
7262 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7263 1e37a5c2 2019-05-12 jcs break;
7264 83cc4199 2022-06-13 stsp case CTRL('d'):
7265 33c3719a 2022-06-15 stsp case 'd':
7266 83cc4199 2022-06-13 stsp nscroll /= 2;
7267 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7268 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7269 ea025d1d 2020-02-22 naddy case CTRL('f'):
7270 61417565 2022-06-20 mark case 'f':
7271 48bb96f0 2022-06-20 naddy case ' ':
7272 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7273 1e37a5c2 2019-05-12 jcs == NULL) {
7274 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7275 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7276 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7277 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7278 640cd7ff 2022-06-22 mark else
7279 640cd7ff 2022-06-22 mark view->count = 0;
7280 1e37a5c2 2019-05-12 jcs break;
7281 1e37a5c2 2019-05-12 jcs }
7282 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7283 1e37a5c2 2019-05-12 jcs break;
7284 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7285 1e37a5c2 2019-05-12 jcs case '\r':
7286 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7287 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7288 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7289 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7290 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7291 640cd7ff 2022-06-22 mark view->count = 0;
7292 1e37a5c2 2019-05-12 jcs break;
7293 640cd7ff 2022-06-22 mark }
7294 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7295 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7296 1e37a5c2 2019-05-12 jcs entry);
7297 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7298 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7299 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7300 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7301 1e37a5c2 2019-05-12 jcs s->selected_entry =
7302 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7303 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7304 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7305 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7306 9b058f45 2022-06-30 mark if (err)
7307 9b058f45 2022-06-30 mark break;
7308 9b058f45 2022-06-30 mark }
7309 1e37a5c2 2019-05-12 jcs free(parent);
7310 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7311 56e0773d 2019-11-28 stsp s->selected_entry))) {
7312 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7313 640cd7ff 2022-06-22 mark view->count = 0;
7314 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7315 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7316 1e37a5c2 2019-05-12 jcs if (err)
7317 1e37a5c2 2019-05-12 jcs break;
7318 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7319 941e9f74 2019-05-21 stsp if (err) {
7320 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7321 1e37a5c2 2019-05-12 jcs break;
7322 1e37a5c2 2019-05-12 jcs }
7323 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7324 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7325 1e37a5c2 2019-05-12 jcs break;
7326 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7327 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7328 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7329 640cd7ff 2022-06-22 mark view->count = 0;
7330 1e37a5c2 2019-05-12 jcs break;
7331 1e37a5c2 2019-05-12 jcs default:
7332 640cd7ff 2022-06-22 mark view->count = 0;
7333 1e37a5c2 2019-05-12 jcs break;
7334 ffd1d5e5 2018-06-23 stsp }
7335 e5a0f69f 2018-08-18 stsp
7336 ffd1d5e5 2018-06-23 stsp return err;
7337 9f7d7167 2018-04-29 stsp }
7338 9f7d7167 2018-04-29 stsp
7339 ffd1d5e5 2018-06-23 stsp __dead static void
7340 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7341 ffd1d5e5 2018-06-23 stsp {
7342 ffd1d5e5 2018-06-23 stsp endwin();
7343 87411fa9 2022-06-16 stsp fprintf(stderr,
7344 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7345 ffd1d5e5 2018-06-23 stsp getprogname());
7346 ffd1d5e5 2018-06-23 stsp exit(1);
7347 ffd1d5e5 2018-06-23 stsp }
7348 ffd1d5e5 2018-06-23 stsp
7349 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7350 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7351 ffd1d5e5 2018-06-23 stsp {
7352 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7353 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7354 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7355 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7356 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7357 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7358 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7359 4e97c21c 2020-12-06 stsp char *label = NULL;
7360 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7361 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7362 ffd1d5e5 2018-06-23 stsp int ch;
7363 5221c383 2018-08-01 stsp struct tog_view *view;
7364 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7365 70ac5f84 2019-03-28 stsp
7366 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7367 ffd1d5e5 2018-06-23 stsp switch (ch) {
7368 ffd1d5e5 2018-06-23 stsp case 'c':
7369 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7370 74283ab8 2020-02-07 stsp break;
7371 74283ab8 2020-02-07 stsp case 'r':
7372 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7373 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7374 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7375 74283ab8 2020-02-07 stsp optarg);
7376 ffd1d5e5 2018-06-23 stsp break;
7377 ffd1d5e5 2018-06-23 stsp default:
7378 e99e2d15 2020-11-24 naddy usage_tree();
7379 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7380 ffd1d5e5 2018-06-23 stsp }
7381 ffd1d5e5 2018-06-23 stsp }
7382 ffd1d5e5 2018-06-23 stsp
7383 ffd1d5e5 2018-06-23 stsp argc -= optind;
7384 ffd1d5e5 2018-06-23 stsp argv += optind;
7385 ffd1d5e5 2018-06-23 stsp
7386 55cccc34 2020-02-20 stsp if (argc > 1)
7387 e99e2d15 2020-11-24 naddy usage_tree();
7388 0ae84acc 2022-06-15 tracey
7389 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7390 0ae84acc 2022-06-15 tracey if (error != NULL)
7391 0ae84acc 2022-06-15 tracey goto done;
7392 74283ab8 2020-02-07 stsp
7393 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7394 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7395 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7396 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7397 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7398 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7399 c156c7a4 2020-12-18 stsp goto done;
7400 55cccc34 2020-02-20 stsp if (worktree)
7401 52185f70 2019-02-05 stsp repo_path =
7402 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7403 55cccc34 2020-02-20 stsp else
7404 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7405 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7406 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7407 c156c7a4 2020-12-18 stsp goto done;
7408 c156c7a4 2020-12-18 stsp }
7409 55cccc34 2020-02-20 stsp }
7410 a915003a 2019-02-05 stsp
7411 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7412 c02c541e 2019-03-29 stsp if (error != NULL)
7413 52185f70 2019-02-05 stsp goto done;
7414 d188b9a6 2019-01-04 stsp
7415 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7416 55cccc34 2020-02-20 stsp repo, worktree);
7417 55cccc34 2020-02-20 stsp if (error)
7418 55cccc34 2020-02-20 stsp goto done;
7419 55cccc34 2020-02-20 stsp
7420 55cccc34 2020-02-20 stsp init_curses();
7421 55cccc34 2020-02-20 stsp
7422 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7423 c02c541e 2019-03-29 stsp if (error)
7424 52185f70 2019-02-05 stsp goto done;
7425 ffd1d5e5 2018-06-23 stsp
7426 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7427 51a10b52 2020-12-26 stsp if (error)
7428 51a10b52 2020-12-26 stsp goto done;
7429 51a10b52 2020-12-26 stsp
7430 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7431 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7432 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7433 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7434 4e97c21c 2020-12-06 stsp if (error)
7435 4e97c21c 2020-12-06 stsp goto done;
7436 4e97c21c 2020-12-06 stsp head_ref_name = label;
7437 4e97c21c 2020-12-06 stsp } else {
7438 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7439 4e97c21c 2020-12-06 stsp if (error == NULL)
7440 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7441 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7442 4e97c21c 2020-12-06 stsp goto done;
7443 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7444 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7445 4e97c21c 2020-12-06 stsp if (error)
7446 4e97c21c 2020-12-06 stsp goto done;
7447 4e97c21c 2020-12-06 stsp }
7448 ffd1d5e5 2018-06-23 stsp
7449 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7450 a44927cc 2022-04-07 stsp if (error)
7451 a44927cc 2022-04-07 stsp goto done;
7452 a44927cc 2022-04-07 stsp
7453 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7454 5221c383 2018-08-01 stsp if (view == NULL) {
7455 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7456 5221c383 2018-08-01 stsp goto done;
7457 5221c383 2018-08-01 stsp }
7458 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7459 ad80ab7b 2018-08-04 stsp if (error)
7460 ad80ab7b 2018-08-04 stsp goto done;
7461 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7462 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7463 d91faf3b 2020-12-01 naddy in_repo_path);
7464 55cccc34 2020-02-20 stsp if (error)
7465 55cccc34 2020-02-20 stsp goto done;
7466 55cccc34 2020-02-20 stsp }
7467 55cccc34 2020-02-20 stsp
7468 55cccc34 2020-02-20 stsp if (worktree) {
7469 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7470 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7471 55cccc34 2020-02-20 stsp worktree = NULL;
7472 55cccc34 2020-02-20 stsp }
7473 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7474 ffd1d5e5 2018-06-23 stsp done:
7475 52185f70 2019-02-05 stsp free(repo_path);
7476 e4a0e26d 2020-02-20 stsp free(cwd);
7477 ffd1d5e5 2018-06-23 stsp free(commit_id);
7478 4e97c21c 2020-12-06 stsp free(label);
7479 486cd271 2020-12-06 stsp if (ref)
7480 486cd271 2020-12-06 stsp got_ref_close(ref);
7481 1d0f4054 2021-06-17 stsp if (repo) {
7482 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7483 1d0f4054 2021-06-17 stsp if (error == NULL)
7484 1d0f4054 2021-06-17 stsp error = close_err;
7485 1d0f4054 2021-06-17 stsp }
7486 0ae84acc 2022-06-15 tracey if (pack_fds) {
7487 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7488 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7489 0ae84acc 2022-06-15 tracey if (error == NULL)
7490 0ae84acc 2022-06-15 tracey error = pack_err;
7491 0ae84acc 2022-06-15 tracey }
7492 51a10b52 2020-12-26 stsp tog_free_refs();
7493 ffd1d5e5 2018-06-23 stsp return error;
7494 6458efa5 2020-11-24 stsp }
7495 6458efa5 2020-11-24 stsp
7496 6458efa5 2020-11-24 stsp static const struct got_error *
7497 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7498 6458efa5 2020-11-24 stsp {
7499 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7500 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7501 6458efa5 2020-11-24 stsp
7502 6458efa5 2020-11-24 stsp s->nrefs = 0;
7503 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7504 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7505 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7506 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7507 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7508 6458efa5 2020-11-24 stsp continue;
7509 6458efa5 2020-11-24 stsp
7510 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7511 6458efa5 2020-11-24 stsp if (re == NULL)
7512 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7513 6458efa5 2020-11-24 stsp
7514 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7515 8924d611 2020-12-26 stsp if (re->ref == NULL)
7516 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7517 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7518 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7519 6458efa5 2020-11-24 stsp }
7520 6458efa5 2020-11-24 stsp
7521 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7522 6458efa5 2020-11-24 stsp return NULL;
7523 6458efa5 2020-11-24 stsp }
7524 6458efa5 2020-11-24 stsp
7525 336075a4 2022-06-25 op static void
7526 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7527 6458efa5 2020-11-24 stsp {
7528 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7529 6458efa5 2020-11-24 stsp
7530 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7531 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7532 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7533 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7534 6458efa5 2020-11-24 stsp free(re);
7535 6458efa5 2020-11-24 stsp }
7536 6458efa5 2020-11-24 stsp }
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp static const struct got_error *
7539 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7540 6458efa5 2020-11-24 stsp {
7541 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7542 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7543 6458efa5 2020-11-24 stsp
7544 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7545 6458efa5 2020-11-24 stsp s->repo = repo;
7546 6458efa5 2020-11-24 stsp
7547 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7548 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7549 6458efa5 2020-11-24 stsp
7550 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7551 6458efa5 2020-11-24 stsp if (err)
7552 6458efa5 2020-11-24 stsp return err;
7553 34ba6917 2020-11-30 stsp
7554 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7555 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7556 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7557 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7558 6458efa5 2020-11-24 stsp if (err)
7559 6458efa5 2020-11-24 stsp goto done;
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7562 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7563 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7564 6458efa5 2020-11-24 stsp if (err)
7565 6458efa5 2020-11-24 stsp goto done;
7566 6458efa5 2020-11-24 stsp
7567 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7568 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7569 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7570 cc488aa7 2022-01-23 stsp if (err)
7571 cc488aa7 2022-01-23 stsp goto done;
7572 cc488aa7 2022-01-23 stsp
7573 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7574 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7575 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7576 6458efa5 2020-11-24 stsp if (err)
7577 6458efa5 2020-11-24 stsp goto done;
7578 6458efa5 2020-11-24 stsp }
7579 6458efa5 2020-11-24 stsp
7580 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7581 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7582 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7583 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7584 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7585 6458efa5 2020-11-24 stsp done:
7586 6458efa5 2020-11-24 stsp if (err)
7587 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7588 6458efa5 2020-11-24 stsp return err;
7589 6458efa5 2020-11-24 stsp }
7590 6458efa5 2020-11-24 stsp
7591 6458efa5 2020-11-24 stsp static const struct got_error *
7592 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7593 6458efa5 2020-11-24 stsp {
7594 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7595 6458efa5 2020-11-24 stsp
7596 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7597 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7598 6458efa5 2020-11-24 stsp
7599 6458efa5 2020-11-24 stsp return NULL;
7600 9f7d7167 2018-04-29 stsp }
7601 ce5b7c56 2019-07-09 stsp
7602 6458efa5 2020-11-24 stsp static const struct got_error *
7603 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7604 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7605 6458efa5 2020-11-24 stsp {
7606 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7607 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7608 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7609 6458efa5 2020-11-24 stsp int obj_type;
7610 6458efa5 2020-11-24 stsp
7611 c42c9805 2020-11-24 stsp *commit_id = NULL;
7612 6458efa5 2020-11-24 stsp
7613 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7614 6458efa5 2020-11-24 stsp if (err)
7615 6458efa5 2020-11-24 stsp return err;
7616 6458efa5 2020-11-24 stsp
7617 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7618 6458efa5 2020-11-24 stsp if (err)
7619 6458efa5 2020-11-24 stsp goto done;
7620 6458efa5 2020-11-24 stsp
7621 6458efa5 2020-11-24 stsp switch (obj_type) {
7622 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7623 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7624 6458efa5 2020-11-24 stsp break;
7625 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7626 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7627 6458efa5 2020-11-24 stsp if (err)
7628 6458efa5 2020-11-24 stsp goto done;
7629 c42c9805 2020-11-24 stsp free(obj_id);
7630 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7631 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7632 c42c9805 2020-11-24 stsp if (err)
7633 6458efa5 2020-11-24 stsp goto done;
7634 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7635 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7636 c42c9805 2020-11-24 stsp goto done;
7637 c42c9805 2020-11-24 stsp }
7638 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7639 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7640 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7641 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7642 c42c9805 2020-11-24 stsp goto done;
7643 c42c9805 2020-11-24 stsp }
7644 6458efa5 2020-11-24 stsp break;
7645 6458efa5 2020-11-24 stsp default:
7646 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7647 c42c9805 2020-11-24 stsp break;
7648 6458efa5 2020-11-24 stsp }
7649 6458efa5 2020-11-24 stsp
7650 c42c9805 2020-11-24 stsp done:
7651 c42c9805 2020-11-24 stsp if (tag)
7652 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7653 c42c9805 2020-11-24 stsp if (err) {
7654 c42c9805 2020-11-24 stsp free(*commit_id);
7655 c42c9805 2020-11-24 stsp *commit_id = NULL;
7656 c42c9805 2020-11-24 stsp }
7657 c42c9805 2020-11-24 stsp return err;
7658 c42c9805 2020-11-24 stsp }
7659 c42c9805 2020-11-24 stsp
7660 c42c9805 2020-11-24 stsp static const struct got_error *
7661 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7662 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7663 c42c9805 2020-11-24 stsp {
7664 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7665 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7666 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7667 c42c9805 2020-11-24 stsp
7668 c42c9805 2020-11-24 stsp *new_view = NULL;
7669 c42c9805 2020-11-24 stsp
7670 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7671 c42c9805 2020-11-24 stsp if (err) {
7672 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7673 c42c9805 2020-11-24 stsp return err;
7674 c42c9805 2020-11-24 stsp else
7675 c42c9805 2020-11-24 stsp return NULL;
7676 c42c9805 2020-11-24 stsp }
7677 c42c9805 2020-11-24 stsp
7678 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7679 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7680 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7681 6458efa5 2020-11-24 stsp goto done;
7682 6458efa5 2020-11-24 stsp }
7683 6458efa5 2020-11-24 stsp
7684 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7685 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7686 6458efa5 2020-11-24 stsp done:
7687 6458efa5 2020-11-24 stsp if (err)
7688 6458efa5 2020-11-24 stsp view_close(log_view);
7689 6458efa5 2020-11-24 stsp else
7690 6458efa5 2020-11-24 stsp *new_view = log_view;
7691 c42c9805 2020-11-24 stsp free(commit_id);
7692 6458efa5 2020-11-24 stsp return err;
7693 6458efa5 2020-11-24 stsp }
7694 6458efa5 2020-11-24 stsp
7695 ce5b7c56 2019-07-09 stsp static void
7696 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7697 6458efa5 2020-11-24 stsp {
7698 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7699 34ba6917 2020-11-30 stsp int i = 0;
7700 6458efa5 2020-11-24 stsp
7701 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7702 6458efa5 2020-11-24 stsp return;
7703 6458efa5 2020-11-24 stsp
7704 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7705 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7706 34ba6917 2020-11-30 stsp if (re == NULL)
7707 34ba6917 2020-11-30 stsp break;
7708 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7709 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7710 6458efa5 2020-11-24 stsp }
7711 6458efa5 2020-11-24 stsp }
7712 6458efa5 2020-11-24 stsp
7713 9b058f45 2022-06-30 mark static const struct got_error *
7714 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7715 6458efa5 2020-11-24 stsp {
7716 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7717 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7718 6458efa5 2020-11-24 stsp int n = 0;
7719 6458efa5 2020-11-24 stsp
7720 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7721 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7722 6458efa5 2020-11-24 stsp else
7723 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7724 6458efa5 2020-11-24 stsp
7725 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7726 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7727 94b80cfa 2022-08-01 mark if (last) {
7728 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7729 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7730 94b80cfa 2022-08-01 mark }
7731 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7732 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7733 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7734 6458efa5 2020-11-24 stsp }
7735 6458efa5 2020-11-24 stsp }
7736 9b058f45 2022-06-30 mark
7737 9b058f45 2022-06-30 mark return NULL;
7738 6458efa5 2020-11-24 stsp }
7739 6458efa5 2020-11-24 stsp
7740 6458efa5 2020-11-24 stsp static const struct got_error *
7741 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7742 6458efa5 2020-11-24 stsp {
7743 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7744 6458efa5 2020-11-24 stsp
7745 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7746 6458efa5 2020-11-24 stsp return NULL;
7747 6458efa5 2020-11-24 stsp }
7748 6458efa5 2020-11-24 stsp
7749 6458efa5 2020-11-24 stsp static int
7750 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7751 6458efa5 2020-11-24 stsp {
7752 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7753 6458efa5 2020-11-24 stsp
7754 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7755 6458efa5 2020-11-24 stsp 0) == 0;
7756 6458efa5 2020-11-24 stsp }
7757 6458efa5 2020-11-24 stsp
7758 6458efa5 2020-11-24 stsp static const struct got_error *
7759 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7760 6458efa5 2020-11-24 stsp {
7761 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7762 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7763 6458efa5 2020-11-24 stsp
7764 6458efa5 2020-11-24 stsp if (!view->searching) {
7765 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7766 6458efa5 2020-11-24 stsp return NULL;
7767 6458efa5 2020-11-24 stsp }
7768 6458efa5 2020-11-24 stsp
7769 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7770 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7771 6458efa5 2020-11-24 stsp if (s->selected_entry)
7772 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7773 6458efa5 2020-11-24 stsp else
7774 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7775 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7776 6458efa5 2020-11-24 stsp } else {
7777 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7778 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7779 6458efa5 2020-11-24 stsp else
7780 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7781 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7782 6458efa5 2020-11-24 stsp }
7783 6458efa5 2020-11-24 stsp } else {
7784 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7785 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7786 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7787 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7788 6458efa5 2020-11-24 stsp else
7789 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7790 6458efa5 2020-11-24 stsp }
7791 6458efa5 2020-11-24 stsp
7792 6458efa5 2020-11-24 stsp while (1) {
7793 6458efa5 2020-11-24 stsp if (re == NULL) {
7794 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7795 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7796 6458efa5 2020-11-24 stsp return NULL;
7797 6458efa5 2020-11-24 stsp }
7798 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7799 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7800 6458efa5 2020-11-24 stsp else
7801 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7802 6458efa5 2020-11-24 stsp }
7803 6458efa5 2020-11-24 stsp
7804 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7805 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7806 6458efa5 2020-11-24 stsp s->matched_entry = re;
7807 6458efa5 2020-11-24 stsp break;
7808 6458efa5 2020-11-24 stsp }
7809 6458efa5 2020-11-24 stsp
7810 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7811 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7812 6458efa5 2020-11-24 stsp else
7813 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7814 6458efa5 2020-11-24 stsp }
7815 6458efa5 2020-11-24 stsp
7816 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7817 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7818 6458efa5 2020-11-24 stsp s->selected = 0;
7819 6458efa5 2020-11-24 stsp }
7820 6458efa5 2020-11-24 stsp
7821 6458efa5 2020-11-24 stsp return NULL;
7822 6458efa5 2020-11-24 stsp }
7823 6458efa5 2020-11-24 stsp
7824 6458efa5 2020-11-24 stsp static const struct got_error *
7825 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7826 6458efa5 2020-11-24 stsp {
7827 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7828 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7829 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7830 6458efa5 2020-11-24 stsp char *line = NULL;
7831 6458efa5 2020-11-24 stsp wchar_t *wline;
7832 6458efa5 2020-11-24 stsp struct tog_color *tc;
7833 6458efa5 2020-11-24 stsp int width, n;
7834 6458efa5 2020-11-24 stsp int limit = view->nlines;
7835 6458efa5 2020-11-24 stsp
7836 6458efa5 2020-11-24 stsp werase(view->window);
7837 6458efa5 2020-11-24 stsp
7838 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7839 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7840 9b058f45 2022-06-30 mark --limit; /* border */
7841 6458efa5 2020-11-24 stsp
7842 6458efa5 2020-11-24 stsp if (limit == 0)
7843 6458efa5 2020-11-24 stsp return NULL;
7844 6458efa5 2020-11-24 stsp
7845 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7846 6458efa5 2020-11-24 stsp
7847 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7848 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7849 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7850 6458efa5 2020-11-24 stsp
7851 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7852 6458efa5 2020-11-24 stsp if (err) {
7853 6458efa5 2020-11-24 stsp free(line);
7854 6458efa5 2020-11-24 stsp return err;
7855 6458efa5 2020-11-24 stsp }
7856 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7857 6458efa5 2020-11-24 stsp wstandout(view->window);
7858 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7859 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
7860 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
7861 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7862 6458efa5 2020-11-24 stsp wstandend(view->window);
7863 6458efa5 2020-11-24 stsp free(wline);
7864 6458efa5 2020-11-24 stsp wline = NULL;
7865 6458efa5 2020-11-24 stsp free(line);
7866 6458efa5 2020-11-24 stsp line = NULL;
7867 6458efa5 2020-11-24 stsp if (--limit <= 0)
7868 6458efa5 2020-11-24 stsp return NULL;
7869 6458efa5 2020-11-24 stsp
7870 6458efa5 2020-11-24 stsp n = 0;
7871 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7872 6458efa5 2020-11-24 stsp char *line = NULL;
7873 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7874 6458efa5 2020-11-24 stsp
7875 b4996bee 2022-06-16 stsp if (s->show_date) {
7876 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7877 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7878 b4996bee 2022-06-16 stsp struct got_object_id *id;
7879 b4996bee 2022-06-16 stsp struct tm tm;
7880 b4996bee 2022-06-16 stsp time_t t;
7881 b4996bee 2022-06-16 stsp
7882 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7883 b4996bee 2022-06-16 stsp if (err)
7884 b4996bee 2022-06-16 stsp return err;
7885 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7886 b4996bee 2022-06-16 stsp if (err) {
7887 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7888 b4996bee 2022-06-16 stsp free(id);
7889 b4996bee 2022-06-16 stsp return err;
7890 b4996bee 2022-06-16 stsp }
7891 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7892 b4996bee 2022-06-16 stsp id);
7893 b4996bee 2022-06-16 stsp if (err) {
7894 b4996bee 2022-06-16 stsp free(id);
7895 b4996bee 2022-06-16 stsp return err;
7896 b4996bee 2022-06-16 stsp }
7897 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7898 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7899 b4996bee 2022-06-16 stsp } else {
7900 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7901 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7902 b4996bee 2022-06-16 stsp }
7903 b4996bee 2022-06-16 stsp free(id);
7904 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7905 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7906 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7907 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7908 b4996bee 2022-06-16 stsp }
7909 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7910 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7911 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7912 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7913 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7914 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7915 6458efa5 2020-11-24 stsp struct got_object_id *id;
7916 6458efa5 2020-11-24 stsp char *id_str;
7917 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7918 6458efa5 2020-11-24 stsp if (err)
7919 6458efa5 2020-11-24 stsp return err;
7920 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7921 6458efa5 2020-11-24 stsp if (err) {
7922 6458efa5 2020-11-24 stsp free(id);
7923 6458efa5 2020-11-24 stsp return err;
7924 6458efa5 2020-11-24 stsp }
7925 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7926 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7927 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7928 6458efa5 2020-11-24 stsp free(id);
7929 6458efa5 2020-11-24 stsp free(id_str);
7930 6458efa5 2020-11-24 stsp return err;
7931 6458efa5 2020-11-24 stsp }
7932 6458efa5 2020-11-24 stsp free(id);
7933 6458efa5 2020-11-24 stsp free(id_str);
7934 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7935 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7936 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7937 6458efa5 2020-11-24 stsp
7938 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7939 ccda2f4d 2022-06-16 stsp 0, 0);
7940 6458efa5 2020-11-24 stsp if (err) {
7941 6458efa5 2020-11-24 stsp free(line);
7942 6458efa5 2020-11-24 stsp return err;
7943 6458efa5 2020-11-24 stsp }
7944 6458efa5 2020-11-24 stsp if (n == s->selected) {
7945 6458efa5 2020-11-24 stsp if (view->focussed)
7946 6458efa5 2020-11-24 stsp wstandout(view->window);
7947 6458efa5 2020-11-24 stsp s->selected_entry = re;
7948 6458efa5 2020-11-24 stsp }
7949 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7950 6458efa5 2020-11-24 stsp if (tc)
7951 6458efa5 2020-11-24 stsp wattr_on(view->window,
7952 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7953 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7954 6458efa5 2020-11-24 stsp if (tc)
7955 6458efa5 2020-11-24 stsp wattr_off(view->window,
7956 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7957 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7958 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7959 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7960 6458efa5 2020-11-24 stsp wstandend(view->window);
7961 6458efa5 2020-11-24 stsp free(line);
7962 6458efa5 2020-11-24 stsp free(wline);
7963 6458efa5 2020-11-24 stsp wline = NULL;
7964 6458efa5 2020-11-24 stsp n++;
7965 6458efa5 2020-11-24 stsp s->ndisplayed++;
7966 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7967 6458efa5 2020-11-24 stsp
7968 6458efa5 2020-11-24 stsp limit--;
7969 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7970 6458efa5 2020-11-24 stsp }
7971 6458efa5 2020-11-24 stsp
7972 9b058f45 2022-06-30 mark view_border(view);
7973 6458efa5 2020-11-24 stsp return err;
7974 6458efa5 2020-11-24 stsp }
7975 6458efa5 2020-11-24 stsp
7976 6458efa5 2020-11-24 stsp static const struct got_error *
7977 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7978 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7979 c42c9805 2020-11-24 stsp {
7980 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7981 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7982 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7983 c42c9805 2020-11-24 stsp
7984 c42c9805 2020-11-24 stsp *new_view = NULL;
7985 c42c9805 2020-11-24 stsp
7986 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7987 c42c9805 2020-11-24 stsp if (err) {
7988 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7989 c42c9805 2020-11-24 stsp return err;
7990 c42c9805 2020-11-24 stsp else
7991 c42c9805 2020-11-24 stsp return NULL;
7992 c42c9805 2020-11-24 stsp }
7993 c42c9805 2020-11-24 stsp
7994 c42c9805 2020-11-24 stsp
7995 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7996 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7997 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7998 c42c9805 2020-11-24 stsp goto done;
7999 c42c9805 2020-11-24 stsp }
8000 c42c9805 2020-11-24 stsp
8001 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8002 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8003 c42c9805 2020-11-24 stsp if (err)
8004 c42c9805 2020-11-24 stsp goto done;
8005 c42c9805 2020-11-24 stsp
8006 c42c9805 2020-11-24 stsp *new_view = tree_view;
8007 c42c9805 2020-11-24 stsp done:
8008 c42c9805 2020-11-24 stsp free(commit_id);
8009 c42c9805 2020-11-24 stsp return err;
8010 94b80cfa 2022-08-01 mark }
8011 94b80cfa 2022-08-01 mark
8012 94b80cfa 2022-08-01 mark static const struct got_error *
8013 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8014 94b80cfa 2022-08-01 mark {
8015 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8016 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8017 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8018 94b80cfa 2022-08-01 mark
8019 94b80cfa 2022-08-01 mark g = view->gline;
8020 94b80cfa 2022-08-01 mark view->gline = 0;
8021 94b80cfa 2022-08-01 mark
8022 94b80cfa 2022-08-01 mark if (g == 0)
8023 94b80cfa 2022-08-01 mark g = 1;
8024 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8025 94b80cfa 2022-08-01 mark g = s->nrefs;
8026 94b80cfa 2022-08-01 mark
8027 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8028 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8029 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8030 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8031 94b80cfa 2022-08-01 mark return NULL;
8032 94b80cfa 2022-08-01 mark }
8033 94b80cfa 2022-08-01 mark
8034 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8035 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8036 94b80cfa 2022-08-01 mark if (err)
8037 94b80cfa 2022-08-01 mark return err;
8038 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8039 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8040 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8041 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8042 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8043 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8044 94b80cfa 2022-08-01 mark
8045 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8046 94b80cfa 2022-08-01 mark s->selected = g - 1;
8047 94b80cfa 2022-08-01 mark
8048 94b80cfa 2022-08-01 mark return NULL;
8049 94b80cfa 2022-08-01 mark
8050 c42c9805 2020-11-24 stsp }
8051 94b80cfa 2022-08-01 mark
8052 c42c9805 2020-11-24 stsp static const struct got_error *
8053 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8054 6458efa5 2020-11-24 stsp {
8055 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8056 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8057 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8058 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8059 6458efa5 2020-11-24 stsp
8060 94b80cfa 2022-08-01 mark if (view->gline)
8061 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8062 94b80cfa 2022-08-01 mark
8063 6458efa5 2020-11-24 stsp switch (ch) {
8064 6458efa5 2020-11-24 stsp case 'i':
8065 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8066 640cd7ff 2022-06-22 mark view->count = 0;
8067 7f66531d 2021-11-16 stsp break;
8068 b4996bee 2022-06-16 stsp case 'm':
8069 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8070 640cd7ff 2022-06-22 mark view->count = 0;
8071 b4996bee 2022-06-16 stsp break;
8072 07a065fe 2021-11-20 stsp case 'o':
8073 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8074 640cd7ff 2022-06-22 mark view->count = 0;
8075 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8076 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8077 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8078 50617b77 2021-11-20 stsp if (err)
8079 50617b77 2021-11-20 stsp break;
8080 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8081 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8082 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8083 7f66531d 2021-11-16 stsp if (err)
8084 7f66531d 2021-11-16 stsp break;
8085 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8086 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8087 6458efa5 2020-11-24 stsp break;
8088 6458efa5 2020-11-24 stsp case KEY_ENTER:
8089 6458efa5 2020-11-24 stsp case '\r':
8090 640cd7ff 2022-06-22 mark view->count = 0;
8091 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8092 9b058f45 2022-06-30 mark break;
8093 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8094 6458efa5 2020-11-24 stsp break;
8095 5e98fb33 2022-07-22 mark case 'T':
8096 640cd7ff 2022-06-22 mark view->count = 0;
8097 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8098 c42c9805 2020-11-24 stsp break;
8099 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8100 c42c9805 2020-11-24 stsp break;
8101 e4526bf5 2021-09-03 naddy case 'g':
8102 e4526bf5 2021-09-03 naddy case KEY_HOME:
8103 e4526bf5 2021-09-03 naddy s->selected = 0;
8104 640cd7ff 2022-06-22 mark view->count = 0;
8105 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8106 e4526bf5 2021-09-03 naddy break;
8107 e4526bf5 2021-09-03 naddy case 'G':
8108 9b058f45 2022-06-30 mark case KEY_END: {
8109 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8110 9b058f45 2022-06-30 mark
8111 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8112 9b058f45 2022-06-30 mark --eos; /* border */
8113 e4526bf5 2021-09-03 naddy s->selected = 0;
8114 640cd7ff 2022-06-22 mark view->count = 0;
8115 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8116 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8117 e4526bf5 2021-09-03 naddy if (re == NULL)
8118 e4526bf5 2021-09-03 naddy break;
8119 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8120 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8121 e4526bf5 2021-09-03 naddy }
8122 e4526bf5 2021-09-03 naddy if (n > 0)
8123 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8124 e4526bf5 2021-09-03 naddy break;
8125 9b058f45 2022-06-30 mark }
8126 6458efa5 2020-11-24 stsp case 'k':
8127 6458efa5 2020-11-24 stsp case KEY_UP:
8128 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8129 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8130 6458efa5 2020-11-24 stsp s->selected--;
8131 6458efa5 2020-11-24 stsp break;
8132 34ba6917 2020-11-30 stsp }
8133 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8134 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8135 640cd7ff 2022-06-22 mark view->count = 0;
8136 6458efa5 2020-11-24 stsp break;
8137 83cc4199 2022-06-13 stsp case CTRL('u'):
8138 33c3719a 2022-06-15 stsp case 'u':
8139 83cc4199 2022-06-13 stsp nscroll /= 2;
8140 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8141 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8142 6458efa5 2020-11-24 stsp case CTRL('b'):
8143 61417565 2022-06-20 mark case 'b':
8144 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8145 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8146 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8147 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8148 640cd7ff 2022-06-22 mark view->count = 0;
8149 6458efa5 2020-11-24 stsp break;
8150 6458efa5 2020-11-24 stsp case 'j':
8151 6458efa5 2020-11-24 stsp case KEY_DOWN:
8152 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8153 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8154 6458efa5 2020-11-24 stsp s->selected++;
8155 6458efa5 2020-11-24 stsp break;
8156 6458efa5 2020-11-24 stsp }
8157 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8158 6458efa5 2020-11-24 stsp /* can't scroll any further */
8159 640cd7ff 2022-06-22 mark view->count = 0;
8160 6458efa5 2020-11-24 stsp break;
8161 640cd7ff 2022-06-22 mark }
8162 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8163 6458efa5 2020-11-24 stsp break;
8164 83cc4199 2022-06-13 stsp case CTRL('d'):
8165 33c3719a 2022-06-15 stsp case 'd':
8166 83cc4199 2022-06-13 stsp nscroll /= 2;
8167 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8168 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8169 6458efa5 2020-11-24 stsp case CTRL('f'):
8170 61417565 2022-06-20 mark case 'f':
8171 48bb96f0 2022-06-20 naddy case ' ':
8172 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8173 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8174 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8175 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8176 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8177 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8178 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8179 640cd7ff 2022-06-22 mark view->count = 0;
8180 6458efa5 2020-11-24 stsp break;
8181 6458efa5 2020-11-24 stsp }
8182 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8183 6458efa5 2020-11-24 stsp break;
8184 6458efa5 2020-11-24 stsp case CTRL('l'):
8185 640cd7ff 2022-06-22 mark view->count = 0;
8186 8924d611 2020-12-26 stsp tog_free_refs();
8187 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8188 8924d611 2020-12-26 stsp if (err)
8189 8924d611 2020-12-26 stsp break;
8190 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8191 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8192 6458efa5 2020-11-24 stsp break;
8193 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8194 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8195 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8196 6458efa5 2020-11-24 stsp break;
8197 6458efa5 2020-11-24 stsp default:
8198 640cd7ff 2022-06-22 mark view->count = 0;
8199 6458efa5 2020-11-24 stsp break;
8200 6458efa5 2020-11-24 stsp }
8201 6458efa5 2020-11-24 stsp
8202 6458efa5 2020-11-24 stsp return err;
8203 6458efa5 2020-11-24 stsp }
8204 6458efa5 2020-11-24 stsp
8205 6458efa5 2020-11-24 stsp __dead static void
8206 6458efa5 2020-11-24 stsp usage_ref(void)
8207 6458efa5 2020-11-24 stsp {
8208 6458efa5 2020-11-24 stsp endwin();
8209 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8210 6458efa5 2020-11-24 stsp getprogname());
8211 6458efa5 2020-11-24 stsp exit(1);
8212 6458efa5 2020-11-24 stsp }
8213 6458efa5 2020-11-24 stsp
8214 6458efa5 2020-11-24 stsp static const struct got_error *
8215 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8216 6458efa5 2020-11-24 stsp {
8217 6458efa5 2020-11-24 stsp const struct got_error *error;
8218 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8219 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8220 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8221 6458efa5 2020-11-24 stsp int ch;
8222 6458efa5 2020-11-24 stsp struct tog_view *view;
8223 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8224 6458efa5 2020-11-24 stsp
8225 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8226 6458efa5 2020-11-24 stsp switch (ch) {
8227 6458efa5 2020-11-24 stsp case 'r':
8228 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8229 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8230 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8231 6458efa5 2020-11-24 stsp optarg);
8232 6458efa5 2020-11-24 stsp break;
8233 6458efa5 2020-11-24 stsp default:
8234 e99e2d15 2020-11-24 naddy usage_ref();
8235 6458efa5 2020-11-24 stsp /* NOTREACHED */
8236 6458efa5 2020-11-24 stsp }
8237 6458efa5 2020-11-24 stsp }
8238 6458efa5 2020-11-24 stsp
8239 6458efa5 2020-11-24 stsp argc -= optind;
8240 6458efa5 2020-11-24 stsp argv += optind;
8241 6458efa5 2020-11-24 stsp
8242 6458efa5 2020-11-24 stsp if (argc > 1)
8243 e99e2d15 2020-11-24 naddy usage_ref();
8244 0ae84acc 2022-06-15 tracey
8245 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8246 0ae84acc 2022-06-15 tracey if (error != NULL)
8247 0ae84acc 2022-06-15 tracey goto done;
8248 6458efa5 2020-11-24 stsp
8249 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8250 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8251 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8252 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8253 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8254 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8255 c156c7a4 2020-12-18 stsp goto done;
8256 6458efa5 2020-11-24 stsp if (worktree)
8257 6458efa5 2020-11-24 stsp repo_path =
8258 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8259 6458efa5 2020-11-24 stsp else
8260 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8261 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8262 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8263 c156c7a4 2020-12-18 stsp goto done;
8264 c156c7a4 2020-12-18 stsp }
8265 6458efa5 2020-11-24 stsp }
8266 6458efa5 2020-11-24 stsp
8267 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8268 6458efa5 2020-11-24 stsp if (error != NULL)
8269 6458efa5 2020-11-24 stsp goto done;
8270 6458efa5 2020-11-24 stsp
8271 6458efa5 2020-11-24 stsp init_curses();
8272 6458efa5 2020-11-24 stsp
8273 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8274 51a10b52 2020-12-26 stsp if (error)
8275 51a10b52 2020-12-26 stsp goto done;
8276 51a10b52 2020-12-26 stsp
8277 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8278 6458efa5 2020-11-24 stsp if (error)
8279 6458efa5 2020-11-24 stsp goto done;
8280 6458efa5 2020-11-24 stsp
8281 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8282 6458efa5 2020-11-24 stsp if (view == NULL) {
8283 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8284 6458efa5 2020-11-24 stsp goto done;
8285 6458efa5 2020-11-24 stsp }
8286 6458efa5 2020-11-24 stsp
8287 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8288 6458efa5 2020-11-24 stsp if (error)
8289 6458efa5 2020-11-24 stsp goto done;
8290 6458efa5 2020-11-24 stsp
8291 6458efa5 2020-11-24 stsp if (worktree) {
8292 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8293 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8294 6458efa5 2020-11-24 stsp worktree = NULL;
8295 6458efa5 2020-11-24 stsp }
8296 6458efa5 2020-11-24 stsp error = view_loop(view);
8297 6458efa5 2020-11-24 stsp done:
8298 6458efa5 2020-11-24 stsp free(repo_path);
8299 6458efa5 2020-11-24 stsp free(cwd);
8300 1d0f4054 2021-06-17 stsp if (repo) {
8301 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8302 1d0f4054 2021-06-17 stsp if (close_err)
8303 1d0f4054 2021-06-17 stsp error = close_err;
8304 1d0f4054 2021-06-17 stsp }
8305 0ae84acc 2022-06-15 tracey if (pack_fds) {
8306 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8307 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8308 0ae84acc 2022-06-15 tracey if (error == NULL)
8309 0ae84acc 2022-06-15 tracey error = pack_err;
8310 0ae84acc 2022-06-15 tracey }
8311 51a10b52 2020-12-26 stsp tog_free_refs();
8312 6458efa5 2020-11-24 stsp return error;
8313 6458efa5 2020-11-24 stsp }
8314 6458efa5 2020-11-24 stsp
8315 136e2bd4 2022-07-23 mark static const struct got_error *
8316 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8317 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8318 136e2bd4 2022-07-23 mark {
8319 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8320 136e2bd4 2022-07-23 mark
8321 136e2bd4 2022-07-23 mark *new_view = NULL;
8322 136e2bd4 2022-07-23 mark
8323 136e2bd4 2022-07-23 mark switch (request) {
8324 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8325 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
8326 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
8327 136e2bd4 2022-07-23 mark
8328 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
8329 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
8330 136e2bd4 2022-07-23 mark view, s->repo);
8331 136e2bd4 2022-07-23 mark } else
8332 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8333 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8334 136e2bd4 2022-07-23 mark break;
8335 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
8336 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
8337 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
8338 136e2bd4 2022-07-23 mark
8339 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
8340 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
8341 136e2bd4 2022-07-23 mark s->repo);
8342 136e2bd4 2022-07-23 mark } else
8343 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8344 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8345 136e2bd4 2022-07-23 mark break;
8346 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
8347 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
8348 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
8349 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
8350 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8351 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
8352 136e2bd4 2022-07-23 mark &view->state.tree);
8353 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8354 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
8355 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8356 136e2bd4 2022-07-23 mark view->state.ref.repo);
8357 136e2bd4 2022-07-23 mark else
8358 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8359 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8360 136e2bd4 2022-07-23 mark break;
8361 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
8362 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8363 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
8364 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
8365 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
8366 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
8367 136e2bd4 2022-07-23 mark view->state.log.repo);
8368 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8369 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
8370 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8371 136e2bd4 2022-07-23 mark view->state.ref.repo);
8372 136e2bd4 2022-07-23 mark else
8373 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8374 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8375 136e2bd4 2022-07-23 mark break;
8376 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
8377 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8378 136e2bd4 2022-07-23 mark if (*new_view == NULL)
8379 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
8380 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8381 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
8382 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8383 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
8384 136e2bd4 2022-07-23 mark else
8385 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
8386 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8387 136e2bd4 2022-07-23 mark if (err)
8388 136e2bd4 2022-07-23 mark view_close(*new_view);
8389 136e2bd4 2022-07-23 mark break;
8390 136e2bd4 2022-07-23 mark default:
8391 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8392 136e2bd4 2022-07-23 mark }
8393 136e2bd4 2022-07-23 mark
8394 136e2bd4 2022-07-23 mark return err;
8395 136e2bd4 2022-07-23 mark }
8396 136e2bd4 2022-07-23 mark
8397 9b058f45 2022-06-30 mark /*
8398 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8399 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8400 9b058f45 2022-06-30 mark */
8401 6458efa5 2020-11-24 stsp static void
8402 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8403 9b058f45 2022-06-30 mark {
8404 9b058f45 2022-06-30 mark switch (view->type) {
8405 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8406 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8407 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8408 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8409 9b058f45 2022-06-30 mark 1);
8410 9b058f45 2022-06-30 mark break;
8411 9b058f45 2022-06-30 mark }
8412 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8413 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8414 9b058f45 2022-06-30 mark else
8415 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8416 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8417 9b058f45 2022-06-30 mark break;
8418 9b058f45 2022-06-30 mark }
8419 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8420 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8421 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8422 9b058f45 2022-06-30 mark break;
8423 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8424 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8425 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8426 9b058f45 2022-06-30 mark break;
8427 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8428 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8429 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8430 9b058f45 2022-06-30 mark break;
8431 9b058f45 2022-06-30 mark default:
8432 9b058f45 2022-06-30 mark break;
8433 9b058f45 2022-06-30 mark }
8434 9b058f45 2022-06-30 mark
8435 9b058f45 2022-06-30 mark view->offset = 0;
8436 9b058f45 2022-06-30 mark }
8437 9b058f45 2022-06-30 mark
8438 9b058f45 2022-06-30 mark /*
8439 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8440 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8441 9b058f45 2022-06-30 mark */
8442 9b058f45 2022-06-30 mark static const struct got_error *
8443 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8444 9b058f45 2022-06-30 mark {
8445 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8446 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8447 9b058f45 2022-06-30 mark int *selected = NULL;
8448 9b058f45 2022-06-30 mark int header, offset;
8449 9b058f45 2022-06-30 mark
8450 9b058f45 2022-06-30 mark switch (view->type) {
8451 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8452 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8453 9b058f45 2022-06-30 mark header = 3;
8454 9b058f45 2022-06-30 mark scrolld = NULL;
8455 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8456 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8457 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8458 9b058f45 2022-06-30 mark s->selected_line -= offset;
8459 9b058f45 2022-06-30 mark view->offset = offset;
8460 9b058f45 2022-06-30 mark }
8461 9b058f45 2022-06-30 mark break;
8462 9b058f45 2022-06-30 mark }
8463 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8464 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8465 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8466 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8467 9b058f45 2022-06-30 mark selected = &s->selected;
8468 9b058f45 2022-06-30 mark break;
8469 9b058f45 2022-06-30 mark }
8470 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8471 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8472 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8473 9b058f45 2022-06-30 mark header = 3;
8474 9b058f45 2022-06-30 mark selected = &s->selected;
8475 9b058f45 2022-06-30 mark break;
8476 9b058f45 2022-06-30 mark }
8477 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8478 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8479 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8480 9b058f45 2022-06-30 mark header = 5;
8481 9b058f45 2022-06-30 mark selected = &s->selected;
8482 9b058f45 2022-06-30 mark break;
8483 9b058f45 2022-06-30 mark }
8484 9b058f45 2022-06-30 mark default:
8485 9b058f45 2022-06-30 mark selected = NULL;
8486 9b058f45 2022-06-30 mark scrolld = NULL;
8487 9b058f45 2022-06-30 mark header = 0;
8488 9b058f45 2022-06-30 mark break;
8489 9b058f45 2022-06-30 mark }
8490 9b058f45 2022-06-30 mark
8491 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8492 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8493 9b058f45 2022-06-30 mark view->offset = offset;
8494 9b058f45 2022-06-30 mark if (scrolld && offset) {
8495 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8496 9b058f45 2022-06-30 mark *selected -= offset;
8497 9b058f45 2022-06-30 mark }
8498 9b058f45 2022-06-30 mark }
8499 9b058f45 2022-06-30 mark
8500 9b058f45 2022-06-30 mark return err;
8501 9b058f45 2022-06-30 mark }
8502 9b058f45 2022-06-30 mark
8503 9b058f45 2022-06-30 mark static void
8504 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8505 ce5b7c56 2019-07-09 stsp {
8506 6059809a 2020-12-17 stsp size_t i;
8507 9f7d7167 2018-04-29 stsp
8508 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8509 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8510 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8511 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8512 ce5b7c56 2019-07-09 stsp }
8513 6879ba42 2020-10-01 naddy fputc('\n', fp);
8514 ce5b7c56 2019-07-09 stsp }
8515 ce5b7c56 2019-07-09 stsp
8516 4ed7e80c 2018-05-20 stsp __dead static void
8517 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8518 9f7d7167 2018-04-29 stsp {
8519 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8520 6879ba42 2020-10-01 naddy
8521 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8522 6879ba42 2020-10-01 naddy getprogname());
8523 6879ba42 2020-10-01 naddy if (hflag) {
8524 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8525 6879ba42 2020-10-01 naddy list_commands(fp);
8526 ee85c5e8 2020-02-29 stsp }
8527 6879ba42 2020-10-01 naddy exit(status);
8528 9f7d7167 2018-04-29 stsp }
8529 9f7d7167 2018-04-29 stsp
8530 c2301be8 2018-04-30 stsp static char **
8531 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8532 c2301be8 2018-04-30 stsp {
8533 ee85c5e8 2020-02-29 stsp va_list ap;
8534 c2301be8 2018-04-30 stsp char **argv;
8535 ee85c5e8 2020-02-29 stsp int i;
8536 c2301be8 2018-04-30 stsp
8537 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8538 ee85c5e8 2020-02-29 stsp
8539 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8540 c2301be8 2018-04-30 stsp if (argv == NULL)
8541 c2301be8 2018-04-30 stsp err(1, "calloc");
8542 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8543 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8544 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8545 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8546 c2301be8 2018-04-30 stsp }
8547 c2301be8 2018-04-30 stsp
8548 ee85c5e8 2020-02-29 stsp va_end(ap);
8549 c2301be8 2018-04-30 stsp return argv;
8550 ee85c5e8 2020-02-29 stsp }
8551 ee85c5e8 2020-02-29 stsp
8552 ee85c5e8 2020-02-29 stsp /*
8553 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8554 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8555 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8556 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8557 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8558 ee85c5e8 2020-02-29 stsp */
8559 ee85c5e8 2020-02-29 stsp static const struct got_error *
8560 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8561 ee85c5e8 2020-02-29 stsp {
8562 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8563 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8564 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8565 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8566 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8567 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8568 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8569 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8570 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8571 84de9106 2020-12-26 stsp
8572 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8573 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8574 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8575 ee85c5e8 2020-02-29 stsp
8576 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8577 0ae84acc 2022-06-15 tracey if (error != NULL)
8578 0ae84acc 2022-06-15 tracey goto done;
8579 0ae84acc 2022-06-15 tracey
8580 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8581 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8582 ee85c5e8 2020-02-29 stsp goto done;
8583 ee85c5e8 2020-02-29 stsp
8584 ee85c5e8 2020-02-29 stsp if (worktree)
8585 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8586 ee85c5e8 2020-02-29 stsp else
8587 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8588 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8589 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8590 ee85c5e8 2020-02-29 stsp goto done;
8591 ee85c5e8 2020-02-29 stsp }
8592 ee85c5e8 2020-02-29 stsp
8593 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8594 ee85c5e8 2020-02-29 stsp if (error != NULL)
8595 ee85c5e8 2020-02-29 stsp goto done;
8596 ee85c5e8 2020-02-29 stsp
8597 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8598 ee85c5e8 2020-02-29 stsp repo, worktree);
8599 ee85c5e8 2020-02-29 stsp if (error)
8600 ee85c5e8 2020-02-29 stsp goto done;
8601 ee85c5e8 2020-02-29 stsp
8602 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8603 84de9106 2020-12-26 stsp if (error)
8604 84de9106 2020-12-26 stsp goto done;
8605 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8606 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8607 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8608 ee85c5e8 2020-02-29 stsp if (error)
8609 ee85c5e8 2020-02-29 stsp goto done;
8610 ee85c5e8 2020-02-29 stsp
8611 ee85c5e8 2020-02-29 stsp if (worktree) {
8612 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8613 ee85c5e8 2020-02-29 stsp worktree = NULL;
8614 ee85c5e8 2020-02-29 stsp }
8615 ee85c5e8 2020-02-29 stsp
8616 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8617 a44927cc 2022-04-07 stsp if (error)
8618 a44927cc 2022-04-07 stsp goto done;
8619 a44927cc 2022-04-07 stsp
8620 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8621 ee85c5e8 2020-02-29 stsp if (error) {
8622 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8623 ee85c5e8 2020-02-29 stsp goto done;
8624 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8625 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8626 6879ba42 2020-10-01 naddy usage(1, 1);
8627 ee85c5e8 2020-02-29 stsp /* not reached */
8628 ee85c5e8 2020-02-29 stsp }
8629 ee85c5e8 2020-02-29 stsp
8630 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8631 ee85c5e8 2020-02-29 stsp if (error)
8632 ee85c5e8 2020-02-29 stsp goto done;
8633 ee85c5e8 2020-02-29 stsp
8634 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8635 ee85c5e8 2020-02-29 stsp argc = 4;
8636 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8637 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8638 ee85c5e8 2020-02-29 stsp done:
8639 1d0f4054 2021-06-17 stsp if (repo) {
8640 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8641 1d0f4054 2021-06-17 stsp if (error == NULL)
8642 1d0f4054 2021-06-17 stsp error = close_err;
8643 1d0f4054 2021-06-17 stsp }
8644 a44927cc 2022-04-07 stsp if (commit)
8645 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8646 ee85c5e8 2020-02-29 stsp if (worktree)
8647 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8648 0ae84acc 2022-06-15 tracey if (pack_fds) {
8649 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8650 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8651 0ae84acc 2022-06-15 tracey if (error == NULL)
8652 0ae84acc 2022-06-15 tracey error = pack_err;
8653 0ae84acc 2022-06-15 tracey }
8654 ee85c5e8 2020-02-29 stsp free(id);
8655 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8656 ee85c5e8 2020-02-29 stsp free(commit_id);
8657 ee85c5e8 2020-02-29 stsp free(cwd);
8658 ee85c5e8 2020-02-29 stsp free(repo_path);
8659 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8660 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8661 ee85c5e8 2020-02-29 stsp int i;
8662 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8663 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8664 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8665 ee85c5e8 2020-02-29 stsp }
8666 87670572 2020-12-26 naddy tog_free_refs();
8667 ee85c5e8 2020-02-29 stsp return error;
8668 c2301be8 2018-04-30 stsp }
8669 c2301be8 2018-04-30 stsp
8670 9f7d7167 2018-04-29 stsp int
8671 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8672 9f7d7167 2018-04-29 stsp {
8673 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8674 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8675 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8676 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8677 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8678 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8679 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8680 83cd27f8 2020-01-13 stsp };
8681 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8682 9f7d7167 2018-04-29 stsp
8683 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
8684 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
8685 3264c09c 2022-09-06 mark
8686 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8687 9f7d7167 2018-04-29 stsp
8688 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8689 9f7d7167 2018-04-29 stsp switch (ch) {
8690 9f7d7167 2018-04-29 stsp case 'h':
8691 9f7d7167 2018-04-29 stsp hflag = 1;
8692 9f7d7167 2018-04-29 stsp break;
8693 53ccebc2 2019-07-30 stsp case 'V':
8694 53ccebc2 2019-07-30 stsp Vflag = 1;
8695 53ccebc2 2019-07-30 stsp break;
8696 9f7d7167 2018-04-29 stsp default:
8697 6879ba42 2020-10-01 naddy usage(hflag, 1);
8698 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8699 9f7d7167 2018-04-29 stsp }
8700 9f7d7167 2018-04-29 stsp }
8701 9f7d7167 2018-04-29 stsp
8702 9f7d7167 2018-04-29 stsp argc -= optind;
8703 9f7d7167 2018-04-29 stsp argv += optind;
8704 9814e6a3 2020-09-27 naddy optind = 1;
8705 c2301be8 2018-04-30 stsp optreset = 1;
8706 9f7d7167 2018-04-29 stsp
8707 53ccebc2 2019-07-30 stsp if (Vflag) {
8708 53ccebc2 2019-07-30 stsp got_version_print_str();
8709 6879ba42 2020-10-01 naddy return 0;
8710 53ccebc2 2019-07-30 stsp }
8711 4010e238 2020-12-04 stsp
8712 4010e238 2020-12-04 stsp #ifndef PROFILE
8713 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8714 4010e238 2020-12-04 stsp NULL) == -1)
8715 4010e238 2020-12-04 stsp err(1, "pledge");
8716 4010e238 2020-12-04 stsp #endif
8717 53ccebc2 2019-07-30 stsp
8718 c2301be8 2018-04-30 stsp if (argc == 0) {
8719 f29d3e89 2018-06-23 stsp if (hflag)
8720 6879ba42 2020-10-01 naddy usage(hflag, 0);
8721 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8722 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8723 c2301be8 2018-04-30 stsp argc = 1;
8724 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8725 c2301be8 2018-04-30 stsp } else {
8726 6059809a 2020-12-17 stsp size_t i;
8727 9f7d7167 2018-04-29 stsp
8728 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8729 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8730 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8731 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8732 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8733 9f7d7167 2018-04-29 stsp break;
8734 9f7d7167 2018-04-29 stsp }
8735 9f7d7167 2018-04-29 stsp }
8736 ee85c5e8 2020-02-29 stsp }
8737 3642c4c6 2019-07-09 stsp
8738 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8739 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8740 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8741 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8742 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8743 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8744 917d79a7 2022-07-01 stsp }
8745 917d79a7 2022-07-01 stsp
8746 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8747 ee85c5e8 2020-02-29 stsp if (argc != 1)
8748 6879ba42 2020-10-01 naddy usage(0, 1);
8749 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8750 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8751 ee85c5e8 2020-02-29 stsp } else {
8752 ee85c5e8 2020-02-29 stsp if (hflag)
8753 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8754 ee85c5e8 2020-02-29 stsp else
8755 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8756 9f7d7167 2018-04-29 stsp }
8757 9f7d7167 2018-04-29 stsp
8758 9f7d7167 2018-04-29 stsp endwin();
8759 b46c1e04 2020-09-20 naddy putchar('\n');
8760 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8761 a2f4a359 2020-02-28 stsp int i;
8762 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8763 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8764 a2f4a359 2020-02-28 stsp free(cmd_argv);
8765 a2f4a359 2020-02-28 stsp }
8766 a2f4a359 2020-02-28 stsp
8767 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8768 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8769 9f7d7167 2018-04-29 stsp return 0;
8770 9f7d7167 2018-04-29 stsp }