Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
112 9b058f45 2022-06-30 mark };
113 9b058f45 2022-06-30 mark
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
115 9b058f45 2022-06-30 mark
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 d6b05b5b 2018-08-04 stsp
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
122 1a76625f 2018-10-22 stsp int idx;
123 ba4f502b 2018-08-04 stsp };
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
128 6d17833f 2019-11-08 stsp };
129 6d17833f 2019-11-08 stsp
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
134 15a087fe 2019-02-21 stsp };
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 11b20872 2019-11-08 stsp
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
140 cc488aa7 2022-01-23 stsp
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
144 cc488aa7 2022-01-23 stsp {
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
148 cc488aa7 2022-01-23 stsp
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
153 cc488aa7 2022-01-23 stsp *cmp = -1;
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
156 cc488aa7 2022-01-23 stsp *cmp = 1;
157 cc488aa7 2022-01-23 stsp return NULL;
158 cc488aa7 2022-01-23 stsp }
159 cc488aa7 2022-01-23 stsp
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
162 cc488aa7 2022-01-23 stsp }
163 51a10b52 2020-12-26 stsp
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp {
167 51a10b52 2020-12-26 stsp const struct got_error *err;
168 51a10b52 2020-12-26 stsp
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 7f66531d 2021-11-16 stsp repo);
172 51a10b52 2020-12-26 stsp if (err)
173 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 51a10b52 2020-12-26 stsp repo);
177 51a10b52 2020-12-26 stsp }
178 51a10b52 2020-12-26 stsp
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp {
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
187 51a10b52 2020-12-26 stsp }
188 51a10b52 2020-12-26 stsp
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp {
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp free(tc);
216 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp }
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
221 11b20872 2019-11-08 stsp }
222 11b20872 2019-11-08 stsp
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp {
226 11b20872 2019-11-08 stsp struct tog_color *tc;
227 11b20872 2019-11-08 stsp
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
232 11b20872 2019-11-08 stsp free(tc);
233 11b20872 2019-11-08 stsp }
234 11b20872 2019-11-08 stsp }
235 11b20872 2019-11-08 stsp
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp {
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 11b20872 2019-11-08 stsp
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
243 11b20872 2019-11-08 stsp return tc;
244 11b20872 2019-11-08 stsp }
245 11b20872 2019-11-08 stsp
246 11b20872 2019-11-08 stsp return NULL;
247 11b20872 2019-11-08 stsp }
248 11b20872 2019-11-08 stsp
249 11b20872 2019-11-08 stsp static int
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp {
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
282 11b20872 2019-11-08 stsp
283 11b20872 2019-11-08 stsp return -1;
284 11b20872 2019-11-08 stsp }
285 11b20872 2019-11-08 stsp
286 11b20872 2019-11-08 stsp static int
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp {
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 15a087fe 2019-02-21 stsp
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
311 11b20872 2019-11-08 stsp return -1;
312 11b20872 2019-11-08 stsp
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
314 11b20872 2019-11-08 stsp }
315 11b20872 2019-11-08 stsp
316 11b20872 2019-11-08 stsp
317 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
318 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
319 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
321 f9d37699 2022-06-28 stsp int fd1, fd2;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
324 15a087fe 2019-02-21 stsp int eof;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 bddb1296 2019-11-08 stsp struct tog_colors colors;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey off_t *line_offsets;
332 f44b1f58 2020-02-02 tracey int matched_line;
333 f44b1f58 2020-02-02 tracey int selected_line;
334 15a087fe 2019-02-21 stsp
335 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
336 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
337 b01e7d3b 2018-08-04 stsp };
338 b01e7d3b 2018-08-04 stsp
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 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 1a76625f 2018-10-22 stsp struct commit_queue *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 1a76625f 2018-10-22 stsp };
360 1a76625f 2018-10-22 stsp
361 1a76625f 2018-10-22 stsp struct tog_log_view_state {
362 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
366 b01e7d3b 2018-08-04 stsp int selected;
367 b01e7d3b 2018-08-04 stsp char *in_repo_path;
368 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
369 b672a97a 2020-01-27 stsp int log_branches;
370 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
371 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
372 1a76625f 2018-10-22 stsp sig_atomic_t quit;
373 1a76625f 2018-10-22 stsp pthread_t thread;
374 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
375 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
376 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
377 11b20872 2019-11-08 stsp struct tog_colors colors;
378 ba4f502b 2018-08-04 stsp };
379 11b20872 2019-11-08 stsp
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
388 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
389 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
390 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
391 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
394 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
395 ba4f502b 2018-08-04 stsp
396 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
397 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
398 e9424729 2018-08-04 stsp int nlines;
399 e9424729 2018-08-04 stsp
400 e9424729 2018-08-04 stsp struct tog_view *view;
401 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
402 e9424729 2018-08-04 stsp int *quit;
403 e9424729 2018-08-04 stsp };
404 e9424729 2018-08-04 stsp
405 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
406 e9424729 2018-08-04 stsp const char *path;
407 e9424729 2018-08-04 stsp struct got_repository *repo;
408 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
409 e9424729 2018-08-04 stsp int *complete;
410 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
411 fc06ba56 2019-08-22 stsp void *cancel_arg;
412 e9424729 2018-08-04 stsp };
413 e9424729 2018-08-04 stsp
414 e9424729 2018-08-04 stsp struct tog_blame {
415 e9424729 2018-08-04 stsp FILE *f;
416 be659d10 2020-11-18 stsp off_t filesize;
417 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
418 6fcac457 2018-11-19 stsp int nlines;
419 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
420 e9424729 2018-08-04 stsp pthread_t thread;
421 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
422 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
423 e9424729 2018-08-04 stsp const char *path;
424 0ae84acc 2022-06-15 tracey int *pack_fds;
425 e9424729 2018-08-04 stsp };
426 e9424729 2018-08-04 stsp
427 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
428 7cbe629d 2018-08-04 stsp int first_displayed_line;
429 7cbe629d 2018-08-04 stsp int last_displayed_line;
430 7cbe629d 2018-08-04 stsp int selected_line;
431 7cbe629d 2018-08-04 stsp int blame_complete;
432 e5a0f69f 2018-08-18 stsp int eof;
433 e5a0f69f 2018-08-18 stsp int done;
434 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
435 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
436 e5a0f69f 2018-08-18 stsp char *path;
437 7cbe629d 2018-08-04 stsp struct got_repository *repo;
438 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
439 e9424729 2018-08-04 stsp struct tog_blame blame;
440 6c4c42e0 2019-06-24 stsp int matched_line;
441 11b20872 2019-11-08 stsp struct tog_colors colors;
442 ad80ab7b 2018-08-04 stsp };
443 ad80ab7b 2018-08-04 stsp
444 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
445 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
446 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
448 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
449 ad80ab7b 2018-08-04 stsp int selected;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
453 ad80ab7b 2018-08-04 stsp
454 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
455 ad80ab7b 2018-08-04 stsp char *tree_label;
456 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
457 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
458 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
460 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
461 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
462 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
463 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
464 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
465 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
466 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
467 6458efa5 2020-11-24 stsp struct tog_colors colors;
468 6458efa5 2020-11-24 stsp };
469 6458efa5 2020-11-24 stsp
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
471 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
472 6458efa5 2020-11-24 stsp struct got_reference *ref;
473 6458efa5 2020-11-24 stsp int idx;
474 6458efa5 2020-11-24 stsp };
475 6458efa5 2020-11-24 stsp
476 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
479 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
483 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
484 6458efa5 2020-11-24 stsp struct got_repository *repo;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
486 bddb1296 2019-11-08 stsp struct tog_colors colors;
487 7cbe629d 2018-08-04 stsp };
488 7cbe629d 2018-08-04 stsp
489 669b5ffa 2018-10-07 stsp /*
490 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
491 669b5ffa 2018-10-07 stsp *
492 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
493 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
494 669b5ffa 2018-10-07 stsp * there is enough screen estate.
495 669b5ffa 2018-10-07 stsp *
496 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
497 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
498 669b5ffa 2018-10-07 stsp *
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
501 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
502 669b5ffa 2018-10-07 stsp *
503 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
504 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
505 669b5ffa 2018-10-07 stsp */
506 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
507 669b5ffa 2018-10-07 stsp
508 cc3c9aac 2018-08-01 stsp struct tog_view {
509 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
510 26ed57b2 2018-05-19 stsp WINDOW *window;
511 26ed57b2 2018-05-19 stsp PANEL *panel;
512 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
513 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
514 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
515 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
516 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
517 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
518 9970f7fc 2020-12-03 stsp int dying;
519 669b5ffa 2018-10-07 stsp struct tog_view *parent;
520 669b5ffa 2018-10-07 stsp struct tog_view *child;
521 5dc9f4bc 2018-08-04 stsp
522 e78dc838 2020-12-04 stsp /*
523 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
524 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
525 e78dc838 2020-12-04 stsp * between parent and child.
526 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
527 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
528 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
529 e78dc838 2020-12-04 stsp * situations.
530 e78dc838 2020-12-04 stsp */
531 e78dc838 2020-12-04 stsp int focus_child;
532 e78dc838 2020-12-04 stsp
533 9b058f45 2022-06-30 mark enum tog_view_mode mode;
534 5dc9f4bc 2018-08-04 stsp /* type-specific state */
535 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
536 5dc9f4bc 2018-08-04 stsp union {
537 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
538 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
539 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
540 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
541 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
542 5dc9f4bc 2018-08-04 stsp } state;
543 e5a0f69f 2018-08-18 stsp
544 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
545 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
546 e78dc838 2020-12-04 stsp struct tog_view *, int);
547 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
548 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
549 60493ae3 2019-06-20 stsp
550 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
551 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
552 c0c4acc8 2021-01-24 stsp int search_started;
553 60493ae3 2019-06-20 stsp int searching;
554 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
555 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
556 60493ae3 2019-06-20 stsp int search_next_done;
557 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
558 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
559 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
560 1803e47f 2019-06-22 stsp regex_t regex;
561 41605754 2020-11-12 stsp regmatch_t regmatch;
562 cc3c9aac 2018-08-01 stsp };
563 cd0acaa7 2018-05-20 stsp
564 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
565 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
566 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
567 78756c87 2020-11-24 stsp struct got_repository *);
568 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
572 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
573 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
574 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
577 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
578 78756c87 2020-11-24 stsp const char *, const char *, int);
579 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
583 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
584 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
585 e5a0f69f 2018-08-18 stsp
586 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
587 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
588 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
593 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
594 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp
596 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
597 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
598 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
600 e78dc838 2020-12-04 stsp struct tog_view *, int);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
602 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
603 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
604 6458efa5 2020-11-24 stsp
605 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
606 6458efa5 2020-11-24 stsp struct got_repository *);
607 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
609 e78dc838 2020-12-04 stsp struct tog_view *, int);
610 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
611 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
613 25791caa 2018-10-24 stsp
614 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
615 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
616 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
617 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
618 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
619 25791caa 2018-10-24 stsp
620 25791caa 2018-10-24 stsp static void
621 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
622 25791caa 2018-10-24 stsp {
623 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
624 83baff54 2019-08-12 stsp }
625 83baff54 2019-08-12 stsp
626 83baff54 2019-08-12 stsp static void
627 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
628 83baff54 2019-08-12 stsp {
629 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
630 25791caa 2018-10-24 stsp }
631 26ed57b2 2018-05-19 stsp
632 61266923 2020-01-14 stsp static void
633 61266923 2020-01-14 stsp tog_sigcont(int signo)
634 61266923 2020-01-14 stsp {
635 61266923 2020-01-14 stsp tog_sigcont_received = 1;
636 61266923 2020-01-14 stsp }
637 61266923 2020-01-14 stsp
638 2497f032 2022-05-31 stsp static void
639 2497f032 2022-05-31 stsp tog_sigint(int signo)
640 2497f032 2022-05-31 stsp {
641 2497f032 2022-05-31 stsp tog_sigint_received = 1;
642 2497f032 2022-05-31 stsp }
643 2497f032 2022-05-31 stsp
644 2497f032 2022-05-31 stsp static void
645 2497f032 2022-05-31 stsp tog_sigterm(int signo)
646 2497f032 2022-05-31 stsp {
647 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
648 2497f032 2022-05-31 stsp }
649 2497f032 2022-05-31 stsp
650 2497f032 2022-05-31 stsp static int
651 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
652 2497f032 2022-05-31 stsp {
653 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
654 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
655 2497f032 2022-05-31 stsp }
656 2497f032 2022-05-31 stsp
657 e5a0f69f 2018-08-18 stsp static const struct got_error *
658 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
659 ea5e7bb5 2018-08-01 stsp {
660 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
661 e5a0f69f 2018-08-18 stsp
662 669b5ffa 2018-10-07 stsp if (view->child) {
663 669b5ffa 2018-10-07 stsp view_close(view->child);
664 669b5ffa 2018-10-07 stsp view->child = NULL;
665 669b5ffa 2018-10-07 stsp }
666 e5a0f69f 2018-08-18 stsp if (view->close)
667 e5a0f69f 2018-08-18 stsp err = view->close(view);
668 ea5e7bb5 2018-08-01 stsp if (view->panel)
669 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
670 ea5e7bb5 2018-08-01 stsp if (view->window)
671 ea5e7bb5 2018-08-01 stsp delwin(view->window);
672 ea5e7bb5 2018-08-01 stsp free(view);
673 e5a0f69f 2018-08-18 stsp return err;
674 ea5e7bb5 2018-08-01 stsp }
675 ea5e7bb5 2018-08-01 stsp
676 ea5e7bb5 2018-08-01 stsp static struct tog_view *
677 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
678 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
679 ea5e7bb5 2018-08-01 stsp {
680 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
681 ea5e7bb5 2018-08-01 stsp
682 ea5e7bb5 2018-08-01 stsp if (view == NULL)
683 ea5e7bb5 2018-08-01 stsp return NULL;
684 ea5e7bb5 2018-08-01 stsp
685 d6b05b5b 2018-08-04 stsp view->type = type;
686 f7d12f7e 2018-08-01 stsp view->lines = LINES;
687 f7d12f7e 2018-08-01 stsp view->cols = COLS;
688 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
689 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
690 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
691 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
692 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
693 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
694 96a765a8 2018-08-04 stsp view_close(view);
695 ea5e7bb5 2018-08-01 stsp return NULL;
696 ea5e7bb5 2018-08-01 stsp }
697 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
698 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
699 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
700 96a765a8 2018-08-04 stsp view_close(view);
701 ea5e7bb5 2018-08-01 stsp return NULL;
702 ea5e7bb5 2018-08-01 stsp }
703 ea5e7bb5 2018-08-01 stsp
704 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
705 ea5e7bb5 2018-08-01 stsp return view;
706 cdf1ee82 2018-08-01 stsp }
707 cdf1ee82 2018-08-01 stsp
708 0cf4efb1 2018-09-29 stsp static int
709 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
710 0cf4efb1 2018-09-29 stsp {
711 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
712 0cf4efb1 2018-09-29 stsp return 0;
713 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
714 5c60c32a 2018-10-18 stsp }
715 5c60c32a 2018-10-18 stsp
716 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
717 9b058f45 2022-06-30 mark static int
718 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
719 9b058f45 2022-06-30 mark {
720 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
721 9b058f45 2022-06-30 mark }
722 9b058f45 2022-06-30 mark
723 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
724 5c60c32a 2018-10-18 stsp
725 5c60c32a 2018-10-18 stsp static const struct got_error *
726 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
727 5c60c32a 2018-10-18 stsp {
728 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
729 5c60c32a 2018-10-18 stsp
730 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
731 9b058f45 2022-06-30 mark view->begin_y = view_split_begin_y(view->nlines);
732 9b058f45 2022-06-30 mark view->begin_x = 0;
733 9b058f45 2022-06-30 mark } else {
734 9b058f45 2022-06-30 mark view->begin_x = view_split_begin_x(0);
735 9b058f45 2022-06-30 mark view->begin_y = 0;
736 9b058f45 2022-06-30 mark }
737 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
738 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
739 5c60c32a 2018-10-18 stsp view->lines = LINES;
740 5c60c32a 2018-10-18 stsp view->cols = COLS;
741 5c60c32a 2018-10-18 stsp err = view_resize(view);
742 5c60c32a 2018-10-18 stsp if (err)
743 5c60c32a 2018-10-18 stsp return err;
744 5c60c32a 2018-10-18 stsp
745 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
746 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
747 9b058f45 2022-06-30 mark
748 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
749 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
750 5c60c32a 2018-10-18 stsp
751 5c60c32a 2018-10-18 stsp return NULL;
752 5c60c32a 2018-10-18 stsp }
753 5c60c32a 2018-10-18 stsp
754 5c60c32a 2018-10-18 stsp static const struct got_error *
755 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
756 5c60c32a 2018-10-18 stsp {
757 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
758 5c60c32a 2018-10-18 stsp
759 5c60c32a 2018-10-18 stsp view->begin_x = 0;
760 5c60c32a 2018-10-18 stsp view->begin_y = 0;
761 5c60c32a 2018-10-18 stsp view->nlines = LINES;
762 5c60c32a 2018-10-18 stsp view->ncols = COLS;
763 5c60c32a 2018-10-18 stsp view->lines = LINES;
764 5c60c32a 2018-10-18 stsp view->cols = COLS;
765 5c60c32a 2018-10-18 stsp err = view_resize(view);
766 5c60c32a 2018-10-18 stsp if (err)
767 5c60c32a 2018-10-18 stsp return err;
768 5c60c32a 2018-10-18 stsp
769 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
770 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
771 5c60c32a 2018-10-18 stsp
772 5c60c32a 2018-10-18 stsp return NULL;
773 0cf4efb1 2018-09-29 stsp }
774 0cf4efb1 2018-09-29 stsp
775 5c60c32a 2018-10-18 stsp static int
776 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
777 5c60c32a 2018-10-18 stsp {
778 5c60c32a 2018-10-18 stsp return view->parent == NULL;
779 5c60c32a 2018-10-18 stsp }
780 5c60c32a 2018-10-18 stsp
781 6131ff18 2022-06-20 mark static int
782 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
783 6131ff18 2022-06-20 mark {
784 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
785 24b9cfdc 2022-06-30 stsp }
786 24b9cfdc 2022-06-30 stsp
787 24b9cfdc 2022-06-30 stsp static int
788 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
789 24b9cfdc 2022-06-30 stsp {
790 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
791 6131ff18 2022-06-20 mark }
792 6131ff18 2022-06-20 mark
793 49b24ee5 2022-07-03 mark static int
794 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
795 49b24ee5 2022-07-03 mark {
796 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
797 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
798 49b24ee5 2022-07-03 mark }
799 49b24ee5 2022-07-03 mark
800 9b058f45 2022-06-30 mark static void
801 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
802 9b058f45 2022-06-30 mark {
803 9b058f45 2022-06-30 mark PANEL *panel;
804 9b058f45 2022-06-30 mark const struct tog_view *view_above;
805 6131ff18 2022-06-20 mark
806 9b058f45 2022-06-30 mark if (view->parent)
807 9b058f45 2022-06-30 mark return view_border(view->parent);
808 9b058f45 2022-06-30 mark
809 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
810 9b058f45 2022-06-30 mark if (panel == NULL)
811 9b058f45 2022-06-30 mark return;
812 9b058f45 2022-06-30 mark
813 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
814 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
815 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
816 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
817 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
818 9b058f45 2022-06-30 mark else
819 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
820 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
821 9b058f45 2022-06-30 mark }
822 9b058f45 2022-06-30 mark
823 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
824 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
825 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
826 9b058f45 2022-06-30 mark
827 4d8c2215 2018-08-19 stsp static const struct got_error *
828 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
829 f7d12f7e 2018-08-01 stsp {
830 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
831 9b058f45 2022-06-30 mark int dif, nlines, ncols;
832 f7d12f7e 2018-08-01 stsp
833 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
834 9b058f45 2022-06-30 mark
835 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
836 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
837 0cf4efb1 2018-09-29 stsp else
838 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
839 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
840 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
841 0cf4efb1 2018-09-29 stsp else
842 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
843 6d0fee91 2018-08-01 stsp
844 4dd27a72 2022-06-29 stsp if (view->child) {
845 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
846 9b058f45 2022-06-30 mark
847 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
848 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
849 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
850 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
851 0dbbbe90 2022-06-17 op ncols = COLS;
852 0dbbbe90 2022-06-17 op
853 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
854 5c60c32a 2018-10-18 stsp if (view->child->focussed)
855 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
856 5c60c32a 2018-10-18 stsp else
857 5c60c32a 2018-10-18 stsp show_panel(view->panel);
858 5c60c32a 2018-10-18 stsp } else {
859 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
860 0dbbbe90 2022-06-17 op
861 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
862 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
863 5c60c32a 2018-10-18 stsp }
864 9b058f45 2022-06-30 mark /*
865 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
866 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
867 9b058f45 2022-06-30 mark */
868 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
869 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
870 9b058f45 2022-06-30 mark
871 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
872 9b058f45 2022-06-30 mark view->lines - ts->selected) {
873 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
874 9b058f45 2022-06-30 mark view->lines - ts->selected -
875 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
876 9b058f45 2022-06-30 mark err = request_log_commits(view);
877 9b058f45 2022-06-30 mark if (err)
878 9b058f45 2022-06-30 mark return err;
879 9b058f45 2022-06-30 mark }
880 9b058f45 2022-06-30 mark }
881 9b058f45 2022-06-30 mark
882 9b058f45 2022-06-30 mark /*
883 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
884 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
885 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
886 9b058f45 2022-06-30 mark */
887 9b058f45 2022-06-30 mark if (hs) {
888 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
889 9b058f45 2022-06-30 mark if (err)
890 9b058f45 2022-06-30 mark return err;
891 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
892 9b058f45 2022-06-30 mark err = offset_selection_down(view);
893 9b058f45 2022-06-30 mark if (err)
894 9b058f45 2022-06-30 mark return err;
895 9b058f45 2022-06-30 mark }
896 9b058f45 2022-06-30 mark view_border(view);
897 9b058f45 2022-06-30 mark update_panels();
898 9b058f45 2022-06-30 mark doupdate();
899 9b058f45 2022-06-30 mark show_panel(view->child->panel);
900 9b058f45 2022-06-30 mark nlines = view->nlines;
901 9b058f45 2022-06-30 mark }
902 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
903 0dbbbe90 2022-06-17 op ncols = COLS;
904 669b5ffa 2018-10-07 stsp
905 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
906 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
907 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
908 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
909 0dbbbe90 2022-06-17 op wclear(view->window);
910 0dbbbe90 2022-06-17 op
911 0dbbbe90 2022-06-17 op view->nlines = nlines;
912 0dbbbe90 2022-06-17 op view->ncols = ncols;
913 0dbbbe90 2022-06-17 op view->lines = LINES;
914 0dbbbe90 2022-06-17 op view->cols = COLS;
915 0dbbbe90 2022-06-17 op
916 5c60c32a 2018-10-18 stsp return NULL;
917 669b5ffa 2018-10-07 stsp }
918 669b5ffa 2018-10-07 stsp
919 669b5ffa 2018-10-07 stsp static const struct got_error *
920 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
921 669b5ffa 2018-10-07 stsp {
922 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
923 669b5ffa 2018-10-07 stsp
924 669b5ffa 2018-10-07 stsp if (view->child == NULL)
925 669b5ffa 2018-10-07 stsp return NULL;
926 669b5ffa 2018-10-07 stsp
927 669b5ffa 2018-10-07 stsp err = view_close(view->child);
928 669b5ffa 2018-10-07 stsp view->child = NULL;
929 669b5ffa 2018-10-07 stsp return err;
930 669b5ffa 2018-10-07 stsp }
931 669b5ffa 2018-10-07 stsp
932 0dbbbe90 2022-06-17 op static const struct got_error *
933 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
934 669b5ffa 2018-10-07 stsp {
935 669b5ffa 2018-10-07 stsp view->child = child;
936 669b5ffa 2018-10-07 stsp child->parent = view;
937 0dbbbe90 2022-06-17 op
938 0dbbbe90 2022-06-17 op return view_resize(view);
939 bfddd0d9 2018-09-29 stsp }
940 bfddd0d9 2018-09-29 stsp
941 34bc9ec9 2019-02-22 stsp static void
942 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
943 25791caa 2018-10-24 stsp {
944 25791caa 2018-10-24 stsp int cols, lines;
945 25791caa 2018-10-24 stsp struct winsize size;
946 25791caa 2018-10-24 stsp
947 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
948 25791caa 2018-10-24 stsp cols = 80; /* Default */
949 25791caa 2018-10-24 stsp lines = 24;
950 25791caa 2018-10-24 stsp } else {
951 25791caa 2018-10-24 stsp cols = size.ws_col;
952 25791caa 2018-10-24 stsp lines = size.ws_row;
953 25791caa 2018-10-24 stsp }
954 25791caa 2018-10-24 stsp resize_term(lines, cols);
955 2b49a8ae 2019-06-22 stsp }
956 2b49a8ae 2019-06-22 stsp
957 2b49a8ae 2019-06-22 stsp static const struct got_error *
958 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
959 2b49a8ae 2019-06-22 stsp {
960 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
961 9b058f45 2022-06-30 mark struct tog_view *v = view;
962 2b49a8ae 2019-06-22 stsp char pattern[1024];
963 2b49a8ae 2019-06-22 stsp int ret;
964 c0c4acc8 2021-01-24 stsp
965 c0c4acc8 2021-01-24 stsp if (view->search_started) {
966 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
967 c0c4acc8 2021-01-24 stsp view->searching = 0;
968 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
969 c0c4acc8 2021-01-24 stsp }
970 c0c4acc8 2021-01-24 stsp view->search_started = 0;
971 2b49a8ae 2019-06-22 stsp
972 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
973 2b49a8ae 2019-06-22 stsp return NULL;
974 2b49a8ae 2019-06-22 stsp
975 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
976 9b058f45 2022-06-30 mark v = view->child;
977 2b49a8ae 2019-06-22 stsp
978 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
979 9b058f45 2022-06-30 mark wclrtoeol(v->window);
980 9b058f45 2022-06-30 mark
981 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
982 2b49a8ae 2019-06-22 stsp nocbreak();
983 2b49a8ae 2019-06-22 stsp echo();
984 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
985 9b058f45 2022-06-30 mark wrefresh(v->window);
986 2b49a8ae 2019-06-22 stsp cbreak();
987 2b49a8ae 2019-06-22 stsp noecho();
988 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
989 2b49a8ae 2019-06-22 stsp if (ret == ERR)
990 2b49a8ae 2019-06-22 stsp return NULL;
991 2b49a8ae 2019-06-22 stsp
992 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
993 7c32bd05 2019-06-22 stsp err = view->search_start(view);
994 7c32bd05 2019-06-22 stsp if (err) {
995 7c32bd05 2019-06-22 stsp regfree(&view->regex);
996 7c32bd05 2019-06-22 stsp return err;
997 7c32bd05 2019-06-22 stsp }
998 c0c4acc8 2021-01-24 stsp view->search_started = 1;
999 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1000 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1001 2b49a8ae 2019-06-22 stsp view->search_next(view);
1002 2b49a8ae 2019-06-22 stsp }
1003 2b49a8ae 2019-06-22 stsp
1004 2b49a8ae 2019-06-22 stsp return NULL;
1005 0cf4efb1 2018-09-29 stsp }
1006 6d0fee91 2018-08-01 stsp
1007 640cd7ff 2022-06-22 mark /*
1008 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1009 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1010 640cd7ff 2022-06-22 mark */
1011 640cd7ff 2022-06-22 mark static int
1012 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1013 640cd7ff 2022-06-22 mark {
1014 9b058f45 2022-06-30 mark struct tog_view *v = view;
1015 9b058f45 2022-06-30 mark int x, n = 0;
1016 640cd7ff 2022-06-22 mark
1017 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1018 9b058f45 2022-06-30 mark v = view->child;
1019 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1020 9b058f45 2022-06-30 mark v = view->parent;
1021 9b058f45 2022-06-30 mark
1022 640cd7ff 2022-06-22 mark view->count = 0;
1023 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1024 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1025 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1026 9b058f45 2022-06-30 mark waddch(v->window, ':');
1027 640cd7ff 2022-06-22 mark
1028 640cd7ff 2022-06-22 mark do {
1029 9b058f45 2022-06-30 mark x = getcurx(v->window);
1030 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1031 9b058f45 2022-06-30 mark waddch(v->window, c);
1032 9b058f45 2022-06-30 mark wrefresh(v->window);
1033 9b058f45 2022-06-30 mark }
1034 9b058f45 2022-06-30 mark
1035 640cd7ff 2022-06-22 mark /*
1036 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1037 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1038 640cd7ff 2022-06-22 mark */
1039 640cd7ff 2022-06-22 mark if (n >= 9999999)
1040 640cd7ff 2022-06-22 mark n = 9999999;
1041 640cd7ff 2022-06-22 mark else
1042 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1043 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1044 640cd7ff 2022-06-22 mark
1045 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1046 640cd7ff 2022-06-22 mark view->count = n;
1047 640cd7ff 2022-06-22 mark
1048 640cd7ff 2022-06-22 mark return c;
1049 640cd7ff 2022-06-22 mark }
1050 640cd7ff 2022-06-22 mark
1051 0cf4efb1 2018-09-29 stsp static const struct got_error *
1052 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1053 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1054 e5a0f69f 2018-08-18 stsp {
1055 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1056 669b5ffa 2018-10-07 stsp struct tog_view *v;
1057 1a76625f 2018-10-22 stsp int ch, errcode;
1058 e5a0f69f 2018-08-18 stsp
1059 e5a0f69f 2018-08-18 stsp *new = NULL;
1060 8f4ed634 2020-03-26 stsp
1061 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1062 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1063 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1064 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1065 640cd7ff 2022-06-22 mark view->count = 0;
1066 640cd7ff 2022-06-22 mark }
1067 e5a0f69f 2018-08-18 stsp
1068 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1069 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1070 82954512 2020-02-03 stsp if (errcode)
1071 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1072 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1073 3da8ef85 2021-09-21 stsp sched_yield();
1074 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1075 82954512 2020-02-03 stsp if (errcode)
1076 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1077 82954512 2020-02-03 stsp "pthread_mutex_lock");
1078 60493ae3 2019-06-20 stsp view->search_next(view);
1079 60493ae3 2019-06-20 stsp return NULL;
1080 60493ae3 2019-06-20 stsp }
1081 60493ae3 2019-06-20 stsp
1082 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1083 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1084 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1085 1a76625f 2018-10-22 stsp if (errcode)
1086 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1087 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1088 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1089 a6d37fac 2022-07-03 mark cbreak();
1090 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1091 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1092 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1093 a6d37fac 2022-07-03 mark view->count = 0;
1094 a6d37fac 2022-07-03 mark else
1095 a6d37fac 2022-07-03 mark ch = view->ch;
1096 a6d37fac 2022-07-03 mark } else {
1097 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1098 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1099 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1100 640cd7ff 2022-06-22 mark }
1101 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1102 1a76625f 2018-10-22 stsp if (errcode)
1103 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1104 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1105 25791caa 2018-10-24 stsp
1106 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1107 25791caa 2018-10-24 stsp tog_resizeterm();
1108 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1109 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1110 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1111 25791caa 2018-10-24 stsp err = view_resize(v);
1112 25791caa 2018-10-24 stsp if (err)
1113 25791caa 2018-10-24 stsp return err;
1114 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1115 25791caa 2018-10-24 stsp if (err)
1116 25791caa 2018-10-24 stsp return err;
1117 cdfcfb03 2020-12-06 stsp if (v->child) {
1118 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1119 cdfcfb03 2020-12-06 stsp if (err)
1120 cdfcfb03 2020-12-06 stsp return err;
1121 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1122 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1123 cdfcfb03 2020-12-06 stsp if (err)
1124 cdfcfb03 2020-12-06 stsp return err;
1125 cdfcfb03 2020-12-06 stsp }
1126 25791caa 2018-10-24 stsp }
1127 25791caa 2018-10-24 stsp }
1128 25791caa 2018-10-24 stsp
1129 e5a0f69f 2018-08-18 stsp switch (ch) {
1130 1e37a5c2 2019-05-12 jcs case '\t':
1131 640cd7ff 2022-06-22 mark view->count = 0;
1132 1e37a5c2 2019-05-12 jcs if (view->child) {
1133 e78dc838 2020-12-04 stsp view->focussed = 0;
1134 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1135 e78dc838 2020-12-04 stsp view->focus_child = 1;
1136 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1137 e78dc838 2020-12-04 stsp view->focussed = 0;
1138 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1139 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1140 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1141 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1142 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1143 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1144 9b058f45 2022-06-30 mark if (err)
1145 9b058f45 2022-06-30 mark return err;
1146 9b058f45 2022-06-30 mark }
1147 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1148 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1149 9b058f45 2022-06-30 mark if (err)
1150 9b058f45 2022-06-30 mark return err;
1151 9b058f45 2022-06-30 mark }
1152 1e37a5c2 2019-05-12 jcs }
1153 1e37a5c2 2019-05-12 jcs break;
1154 1e37a5c2 2019-05-12 jcs case 'q':
1155 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1156 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1157 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1158 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1159 9b058f45 2022-06-30 mark if (err)
1160 9b058f45 2022-06-30 mark break;
1161 9b058f45 2022-06-30 mark }
1162 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1163 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1164 9b058f45 2022-06-30 mark }
1165 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1166 9970f7fc 2020-12-03 stsp view->dying = 1;
1167 1e37a5c2 2019-05-12 jcs break;
1168 1e37a5c2 2019-05-12 jcs case 'Q':
1169 1e37a5c2 2019-05-12 jcs *done = 1;
1170 1e37a5c2 2019-05-12 jcs break;
1171 61417565 2022-06-20 mark case 'F':
1172 640cd7ff 2022-06-22 mark view->count = 0;
1173 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1174 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1175 1e37a5c2 2019-05-12 jcs break;
1176 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1177 e78dc838 2020-12-04 stsp view->focussed = 0;
1178 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1179 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1180 1e37a5c2 2019-05-12 jcs } else
1181 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1182 1e37a5c2 2019-05-12 jcs if (err)
1183 1e37a5c2 2019-05-12 jcs break;
1184 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1185 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1186 1e37a5c2 2019-05-12 jcs } else {
1187 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1188 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1189 e78dc838 2020-12-04 stsp view->focussed = 1;
1190 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1191 1e37a5c2 2019-05-12 jcs } else {
1192 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1193 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1194 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1195 669b5ffa 2018-10-07 stsp }
1196 1e37a5c2 2019-05-12 jcs if (err)
1197 1e37a5c2 2019-05-12 jcs break;
1198 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1199 1e37a5c2 2019-05-12 jcs }
1200 9b058f45 2022-06-30 mark if (err)
1201 9b058f45 2022-06-30 mark break;
1202 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1203 9b058f45 2022-06-30 mark err = request_log_commits(view);
1204 9b058f45 2022-06-30 mark if (err)
1205 9b058f45 2022-06-30 mark break;
1206 9b058f45 2022-06-30 mark }
1207 9b058f45 2022-06-30 mark if (view->parent)
1208 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1209 9b058f45 2022-06-30 mark if (!err)
1210 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1211 1e37a5c2 2019-05-12 jcs break;
1212 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1213 60493ae3 2019-06-20 stsp break;
1214 60493ae3 2019-06-20 stsp case '/':
1215 640cd7ff 2022-06-22 mark view->count = 0;
1216 60493ae3 2019-06-20 stsp if (view->search_start)
1217 2b49a8ae 2019-06-22 stsp view_search_start(view);
1218 60493ae3 2019-06-20 stsp else
1219 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1220 1e37a5c2 2019-05-12 jcs break;
1221 b1bf1435 2019-06-21 stsp case 'N':
1222 60493ae3 2019-06-20 stsp case 'n':
1223 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1224 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1225 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1226 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1227 60493ae3 2019-06-20 stsp view->search_next(view);
1228 60493ae3 2019-06-20 stsp } else
1229 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1230 917d79a7 2022-07-01 stsp break;
1231 917d79a7 2022-07-01 stsp case 'A':
1232 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1233 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1234 917d79a7 2022-07-01 stsp else
1235 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1236 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1237 917d79a7 2022-07-01 stsp if (v->reset) {
1238 917d79a7 2022-07-01 stsp err = v->reset(v);
1239 917d79a7 2022-07-01 stsp if (err)
1240 917d79a7 2022-07-01 stsp return err;
1241 917d79a7 2022-07-01 stsp }
1242 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1243 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1244 917d79a7 2022-07-01 stsp if (err)
1245 917d79a7 2022-07-01 stsp return err;
1246 917d79a7 2022-07-01 stsp }
1247 917d79a7 2022-07-01 stsp }
1248 60493ae3 2019-06-20 stsp break;
1249 1e37a5c2 2019-05-12 jcs default:
1250 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1251 1e37a5c2 2019-05-12 jcs break;
1252 e5a0f69f 2018-08-18 stsp }
1253 e5a0f69f 2018-08-18 stsp
1254 e5a0f69f 2018-08-18 stsp return err;
1255 e5a0f69f 2018-08-18 stsp }
1256 e5a0f69f 2018-08-18 stsp
1257 336075a4 2022-06-25 op static int
1258 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1259 a3404814 2018-09-02 stsp {
1260 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1261 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1262 669b5ffa 2018-10-07 stsp return 0;
1263 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1264 669b5ffa 2018-10-07 stsp return 0;
1265 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1266 a3404814 2018-09-02 stsp return 0;
1267 a3404814 2018-09-02 stsp
1268 669b5ffa 2018-10-07 stsp return view->focussed;
1269 a3404814 2018-09-02 stsp }
1270 a3404814 2018-09-02 stsp
1271 bcbd79e2 2018-08-19 stsp static const struct got_error *
1272 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1273 e5a0f69f 2018-08-18 stsp {
1274 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1275 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1276 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1277 fd823528 2018-10-22 stsp int fast_refresh = 10;
1278 1a76625f 2018-10-22 stsp int done = 0, errcode;
1279 e5a0f69f 2018-08-18 stsp
1280 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1281 1a76625f 2018-10-22 stsp if (errcode)
1282 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1283 1a76625f 2018-10-22 stsp
1284 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1285 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1286 e5a0f69f 2018-08-18 stsp
1287 1004088d 2018-09-29 stsp view->focussed = 1;
1288 878940b7 2018-09-29 stsp err = view->show(view);
1289 0cf4efb1 2018-09-29 stsp if (err)
1290 0cf4efb1 2018-09-29 stsp return err;
1291 0cf4efb1 2018-09-29 stsp update_panels();
1292 0cf4efb1 2018-09-29 stsp doupdate();
1293 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1294 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1295 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1296 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1297 fd823528 2018-10-22 stsp
1298 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1299 e5a0f69f 2018-08-18 stsp if (err)
1300 e5a0f69f 2018-08-18 stsp break;
1301 9970f7fc 2020-12-03 stsp if (view->dying) {
1302 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1303 669b5ffa 2018-10-07 stsp
1304 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1305 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1306 9970f7fc 2020-12-03 stsp entry);
1307 e78dc838 2020-12-04 stsp else if (view->parent)
1308 669b5ffa 2018-10-07 stsp prev = view->parent;
1309 669b5ffa 2018-10-07 stsp
1310 e78dc838 2020-12-04 stsp if (view->parent) {
1311 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1312 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1313 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1314 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1315 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1316 0dbbbe90 2022-06-17 op if (err)
1317 0dbbbe90 2022-06-17 op break;
1318 e78dc838 2020-12-04 stsp } else
1319 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1320 669b5ffa 2018-10-07 stsp
1321 9970f7fc 2020-12-03 stsp err = view_close(view);
1322 fb59748f 2020-12-05 stsp if (err)
1323 e5a0f69f 2018-08-18 stsp goto done;
1324 669b5ffa 2018-10-07 stsp
1325 e78dc838 2020-12-04 stsp view = NULL;
1326 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1327 e78dc838 2020-12-04 stsp if (v->focussed)
1328 e78dc838 2020-12-04 stsp break;
1329 0cf4efb1 2018-09-29 stsp }
1330 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1331 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1332 e78dc838 2020-12-04 stsp if (prev)
1333 e78dc838 2020-12-04 stsp view = prev;
1334 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1335 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1336 e78dc838 2020-12-04 stsp tog_view_list_head);
1337 e78dc838 2020-12-04 stsp }
1338 e78dc838 2020-12-04 stsp if (view) {
1339 e78dc838 2020-12-04 stsp if (view->focus_child) {
1340 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1341 e78dc838 2020-12-04 stsp view = view->child;
1342 e78dc838 2020-12-04 stsp } else
1343 e78dc838 2020-12-04 stsp view->focussed = 1;
1344 e78dc838 2020-12-04 stsp }
1345 e78dc838 2020-12-04 stsp }
1346 e5a0f69f 2018-08-18 stsp }
1347 bcbd79e2 2018-08-19 stsp if (new_view) {
1348 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1349 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1350 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1351 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1352 86c66b02 2018-10-18 stsp continue;
1353 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1354 86c66b02 2018-10-18 stsp err = view_close(v);
1355 86c66b02 2018-10-18 stsp if (err)
1356 86c66b02 2018-10-18 stsp goto done;
1357 86c66b02 2018-10-18 stsp break;
1358 86c66b02 2018-10-18 stsp }
1359 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1360 fed7eaa8 2018-10-24 stsp view = new_view;
1361 0ae84acc 2022-06-15 tracey }
1362 669b5ffa 2018-10-07 stsp if (view) {
1363 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1364 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1365 e78dc838 2020-12-04 stsp view = view->child;
1366 e78dc838 2020-12-04 stsp } else {
1367 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1368 e78dc838 2020-12-04 stsp view = view->parent;
1369 1a76625f 2018-10-22 stsp }
1370 e78dc838 2020-12-04 stsp show_panel(view->panel);
1371 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1372 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1373 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1374 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1375 669b5ffa 2018-10-07 stsp if (err)
1376 1a76625f 2018-10-22 stsp goto done;
1377 669b5ffa 2018-10-07 stsp }
1378 669b5ffa 2018-10-07 stsp err = view->show(view);
1379 0cf4efb1 2018-09-29 stsp if (err)
1380 1a76625f 2018-10-22 stsp goto done;
1381 669b5ffa 2018-10-07 stsp if (view->child) {
1382 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1383 669b5ffa 2018-10-07 stsp if (err)
1384 1a76625f 2018-10-22 stsp goto done;
1385 669b5ffa 2018-10-07 stsp }
1386 1a76625f 2018-10-22 stsp update_panels();
1387 1a76625f 2018-10-22 stsp doupdate();
1388 0cf4efb1 2018-09-29 stsp }
1389 e5a0f69f 2018-08-18 stsp }
1390 e5a0f69f 2018-08-18 stsp done:
1391 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1392 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1393 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1394 e5a0f69f 2018-08-18 stsp view_close(view);
1395 e5a0f69f 2018-08-18 stsp }
1396 1a76625f 2018-10-22 stsp
1397 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1398 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1399 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1400 1a76625f 2018-10-22 stsp
1401 e5a0f69f 2018-08-18 stsp return err;
1402 ea5e7bb5 2018-08-01 stsp }
1403 ea5e7bb5 2018-08-01 stsp
1404 4ed7e80c 2018-05-20 stsp __dead static void
1405 9f7d7167 2018-04-29 stsp usage_log(void)
1406 9f7d7167 2018-04-29 stsp {
1407 80ddbec8 2018-04-29 stsp endwin();
1408 c70c5802 2018-08-01 stsp fprintf(stderr,
1409 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1410 9f7d7167 2018-04-29 stsp getprogname());
1411 9f7d7167 2018-04-29 stsp exit(1);
1412 80ddbec8 2018-04-29 stsp }
1413 80ddbec8 2018-04-29 stsp
1414 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1415 80ddbec8 2018-04-29 stsp static const struct got_error *
1416 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1417 963b370f 2018-05-20 stsp {
1418 00dfcb92 2018-06-11 stsp char *vis = NULL;
1419 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1420 963b370f 2018-05-20 stsp
1421 963b370f 2018-05-20 stsp *ws = NULL;
1422 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1423 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1424 00dfcb92 2018-06-11 stsp int vislen;
1425 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1426 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1427 00dfcb92 2018-06-11 stsp
1428 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1429 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1430 00dfcb92 2018-06-11 stsp if (err)
1431 00dfcb92 2018-06-11 stsp return err;
1432 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1433 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1434 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1435 a7f50699 2018-06-11 stsp goto done;
1436 a7f50699 2018-06-11 stsp }
1437 00dfcb92 2018-06-11 stsp }
1438 963b370f 2018-05-20 stsp
1439 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1440 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1441 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1442 a7f50699 2018-06-11 stsp goto done;
1443 a7f50699 2018-06-11 stsp }
1444 963b370f 2018-05-20 stsp
1445 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1446 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1447 a7f50699 2018-06-11 stsp done:
1448 00dfcb92 2018-06-11 stsp free(vis);
1449 963b370f 2018-05-20 stsp if (err) {
1450 963b370f 2018-05-20 stsp free(*ws);
1451 963b370f 2018-05-20 stsp *ws = NULL;
1452 963b370f 2018-05-20 stsp *wlen = 0;
1453 963b370f 2018-05-20 stsp }
1454 963b370f 2018-05-20 stsp return err;
1455 145b6838 2022-06-16 stsp }
1456 145b6838 2022-06-16 stsp
1457 145b6838 2022-06-16 stsp static const struct got_error *
1458 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1459 145b6838 2022-06-16 stsp {
1460 145b6838 2022-06-16 stsp char *dst;
1461 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1462 145b6838 2022-06-16 stsp
1463 145b6838 2022-06-16 stsp *ptr = NULL;
1464 145b6838 2022-06-16 stsp n = len = strlen(src);
1465 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1466 145b6838 2022-06-16 stsp if (dst == NULL)
1467 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1468 145b6838 2022-06-16 stsp
1469 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1470 145b6838 2022-06-16 stsp const char c = src[idx];
1471 145b6838 2022-06-16 stsp
1472 145b6838 2022-06-16 stsp if (c == '\t') {
1473 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1474 367ddf28 2022-06-16 mark char *p;
1475 367ddf28 2022-06-16 mark
1476 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1477 6e1c41ad 2022-06-16 mark if (p == NULL) {
1478 6e1c41ad 2022-06-16 mark free(dst);
1479 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1480 6e1c41ad 2022-06-16 mark
1481 6e1c41ad 2022-06-16 mark }
1482 6e1c41ad 2022-06-16 mark dst = p;
1483 145b6838 2022-06-16 stsp n += nb;
1484 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1485 145b6838 2022-06-16 stsp sz += nb;
1486 145b6838 2022-06-16 stsp } else
1487 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1488 145b6838 2022-06-16 stsp ++idx;
1489 145b6838 2022-06-16 stsp }
1490 145b6838 2022-06-16 stsp
1491 145b6838 2022-06-16 stsp dst[sz] = '\0';
1492 145b6838 2022-06-16 stsp *ptr = dst;
1493 145b6838 2022-06-16 stsp return NULL;
1494 963b370f 2018-05-20 stsp }
1495 963b370f 2018-05-20 stsp
1496 4e4a9ac8 2022-06-17 op /*
1497 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1498 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1499 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1500 4e4a9ac8 2022-06-17 op * *rcol.
1501 ccda2f4d 2022-06-16 stsp */
1502 4e4a9ac8 2022-06-17 op static int
1503 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1504 4e4a9ac8 2022-06-17 op {
1505 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1506 ccda2f4d 2022-06-16 stsp
1507 4e4a9ac8 2022-06-17 op if (n == 0) {
1508 4e4a9ac8 2022-06-17 op *rcol = cols;
1509 4e4a9ac8 2022-06-17 op return off;
1510 4e4a9ac8 2022-06-17 op }
1511 ccda2f4d 2022-06-16 stsp
1512 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1513 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1514 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1515 4e4a9ac8 2022-06-17 op else
1516 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1517 ccda2f4d 2022-06-16 stsp
1518 4e4a9ac8 2022-06-17 op if (width == -1) {
1519 4e4a9ac8 2022-06-17 op width = 1;
1520 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1521 ccda2f4d 2022-06-16 stsp }
1522 ccda2f4d 2022-06-16 stsp
1523 4e4a9ac8 2022-06-17 op if (cols + width > n)
1524 4e4a9ac8 2022-06-17 op break;
1525 4e4a9ac8 2022-06-17 op cols += width;
1526 ccda2f4d 2022-06-16 stsp }
1527 ccda2f4d 2022-06-16 stsp
1528 4e4a9ac8 2022-06-17 op *rcol = cols;
1529 4e4a9ac8 2022-06-17 op return i;
1530 ccda2f4d 2022-06-16 stsp }
1531 ccda2f4d 2022-06-16 stsp
1532 ccda2f4d 2022-06-16 stsp /*
1533 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1534 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1535 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1536 ccda2f4d 2022-06-16 stsp */
1537 ccda2f4d 2022-06-16 stsp static const struct got_error *
1538 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1539 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1540 ccda2f4d 2022-06-16 stsp {
1541 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1542 4e4a9ac8 2022-06-17 op int cols;
1543 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1544 145b6838 2022-06-16 stsp char *exstr = NULL;
1545 963b370f 2018-05-20 stsp size_t wlen;
1546 4e4a9ac8 2022-06-17 op int i, scrollx;
1547 963b370f 2018-05-20 stsp
1548 963b370f 2018-05-20 stsp *wlinep = NULL;
1549 b700b5d6 2018-07-10 stsp *widthp = 0;
1550 963b370f 2018-05-20 stsp
1551 145b6838 2022-06-16 stsp if (expand) {
1552 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1553 145b6838 2022-06-16 stsp if (err)
1554 145b6838 2022-06-16 stsp return err;
1555 145b6838 2022-06-16 stsp }
1556 145b6838 2022-06-16 stsp
1557 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1558 145b6838 2022-06-16 stsp free(exstr);
1559 963b370f 2018-05-20 stsp if (err)
1560 963b370f 2018-05-20 stsp return err;
1561 963b370f 2018-05-20 stsp
1562 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1563 ccda2f4d 2022-06-16 stsp
1564 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1565 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1566 3f670bfb 2020-12-10 stsp wlen--;
1567 3f670bfb 2020-12-10 stsp }
1568 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1569 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1570 3f670bfb 2020-12-10 stsp wlen--;
1571 3f670bfb 2020-12-10 stsp }
1572 3f670bfb 2020-12-10 stsp
1573 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1574 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1575 27a741e5 2019-09-11 stsp
1576 b700b5d6 2018-07-10 stsp if (widthp)
1577 b700b5d6 2018-07-10 stsp *widthp = cols;
1578 ccda2f4d 2022-06-16 stsp if (scrollxp)
1579 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1580 963b370f 2018-05-20 stsp if (err)
1581 963b370f 2018-05-20 stsp free(wline);
1582 963b370f 2018-05-20 stsp else
1583 963b370f 2018-05-20 stsp *wlinep = wline;
1584 963b370f 2018-05-20 stsp return err;
1585 963b370f 2018-05-20 stsp }
1586 963b370f 2018-05-20 stsp
1587 8b473291 2019-02-21 stsp static const struct got_error*
1588 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1589 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1590 8b473291 2019-02-21 stsp {
1591 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1592 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1593 8b473291 2019-02-21 stsp char *s;
1594 8b473291 2019-02-21 stsp const char *name;
1595 8b473291 2019-02-21 stsp
1596 8b473291 2019-02-21 stsp *refs_str = NULL;
1597 8b473291 2019-02-21 stsp
1598 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1599 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1600 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1601 52b5abe1 2019-08-13 stsp int cmp;
1602 52b5abe1 2019-08-13 stsp
1603 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1604 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1605 8b473291 2019-02-21 stsp continue;
1606 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1607 8b473291 2019-02-21 stsp name += 5;
1608 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1609 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1610 7143d404 2019-03-12 stsp continue;
1611 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1612 8b473291 2019-02-21 stsp name += 6;
1613 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1614 8b473291 2019-02-21 stsp name += 8;
1615 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1616 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1617 79cc719f 2020-04-24 stsp continue;
1618 79cc719f 2020-04-24 stsp }
1619 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1620 48cae60d 2020-09-22 stsp if (err)
1621 48cae60d 2020-09-22 stsp break;
1622 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1623 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1624 5d844a1e 2019-08-13 stsp if (err) {
1625 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1626 48cae60d 2020-09-22 stsp free(ref_id);
1627 5d844a1e 2019-08-13 stsp break;
1628 48cae60d 2020-09-22 stsp }
1629 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1630 5d844a1e 2019-08-13 stsp err = NULL;
1631 5d844a1e 2019-08-13 stsp tag = NULL;
1632 5d844a1e 2019-08-13 stsp }
1633 52b5abe1 2019-08-13 stsp }
1634 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1635 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1636 48cae60d 2020-09-22 stsp free(ref_id);
1637 52b5abe1 2019-08-13 stsp if (tag)
1638 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1639 52b5abe1 2019-08-13 stsp if (cmp != 0)
1640 52b5abe1 2019-08-13 stsp continue;
1641 8b473291 2019-02-21 stsp s = *refs_str;
1642 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1643 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1644 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1645 8b473291 2019-02-21 stsp free(s);
1646 8b473291 2019-02-21 stsp *refs_str = NULL;
1647 8b473291 2019-02-21 stsp break;
1648 8b473291 2019-02-21 stsp }
1649 8b473291 2019-02-21 stsp free(s);
1650 8b473291 2019-02-21 stsp }
1651 8b473291 2019-02-21 stsp
1652 8b473291 2019-02-21 stsp return err;
1653 8b473291 2019-02-21 stsp }
1654 8b473291 2019-02-21 stsp
1655 963b370f 2018-05-20 stsp static const struct got_error *
1656 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1657 27a741e5 2019-09-11 stsp int col_tab_align)
1658 5813d178 2019-03-09 stsp {
1659 e6b8b890 2020-12-29 naddy char *smallerthan;
1660 5813d178 2019-03-09 stsp
1661 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1662 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1663 5813d178 2019-03-09 stsp author = smallerthan + 1;
1664 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1665 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1666 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1667 5813d178 2019-03-09 stsp }
1668 5813d178 2019-03-09 stsp
1669 5813d178 2019-03-09 stsp static const struct got_error *
1670 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1671 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1672 8fdc79fe 2020-12-01 naddy int author_display_cols)
1673 80ddbec8 2018-04-29 stsp {
1674 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1675 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1676 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1677 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1678 5813d178 2019-03-09 stsp char *author = NULL;
1679 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1680 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1681 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1682 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1683 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1684 ccb26ccd 2018-11-05 stsp struct tm tm;
1685 45d799e2 2018-12-23 stsp time_t committer_time;
1686 11b20872 2019-11-08 stsp struct tog_color *tc;
1687 80ddbec8 2018-04-29 stsp
1688 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1689 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1690 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1691 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1692 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1693 b39d25c7 2018-07-10 stsp
1694 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1695 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1696 b39d25c7 2018-07-10 stsp else
1697 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1698 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1699 11b20872 2019-11-08 stsp if (tc)
1700 11b20872 2019-11-08 stsp wattr_on(view->window,
1701 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1702 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1703 11b20872 2019-11-08 stsp if (tc)
1704 11b20872 2019-11-08 stsp wattr_off(view->window,
1705 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1706 27a741e5 2019-09-11 stsp col = limit;
1707 b39d25c7 2018-07-10 stsp if (col > avail)
1708 b39d25c7 2018-07-10 stsp goto done;
1709 6570a66d 2019-11-08 stsp
1710 6570a66d 2019-11-08 stsp if (avail >= 120) {
1711 6570a66d 2019-11-08 stsp char *id_str;
1712 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1713 6570a66d 2019-11-08 stsp if (err)
1714 6570a66d 2019-11-08 stsp goto done;
1715 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1716 11b20872 2019-11-08 stsp if (tc)
1717 11b20872 2019-11-08 stsp wattr_on(view->window,
1718 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1719 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1720 11b20872 2019-11-08 stsp if (tc)
1721 11b20872 2019-11-08 stsp wattr_off(view->window,
1722 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1723 6570a66d 2019-11-08 stsp free(id_str);
1724 6570a66d 2019-11-08 stsp col += 9;
1725 6570a66d 2019-11-08 stsp if (col > avail)
1726 6570a66d 2019-11-08 stsp goto done;
1727 6570a66d 2019-11-08 stsp }
1728 b39d25c7 2018-07-10 stsp
1729 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1730 5813d178 2019-03-09 stsp if (author == NULL) {
1731 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1732 80ddbec8 2018-04-29 stsp goto done;
1733 80ddbec8 2018-04-29 stsp }
1734 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1735 bb737323 2018-05-20 stsp if (err)
1736 bb737323 2018-05-20 stsp goto done;
1737 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1738 11b20872 2019-11-08 stsp if (tc)
1739 11b20872 2019-11-08 stsp wattr_on(view->window,
1740 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1741 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1742 11b20872 2019-11-08 stsp if (tc)
1743 11b20872 2019-11-08 stsp wattr_off(view->window,
1744 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1745 bb737323 2018-05-20 stsp col += author_width;
1746 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1747 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1748 bb737323 2018-05-20 stsp col++;
1749 bb737323 2018-05-20 stsp author_width++;
1750 bb737323 2018-05-20 stsp }
1751 9c2eaf34 2018-05-20 stsp if (col > avail)
1752 9c2eaf34 2018-05-20 stsp goto done;
1753 80ddbec8 2018-04-29 stsp
1754 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1755 5943eee2 2019-08-13 stsp if (err)
1756 6d9fbc00 2018-04-29 stsp goto done;
1757 bb737323 2018-05-20 stsp logmsg = logmsg0;
1758 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1759 bb737323 2018-05-20 stsp logmsg++;
1760 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1761 bb737323 2018-05-20 stsp if (newline)
1762 bb737323 2018-05-20 stsp *newline = '\0';
1763 ccda2f4d 2022-06-16 stsp limit = avail - col;
1764 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
1765 4d1f6af3 2022-06-17 op limit--; /* for the border */
1766 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1767 ccda2f4d 2022-06-16 stsp limit, col, 1);
1768 29688b02 2022-06-16 stsp if (err)
1769 29688b02 2022-06-16 stsp goto done;
1770 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1771 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1772 27a741e5 2019-09-11 stsp while (col < avail) {
1773 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1774 bb737323 2018-05-20 stsp col++;
1775 881b2d3e 2018-04-30 stsp }
1776 80ddbec8 2018-04-29 stsp done:
1777 80ddbec8 2018-04-29 stsp free(logmsg0);
1778 bb737323 2018-05-20 stsp free(wlogmsg);
1779 5813d178 2019-03-09 stsp free(author);
1780 bb737323 2018-05-20 stsp free(wauthor);
1781 80ddbec8 2018-04-29 stsp free(line);
1782 80ddbec8 2018-04-29 stsp return err;
1783 80ddbec8 2018-04-29 stsp }
1784 26ed57b2 2018-05-19 stsp
1785 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1786 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1787 899d86c2 2018-05-10 stsp struct got_object_id *id)
1788 80ddbec8 2018-04-29 stsp {
1789 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1790 80ddbec8 2018-04-29 stsp
1791 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1792 80ddbec8 2018-04-29 stsp if (entry == NULL)
1793 899d86c2 2018-05-10 stsp return NULL;
1794 99db9666 2018-05-07 stsp
1795 899d86c2 2018-05-10 stsp entry->id = id;
1796 99db9666 2018-05-07 stsp entry->commit = commit;
1797 899d86c2 2018-05-10 stsp return entry;
1798 99db9666 2018-05-07 stsp }
1799 80ddbec8 2018-04-29 stsp
1800 99db9666 2018-05-07 stsp static void
1801 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1802 99db9666 2018-05-07 stsp {
1803 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1804 99db9666 2018-05-07 stsp
1805 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1806 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1807 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1808 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1809 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1810 99db9666 2018-05-07 stsp free(entry);
1811 99db9666 2018-05-07 stsp }
1812 99db9666 2018-05-07 stsp
1813 99db9666 2018-05-07 stsp static void
1814 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1815 99db9666 2018-05-07 stsp {
1816 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1817 99db9666 2018-05-07 stsp pop_commit(commits);
1818 c4972b91 2018-05-07 stsp }
1819 c4972b91 2018-05-07 stsp
1820 c4972b91 2018-05-07 stsp static const struct got_error *
1821 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1822 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1823 13add988 2019-10-15 stsp {
1824 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1825 13add988 2019-10-15 stsp regmatch_t regmatch;
1826 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1827 13add988 2019-10-15 stsp
1828 13add988 2019-10-15 stsp *have_match = 0;
1829 13add988 2019-10-15 stsp
1830 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1831 13add988 2019-10-15 stsp if (err)
1832 13add988 2019-10-15 stsp return err;
1833 13add988 2019-10-15 stsp
1834 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1835 13add988 2019-10-15 stsp if (err)
1836 13add988 2019-10-15 stsp goto done;
1837 13add988 2019-10-15 stsp
1838 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1839 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1840 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1841 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1842 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1843 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1844 13add988 2019-10-15 stsp *have_match = 1;
1845 13add988 2019-10-15 stsp done:
1846 13add988 2019-10-15 stsp free(id_str);
1847 13add988 2019-10-15 stsp free(logmsg);
1848 13add988 2019-10-15 stsp return err;
1849 13add988 2019-10-15 stsp }
1850 13add988 2019-10-15 stsp
1851 13add988 2019-10-15 stsp static const struct got_error *
1852 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1853 c4972b91 2018-05-07 stsp {
1854 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1855 9ba79e04 2018-06-11 stsp
1856 1a76625f 2018-10-22 stsp /*
1857 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1858 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1859 1a76625f 2018-10-22 stsp * while updating the display.
1860 1a76625f 2018-10-22 stsp */
1861 4e0d2870 2020-12-07 naddy do {
1862 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1863 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1864 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1865 1a76625f 2018-10-22 stsp int errcode;
1866 899d86c2 2018-05-10 stsp
1867 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1868 4e0d2870 2020-12-07 naddy NULL, NULL);
1869 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1870 ecb28ae0 2018-07-16 stsp break;
1871 899d86c2 2018-05-10 stsp
1872 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1873 9ba79e04 2018-06-11 stsp if (err)
1874 9ba79e04 2018-06-11 stsp break;
1875 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1876 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1877 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1878 9ba79e04 2018-06-11 stsp break;
1879 9ba79e04 2018-06-11 stsp }
1880 93e45b7c 2018-09-24 stsp
1881 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1882 1a76625f 2018-10-22 stsp if (errcode) {
1883 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1884 13add988 2019-10-15 stsp "pthread_mutex_lock");
1885 1a76625f 2018-10-22 stsp break;
1886 1a76625f 2018-10-22 stsp }
1887 1a76625f 2018-10-22 stsp
1888 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1889 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1890 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1891 1a76625f 2018-10-22 stsp
1892 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1893 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1894 7c1452c1 2020-03-26 stsp int have_match;
1895 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1896 7c1452c1 2020-03-26 stsp if (err)
1897 7c1452c1 2020-03-26 stsp break;
1898 7c1452c1 2020-03-26 stsp if (have_match)
1899 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1900 13add988 2019-10-15 stsp }
1901 13add988 2019-10-15 stsp
1902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1903 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1904 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1905 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1906 7c1452c1 2020-03-26 stsp if (err)
1907 13add988 2019-10-15 stsp break;
1908 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1909 899d86c2 2018-05-10 stsp
1910 9ba79e04 2018-06-11 stsp return err;
1911 0553a4e3 2018-04-30 stsp }
1912 0553a4e3 2018-04-30 stsp
1913 2b779855 2020-12-05 naddy static void
1914 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1915 2b779855 2020-12-05 naddy {
1916 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1917 2b779855 2020-12-05 naddy int ncommits = 0;
1918 2b779855 2020-12-05 naddy
1919 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1920 2b779855 2020-12-05 naddy while (entry) {
1921 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1922 2b779855 2020-12-05 naddy s->selected_entry = entry;
1923 2b779855 2020-12-05 naddy break;
1924 2b779855 2020-12-05 naddy }
1925 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1926 2b779855 2020-12-05 naddy ncommits++;
1927 2b779855 2020-12-05 naddy }
1928 2b779855 2020-12-05 naddy }
1929 2b779855 2020-12-05 naddy
1930 0553a4e3 2018-04-30 stsp static const struct got_error *
1931 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1932 0553a4e3 2018-04-30 stsp {
1933 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1934 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1935 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1936 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1937 60493ae3 2019-06-20 stsp int width;
1938 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1939 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1940 8b473291 2019-02-21 stsp char *refs_str = NULL;
1941 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1942 11b20872 2019-11-08 stsp struct tog_color *tc;
1943 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1944 0553a4e3 2018-04-30 stsp
1945 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1946 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1947 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1948 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1949 1a76625f 2018-10-22 stsp if (err)
1950 ecb28ae0 2018-07-16 stsp return err;
1951 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1952 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1953 d2075bf3 2020-12-25 stsp if (refs) {
1954 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1955 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1956 d2075bf3 2020-12-25 stsp if (err)
1957 d2075bf3 2020-12-25 stsp goto done;
1958 d2075bf3 2020-12-25 stsp }
1959 867c6645 2018-07-10 stsp }
1960 359bfafd 2019-02-22 stsp
1961 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1962 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1963 1a76625f 2018-10-22 stsp
1964 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1965 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1966 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1967 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1968 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1969 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1970 8f4ed634 2020-03-26 stsp goto done;
1971 8f4ed634 2020-03-26 stsp }
1972 8f4ed634 2020-03-26 stsp } else {
1973 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1974 f9686aa5 2020-03-27 stsp
1975 f9686aa5 2020-03-27 stsp if (view->searching) {
1976 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1977 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1978 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1979 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1980 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1981 f9686aa5 2020-03-27 stsp search_str = "searching...";
1982 f9686aa5 2020-03-27 stsp }
1983 f9686aa5 2020-03-27 stsp
1984 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1985 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1986 f9686aa5 2020-03-27 stsp search_str ? search_str :
1987 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1988 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1989 8f4ed634 2020-03-26 stsp goto done;
1990 8f4ed634 2020-03-26 stsp }
1991 8b473291 2019-02-21 stsp }
1992 1a76625f 2018-10-22 stsp
1993 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1994 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1995 87411fa9 2022-06-16 stsp "........................................",
1996 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1997 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1998 1a76625f 2018-10-22 stsp header = NULL;
1999 1a76625f 2018-10-22 stsp goto done;
2000 1a76625f 2018-10-22 stsp }
2001 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2002 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2003 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2004 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2005 1a76625f 2018-10-22 stsp header = NULL;
2006 1a76625f 2018-10-22 stsp goto done;
2007 ecb28ae0 2018-07-16 stsp }
2008 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2009 1a76625f 2018-10-22 stsp if (err)
2010 1a76625f 2018-10-22 stsp goto done;
2011 867c6645 2018-07-10 stsp
2012 2814baeb 2018-08-01 stsp werase(view->window);
2013 867c6645 2018-07-10 stsp
2014 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2015 a3404814 2018-09-02 stsp wstandout(view->window);
2016 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2017 11b20872 2019-11-08 stsp if (tc)
2018 11b20872 2019-11-08 stsp wattr_on(view->window,
2019 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2020 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2021 11b20872 2019-11-08 stsp if (tc)
2022 11b20872 2019-11-08 stsp wattr_off(view->window,
2023 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2024 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2025 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2026 1a76625f 2018-10-22 stsp width++;
2027 1a76625f 2018-10-22 stsp }
2028 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2029 a3404814 2018-09-02 stsp wstandend(view->window);
2030 ecb28ae0 2018-07-16 stsp free(wline);
2031 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2032 1a76625f 2018-10-22 stsp goto done;
2033 0553a4e3 2018-04-30 stsp
2034 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2035 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2036 5813d178 2019-03-09 stsp ncommits = 0;
2037 145b6838 2022-06-16 stsp view->maxx = 0;
2038 5813d178 2019-03-09 stsp while (entry) {
2039 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2040 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2041 5813d178 2019-03-09 stsp int width;
2042 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2043 5813d178 2019-03-09 stsp break;
2044 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2045 5813d178 2019-03-09 stsp if (author == NULL) {
2046 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2047 5813d178 2019-03-09 stsp goto done;
2048 5813d178 2019-03-09 stsp }
2049 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2050 27a741e5 2019-09-11 stsp date_display_cols);
2051 5813d178 2019-03-09 stsp if (author_cols < width)
2052 5813d178 2019-03-09 stsp author_cols = width;
2053 5813d178 2019-03-09 stsp free(wauthor);
2054 5813d178 2019-03-09 stsp free(author);
2055 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2056 145b6838 2022-06-16 stsp if (err)
2057 145b6838 2022-06-16 stsp goto done;
2058 145b6838 2022-06-16 stsp msg = msg0;
2059 145b6838 2022-06-16 stsp while (*msg == '\n')
2060 145b6838 2022-06-16 stsp ++msg;
2061 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2062 29688b02 2022-06-16 stsp *eol = '\0';
2063 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2064 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2065 29688b02 2022-06-16 stsp if (err)
2066 29688b02 2022-06-16 stsp goto done;
2067 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2068 145b6838 2022-06-16 stsp free(msg0);
2069 29688b02 2022-06-16 stsp free(wmsg);
2070 7ca04879 2019-10-19 stsp ncommits++;
2071 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2072 5813d178 2019-03-09 stsp }
2073 5813d178 2019-03-09 stsp
2074 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2075 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2076 867c6645 2018-07-10 stsp ncommits = 0;
2077 899d86c2 2018-05-10 stsp while (entry) {
2078 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2079 899d86c2 2018-05-10 stsp break;
2080 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2081 2814baeb 2018-08-01 stsp wstandout(view->window);
2082 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2083 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2084 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2085 2814baeb 2018-08-01 stsp wstandend(view->window);
2086 0553a4e3 2018-04-30 stsp if (err)
2087 60493ae3 2019-06-20 stsp goto done;
2088 0553a4e3 2018-04-30 stsp ncommits++;
2089 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2090 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2091 80ddbec8 2018-04-29 stsp }
2092 80ddbec8 2018-04-29 stsp
2093 9b058f45 2022-06-30 mark view_border(view);
2094 1a76625f 2018-10-22 stsp done:
2095 1a76625f 2018-10-22 stsp free(id_str);
2096 8b473291 2019-02-21 stsp free(refs_str);
2097 1a76625f 2018-10-22 stsp free(ncommits_str);
2098 1a76625f 2018-10-22 stsp free(header);
2099 80ddbec8 2018-04-29 stsp return err;
2100 9f7d7167 2018-04-29 stsp }
2101 07b55e75 2018-05-10 stsp
2102 07b55e75 2018-05-10 stsp static void
2103 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2104 07b55e75 2018-05-10 stsp {
2105 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2106 07b55e75 2018-05-10 stsp int nscrolled = 0;
2107 07b55e75 2018-05-10 stsp
2108 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2109 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2110 07b55e75 2018-05-10 stsp return;
2111 9f7d7167 2018-04-29 stsp
2112 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2113 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2114 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2115 07b55e75 2018-05-10 stsp if (entry) {
2116 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2117 07b55e75 2018-05-10 stsp nscrolled++;
2118 07b55e75 2018-05-10 stsp }
2119 07b55e75 2018-05-10 stsp }
2120 aa075928 2018-05-10 stsp }
2121 aa075928 2018-05-10 stsp
2122 aa075928 2018-05-10 stsp static const struct got_error *
2123 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2124 aa075928 2018-05-10 stsp {
2125 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2126 5e224a3e 2019-02-22 stsp int errcode;
2127 8a42fca8 2019-02-22 stsp
2128 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2129 aa075928 2018-05-10 stsp
2130 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2131 ffe38506 2020-12-01 naddy if (ta->log_complete)
2132 5e224a3e 2019-02-22 stsp break;
2133 b295e71b 2019-02-22 stsp
2134 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2135 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2136 7aafa0d1 2019-02-22 stsp if (errcode)
2137 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2138 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2139 7c1452c1 2020-03-26 stsp
2140 7c1452c1 2020-03-26 stsp /*
2141 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2142 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2143 7c1452c1 2020-03-26 stsp */
2144 7c1452c1 2020-03-26 stsp if (!wait)
2145 7c1452c1 2020-03-26 stsp break;
2146 7c1452c1 2020-03-26 stsp
2147 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2148 ffe38506 2020-12-01 naddy show_log_view(view);
2149 7c1452c1 2020-03-26 stsp update_panels();
2150 7c1452c1 2020-03-26 stsp doupdate();
2151 7c1452c1 2020-03-26 stsp
2152 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2153 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2154 82954512 2020-02-03 stsp if (errcode)
2155 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2156 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2157 82954512 2020-02-03 stsp
2158 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2159 ffe38506 2020-12-01 naddy show_log_view(view);
2160 7c1452c1 2020-03-26 stsp update_panels();
2161 7c1452c1 2020-03-26 stsp doupdate();
2162 5e224a3e 2019-02-22 stsp }
2163 5e224a3e 2019-02-22 stsp
2164 5e224a3e 2019-02-22 stsp return NULL;
2165 5e224a3e 2019-02-22 stsp }
2166 5e224a3e 2019-02-22 stsp
2167 5e224a3e 2019-02-22 stsp static const struct got_error *
2168 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2169 9b058f45 2022-06-30 mark {
2170 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2171 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2172 9b058f45 2022-06-30 mark
2173 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2174 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2175 9b058f45 2022-06-30 mark view->nscrolled = 0;
2176 9b058f45 2022-06-30 mark
2177 9b058f45 2022-06-30 mark return err;
2178 9b058f45 2022-06-30 mark }
2179 9b058f45 2022-06-30 mark
2180 9b058f45 2022-06-30 mark static const struct got_error *
2181 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2182 5e224a3e 2019-02-22 stsp {
2183 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2184 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2185 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2186 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2187 5e224a3e 2019-02-22 stsp
2188 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2189 5e224a3e 2019-02-22 stsp return NULL;
2190 5e224a3e 2019-02-22 stsp
2191 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2192 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2193 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2194 08ebd0a9 2019-02-22 stsp /*
2195 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2196 08ebd0a9 2019-02-22 stsp */
2197 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2198 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2199 5e224a3e 2019-02-22 stsp if (err)
2200 5e224a3e 2019-02-22 stsp return err;
2201 7aafa0d1 2019-02-22 stsp }
2202 b295e71b 2019-02-22 stsp
2203 7aafa0d1 2019-02-22 stsp do {
2204 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2205 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2206 88048b54 2019-02-21 stsp break;
2207 88048b54 2019-02-21 stsp
2208 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2209 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2210 aa075928 2018-05-10 stsp
2211 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2212 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2213 dd0a52c1 2018-05-20 stsp break;
2214 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2215 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2216 aa075928 2018-05-10 stsp
2217 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2218 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2219 9b058f45 2022-06-30 mark else
2220 9b058f45 2022-06-30 mark view->nscrolled = 0;
2221 9b058f45 2022-06-30 mark
2222 dd0a52c1 2018-05-20 stsp return err;
2223 07b55e75 2018-05-10 stsp }
2224 4a7f7875 2018-05-10 stsp
2225 cd0acaa7 2018-05-20 stsp static const struct got_error *
2226 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2227 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2228 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2229 cd0acaa7 2018-05-20 stsp {
2230 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2231 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2232 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2233 cd0acaa7 2018-05-20 stsp
2234 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2235 15a94983 2018-12-23 stsp if (diff_view == NULL)
2236 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2237 ea5e7bb5 2018-08-01 stsp
2238 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2239 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2240 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2241 e5a0f69f 2018-08-18 stsp if (err == NULL)
2242 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2243 cd0acaa7 2018-05-20 stsp return err;
2244 4a7f7875 2018-05-10 stsp }
2245 4a7f7875 2018-05-10 stsp
2246 80ddbec8 2018-04-29 stsp static const struct got_error *
2247 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2248 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2249 9343a5fb 2018-06-23 stsp {
2250 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2251 941e9f74 2019-05-21 stsp
2252 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2253 941e9f74 2019-05-21 stsp if (parent == NULL)
2254 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2255 941e9f74 2019-05-21 stsp
2256 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2257 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2258 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2259 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2260 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2261 941e9f74 2019-05-21 stsp s->tree = subtree;
2262 941e9f74 2019-05-21 stsp s->selected = 0;
2263 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2264 941e9f74 2019-05-21 stsp return NULL;
2265 941e9f74 2019-05-21 stsp }
2266 941e9f74 2019-05-21 stsp
2267 941e9f74 2019-05-21 stsp static const struct got_error *
2268 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2269 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2270 941e9f74 2019-05-21 stsp {
2271 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2272 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2273 941e9f74 2019-05-21 stsp const char *p;
2274 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2275 9343a5fb 2018-06-23 stsp
2276 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2277 941e9f74 2019-05-21 stsp p = path;
2278 941e9f74 2019-05-21 stsp while (*p) {
2279 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2280 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2281 56e0773d 2019-11-28 stsp char *te_name;
2282 33cbf02b 2020-01-12 stsp
2283 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2284 33cbf02b 2020-01-12 stsp p++;
2285 941e9f74 2019-05-21 stsp
2286 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2287 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2288 941e9f74 2019-05-21 stsp if (slash == NULL)
2289 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2290 33cbf02b 2020-01-12 stsp else
2291 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2292 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2293 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2294 56e0773d 2019-11-28 stsp break;
2295 941e9f74 2019-05-21 stsp }
2296 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2297 56e0773d 2019-11-28 stsp if (te == NULL) {
2298 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2299 56e0773d 2019-11-28 stsp free(te_name);
2300 941e9f74 2019-05-21 stsp break;
2301 941e9f74 2019-05-21 stsp }
2302 56e0773d 2019-11-28 stsp free(te_name);
2303 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2304 941e9f74 2019-05-21 stsp
2305 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2306 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2307 b03c880f 2019-05-21 stsp
2308 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2309 941e9f74 2019-05-21 stsp if (slash)
2310 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2311 941e9f74 2019-05-21 stsp else
2312 941e9f74 2019-05-21 stsp subpath = strdup(path);
2313 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2314 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2315 941e9f74 2019-05-21 stsp break;
2316 941e9f74 2019-05-21 stsp }
2317 941e9f74 2019-05-21 stsp
2318 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2319 941e9f74 2019-05-21 stsp subpath);
2320 941e9f74 2019-05-21 stsp if (err)
2321 941e9f74 2019-05-21 stsp break;
2322 941e9f74 2019-05-21 stsp
2323 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2324 941e9f74 2019-05-21 stsp free(tree_id);
2325 941e9f74 2019-05-21 stsp if (err)
2326 941e9f74 2019-05-21 stsp break;
2327 941e9f74 2019-05-21 stsp
2328 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2329 941e9f74 2019-05-21 stsp if (err) {
2330 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2331 941e9f74 2019-05-21 stsp break;
2332 941e9f74 2019-05-21 stsp }
2333 941e9f74 2019-05-21 stsp if (slash == NULL)
2334 941e9f74 2019-05-21 stsp break;
2335 941e9f74 2019-05-21 stsp free(subpath);
2336 941e9f74 2019-05-21 stsp subpath = NULL;
2337 941e9f74 2019-05-21 stsp p = slash;
2338 941e9f74 2019-05-21 stsp }
2339 941e9f74 2019-05-21 stsp
2340 941e9f74 2019-05-21 stsp free(subpath);
2341 1a76625f 2018-10-22 stsp return err;
2342 61266923 2020-01-14 stsp }
2343 61266923 2020-01-14 stsp
2344 61266923 2020-01-14 stsp static const struct got_error *
2345 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2346 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2347 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2348 55cccc34 2020-02-20 stsp {
2349 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2350 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2351 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2352 55cccc34 2020-02-20 stsp
2353 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2354 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2355 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2356 55cccc34 2020-02-20 stsp
2357 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2358 bc573f3b 2021-07-10 stsp if (err)
2359 55cccc34 2020-02-20 stsp return err;
2360 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2361 55cccc34 2020-02-20 stsp
2362 55cccc34 2020-02-20 stsp *new_view = tree_view;
2363 55cccc34 2020-02-20 stsp
2364 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2365 55cccc34 2020-02-20 stsp return NULL;
2366 55cccc34 2020-02-20 stsp
2367 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2368 55cccc34 2020-02-20 stsp }
2369 55cccc34 2020-02-20 stsp
2370 55cccc34 2020-02-20 stsp static const struct got_error *
2371 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2372 61266923 2020-01-14 stsp {
2373 61266923 2020-01-14 stsp sigset_t sigset;
2374 61266923 2020-01-14 stsp int errcode;
2375 61266923 2020-01-14 stsp
2376 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2377 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2378 61266923 2020-01-14 stsp
2379 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2380 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2381 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2382 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2383 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2384 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2385 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2386 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2387 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2388 61266923 2020-01-14 stsp
2389 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2390 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2391 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2392 61266923 2020-01-14 stsp
2393 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2394 61266923 2020-01-14 stsp if (errcode)
2395 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2396 61266923 2020-01-14 stsp
2397 61266923 2020-01-14 stsp return NULL;
2398 1a76625f 2018-10-22 stsp }
2399 1a76625f 2018-10-22 stsp
2400 1a76625f 2018-10-22 stsp static void *
2401 1a76625f 2018-10-22 stsp log_thread(void *arg)
2402 1a76625f 2018-10-22 stsp {
2403 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2404 1a76625f 2018-10-22 stsp int errcode = 0;
2405 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2406 1a76625f 2018-10-22 stsp int done = 0;
2407 61266923 2020-01-14 stsp
2408 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2409 61266923 2020-01-14 stsp if (err)
2410 61266923 2020-01-14 stsp return (void *)err;
2411 1a76625f 2018-10-22 stsp
2412 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2413 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2414 1a76625f 2018-10-22 stsp if (err) {
2415 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2416 1a76625f 2018-10-22 stsp return (void *)err;
2417 1a76625f 2018-10-22 stsp err = NULL;
2418 1a76625f 2018-10-22 stsp done = 1;
2419 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2420 1a76625f 2018-10-22 stsp a->commits_needed--;
2421 1a76625f 2018-10-22 stsp
2422 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2423 3abe8080 2019-04-10 stsp if (errcode) {
2424 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2425 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2426 3abe8080 2019-04-10 stsp break;
2427 3abe8080 2019-04-10 stsp } else if (*a->quit)
2428 1a76625f 2018-10-22 stsp done = 1;
2429 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2430 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2431 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2432 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2433 1a76625f 2018-10-22 stsp }
2434 1a76625f 2018-10-22 stsp
2435 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2436 7c1452c1 2020-03-26 stsp if (errcode) {
2437 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2438 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2439 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2440 7c1452c1 2020-03-26 stsp break;
2441 7c1452c1 2020-03-26 stsp }
2442 7c1452c1 2020-03-26 stsp
2443 1a76625f 2018-10-22 stsp if (done)
2444 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2445 7c1452c1 2020-03-26 stsp else {
2446 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2447 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2448 7c1452c1 2020-03-26 stsp &tog_mutex);
2449 7c1452c1 2020-03-26 stsp if (errcode)
2450 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2451 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2452 21355643 2020-12-06 stsp if (*a->quit)
2453 21355643 2020-12-06 stsp done = 1;
2454 7c1452c1 2020-03-26 stsp }
2455 1a76625f 2018-10-22 stsp }
2456 1a76625f 2018-10-22 stsp
2457 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2458 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2459 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2460 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2461 1a76625f 2018-10-22 stsp }
2462 3abe8080 2019-04-10 stsp a->log_complete = 1;
2463 1a76625f 2018-10-22 stsp return (void *)err;
2464 1a76625f 2018-10-22 stsp }
2465 1a76625f 2018-10-22 stsp
2466 1a76625f 2018-10-22 stsp static const struct got_error *
2467 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2468 1a76625f 2018-10-22 stsp {
2469 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2470 1a76625f 2018-10-22 stsp int errcode;
2471 1a76625f 2018-10-22 stsp
2472 1a76625f 2018-10-22 stsp if (s->thread) {
2473 1a76625f 2018-10-22 stsp s->quit = 1;
2474 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2475 1a76625f 2018-10-22 stsp if (errcode)
2476 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2477 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2479 1a76625f 2018-10-22 stsp if (errcode)
2480 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2481 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2482 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2483 1a76625f 2018-10-22 stsp if (errcode)
2484 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2485 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2486 1a76625f 2018-10-22 stsp if (errcode)
2487 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2488 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2489 1a76625f 2018-10-22 stsp s->thread = NULL;
2490 1a76625f 2018-10-22 stsp }
2491 1a76625f 2018-10-22 stsp
2492 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2493 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2494 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2495 74467cc8 2022-06-15 stsp }
2496 74467cc8 2022-06-15 stsp
2497 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2498 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2499 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2500 74467cc8 2022-06-15 stsp if (err == NULL)
2501 74467cc8 2022-06-15 stsp err = pack_err;
2502 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2503 1a76625f 2018-10-22 stsp }
2504 1a76625f 2018-10-22 stsp
2505 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2506 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2507 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2508 1a76625f 2018-10-22 stsp }
2509 1a76625f 2018-10-22 stsp
2510 9343a5fb 2018-06-23 stsp return err;
2511 9343a5fb 2018-06-23 stsp }
2512 9343a5fb 2018-06-23 stsp
2513 9343a5fb 2018-06-23 stsp static const struct got_error *
2514 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2515 1a76625f 2018-10-22 stsp {
2516 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2517 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2518 276b94a1 2020-11-13 naddy int errcode;
2519 1a76625f 2018-10-22 stsp
2520 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2521 276b94a1 2020-11-13 naddy
2522 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2523 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2524 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2525 276b94a1 2020-11-13 naddy
2526 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2527 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2528 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2529 276b94a1 2020-11-13 naddy
2530 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2531 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2532 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2533 1a76625f 2018-10-22 stsp free(s->start_id);
2534 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2535 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2536 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2537 1a76625f 2018-10-22 stsp return err;
2538 1a76625f 2018-10-22 stsp }
2539 1a76625f 2018-10-22 stsp
2540 1a76625f 2018-10-22 stsp static const struct got_error *
2541 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2542 60493ae3 2019-06-20 stsp {
2543 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2544 60493ae3 2019-06-20 stsp
2545 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2546 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2547 60493ae3 2019-06-20 stsp return NULL;
2548 60493ae3 2019-06-20 stsp }
2549 60493ae3 2019-06-20 stsp
2550 60493ae3 2019-06-20 stsp static const struct got_error *
2551 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2552 60493ae3 2019-06-20 stsp {
2553 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2554 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2555 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2556 60493ae3 2019-06-20 stsp
2557 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2558 f9686aa5 2020-03-27 stsp show_log_view(view);
2559 f9686aa5 2020-03-27 stsp update_panels();
2560 f9686aa5 2020-03-27 stsp doupdate();
2561 f9686aa5 2020-03-27 stsp
2562 96e2b566 2019-07-08 stsp if (s->search_entry) {
2563 13add988 2019-10-15 stsp int errcode, ch;
2564 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2565 13add988 2019-10-15 stsp if (errcode)
2566 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2567 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2568 13add988 2019-10-15 stsp ch = wgetch(view->window);
2569 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2570 13add988 2019-10-15 stsp if (errcode)
2571 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2572 13add988 2019-10-15 stsp "pthread_mutex_lock");
2573 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2574 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2575 678cbce5 2019-07-28 stsp return NULL;
2576 678cbce5 2019-07-28 stsp }
2577 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2578 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2579 96e2b566 2019-07-08 stsp else
2580 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2581 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2582 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2583 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2584 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2585 364ac6fd 2022-06-18 stsp
2586 364ac6fd 2022-06-18 stsp /*
2587 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2588 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2589 f704b35c 2022-06-18 stsp * might have changed.
2590 364ac6fd 2022-06-18 stsp */
2591 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2592 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2593 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2594 364ac6fd 2022-06-18 stsp else
2595 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2596 4bfe9f0a 2022-06-18 stsp } else {
2597 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2598 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2599 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2600 364ac6fd 2022-06-18 stsp else
2601 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2602 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2603 4bfe9f0a 2022-06-18 stsp }
2604 20be8d96 2019-06-21 stsp } else {
2605 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2606 20be8d96 2019-06-21 stsp }
2607 60493ae3 2019-06-20 stsp
2608 60493ae3 2019-06-20 stsp while (1) {
2609 13add988 2019-10-15 stsp int have_match = 0;
2610 13add988 2019-10-15 stsp
2611 60493ae3 2019-06-20 stsp if (entry == NULL) {
2612 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2613 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2614 f9967bca 2020-03-27 stsp view->search_next_done =
2615 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2616 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2617 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2618 f801134a 2019-06-25 stsp return NULL;
2619 60493ae3 2019-06-20 stsp }
2620 96e2b566 2019-07-08 stsp /*
2621 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2622 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2623 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2624 96e2b566 2019-07-08 stsp */
2625 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2626 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2627 60493ae3 2019-06-20 stsp }
2628 60493ae3 2019-06-20 stsp
2629 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2630 13add988 2019-10-15 stsp &view->regex);
2631 5943eee2 2019-08-13 stsp if (err)
2632 13add988 2019-10-15 stsp break;
2633 13add988 2019-10-15 stsp if (have_match) {
2634 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2635 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2636 60493ae3 2019-06-20 stsp break;
2637 60493ae3 2019-06-20 stsp }
2638 13add988 2019-10-15 stsp
2639 96e2b566 2019-07-08 stsp s->search_entry = entry;
2640 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2641 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2642 b1bf1435 2019-06-21 stsp else
2643 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2644 60493ae3 2019-06-20 stsp }
2645 60493ae3 2019-06-20 stsp
2646 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2647 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2648 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2649 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2650 60493ae3 2019-06-20 stsp if (err)
2651 60493ae3 2019-06-20 stsp return err;
2652 ead14cbe 2019-06-21 stsp cur++;
2653 ead14cbe 2019-06-21 stsp }
2654 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2655 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2656 60493ae3 2019-06-20 stsp if (err)
2657 60493ae3 2019-06-20 stsp return err;
2658 ead14cbe 2019-06-21 stsp cur--;
2659 60493ae3 2019-06-20 stsp }
2660 60493ae3 2019-06-20 stsp }
2661 60493ae3 2019-06-20 stsp
2662 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2663 96e2b566 2019-07-08 stsp
2664 60493ae3 2019-06-20 stsp return NULL;
2665 60493ae3 2019-06-20 stsp }
2666 60493ae3 2019-06-20 stsp
2667 60493ae3 2019-06-20 stsp static const struct got_error *
2668 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2669 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2670 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2671 80ddbec8 2018-04-29 stsp {
2672 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2673 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2674 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2675 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2676 1a76625f 2018-10-22 stsp int errcode;
2677 80ddbec8 2018-04-29 stsp
2678 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2679 f135c941 2020-02-20 stsp free(s->in_repo_path);
2680 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2681 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2682 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2683 f135c941 2020-02-20 stsp }
2684 ecb28ae0 2018-07-16 stsp
2685 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2686 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2687 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2688 78756c87 2020-11-24 stsp
2689 fb2756b9 2018-08-04 stsp s->repo = repo;
2690 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2691 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2692 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2693 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2694 9cd7cbd1 2020-12-07 stsp goto done;
2695 9cd7cbd1 2020-12-07 stsp }
2696 9cd7cbd1 2020-12-07 stsp }
2697 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2698 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2699 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2700 5036bf37 2018-09-24 stsp goto done;
2701 5036bf37 2018-09-24 stsp }
2702 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2703 e5a0f69f 2018-08-18 stsp
2704 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2705 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2706 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2707 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2708 11b20872 2019-11-08 stsp if (err)
2709 11b20872 2019-11-08 stsp goto done;
2710 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2711 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2712 11b20872 2019-11-08 stsp if (err) {
2713 11b20872 2019-11-08 stsp free_colors(&s->colors);
2714 11b20872 2019-11-08 stsp goto done;
2715 11b20872 2019-11-08 stsp }
2716 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2717 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2718 11b20872 2019-11-08 stsp if (err) {
2719 11b20872 2019-11-08 stsp free_colors(&s->colors);
2720 11b20872 2019-11-08 stsp goto done;
2721 11b20872 2019-11-08 stsp }
2722 11b20872 2019-11-08 stsp }
2723 11b20872 2019-11-08 stsp
2724 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2725 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2726 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2727 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2728 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2729 1a76625f 2018-10-22 stsp
2730 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2731 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2732 74467cc8 2022-06-15 stsp if (err)
2733 74467cc8 2022-06-15 stsp goto done;
2734 74467cc8 2022-06-15 stsp }
2735 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2736 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2737 0ae84acc 2022-06-15 tracey if (err)
2738 0ae84acc 2022-06-15 tracey goto done;
2739 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2740 b672a97a 2020-01-27 stsp !s->log_branches);
2741 1a76625f 2018-10-22 stsp if (err)
2742 1a76625f 2018-10-22 stsp goto done;
2743 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2744 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2745 c5b78334 2020-01-12 stsp if (err)
2746 c5b78334 2020-01-12 stsp goto done;
2747 1a76625f 2018-10-22 stsp
2748 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2749 1a76625f 2018-10-22 stsp if (errcode) {
2750 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2751 1a76625f 2018-10-22 stsp goto done;
2752 1a76625f 2018-10-22 stsp }
2753 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2754 7c1452c1 2020-03-26 stsp if (errcode) {
2755 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2756 7c1452c1 2020-03-26 stsp goto done;
2757 7c1452c1 2020-03-26 stsp }
2758 1a76625f 2018-10-22 stsp
2759 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2760 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2761 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2762 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2763 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2764 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2765 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2766 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2767 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2768 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2769 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2770 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2771 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2772 ba4f502b 2018-08-04 stsp done:
2773 1a76625f 2018-10-22 stsp if (err)
2774 1a76625f 2018-10-22 stsp close_log_view(view);
2775 ba4f502b 2018-08-04 stsp return err;
2776 ba4f502b 2018-08-04 stsp }
2777 ba4f502b 2018-08-04 stsp
2778 e5a0f69f 2018-08-18 stsp static const struct got_error *
2779 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2780 ba4f502b 2018-08-04 stsp {
2781 f2f6d207 2020-11-24 stsp const struct got_error *err;
2782 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2783 ba4f502b 2018-08-04 stsp
2784 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2785 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2786 2b380cc8 2018-10-24 stsp &s->thread_args);
2787 2b380cc8 2018-10-24 stsp if (errcode)
2788 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2789 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2790 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2791 f2f6d207 2020-11-24 stsp if (err)
2792 f2f6d207 2020-11-24 stsp return err;
2793 f2f6d207 2020-11-24 stsp }
2794 2b380cc8 2018-10-24 stsp }
2795 2b380cc8 2018-10-24 stsp
2796 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2797 b880cc75 2022-06-30 mark }
2798 b880cc75 2022-06-30 mark
2799 b880cc75 2022-06-30 mark static void
2800 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2801 b880cc75 2022-06-30 mark {
2802 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2803 b880cc75 2022-06-30 mark
2804 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2805 b880cc75 2022-06-30 mark view->count = 0;
2806 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2807 b880cc75 2022-06-30 mark return;
2808 b880cc75 2022-06-30 mark
2809 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2810 b880cc75 2022-06-30 mark || home)
2811 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2812 b880cc75 2022-06-30 mark
2813 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2814 b880cc75 2022-06-30 mark --s->selected;
2815 b880cc75 2022-06-30 mark else
2816 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2817 b880cc75 2022-06-30 mark
2818 b880cc75 2022-06-30 mark select_commit(s);
2819 b880cc75 2022-06-30 mark return;
2820 b880cc75 2022-06-30 mark }
2821 b880cc75 2022-06-30 mark
2822 b880cc75 2022-06-30 mark static const struct got_error *
2823 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2824 b880cc75 2022-06-30 mark {
2825 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2826 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2827 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2828 b880cc75 2022-06-30 mark
2829 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2830 b880cc75 2022-06-30 mark if (first == NULL) {
2831 b880cc75 2022-06-30 mark view->count = 0;
2832 b880cc75 2022-06-30 mark return NULL;
2833 b880cc75 2022-06-30 mark }
2834 b880cc75 2022-06-30 mark
2835 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2836 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2837 b880cc75 2022-06-30 mark return NULL;
2838 b880cc75 2022-06-30 mark
2839 b880cc75 2022-06-30 mark if (!page) {
2840 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2841 b880cc75 2022-06-30 mark
2842 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
2843 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2844 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2845 b880cc75 2022-06-30 mark ++s->selected;
2846 b880cc75 2022-06-30 mark else
2847 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2848 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
2849 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2850 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2851 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2852 b880cc75 2022-06-30 mark else
2853 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2854 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2855 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2856 b880cc75 2022-06-30 mark } else {
2857 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2858 b880cc75 2022-06-30 mark if (err)
2859 b880cc75 2022-06-30 mark return err;
2860 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2861 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2862 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2863 b880cc75 2022-06-30 mark }
2864 b880cc75 2022-06-30 mark }
2865 b880cc75 2022-06-30 mark if (err)
2866 b880cc75 2022-06-30 mark return err;
2867 b880cc75 2022-06-30 mark
2868 9b058f45 2022-06-30 mark /*
2869 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2870 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2871 9b058f45 2022-06-30 mark */
2872 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2873 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2874 9b058f45 2022-06-30 mark
2875 b880cc75 2022-06-30 mark select_commit(s);
2876 b880cc75 2022-06-30 mark
2877 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2878 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2879 b880cc75 2022-06-30 mark view->count = 0;
2880 b880cc75 2022-06-30 mark
2881 b880cc75 2022-06-30 mark return NULL;
2882 e5a0f69f 2018-08-18 stsp }
2883 04cc582a 2018-08-01 stsp
2884 9b058f45 2022-06-30 mark /*
2885 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2886 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2887 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2888 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2889 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2890 9b058f45 2022-06-30 mark */
2891 9b058f45 2022-06-30 mark static void
2892 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2893 9b058f45 2022-06-30 mark {
2894 9b058f45 2022-06-30 mark char *mode;
2895 9b058f45 2022-06-30 mark
2896 76364b2d 2022-07-02 mark *x = 0;
2897 76364b2d 2022-07-02 mark *y = 0;
2898 76364b2d 2022-07-02 mark
2899 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2900 9b058f45 2022-06-30 mark
2901 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2902 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2903 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2904 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2905 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2906 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2907 9b058f45 2022-06-30 mark }
2908 9b058f45 2022-06-30 mark }
2909 9b058f45 2022-06-30 mark
2910 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2911 e5a0f69f 2018-08-18 stsp static const struct got_error *
2912 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2913 9b058f45 2022-06-30 mark {
2914 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2915 9b058f45 2022-06-30 mark
2916 9b058f45 2022-06-30 mark view->nlines = y;
2917 9b058f45 2022-06-30 mark err = view_resize(view);
2918 9b058f45 2022-06-30 mark if (err)
2919 9b058f45 2022-06-30 mark return err;
2920 9b058f45 2022-06-30 mark
2921 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2922 9b058f45 2022-06-30 mark
2923 9b058f45 2022-06-30 mark return err;
2924 9b058f45 2022-06-30 mark }
2925 9b058f45 2022-06-30 mark
2926 9b058f45 2022-06-30 mark static const struct got_error *
2927 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2928 e5a0f69f 2018-08-18 stsp {
2929 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2930 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2931 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2932 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2933 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2934 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
2935 80ddbec8 2018-04-29 stsp
2936 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2937 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
2938 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2939 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2940 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2941 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
2942 fb280deb 2021-08-30 stsp }
2943 b880cc75 2022-06-30 mark return err;
2944 528dedf3 2021-08-30 stsp }
2945 0dca135e 2022-06-30 mark
2946 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
2947 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
2948 0dca135e 2022-06-30 mark --eos; /* border */
2949 0dca135e 2022-06-30 mark
2950 528dedf3 2021-08-30 stsp switch (ch) {
2951 1e37a5c2 2019-05-12 jcs case 'q':
2952 1e37a5c2 2019-05-12 jcs s->quit = 1;
2953 145b6838 2022-06-16 stsp break;
2954 145b6838 2022-06-16 stsp case '0':
2955 145b6838 2022-06-16 stsp view->x = 0;
2956 145b6838 2022-06-16 stsp break;
2957 145b6838 2022-06-16 stsp case '$':
2958 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2959 640cd7ff 2022-06-22 mark view->count = 0;
2960 145b6838 2022-06-16 stsp break;
2961 145b6838 2022-06-16 stsp case KEY_RIGHT:
2962 145b6838 2022-06-16 stsp case 'l':
2963 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2964 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2965 640cd7ff 2022-06-22 mark else
2966 640cd7ff 2022-06-22 mark view->count = 0;
2967 1e37a5c2 2019-05-12 jcs break;
2968 145b6838 2022-06-16 stsp case KEY_LEFT:
2969 145b6838 2022-06-16 stsp case 'h':
2970 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2971 640cd7ff 2022-06-22 mark if (view->x <= 0)
2972 640cd7ff 2022-06-22 mark view->count = 0;
2973 145b6838 2022-06-16 stsp break;
2974 1e37a5c2 2019-05-12 jcs case 'k':
2975 1e37a5c2 2019-05-12 jcs case KEY_UP:
2976 1e37a5c2 2019-05-12 jcs case '<':
2977 1e37a5c2 2019-05-12 jcs case ',':
2978 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2979 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2980 912a3f79 2021-08-30 j break;
2981 912a3f79 2021-08-30 j case 'g':
2982 912a3f79 2021-08-30 j case KEY_HOME:
2983 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2984 640cd7ff 2022-06-22 mark view->count = 0;
2985 1e37a5c2 2019-05-12 jcs break;
2986 83cc4199 2022-06-13 stsp case CTRL('u'):
2987 33c3719a 2022-06-15 stsp case 'u':
2988 83cc4199 2022-06-13 stsp nscroll /= 2;
2989 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2990 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2991 a4292ac5 2019-05-12 jcs case CTRL('b'):
2992 61417565 2022-06-20 mark case 'b':
2993 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2994 1e37a5c2 2019-05-12 jcs break;
2995 1e37a5c2 2019-05-12 jcs case 'j':
2996 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2997 1e37a5c2 2019-05-12 jcs case '>':
2998 1e37a5c2 2019-05-12 jcs case '.':
2999 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3000 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3001 912a3f79 2021-08-30 j break;
3002 912a3f79 2021-08-30 j case 'G':
3003 912a3f79 2021-08-30 j case KEY_END: {
3004 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3005 912a3f79 2021-08-30 j * traverse them all. */
3006 640cd7ff 2022-06-22 mark view->count = 0;
3007 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3008 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3009 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3010 80ddbec8 2018-04-29 stsp }
3011 912a3f79 2021-08-30 j
3012 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3013 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3014 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3015 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3016 f3bc9f1d 2021-09-05 naddy break;
3017 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3018 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3019 f3bc9f1d 2021-09-05 naddy }
3020 f3bc9f1d 2021-09-05 naddy if (n > 0)
3021 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3022 2b779855 2020-12-05 naddy select_commit(s);
3023 1e37a5c2 2019-05-12 jcs break;
3024 912a3f79 2021-08-30 j }
3025 80b7e8da 2022-06-11 stsp case CTRL('d'):
3026 33c3719a 2022-06-15 stsp case 'd':
3027 83cc4199 2022-06-13 stsp nscroll /= 2;
3028 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3029 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3030 61417565 2022-06-20 mark case CTRL('f'):
3031 48bb96f0 2022-06-20 naddy case 'f':
3032 b880cc75 2022-06-30 mark case ' ':
3033 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3034 1e37a5c2 2019-05-12 jcs break;
3035 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3036 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3037 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3038 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3039 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3040 2b779855 2020-12-05 naddy select_commit(s);
3041 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3042 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3043 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3044 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3045 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3046 0bf7f153 2020-12-02 naddy }
3047 1e37a5c2 2019-05-12 jcs break;
3048 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3049 49b24ee5 2022-07-03 mark case '\r':
3050 640cd7ff 2022-06-22 mark view->count = 0;
3051 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3052 e5a0f69f 2018-08-18 stsp break;
3053 9b058f45 2022-06-30 mark
3054 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3055 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3056 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3057 9b058f45 2022-06-30 mark
3058 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3059 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3060 78756c87 2020-11-24 stsp view, s->repo);
3061 1e37a5c2 2019-05-12 jcs if (err)
3062 1e37a5c2 2019-05-12 jcs break;
3063 9b058f45 2022-06-30 mark
3064 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3065 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3066 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3067 9b058f45 2022-06-30 mark if (err)
3068 9b058f45 2022-06-30 mark break;
3069 9b058f45 2022-06-30 mark }
3070 9b058f45 2022-06-30 mark
3071 e78dc838 2020-12-04 stsp view->focussed = 0;
3072 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3073 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3074 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3075 9b058f45 2022-06-30 mark
3076 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3077 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3078 f7013a22 2018-10-24 stsp if (err)
3079 1e37a5c2 2019-05-12 jcs return err;
3080 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3081 0dbbbe90 2022-06-17 op if (err)
3082 0dbbbe90 2022-06-17 op return err;
3083 e78dc838 2020-12-04 stsp view->focus_child = 1;
3084 1e37a5c2 2019-05-12 jcs } else
3085 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3086 1e37a5c2 2019-05-12 jcs break;
3087 1e37a5c2 2019-05-12 jcs case 't':
3088 640cd7ff 2022-06-22 mark view->count = 0;
3089 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3090 5036bf37 2018-09-24 stsp break;
3091 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3092 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3093 49b24ee5 2022-07-03 mark err = browse_commit_tree(&tree_view, begin_y, begin_x,
3094 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3095 4e97c21c 2020-12-06 stsp s->repo);
3096 1e37a5c2 2019-05-12 jcs if (err)
3097 e5a0f69f 2018-08-18 stsp break;
3098 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3099 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3100 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3101 49b24ee5 2022-07-03 mark if (err)
3102 49b24ee5 2022-07-03 mark break;
3103 49b24ee5 2022-07-03 mark }
3104 e78dc838 2020-12-04 stsp view->focussed = 0;
3105 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3106 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
3107 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
3108 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3109 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3110 1e37a5c2 2019-05-12 jcs if (err)
3111 1e37a5c2 2019-05-12 jcs return err;
3112 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3113 0dbbbe90 2022-06-17 op if (err)
3114 0dbbbe90 2022-06-17 op return err;
3115 e78dc838 2020-12-04 stsp view->focus_child = 1;
3116 1e37a5c2 2019-05-12 jcs } else
3117 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3118 1e37a5c2 2019-05-12 jcs break;
3119 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3120 21355643 2020-12-06 stsp case CTRL('l'):
3121 21355643 2020-12-06 stsp case 'B':
3122 640cd7ff 2022-06-22 mark view->count = 0;
3123 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3124 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3125 1e37a5c2 2019-05-12 jcs break;
3126 21355643 2020-12-06 stsp err = stop_log_thread(s);
3127 74cfe85e 2020-10-20 stsp if (err)
3128 74cfe85e 2020-10-20 stsp return err;
3129 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3130 21355643 2020-12-06 stsp char *parent_path;
3131 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3132 21355643 2020-12-06 stsp if (err)
3133 21355643 2020-12-06 stsp return err;
3134 21355643 2020-12-06 stsp free(s->in_repo_path);
3135 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3136 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3137 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3138 21355643 2020-12-06 stsp struct got_object_id *start_id;
3139 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3140 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3141 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3142 21355643 2020-12-06 stsp if (err)
3143 21355643 2020-12-06 stsp return err;
3144 21355643 2020-12-06 stsp free(s->start_id);
3145 21355643 2020-12-06 stsp s->start_id = start_id;
3146 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3147 21355643 2020-12-06 stsp } else /* 'B' */
3148 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3149 21355643 2020-12-06 stsp
3150 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3151 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3152 b0dd8d27 2022-06-16 stsp if (err)
3153 b0dd8d27 2022-06-16 stsp return err;
3154 b0dd8d27 2022-06-16 stsp }
3155 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3156 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3157 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3158 74cfe85e 2020-10-20 stsp if (err)
3159 21355643 2020-12-06 stsp return err;
3160 51a10b52 2020-12-26 stsp tog_free_refs();
3161 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3162 ca51c541 2020-12-07 stsp if (err)
3163 ca51c541 2020-12-07 stsp return err;
3164 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3165 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3166 d01904d4 2019-06-25 stsp if (err)
3167 d01904d4 2019-06-25 stsp return err;
3168 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3169 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3170 b672a97a 2020-01-27 stsp if (err)
3171 b672a97a 2020-01-27 stsp return err;
3172 21355643 2020-12-06 stsp free_commits(&s->commits);
3173 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3174 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3175 21355643 2020-12-06 stsp s->selected_entry = NULL;
3176 21355643 2020-12-06 stsp s->selected = 0;
3177 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3178 21355643 2020-12-06 stsp s->quit = 0;
3179 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3180 dfee752e 2022-06-18 op s->matched_entry = NULL;
3181 dfee752e 2022-06-18 op s->search_entry = NULL;
3182 d01904d4 2019-06-25 stsp break;
3183 6458efa5 2020-11-24 stsp case 'r':
3184 640cd7ff 2022-06-22 mark view->count = 0;
3185 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3186 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
3187 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3188 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3189 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3190 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3191 6458efa5 2020-11-24 stsp if (err) {
3192 6458efa5 2020-11-24 stsp view_close(ref_view);
3193 6458efa5 2020-11-24 stsp return err;
3194 6458efa5 2020-11-24 stsp }
3195 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
3196 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
3197 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
3198 49b24ee5 2022-07-03 mark if (err)
3199 49b24ee5 2022-07-03 mark break;
3200 49b24ee5 2022-07-03 mark }
3201 e78dc838 2020-12-04 stsp view->focussed = 0;
3202 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3203 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
3204 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
3205 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3206 6458efa5 2020-11-24 stsp err = view_close_child(view);
3207 6458efa5 2020-11-24 stsp if (err)
3208 6458efa5 2020-11-24 stsp return err;
3209 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3210 0dbbbe90 2022-06-17 op if (err)
3211 0dbbbe90 2022-06-17 op return err;
3212 e78dc838 2020-12-04 stsp view->focus_child = 1;
3213 6458efa5 2020-11-24 stsp } else
3214 6458efa5 2020-11-24 stsp *new_view = ref_view;
3215 6458efa5 2020-11-24 stsp break;
3216 1e37a5c2 2019-05-12 jcs default:
3217 640cd7ff 2022-06-22 mark view->count = 0;
3218 1e37a5c2 2019-05-12 jcs break;
3219 899d86c2 2018-05-10 stsp }
3220 e5a0f69f 2018-08-18 stsp
3221 80ddbec8 2018-04-29 stsp return err;
3222 80ddbec8 2018-04-29 stsp }
3223 80ddbec8 2018-04-29 stsp
3224 4ed7e80c 2018-05-20 stsp static const struct got_error *
3225 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3226 c2db6724 2019-01-04 stsp {
3227 c2db6724 2019-01-04 stsp const struct got_error *error;
3228 c2db6724 2019-01-04 stsp
3229 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3230 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3231 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3232 37c06ea4 2019-07-15 stsp #endif
3233 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3234 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3235 c2db6724 2019-01-04 stsp
3236 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3237 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3238 c2db6724 2019-01-04 stsp
3239 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3240 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3241 c2db6724 2019-01-04 stsp
3242 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3243 c2db6724 2019-01-04 stsp if (error != NULL)
3244 c2db6724 2019-01-04 stsp return error;
3245 c2db6724 2019-01-04 stsp
3246 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3247 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3248 c2db6724 2019-01-04 stsp
3249 c2db6724 2019-01-04 stsp return NULL;
3250 c2db6724 2019-01-04 stsp }
3251 c2db6724 2019-01-04 stsp
3252 a915003a 2019-02-05 stsp static void
3253 a915003a 2019-02-05 stsp init_curses(void)
3254 a915003a 2019-02-05 stsp {
3255 2497f032 2022-05-31 stsp /*
3256 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3257 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3258 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3259 2497f032 2022-05-31 stsp */
3260 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3261 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3262 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3263 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3264 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3265 2497f032 2022-05-31 stsp
3266 a915003a 2019-02-05 stsp initscr();
3267 a915003a 2019-02-05 stsp cbreak();
3268 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3269 a915003a 2019-02-05 stsp noecho();
3270 a915003a 2019-02-05 stsp nonl();
3271 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3272 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3273 a915003a 2019-02-05 stsp curs_set(0);
3274 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3275 6d17833f 2019-11-08 stsp start_color();
3276 6d17833f 2019-11-08 stsp use_default_colors();
3277 6d17833f 2019-11-08 stsp }
3278 a915003a 2019-02-05 stsp }
3279 a915003a 2019-02-05 stsp
3280 c2db6724 2019-01-04 stsp static const struct got_error *
3281 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3282 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3283 f135c941 2020-02-20 stsp {
3284 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3285 f135c941 2020-02-20 stsp
3286 f135c941 2020-02-20 stsp if (argc == 0) {
3287 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3288 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3289 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3290 f135c941 2020-02-20 stsp return NULL;
3291 f135c941 2020-02-20 stsp }
3292 f135c941 2020-02-20 stsp
3293 f135c941 2020-02-20 stsp if (worktree) {
3294 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3295 bfd61697 2020-11-14 stsp char *p;
3296 f135c941 2020-02-20 stsp
3297 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3298 f135c941 2020-02-20 stsp if (err)
3299 f135c941 2020-02-20 stsp return err;
3300 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3301 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3302 bfd61697 2020-11-14 stsp p) == -1) {
3303 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3304 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3305 f135c941 2020-02-20 stsp }
3306 f135c941 2020-02-20 stsp free(p);
3307 f135c941 2020-02-20 stsp } else
3308 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3309 f135c941 2020-02-20 stsp
3310 f135c941 2020-02-20 stsp return err;
3311 f135c941 2020-02-20 stsp }
3312 f135c941 2020-02-20 stsp
3313 f135c941 2020-02-20 stsp static const struct got_error *
3314 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3315 9f7d7167 2018-04-29 stsp {
3316 80ddbec8 2018-04-29 stsp const struct got_error *error;
3317 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3318 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3319 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3320 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3321 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3322 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3323 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3324 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3325 04cc582a 2018-08-01 stsp struct tog_view *view;
3326 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3327 80ddbec8 2018-04-29 stsp
3328 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3329 80ddbec8 2018-04-29 stsp switch (ch) {
3330 b672a97a 2020-01-27 stsp case 'b':
3331 b672a97a 2020-01-27 stsp log_branches = 1;
3332 b672a97a 2020-01-27 stsp break;
3333 80ddbec8 2018-04-29 stsp case 'c':
3334 80ddbec8 2018-04-29 stsp start_commit = optarg;
3335 80ddbec8 2018-04-29 stsp break;
3336 ecb28ae0 2018-07-16 stsp case 'r':
3337 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3338 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3339 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3340 9ba1d308 2019-10-21 stsp optarg);
3341 ecb28ae0 2018-07-16 stsp break;
3342 80ddbec8 2018-04-29 stsp default:
3343 17020d27 2019-03-07 stsp usage_log();
3344 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3345 80ddbec8 2018-04-29 stsp }
3346 80ddbec8 2018-04-29 stsp }
3347 80ddbec8 2018-04-29 stsp
3348 80ddbec8 2018-04-29 stsp argc -= optind;
3349 80ddbec8 2018-04-29 stsp argv += optind;
3350 80ddbec8 2018-04-29 stsp
3351 f135c941 2020-02-20 stsp if (argc > 1)
3352 f135c941 2020-02-20 stsp usage_log();
3353 963f97a1 2019-03-18 stsp
3354 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3355 0ae84acc 2022-06-15 tracey if (error != NULL)
3356 0ae84acc 2022-06-15 tracey goto done;
3357 0ae84acc 2022-06-15 tracey
3358 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3359 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3360 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3361 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3362 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3363 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3364 c156c7a4 2020-12-18 stsp goto done;
3365 a1fbf39a 2019-08-11 stsp if (worktree)
3366 6962eb72 2020-02-20 stsp repo_path =
3367 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3368 a1fbf39a 2019-08-11 stsp else
3369 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3370 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3371 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3372 c156c7a4 2020-12-18 stsp goto done;
3373 c156c7a4 2020-12-18 stsp }
3374 ecb28ae0 2018-07-16 stsp }
3375 ecb28ae0 2018-07-16 stsp
3376 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3377 80ddbec8 2018-04-29 stsp if (error != NULL)
3378 ecb28ae0 2018-07-16 stsp goto done;
3379 80ddbec8 2018-04-29 stsp
3380 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3381 f135c941 2020-02-20 stsp repo, worktree);
3382 f135c941 2020-02-20 stsp if (error)
3383 f135c941 2020-02-20 stsp goto done;
3384 f135c941 2020-02-20 stsp
3385 f135c941 2020-02-20 stsp init_curses();
3386 f135c941 2020-02-20 stsp
3387 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3388 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3389 c02c541e 2019-03-29 stsp if (error)
3390 c02c541e 2019-03-29 stsp goto done;
3391 c02c541e 2019-03-29 stsp
3392 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3393 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3394 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3395 87670572 2020-12-26 naddy if (error)
3396 87670572 2020-12-26 naddy goto done;
3397 87670572 2020-12-26 naddy }
3398 51a10b52 2020-12-26 stsp
3399 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3400 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3401 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3402 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3403 d8f38dc4 2020-12-05 stsp if (error)
3404 d8f38dc4 2020-12-05 stsp goto done;
3405 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3406 d8f38dc4 2020-12-05 stsp } else {
3407 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3408 d8f38dc4 2020-12-05 stsp if (error == NULL)
3409 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3410 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3411 d8f38dc4 2020-12-05 stsp goto done;
3412 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3413 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3414 d8f38dc4 2020-12-05 stsp if (error)
3415 d8f38dc4 2020-12-05 stsp goto done;
3416 d8f38dc4 2020-12-05 stsp }
3417 8b473291 2019-02-21 stsp
3418 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3419 04cc582a 2018-08-01 stsp if (view == NULL) {
3420 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3421 04cc582a 2018-08-01 stsp goto done;
3422 04cc582a 2018-08-01 stsp }
3423 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3424 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3425 ba4f502b 2018-08-04 stsp if (error)
3426 ba4f502b 2018-08-04 stsp goto done;
3427 2fc00ff4 2019-08-31 stsp if (worktree) {
3428 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3429 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3430 2fc00ff4 2019-08-31 stsp worktree = NULL;
3431 2fc00ff4 2019-08-31 stsp }
3432 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3433 ecb28ae0 2018-07-16 stsp done:
3434 f135c941 2020-02-20 stsp free(in_repo_path);
3435 ecb28ae0 2018-07-16 stsp free(repo_path);
3436 ecb28ae0 2018-07-16 stsp free(cwd);
3437 899d86c2 2018-05-10 stsp free(start_id);
3438 d8f38dc4 2020-12-05 stsp free(label);
3439 d8f38dc4 2020-12-05 stsp if (ref)
3440 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3441 1d0f4054 2021-06-17 stsp if (repo) {
3442 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3443 1d0f4054 2021-06-17 stsp if (error == NULL)
3444 1d0f4054 2021-06-17 stsp error = close_err;
3445 1d0f4054 2021-06-17 stsp }
3446 ec142235 2019-03-07 stsp if (worktree)
3447 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3448 0ae84acc 2022-06-15 tracey if (pack_fds) {
3449 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3450 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3451 0ae84acc 2022-06-15 tracey if (error == NULL)
3452 0ae84acc 2022-06-15 tracey error = pack_err;
3453 0ae84acc 2022-06-15 tracey }
3454 51a10b52 2020-12-26 stsp tog_free_refs();
3455 80ddbec8 2018-04-29 stsp return error;
3456 9f7d7167 2018-04-29 stsp }
3457 9f7d7167 2018-04-29 stsp
3458 4ed7e80c 2018-05-20 stsp __dead static void
3459 9f7d7167 2018-04-29 stsp usage_diff(void)
3460 9f7d7167 2018-04-29 stsp {
3461 80ddbec8 2018-04-29 stsp endwin();
3462 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3463 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3464 9f7d7167 2018-04-29 stsp exit(1);
3465 b304db33 2018-05-20 stsp }
3466 b304db33 2018-05-20 stsp
3467 6d17833f 2019-11-08 stsp static int
3468 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3469 41605754 2020-11-12 stsp regmatch_t *regmatch)
3470 6d17833f 2019-11-08 stsp {
3471 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3472 6d17833f 2019-11-08 stsp }
3473 6d17833f 2019-11-08 stsp
3474 336075a4 2022-06-25 op static struct tog_color *
3475 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3476 6d17833f 2019-11-08 stsp {
3477 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3478 6d17833f 2019-11-08 stsp
3479 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3480 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3481 6d17833f 2019-11-08 stsp return tc;
3482 6d17833f 2019-11-08 stsp }
3483 6d17833f 2019-11-08 stsp
3484 6d17833f 2019-11-08 stsp return NULL;
3485 6d17833f 2019-11-08 stsp }
3486 6d17833f 2019-11-08 stsp
3487 4ed7e80c 2018-05-20 stsp static const struct got_error *
3488 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3489 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3490 41605754 2020-11-12 stsp {
3491 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3492 44a87665 2022-06-16 stsp char *exstr = NULL;
3493 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3494 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3495 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3496 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3497 41605754 2020-11-12 stsp
3498 41605754 2020-11-12 stsp *wtotal = 0;
3499 1853e0f4 2022-06-16 stsp
3500 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3501 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3502 41605754 2020-11-12 stsp
3503 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3504 44a87665 2022-06-16 stsp if (err)
3505 44a87665 2022-06-16 stsp return err;
3506 44a87665 2022-06-16 stsp
3507 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3508 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3509 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3510 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3511 44a87665 2022-06-16 stsp goto done;
3512 44a87665 2022-06-16 stsp }
3513 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3514 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3515 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3516 1853e0f4 2022-06-16 stsp goto done;
3517 1853e0f4 2022-06-16 stsp }
3518 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3519 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3520 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3521 1853e0f4 2022-06-16 stsp goto done;
3522 1853e0f4 2022-06-16 stsp }
3523 145b6838 2022-06-16 stsp
3524 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3525 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3526 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3527 1853e0f4 2022-06-16 stsp if (err)
3528 1853e0f4 2022-06-16 stsp goto done;
3529 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3530 145b6838 2022-06-16 stsp if (n) {
3531 1853e0f4 2022-06-16 stsp free(wline);
3532 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3533 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3534 1853e0f4 2022-06-16 stsp if (err)
3535 1853e0f4 2022-06-16 stsp goto done;
3536 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3537 1853e0f4 2022-06-16 stsp wlimit -= width;
3538 1853e0f4 2022-06-16 stsp *wtotal += width;
3539 41605754 2020-11-12 stsp }
3540 41605754 2020-11-12 stsp
3541 41605754 2020-11-12 stsp if (wlimit > 0) {
3542 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3543 1853e0f4 2022-06-16 stsp size_t wlen;
3544 1853e0f4 2022-06-16 stsp
3545 1853e0f4 2022-06-16 stsp free(wline);
3546 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3547 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3548 1853e0f4 2022-06-16 stsp if (err)
3549 1853e0f4 2022-06-16 stsp goto done;
3550 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3551 1853e0f4 2022-06-16 stsp while (i < wlen) {
3552 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3553 1853e0f4 2022-06-16 stsp if (width == -1) {
3554 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3555 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3556 1853e0f4 2022-06-16 stsp goto done;
3557 1853e0f4 2022-06-16 stsp }
3558 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3559 1853e0f4 2022-06-16 stsp break;
3560 61417565 2022-06-20 mark w += width;
3561 1853e0f4 2022-06-16 stsp i++;
3562 41605754 2020-11-12 stsp }
3563 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3564 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3565 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3566 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3567 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3568 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3569 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3570 41605754 2020-11-12 stsp }
3571 41605754 2020-11-12 stsp }
3572 41605754 2020-11-12 stsp
3573 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3574 1853e0f4 2022-06-16 stsp free(wline);
3575 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3576 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3577 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3578 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3579 1853e0f4 2022-06-16 stsp if (err)
3580 1853e0f4 2022-06-16 stsp goto done;
3581 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3582 1853e0f4 2022-06-16 stsp } else {
3583 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3584 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3585 1853e0f4 2022-06-16 stsp if (err)
3586 1853e0f4 2022-06-16 stsp goto done;
3587 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3588 1853e0f4 2022-06-16 stsp }
3589 1853e0f4 2022-06-16 stsp *wtotal += width2;
3590 41605754 2020-11-12 stsp }
3591 1853e0f4 2022-06-16 stsp done:
3592 145b6838 2022-06-16 stsp free(wline);
3593 44a87665 2022-06-16 stsp free(exstr);
3594 1853e0f4 2022-06-16 stsp free(seg0);
3595 1853e0f4 2022-06-16 stsp free(seg1);
3596 1853e0f4 2022-06-16 stsp free(seg2);
3597 1853e0f4 2022-06-16 stsp return err;
3598 41605754 2020-11-12 stsp }
3599 41605754 2020-11-12 stsp
3600 41605754 2020-11-12 stsp static const struct got_error *
3601 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3602 26ed57b2 2018-05-19 stsp {
3603 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3604 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3605 61e69b96 2018-05-20 stsp const struct got_error *err;
3606 fe621944 2020-11-10 stsp int nprinted = 0;
3607 b304db33 2018-05-20 stsp char *line;
3608 826082fe 2020-12-10 stsp size_t linesize = 0;
3609 826082fe 2020-12-10 stsp ssize_t linelen;
3610 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3611 61e69b96 2018-05-20 stsp wchar_t *wline;
3612 e0b650dd 2018-05-20 stsp int width;
3613 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3614 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3615 fe621944 2020-11-10 stsp off_t line_offset;
3616 26ed57b2 2018-05-19 stsp
3617 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3618 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3619 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3620 fe621944 2020-11-10 stsp
3621 f7d12f7e 2018-08-01 stsp werase(view->window);
3622 a3404814 2018-09-02 stsp
3623 a3404814 2018-09-02 stsp if (header) {
3624 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3625 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3626 135a2da0 2020-11-11 stsp header) == -1)
3627 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3628 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3629 ccda2f4d 2022-06-16 stsp 0, 0);
3630 135a2da0 2020-11-11 stsp free(line);
3631 135a2da0 2020-11-11 stsp if (err)
3632 a3404814 2018-09-02 stsp return err;
3633 a3404814 2018-09-02 stsp
3634 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3635 a3404814 2018-09-02 stsp wstandout(view->window);
3636 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3637 e54cc94a 2020-11-11 stsp free(wline);
3638 e54cc94a 2020-11-11 stsp wline = NULL;
3639 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3640 a3404814 2018-09-02 stsp wstandend(view->window);
3641 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3642 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3643 26ed57b2 2018-05-19 stsp
3644 a3404814 2018-09-02 stsp if (max_lines <= 1)
3645 a3404814 2018-09-02 stsp return NULL;
3646 a3404814 2018-09-02 stsp max_lines--;
3647 a3404814 2018-09-02 stsp }
3648 a3404814 2018-09-02 stsp
3649 89f1a395 2020-12-01 naddy s->eof = 0;
3650 145b6838 2022-06-16 stsp view->maxx = 0;
3651 826082fe 2020-12-10 stsp line = NULL;
3652 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3653 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3654 826082fe 2020-12-10 stsp if (linelen == -1) {
3655 826082fe 2020-12-10 stsp if (feof(s->f)) {
3656 826082fe 2020-12-10 stsp s->eof = 1;
3657 826082fe 2020-12-10 stsp break;
3658 826082fe 2020-12-10 stsp }
3659 826082fe 2020-12-10 stsp free(line);
3660 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3661 61e69b96 2018-05-20 stsp }
3662 145b6838 2022-06-16 stsp
3663 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3664 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3665 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3666 1853e0f4 2022-06-16 stsp if (err) {
3667 1853e0f4 2022-06-16 stsp free(line);
3668 1853e0f4 2022-06-16 stsp return err;
3669 1853e0f4 2022-06-16 stsp }
3670 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3671 1853e0f4 2022-06-16 stsp free(wline);
3672 1853e0f4 2022-06-16 stsp wline = NULL;
3673 1853e0f4 2022-06-16 stsp
3674 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3675 f26dddb7 2019-11-08 stsp if (tc)
3676 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3677 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3678 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3679 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3680 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3681 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3682 41605754 2020-11-12 stsp if (err) {
3683 41605754 2020-11-12 stsp free(line);
3684 41605754 2020-11-12 stsp return err;
3685 41605754 2020-11-12 stsp }
3686 41605754 2020-11-12 stsp } else {
3687 969c159c 2022-06-16 stsp int skip;
3688 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3689 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3690 969c159c 2022-06-16 stsp if (err) {
3691 969c159c 2022-06-16 stsp free(line);
3692 969c159c 2022-06-16 stsp return err;
3693 969c159c 2022-06-16 stsp }
3694 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3695 969c159c 2022-06-16 stsp free(wline);
3696 41605754 2020-11-12 stsp wline = NULL;
3697 41605754 2020-11-12 stsp }
3698 f26dddb7 2019-11-08 stsp if (tc)
3699 6d17833f 2019-11-08 stsp wattr_off(view->window,
3700 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3701 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3702 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3703 fe621944 2020-11-10 stsp nprinted++;
3704 826082fe 2020-12-10 stsp }
3705 826082fe 2020-12-10 stsp free(line);
3706 fe621944 2020-11-10 stsp if (nprinted >= 1)
3707 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3708 89f1a395 2020-12-01 naddy (nprinted - 1);
3709 fe621944 2020-11-10 stsp else
3710 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3711 26ed57b2 2018-05-19 stsp
3712 9b058f45 2022-06-30 mark view_border(view);
3713 c3e9aa98 2019-05-13 jcs
3714 89f1a395 2020-12-01 naddy if (s->eof) {
3715 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3716 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3717 c3e9aa98 2019-05-13 jcs nprinted++;
3718 c3e9aa98 2019-05-13 jcs }
3719 c3e9aa98 2019-05-13 jcs
3720 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3721 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3722 c3e9aa98 2019-05-13 jcs if (err) {
3723 c3e9aa98 2019-05-13 jcs return err;
3724 c3e9aa98 2019-05-13 jcs }
3725 26ed57b2 2018-05-19 stsp
3726 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3727 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3728 e54cc94a 2020-11-11 stsp free(wline);
3729 e54cc94a 2020-11-11 stsp wline = NULL;
3730 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3731 c3e9aa98 2019-05-13 jcs }
3732 c3e9aa98 2019-05-13 jcs
3733 26ed57b2 2018-05-19 stsp return NULL;
3734 abd2672a 2018-12-23 stsp }
3735 abd2672a 2018-12-23 stsp
3736 abd2672a 2018-12-23 stsp static char *
3737 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3738 abd2672a 2018-12-23 stsp {
3739 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3740 09867e48 2019-08-13 stsp char *p, *s;
3741 09867e48 2019-08-13 stsp
3742 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3743 09867e48 2019-08-13 stsp if (tm == NULL)
3744 09867e48 2019-08-13 stsp return NULL;
3745 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3746 09867e48 2019-08-13 stsp if (s == NULL)
3747 09867e48 2019-08-13 stsp return NULL;
3748 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3749 abd2672a 2018-12-23 stsp if (p)
3750 abd2672a 2018-12-23 stsp *p = '\0';
3751 abd2672a 2018-12-23 stsp return s;
3752 9f7d7167 2018-04-29 stsp }
3753 9f7d7167 2018-04-29 stsp
3754 4ed7e80c 2018-05-20 stsp static const struct got_error *
3755 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3756 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3757 0208f208 2020-05-05 stsp {
3758 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3759 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3760 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3761 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3762 0208f208 2020-05-05 stsp
3763 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3764 0208f208 2020-05-05 stsp if (qid != NULL) {
3765 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3766 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3767 d7b5a0e8 2022-04-20 stsp &qid->id);
3768 0208f208 2020-05-05 stsp if (err)
3769 0208f208 2020-05-05 stsp return err;
3770 0208f208 2020-05-05 stsp
3771 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3772 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3773 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3774 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3775 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3776 aa8b5dd0 2021-08-01 stsp }
3777 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3778 0208f208 2020-05-05 stsp
3779 0208f208 2020-05-05 stsp }
3780 0208f208 2020-05-05 stsp
3781 0208f208 2020-05-05 stsp if (tree_id1) {
3782 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3783 0208f208 2020-05-05 stsp if (err)
3784 0208f208 2020-05-05 stsp goto done;
3785 0208f208 2020-05-05 stsp }
3786 0208f208 2020-05-05 stsp
3787 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3788 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3789 0208f208 2020-05-05 stsp if (err)
3790 0208f208 2020-05-05 stsp goto done;
3791 0208f208 2020-05-05 stsp
3792 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3793 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3794 0208f208 2020-05-05 stsp done:
3795 0208f208 2020-05-05 stsp if (tree1)
3796 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3797 0208f208 2020-05-05 stsp if (tree2)
3798 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3799 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3800 0208f208 2020-05-05 stsp return err;
3801 0208f208 2020-05-05 stsp }
3802 0208f208 2020-05-05 stsp
3803 0208f208 2020-05-05 stsp static const struct got_error *
3804 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3805 abd2672a 2018-12-23 stsp {
3806 fe621944 2020-11-10 stsp off_t *p;
3807 fe621944 2020-11-10 stsp
3808 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3809 fe621944 2020-11-10 stsp if (p == NULL)
3810 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3811 fe621944 2020-11-10 stsp *line_offsets = p;
3812 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3813 fe621944 2020-11-10 stsp (*nlines)++;
3814 fe621944 2020-11-10 stsp return NULL;
3815 fe621944 2020-11-10 stsp }
3816 fe621944 2020-11-10 stsp
3817 fe621944 2020-11-10 stsp static const struct got_error *
3818 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3819 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3820 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3821 fe621944 2020-11-10 stsp {
3822 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3823 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3824 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3825 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3826 45d799e2 2018-12-23 stsp time_t committer_time;
3827 45d799e2 2018-12-23 stsp const char *author, *committer;
3828 8b473291 2019-02-21 stsp char *refs_str = NULL;
3829 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3830 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3831 fe621944 2020-11-10 stsp off_t outoff = 0;
3832 fe621944 2020-11-10 stsp int n;
3833 abd2672a 2018-12-23 stsp
3834 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3835 0208f208 2020-05-05 stsp
3836 8b473291 2019-02-21 stsp if (refs) {
3837 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3838 8b473291 2019-02-21 stsp if (err)
3839 8b473291 2019-02-21 stsp return err;
3840 8b473291 2019-02-21 stsp }
3841 8b473291 2019-02-21 stsp
3842 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3843 abd2672a 2018-12-23 stsp if (err)
3844 abd2672a 2018-12-23 stsp return err;
3845 abd2672a 2018-12-23 stsp
3846 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3847 15a94983 2018-12-23 stsp if (err) {
3848 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3849 15a94983 2018-12-23 stsp goto done;
3850 15a94983 2018-12-23 stsp }
3851 abd2672a 2018-12-23 stsp
3852 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3853 fe621944 2020-11-10 stsp if (err)
3854 fe621944 2020-11-10 stsp goto done;
3855 fe621944 2020-11-10 stsp
3856 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3857 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3858 fe621944 2020-11-10 stsp if (n < 0) {
3859 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3860 abd2672a 2018-12-23 stsp goto done;
3861 abd2672a 2018-12-23 stsp }
3862 fe621944 2020-11-10 stsp outoff += n;
3863 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3864 fe621944 2020-11-10 stsp if (err)
3865 fe621944 2020-11-10 stsp goto done;
3866 fe621944 2020-11-10 stsp
3867 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3868 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3869 fe621944 2020-11-10 stsp if (n < 0) {
3870 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3871 abd2672a 2018-12-23 stsp goto done;
3872 abd2672a 2018-12-23 stsp }
3873 fe621944 2020-11-10 stsp outoff += n;
3874 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3875 fe621944 2020-11-10 stsp if (err)
3876 fe621944 2020-11-10 stsp goto done;
3877 fe621944 2020-11-10 stsp
3878 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3879 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3880 fe621944 2020-11-10 stsp if (datestr) {
3881 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3882 fe621944 2020-11-10 stsp if (n < 0) {
3883 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3884 fe621944 2020-11-10 stsp goto done;
3885 fe621944 2020-11-10 stsp }
3886 fe621944 2020-11-10 stsp outoff += n;
3887 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3888 fe621944 2020-11-10 stsp if (err)
3889 fe621944 2020-11-10 stsp goto done;
3890 abd2672a 2018-12-23 stsp }
3891 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3892 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3893 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3894 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3895 fe621944 2020-11-10 stsp if (n < 0) {
3896 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3897 fe621944 2020-11-10 stsp goto done;
3898 fe621944 2020-11-10 stsp }
3899 fe621944 2020-11-10 stsp outoff += n;
3900 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3901 fe621944 2020-11-10 stsp if (err)
3902 fe621944 2020-11-10 stsp goto done;
3903 abd2672a 2018-12-23 stsp }
3904 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3905 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3906 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3907 9f98ca05 2021-09-24 stsp int pn = 1;
3908 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3909 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3910 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3911 9f98ca05 2021-09-24 stsp if (err)
3912 9f98ca05 2021-09-24 stsp goto done;
3913 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3914 9f98ca05 2021-09-24 stsp if (n < 0) {
3915 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3916 9f98ca05 2021-09-24 stsp goto done;
3917 9f98ca05 2021-09-24 stsp }
3918 9f98ca05 2021-09-24 stsp outoff += n;
3919 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3920 9f98ca05 2021-09-24 stsp if (err)
3921 9f98ca05 2021-09-24 stsp goto done;
3922 9f98ca05 2021-09-24 stsp free(id_str);
3923 9f98ca05 2021-09-24 stsp id_str = NULL;
3924 9f98ca05 2021-09-24 stsp }
3925 9f98ca05 2021-09-24 stsp }
3926 9f98ca05 2021-09-24 stsp
3927 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3928 5943eee2 2019-08-13 stsp if (err)
3929 5943eee2 2019-08-13 stsp goto done;
3930 fe621944 2020-11-10 stsp s = logmsg;
3931 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3932 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3933 fe621944 2020-11-10 stsp if (n < 0) {
3934 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3935 fe621944 2020-11-10 stsp goto done;
3936 fe621944 2020-11-10 stsp }
3937 fe621944 2020-11-10 stsp outoff += n;
3938 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3939 fe621944 2020-11-10 stsp if (err)
3940 fe621944 2020-11-10 stsp goto done;
3941 abd2672a 2018-12-23 stsp }
3942 fe621944 2020-11-10 stsp
3943 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3944 0208f208 2020-05-05 stsp if (err)
3945 0208f208 2020-05-05 stsp goto done;
3946 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3947 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3948 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3949 fe621944 2020-11-10 stsp if (n < 0) {
3950 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3951 fe621944 2020-11-10 stsp goto done;
3952 fe621944 2020-11-10 stsp }
3953 fe621944 2020-11-10 stsp outoff += n;
3954 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3955 fe621944 2020-11-10 stsp if (err)
3956 fe621944 2020-11-10 stsp goto done;
3957 0208f208 2020-05-05 stsp free((char *)pe->path);
3958 0208f208 2020-05-05 stsp free(pe->data);
3959 0208f208 2020-05-05 stsp }
3960 fe621944 2020-11-10 stsp
3961 0208f208 2020-05-05 stsp fputc('\n', outfile);
3962 fe621944 2020-11-10 stsp outoff++;
3963 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3964 abd2672a 2018-12-23 stsp done:
3965 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3966 abd2672a 2018-12-23 stsp free(id_str);
3967 5943eee2 2019-08-13 stsp free(logmsg);
3968 8b473291 2019-02-21 stsp free(refs_str);
3969 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3970 7510f233 2020-08-09 stsp if (err) {
3971 ae6a6978 2020-08-09 stsp free(*line_offsets);
3972 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3973 ae6a6978 2020-08-09 stsp *nlines = 0;
3974 7510f233 2020-08-09 stsp }
3975 fe621944 2020-11-10 stsp return err;
3976 abd2672a 2018-12-23 stsp }
3977 abd2672a 2018-12-23 stsp
3978 abd2672a 2018-12-23 stsp static const struct got_error *
3979 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3980 26ed57b2 2018-05-19 stsp {
3981 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3982 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3983 15a94983 2018-12-23 stsp int obj_type;
3984 fe621944 2020-11-10 stsp
3985 fe621944 2020-11-10 stsp free(s->line_offsets);
3986 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3987 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3988 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3989 fe621944 2020-11-10 stsp s->nlines = 0;
3990 26ed57b2 2018-05-19 stsp
3991 511a516b 2018-05-19 stsp f = got_opentemp();
3992 48ae06ee 2018-10-18 stsp if (f == NULL) {
3993 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3994 48ae06ee 2018-10-18 stsp goto done;
3995 48ae06ee 2018-10-18 stsp }
3996 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3997 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3998 fb43ecf1 2019-02-11 stsp goto done;
3999 fb43ecf1 2019-02-11 stsp }
4000 48ae06ee 2018-10-18 stsp s->f = f;
4001 26ed57b2 2018-05-19 stsp
4002 15a94983 2018-12-23 stsp if (s->id1)
4003 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4004 15a94983 2018-12-23 stsp else
4005 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4006 15a94983 2018-12-23 stsp if (err)
4007 15a94983 2018-12-23 stsp goto done;
4008 15a94983 2018-12-23 stsp
4009 15a94983 2018-12-23 stsp switch (obj_type) {
4010 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4011 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4012 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4013 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4014 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4015 26ed57b2 2018-05-19 stsp break;
4016 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4017 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4018 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4019 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4020 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4021 26ed57b2 2018-05-19 stsp break;
4022 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4023 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4024 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4025 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4026 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4027 abd2672a 2018-12-23 stsp
4028 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4029 abd2672a 2018-12-23 stsp if (err)
4030 3ffacbe1 2020-02-02 tracey goto done;
4031 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4032 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4033 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4034 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4035 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4036 f44b1f58 2020-02-02 tracey if (err)
4037 f44b1f58 2020-02-02 tracey goto done;
4038 f44b1f58 2020-02-02 tracey } else {
4039 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4040 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4041 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4042 fe621944 2020-11-10 stsp err = write_commit_info(
4043 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4044 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4045 f44b1f58 2020-02-02 tracey if (err)
4046 f44b1f58 2020-02-02 tracey goto done;
4047 f5404e4e 2020-02-02 tracey break;
4048 15a087fe 2019-02-21 stsp }
4049 abd2672a 2018-12-23 stsp }
4050 abd2672a 2018-12-23 stsp }
4051 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4052 abd2672a 2018-12-23 stsp
4053 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4054 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4055 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4056 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4057 26ed57b2 2018-05-19 stsp break;
4058 abd2672a 2018-12-23 stsp }
4059 26ed57b2 2018-05-19 stsp default:
4060 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4061 48ae06ee 2018-10-18 stsp break;
4062 26ed57b2 2018-05-19 stsp }
4063 f44b1f58 2020-02-02 tracey if (err)
4064 f44b1f58 2020-02-02 tracey goto done;
4065 48ae06ee 2018-10-18 stsp done:
4066 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4067 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4068 48ae06ee 2018-10-18 stsp return err;
4069 48ae06ee 2018-10-18 stsp }
4070 26ed57b2 2018-05-19 stsp
4071 f5215bb9 2019-02-22 stsp static void
4072 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4073 f5215bb9 2019-02-22 stsp {
4074 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4075 f5215bb9 2019-02-22 stsp update_panels();
4076 f5215bb9 2019-02-22 stsp doupdate();
4077 f44b1f58 2020-02-02 tracey }
4078 f44b1f58 2020-02-02 tracey
4079 f44b1f58 2020-02-02 tracey static const struct got_error *
4080 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4081 f44b1f58 2020-02-02 tracey {
4082 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4083 f44b1f58 2020-02-02 tracey
4084 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4085 f44b1f58 2020-02-02 tracey return NULL;
4086 f44b1f58 2020-02-02 tracey }
4087 f44b1f58 2020-02-02 tracey
4088 f44b1f58 2020-02-02 tracey static const struct got_error *
4089 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4090 f44b1f58 2020-02-02 tracey {
4091 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4092 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4093 f44b1f58 2020-02-02 tracey int lineno;
4094 cb713507 2022-06-16 stsp char *line = NULL;
4095 826082fe 2020-12-10 stsp size_t linesize = 0;
4096 826082fe 2020-12-10 stsp ssize_t linelen;
4097 f44b1f58 2020-02-02 tracey
4098 f44b1f58 2020-02-02 tracey if (!view->searching) {
4099 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4100 f44b1f58 2020-02-02 tracey return NULL;
4101 f44b1f58 2020-02-02 tracey }
4102 f44b1f58 2020-02-02 tracey
4103 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4104 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4105 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4106 f44b1f58 2020-02-02 tracey else
4107 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4108 487cd7d2 2021-12-17 stsp } else
4109 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4110 f44b1f58 2020-02-02 tracey
4111 f44b1f58 2020-02-02 tracey while (1) {
4112 f44b1f58 2020-02-02 tracey off_t offset;
4113 f44b1f58 2020-02-02 tracey
4114 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4115 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4116 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4117 f44b1f58 2020-02-02 tracey break;
4118 f44b1f58 2020-02-02 tracey }
4119 f44b1f58 2020-02-02 tracey
4120 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4121 f44b1f58 2020-02-02 tracey lineno = 1;
4122 f44b1f58 2020-02-02 tracey else
4123 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4124 f44b1f58 2020-02-02 tracey }
4125 f44b1f58 2020-02-02 tracey
4126 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4127 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4128 f44b1f58 2020-02-02 tracey free(line);
4129 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4130 f44b1f58 2020-02-02 tracey }
4131 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4132 cb713507 2022-06-16 stsp if (linelen != -1) {
4133 cb713507 2022-06-16 stsp char *exstr;
4134 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4135 cb713507 2022-06-16 stsp if (err)
4136 cb713507 2022-06-16 stsp break;
4137 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4138 cb713507 2022-06-16 stsp &view->regmatch)) {
4139 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4140 cb713507 2022-06-16 stsp s->matched_line = lineno;
4141 cb713507 2022-06-16 stsp free(exstr);
4142 cb713507 2022-06-16 stsp break;
4143 cb713507 2022-06-16 stsp }
4144 cb713507 2022-06-16 stsp free(exstr);
4145 f44b1f58 2020-02-02 tracey }
4146 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4147 f44b1f58 2020-02-02 tracey lineno++;
4148 f44b1f58 2020-02-02 tracey else
4149 f44b1f58 2020-02-02 tracey lineno--;
4150 f44b1f58 2020-02-02 tracey }
4151 826082fe 2020-12-10 stsp free(line);
4152 f44b1f58 2020-02-02 tracey
4153 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4154 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4155 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4156 f44b1f58 2020-02-02 tracey }
4157 f44b1f58 2020-02-02 tracey
4158 145b6838 2022-06-16 stsp return err;
4159 f5215bb9 2019-02-22 stsp }
4160 f5215bb9 2019-02-22 stsp
4161 48ae06ee 2018-10-18 stsp static const struct got_error *
4162 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4163 b72706c3 2022-06-01 stsp {
4164 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4165 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4166 b72706c3 2022-06-01 stsp
4167 b72706c3 2022-06-01 stsp free(s->id1);
4168 b72706c3 2022-06-01 stsp s->id1 = NULL;
4169 b72706c3 2022-06-01 stsp free(s->id2);
4170 b72706c3 2022-06-01 stsp s->id2 = NULL;
4171 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4172 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4173 b72706c3 2022-06-01 stsp s->f = NULL;
4174 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4175 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4176 b72706c3 2022-06-01 stsp s->f1 = NULL;
4177 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4178 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4179 b72706c3 2022-06-01 stsp s->f2 = NULL;
4180 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4181 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4182 f9d37699 2022-06-28 stsp s->fd1 = -1;
4183 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4184 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4185 f9d37699 2022-06-28 stsp s->fd2 = -1;
4186 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4187 b72706c3 2022-06-01 stsp free(s->line_offsets);
4188 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4189 b72706c3 2022-06-01 stsp s->nlines = 0;
4190 b72706c3 2022-06-01 stsp return err;
4191 b72706c3 2022-06-01 stsp }
4192 b72706c3 2022-06-01 stsp
4193 b72706c3 2022-06-01 stsp static const struct got_error *
4194 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4195 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4196 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4197 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4198 48ae06ee 2018-10-18 stsp {
4199 48ae06ee 2018-10-18 stsp const struct got_error *err;
4200 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4201 5dc9f4bc 2018-08-04 stsp
4202 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4203 f9d37699 2022-06-28 stsp s->fd1 = -1;
4204 f9d37699 2022-06-28 stsp s->fd2 = -1;
4205 b72706c3 2022-06-01 stsp
4206 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4207 15a94983 2018-12-23 stsp int type1, type2;
4208 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4209 15a94983 2018-12-23 stsp if (err)
4210 15a94983 2018-12-23 stsp return err;
4211 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4212 15a94983 2018-12-23 stsp if (err)
4213 15a94983 2018-12-23 stsp return err;
4214 15a94983 2018-12-23 stsp
4215 15a94983 2018-12-23 stsp if (type1 != type2)
4216 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4217 15a94983 2018-12-23 stsp }
4218 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4219 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4220 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4221 f44b1f58 2020-02-02 tracey s->repo = repo;
4222 f44b1f58 2020-02-02 tracey s->id1 = id1;
4223 f44b1f58 2020-02-02 tracey s->id2 = id2;
4224 3dbaef42 2020-11-24 stsp s->label1 = label1;
4225 3dbaef42 2020-11-24 stsp s->label2 = label2;
4226 48ae06ee 2018-10-18 stsp
4227 15a94983 2018-12-23 stsp if (id1) {
4228 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4229 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4230 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4231 48ae06ee 2018-10-18 stsp } else
4232 5465d566 2020-02-01 tracey s->id1 = NULL;
4233 48ae06ee 2018-10-18 stsp
4234 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4235 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4236 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4237 b72706c3 2022-06-01 stsp goto done;
4238 48ae06ee 2018-10-18 stsp }
4239 b72706c3 2022-06-01 stsp
4240 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4241 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4242 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4243 a00719e9 2022-06-17 stsp goto done;
4244 a00719e9 2022-06-17 stsp }
4245 a00719e9 2022-06-17 stsp
4246 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4247 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4248 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4249 b72706c3 2022-06-01 stsp goto done;
4250 b72706c3 2022-06-01 stsp }
4251 b72706c3 2022-06-01 stsp
4252 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4253 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4254 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4255 f9d37699 2022-06-28 stsp goto done;
4256 f9d37699 2022-06-28 stsp }
4257 f9d37699 2022-06-28 stsp
4258 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4259 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4260 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4261 f9d37699 2022-06-28 stsp goto done;
4262 f9d37699 2022-06-28 stsp }
4263 f9d37699 2022-06-28 stsp
4264 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4265 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4266 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4267 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4268 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4269 5465d566 2020-02-01 tracey s->log_view = log_view;
4270 5465d566 2020-02-01 tracey s->repo = repo;
4271 6d17833f 2019-11-08 stsp
4272 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4273 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4274 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4275 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4276 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4277 6d17833f 2019-11-08 stsp if (err)
4278 b72706c3 2022-06-01 stsp goto done;
4279 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4280 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4281 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4282 b72706c3 2022-06-01 stsp if (err)
4283 b72706c3 2022-06-01 stsp goto done;
4284 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4285 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4286 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4287 b72706c3 2022-06-01 stsp if (err)
4288 b72706c3 2022-06-01 stsp goto done;
4289 6d17833f 2019-11-08 stsp
4290 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4291 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4292 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4293 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4294 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4295 b72706c3 2022-06-01 stsp if (err)
4296 b72706c3 2022-06-01 stsp goto done;
4297 11b20872 2019-11-08 stsp
4298 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4299 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4300 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4301 b72706c3 2022-06-01 stsp if (err)
4302 b72706c3 2022-06-01 stsp goto done;
4303 11b20872 2019-11-08 stsp
4304 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4305 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4306 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4307 b72706c3 2022-06-01 stsp if (err)
4308 b72706c3 2022-06-01 stsp goto done;
4309 6d17833f 2019-11-08 stsp }
4310 5dc9f4bc 2018-08-04 stsp
4311 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4312 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4313 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4314 f5215bb9 2019-02-22 stsp
4315 5465d566 2020-02-01 tracey err = create_diff(s);
4316 48ae06ee 2018-10-18 stsp
4317 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4318 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4319 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4320 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4321 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4322 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4323 b72706c3 2022-06-01 stsp done:
4324 b72706c3 2022-06-01 stsp if (err)
4325 b72706c3 2022-06-01 stsp close_diff_view(view);
4326 e5a0f69f 2018-08-18 stsp return err;
4327 5dc9f4bc 2018-08-04 stsp }
4328 5dc9f4bc 2018-08-04 stsp
4329 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4330 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4331 5dc9f4bc 2018-08-04 stsp {
4332 a3404814 2018-09-02 stsp const struct got_error *err;
4333 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4334 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4335 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4336 a3404814 2018-09-02 stsp
4337 a3404814 2018-09-02 stsp if (s->id1) {
4338 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4339 a3404814 2018-09-02 stsp if (err)
4340 a3404814 2018-09-02 stsp return err;
4341 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4342 3dbaef42 2020-11-24 stsp } else
4343 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4344 3dbaef42 2020-11-24 stsp
4345 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4346 a3404814 2018-09-02 stsp if (err)
4347 a3404814 2018-09-02 stsp return err;
4348 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4349 26ed57b2 2018-05-19 stsp
4350 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4352 a3404814 2018-09-02 stsp free(id_str1);
4353 a3404814 2018-09-02 stsp free(id_str2);
4354 a3404814 2018-09-02 stsp return err;
4355 a3404814 2018-09-02 stsp }
4356 a3404814 2018-09-02 stsp free(id_str1);
4357 a3404814 2018-09-02 stsp free(id_str2);
4358 a3404814 2018-09-02 stsp
4359 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4360 267bb3b8 2021-08-01 stsp free(header);
4361 267bb3b8 2021-08-01 stsp return err;
4362 15a087fe 2019-02-21 stsp }
4363 15a087fe 2019-02-21 stsp
4364 15a087fe 2019-02-21 stsp static const struct got_error *
4365 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4366 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4367 15a087fe 2019-02-21 stsp {
4368 d7a04538 2019-02-21 stsp const struct got_error *err;
4369 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4370 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4371 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4372 15a087fe 2019-02-21 stsp
4373 15a087fe 2019-02-21 stsp free(s->id2);
4374 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4375 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4376 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4377 15a087fe 2019-02-21 stsp
4378 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4379 d7a04538 2019-02-21 stsp if (err)
4380 d7a04538 2019-02-21 stsp return err;
4381 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4382 15a087fe 2019-02-21 stsp free(s->id1);
4383 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4384 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4385 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4386 15a087fe 2019-02-21 stsp return NULL;
4387 0cf4efb1 2018-09-29 stsp }
4388 0cf4efb1 2018-09-29 stsp
4389 0cf4efb1 2018-09-29 stsp static const struct got_error *
4390 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4391 917d79a7 2022-07-01 stsp {
4392 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4393 917d79a7 2022-07-01 stsp
4394 917d79a7 2022-07-01 stsp view->count = 0;
4395 917d79a7 2022-07-01 stsp wclear(view->window);
4396 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4397 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4398 917d79a7 2022-07-01 stsp s->matched_line = 0;
4399 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4400 917d79a7 2022-07-01 stsp return create_diff(s);
4401 917d79a7 2022-07-01 stsp }
4402 917d79a7 2022-07-01 stsp
4403 917d79a7 2022-07-01 stsp static const struct got_error *
4404 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4405 e5a0f69f 2018-08-18 stsp {
4406 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4407 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4408 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4409 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4410 826082fe 2020-12-10 stsp char *line = NULL;
4411 826082fe 2020-12-10 stsp size_t linesize = 0;
4412 826082fe 2020-12-10 stsp ssize_t linelen;
4413 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4414 e5a0f69f 2018-08-18 stsp
4415 e5a0f69f 2018-08-18 stsp switch (ch) {
4416 145b6838 2022-06-16 stsp case '0':
4417 145b6838 2022-06-16 stsp view->x = 0;
4418 145b6838 2022-06-16 stsp break;
4419 145b6838 2022-06-16 stsp case '$':
4420 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4421 640cd7ff 2022-06-22 mark view->count = 0;
4422 145b6838 2022-06-16 stsp break;
4423 145b6838 2022-06-16 stsp case KEY_RIGHT:
4424 145b6838 2022-06-16 stsp case 'l':
4425 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4426 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4427 640cd7ff 2022-06-22 mark else
4428 640cd7ff 2022-06-22 mark view->count = 0;
4429 145b6838 2022-06-16 stsp break;
4430 145b6838 2022-06-16 stsp case KEY_LEFT:
4431 145b6838 2022-06-16 stsp case 'h':
4432 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4433 640cd7ff 2022-06-22 mark if (view->x <= 0)
4434 640cd7ff 2022-06-22 mark view->count = 0;
4435 145b6838 2022-06-16 stsp break;
4436 64453f7e 2020-11-21 stsp case 'a':
4437 3dbaef42 2020-11-24 stsp case 'w':
4438 3dbaef42 2020-11-24 stsp if (ch == 'a')
4439 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4440 3dbaef42 2020-11-24 stsp if (ch == 'w')
4441 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4442 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4443 912a3f79 2021-08-30 j break;
4444 912a3f79 2021-08-30 j case 'g':
4445 912a3f79 2021-08-30 j case KEY_HOME:
4446 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4447 640cd7ff 2022-06-22 mark view->count = 0;
4448 64453f7e 2020-11-21 stsp break;
4449 912a3f79 2021-08-30 j case 'G':
4450 912a3f79 2021-08-30 j case KEY_END:
4451 640cd7ff 2022-06-22 mark view->count = 0;
4452 912a3f79 2021-08-30 j if (s->eof)
4453 912a3f79 2021-08-30 j break;
4454 912a3f79 2021-08-30 j
4455 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4456 912a3f79 2021-08-30 j s->eof = 1;
4457 912a3f79 2021-08-30 j break;
4458 1e37a5c2 2019-05-12 jcs case 'k':
4459 1e37a5c2 2019-05-12 jcs case KEY_UP:
4460 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4461 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4462 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4463 640cd7ff 2022-06-22 mark else
4464 640cd7ff 2022-06-22 mark view->count = 0;
4465 1e37a5c2 2019-05-12 jcs break;
4466 83cc4199 2022-06-13 stsp case CTRL('u'):
4467 33c3719a 2022-06-15 stsp case 'u':
4468 83cc4199 2022-06-13 stsp nscroll /= 2;
4469 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4470 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4471 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4472 61417565 2022-06-20 mark case 'b':
4473 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4474 640cd7ff 2022-06-22 mark view->count = 0;
4475 26ed57b2 2018-05-19 stsp break;
4476 640cd7ff 2022-06-22 mark }
4477 1e37a5c2 2019-05-12 jcs i = 0;
4478 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4479 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4480 1e37a5c2 2019-05-12 jcs break;
4481 1e37a5c2 2019-05-12 jcs case 'j':
4482 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4483 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4484 1e37a5c2 2019-05-12 jcs if (!s->eof)
4485 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4486 640cd7ff 2022-06-22 mark else
4487 640cd7ff 2022-06-22 mark view->count = 0;
4488 1e37a5c2 2019-05-12 jcs break;
4489 83cc4199 2022-06-13 stsp case CTRL('d'):
4490 33c3719a 2022-06-15 stsp case 'd':
4491 83cc4199 2022-06-13 stsp nscroll /= 2;
4492 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4493 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4494 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4495 61417565 2022-06-20 mark case 'f':
4496 1e37a5c2 2019-05-12 jcs case ' ':
4497 640cd7ff 2022-06-22 mark if (s->eof) {
4498 640cd7ff 2022-06-22 mark view->count = 0;
4499 1e37a5c2 2019-05-12 jcs break;
4500 640cd7ff 2022-06-22 mark }
4501 1e37a5c2 2019-05-12 jcs i = 0;
4502 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4503 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4504 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4505 826082fe 2020-12-10 stsp if (linelen == -1) {
4506 826082fe 2020-12-10 stsp if (feof(s->f)) {
4507 826082fe 2020-12-10 stsp s->eof = 1;
4508 826082fe 2020-12-10 stsp } else
4509 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4510 34bc9ec9 2019-02-22 stsp break;
4511 826082fe 2020-12-10 stsp }
4512 1e37a5c2 2019-05-12 jcs }
4513 826082fe 2020-12-10 stsp free(line);
4514 1e37a5c2 2019-05-12 jcs break;
4515 1e37a5c2 2019-05-12 jcs case '[':
4516 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4517 1e37a5c2 2019-05-12 jcs s->diff_context--;
4518 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4519 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4520 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4521 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4522 27829c9e 2020-11-21 stsp s->nlines) {
4523 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4524 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4525 27829c9e 2020-11-21 stsp }
4526 640cd7ff 2022-06-22 mark } else
4527 640cd7ff 2022-06-22 mark view->count = 0;
4528 1e37a5c2 2019-05-12 jcs break;
4529 1e37a5c2 2019-05-12 jcs case ']':
4530 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4531 1e37a5c2 2019-05-12 jcs s->diff_context++;
4532 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4533 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4534 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4535 640cd7ff 2022-06-22 mark } else
4536 640cd7ff 2022-06-22 mark view->count = 0;
4537 1e37a5c2 2019-05-12 jcs break;
4538 1e37a5c2 2019-05-12 jcs case '<':
4539 1e37a5c2 2019-05-12 jcs case ',':
4540 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4541 640cd7ff 2022-06-22 mark view->count = 0;
4542 48ae06ee 2018-10-18 stsp break;
4543 640cd7ff 2022-06-22 mark }
4544 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4545 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4546 6524637e 2019-02-21 stsp
4547 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4548 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4549 1e37a5c2 2019-05-12 jcs if (err)
4550 1e37a5c2 2019-05-12 jcs break;
4551 15a087fe 2019-02-21 stsp
4552 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4553 5a8b5076 2020-12-05 stsp break;
4554 5a8b5076 2020-12-05 stsp
4555 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4556 1e37a5c2 2019-05-12 jcs if (err)
4557 1e37a5c2 2019-05-12 jcs break;
4558 15a087fe 2019-02-21 stsp
4559 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4560 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4561 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4562 145b6838 2022-06-16 stsp view->x = 0;
4563 15a087fe 2019-02-21 stsp
4564 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4565 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4566 1e37a5c2 2019-05-12 jcs break;
4567 1e37a5c2 2019-05-12 jcs case '>':
4568 1e37a5c2 2019-05-12 jcs case '.':
4569 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4570 640cd7ff 2022-06-22 mark view->count = 0;
4571 15a087fe 2019-02-21 stsp break;
4572 640cd7ff 2022-06-22 mark }
4573 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4574 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4575 5e224a3e 2019-02-22 stsp
4576 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4577 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4578 1e37a5c2 2019-05-12 jcs if (err)
4579 5a8b5076 2020-12-05 stsp break;
4580 5a8b5076 2020-12-05 stsp
4581 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4582 1e37a5c2 2019-05-12 jcs break;
4583 15a087fe 2019-02-21 stsp
4584 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4585 1e37a5c2 2019-05-12 jcs if (err)
4586 1e37a5c2 2019-05-12 jcs break;
4587 15a087fe 2019-02-21 stsp
4588 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4589 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4590 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4591 145b6838 2022-06-16 stsp view->x = 0;
4592 1e37a5c2 2019-05-12 jcs
4593 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4594 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4595 1e37a5c2 2019-05-12 jcs break;
4596 1e37a5c2 2019-05-12 jcs default:
4597 640cd7ff 2022-06-22 mark view->count = 0;
4598 1e37a5c2 2019-05-12 jcs break;
4599 26ed57b2 2018-05-19 stsp }
4600 e5a0f69f 2018-08-18 stsp
4601 bcbd79e2 2018-08-19 stsp return err;
4602 26ed57b2 2018-05-19 stsp }
4603 26ed57b2 2018-05-19 stsp
4604 4ed7e80c 2018-05-20 stsp static const struct got_error *
4605 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4606 9f7d7167 2018-04-29 stsp {
4607 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4608 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4609 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4610 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4611 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4612 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4613 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4614 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4615 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4616 3dbaef42 2020-11-24 stsp const char *errstr;
4617 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4618 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4619 70ac5f84 2019-03-28 stsp
4620 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4621 26ed57b2 2018-05-19 stsp switch (ch) {
4622 64453f7e 2020-11-21 stsp case 'a':
4623 64453f7e 2020-11-21 stsp force_text_diff = 1;
4624 3dbaef42 2020-11-24 stsp break;
4625 3dbaef42 2020-11-24 stsp case 'C':
4626 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4627 3dbaef42 2020-11-24 stsp &errstr);
4628 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4629 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4630 5a20d08d 2022-02-09 op errstr, errstr);
4631 64453f7e 2020-11-21 stsp break;
4632 09b5bff8 2020-02-23 naddy case 'r':
4633 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4634 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4635 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4636 09b5bff8 2020-02-23 naddy optarg);
4637 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4638 09b5bff8 2020-02-23 naddy break;
4639 3dbaef42 2020-11-24 stsp case 'w':
4640 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4641 3dbaef42 2020-11-24 stsp break;
4642 26ed57b2 2018-05-19 stsp default:
4643 17020d27 2019-03-07 stsp usage_diff();
4644 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4645 26ed57b2 2018-05-19 stsp }
4646 26ed57b2 2018-05-19 stsp }
4647 26ed57b2 2018-05-19 stsp
4648 26ed57b2 2018-05-19 stsp argc -= optind;
4649 26ed57b2 2018-05-19 stsp argv += optind;
4650 26ed57b2 2018-05-19 stsp
4651 26ed57b2 2018-05-19 stsp if (argc == 0) {
4652 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4653 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4654 15a94983 2018-12-23 stsp id_str1 = argv[0];
4655 15a94983 2018-12-23 stsp id_str2 = argv[1];
4656 26ed57b2 2018-05-19 stsp } else
4657 26ed57b2 2018-05-19 stsp usage_diff();
4658 eb6600df 2019-01-04 stsp
4659 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4660 0ae84acc 2022-06-15 tracey if (error)
4661 0ae84acc 2022-06-15 tracey goto done;
4662 0ae84acc 2022-06-15 tracey
4663 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4664 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4665 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4666 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4667 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4668 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4669 c156c7a4 2020-12-18 stsp goto done;
4670 a273ac94 2020-02-23 naddy if (worktree)
4671 a273ac94 2020-02-23 naddy repo_path =
4672 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4673 a273ac94 2020-02-23 naddy else
4674 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4675 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4676 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4677 c156c7a4 2020-12-18 stsp goto done;
4678 c156c7a4 2020-12-18 stsp }
4679 a273ac94 2020-02-23 naddy }
4680 a273ac94 2020-02-23 naddy
4681 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4682 eb6600df 2019-01-04 stsp if (error)
4683 eb6600df 2019-01-04 stsp goto done;
4684 26ed57b2 2018-05-19 stsp
4685 a273ac94 2020-02-23 naddy init_curses();
4686 a273ac94 2020-02-23 naddy
4687 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4688 51a10b52 2020-12-26 stsp if (error)
4689 51a10b52 2020-12-26 stsp goto done;
4690 51a10b52 2020-12-26 stsp
4691 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4692 26ed57b2 2018-05-19 stsp if (error)
4693 26ed57b2 2018-05-19 stsp goto done;
4694 26ed57b2 2018-05-19 stsp
4695 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4696 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4697 26ed57b2 2018-05-19 stsp if (error)
4698 26ed57b2 2018-05-19 stsp goto done;
4699 26ed57b2 2018-05-19 stsp
4700 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4701 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4702 26ed57b2 2018-05-19 stsp if (error)
4703 26ed57b2 2018-05-19 stsp goto done;
4704 26ed57b2 2018-05-19 stsp
4705 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4706 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4707 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4708 ea5e7bb5 2018-08-01 stsp goto done;
4709 ea5e7bb5 2018-08-01 stsp }
4710 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4711 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4712 5dc9f4bc 2018-08-04 stsp if (error)
4713 5dc9f4bc 2018-08-04 stsp goto done;
4714 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4715 26ed57b2 2018-05-19 stsp done:
4716 3dbaef42 2020-11-24 stsp free(label1);
4717 3dbaef42 2020-11-24 stsp free(label2);
4718 c02c541e 2019-03-29 stsp free(repo_path);
4719 a273ac94 2020-02-23 naddy free(cwd);
4720 1d0f4054 2021-06-17 stsp if (repo) {
4721 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4722 1d0f4054 2021-06-17 stsp if (error == NULL)
4723 1d0f4054 2021-06-17 stsp error = close_err;
4724 1d0f4054 2021-06-17 stsp }
4725 a273ac94 2020-02-23 naddy if (worktree)
4726 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4727 0ae84acc 2022-06-15 tracey if (pack_fds) {
4728 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4729 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4730 0ae84acc 2022-06-15 tracey if (error == NULL)
4731 0ae84acc 2022-06-15 tracey error = pack_err;
4732 0ae84acc 2022-06-15 tracey }
4733 51a10b52 2020-12-26 stsp tog_free_refs();
4734 26ed57b2 2018-05-19 stsp return error;
4735 9f7d7167 2018-04-29 stsp }
4736 9f7d7167 2018-04-29 stsp
4737 4ed7e80c 2018-05-20 stsp __dead static void
4738 9f7d7167 2018-04-29 stsp usage_blame(void)
4739 9f7d7167 2018-04-29 stsp {
4740 80ddbec8 2018-04-29 stsp endwin();
4741 87411fa9 2022-06-16 stsp fprintf(stderr,
4742 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4743 9f7d7167 2018-04-29 stsp getprogname());
4744 9f7d7167 2018-04-29 stsp exit(1);
4745 9f7d7167 2018-04-29 stsp }
4746 84451b3e 2018-07-10 stsp
4747 84451b3e 2018-07-10 stsp struct tog_blame_line {
4748 84451b3e 2018-07-10 stsp int annotated;
4749 84451b3e 2018-07-10 stsp struct got_object_id *id;
4750 84451b3e 2018-07-10 stsp };
4751 9f7d7167 2018-04-29 stsp
4752 4ed7e80c 2018-05-20 stsp static const struct got_error *
4753 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4754 84451b3e 2018-07-10 stsp {
4755 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4756 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4757 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4758 84451b3e 2018-07-10 stsp const struct got_error *err;
4759 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4760 826082fe 2020-12-10 stsp char *line = NULL;
4761 826082fe 2020-12-10 stsp size_t linesize = 0;
4762 826082fe 2020-12-10 stsp ssize_t linelen;
4763 84451b3e 2018-07-10 stsp wchar_t *wline;
4764 27a741e5 2019-09-11 stsp int width;
4765 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4766 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4767 ab089a2a 2018-07-12 stsp char *id_str;
4768 11b20872 2019-11-08 stsp struct tog_color *tc;
4769 ab089a2a 2018-07-12 stsp
4770 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4771 ab089a2a 2018-07-12 stsp if (err)
4772 ab089a2a 2018-07-12 stsp return err;
4773 84451b3e 2018-07-10 stsp
4774 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4775 f7d12f7e 2018-08-01 stsp werase(view->window);
4776 84451b3e 2018-07-10 stsp
4777 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4778 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4779 ab089a2a 2018-07-12 stsp free(id_str);
4780 ab089a2a 2018-07-12 stsp return err;
4781 ab089a2a 2018-07-12 stsp }
4782 ab089a2a 2018-07-12 stsp
4783 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4784 ab089a2a 2018-07-12 stsp free(line);
4785 2550e4c3 2018-07-13 stsp line = NULL;
4786 1cae65b4 2019-09-22 stsp if (err)
4787 1cae65b4 2019-09-22 stsp return err;
4788 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4789 a3404814 2018-09-02 stsp wstandout(view->window);
4790 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4791 11b20872 2019-11-08 stsp if (tc)
4792 11b20872 2019-11-08 stsp wattr_on(view->window,
4793 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4794 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4795 11b20872 2019-11-08 stsp if (tc)
4796 11b20872 2019-11-08 stsp wattr_off(view->window,
4797 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4798 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4799 a3404814 2018-09-02 stsp wstandend(view->window);
4800 2550e4c3 2018-07-13 stsp free(wline);
4801 2550e4c3 2018-07-13 stsp wline = NULL;
4802 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4803 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4804 ab089a2a 2018-07-12 stsp
4805 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4806 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4807 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4808 ab089a2a 2018-07-12 stsp free(id_str);
4809 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4810 ab089a2a 2018-07-12 stsp }
4811 ab089a2a 2018-07-12 stsp free(id_str);
4812 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4813 3f60a8ef 2018-07-10 stsp free(line);
4814 2550e4c3 2018-07-13 stsp line = NULL;
4815 3f60a8ef 2018-07-10 stsp if (err)
4816 3f60a8ef 2018-07-10 stsp return err;
4817 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4818 2550e4c3 2018-07-13 stsp free(wline);
4819 2550e4c3 2018-07-13 stsp wline = NULL;
4820 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4821 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4822 3f60a8ef 2018-07-10 stsp
4823 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4824 145b6838 2022-06-16 stsp view->maxx = 0;
4825 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4826 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4827 826082fe 2020-12-10 stsp if (linelen == -1) {
4828 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4829 826082fe 2020-12-10 stsp s->eof = 1;
4830 826082fe 2020-12-10 stsp break;
4831 826082fe 2020-12-10 stsp }
4832 84451b3e 2018-07-10 stsp free(line);
4833 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4834 84451b3e 2018-07-10 stsp }
4835 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4836 826082fe 2020-12-10 stsp continue;
4837 1853e0f4 2022-06-16 stsp
4838 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4839 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4840 1853e0f4 2022-06-16 stsp if (err) {
4841 1853e0f4 2022-06-16 stsp free(line);
4842 1853e0f4 2022-06-16 stsp return err;
4843 1853e0f4 2022-06-16 stsp }
4844 1853e0f4 2022-06-16 stsp free(wline);
4845 1853e0f4 2022-06-16 stsp wline = NULL;
4846 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4847 84451b3e 2018-07-10 stsp
4848 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4849 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4850 b700b5d6 2018-07-10 stsp
4851 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4852 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4853 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4854 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4855 27a741e5 2019-09-11 stsp !(view->focussed &&
4856 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4857 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4858 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4859 8d0fe45a 2019-08-12 stsp char *id_str;
4860 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4861 87411fa9 2022-06-16 stsp blame_line->id);
4862 8d0fe45a 2019-08-12 stsp if (err) {
4863 8d0fe45a 2019-08-12 stsp free(line);
4864 8d0fe45a 2019-08-12 stsp return err;
4865 8d0fe45a 2019-08-12 stsp }
4866 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4867 11b20872 2019-11-08 stsp if (tc)
4868 11b20872 2019-11-08 stsp wattr_on(view->window,
4869 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4870 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4871 11b20872 2019-11-08 stsp if (tc)
4872 11b20872 2019-11-08 stsp wattr_off(view->window,
4873 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4874 8d0fe45a 2019-08-12 stsp free(id_str);
4875 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4876 8d0fe45a 2019-08-12 stsp } else {
4877 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4878 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4879 84451b3e 2018-07-10 stsp }
4880 ee41ec32 2018-07-10 stsp } else {
4881 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4882 ee41ec32 2018-07-10 stsp prev_id = NULL;
4883 ee41ec32 2018-07-10 stsp }
4884 84451b3e 2018-07-10 stsp
4885 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4886 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4887 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4888 27a741e5 2019-09-11 stsp
4889 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4890 41605754 2020-11-12 stsp width = 9;
4891 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4892 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4893 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4894 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4895 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4896 41605754 2020-11-12 stsp if (err) {
4897 41605754 2020-11-12 stsp free(line);
4898 41605754 2020-11-12 stsp return err;
4899 41605754 2020-11-12 stsp }
4900 41605754 2020-11-12 stsp width += 9;
4901 41605754 2020-11-12 stsp } else {
4902 4cb9d869 2022-06-16 stsp int skip;
4903 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4904 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4905 4cb9d869 2022-06-16 stsp if (err) {
4906 4cb9d869 2022-06-16 stsp free(line);
4907 4cb9d869 2022-06-16 stsp return err;
4908 145b6838 2022-06-16 stsp }
4909 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4910 a75b3e2d 2022-06-16 stsp width += 9;
4911 41605754 2020-11-12 stsp free(wline);
4912 41605754 2020-11-12 stsp wline = NULL;
4913 41605754 2020-11-12 stsp }
4914 41605754 2020-11-12 stsp
4915 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4916 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4917 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4918 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4919 84451b3e 2018-07-10 stsp }
4920 826082fe 2020-12-10 stsp free(line);
4921 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4922 84451b3e 2018-07-10 stsp
4923 9b058f45 2022-06-30 mark view_border(view);
4924 84451b3e 2018-07-10 stsp
4925 84451b3e 2018-07-10 stsp return NULL;
4926 84451b3e 2018-07-10 stsp }
4927 84451b3e 2018-07-10 stsp
4928 84451b3e 2018-07-10 stsp static const struct got_error *
4929 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4930 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4931 84451b3e 2018-07-10 stsp {
4932 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4933 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4934 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4935 1a76625f 2018-10-22 stsp int errcode;
4936 84451b3e 2018-07-10 stsp
4937 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4938 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4939 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4940 84451b3e 2018-07-10 stsp
4941 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4942 1a76625f 2018-10-22 stsp if (errcode)
4943 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4944 84451b3e 2018-07-10 stsp
4945 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4946 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4947 d68a0a7d 2018-07-10 stsp goto done;
4948 d68a0a7d 2018-07-10 stsp }
4949 d68a0a7d 2018-07-10 stsp
4950 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4951 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4952 d68a0a7d 2018-07-10 stsp
4953 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4954 d68a0a7d 2018-07-10 stsp if (line->annotated)
4955 d68a0a7d 2018-07-10 stsp goto done;
4956 d68a0a7d 2018-07-10 stsp
4957 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4958 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4959 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4960 84451b3e 2018-07-10 stsp goto done;
4961 84451b3e 2018-07-10 stsp }
4962 84451b3e 2018-07-10 stsp line->annotated = 1;
4963 84451b3e 2018-07-10 stsp done:
4964 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4965 1a76625f 2018-10-22 stsp if (errcode)
4966 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4967 84451b3e 2018-07-10 stsp return err;
4968 84451b3e 2018-07-10 stsp }
4969 84451b3e 2018-07-10 stsp
4970 84451b3e 2018-07-10 stsp static void *
4971 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4972 84451b3e 2018-07-10 stsp {
4973 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4974 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4975 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4976 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4977 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4978 1b484788 2022-06-28 tracey
4979 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4980 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4981 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4982 e6e73e55 2022-06-30 tracey
4983 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4984 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4985 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4986 e6e73e55 2022-06-30 tracey goto done;
4987 e6e73e55 2022-06-30 tracey }
4988 18430de3 2018-07-10 stsp
4989 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4990 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4991 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4992 e6e73e55 2022-06-30 tracey goto done;
4993 e6e73e55 2022-06-30 tracey }
4994 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4995 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4996 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4997 e6e73e55 2022-06-30 tracey goto done;
4998 e6e73e55 2022-06-30 tracey }
4999 e6e73e55 2022-06-30 tracey
5000 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5001 61266923 2020-01-14 stsp if (err)
5002 e6e73e55 2022-06-30 tracey goto done;
5003 61266923 2020-01-14 stsp
5004 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5005 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5006 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5007 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5008 fc06ba56 2019-08-22 stsp err = NULL;
5009 18430de3 2018-07-10 stsp
5010 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5011 e6e73e55 2022-06-30 tracey if (errcode) {
5012 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5013 e6e73e55 2022-06-30 tracey goto done;
5014 e6e73e55 2022-06-30 tracey }
5015 18430de3 2018-07-10 stsp
5016 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5017 1d0f4054 2021-06-17 stsp if (err == NULL)
5018 1d0f4054 2021-06-17 stsp err = close_err;
5019 c9beca56 2018-07-22 stsp ta->repo = NULL;
5020 c9beca56 2018-07-22 stsp *ta->complete = 1;
5021 18430de3 2018-07-10 stsp
5022 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5023 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5024 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5025 18430de3 2018-07-10 stsp
5026 e6e73e55 2022-06-30 tracey done:
5027 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5028 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5029 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5030 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5031 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5032 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5033 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5034 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5035 1b484788 2022-06-28 tracey
5036 18430de3 2018-07-10 stsp return (void *)err;
5037 84451b3e 2018-07-10 stsp }
5038 84451b3e 2018-07-10 stsp
5039 245d91c1 2018-07-12 stsp static struct got_object_id *
5040 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5041 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5042 245d91c1 2018-07-12 stsp {
5043 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5044 8d0fe45a 2019-08-12 stsp
5045 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5046 8d0fe45a 2019-08-12 stsp return NULL;
5047 b880a918 2018-07-10 stsp
5048 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5049 245d91c1 2018-07-12 stsp if (!line->annotated)
5050 245d91c1 2018-07-12 stsp return NULL;
5051 245d91c1 2018-07-12 stsp
5052 245d91c1 2018-07-12 stsp return line->id;
5053 b880a918 2018-07-10 stsp }
5054 245d91c1 2018-07-12 stsp
5055 b880a918 2018-07-10 stsp static const struct got_error *
5056 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5057 a70480e0 2018-06-23 stsp {
5058 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5059 245d91c1 2018-07-12 stsp int i;
5060 245d91c1 2018-07-12 stsp
5061 245d91c1 2018-07-12 stsp if (blame->thread) {
5062 1a76625f 2018-10-22 stsp int errcode;
5063 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5064 1a76625f 2018-10-22 stsp if (errcode)
5065 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5066 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5067 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5068 1a76625f 2018-10-22 stsp if (errcode)
5069 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5070 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5071 1a76625f 2018-10-22 stsp if (errcode)
5072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5073 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5074 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5075 245d91c1 2018-07-12 stsp err = NULL;
5076 245d91c1 2018-07-12 stsp blame->thread = NULL;
5077 245d91c1 2018-07-12 stsp }
5078 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5079 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5080 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5081 1d0f4054 2021-06-17 stsp if (err == NULL)
5082 1d0f4054 2021-06-17 stsp err = close_err;
5083 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5084 245d91c1 2018-07-12 stsp }
5085 245d91c1 2018-07-12 stsp if (blame->f) {
5086 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5087 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5088 245d91c1 2018-07-12 stsp blame->f = NULL;
5089 245d91c1 2018-07-12 stsp }
5090 57670559 2018-12-23 stsp if (blame->lines) {
5091 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5092 57670559 2018-12-23 stsp free(blame->lines[i].id);
5093 57670559 2018-12-23 stsp free(blame->lines);
5094 57670559 2018-12-23 stsp blame->lines = NULL;
5095 57670559 2018-12-23 stsp }
5096 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5097 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5098 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5099 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5100 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5101 0ae84acc 2022-06-15 tracey if (err == NULL)
5102 0ae84acc 2022-06-15 tracey err = pack_err;
5103 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5104 0ae84acc 2022-06-15 tracey }
5105 245d91c1 2018-07-12 stsp return err;
5106 245d91c1 2018-07-12 stsp }
5107 245d91c1 2018-07-12 stsp
5108 245d91c1 2018-07-12 stsp static const struct got_error *
5109 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5110 fc06ba56 2019-08-22 stsp {
5111 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5112 fc06ba56 2019-08-22 stsp int *done = arg;
5113 fc06ba56 2019-08-22 stsp int errcode;
5114 fc06ba56 2019-08-22 stsp
5115 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5116 fc06ba56 2019-08-22 stsp if (errcode)
5117 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5118 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5119 fc06ba56 2019-08-22 stsp
5120 fc06ba56 2019-08-22 stsp if (*done)
5121 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5122 fc06ba56 2019-08-22 stsp
5123 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5124 fc06ba56 2019-08-22 stsp if (errcode)
5125 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5126 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5127 fc06ba56 2019-08-22 stsp
5128 fc06ba56 2019-08-22 stsp return err;
5129 fc06ba56 2019-08-22 stsp }
5130 fc06ba56 2019-08-22 stsp
5131 fc06ba56 2019-08-22 stsp static const struct got_error *
5132 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5133 245d91c1 2018-07-12 stsp {
5134 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5135 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5136 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5137 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5138 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5139 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5140 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5141 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5142 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5143 a70480e0 2018-06-23 stsp
5144 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5145 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5146 27d434c2 2018-09-15 stsp if (err)
5147 15a94983 2018-12-23 stsp return err;
5148 eb81bc23 2022-06-28 tracey
5149 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5150 eb81bc23 2022-06-28 tracey if (fd == -1) {
5151 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5152 eb81bc23 2022-06-28 tracey goto done;
5153 eb81bc23 2022-06-28 tracey }
5154 a44927cc 2022-04-07 stsp
5155 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5156 a44927cc 2022-04-07 stsp if (err)
5157 a44927cc 2022-04-07 stsp goto done;
5158 27d434c2 2018-09-15 stsp
5159 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5160 84451b3e 2018-07-10 stsp if (err)
5161 84451b3e 2018-07-10 stsp goto done;
5162 27d434c2 2018-09-15 stsp
5163 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5164 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5165 84451b3e 2018-07-10 stsp goto done;
5166 84451b3e 2018-07-10 stsp }
5167 a70480e0 2018-06-23 stsp
5168 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5169 a70480e0 2018-06-23 stsp if (err)
5170 a70480e0 2018-06-23 stsp goto done;
5171 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5172 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5173 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5174 84451b3e 2018-07-10 stsp goto done;
5175 84451b3e 2018-07-10 stsp }
5176 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5177 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5178 1fddf795 2021-01-20 stsp if (err)
5179 1fddf795 2021-01-20 stsp goto done;
5180 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5181 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5182 84451b3e 2018-07-10 stsp goto done;
5183 1fddf795 2021-01-20 stsp }
5184 b02560ec 2019-08-19 stsp
5185 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5186 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5187 b02560ec 2019-08-19 stsp blame->nlines--;
5188 a70480e0 2018-06-23 stsp
5189 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5190 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5191 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5192 84451b3e 2018-07-10 stsp goto done;
5193 84451b3e 2018-07-10 stsp }
5194 a70480e0 2018-06-23 stsp
5195 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5196 bd24772e 2018-07-11 stsp if (err)
5197 bd24772e 2018-07-11 stsp goto done;
5198 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5199 0ae84acc 2022-06-15 tracey pack_fds);
5200 0ae84acc 2022-06-15 tracey if (err)
5201 0ae84acc 2022-06-15 tracey goto done;
5202 bd24772e 2018-07-11 stsp
5203 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5204 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5205 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5206 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5207 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5208 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5209 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5210 245d91c1 2018-07-12 stsp goto done;
5211 245d91c1 2018-07-12 stsp }
5212 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5213 245d91c1 2018-07-12 stsp
5214 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5215 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5216 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5217 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5218 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5219 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5220 a5388363 2020-12-01 naddy s->blame_complete = 0;
5221 f5a09613 2020-12-13 naddy
5222 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5223 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5224 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5225 f5a09613 2020-12-13 naddy s->selected_line = 1;
5226 f5a09613 2020-12-13 naddy }
5227 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5228 245d91c1 2018-07-12 stsp
5229 245d91c1 2018-07-12 stsp done:
5230 a44927cc 2022-04-07 stsp if (commit)
5231 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5232 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5233 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5234 245d91c1 2018-07-12 stsp if (blob)
5235 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5236 27d434c2 2018-09-15 stsp free(obj_id);
5237 245d91c1 2018-07-12 stsp if (err)
5238 245d91c1 2018-07-12 stsp stop_blame(blame);
5239 245d91c1 2018-07-12 stsp return err;
5240 245d91c1 2018-07-12 stsp }
5241 245d91c1 2018-07-12 stsp
5242 245d91c1 2018-07-12 stsp static const struct got_error *
5243 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5244 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5245 245d91c1 2018-07-12 stsp {
5246 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5247 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5248 dbc6a6b6 2018-07-12 stsp
5249 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5250 245d91c1 2018-07-12 stsp
5251 c4843652 2019-08-12 stsp s->path = strdup(path);
5252 c4843652 2019-08-12 stsp if (s->path == NULL)
5253 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5254 c4843652 2019-08-12 stsp
5255 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5256 c4843652 2019-08-12 stsp if (err) {
5257 c4843652 2019-08-12 stsp free(s->path);
5258 7cbe629d 2018-08-04 stsp return err;
5259 c4843652 2019-08-12 stsp }
5260 245d91c1 2018-07-12 stsp
5261 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5262 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5263 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5264 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5265 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5266 fb2756b9 2018-08-04 stsp s->repo = repo;
5267 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5268 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5269 7cbe629d 2018-08-04 stsp
5270 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5271 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5272 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5273 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5274 11b20872 2019-11-08 stsp if (err)
5275 11b20872 2019-11-08 stsp return err;
5276 11b20872 2019-11-08 stsp }
5277 11b20872 2019-11-08 stsp
5278 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5279 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5280 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5281 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5282 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5283 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5284 e5a0f69f 2018-08-18 stsp
5285 a5388363 2020-12-01 naddy return run_blame(view);
5286 7cbe629d 2018-08-04 stsp }
5287 7cbe629d 2018-08-04 stsp
5288 e5a0f69f 2018-08-18 stsp static const struct got_error *
5289 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5290 7cbe629d 2018-08-04 stsp {
5291 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5292 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5293 7cbe629d 2018-08-04 stsp
5294 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5295 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5296 e5a0f69f 2018-08-18 stsp
5297 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5298 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5299 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5300 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5301 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5302 7cbe629d 2018-08-04 stsp }
5303 e5a0f69f 2018-08-18 stsp
5304 e5a0f69f 2018-08-18 stsp free(s->path);
5305 11b20872 2019-11-08 stsp free_colors(&s->colors);
5306 e5a0f69f 2018-08-18 stsp return err;
5307 7cbe629d 2018-08-04 stsp }
5308 7cbe629d 2018-08-04 stsp
5309 7cbe629d 2018-08-04 stsp static const struct got_error *
5310 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5311 6c4c42e0 2019-06-24 stsp {
5312 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5313 6c4c42e0 2019-06-24 stsp
5314 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5315 6c4c42e0 2019-06-24 stsp return NULL;
5316 6c4c42e0 2019-06-24 stsp }
5317 6c4c42e0 2019-06-24 stsp
5318 6c4c42e0 2019-06-24 stsp static const struct got_error *
5319 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5320 6c4c42e0 2019-06-24 stsp {
5321 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5322 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5323 6c4c42e0 2019-06-24 stsp int lineno;
5324 cb713507 2022-06-16 stsp char *line = NULL;
5325 826082fe 2020-12-10 stsp size_t linesize = 0;
5326 826082fe 2020-12-10 stsp ssize_t linelen;
5327 6c4c42e0 2019-06-24 stsp
5328 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5329 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5330 6c4c42e0 2019-06-24 stsp return NULL;
5331 6c4c42e0 2019-06-24 stsp }
5332 6c4c42e0 2019-06-24 stsp
5333 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5334 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5335 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5336 6c4c42e0 2019-06-24 stsp else
5337 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5338 487cd7d2 2021-12-17 stsp } else
5339 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5340 6c4c42e0 2019-06-24 stsp
5341 6c4c42e0 2019-06-24 stsp while (1) {
5342 6c4c42e0 2019-06-24 stsp off_t offset;
5343 6c4c42e0 2019-06-24 stsp
5344 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5345 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5347 6c4c42e0 2019-06-24 stsp break;
5348 6c4c42e0 2019-06-24 stsp }
5349 2246482e 2019-06-25 stsp
5350 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5351 6c4c42e0 2019-06-24 stsp lineno = 1;
5352 6c4c42e0 2019-06-24 stsp else
5353 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5354 6c4c42e0 2019-06-24 stsp }
5355 6c4c42e0 2019-06-24 stsp
5356 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5357 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5358 6c4c42e0 2019-06-24 stsp free(line);
5359 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5360 6c4c42e0 2019-06-24 stsp }
5361 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5362 cb713507 2022-06-16 stsp if (linelen != -1) {
5363 cb713507 2022-06-16 stsp char *exstr;
5364 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5365 cb713507 2022-06-16 stsp if (err)
5366 cb713507 2022-06-16 stsp break;
5367 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5368 cb713507 2022-06-16 stsp &view->regmatch)) {
5369 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5370 cb713507 2022-06-16 stsp s->matched_line = lineno;
5371 cb713507 2022-06-16 stsp free(exstr);
5372 cb713507 2022-06-16 stsp break;
5373 cb713507 2022-06-16 stsp }
5374 cb713507 2022-06-16 stsp free(exstr);
5375 6c4c42e0 2019-06-24 stsp }
5376 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5377 6c4c42e0 2019-06-24 stsp lineno++;
5378 6c4c42e0 2019-06-24 stsp else
5379 6c4c42e0 2019-06-24 stsp lineno--;
5380 6c4c42e0 2019-06-24 stsp }
5381 826082fe 2020-12-10 stsp free(line);
5382 6c4c42e0 2019-06-24 stsp
5383 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5384 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5385 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5386 6c4c42e0 2019-06-24 stsp }
5387 6c4c42e0 2019-06-24 stsp
5388 145b6838 2022-06-16 stsp return err;
5389 6c4c42e0 2019-06-24 stsp }
5390 6c4c42e0 2019-06-24 stsp
5391 6c4c42e0 2019-06-24 stsp static const struct got_error *
5392 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5393 7cbe629d 2018-08-04 stsp {
5394 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5395 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5396 2b380cc8 2018-10-24 stsp int errcode;
5397 2b380cc8 2018-10-24 stsp
5398 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5399 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5400 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5401 2b380cc8 2018-10-24 stsp if (errcode)
5402 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5403 51fe7530 2019-08-19 stsp
5404 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5405 2b380cc8 2018-10-24 stsp }
5406 e5a0f69f 2018-08-18 stsp
5407 51fe7530 2019-08-19 stsp if (s->blame_complete)
5408 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5409 51fe7530 2019-08-19 stsp
5410 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5411 e5a0f69f 2018-08-18 stsp
5412 9b058f45 2022-06-30 mark view_border(view);
5413 e5a0f69f 2018-08-18 stsp return err;
5414 e5a0f69f 2018-08-18 stsp }
5415 e5a0f69f 2018-08-18 stsp
5416 e5a0f69f 2018-08-18 stsp static const struct got_error *
5417 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5418 e5a0f69f 2018-08-18 stsp {
5419 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5420 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5421 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5422 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5423 9b058f45 2022-06-30 mark
5424 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5425 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5426 9b058f45 2022-06-30 mark --eos; /* border */
5427 7cbe629d 2018-08-04 stsp
5428 e5a0f69f 2018-08-18 stsp switch (ch) {
5429 145b6838 2022-06-16 stsp case '0':
5430 145b6838 2022-06-16 stsp view->x = 0;
5431 145b6838 2022-06-16 stsp break;
5432 145b6838 2022-06-16 stsp case '$':
5433 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5434 640cd7ff 2022-06-22 mark view->count = 0;
5435 145b6838 2022-06-16 stsp break;
5436 145b6838 2022-06-16 stsp case KEY_RIGHT:
5437 145b6838 2022-06-16 stsp case 'l':
5438 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5439 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5440 640cd7ff 2022-06-22 mark else
5441 640cd7ff 2022-06-22 mark view->count = 0;
5442 145b6838 2022-06-16 stsp break;
5443 145b6838 2022-06-16 stsp case KEY_LEFT:
5444 145b6838 2022-06-16 stsp case 'h':
5445 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5446 640cd7ff 2022-06-22 mark if (view->x <= 0)
5447 640cd7ff 2022-06-22 mark view->count = 0;
5448 145b6838 2022-06-16 stsp break;
5449 1e37a5c2 2019-05-12 jcs case 'q':
5450 1e37a5c2 2019-05-12 jcs s->done = 1;
5451 4deef56f 2021-09-02 naddy break;
5452 4deef56f 2021-09-02 naddy case 'g':
5453 4deef56f 2021-09-02 naddy case KEY_HOME:
5454 4deef56f 2021-09-02 naddy s->selected_line = 1;
5455 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5456 640cd7ff 2022-06-22 mark view->count = 0;
5457 4deef56f 2021-09-02 naddy break;
5458 4deef56f 2021-09-02 naddy case 'G':
5459 4deef56f 2021-09-02 naddy case KEY_END:
5460 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5461 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5462 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5463 4deef56f 2021-09-02 naddy } else {
5464 9b058f45 2022-06-30 mark s->selected_line = eos;
5465 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5466 4deef56f 2021-09-02 naddy }
5467 640cd7ff 2022-06-22 mark view->count = 0;
5468 1e37a5c2 2019-05-12 jcs break;
5469 1e37a5c2 2019-05-12 jcs case 'k':
5470 1e37a5c2 2019-05-12 jcs case KEY_UP:
5471 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5472 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5473 1e37a5c2 2019-05-12 jcs s->selected_line--;
5474 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5475 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5476 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5477 640cd7ff 2022-06-22 mark else
5478 640cd7ff 2022-06-22 mark view->count = 0;
5479 1e37a5c2 2019-05-12 jcs break;
5480 83cc4199 2022-06-13 stsp case CTRL('u'):
5481 33c3719a 2022-06-15 stsp case 'u':
5482 83cc4199 2022-06-13 stsp nscroll /= 2;
5483 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5484 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5485 ea025d1d 2020-02-22 naddy case CTRL('b'):
5486 61417565 2022-06-20 mark case 'b':
5487 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5488 640cd7ff 2022-06-22 mark if (view->count > 1)
5489 640cd7ff 2022-06-22 mark nscroll += nscroll;
5490 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5491 640cd7ff 2022-06-22 mark view->count = 0;
5492 e5a0f69f 2018-08-18 stsp break;
5493 1e37a5c2 2019-05-12 jcs }
5494 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5495 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5496 1e37a5c2 2019-05-12 jcs else
5497 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5498 1e37a5c2 2019-05-12 jcs break;
5499 1e37a5c2 2019-05-12 jcs case 'j':
5500 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5501 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5502 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5503 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5504 1e37a5c2 2019-05-12 jcs s->selected_line++;
5505 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5506 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5507 640cd7ff 2022-06-22 mark else
5508 640cd7ff 2022-06-22 mark view->count = 0;
5509 1e37a5c2 2019-05-12 jcs break;
5510 61417565 2022-06-20 mark case 'c':
5511 1e37a5c2 2019-05-12 jcs case 'p': {
5512 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5513 640cd7ff 2022-06-22 mark
5514 640cd7ff 2022-06-22 mark view->count = 0;
5515 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5516 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5517 1e37a5c2 2019-05-12 jcs if (id == NULL)
5518 e5a0f69f 2018-08-18 stsp break;
5519 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5520 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5521 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5522 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5523 1e37a5c2 2019-05-12 jcs int obj_type;
5524 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5525 1e37a5c2 2019-05-12 jcs s->repo, id);
5526 e5a0f69f 2018-08-18 stsp if (err)
5527 e5a0f69f 2018-08-18 stsp break;
5528 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5529 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5530 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5531 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5532 e5a0f69f 2018-08-18 stsp break;
5533 e5a0f69f 2018-08-18 stsp }
5534 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5535 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5536 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5537 a44927cc 2022-04-07 stsp if (err)
5538 a44927cc 2022-04-07 stsp break;
5539 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5540 a44927cc 2022-04-07 stsp pcommit, s->path);
5541 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5542 e5a0f69f 2018-08-18 stsp if (err) {
5543 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5544 1e37a5c2 2019-05-12 jcs err = NULL;
5545 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5546 e5a0f69f 2018-08-18 stsp break;
5547 e5a0f69f 2018-08-18 stsp }
5548 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5549 1e37a5c2 2019-05-12 jcs blob_id);
5550 1e37a5c2 2019-05-12 jcs free(blob_id);
5551 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5552 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5553 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5554 e5a0f69f 2018-08-18 stsp break;
5555 1e37a5c2 2019-05-12 jcs }
5556 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5557 d7b5a0e8 2022-04-20 stsp &pid->id);
5558 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5559 1e37a5c2 2019-05-12 jcs } else {
5560 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5561 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5562 1e37a5c2 2019-05-12 jcs break;
5563 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5564 1e37a5c2 2019-05-12 jcs id);
5565 1e37a5c2 2019-05-12 jcs }
5566 1e37a5c2 2019-05-12 jcs if (err)
5567 e5a0f69f 2018-08-18 stsp break;
5568 1e37a5c2 2019-05-12 jcs s->done = 1;
5569 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5570 1e37a5c2 2019-05-12 jcs s->done = 0;
5571 1e37a5c2 2019-05-12 jcs if (thread_err)
5572 1e37a5c2 2019-05-12 jcs break;
5573 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5574 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5575 a5388363 2020-12-01 naddy err = run_blame(view);
5576 1e37a5c2 2019-05-12 jcs if (err)
5577 1e37a5c2 2019-05-12 jcs break;
5578 1e37a5c2 2019-05-12 jcs break;
5579 1e37a5c2 2019-05-12 jcs }
5580 61417565 2022-06-20 mark case 'C': {
5581 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5582 640cd7ff 2022-06-22 mark
5583 640cd7ff 2022-06-22 mark view->count = 0;
5584 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5585 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5586 1e37a5c2 2019-05-12 jcs break;
5587 1e37a5c2 2019-05-12 jcs s->done = 1;
5588 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5589 1e37a5c2 2019-05-12 jcs s->done = 0;
5590 1e37a5c2 2019-05-12 jcs if (thread_err)
5591 1e37a5c2 2019-05-12 jcs break;
5592 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5593 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5594 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5595 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5596 a5388363 2020-12-01 naddy err = run_blame(view);
5597 1e37a5c2 2019-05-12 jcs if (err)
5598 1e37a5c2 2019-05-12 jcs break;
5599 1e37a5c2 2019-05-12 jcs break;
5600 1e37a5c2 2019-05-12 jcs }
5601 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5602 1e37a5c2 2019-05-12 jcs case '\r': {
5603 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5604 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5605 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5606 640cd7ff 2022-06-22 mark
5607 640cd7ff 2022-06-22 mark view->count = 0;
5608 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5609 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5610 1e37a5c2 2019-05-12 jcs if (id == NULL)
5611 1e37a5c2 2019-05-12 jcs break;
5612 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5613 1e37a5c2 2019-05-12 jcs if (err)
5614 1e37a5c2 2019-05-12 jcs break;
5615 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5616 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5617 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5618 9b058f45 2022-06-30 mark
5619 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5620 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5621 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5622 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5623 1e37a5c2 2019-05-12 jcs break;
5624 15a94983 2018-12-23 stsp }
5625 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5626 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5627 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5628 1e37a5c2 2019-05-12 jcs if (err) {
5629 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5630 1e37a5c2 2019-05-12 jcs break;
5631 1e37a5c2 2019-05-12 jcs }
5632 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
5633 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5634 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5635 9b058f45 2022-06-30 mark if (err)
5636 9b058f45 2022-06-30 mark break;
5637 9b058f45 2022-06-30 mark }
5638 9b058f45 2022-06-30 mark
5639 e78dc838 2020-12-04 stsp view->focussed = 0;
5640 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5641 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5642 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5643 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5644 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5645 1e37a5c2 2019-05-12 jcs if (err)
5646 34bc9ec9 2019-02-22 stsp break;
5647 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5648 0dbbbe90 2022-06-17 op if (err)
5649 0dbbbe90 2022-06-17 op break;
5650 e78dc838 2020-12-04 stsp view->focus_child = 1;
5651 1e37a5c2 2019-05-12 jcs } else
5652 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5653 1e37a5c2 2019-05-12 jcs if (err)
5654 e5a0f69f 2018-08-18 stsp break;
5655 1e37a5c2 2019-05-12 jcs break;
5656 1e37a5c2 2019-05-12 jcs }
5657 83cc4199 2022-06-13 stsp case CTRL('d'):
5658 33c3719a 2022-06-15 stsp case 'd':
5659 83cc4199 2022-06-13 stsp nscroll /= 2;
5660 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5661 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5662 ea025d1d 2020-02-22 naddy case CTRL('f'):
5663 61417565 2022-06-20 mark case 'f':
5664 1e37a5c2 2019-05-12 jcs case ' ':
5665 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5666 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5667 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5668 640cd7ff 2022-06-22 mark view->count = 0;
5669 e5a0f69f 2018-08-18 stsp break;
5670 1e37a5c2 2019-05-12 jcs }
5671 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5672 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5673 83cc4199 2022-06-13 stsp s->selected_line +=
5674 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5675 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5676 1e37a5c2 2019-05-12 jcs }
5677 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5678 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5679 1e37a5c2 2019-05-12 jcs else
5680 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5681 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5682 1e37a5c2 2019-05-12 jcs break;
5683 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5684 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5685 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5686 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5687 1e37a5c2 2019-05-12 jcs }
5688 1e37a5c2 2019-05-12 jcs break;
5689 1e37a5c2 2019-05-12 jcs default:
5690 640cd7ff 2022-06-22 mark view->count = 0;
5691 1e37a5c2 2019-05-12 jcs break;
5692 a70480e0 2018-06-23 stsp }
5693 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5694 917d79a7 2022-07-01 stsp }
5695 917d79a7 2022-07-01 stsp
5696 917d79a7 2022-07-01 stsp static const struct got_error *
5697 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
5698 917d79a7 2022-07-01 stsp {
5699 917d79a7 2022-07-01 stsp const struct got_error *err;
5700 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
5701 917d79a7 2022-07-01 stsp
5702 917d79a7 2022-07-01 stsp view->count = 0;
5703 917d79a7 2022-07-01 stsp s->done = 1;
5704 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
5705 917d79a7 2022-07-01 stsp s->done = 0;
5706 917d79a7 2022-07-01 stsp if (err)
5707 917d79a7 2022-07-01 stsp return err;
5708 917d79a7 2022-07-01 stsp return run_blame(view);
5709 a70480e0 2018-06-23 stsp }
5710 a70480e0 2018-06-23 stsp
5711 a70480e0 2018-06-23 stsp static const struct got_error *
5712 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5713 9f7d7167 2018-04-29 stsp {
5714 a70480e0 2018-06-23 stsp const struct got_error *error;
5715 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5716 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5717 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5718 0587e10c 2020-07-23 stsp char *link_target = NULL;
5719 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5720 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5721 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5722 a70480e0 2018-06-23 stsp int ch;
5723 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5724 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5725 a70480e0 2018-06-23 stsp
5726 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5727 a70480e0 2018-06-23 stsp switch (ch) {
5728 a70480e0 2018-06-23 stsp case 'c':
5729 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5730 a70480e0 2018-06-23 stsp break;
5731 69069811 2018-08-02 stsp case 'r':
5732 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5733 69069811 2018-08-02 stsp if (repo_path == NULL)
5734 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5735 9ba1d308 2019-10-21 stsp optarg);
5736 69069811 2018-08-02 stsp break;
5737 a70480e0 2018-06-23 stsp default:
5738 17020d27 2019-03-07 stsp usage_blame();
5739 a70480e0 2018-06-23 stsp /* NOTREACHED */
5740 a70480e0 2018-06-23 stsp }
5741 a70480e0 2018-06-23 stsp }
5742 a70480e0 2018-06-23 stsp
5743 a70480e0 2018-06-23 stsp argc -= optind;
5744 a70480e0 2018-06-23 stsp argv += optind;
5745 a70480e0 2018-06-23 stsp
5746 f135c941 2020-02-20 stsp if (argc != 1)
5747 a70480e0 2018-06-23 stsp usage_blame();
5748 6962eb72 2020-02-20 stsp
5749 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5750 0ae84acc 2022-06-15 tracey if (error != NULL)
5751 0ae84acc 2022-06-15 tracey goto done;
5752 0ae84acc 2022-06-15 tracey
5753 69069811 2018-08-02 stsp if (repo_path == NULL) {
5754 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5755 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5756 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5757 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5758 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5759 c156c7a4 2020-12-18 stsp goto done;
5760 f135c941 2020-02-20 stsp if (worktree)
5761 eb41ed75 2019-02-05 stsp repo_path =
5762 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5763 f135c941 2020-02-20 stsp else
5764 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5765 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5766 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5767 c156c7a4 2020-12-18 stsp goto done;
5768 c156c7a4 2020-12-18 stsp }
5769 f135c941 2020-02-20 stsp }
5770 a915003a 2019-02-05 stsp
5771 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5772 c02c541e 2019-03-29 stsp if (error != NULL)
5773 8e94dd5b 2019-01-04 stsp goto done;
5774 69069811 2018-08-02 stsp
5775 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5776 f135c941 2020-02-20 stsp worktree);
5777 c02c541e 2019-03-29 stsp if (error)
5778 92205607 2019-01-04 stsp goto done;
5779 69069811 2018-08-02 stsp
5780 f135c941 2020-02-20 stsp init_curses();
5781 f135c941 2020-02-20 stsp
5782 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5783 51a10b52 2020-12-26 stsp if (error)
5784 51a10b52 2020-12-26 stsp goto done;
5785 51a10b52 2020-12-26 stsp
5786 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5787 eb41ed75 2019-02-05 stsp if (error)
5788 69069811 2018-08-02 stsp goto done;
5789 a70480e0 2018-06-23 stsp
5790 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5791 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5792 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5793 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5794 a70480e0 2018-06-23 stsp if (error != NULL)
5795 66b4983c 2018-06-23 stsp goto done;
5796 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5797 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5798 a70480e0 2018-06-23 stsp } else {
5799 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5800 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5801 a70480e0 2018-06-23 stsp }
5802 a19e88aa 2018-06-23 stsp if (error != NULL)
5803 8b473291 2019-02-21 stsp goto done;
5804 8b473291 2019-02-21 stsp
5805 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5806 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5807 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5808 e1cd8fed 2018-08-01 stsp goto done;
5809 e1cd8fed 2018-08-01 stsp }
5810 0587e10c 2020-07-23 stsp
5811 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5812 a44927cc 2022-04-07 stsp if (error)
5813 a44927cc 2022-04-07 stsp goto done;
5814 a44927cc 2022-04-07 stsp
5815 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5816 a44927cc 2022-04-07 stsp commit, repo);
5817 7cbe629d 2018-08-04 stsp if (error)
5818 7cbe629d 2018-08-04 stsp goto done;
5819 0587e10c 2020-07-23 stsp
5820 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5821 78756c87 2020-11-24 stsp commit_id, repo);
5822 0587e10c 2020-07-23 stsp if (error)
5823 0587e10c 2020-07-23 stsp goto done;
5824 12314ad4 2019-08-31 stsp if (worktree) {
5825 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5826 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5827 12314ad4 2019-08-31 stsp worktree = NULL;
5828 12314ad4 2019-08-31 stsp }
5829 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5830 a70480e0 2018-06-23 stsp done:
5831 69069811 2018-08-02 stsp free(repo_path);
5832 f135c941 2020-02-20 stsp free(in_repo_path);
5833 0587e10c 2020-07-23 stsp free(link_target);
5834 69069811 2018-08-02 stsp free(cwd);
5835 a70480e0 2018-06-23 stsp free(commit_id);
5836 a44927cc 2022-04-07 stsp if (commit)
5837 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5838 eb41ed75 2019-02-05 stsp if (worktree)
5839 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5840 1d0f4054 2021-06-17 stsp if (repo) {
5841 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5842 1d0f4054 2021-06-17 stsp if (error == NULL)
5843 1d0f4054 2021-06-17 stsp error = close_err;
5844 1d0f4054 2021-06-17 stsp }
5845 0ae84acc 2022-06-15 tracey if (pack_fds) {
5846 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5847 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5848 0ae84acc 2022-06-15 tracey if (error == NULL)
5849 0ae84acc 2022-06-15 tracey error = pack_err;
5850 0ae84acc 2022-06-15 tracey }
5851 51a10b52 2020-12-26 stsp tog_free_refs();
5852 a70480e0 2018-06-23 stsp return error;
5853 ffd1d5e5 2018-06-23 stsp }
5854 ffd1d5e5 2018-06-23 stsp
5855 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5856 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5857 ffd1d5e5 2018-06-23 stsp {
5858 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5859 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5860 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5861 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5862 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5863 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5864 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5865 ffd1d5e5 2018-06-23 stsp
5866 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5867 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5868 9b058f45 2022-06-30 mark --limit; /* border */
5869 ffd1d5e5 2018-06-23 stsp
5870 f7d12f7e 2018-08-01 stsp werase(view->window);
5871 ffd1d5e5 2018-06-23 stsp
5872 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5873 ffd1d5e5 2018-06-23 stsp return NULL;
5874 ffd1d5e5 2018-06-23 stsp
5875 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5876 ccda2f4d 2022-06-16 stsp 0, 0);
5877 ffd1d5e5 2018-06-23 stsp if (err)
5878 ffd1d5e5 2018-06-23 stsp return err;
5879 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5880 a3404814 2018-09-02 stsp wstandout(view->window);
5881 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5882 11b20872 2019-11-08 stsp if (tc)
5883 11b20872 2019-11-08 stsp wattr_on(view->window,
5884 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5885 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5886 11b20872 2019-11-08 stsp if (tc)
5887 11b20872 2019-11-08 stsp wattr_off(view->window,
5888 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5889 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5890 a3404814 2018-09-02 stsp wstandend(view->window);
5891 2550e4c3 2018-07-13 stsp free(wline);
5892 2550e4c3 2018-07-13 stsp wline = NULL;
5893 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5894 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5895 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5896 ffd1d5e5 2018-06-23 stsp return NULL;
5897 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5898 ccda2f4d 2022-06-16 stsp 0, 0);
5899 ce52c690 2018-06-23 stsp if (err)
5900 ce52c690 2018-06-23 stsp return err;
5901 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5902 2550e4c3 2018-07-13 stsp free(wline);
5903 2550e4c3 2018-07-13 stsp wline = NULL;
5904 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5905 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5906 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5907 ffd1d5e5 2018-06-23 stsp return NULL;
5908 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5909 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5910 a1eca9bb 2018-06-23 stsp return NULL;
5911 ffd1d5e5 2018-06-23 stsp
5912 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5913 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5914 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5915 0cf4efb1 2018-09-29 stsp if (view->focussed)
5916 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5917 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5918 ffd1d5e5 2018-06-23 stsp }
5919 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5920 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5921 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5922 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5923 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5924 ffd1d5e5 2018-06-23 stsp return NULL;
5925 ffd1d5e5 2018-06-23 stsp n = 1;
5926 ffd1d5e5 2018-06-23 stsp } else {
5927 ffd1d5e5 2018-06-23 stsp n = 0;
5928 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5929 ffd1d5e5 2018-06-23 stsp }
5930 ffd1d5e5 2018-06-23 stsp
5931 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5932 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5933 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5934 848d6979 2019-08-12 stsp const char *modestr = "";
5935 56e0773d 2019-11-28 stsp mode_t mode;
5936 1d13200f 2018-07-12 stsp
5937 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5938 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5939 56e0773d 2019-11-28 stsp
5940 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5941 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5942 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5943 1d13200f 2018-07-12 stsp if (err)
5944 638f9024 2019-05-13 stsp return got_error_from_errno(
5945 230a42bd 2019-05-11 jcs "got_object_id_str");
5946 1d13200f 2018-07-12 stsp }
5947 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5948 63c5ca5d 2019-08-24 stsp modestr = "$";
5949 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5950 0d6c6ee3 2020-05-20 stsp int i;
5951 0d6c6ee3 2020-05-20 stsp
5952 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5953 d86d3b18 2020-12-01 naddy te, s->repo);
5954 0d6c6ee3 2020-05-20 stsp if (err) {
5955 0d6c6ee3 2020-05-20 stsp free(id_str);
5956 0d6c6ee3 2020-05-20 stsp return err;
5957 0d6c6ee3 2020-05-20 stsp }
5958 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5959 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5960 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5961 0d6c6ee3 2020-05-20 stsp }
5962 848d6979 2019-08-12 stsp modestr = "@";
5963 0d6c6ee3 2020-05-20 stsp }
5964 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5965 848d6979 2019-08-12 stsp modestr = "/";
5966 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5967 848d6979 2019-08-12 stsp modestr = "*";
5968 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5969 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5970 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5971 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5972 1d13200f 2018-07-12 stsp free(id_str);
5973 0d6c6ee3 2020-05-20 stsp free(link_target);
5974 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5975 1d13200f 2018-07-12 stsp }
5976 1d13200f 2018-07-12 stsp free(id_str);
5977 0d6c6ee3 2020-05-20 stsp free(link_target);
5978 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5979 ccda2f4d 2022-06-16 stsp 0, 0);
5980 ffd1d5e5 2018-06-23 stsp if (err) {
5981 ffd1d5e5 2018-06-23 stsp free(line);
5982 ffd1d5e5 2018-06-23 stsp break;
5983 ffd1d5e5 2018-06-23 stsp }
5984 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5985 0cf4efb1 2018-09-29 stsp if (view->focussed)
5986 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5987 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5988 ffd1d5e5 2018-06-23 stsp }
5989 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5990 f26dddb7 2019-11-08 stsp if (tc)
5991 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5992 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5993 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5994 f26dddb7 2019-11-08 stsp if (tc)
5995 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5996 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5997 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5998 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5999 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6000 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6001 ffd1d5e5 2018-06-23 stsp free(line);
6002 2550e4c3 2018-07-13 stsp free(wline);
6003 2550e4c3 2018-07-13 stsp wline = NULL;
6004 ffd1d5e5 2018-06-23 stsp n++;
6005 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6006 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6007 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6008 ffd1d5e5 2018-06-23 stsp break;
6009 ffd1d5e5 2018-06-23 stsp }
6010 ffd1d5e5 2018-06-23 stsp
6011 ffd1d5e5 2018-06-23 stsp return err;
6012 ffd1d5e5 2018-06-23 stsp }
6013 ffd1d5e5 2018-06-23 stsp
6014 ffd1d5e5 2018-06-23 stsp static void
6015 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6016 ffd1d5e5 2018-06-23 stsp {
6017 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6018 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6019 fa86c4bf 2020-11-29 stsp int i = 0;
6020 ffd1d5e5 2018-06-23 stsp
6021 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6022 ffd1d5e5 2018-06-23 stsp return;
6023 ffd1d5e5 2018-06-23 stsp
6024 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6025 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6026 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6027 fa86c4bf 2020-11-29 stsp if (!isroot)
6028 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6029 fa86c4bf 2020-11-29 stsp break;
6030 fa86c4bf 2020-11-29 stsp }
6031 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6032 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6033 ffd1d5e5 2018-06-23 stsp }
6034 ffd1d5e5 2018-06-23 stsp }
6035 ffd1d5e5 2018-06-23 stsp
6036 9b058f45 2022-06-30 mark static const struct got_error *
6037 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6038 ffd1d5e5 2018-06-23 stsp {
6039 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6040 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6041 ffd1d5e5 2018-06-23 stsp int n = 0;
6042 ffd1d5e5 2018-06-23 stsp
6043 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6044 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6045 694d3271 2020-12-01 naddy s->first_displayed_entry);
6046 694d3271 2020-12-01 naddy else
6047 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6048 56e0773d 2019-11-28 stsp
6049 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6050 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6051 9b058f45 2022-06-30 mark if (last)
6052 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6053 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6054 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6055 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6056 768394f3 2019-01-24 stsp }
6057 ffd1d5e5 2018-06-23 stsp }
6058 9b058f45 2022-06-30 mark
6059 9b058f45 2022-06-30 mark return NULL;
6060 ffd1d5e5 2018-06-23 stsp }
6061 ffd1d5e5 2018-06-23 stsp
6062 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6063 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6064 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6065 ffd1d5e5 2018-06-23 stsp {
6066 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6067 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6068 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6069 ffd1d5e5 2018-06-23 stsp
6070 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6071 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6072 56e0773d 2019-11-28 stsp + 1 /* slash */;
6073 ce52c690 2018-06-23 stsp if (te)
6074 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6075 ce52c690 2018-06-23 stsp
6076 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6077 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6078 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6079 ffd1d5e5 2018-06-23 stsp
6080 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6081 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6082 d9765a41 2018-06-23 stsp while (pt) {
6083 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6084 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6085 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6086 cb2ebc8a 2018-06-23 stsp goto done;
6087 cb2ebc8a 2018-06-23 stsp }
6088 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6089 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6090 cb2ebc8a 2018-06-23 stsp goto done;
6091 cb2ebc8a 2018-06-23 stsp }
6092 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6093 ffd1d5e5 2018-06-23 stsp }
6094 ce52c690 2018-06-23 stsp if (te) {
6095 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6096 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6097 ce52c690 2018-06-23 stsp goto done;
6098 ce52c690 2018-06-23 stsp }
6099 cb2ebc8a 2018-06-23 stsp }
6100 ce52c690 2018-06-23 stsp done:
6101 ce52c690 2018-06-23 stsp if (err) {
6102 ce52c690 2018-06-23 stsp free(*path);
6103 ce52c690 2018-06-23 stsp *path = NULL;
6104 ce52c690 2018-06-23 stsp }
6105 ce52c690 2018-06-23 stsp return err;
6106 ce52c690 2018-06-23 stsp }
6107 ce52c690 2018-06-23 stsp
6108 ce52c690 2018-06-23 stsp static const struct got_error *
6109 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6110 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6111 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6112 ce52c690 2018-06-23 stsp {
6113 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6114 ce52c690 2018-06-23 stsp char *path;
6115 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6116 a0de39f3 2019-08-09 stsp
6117 a0de39f3 2019-08-09 stsp *new_view = NULL;
6118 69efd4c4 2018-07-18 stsp
6119 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6120 ce52c690 2018-06-23 stsp if (err)
6121 ce52c690 2018-06-23 stsp return err;
6122 ffd1d5e5 2018-06-23 stsp
6123 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6124 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6125 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6126 83ce39e3 2019-08-12 stsp goto done;
6127 83ce39e3 2019-08-12 stsp }
6128 cdf1ee82 2018-08-01 stsp
6129 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6130 e5a0f69f 2018-08-18 stsp if (err) {
6131 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6132 fc06ba56 2019-08-22 stsp err = NULL;
6133 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6134 e5a0f69f 2018-08-18 stsp } else
6135 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6136 83ce39e3 2019-08-12 stsp done:
6137 83ce39e3 2019-08-12 stsp free(path);
6138 69efd4c4 2018-07-18 stsp return err;
6139 69efd4c4 2018-07-18 stsp }
6140 69efd4c4 2018-07-18 stsp
6141 69efd4c4 2018-07-18 stsp static const struct got_error *
6142 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6143 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6144 69efd4c4 2018-07-18 stsp {
6145 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6146 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6147 69efd4c4 2018-07-18 stsp char *path;
6148 a0de39f3 2019-08-09 stsp
6149 a0de39f3 2019-08-09 stsp *new_view = NULL;
6150 69efd4c4 2018-07-18 stsp
6151 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6152 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6153 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6154 e5a0f69f 2018-08-18 stsp
6155 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6156 69efd4c4 2018-07-18 stsp if (err)
6157 69efd4c4 2018-07-18 stsp return err;
6158 69efd4c4 2018-07-18 stsp
6159 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6160 4e97c21c 2020-12-06 stsp path, 0);
6161 ba4f502b 2018-08-04 stsp if (err)
6162 e5a0f69f 2018-08-18 stsp view_close(log_view);
6163 e5a0f69f 2018-08-18 stsp else
6164 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6165 cb2ebc8a 2018-06-23 stsp free(path);
6166 cb2ebc8a 2018-06-23 stsp return err;
6167 ffd1d5e5 2018-06-23 stsp }
6168 ffd1d5e5 2018-06-23 stsp
6169 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6170 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6171 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6172 ffd1d5e5 2018-06-23 stsp {
6173 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6174 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6175 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6176 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6177 ffd1d5e5 2018-06-23 stsp
6178 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6179 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6180 bc573f3b 2021-07-10 stsp
6181 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6182 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6183 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6184 ffd1d5e5 2018-06-23 stsp
6185 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6186 bc573f3b 2021-07-10 stsp if (err)
6187 bc573f3b 2021-07-10 stsp goto done;
6188 bc573f3b 2021-07-10 stsp
6189 bc573f3b 2021-07-10 stsp /*
6190 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6191 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6192 bc573f3b 2021-07-10 stsp * closed on demand.
6193 bc573f3b 2021-07-10 stsp */
6194 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6195 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6196 bc573f3b 2021-07-10 stsp if (err)
6197 bc573f3b 2021-07-10 stsp goto done;
6198 bc573f3b 2021-07-10 stsp s->tree = s->root;
6199 bc573f3b 2021-07-10 stsp
6200 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6201 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6202 ffd1d5e5 2018-06-23 stsp goto done;
6203 ffd1d5e5 2018-06-23 stsp
6204 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6205 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6206 ffd1d5e5 2018-06-23 stsp goto done;
6207 ffd1d5e5 2018-06-23 stsp }
6208 ffd1d5e5 2018-06-23 stsp
6209 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6210 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6211 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6212 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6213 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6214 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6215 9cd7cbd1 2020-12-07 stsp goto done;
6216 9cd7cbd1 2020-12-07 stsp }
6217 9cd7cbd1 2020-12-07 stsp }
6218 fb2756b9 2018-08-04 stsp s->repo = repo;
6219 c0b01bdb 2019-11-08 stsp
6220 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6221 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6222 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6223 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6224 c0b01bdb 2019-11-08 stsp if (err)
6225 c0b01bdb 2019-11-08 stsp goto done;
6226 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6227 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6228 bc573f3b 2021-07-10 stsp if (err)
6229 c0b01bdb 2019-11-08 stsp goto done;
6230 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6231 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6232 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6233 bc573f3b 2021-07-10 stsp if (err)
6234 c0b01bdb 2019-11-08 stsp goto done;
6235 e5a0f69f 2018-08-18 stsp
6236 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6237 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6238 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6239 bc573f3b 2021-07-10 stsp if (err)
6240 11b20872 2019-11-08 stsp goto done;
6241 11b20872 2019-11-08 stsp
6242 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6243 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6244 bc573f3b 2021-07-10 stsp if (err)
6245 c0b01bdb 2019-11-08 stsp goto done;
6246 c0b01bdb 2019-11-08 stsp }
6247 c0b01bdb 2019-11-08 stsp
6248 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6249 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6250 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6251 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6252 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6253 ad80ab7b 2018-08-04 stsp done:
6254 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6255 bc573f3b 2021-07-10 stsp if (commit)
6256 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6257 bc573f3b 2021-07-10 stsp if (err)
6258 bc573f3b 2021-07-10 stsp close_tree_view(view);
6259 ad80ab7b 2018-08-04 stsp return err;
6260 ad80ab7b 2018-08-04 stsp }
6261 ad80ab7b 2018-08-04 stsp
6262 e5a0f69f 2018-08-18 stsp static const struct got_error *
6263 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6264 ad80ab7b 2018-08-04 stsp {
6265 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6266 ad80ab7b 2018-08-04 stsp
6267 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6268 fb2756b9 2018-08-04 stsp free(s->tree_label);
6269 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6270 6484ec90 2018-09-29 stsp free(s->commit_id);
6271 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6272 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6273 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6274 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6275 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6276 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6277 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6278 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6279 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6280 ad80ab7b 2018-08-04 stsp free(parent);
6281 ad80ab7b 2018-08-04 stsp
6282 ad80ab7b 2018-08-04 stsp }
6283 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6284 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6285 bc573f3b 2021-07-10 stsp if (s->root)
6286 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6287 7c32bd05 2019-06-22 stsp return NULL;
6288 7c32bd05 2019-06-22 stsp }
6289 7c32bd05 2019-06-22 stsp
6290 7c32bd05 2019-06-22 stsp static const struct got_error *
6291 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6292 7c32bd05 2019-06-22 stsp {
6293 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6294 7c32bd05 2019-06-22 stsp
6295 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6296 7c32bd05 2019-06-22 stsp return NULL;
6297 7c32bd05 2019-06-22 stsp }
6298 7c32bd05 2019-06-22 stsp
6299 7c32bd05 2019-06-22 stsp static int
6300 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6301 7c32bd05 2019-06-22 stsp {
6302 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6303 7c32bd05 2019-06-22 stsp
6304 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6305 56e0773d 2019-11-28 stsp 0) == 0;
6306 7c32bd05 2019-06-22 stsp }
6307 7c32bd05 2019-06-22 stsp
6308 7c32bd05 2019-06-22 stsp static const struct got_error *
6309 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6310 7c32bd05 2019-06-22 stsp {
6311 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6312 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6313 7c32bd05 2019-06-22 stsp
6314 7c32bd05 2019-06-22 stsp if (!view->searching) {
6315 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6316 7c32bd05 2019-06-22 stsp return NULL;
6317 7c32bd05 2019-06-22 stsp }
6318 7c32bd05 2019-06-22 stsp
6319 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6320 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6321 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6322 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6323 56e0773d 2019-11-28 stsp s->selected_entry);
6324 7c32bd05 2019-06-22 stsp else
6325 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6326 56e0773d 2019-11-28 stsp } else {
6327 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6328 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6329 56e0773d 2019-11-28 stsp else
6330 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6331 56e0773d 2019-11-28 stsp s->selected_entry);
6332 7c32bd05 2019-06-22 stsp }
6333 7c32bd05 2019-06-22 stsp } else {
6334 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6335 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6336 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6337 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6338 56e0773d 2019-11-28 stsp else
6339 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6340 7c32bd05 2019-06-22 stsp }
6341 7c32bd05 2019-06-22 stsp
6342 7c32bd05 2019-06-22 stsp while (1) {
6343 56e0773d 2019-11-28 stsp if (te == NULL) {
6344 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6345 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6346 ac66afb8 2019-06-24 stsp return NULL;
6347 ac66afb8 2019-06-24 stsp }
6348 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6349 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6350 56e0773d 2019-11-28 stsp else
6351 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6352 7c32bd05 2019-06-22 stsp }
6353 7c32bd05 2019-06-22 stsp
6354 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6355 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6356 56e0773d 2019-11-28 stsp s->matched_entry = te;
6357 7c32bd05 2019-06-22 stsp break;
6358 7c32bd05 2019-06-22 stsp }
6359 7c32bd05 2019-06-22 stsp
6360 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6361 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6362 56e0773d 2019-11-28 stsp else
6363 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6364 7c32bd05 2019-06-22 stsp }
6365 e5a0f69f 2018-08-18 stsp
6366 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6367 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6368 7c32bd05 2019-06-22 stsp s->selected = 0;
6369 7c32bd05 2019-06-22 stsp }
6370 7c32bd05 2019-06-22 stsp
6371 e5a0f69f 2018-08-18 stsp return NULL;
6372 ad80ab7b 2018-08-04 stsp }
6373 ad80ab7b 2018-08-04 stsp
6374 ad80ab7b 2018-08-04 stsp static const struct got_error *
6375 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6376 ad80ab7b 2018-08-04 stsp {
6377 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6378 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6379 e5a0f69f 2018-08-18 stsp char *parent_path;
6380 ad80ab7b 2018-08-04 stsp
6381 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6382 e5a0f69f 2018-08-18 stsp if (err)
6383 e5a0f69f 2018-08-18 stsp return err;
6384 ffd1d5e5 2018-06-23 stsp
6385 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6386 e5a0f69f 2018-08-18 stsp free(parent_path);
6387 669b5ffa 2018-10-07 stsp
6388 9b058f45 2022-06-30 mark view_border(view);
6389 e5a0f69f 2018-08-18 stsp return err;
6390 e5a0f69f 2018-08-18 stsp }
6391 ce52c690 2018-06-23 stsp
6392 e5a0f69f 2018-08-18 stsp static const struct got_error *
6393 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6394 e5a0f69f 2018-08-18 stsp {
6395 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6396 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6397 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6398 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6399 49b24ee5 2022-07-03 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6400 ffd1d5e5 2018-06-23 stsp
6401 e5a0f69f 2018-08-18 stsp switch (ch) {
6402 1e37a5c2 2019-05-12 jcs case 'i':
6403 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6404 640cd7ff 2022-06-22 mark view->count = 0;
6405 1e37a5c2 2019-05-12 jcs break;
6406 1e37a5c2 2019-05-12 jcs case 'l':
6407 640cd7ff 2022-06-22 mark view->count = 0;
6408 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6409 ffd1d5e5 2018-06-23 stsp break;
6410 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6411 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6412 49b24ee5 2022-07-03 mark err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6413 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6414 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6415 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6416 49b24ee5 2022-07-03 mark if (err)
6417 49b24ee5 2022-07-03 mark break;
6418 49b24ee5 2022-07-03 mark }
6419 e78dc838 2020-12-04 stsp view->focussed = 0;
6420 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6421 49b24ee5 2022-07-03 mark log_view->mode = view->mode;
6422 49b24ee5 2022-07-03 mark log_view->nlines = view->lines - begin_y;
6423 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6424 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6425 1e37a5c2 2019-05-12 jcs if (err)
6426 1e37a5c2 2019-05-12 jcs return err;
6427 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6428 0dbbbe90 2022-06-17 op if (err)
6429 0dbbbe90 2022-06-17 op return err;
6430 e78dc838 2020-12-04 stsp view->focus_child = 1;
6431 1e37a5c2 2019-05-12 jcs } else
6432 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6433 152c1c93 2020-11-29 stsp break;
6434 152c1c93 2020-11-29 stsp case 'r':
6435 640cd7ff 2022-06-22 mark view->count = 0;
6436 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6437 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
6438 49b24ee5 2022-07-03 mark ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6439 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6440 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6441 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6442 152c1c93 2020-11-29 stsp if (err) {
6443 152c1c93 2020-11-29 stsp view_close(ref_view);
6444 152c1c93 2020-11-29 stsp return err;
6445 152c1c93 2020-11-29 stsp }
6446 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6447 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6448 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
6449 49b24ee5 2022-07-03 mark if (err)
6450 49b24ee5 2022-07-03 mark break;
6451 49b24ee5 2022-07-03 mark }
6452 e78dc838 2020-12-04 stsp view->focussed = 0;
6453 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6454 49b24ee5 2022-07-03 mark ref_view->mode = view->mode;
6455 49b24ee5 2022-07-03 mark ref_view->nlines = view->lines - begin_y;
6456 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6457 152c1c93 2020-11-29 stsp err = view_close_child(view);
6458 152c1c93 2020-11-29 stsp if (err)
6459 152c1c93 2020-11-29 stsp return err;
6460 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6461 0dbbbe90 2022-06-17 op if (err)
6462 0dbbbe90 2022-06-17 op return err;
6463 e78dc838 2020-12-04 stsp view->focus_child = 1;
6464 152c1c93 2020-11-29 stsp } else
6465 152c1c93 2020-11-29 stsp *new_view = ref_view;
6466 e4526bf5 2021-09-03 naddy break;
6467 e4526bf5 2021-09-03 naddy case 'g':
6468 e4526bf5 2021-09-03 naddy case KEY_HOME:
6469 e4526bf5 2021-09-03 naddy s->selected = 0;
6470 640cd7ff 2022-06-22 mark view->count = 0;
6471 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6472 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6473 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6474 e4526bf5 2021-09-03 naddy else
6475 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6476 1e37a5c2 2019-05-12 jcs break;
6477 e4526bf5 2021-09-03 naddy case 'G':
6478 9b058f45 2022-06-30 mark case KEY_END: {
6479 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6480 9b058f45 2022-06-30 mark
6481 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6482 9b058f45 2022-06-30 mark --eos; /* border */
6483 e4526bf5 2021-09-03 naddy s->selected = 0;
6484 640cd7ff 2022-06-22 mark view->count = 0;
6485 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6486 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6487 e4526bf5 2021-09-03 naddy if (te == NULL) {
6488 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6489 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6490 e4526bf5 2021-09-03 naddy n++;
6491 e4526bf5 2021-09-03 naddy }
6492 e4526bf5 2021-09-03 naddy break;
6493 e4526bf5 2021-09-03 naddy }
6494 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6495 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6496 e4526bf5 2021-09-03 naddy }
6497 e4526bf5 2021-09-03 naddy if (n > 0)
6498 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6499 e4526bf5 2021-09-03 naddy break;
6500 9b058f45 2022-06-30 mark }
6501 1e37a5c2 2019-05-12 jcs case 'k':
6502 1e37a5c2 2019-05-12 jcs case KEY_UP:
6503 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6504 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6505 1e37a5c2 2019-05-12 jcs s->selected--;
6506 fa86c4bf 2020-11-29 stsp break;
6507 1e37a5c2 2019-05-12 jcs }
6508 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6509 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6510 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6511 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6512 640cd7ff 2022-06-22 mark view->count = 0;
6513 1e37a5c2 2019-05-12 jcs break;
6514 83cc4199 2022-06-13 stsp case CTRL('u'):
6515 33c3719a 2022-06-15 stsp case 'u':
6516 83cc4199 2022-06-13 stsp nscroll /= 2;
6517 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6518 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6519 ea025d1d 2020-02-22 naddy case CTRL('b'):
6520 61417565 2022-06-20 mark case 'b':
6521 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6522 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6523 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6524 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6525 fa86c4bf 2020-11-29 stsp } else {
6526 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6527 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6528 fa86c4bf 2020-11-29 stsp }
6529 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6530 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6531 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6532 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6533 640cd7ff 2022-06-22 mark view->count = 0;
6534 1e37a5c2 2019-05-12 jcs break;
6535 1e37a5c2 2019-05-12 jcs case 'j':
6536 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6537 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6538 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6539 1e37a5c2 2019-05-12 jcs s->selected++;
6540 1e37a5c2 2019-05-12 jcs break;
6541 1e37a5c2 2019-05-12 jcs }
6542 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6543 640cd7ff 2022-06-22 mark == NULL) {
6544 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6545 640cd7ff 2022-06-22 mark view->count = 0;
6546 1e37a5c2 2019-05-12 jcs break;
6547 640cd7ff 2022-06-22 mark }
6548 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6549 1e37a5c2 2019-05-12 jcs break;
6550 83cc4199 2022-06-13 stsp case CTRL('d'):
6551 33c3719a 2022-06-15 stsp case 'd':
6552 83cc4199 2022-06-13 stsp nscroll /= 2;
6553 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6554 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6555 ea025d1d 2020-02-22 naddy case CTRL('f'):
6556 61417565 2022-06-20 mark case 'f':
6557 48bb96f0 2022-06-20 naddy case ' ':
6558 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6559 1e37a5c2 2019-05-12 jcs == NULL) {
6560 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6561 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6562 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6563 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6564 640cd7ff 2022-06-22 mark else
6565 640cd7ff 2022-06-22 mark view->count = 0;
6566 1e37a5c2 2019-05-12 jcs break;
6567 1e37a5c2 2019-05-12 jcs }
6568 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6569 1e37a5c2 2019-05-12 jcs break;
6570 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6571 1e37a5c2 2019-05-12 jcs case '\r':
6572 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6573 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6574 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6575 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6576 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6577 640cd7ff 2022-06-22 mark view->count = 0;
6578 1e37a5c2 2019-05-12 jcs break;
6579 640cd7ff 2022-06-22 mark }
6580 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6581 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6582 1e37a5c2 2019-05-12 jcs entry);
6583 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6584 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6585 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6586 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6587 1e37a5c2 2019-05-12 jcs s->selected_entry =
6588 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6589 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6590 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6591 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6592 9b058f45 2022-06-30 mark if (err)
6593 9b058f45 2022-06-30 mark break;
6594 9b058f45 2022-06-30 mark }
6595 1e37a5c2 2019-05-12 jcs free(parent);
6596 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6597 56e0773d 2019-11-28 stsp s->selected_entry))) {
6598 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6599 640cd7ff 2022-06-22 mark view->count = 0;
6600 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6601 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6602 1e37a5c2 2019-05-12 jcs if (err)
6603 1e37a5c2 2019-05-12 jcs break;
6604 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6605 941e9f74 2019-05-21 stsp if (err) {
6606 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6607 1e37a5c2 2019-05-12 jcs break;
6608 1e37a5c2 2019-05-12 jcs }
6609 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6610 56e0773d 2019-11-28 stsp s->selected_entry))) {
6611 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6612 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6613 1e37a5c2 2019-05-12 jcs
6614 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6615 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6616 9b058f45 2022-06-30 mark
6617 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6618 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6619 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6620 1e37a5c2 2019-05-12 jcs if (err)
6621 1e37a5c2 2019-05-12 jcs break;
6622 9b058f45 2022-06-30 mark
6623 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6624 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6625 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6626 9b058f45 2022-06-30 mark if (err)
6627 9b058f45 2022-06-30 mark break;
6628 9b058f45 2022-06-30 mark }
6629 9b058f45 2022-06-30 mark
6630 640cd7ff 2022-06-22 mark view->count = 0;
6631 e78dc838 2020-12-04 stsp view->focussed = 0;
6632 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6633 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6634 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6635 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6636 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6637 669b5ffa 2018-10-07 stsp if (err)
6638 669b5ffa 2018-10-07 stsp return err;
6639 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6640 0dbbbe90 2022-06-17 op if (err)
6641 0dbbbe90 2022-06-17 op return err;
6642 e78dc838 2020-12-04 stsp view->focus_child = 1;
6643 669b5ffa 2018-10-07 stsp } else
6644 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6645 1e37a5c2 2019-05-12 jcs }
6646 1e37a5c2 2019-05-12 jcs break;
6647 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6648 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6649 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6650 640cd7ff 2022-06-22 mark view->count = 0;
6651 1e37a5c2 2019-05-12 jcs break;
6652 1e37a5c2 2019-05-12 jcs default:
6653 640cd7ff 2022-06-22 mark view->count = 0;
6654 1e37a5c2 2019-05-12 jcs break;
6655 ffd1d5e5 2018-06-23 stsp }
6656 e5a0f69f 2018-08-18 stsp
6657 ffd1d5e5 2018-06-23 stsp return err;
6658 9f7d7167 2018-04-29 stsp }
6659 9f7d7167 2018-04-29 stsp
6660 ffd1d5e5 2018-06-23 stsp __dead static void
6661 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6662 ffd1d5e5 2018-06-23 stsp {
6663 ffd1d5e5 2018-06-23 stsp endwin();
6664 87411fa9 2022-06-16 stsp fprintf(stderr,
6665 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6666 ffd1d5e5 2018-06-23 stsp getprogname());
6667 ffd1d5e5 2018-06-23 stsp exit(1);
6668 ffd1d5e5 2018-06-23 stsp }
6669 ffd1d5e5 2018-06-23 stsp
6670 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6671 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6672 ffd1d5e5 2018-06-23 stsp {
6673 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6674 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6675 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6676 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6677 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6678 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6679 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6680 4e97c21c 2020-12-06 stsp char *label = NULL;
6681 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6682 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6683 ffd1d5e5 2018-06-23 stsp int ch;
6684 5221c383 2018-08-01 stsp struct tog_view *view;
6685 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6686 70ac5f84 2019-03-28 stsp
6687 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6688 ffd1d5e5 2018-06-23 stsp switch (ch) {
6689 ffd1d5e5 2018-06-23 stsp case 'c':
6690 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6691 74283ab8 2020-02-07 stsp break;
6692 74283ab8 2020-02-07 stsp case 'r':
6693 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6694 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6695 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6696 74283ab8 2020-02-07 stsp optarg);
6697 ffd1d5e5 2018-06-23 stsp break;
6698 ffd1d5e5 2018-06-23 stsp default:
6699 e99e2d15 2020-11-24 naddy usage_tree();
6700 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6701 ffd1d5e5 2018-06-23 stsp }
6702 ffd1d5e5 2018-06-23 stsp }
6703 ffd1d5e5 2018-06-23 stsp
6704 ffd1d5e5 2018-06-23 stsp argc -= optind;
6705 ffd1d5e5 2018-06-23 stsp argv += optind;
6706 ffd1d5e5 2018-06-23 stsp
6707 55cccc34 2020-02-20 stsp if (argc > 1)
6708 e99e2d15 2020-11-24 naddy usage_tree();
6709 0ae84acc 2022-06-15 tracey
6710 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6711 0ae84acc 2022-06-15 tracey if (error != NULL)
6712 0ae84acc 2022-06-15 tracey goto done;
6713 74283ab8 2020-02-07 stsp
6714 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6715 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6716 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6717 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6718 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6719 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6720 c156c7a4 2020-12-18 stsp goto done;
6721 55cccc34 2020-02-20 stsp if (worktree)
6722 52185f70 2019-02-05 stsp repo_path =
6723 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6724 55cccc34 2020-02-20 stsp else
6725 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6726 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6727 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6728 c156c7a4 2020-12-18 stsp goto done;
6729 c156c7a4 2020-12-18 stsp }
6730 55cccc34 2020-02-20 stsp }
6731 a915003a 2019-02-05 stsp
6732 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6733 c02c541e 2019-03-29 stsp if (error != NULL)
6734 52185f70 2019-02-05 stsp goto done;
6735 d188b9a6 2019-01-04 stsp
6736 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6737 55cccc34 2020-02-20 stsp repo, worktree);
6738 55cccc34 2020-02-20 stsp if (error)
6739 55cccc34 2020-02-20 stsp goto done;
6740 55cccc34 2020-02-20 stsp
6741 55cccc34 2020-02-20 stsp init_curses();
6742 55cccc34 2020-02-20 stsp
6743 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6744 c02c541e 2019-03-29 stsp if (error)
6745 52185f70 2019-02-05 stsp goto done;
6746 ffd1d5e5 2018-06-23 stsp
6747 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6748 51a10b52 2020-12-26 stsp if (error)
6749 51a10b52 2020-12-26 stsp goto done;
6750 51a10b52 2020-12-26 stsp
6751 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6752 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6753 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6754 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6755 4e97c21c 2020-12-06 stsp if (error)
6756 4e97c21c 2020-12-06 stsp goto done;
6757 4e97c21c 2020-12-06 stsp head_ref_name = label;
6758 4e97c21c 2020-12-06 stsp } else {
6759 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6760 4e97c21c 2020-12-06 stsp if (error == NULL)
6761 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6762 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6763 4e97c21c 2020-12-06 stsp goto done;
6764 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6765 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6766 4e97c21c 2020-12-06 stsp if (error)
6767 4e97c21c 2020-12-06 stsp goto done;
6768 4e97c21c 2020-12-06 stsp }
6769 ffd1d5e5 2018-06-23 stsp
6770 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6771 a44927cc 2022-04-07 stsp if (error)
6772 a44927cc 2022-04-07 stsp goto done;
6773 a44927cc 2022-04-07 stsp
6774 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6775 5221c383 2018-08-01 stsp if (view == NULL) {
6776 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6777 5221c383 2018-08-01 stsp goto done;
6778 5221c383 2018-08-01 stsp }
6779 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6780 ad80ab7b 2018-08-04 stsp if (error)
6781 ad80ab7b 2018-08-04 stsp goto done;
6782 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6783 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6784 d91faf3b 2020-12-01 naddy in_repo_path);
6785 55cccc34 2020-02-20 stsp if (error)
6786 55cccc34 2020-02-20 stsp goto done;
6787 55cccc34 2020-02-20 stsp }
6788 55cccc34 2020-02-20 stsp
6789 55cccc34 2020-02-20 stsp if (worktree) {
6790 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6791 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6792 55cccc34 2020-02-20 stsp worktree = NULL;
6793 55cccc34 2020-02-20 stsp }
6794 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6795 ffd1d5e5 2018-06-23 stsp done:
6796 52185f70 2019-02-05 stsp free(repo_path);
6797 e4a0e26d 2020-02-20 stsp free(cwd);
6798 ffd1d5e5 2018-06-23 stsp free(commit_id);
6799 4e97c21c 2020-12-06 stsp free(label);
6800 486cd271 2020-12-06 stsp if (ref)
6801 486cd271 2020-12-06 stsp got_ref_close(ref);
6802 1d0f4054 2021-06-17 stsp if (repo) {
6803 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6804 1d0f4054 2021-06-17 stsp if (error == NULL)
6805 1d0f4054 2021-06-17 stsp error = close_err;
6806 1d0f4054 2021-06-17 stsp }
6807 0ae84acc 2022-06-15 tracey if (pack_fds) {
6808 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6809 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6810 0ae84acc 2022-06-15 tracey if (error == NULL)
6811 0ae84acc 2022-06-15 tracey error = pack_err;
6812 0ae84acc 2022-06-15 tracey }
6813 51a10b52 2020-12-26 stsp tog_free_refs();
6814 ffd1d5e5 2018-06-23 stsp return error;
6815 6458efa5 2020-11-24 stsp }
6816 6458efa5 2020-11-24 stsp
6817 6458efa5 2020-11-24 stsp static const struct got_error *
6818 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6819 6458efa5 2020-11-24 stsp {
6820 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6821 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6822 6458efa5 2020-11-24 stsp
6823 6458efa5 2020-11-24 stsp s->nrefs = 0;
6824 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6825 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6826 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6827 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6828 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6829 6458efa5 2020-11-24 stsp continue;
6830 6458efa5 2020-11-24 stsp
6831 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6832 6458efa5 2020-11-24 stsp if (re == NULL)
6833 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6834 6458efa5 2020-11-24 stsp
6835 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6836 8924d611 2020-12-26 stsp if (re->ref == NULL)
6837 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6838 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6839 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6840 6458efa5 2020-11-24 stsp }
6841 6458efa5 2020-11-24 stsp
6842 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6843 6458efa5 2020-11-24 stsp return NULL;
6844 6458efa5 2020-11-24 stsp }
6845 6458efa5 2020-11-24 stsp
6846 336075a4 2022-06-25 op static void
6847 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6848 6458efa5 2020-11-24 stsp {
6849 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6850 6458efa5 2020-11-24 stsp
6851 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6852 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6853 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6854 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6855 6458efa5 2020-11-24 stsp free(re);
6856 6458efa5 2020-11-24 stsp }
6857 6458efa5 2020-11-24 stsp }
6858 6458efa5 2020-11-24 stsp
6859 6458efa5 2020-11-24 stsp static const struct got_error *
6860 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6861 6458efa5 2020-11-24 stsp {
6862 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6863 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6864 6458efa5 2020-11-24 stsp
6865 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6866 6458efa5 2020-11-24 stsp s->repo = repo;
6867 6458efa5 2020-11-24 stsp
6868 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6869 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6870 6458efa5 2020-11-24 stsp
6871 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6872 6458efa5 2020-11-24 stsp if (err)
6873 6458efa5 2020-11-24 stsp return err;
6874 34ba6917 2020-11-30 stsp
6875 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6876 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6877 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6878 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6879 6458efa5 2020-11-24 stsp if (err)
6880 6458efa5 2020-11-24 stsp goto done;
6881 6458efa5 2020-11-24 stsp
6882 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6883 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6884 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6885 6458efa5 2020-11-24 stsp if (err)
6886 6458efa5 2020-11-24 stsp goto done;
6887 6458efa5 2020-11-24 stsp
6888 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6889 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6890 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6891 cc488aa7 2022-01-23 stsp if (err)
6892 cc488aa7 2022-01-23 stsp goto done;
6893 cc488aa7 2022-01-23 stsp
6894 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6895 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6896 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6897 6458efa5 2020-11-24 stsp if (err)
6898 6458efa5 2020-11-24 stsp goto done;
6899 6458efa5 2020-11-24 stsp }
6900 6458efa5 2020-11-24 stsp
6901 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6902 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6903 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6904 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6905 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6906 6458efa5 2020-11-24 stsp done:
6907 6458efa5 2020-11-24 stsp if (err)
6908 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6909 6458efa5 2020-11-24 stsp return err;
6910 6458efa5 2020-11-24 stsp }
6911 6458efa5 2020-11-24 stsp
6912 6458efa5 2020-11-24 stsp static const struct got_error *
6913 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6914 6458efa5 2020-11-24 stsp {
6915 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6916 6458efa5 2020-11-24 stsp
6917 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6918 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6919 6458efa5 2020-11-24 stsp
6920 6458efa5 2020-11-24 stsp return NULL;
6921 9f7d7167 2018-04-29 stsp }
6922 ce5b7c56 2019-07-09 stsp
6923 6458efa5 2020-11-24 stsp static const struct got_error *
6924 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6925 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6926 6458efa5 2020-11-24 stsp {
6927 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6928 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6929 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6930 6458efa5 2020-11-24 stsp int obj_type;
6931 6458efa5 2020-11-24 stsp
6932 c42c9805 2020-11-24 stsp *commit_id = NULL;
6933 6458efa5 2020-11-24 stsp
6934 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6935 6458efa5 2020-11-24 stsp if (err)
6936 6458efa5 2020-11-24 stsp return err;
6937 6458efa5 2020-11-24 stsp
6938 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6939 6458efa5 2020-11-24 stsp if (err)
6940 6458efa5 2020-11-24 stsp goto done;
6941 6458efa5 2020-11-24 stsp
6942 6458efa5 2020-11-24 stsp switch (obj_type) {
6943 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6944 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6945 6458efa5 2020-11-24 stsp break;
6946 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6947 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6948 6458efa5 2020-11-24 stsp if (err)
6949 6458efa5 2020-11-24 stsp goto done;
6950 c42c9805 2020-11-24 stsp free(obj_id);
6951 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6952 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6953 c42c9805 2020-11-24 stsp if (err)
6954 6458efa5 2020-11-24 stsp goto done;
6955 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6956 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6957 c42c9805 2020-11-24 stsp goto done;
6958 c42c9805 2020-11-24 stsp }
6959 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6960 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6961 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6962 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6963 c42c9805 2020-11-24 stsp goto done;
6964 c42c9805 2020-11-24 stsp }
6965 6458efa5 2020-11-24 stsp break;
6966 6458efa5 2020-11-24 stsp default:
6967 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6968 c42c9805 2020-11-24 stsp break;
6969 6458efa5 2020-11-24 stsp }
6970 6458efa5 2020-11-24 stsp
6971 c42c9805 2020-11-24 stsp done:
6972 c42c9805 2020-11-24 stsp if (tag)
6973 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6974 c42c9805 2020-11-24 stsp if (err) {
6975 c42c9805 2020-11-24 stsp free(*commit_id);
6976 c42c9805 2020-11-24 stsp *commit_id = NULL;
6977 c42c9805 2020-11-24 stsp }
6978 c42c9805 2020-11-24 stsp return err;
6979 c42c9805 2020-11-24 stsp }
6980 c42c9805 2020-11-24 stsp
6981 c42c9805 2020-11-24 stsp static const struct got_error *
6982 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6983 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6984 c42c9805 2020-11-24 stsp {
6985 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6986 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6987 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6988 c42c9805 2020-11-24 stsp
6989 c42c9805 2020-11-24 stsp *new_view = NULL;
6990 c42c9805 2020-11-24 stsp
6991 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6992 c42c9805 2020-11-24 stsp if (err) {
6993 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6994 c42c9805 2020-11-24 stsp return err;
6995 c42c9805 2020-11-24 stsp else
6996 c42c9805 2020-11-24 stsp return NULL;
6997 c42c9805 2020-11-24 stsp }
6998 c42c9805 2020-11-24 stsp
6999 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7000 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7001 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7002 6458efa5 2020-11-24 stsp goto done;
7003 6458efa5 2020-11-24 stsp }
7004 6458efa5 2020-11-24 stsp
7005 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7006 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7007 6458efa5 2020-11-24 stsp done:
7008 6458efa5 2020-11-24 stsp if (err)
7009 6458efa5 2020-11-24 stsp view_close(log_view);
7010 6458efa5 2020-11-24 stsp else
7011 6458efa5 2020-11-24 stsp *new_view = log_view;
7012 c42c9805 2020-11-24 stsp free(commit_id);
7013 6458efa5 2020-11-24 stsp return err;
7014 6458efa5 2020-11-24 stsp }
7015 6458efa5 2020-11-24 stsp
7016 ce5b7c56 2019-07-09 stsp static void
7017 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7018 6458efa5 2020-11-24 stsp {
7019 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7020 34ba6917 2020-11-30 stsp int i = 0;
7021 6458efa5 2020-11-24 stsp
7022 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7023 6458efa5 2020-11-24 stsp return;
7024 6458efa5 2020-11-24 stsp
7025 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7026 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7027 34ba6917 2020-11-30 stsp if (re == NULL)
7028 34ba6917 2020-11-30 stsp break;
7029 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7030 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7031 6458efa5 2020-11-24 stsp }
7032 6458efa5 2020-11-24 stsp }
7033 6458efa5 2020-11-24 stsp
7034 9b058f45 2022-06-30 mark static const struct got_error *
7035 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7036 6458efa5 2020-11-24 stsp {
7037 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7038 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7039 6458efa5 2020-11-24 stsp int n = 0;
7040 6458efa5 2020-11-24 stsp
7041 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7042 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7043 6458efa5 2020-11-24 stsp else
7044 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7045 6458efa5 2020-11-24 stsp
7046 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7047 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7048 9b058f45 2022-06-30 mark if (last)
7049 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7050 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7051 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7052 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7053 6458efa5 2020-11-24 stsp }
7054 6458efa5 2020-11-24 stsp }
7055 9b058f45 2022-06-30 mark
7056 9b058f45 2022-06-30 mark return NULL;
7057 6458efa5 2020-11-24 stsp }
7058 6458efa5 2020-11-24 stsp
7059 6458efa5 2020-11-24 stsp static const struct got_error *
7060 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7061 6458efa5 2020-11-24 stsp {
7062 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7063 6458efa5 2020-11-24 stsp
7064 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7065 6458efa5 2020-11-24 stsp return NULL;
7066 6458efa5 2020-11-24 stsp }
7067 6458efa5 2020-11-24 stsp
7068 6458efa5 2020-11-24 stsp static int
7069 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7070 6458efa5 2020-11-24 stsp {
7071 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7072 6458efa5 2020-11-24 stsp
7073 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7074 6458efa5 2020-11-24 stsp 0) == 0;
7075 6458efa5 2020-11-24 stsp }
7076 6458efa5 2020-11-24 stsp
7077 6458efa5 2020-11-24 stsp static const struct got_error *
7078 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7079 6458efa5 2020-11-24 stsp {
7080 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7081 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7082 6458efa5 2020-11-24 stsp
7083 6458efa5 2020-11-24 stsp if (!view->searching) {
7084 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7085 6458efa5 2020-11-24 stsp return NULL;
7086 6458efa5 2020-11-24 stsp }
7087 6458efa5 2020-11-24 stsp
7088 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7089 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7090 6458efa5 2020-11-24 stsp if (s->selected_entry)
7091 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7092 6458efa5 2020-11-24 stsp else
7093 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7094 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7095 6458efa5 2020-11-24 stsp } else {
7096 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7097 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7098 6458efa5 2020-11-24 stsp else
7099 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7100 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7101 6458efa5 2020-11-24 stsp }
7102 6458efa5 2020-11-24 stsp } else {
7103 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7104 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7105 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7106 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7107 6458efa5 2020-11-24 stsp else
7108 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7109 6458efa5 2020-11-24 stsp }
7110 6458efa5 2020-11-24 stsp
7111 6458efa5 2020-11-24 stsp while (1) {
7112 6458efa5 2020-11-24 stsp if (re == NULL) {
7113 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7114 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7115 6458efa5 2020-11-24 stsp return NULL;
7116 6458efa5 2020-11-24 stsp }
7117 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7118 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7119 6458efa5 2020-11-24 stsp else
7120 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7121 6458efa5 2020-11-24 stsp }
7122 6458efa5 2020-11-24 stsp
7123 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7124 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7125 6458efa5 2020-11-24 stsp s->matched_entry = re;
7126 6458efa5 2020-11-24 stsp break;
7127 6458efa5 2020-11-24 stsp }
7128 6458efa5 2020-11-24 stsp
7129 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7130 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7131 6458efa5 2020-11-24 stsp else
7132 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7133 6458efa5 2020-11-24 stsp }
7134 6458efa5 2020-11-24 stsp
7135 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7136 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7137 6458efa5 2020-11-24 stsp s->selected = 0;
7138 6458efa5 2020-11-24 stsp }
7139 6458efa5 2020-11-24 stsp
7140 6458efa5 2020-11-24 stsp return NULL;
7141 6458efa5 2020-11-24 stsp }
7142 6458efa5 2020-11-24 stsp
7143 6458efa5 2020-11-24 stsp static const struct got_error *
7144 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7145 6458efa5 2020-11-24 stsp {
7146 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7147 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7148 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7149 6458efa5 2020-11-24 stsp char *line = NULL;
7150 6458efa5 2020-11-24 stsp wchar_t *wline;
7151 6458efa5 2020-11-24 stsp struct tog_color *tc;
7152 6458efa5 2020-11-24 stsp int width, n;
7153 6458efa5 2020-11-24 stsp int limit = view->nlines;
7154 6458efa5 2020-11-24 stsp
7155 6458efa5 2020-11-24 stsp werase(view->window);
7156 6458efa5 2020-11-24 stsp
7157 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7158 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7159 9b058f45 2022-06-30 mark --limit; /* border */
7160 6458efa5 2020-11-24 stsp
7161 6458efa5 2020-11-24 stsp if (limit == 0)
7162 6458efa5 2020-11-24 stsp return NULL;
7163 6458efa5 2020-11-24 stsp
7164 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7165 6458efa5 2020-11-24 stsp
7166 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7167 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7168 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7169 6458efa5 2020-11-24 stsp
7170 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7171 6458efa5 2020-11-24 stsp if (err) {
7172 6458efa5 2020-11-24 stsp free(line);
7173 6458efa5 2020-11-24 stsp return err;
7174 6458efa5 2020-11-24 stsp }
7175 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7176 6458efa5 2020-11-24 stsp wstandout(view->window);
7177 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7178 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7179 6458efa5 2020-11-24 stsp wstandend(view->window);
7180 6458efa5 2020-11-24 stsp free(wline);
7181 6458efa5 2020-11-24 stsp wline = NULL;
7182 6458efa5 2020-11-24 stsp free(line);
7183 6458efa5 2020-11-24 stsp line = NULL;
7184 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7185 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7186 6458efa5 2020-11-24 stsp if (--limit <= 0)
7187 6458efa5 2020-11-24 stsp return NULL;
7188 6458efa5 2020-11-24 stsp
7189 6458efa5 2020-11-24 stsp n = 0;
7190 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7191 6458efa5 2020-11-24 stsp char *line = NULL;
7192 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7193 6458efa5 2020-11-24 stsp
7194 b4996bee 2022-06-16 stsp if (s->show_date) {
7195 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7196 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7197 b4996bee 2022-06-16 stsp struct got_object_id *id;
7198 b4996bee 2022-06-16 stsp struct tm tm;
7199 b4996bee 2022-06-16 stsp time_t t;
7200 b4996bee 2022-06-16 stsp
7201 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7202 b4996bee 2022-06-16 stsp if (err)
7203 b4996bee 2022-06-16 stsp return err;
7204 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7205 b4996bee 2022-06-16 stsp if (err) {
7206 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7207 b4996bee 2022-06-16 stsp free(id);
7208 b4996bee 2022-06-16 stsp return err;
7209 b4996bee 2022-06-16 stsp }
7210 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7211 b4996bee 2022-06-16 stsp id);
7212 b4996bee 2022-06-16 stsp if (err) {
7213 b4996bee 2022-06-16 stsp free(id);
7214 b4996bee 2022-06-16 stsp return err;
7215 b4996bee 2022-06-16 stsp }
7216 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7217 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7218 b4996bee 2022-06-16 stsp } else {
7219 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7220 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7221 b4996bee 2022-06-16 stsp }
7222 b4996bee 2022-06-16 stsp free(id);
7223 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7224 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7225 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7226 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7227 b4996bee 2022-06-16 stsp }
7228 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7229 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7230 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7231 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7232 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7233 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7234 6458efa5 2020-11-24 stsp struct got_object_id *id;
7235 6458efa5 2020-11-24 stsp char *id_str;
7236 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7237 6458efa5 2020-11-24 stsp if (err)
7238 6458efa5 2020-11-24 stsp return err;
7239 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7240 6458efa5 2020-11-24 stsp if (err) {
7241 6458efa5 2020-11-24 stsp free(id);
7242 6458efa5 2020-11-24 stsp return err;
7243 6458efa5 2020-11-24 stsp }
7244 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7245 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7246 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7247 6458efa5 2020-11-24 stsp free(id);
7248 6458efa5 2020-11-24 stsp free(id_str);
7249 6458efa5 2020-11-24 stsp return err;
7250 6458efa5 2020-11-24 stsp }
7251 6458efa5 2020-11-24 stsp free(id);
7252 6458efa5 2020-11-24 stsp free(id_str);
7253 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7254 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7255 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7256 6458efa5 2020-11-24 stsp
7257 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7258 ccda2f4d 2022-06-16 stsp 0, 0);
7259 6458efa5 2020-11-24 stsp if (err) {
7260 6458efa5 2020-11-24 stsp free(line);
7261 6458efa5 2020-11-24 stsp return err;
7262 6458efa5 2020-11-24 stsp }
7263 6458efa5 2020-11-24 stsp if (n == s->selected) {
7264 6458efa5 2020-11-24 stsp if (view->focussed)
7265 6458efa5 2020-11-24 stsp wstandout(view->window);
7266 6458efa5 2020-11-24 stsp s->selected_entry = re;
7267 6458efa5 2020-11-24 stsp }
7268 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7269 6458efa5 2020-11-24 stsp if (tc)
7270 6458efa5 2020-11-24 stsp wattr_on(view->window,
7271 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7272 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7273 6458efa5 2020-11-24 stsp if (tc)
7274 6458efa5 2020-11-24 stsp wattr_off(view->window,
7275 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7276 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7277 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7278 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7279 6458efa5 2020-11-24 stsp wstandend(view->window);
7280 6458efa5 2020-11-24 stsp free(line);
7281 6458efa5 2020-11-24 stsp free(wline);
7282 6458efa5 2020-11-24 stsp wline = NULL;
7283 6458efa5 2020-11-24 stsp n++;
7284 6458efa5 2020-11-24 stsp s->ndisplayed++;
7285 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7286 6458efa5 2020-11-24 stsp
7287 6458efa5 2020-11-24 stsp limit--;
7288 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7289 6458efa5 2020-11-24 stsp }
7290 6458efa5 2020-11-24 stsp
7291 9b058f45 2022-06-30 mark view_border(view);
7292 6458efa5 2020-11-24 stsp return err;
7293 6458efa5 2020-11-24 stsp }
7294 6458efa5 2020-11-24 stsp
7295 6458efa5 2020-11-24 stsp static const struct got_error *
7296 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7297 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7298 c42c9805 2020-11-24 stsp {
7299 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7300 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7301 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7302 c42c9805 2020-11-24 stsp
7303 c42c9805 2020-11-24 stsp *new_view = NULL;
7304 c42c9805 2020-11-24 stsp
7305 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7306 c42c9805 2020-11-24 stsp if (err) {
7307 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7308 c42c9805 2020-11-24 stsp return err;
7309 c42c9805 2020-11-24 stsp else
7310 c42c9805 2020-11-24 stsp return NULL;
7311 c42c9805 2020-11-24 stsp }
7312 c42c9805 2020-11-24 stsp
7313 c42c9805 2020-11-24 stsp
7314 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7315 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7316 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7317 c42c9805 2020-11-24 stsp goto done;
7318 c42c9805 2020-11-24 stsp }
7319 c42c9805 2020-11-24 stsp
7320 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7321 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7322 c42c9805 2020-11-24 stsp if (err)
7323 c42c9805 2020-11-24 stsp goto done;
7324 c42c9805 2020-11-24 stsp
7325 c42c9805 2020-11-24 stsp *new_view = tree_view;
7326 c42c9805 2020-11-24 stsp done:
7327 c42c9805 2020-11-24 stsp free(commit_id);
7328 c42c9805 2020-11-24 stsp return err;
7329 c42c9805 2020-11-24 stsp }
7330 c42c9805 2020-11-24 stsp static const struct got_error *
7331 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7332 6458efa5 2020-11-24 stsp {
7333 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7334 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7335 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7336 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7337 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7338 6458efa5 2020-11-24 stsp
7339 6458efa5 2020-11-24 stsp switch (ch) {
7340 6458efa5 2020-11-24 stsp case 'i':
7341 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7342 640cd7ff 2022-06-22 mark view->count = 0;
7343 7f66531d 2021-11-16 stsp break;
7344 b4996bee 2022-06-16 stsp case 'm':
7345 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7346 640cd7ff 2022-06-22 mark view->count = 0;
7347 b4996bee 2022-06-16 stsp break;
7348 07a065fe 2021-11-20 stsp case 'o':
7349 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7350 640cd7ff 2022-06-22 mark view->count = 0;
7351 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7352 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7353 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7354 50617b77 2021-11-20 stsp if (err)
7355 50617b77 2021-11-20 stsp break;
7356 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7357 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7358 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7359 7f66531d 2021-11-16 stsp if (err)
7360 7f66531d 2021-11-16 stsp break;
7361 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7362 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7363 6458efa5 2020-11-24 stsp break;
7364 6458efa5 2020-11-24 stsp case KEY_ENTER:
7365 6458efa5 2020-11-24 stsp case '\r':
7366 640cd7ff 2022-06-22 mark view->count = 0;
7367 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7368 6458efa5 2020-11-24 stsp break;
7369 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7370 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7371 9b058f45 2022-06-30 mark
7372 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7373 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7374 9b058f45 2022-06-30 mark if (err)
7375 9b058f45 2022-06-30 mark break;
7376 9b058f45 2022-06-30 mark
7377 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7378 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7379 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7380 9b058f45 2022-06-30 mark if (err)
7381 9b058f45 2022-06-30 mark break;
7382 9b058f45 2022-06-30 mark }
7383 9b058f45 2022-06-30 mark
7384 e78dc838 2020-12-04 stsp view->focussed = 0;
7385 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7386 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7387 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7388 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7389 6458efa5 2020-11-24 stsp err = view_close_child(view);
7390 6458efa5 2020-11-24 stsp if (err)
7391 6458efa5 2020-11-24 stsp return err;
7392 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7393 0dbbbe90 2022-06-17 op if (err)
7394 0dbbbe90 2022-06-17 op return err;
7395 e78dc838 2020-12-04 stsp view->focus_child = 1;
7396 6458efa5 2020-11-24 stsp } else
7397 6458efa5 2020-11-24 stsp *new_view = log_view;
7398 6458efa5 2020-11-24 stsp break;
7399 c42c9805 2020-11-24 stsp case 't':
7400 640cd7ff 2022-06-22 mark view->count = 0;
7401 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7402 c42c9805 2020-11-24 stsp break;
7403 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7404 49b24ee5 2022-07-03 mark view_get_split(view, &begin_y, &begin_x);
7405 49b24ee5 2022-07-03 mark err = browse_ref_tree(&tree_view, begin_y, begin_x,
7406 49b24ee5 2022-07-03 mark s->selected_entry, s->repo);
7407 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7408 c42c9805 2020-11-24 stsp break;
7409 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
7410 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
7411 49b24ee5 2022-07-03 mark err = view_init_hsplit(view, begin_y);
7412 49b24ee5 2022-07-03 mark if (err)
7413 49b24ee5 2022-07-03 mark break;
7414 49b24ee5 2022-07-03 mark }
7415 e78dc838 2020-12-04 stsp view->focussed = 0;
7416 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7417 49b24ee5 2022-07-03 mark tree_view->mode = view->mode;
7418 49b24ee5 2022-07-03 mark tree_view->nlines = view->lines - begin_y;
7419 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7420 c42c9805 2020-11-24 stsp err = view_close_child(view);
7421 c42c9805 2020-11-24 stsp if (err)
7422 c42c9805 2020-11-24 stsp return err;
7423 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7424 0dbbbe90 2022-06-17 op if (err)
7425 0dbbbe90 2022-06-17 op return err;
7426 e78dc838 2020-12-04 stsp view->focus_child = 1;
7427 c42c9805 2020-11-24 stsp } else
7428 c42c9805 2020-11-24 stsp *new_view = tree_view;
7429 c42c9805 2020-11-24 stsp break;
7430 e4526bf5 2021-09-03 naddy case 'g':
7431 e4526bf5 2021-09-03 naddy case KEY_HOME:
7432 e4526bf5 2021-09-03 naddy s->selected = 0;
7433 640cd7ff 2022-06-22 mark view->count = 0;
7434 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7435 e4526bf5 2021-09-03 naddy break;
7436 e4526bf5 2021-09-03 naddy case 'G':
7437 9b058f45 2022-06-30 mark case KEY_END: {
7438 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7439 9b058f45 2022-06-30 mark
7440 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7441 9b058f45 2022-06-30 mark --eos; /* border */
7442 e4526bf5 2021-09-03 naddy s->selected = 0;
7443 640cd7ff 2022-06-22 mark view->count = 0;
7444 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7445 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7446 e4526bf5 2021-09-03 naddy if (re == NULL)
7447 e4526bf5 2021-09-03 naddy break;
7448 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7449 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7450 e4526bf5 2021-09-03 naddy }
7451 e4526bf5 2021-09-03 naddy if (n > 0)
7452 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7453 e4526bf5 2021-09-03 naddy break;
7454 9b058f45 2022-06-30 mark }
7455 6458efa5 2020-11-24 stsp case 'k':
7456 6458efa5 2020-11-24 stsp case KEY_UP:
7457 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7458 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7459 6458efa5 2020-11-24 stsp s->selected--;
7460 6458efa5 2020-11-24 stsp break;
7461 34ba6917 2020-11-30 stsp }
7462 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7463 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7464 640cd7ff 2022-06-22 mark view->count = 0;
7465 6458efa5 2020-11-24 stsp break;
7466 83cc4199 2022-06-13 stsp case CTRL('u'):
7467 33c3719a 2022-06-15 stsp case 'u':
7468 83cc4199 2022-06-13 stsp nscroll /= 2;
7469 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7470 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7471 6458efa5 2020-11-24 stsp case CTRL('b'):
7472 61417565 2022-06-20 mark case 'b':
7473 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7474 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7475 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7476 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7477 640cd7ff 2022-06-22 mark view->count = 0;
7478 6458efa5 2020-11-24 stsp break;
7479 6458efa5 2020-11-24 stsp case 'j':
7480 6458efa5 2020-11-24 stsp case KEY_DOWN:
7481 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7482 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7483 6458efa5 2020-11-24 stsp s->selected++;
7484 6458efa5 2020-11-24 stsp break;
7485 6458efa5 2020-11-24 stsp }
7486 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7487 6458efa5 2020-11-24 stsp /* can't scroll any further */
7488 640cd7ff 2022-06-22 mark view->count = 0;
7489 6458efa5 2020-11-24 stsp break;
7490 640cd7ff 2022-06-22 mark }
7491 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7492 6458efa5 2020-11-24 stsp break;
7493 83cc4199 2022-06-13 stsp case CTRL('d'):
7494 33c3719a 2022-06-15 stsp case 'd':
7495 83cc4199 2022-06-13 stsp nscroll /= 2;
7496 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7497 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7498 6458efa5 2020-11-24 stsp case CTRL('f'):
7499 61417565 2022-06-20 mark case 'f':
7500 48bb96f0 2022-06-20 naddy case ' ':
7501 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7502 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7503 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7504 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7505 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7506 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7507 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7508 640cd7ff 2022-06-22 mark view->count = 0;
7509 6458efa5 2020-11-24 stsp break;
7510 6458efa5 2020-11-24 stsp }
7511 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7512 6458efa5 2020-11-24 stsp break;
7513 6458efa5 2020-11-24 stsp case CTRL('l'):
7514 640cd7ff 2022-06-22 mark view->count = 0;
7515 8924d611 2020-12-26 stsp tog_free_refs();
7516 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7517 8924d611 2020-12-26 stsp if (err)
7518 8924d611 2020-12-26 stsp break;
7519 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7520 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7521 6458efa5 2020-11-24 stsp break;
7522 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7523 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7524 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7525 6458efa5 2020-11-24 stsp break;
7526 6458efa5 2020-11-24 stsp default:
7527 640cd7ff 2022-06-22 mark view->count = 0;
7528 6458efa5 2020-11-24 stsp break;
7529 6458efa5 2020-11-24 stsp }
7530 6458efa5 2020-11-24 stsp
7531 6458efa5 2020-11-24 stsp return err;
7532 6458efa5 2020-11-24 stsp }
7533 6458efa5 2020-11-24 stsp
7534 6458efa5 2020-11-24 stsp __dead static void
7535 6458efa5 2020-11-24 stsp usage_ref(void)
7536 6458efa5 2020-11-24 stsp {
7537 6458efa5 2020-11-24 stsp endwin();
7538 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7539 6458efa5 2020-11-24 stsp getprogname());
7540 6458efa5 2020-11-24 stsp exit(1);
7541 6458efa5 2020-11-24 stsp }
7542 6458efa5 2020-11-24 stsp
7543 6458efa5 2020-11-24 stsp static const struct got_error *
7544 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7545 6458efa5 2020-11-24 stsp {
7546 6458efa5 2020-11-24 stsp const struct got_error *error;
7547 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7548 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7549 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7550 6458efa5 2020-11-24 stsp int ch;
7551 6458efa5 2020-11-24 stsp struct tog_view *view;
7552 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7553 6458efa5 2020-11-24 stsp
7554 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7555 6458efa5 2020-11-24 stsp switch (ch) {
7556 6458efa5 2020-11-24 stsp case 'r':
7557 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7558 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7559 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7560 6458efa5 2020-11-24 stsp optarg);
7561 6458efa5 2020-11-24 stsp break;
7562 6458efa5 2020-11-24 stsp default:
7563 e99e2d15 2020-11-24 naddy usage_ref();
7564 6458efa5 2020-11-24 stsp /* NOTREACHED */
7565 6458efa5 2020-11-24 stsp }
7566 6458efa5 2020-11-24 stsp }
7567 6458efa5 2020-11-24 stsp
7568 6458efa5 2020-11-24 stsp argc -= optind;
7569 6458efa5 2020-11-24 stsp argv += optind;
7570 6458efa5 2020-11-24 stsp
7571 6458efa5 2020-11-24 stsp if (argc > 1)
7572 e99e2d15 2020-11-24 naddy usage_ref();
7573 0ae84acc 2022-06-15 tracey
7574 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7575 0ae84acc 2022-06-15 tracey if (error != NULL)
7576 0ae84acc 2022-06-15 tracey goto done;
7577 6458efa5 2020-11-24 stsp
7578 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7579 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7580 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7581 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7582 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7583 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7584 c156c7a4 2020-12-18 stsp goto done;
7585 6458efa5 2020-11-24 stsp if (worktree)
7586 6458efa5 2020-11-24 stsp repo_path =
7587 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7588 6458efa5 2020-11-24 stsp else
7589 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7590 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7591 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7592 c156c7a4 2020-12-18 stsp goto done;
7593 c156c7a4 2020-12-18 stsp }
7594 6458efa5 2020-11-24 stsp }
7595 6458efa5 2020-11-24 stsp
7596 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7597 6458efa5 2020-11-24 stsp if (error != NULL)
7598 6458efa5 2020-11-24 stsp goto done;
7599 6458efa5 2020-11-24 stsp
7600 6458efa5 2020-11-24 stsp init_curses();
7601 6458efa5 2020-11-24 stsp
7602 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7603 51a10b52 2020-12-26 stsp if (error)
7604 51a10b52 2020-12-26 stsp goto done;
7605 51a10b52 2020-12-26 stsp
7606 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7607 6458efa5 2020-11-24 stsp if (error)
7608 6458efa5 2020-11-24 stsp goto done;
7609 6458efa5 2020-11-24 stsp
7610 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7611 6458efa5 2020-11-24 stsp if (view == NULL) {
7612 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7613 6458efa5 2020-11-24 stsp goto done;
7614 6458efa5 2020-11-24 stsp }
7615 6458efa5 2020-11-24 stsp
7616 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7617 6458efa5 2020-11-24 stsp if (error)
7618 6458efa5 2020-11-24 stsp goto done;
7619 6458efa5 2020-11-24 stsp
7620 6458efa5 2020-11-24 stsp if (worktree) {
7621 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7622 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7623 6458efa5 2020-11-24 stsp worktree = NULL;
7624 6458efa5 2020-11-24 stsp }
7625 6458efa5 2020-11-24 stsp error = view_loop(view);
7626 6458efa5 2020-11-24 stsp done:
7627 6458efa5 2020-11-24 stsp free(repo_path);
7628 6458efa5 2020-11-24 stsp free(cwd);
7629 1d0f4054 2021-06-17 stsp if (repo) {
7630 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7631 1d0f4054 2021-06-17 stsp if (close_err)
7632 1d0f4054 2021-06-17 stsp error = close_err;
7633 1d0f4054 2021-06-17 stsp }
7634 0ae84acc 2022-06-15 tracey if (pack_fds) {
7635 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7636 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7637 0ae84acc 2022-06-15 tracey if (error == NULL)
7638 0ae84acc 2022-06-15 tracey error = pack_err;
7639 0ae84acc 2022-06-15 tracey }
7640 51a10b52 2020-12-26 stsp tog_free_refs();
7641 6458efa5 2020-11-24 stsp return error;
7642 6458efa5 2020-11-24 stsp }
7643 6458efa5 2020-11-24 stsp
7644 9b058f45 2022-06-30 mark /*
7645 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7646 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7647 9b058f45 2022-06-30 mark */
7648 6458efa5 2020-11-24 stsp static void
7649 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7650 9b058f45 2022-06-30 mark {
7651 9b058f45 2022-06-30 mark switch (view->type) {
7652 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7653 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7654 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7655 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7656 9b058f45 2022-06-30 mark 1);
7657 9b058f45 2022-06-30 mark break;
7658 9b058f45 2022-06-30 mark }
7659 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7660 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7661 9b058f45 2022-06-30 mark else
7662 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7663 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7664 9b058f45 2022-06-30 mark break;
7665 9b058f45 2022-06-30 mark }
7666 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7667 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7668 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7669 9b058f45 2022-06-30 mark break;
7670 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7671 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7672 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7673 9b058f45 2022-06-30 mark break;
7674 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7675 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7676 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7677 9b058f45 2022-06-30 mark break;
7678 9b058f45 2022-06-30 mark default:
7679 9b058f45 2022-06-30 mark break;
7680 9b058f45 2022-06-30 mark }
7681 9b058f45 2022-06-30 mark
7682 9b058f45 2022-06-30 mark view->offset = 0;
7683 9b058f45 2022-06-30 mark }
7684 9b058f45 2022-06-30 mark
7685 9b058f45 2022-06-30 mark /*
7686 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7687 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7688 9b058f45 2022-06-30 mark */
7689 9b058f45 2022-06-30 mark static const struct got_error *
7690 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7691 9b058f45 2022-06-30 mark {
7692 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7693 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7694 9b058f45 2022-06-30 mark int *selected = NULL;
7695 9b058f45 2022-06-30 mark int header, offset;
7696 9b058f45 2022-06-30 mark
7697 9b058f45 2022-06-30 mark switch (view->type) {
7698 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7699 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7700 9b058f45 2022-06-30 mark header = 3;
7701 9b058f45 2022-06-30 mark scrolld = NULL;
7702 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7703 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7704 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7705 9b058f45 2022-06-30 mark s->selected_line -= offset;
7706 9b058f45 2022-06-30 mark view->offset = offset;
7707 9b058f45 2022-06-30 mark }
7708 9b058f45 2022-06-30 mark break;
7709 9b058f45 2022-06-30 mark }
7710 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7711 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7712 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7713 9b058f45 2022-06-30 mark header = 3;
7714 9b058f45 2022-06-30 mark selected = &s->selected;
7715 9b058f45 2022-06-30 mark break;
7716 9b058f45 2022-06-30 mark }
7717 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7718 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7719 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7720 9b058f45 2022-06-30 mark header = 3;
7721 9b058f45 2022-06-30 mark selected = &s->selected;
7722 9b058f45 2022-06-30 mark break;
7723 9b058f45 2022-06-30 mark }
7724 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7725 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7726 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7727 9b058f45 2022-06-30 mark header = 5;
7728 9b058f45 2022-06-30 mark selected = &s->selected;
7729 9b058f45 2022-06-30 mark break;
7730 9b058f45 2022-06-30 mark }
7731 9b058f45 2022-06-30 mark default:
7732 9b058f45 2022-06-30 mark selected = NULL;
7733 9b058f45 2022-06-30 mark scrolld = NULL;
7734 9b058f45 2022-06-30 mark header = 0;
7735 9b058f45 2022-06-30 mark break;
7736 9b058f45 2022-06-30 mark }
7737 9b058f45 2022-06-30 mark
7738 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7739 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7740 9b058f45 2022-06-30 mark view->offset = offset;
7741 9b058f45 2022-06-30 mark if (scrolld && offset) {
7742 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7743 9b058f45 2022-06-30 mark *selected -= offset;
7744 9b058f45 2022-06-30 mark }
7745 9b058f45 2022-06-30 mark }
7746 9b058f45 2022-06-30 mark
7747 9b058f45 2022-06-30 mark return err;
7748 9b058f45 2022-06-30 mark }
7749 9b058f45 2022-06-30 mark
7750 9b058f45 2022-06-30 mark static void
7751 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7752 ce5b7c56 2019-07-09 stsp {
7753 6059809a 2020-12-17 stsp size_t i;
7754 9f7d7167 2018-04-29 stsp
7755 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7756 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7757 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7758 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7759 ce5b7c56 2019-07-09 stsp }
7760 6879ba42 2020-10-01 naddy fputc('\n', fp);
7761 ce5b7c56 2019-07-09 stsp }
7762 ce5b7c56 2019-07-09 stsp
7763 4ed7e80c 2018-05-20 stsp __dead static void
7764 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7765 9f7d7167 2018-04-29 stsp {
7766 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7767 6879ba42 2020-10-01 naddy
7768 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7769 6879ba42 2020-10-01 naddy getprogname());
7770 6879ba42 2020-10-01 naddy if (hflag) {
7771 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7772 6879ba42 2020-10-01 naddy list_commands(fp);
7773 ee85c5e8 2020-02-29 stsp }
7774 6879ba42 2020-10-01 naddy exit(status);
7775 9f7d7167 2018-04-29 stsp }
7776 9f7d7167 2018-04-29 stsp
7777 c2301be8 2018-04-30 stsp static char **
7778 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7779 c2301be8 2018-04-30 stsp {
7780 ee85c5e8 2020-02-29 stsp va_list ap;
7781 c2301be8 2018-04-30 stsp char **argv;
7782 ee85c5e8 2020-02-29 stsp int i;
7783 c2301be8 2018-04-30 stsp
7784 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7785 ee85c5e8 2020-02-29 stsp
7786 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7787 c2301be8 2018-04-30 stsp if (argv == NULL)
7788 c2301be8 2018-04-30 stsp err(1, "calloc");
7789 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7790 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7791 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7792 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7793 c2301be8 2018-04-30 stsp }
7794 c2301be8 2018-04-30 stsp
7795 ee85c5e8 2020-02-29 stsp va_end(ap);
7796 c2301be8 2018-04-30 stsp return argv;
7797 ee85c5e8 2020-02-29 stsp }
7798 ee85c5e8 2020-02-29 stsp
7799 ee85c5e8 2020-02-29 stsp /*
7800 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7801 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7802 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7803 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7804 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7805 ee85c5e8 2020-02-29 stsp */
7806 ee85c5e8 2020-02-29 stsp static const struct got_error *
7807 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7808 ee85c5e8 2020-02-29 stsp {
7809 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7810 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7811 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7812 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7813 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7814 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7815 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7816 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7817 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7818 84de9106 2020-12-26 stsp
7819 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7820 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7821 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7822 ee85c5e8 2020-02-29 stsp
7823 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7824 0ae84acc 2022-06-15 tracey if (error != NULL)
7825 0ae84acc 2022-06-15 tracey goto done;
7826 0ae84acc 2022-06-15 tracey
7827 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7828 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7829 ee85c5e8 2020-02-29 stsp goto done;
7830 ee85c5e8 2020-02-29 stsp
7831 ee85c5e8 2020-02-29 stsp if (worktree)
7832 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7833 ee85c5e8 2020-02-29 stsp else
7834 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7835 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7836 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7837 ee85c5e8 2020-02-29 stsp goto done;
7838 ee85c5e8 2020-02-29 stsp }
7839 ee85c5e8 2020-02-29 stsp
7840 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7841 ee85c5e8 2020-02-29 stsp if (error != NULL)
7842 ee85c5e8 2020-02-29 stsp goto done;
7843 ee85c5e8 2020-02-29 stsp
7844 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7845 ee85c5e8 2020-02-29 stsp repo, worktree);
7846 ee85c5e8 2020-02-29 stsp if (error)
7847 ee85c5e8 2020-02-29 stsp goto done;
7848 ee85c5e8 2020-02-29 stsp
7849 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7850 84de9106 2020-12-26 stsp if (error)
7851 84de9106 2020-12-26 stsp goto done;
7852 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7853 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7854 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7855 ee85c5e8 2020-02-29 stsp if (error)
7856 ee85c5e8 2020-02-29 stsp goto done;
7857 ee85c5e8 2020-02-29 stsp
7858 ee85c5e8 2020-02-29 stsp if (worktree) {
7859 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7860 ee85c5e8 2020-02-29 stsp worktree = NULL;
7861 ee85c5e8 2020-02-29 stsp }
7862 ee85c5e8 2020-02-29 stsp
7863 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7864 a44927cc 2022-04-07 stsp if (error)
7865 a44927cc 2022-04-07 stsp goto done;
7866 a44927cc 2022-04-07 stsp
7867 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7868 ee85c5e8 2020-02-29 stsp if (error) {
7869 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7870 ee85c5e8 2020-02-29 stsp goto done;
7871 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7872 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7873 6879ba42 2020-10-01 naddy usage(1, 1);
7874 ee85c5e8 2020-02-29 stsp /* not reached */
7875 ee85c5e8 2020-02-29 stsp }
7876 ee85c5e8 2020-02-29 stsp
7877 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7878 1d0f4054 2021-06-17 stsp if (error == NULL)
7879 1d0f4054 2021-06-17 stsp error = close_err;
7880 ee85c5e8 2020-02-29 stsp repo = NULL;
7881 ee85c5e8 2020-02-29 stsp
7882 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7883 ee85c5e8 2020-02-29 stsp if (error)
7884 ee85c5e8 2020-02-29 stsp goto done;
7885 ee85c5e8 2020-02-29 stsp
7886 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7887 ee85c5e8 2020-02-29 stsp argc = 4;
7888 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7889 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7890 ee85c5e8 2020-02-29 stsp done:
7891 1d0f4054 2021-06-17 stsp if (repo) {
7892 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7893 1d0f4054 2021-06-17 stsp if (error == NULL)
7894 1d0f4054 2021-06-17 stsp error = close_err;
7895 1d0f4054 2021-06-17 stsp }
7896 a44927cc 2022-04-07 stsp if (commit)
7897 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7898 ee85c5e8 2020-02-29 stsp if (worktree)
7899 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7900 0ae84acc 2022-06-15 tracey if (pack_fds) {
7901 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7902 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7903 0ae84acc 2022-06-15 tracey if (error == NULL)
7904 0ae84acc 2022-06-15 tracey error = pack_err;
7905 0ae84acc 2022-06-15 tracey }
7906 ee85c5e8 2020-02-29 stsp free(id);
7907 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7908 ee85c5e8 2020-02-29 stsp free(commit_id);
7909 ee85c5e8 2020-02-29 stsp free(cwd);
7910 ee85c5e8 2020-02-29 stsp free(repo_path);
7911 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7912 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7913 ee85c5e8 2020-02-29 stsp int i;
7914 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7915 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7916 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7917 ee85c5e8 2020-02-29 stsp }
7918 87670572 2020-12-26 naddy tog_free_refs();
7919 ee85c5e8 2020-02-29 stsp return error;
7920 c2301be8 2018-04-30 stsp }
7921 c2301be8 2018-04-30 stsp
7922 9f7d7167 2018-04-29 stsp int
7923 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7924 9f7d7167 2018-04-29 stsp {
7925 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7926 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7927 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7928 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7929 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7930 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7931 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7932 83cd27f8 2020-01-13 stsp };
7933 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
7934 9f7d7167 2018-04-29 stsp
7935 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7936 9f7d7167 2018-04-29 stsp
7937 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7938 9f7d7167 2018-04-29 stsp switch (ch) {
7939 9f7d7167 2018-04-29 stsp case 'h':
7940 9f7d7167 2018-04-29 stsp hflag = 1;
7941 9f7d7167 2018-04-29 stsp break;
7942 53ccebc2 2019-07-30 stsp case 'V':
7943 53ccebc2 2019-07-30 stsp Vflag = 1;
7944 53ccebc2 2019-07-30 stsp break;
7945 9f7d7167 2018-04-29 stsp default:
7946 6879ba42 2020-10-01 naddy usage(hflag, 1);
7947 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7948 9f7d7167 2018-04-29 stsp }
7949 9f7d7167 2018-04-29 stsp }
7950 9f7d7167 2018-04-29 stsp
7951 9f7d7167 2018-04-29 stsp argc -= optind;
7952 9f7d7167 2018-04-29 stsp argv += optind;
7953 9814e6a3 2020-09-27 naddy optind = 1;
7954 c2301be8 2018-04-30 stsp optreset = 1;
7955 9f7d7167 2018-04-29 stsp
7956 53ccebc2 2019-07-30 stsp if (Vflag) {
7957 53ccebc2 2019-07-30 stsp got_version_print_str();
7958 6879ba42 2020-10-01 naddy return 0;
7959 53ccebc2 2019-07-30 stsp }
7960 4010e238 2020-12-04 stsp
7961 4010e238 2020-12-04 stsp #ifndef PROFILE
7962 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7963 4010e238 2020-12-04 stsp NULL) == -1)
7964 4010e238 2020-12-04 stsp err(1, "pledge");
7965 4010e238 2020-12-04 stsp #endif
7966 53ccebc2 2019-07-30 stsp
7967 c2301be8 2018-04-30 stsp if (argc == 0) {
7968 f29d3e89 2018-06-23 stsp if (hflag)
7969 6879ba42 2020-10-01 naddy usage(hflag, 0);
7970 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7971 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7972 c2301be8 2018-04-30 stsp argc = 1;
7973 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7974 c2301be8 2018-04-30 stsp } else {
7975 6059809a 2020-12-17 stsp size_t i;
7976 9f7d7167 2018-04-29 stsp
7977 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7978 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7979 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7980 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7981 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7982 9f7d7167 2018-04-29 stsp break;
7983 9f7d7167 2018-04-29 stsp }
7984 9f7d7167 2018-04-29 stsp }
7985 ee85c5e8 2020-02-29 stsp }
7986 3642c4c6 2019-07-09 stsp
7987 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7988 917d79a7 2022-07-01 stsp if (diff_algo_str) {
7989 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
7990 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7991 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
7992 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7993 917d79a7 2022-07-01 stsp }
7994 917d79a7 2022-07-01 stsp
7995 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7996 ee85c5e8 2020-02-29 stsp if (argc != 1)
7997 6879ba42 2020-10-01 naddy usage(0, 1);
7998 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7999 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8000 ee85c5e8 2020-02-29 stsp } else {
8001 ee85c5e8 2020-02-29 stsp if (hflag)
8002 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8003 ee85c5e8 2020-02-29 stsp else
8004 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8005 9f7d7167 2018-04-29 stsp }
8006 9f7d7167 2018-04-29 stsp
8007 9f7d7167 2018-04-29 stsp endwin();
8008 b46c1e04 2020-09-20 naddy putchar('\n');
8009 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8010 a2f4a359 2020-02-28 stsp int i;
8011 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8012 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8013 a2f4a359 2020-02-28 stsp free(cmd_argv);
8014 a2f4a359 2020-02-28 stsp }
8015 a2f4a359 2020-02-28 stsp
8016 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8017 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8018 9f7d7167 2018-04-29 stsp return 0;
8019 9f7d7167 2018-04-29 stsp }