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 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 d6b05b5b 2018-08-04 stsp
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
114 1a76625f 2018-10-22 stsp int idx;
115 ba4f502b 2018-08-04 stsp };
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
120 6d17833f 2019-11-08 stsp };
121 6d17833f 2019-11-08 stsp
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
126 15a087fe 2019-02-21 stsp };
127 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
128 11b20872 2019-11-08 stsp
129 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
131 cc488aa7 2022-01-23 stsp
132 cc488aa7 2022-01-23 stsp static const struct got_error *
133 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 cc488aa7 2022-01-23 stsp struct got_reference* re2)
135 cc488aa7 2022-01-23 stsp {
136 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
137 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
138 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
139 cc488aa7 2022-01-23 stsp
140 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
141 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
144 cc488aa7 2022-01-23 stsp *cmp = -1;
145 cc488aa7 2022-01-23 stsp return NULL;
146 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
147 cc488aa7 2022-01-23 stsp *cmp = 1;
148 cc488aa7 2022-01-23 stsp return NULL;
149 cc488aa7 2022-01-23 stsp }
150 cc488aa7 2022-01-23 stsp
151 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 cc488aa7 2022-01-23 stsp return NULL;
153 cc488aa7 2022-01-23 stsp }
154 51a10b52 2020-12-26 stsp
155 11b20872 2019-11-08 stsp static const struct got_error *
156 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
157 51a10b52 2020-12-26 stsp {
158 51a10b52 2020-12-26 stsp const struct got_error *err;
159 51a10b52 2020-12-26 stsp
160 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 7f66531d 2021-11-16 stsp repo);
163 51a10b52 2020-12-26 stsp if (err)
164 51a10b52 2020-12-26 stsp return err;
165 51a10b52 2020-12-26 stsp
166 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 51a10b52 2020-12-26 stsp repo);
168 51a10b52 2020-12-26 stsp }
169 51a10b52 2020-12-26 stsp
170 51a10b52 2020-12-26 stsp static void
171 51a10b52 2020-12-26 stsp tog_free_refs(void)
172 51a10b52 2020-12-26 stsp {
173 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
174 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
175 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
176 51a10b52 2020-12-26 stsp }
177 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
178 51a10b52 2020-12-26 stsp }
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp static const struct got_error *
181 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
182 11b20872 2019-11-08 stsp int idx, short color)
183 11b20872 2019-11-08 stsp {
184 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
185 11b20872 2019-11-08 stsp struct tog_color *tc;
186 11b20872 2019-11-08 stsp int regerr = 0;
187 11b20872 2019-11-08 stsp
188 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
189 11b20872 2019-11-08 stsp return NULL;
190 11b20872 2019-11-08 stsp
191 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
192 11b20872 2019-11-08 stsp
193 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
194 11b20872 2019-11-08 stsp if (tc == NULL)
195 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
196 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
197 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 11b20872 2019-11-08 stsp if (regerr) {
199 11b20872 2019-11-08 stsp static char regerr_msg[512];
200 11b20872 2019-11-08 stsp static char err_msg[512];
201 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
202 11b20872 2019-11-08 stsp sizeof(regerr_msg));
203 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 11b20872 2019-11-08 stsp regerr_msg);
205 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 11b20872 2019-11-08 stsp free(tc);
207 11b20872 2019-11-08 stsp return err;
208 11b20872 2019-11-08 stsp }
209 11b20872 2019-11-08 stsp tc->colorpair = idx;
210 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
211 11b20872 2019-11-08 stsp return NULL;
212 11b20872 2019-11-08 stsp }
213 11b20872 2019-11-08 stsp
214 11b20872 2019-11-08 stsp static void
215 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
216 11b20872 2019-11-08 stsp {
217 11b20872 2019-11-08 stsp struct tog_color *tc;
218 11b20872 2019-11-08 stsp
219 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
220 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
221 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
222 11b20872 2019-11-08 stsp regfree(&tc->regex);
223 11b20872 2019-11-08 stsp free(tc);
224 11b20872 2019-11-08 stsp }
225 11b20872 2019-11-08 stsp }
226 11b20872 2019-11-08 stsp
227 11b20872 2019-11-08 stsp struct tog_color *
228 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
229 11b20872 2019-11-08 stsp {
230 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
231 11b20872 2019-11-08 stsp
232 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
233 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
234 11b20872 2019-11-08 stsp return tc;
235 11b20872 2019-11-08 stsp }
236 11b20872 2019-11-08 stsp
237 11b20872 2019-11-08 stsp return NULL;
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp
240 11b20872 2019-11-08 stsp static int
241 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
242 11b20872 2019-11-08 stsp {
243 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
245 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 11b20872 2019-11-08 stsp return COLOR_CYAN;
247 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 11b20872 2019-11-08 stsp return COLOR_YELLOW;
249 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 11b20872 2019-11-08 stsp return COLOR_GREEN;
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 91b8c405 2020-01-25 stsp return COLOR_CYAN;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 11b20872 2019-11-08 stsp return COLOR_GREEN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 11b20872 2019-11-08 stsp return COLOR_CYAN;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 6458efa5 2020-11-24 stsp return COLOR_GREEN;
267 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
269 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
271 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
273 11b20872 2019-11-08 stsp
274 11b20872 2019-11-08 stsp return -1;
275 11b20872 2019-11-08 stsp }
276 11b20872 2019-11-08 stsp
277 11b20872 2019-11-08 stsp static int
278 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
279 11b20872 2019-11-08 stsp {
280 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
281 11b20872 2019-11-08 stsp
282 11b20872 2019-11-08 stsp if (val == NULL)
283 11b20872 2019-11-08 stsp return default_color_value(envvar);
284 15a087fe 2019-02-21 stsp
285 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
286 11b20872 2019-11-08 stsp return COLOR_BLACK;
287 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
288 11b20872 2019-11-08 stsp return COLOR_RED;
289 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
290 11b20872 2019-11-08 stsp return COLOR_GREEN;
291 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
292 11b20872 2019-11-08 stsp return COLOR_YELLOW;
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLUE;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
296 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
298 11b20872 2019-11-08 stsp return COLOR_CYAN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
300 11b20872 2019-11-08 stsp return COLOR_WHITE;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
302 11b20872 2019-11-08 stsp return -1;
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp return default_color_value(envvar);
305 11b20872 2019-11-08 stsp }
306 11b20872 2019-11-08 stsp
307 11b20872 2019-11-08 stsp
308 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
309 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
310 3dbaef42 2020-11-24 stsp const char *label1, *label2;
311 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
312 15a087fe 2019-02-21 stsp int first_displayed_line;
313 15a087fe 2019-02-21 stsp int last_displayed_line;
314 15a087fe 2019-02-21 stsp int eof;
315 15a087fe 2019-02-21 stsp int diff_context;
316 3dbaef42 2020-11-24 stsp int ignore_whitespace;
317 64453f7e 2020-11-21 stsp int force_text_diff;
318 15a087fe 2019-02-21 stsp struct got_repository *repo;
319 bddb1296 2019-11-08 stsp struct tog_colors colors;
320 fe621944 2020-11-10 stsp size_t nlines;
321 f44b1f58 2020-02-02 tracey off_t *line_offsets;
322 f44b1f58 2020-02-02 tracey int matched_line;
323 f44b1f58 2020-02-02 tracey int selected_line;
324 15a087fe 2019-02-21 stsp
325 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
326 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
327 b01e7d3b 2018-08-04 stsp };
328 b01e7d3b 2018-08-04 stsp
329 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
330 1a76625f 2018-10-22 stsp
331 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
332 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
333 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
334 1a76625f 2018-10-22 stsp int commits_needed;
335 fb280deb 2021-08-30 stsp int load_all;
336 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
337 1a76625f 2018-10-22 stsp struct commit_queue *commits;
338 1a76625f 2018-10-22 stsp const char *in_repo_path;
339 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
340 1a76625f 2018-10-22 stsp struct got_repository *repo;
341 74467cc8 2022-06-15 stsp int *pack_fds;
342 1a76625f 2018-10-22 stsp int log_complete;
343 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
344 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
345 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
346 13add988 2019-10-15 stsp int *searching;
347 13add988 2019-10-15 stsp int *search_next_done;
348 13add988 2019-10-15 stsp regex_t *regex;
349 1a76625f 2018-10-22 stsp };
350 1a76625f 2018-10-22 stsp
351 1a76625f 2018-10-22 stsp struct tog_log_view_state {
352 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
353 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
354 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
355 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
356 b01e7d3b 2018-08-04 stsp int selected;
357 b01e7d3b 2018-08-04 stsp char *in_repo_path;
358 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
359 b672a97a 2020-01-27 stsp int log_branches;
360 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
361 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
362 1a76625f 2018-10-22 stsp sig_atomic_t quit;
363 1a76625f 2018-10-22 stsp pthread_t thread;
364 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
365 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
366 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
367 11b20872 2019-11-08 stsp struct tog_colors colors;
368 ba4f502b 2018-08-04 stsp };
369 11b20872 2019-11-08 stsp
370 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
371 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
372 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
374 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
375 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
376 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
377 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
378 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
379 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
380 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
381 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
382 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
383 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
384 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
385 ba4f502b 2018-08-04 stsp
386 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
387 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
388 e9424729 2018-08-04 stsp int nlines;
389 e9424729 2018-08-04 stsp
390 e9424729 2018-08-04 stsp struct tog_view *view;
391 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
392 e9424729 2018-08-04 stsp int *quit;
393 e9424729 2018-08-04 stsp };
394 e9424729 2018-08-04 stsp
395 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
396 e9424729 2018-08-04 stsp const char *path;
397 e9424729 2018-08-04 stsp struct got_repository *repo;
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
399 e9424729 2018-08-04 stsp int *complete;
400 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
401 fc06ba56 2019-08-22 stsp void *cancel_arg;
402 e9424729 2018-08-04 stsp };
403 e9424729 2018-08-04 stsp
404 e9424729 2018-08-04 stsp struct tog_blame {
405 e9424729 2018-08-04 stsp FILE *f;
406 be659d10 2020-11-18 stsp off_t filesize;
407 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
408 6fcac457 2018-11-19 stsp int nlines;
409 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
410 e9424729 2018-08-04 stsp pthread_t thread;
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
412 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
413 e9424729 2018-08-04 stsp const char *path;
414 0ae84acc 2022-06-15 tracey int *pack_fds;
415 e9424729 2018-08-04 stsp };
416 e9424729 2018-08-04 stsp
417 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
418 7cbe629d 2018-08-04 stsp int first_displayed_line;
419 7cbe629d 2018-08-04 stsp int last_displayed_line;
420 7cbe629d 2018-08-04 stsp int selected_line;
421 7cbe629d 2018-08-04 stsp int blame_complete;
422 e5a0f69f 2018-08-18 stsp int eof;
423 e5a0f69f 2018-08-18 stsp int done;
424 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
425 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
426 e5a0f69f 2018-08-18 stsp char *path;
427 7cbe629d 2018-08-04 stsp struct got_repository *repo;
428 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp struct tog_blame blame;
430 6c4c42e0 2019-06-24 stsp int matched_line;
431 11b20872 2019-11-08 stsp struct tog_colors colors;
432 ad80ab7b 2018-08-04 stsp };
433 ad80ab7b 2018-08-04 stsp
434 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
435 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
436 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
437 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
438 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
439 ad80ab7b 2018-08-04 stsp int selected;
440 ad80ab7b 2018-08-04 stsp };
441 ad80ab7b 2018-08-04 stsp
442 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
443 ad80ab7b 2018-08-04 stsp
444 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
445 ad80ab7b 2018-08-04 stsp char *tree_label;
446 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
447 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
448 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
453 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
455 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
456 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
457 6458efa5 2020-11-24 stsp struct tog_colors colors;
458 6458efa5 2020-11-24 stsp };
459 6458efa5 2020-11-24 stsp
460 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
461 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
462 6458efa5 2020-11-24 stsp struct got_reference *ref;
463 6458efa5 2020-11-24 stsp int idx;
464 6458efa5 2020-11-24 stsp };
465 6458efa5 2020-11-24 stsp
466 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
467 6458efa5 2020-11-24 stsp
468 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
469 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
471 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
472 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
473 7f66531d 2021-11-16 stsp int nrefs, ndisplayed, selected, show_ids, sort_by_date;
474 6458efa5 2020-11-24 stsp struct got_repository *repo;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
476 bddb1296 2019-11-08 stsp struct tog_colors colors;
477 7cbe629d 2018-08-04 stsp };
478 7cbe629d 2018-08-04 stsp
479 669b5ffa 2018-10-07 stsp /*
480 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
481 669b5ffa 2018-10-07 stsp *
482 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
483 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
484 669b5ffa 2018-10-07 stsp * there is enough screen estate.
485 669b5ffa 2018-10-07 stsp *
486 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
487 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
488 669b5ffa 2018-10-07 stsp *
489 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
490 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
491 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
492 669b5ffa 2018-10-07 stsp *
493 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
494 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
495 669b5ffa 2018-10-07 stsp */
496 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
497 669b5ffa 2018-10-07 stsp
498 cc3c9aac 2018-08-01 stsp struct tog_view {
499 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
500 26ed57b2 2018-05-19 stsp WINDOW *window;
501 26ed57b2 2018-05-19 stsp PANEL *panel;
502 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
503 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
504 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
505 9970f7fc 2020-12-03 stsp int dying;
506 669b5ffa 2018-10-07 stsp struct tog_view *parent;
507 669b5ffa 2018-10-07 stsp struct tog_view *child;
508 5dc9f4bc 2018-08-04 stsp
509 e78dc838 2020-12-04 stsp /*
510 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
511 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
512 e78dc838 2020-12-04 stsp * between parent and child.
513 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
514 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
515 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
516 e78dc838 2020-12-04 stsp * situations.
517 e78dc838 2020-12-04 stsp */
518 e78dc838 2020-12-04 stsp int focus_child;
519 e78dc838 2020-12-04 stsp
520 5dc9f4bc 2018-08-04 stsp /* type-specific state */
521 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
522 5dc9f4bc 2018-08-04 stsp union {
523 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
524 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
525 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
526 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
527 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
528 5dc9f4bc 2018-08-04 stsp } state;
529 e5a0f69f 2018-08-18 stsp
530 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
531 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
532 e78dc838 2020-12-04 stsp struct tog_view *, int);
533 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
534 60493ae3 2019-06-20 stsp
535 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
536 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
537 c0c4acc8 2021-01-24 stsp int search_started;
538 60493ae3 2019-06-20 stsp int searching;
539 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
540 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
541 60493ae3 2019-06-20 stsp int search_next_done;
542 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
543 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
544 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
545 1803e47f 2019-06-22 stsp regex_t regex;
546 41605754 2020-11-12 stsp regmatch_t regmatch;
547 cc3c9aac 2018-08-01 stsp };
548 cd0acaa7 2018-05-20 stsp
549 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
550 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
551 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
552 78756c87 2020-11-24 stsp struct got_repository *);
553 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
554 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
555 e78dc838 2020-12-04 stsp struct tog_view *, int);
556 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
557 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
558 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp
560 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
561 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
562 78756c87 2020-11-24 stsp const char *, const char *, int);
563 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
565 e78dc838 2020-12-04 stsp struct tog_view *, int);
566 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
567 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
568 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp
570 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
571 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
572 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
574 e78dc838 2020-12-04 stsp struct tog_view *, int);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
576 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
577 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp
579 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
580 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
581 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
583 e78dc838 2020-12-04 stsp struct tog_view *, int);
584 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
585 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
586 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
587 6458efa5 2020-11-24 stsp
588 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
589 6458efa5 2020-11-24 stsp struct got_repository *);
590 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
591 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
594 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
595 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
596 25791caa 2018-10-24 stsp
597 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
598 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
599 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
600 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
601 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
602 25791caa 2018-10-24 stsp
603 25791caa 2018-10-24 stsp static void
604 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
605 25791caa 2018-10-24 stsp {
606 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
607 83baff54 2019-08-12 stsp }
608 83baff54 2019-08-12 stsp
609 83baff54 2019-08-12 stsp static void
610 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
611 83baff54 2019-08-12 stsp {
612 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
613 25791caa 2018-10-24 stsp }
614 26ed57b2 2018-05-19 stsp
615 61266923 2020-01-14 stsp static void
616 61266923 2020-01-14 stsp tog_sigcont(int signo)
617 61266923 2020-01-14 stsp {
618 61266923 2020-01-14 stsp tog_sigcont_received = 1;
619 61266923 2020-01-14 stsp }
620 61266923 2020-01-14 stsp
621 2497f032 2022-05-31 stsp static void
622 2497f032 2022-05-31 stsp tog_sigint(int signo)
623 2497f032 2022-05-31 stsp {
624 2497f032 2022-05-31 stsp tog_sigint_received = 1;
625 2497f032 2022-05-31 stsp }
626 2497f032 2022-05-31 stsp
627 2497f032 2022-05-31 stsp static void
628 2497f032 2022-05-31 stsp tog_sigterm(int signo)
629 2497f032 2022-05-31 stsp {
630 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
631 2497f032 2022-05-31 stsp }
632 2497f032 2022-05-31 stsp
633 2497f032 2022-05-31 stsp static int
634 2497f032 2022-05-31 stsp tog_fatal_signal_received()
635 2497f032 2022-05-31 stsp {
636 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
637 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
638 2497f032 2022-05-31 stsp }
639 2497f032 2022-05-31 stsp
640 2497f032 2022-05-31 stsp
641 e5a0f69f 2018-08-18 stsp static const struct got_error *
642 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
643 ea5e7bb5 2018-08-01 stsp {
644 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
645 e5a0f69f 2018-08-18 stsp
646 669b5ffa 2018-10-07 stsp if (view->child) {
647 669b5ffa 2018-10-07 stsp view_close(view->child);
648 669b5ffa 2018-10-07 stsp view->child = NULL;
649 669b5ffa 2018-10-07 stsp }
650 e5a0f69f 2018-08-18 stsp if (view->close)
651 e5a0f69f 2018-08-18 stsp err = view->close(view);
652 ea5e7bb5 2018-08-01 stsp if (view->panel)
653 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
654 ea5e7bb5 2018-08-01 stsp if (view->window)
655 ea5e7bb5 2018-08-01 stsp delwin(view->window);
656 ea5e7bb5 2018-08-01 stsp free(view);
657 e5a0f69f 2018-08-18 stsp return err;
658 ea5e7bb5 2018-08-01 stsp }
659 ea5e7bb5 2018-08-01 stsp
660 ea5e7bb5 2018-08-01 stsp static struct tog_view *
661 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
662 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
663 ea5e7bb5 2018-08-01 stsp {
664 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
665 ea5e7bb5 2018-08-01 stsp
666 ea5e7bb5 2018-08-01 stsp if (view == NULL)
667 ea5e7bb5 2018-08-01 stsp return NULL;
668 ea5e7bb5 2018-08-01 stsp
669 d6b05b5b 2018-08-04 stsp view->type = type;
670 f7d12f7e 2018-08-01 stsp view->lines = LINES;
671 f7d12f7e 2018-08-01 stsp view->cols = COLS;
672 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
673 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
674 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
675 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
676 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
677 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
678 96a765a8 2018-08-04 stsp view_close(view);
679 ea5e7bb5 2018-08-01 stsp return NULL;
680 ea5e7bb5 2018-08-01 stsp }
681 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
682 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
683 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
684 96a765a8 2018-08-04 stsp view_close(view);
685 ea5e7bb5 2018-08-01 stsp return NULL;
686 ea5e7bb5 2018-08-01 stsp }
687 ea5e7bb5 2018-08-01 stsp
688 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
689 ea5e7bb5 2018-08-01 stsp return view;
690 cdf1ee82 2018-08-01 stsp }
691 cdf1ee82 2018-08-01 stsp
692 0cf4efb1 2018-09-29 stsp static int
693 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
694 0cf4efb1 2018-09-29 stsp {
695 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
696 0cf4efb1 2018-09-29 stsp return 0;
697 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
698 5c60c32a 2018-10-18 stsp }
699 5c60c32a 2018-10-18 stsp
700 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
701 5c60c32a 2018-10-18 stsp
702 5c60c32a 2018-10-18 stsp static const struct got_error *
703 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
704 5c60c32a 2018-10-18 stsp {
705 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
706 5c60c32a 2018-10-18 stsp
707 5c60c32a 2018-10-18 stsp view->begin_y = 0;
708 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
709 5c60c32a 2018-10-18 stsp view->nlines = LINES;
710 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
711 5c60c32a 2018-10-18 stsp view->lines = LINES;
712 5c60c32a 2018-10-18 stsp view->cols = COLS;
713 5c60c32a 2018-10-18 stsp err = view_resize(view);
714 5c60c32a 2018-10-18 stsp if (err)
715 5c60c32a 2018-10-18 stsp return err;
716 5c60c32a 2018-10-18 stsp
717 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
718 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
719 5c60c32a 2018-10-18 stsp
720 5c60c32a 2018-10-18 stsp return NULL;
721 5c60c32a 2018-10-18 stsp }
722 5c60c32a 2018-10-18 stsp
723 5c60c32a 2018-10-18 stsp static const struct got_error *
724 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
725 5c60c32a 2018-10-18 stsp {
726 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
727 5c60c32a 2018-10-18 stsp
728 5c60c32a 2018-10-18 stsp view->begin_x = 0;
729 5c60c32a 2018-10-18 stsp view->begin_y = 0;
730 5c60c32a 2018-10-18 stsp view->nlines = LINES;
731 5c60c32a 2018-10-18 stsp view->ncols = COLS;
732 5c60c32a 2018-10-18 stsp view->lines = LINES;
733 5c60c32a 2018-10-18 stsp view->cols = COLS;
734 5c60c32a 2018-10-18 stsp err = view_resize(view);
735 5c60c32a 2018-10-18 stsp if (err)
736 5c60c32a 2018-10-18 stsp return err;
737 5c60c32a 2018-10-18 stsp
738 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
739 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
740 5c60c32a 2018-10-18 stsp
741 5c60c32a 2018-10-18 stsp return NULL;
742 0cf4efb1 2018-09-29 stsp }
743 0cf4efb1 2018-09-29 stsp
744 5c60c32a 2018-10-18 stsp static int
745 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
746 5c60c32a 2018-10-18 stsp {
747 5c60c32a 2018-10-18 stsp return view->parent == NULL;
748 5c60c32a 2018-10-18 stsp }
749 5c60c32a 2018-10-18 stsp
750 4d8c2215 2018-08-19 stsp static const struct got_error *
751 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
752 f7d12f7e 2018-08-01 stsp {
753 f7d12f7e 2018-08-01 stsp int nlines, ncols;
754 f7d12f7e 2018-08-01 stsp
755 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
756 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
757 0cf4efb1 2018-09-29 stsp else
758 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
759 f7d12f7e 2018-08-01 stsp
760 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
761 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
762 0cf4efb1 2018-09-29 stsp else
763 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
764 f7d12f7e 2018-08-01 stsp
765 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
766 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
767 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
769 25791caa 2018-10-24 stsp wclear(view->window);
770 f7d12f7e 2018-08-01 stsp
771 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
772 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
773 0cf4efb1 2018-09-29 stsp view->lines = LINES;
774 0cf4efb1 2018-09-29 stsp view->cols = COLS;
775 6d0fee91 2018-08-01 stsp
776 6e3e5d9c 2018-10-18 stsp if (view->child) {
777 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
778 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
779 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
780 5c60c32a 2018-10-18 stsp if (view->child->focussed)
781 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
782 5c60c32a 2018-10-18 stsp else
783 5c60c32a 2018-10-18 stsp show_panel(view->panel);
784 5c60c32a 2018-10-18 stsp } else {
785 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
786 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
787 5c60c32a 2018-10-18 stsp }
788 5c60c32a 2018-10-18 stsp }
789 669b5ffa 2018-10-07 stsp
790 5c60c32a 2018-10-18 stsp return NULL;
791 669b5ffa 2018-10-07 stsp }
792 669b5ffa 2018-10-07 stsp
793 669b5ffa 2018-10-07 stsp static const struct got_error *
794 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
795 669b5ffa 2018-10-07 stsp {
796 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
797 669b5ffa 2018-10-07 stsp
798 669b5ffa 2018-10-07 stsp if (view->child == NULL)
799 669b5ffa 2018-10-07 stsp return NULL;
800 669b5ffa 2018-10-07 stsp
801 669b5ffa 2018-10-07 stsp err = view_close(view->child);
802 669b5ffa 2018-10-07 stsp view->child = NULL;
803 669b5ffa 2018-10-07 stsp return err;
804 669b5ffa 2018-10-07 stsp }
805 669b5ffa 2018-10-07 stsp
806 72a9cb46 2020-12-03 stsp static void
807 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
808 669b5ffa 2018-10-07 stsp {
809 669b5ffa 2018-10-07 stsp view->child = child;
810 669b5ffa 2018-10-07 stsp child->parent = view;
811 bfddd0d9 2018-09-29 stsp }
812 bfddd0d9 2018-09-29 stsp
813 bfddd0d9 2018-09-29 stsp static int
814 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
815 bfddd0d9 2018-09-29 stsp {
816 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
817 34bc9ec9 2019-02-22 stsp }
818 34bc9ec9 2019-02-22 stsp
819 34bc9ec9 2019-02-22 stsp static void
820 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
821 25791caa 2018-10-24 stsp {
822 25791caa 2018-10-24 stsp int cols, lines;
823 25791caa 2018-10-24 stsp struct winsize size;
824 25791caa 2018-10-24 stsp
825 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
826 25791caa 2018-10-24 stsp cols = 80; /* Default */
827 25791caa 2018-10-24 stsp lines = 24;
828 25791caa 2018-10-24 stsp } else {
829 25791caa 2018-10-24 stsp cols = size.ws_col;
830 25791caa 2018-10-24 stsp lines = size.ws_row;
831 25791caa 2018-10-24 stsp }
832 25791caa 2018-10-24 stsp resize_term(lines, cols);
833 2b49a8ae 2019-06-22 stsp }
834 2b49a8ae 2019-06-22 stsp
835 2b49a8ae 2019-06-22 stsp static const struct got_error *
836 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
837 2b49a8ae 2019-06-22 stsp {
838 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
839 2b49a8ae 2019-06-22 stsp char pattern[1024];
840 2b49a8ae 2019-06-22 stsp int ret;
841 c0c4acc8 2021-01-24 stsp
842 c0c4acc8 2021-01-24 stsp if (view->search_started) {
843 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
844 c0c4acc8 2021-01-24 stsp view->searching = 0;
845 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
846 c0c4acc8 2021-01-24 stsp }
847 c0c4acc8 2021-01-24 stsp view->search_started = 0;
848 2b49a8ae 2019-06-22 stsp
849 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
850 2b49a8ae 2019-06-22 stsp return NULL;
851 2b49a8ae 2019-06-22 stsp
852 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
853 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
854 2b49a8ae 2019-06-22 stsp
855 2b49a8ae 2019-06-22 stsp nocbreak();
856 2b49a8ae 2019-06-22 stsp echo();
857 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
858 2b49a8ae 2019-06-22 stsp cbreak();
859 2b49a8ae 2019-06-22 stsp noecho();
860 2b49a8ae 2019-06-22 stsp if (ret == ERR)
861 2b49a8ae 2019-06-22 stsp return NULL;
862 2b49a8ae 2019-06-22 stsp
863 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
864 7c32bd05 2019-06-22 stsp err = view->search_start(view);
865 7c32bd05 2019-06-22 stsp if (err) {
866 7c32bd05 2019-06-22 stsp regfree(&view->regex);
867 7c32bd05 2019-06-22 stsp return err;
868 7c32bd05 2019-06-22 stsp }
869 c0c4acc8 2021-01-24 stsp view->search_started = 1;
870 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
871 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
872 2b49a8ae 2019-06-22 stsp view->search_next(view);
873 2b49a8ae 2019-06-22 stsp }
874 2b49a8ae 2019-06-22 stsp
875 2b49a8ae 2019-06-22 stsp return NULL;
876 0cf4efb1 2018-09-29 stsp }
877 6d0fee91 2018-08-01 stsp
878 0cf4efb1 2018-09-29 stsp static const struct got_error *
879 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
880 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
881 e5a0f69f 2018-08-18 stsp {
882 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
883 669b5ffa 2018-10-07 stsp struct tog_view *v;
884 1a76625f 2018-10-22 stsp int ch, errcode;
885 e5a0f69f 2018-08-18 stsp
886 e5a0f69f 2018-08-18 stsp *new = NULL;
887 8f4ed634 2020-03-26 stsp
888 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
889 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
890 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
891 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
892 e5a0f69f 2018-08-18 stsp
893 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
894 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
895 82954512 2020-02-03 stsp if (errcode)
896 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
897 82954512 2020-02-03 stsp "pthread_mutex_unlock");
898 3da8ef85 2021-09-21 stsp sched_yield();
899 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
900 82954512 2020-02-03 stsp if (errcode)
901 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
902 82954512 2020-02-03 stsp "pthread_mutex_lock");
903 60493ae3 2019-06-20 stsp view->search_next(view);
904 60493ae3 2019-06-20 stsp return NULL;
905 60493ae3 2019-06-20 stsp }
906 60493ae3 2019-06-20 stsp
907 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
908 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
910 1a76625f 2018-10-22 stsp if (errcode)
911 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
912 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
913 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
914 1a76625f 2018-10-22 stsp if (errcode)
915 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
916 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
917 25791caa 2018-10-24 stsp
918 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
919 25791caa 2018-10-24 stsp tog_resizeterm();
920 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
921 61266923 2020-01-14 stsp tog_sigcont_received = 0;
922 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
923 25791caa 2018-10-24 stsp err = view_resize(v);
924 25791caa 2018-10-24 stsp if (err)
925 25791caa 2018-10-24 stsp return err;
926 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
927 25791caa 2018-10-24 stsp if (err)
928 25791caa 2018-10-24 stsp return err;
929 cdfcfb03 2020-12-06 stsp if (v->child) {
930 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
931 cdfcfb03 2020-12-06 stsp if (err)
932 cdfcfb03 2020-12-06 stsp return err;
933 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
934 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
935 cdfcfb03 2020-12-06 stsp if (err)
936 cdfcfb03 2020-12-06 stsp return err;
937 cdfcfb03 2020-12-06 stsp }
938 25791caa 2018-10-24 stsp }
939 25791caa 2018-10-24 stsp }
940 25791caa 2018-10-24 stsp
941 e5a0f69f 2018-08-18 stsp switch (ch) {
942 1e37a5c2 2019-05-12 jcs case '\t':
943 1e37a5c2 2019-05-12 jcs if (view->child) {
944 e78dc838 2020-12-04 stsp view->focussed = 0;
945 e78dc838 2020-12-04 stsp view->child->focussed = 1;
946 e78dc838 2020-12-04 stsp view->focus_child = 1;
947 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
948 e78dc838 2020-12-04 stsp view->focussed = 0;
949 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
950 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
951 1e37a5c2 2019-05-12 jcs }
952 1e37a5c2 2019-05-12 jcs break;
953 1e37a5c2 2019-05-12 jcs case 'q':
954 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
955 9970f7fc 2020-12-03 stsp view->dying = 1;
956 1e37a5c2 2019-05-12 jcs break;
957 1e37a5c2 2019-05-12 jcs case 'Q':
958 1e37a5c2 2019-05-12 jcs *done = 1;
959 1e37a5c2 2019-05-12 jcs break;
960 1e37a5c2 2019-05-12 jcs case 'f':
961 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
962 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
963 1e37a5c2 2019-05-12 jcs break;
964 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
965 e78dc838 2020-12-04 stsp view->focussed = 0;
966 e78dc838 2020-12-04 stsp view->child->focussed = 1;
967 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
968 1e37a5c2 2019-05-12 jcs } else
969 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
970 1e37a5c2 2019-05-12 jcs if (err)
971 1e37a5c2 2019-05-12 jcs break;
972 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
973 9970f7fc 2020-12-03 stsp KEY_RESIZE);
974 1e37a5c2 2019-05-12 jcs } else {
975 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
976 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
977 e78dc838 2020-12-04 stsp view->focussed = 1;
978 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
979 1e37a5c2 2019-05-12 jcs } else {
980 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
981 669b5ffa 2018-10-07 stsp }
982 1e37a5c2 2019-05-12 jcs if (err)
983 1e37a5c2 2019-05-12 jcs break;
984 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
985 1e37a5c2 2019-05-12 jcs }
986 1e37a5c2 2019-05-12 jcs break;
987 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
988 60493ae3 2019-06-20 stsp break;
989 60493ae3 2019-06-20 stsp case '/':
990 60493ae3 2019-06-20 stsp if (view->search_start)
991 2b49a8ae 2019-06-22 stsp view_search_start(view);
992 60493ae3 2019-06-20 stsp else
993 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
994 1e37a5c2 2019-05-12 jcs break;
995 b1bf1435 2019-06-21 stsp case 'N':
996 60493ae3 2019-06-20 stsp case 'n':
997 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
998 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
999 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1000 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1001 60493ae3 2019-06-20 stsp view->search_next(view);
1002 60493ae3 2019-06-20 stsp } else
1003 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1004 60493ae3 2019-06-20 stsp break;
1005 1e37a5c2 2019-05-12 jcs default:
1006 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1007 1e37a5c2 2019-05-12 jcs break;
1008 e5a0f69f 2018-08-18 stsp }
1009 e5a0f69f 2018-08-18 stsp
1010 e5a0f69f 2018-08-18 stsp return err;
1011 e5a0f69f 2018-08-18 stsp }
1012 e5a0f69f 2018-08-18 stsp
1013 1a57306a 2018-09-02 stsp void
1014 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1015 1a57306a 2018-09-02 stsp {
1016 0cf4efb1 2018-09-29 stsp PANEL *panel;
1017 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1018 0cf4efb1 2018-09-29 stsp
1019 669b5ffa 2018-10-07 stsp if (view->parent)
1020 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1021 669b5ffa 2018-10-07 stsp
1022 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1023 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1024 1a57306a 2018-09-02 stsp return;
1025 1a57306a 2018-09-02 stsp
1026 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1027 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1028 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1029 bcbd79e2 2018-08-19 stsp }
1030 bcbd79e2 2018-08-19 stsp
1031 a3404814 2018-09-02 stsp int
1032 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1033 a3404814 2018-09-02 stsp {
1034 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1035 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1036 669b5ffa 2018-10-07 stsp return 0;
1037 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1038 669b5ffa 2018-10-07 stsp return 0;
1039 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1040 a3404814 2018-09-02 stsp return 0;
1041 a3404814 2018-09-02 stsp
1042 669b5ffa 2018-10-07 stsp return view->focussed;
1043 a3404814 2018-09-02 stsp }
1044 a3404814 2018-09-02 stsp
1045 bcbd79e2 2018-08-19 stsp static const struct got_error *
1046 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1047 e5a0f69f 2018-08-18 stsp {
1048 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1049 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1050 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1051 fd823528 2018-10-22 stsp int fast_refresh = 10;
1052 1a76625f 2018-10-22 stsp int done = 0, errcode;
1053 e5a0f69f 2018-08-18 stsp
1054 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1055 1a76625f 2018-10-22 stsp if (errcode)
1056 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1057 1a76625f 2018-10-22 stsp
1058 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1059 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1060 e5a0f69f 2018-08-18 stsp
1061 1004088d 2018-09-29 stsp view->focussed = 1;
1062 878940b7 2018-09-29 stsp err = view->show(view);
1063 0cf4efb1 2018-09-29 stsp if (err)
1064 0cf4efb1 2018-09-29 stsp return err;
1065 0cf4efb1 2018-09-29 stsp update_panels();
1066 0cf4efb1 2018-09-29 stsp doupdate();
1067 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1068 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1069 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1070 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1071 fd823528 2018-10-22 stsp
1072 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1073 e5a0f69f 2018-08-18 stsp if (err)
1074 e5a0f69f 2018-08-18 stsp break;
1075 9970f7fc 2020-12-03 stsp if (view->dying) {
1076 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1077 669b5ffa 2018-10-07 stsp
1078 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1079 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1080 9970f7fc 2020-12-03 stsp entry);
1081 e78dc838 2020-12-04 stsp else if (view->parent)
1082 669b5ffa 2018-10-07 stsp prev = view->parent;
1083 669b5ffa 2018-10-07 stsp
1084 e78dc838 2020-12-04 stsp if (view->parent) {
1085 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1086 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1087 e78dc838 2020-12-04 stsp } else
1088 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1089 669b5ffa 2018-10-07 stsp
1090 9970f7fc 2020-12-03 stsp err = view_close(view);
1091 fb59748f 2020-12-05 stsp if (err)
1092 e5a0f69f 2018-08-18 stsp goto done;
1093 669b5ffa 2018-10-07 stsp
1094 e78dc838 2020-12-04 stsp view = NULL;
1095 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1096 e78dc838 2020-12-04 stsp if (v->focussed)
1097 e78dc838 2020-12-04 stsp break;
1098 0cf4efb1 2018-09-29 stsp }
1099 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1100 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1101 e78dc838 2020-12-04 stsp if (prev)
1102 e78dc838 2020-12-04 stsp view = prev;
1103 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1104 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1105 e78dc838 2020-12-04 stsp tog_view_list_head);
1106 e78dc838 2020-12-04 stsp }
1107 e78dc838 2020-12-04 stsp if (view) {
1108 e78dc838 2020-12-04 stsp if (view->focus_child) {
1109 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1110 e78dc838 2020-12-04 stsp view = view->child;
1111 e78dc838 2020-12-04 stsp } else
1112 e78dc838 2020-12-04 stsp view->focussed = 1;
1113 e78dc838 2020-12-04 stsp }
1114 e78dc838 2020-12-04 stsp }
1115 e5a0f69f 2018-08-18 stsp }
1116 bcbd79e2 2018-08-19 stsp if (new_view) {
1117 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1118 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1119 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1120 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1121 86c66b02 2018-10-18 stsp continue;
1122 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1123 86c66b02 2018-10-18 stsp err = view_close(v);
1124 86c66b02 2018-10-18 stsp if (err)
1125 86c66b02 2018-10-18 stsp goto done;
1126 86c66b02 2018-10-18 stsp break;
1127 86c66b02 2018-10-18 stsp }
1128 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1129 fed7eaa8 2018-10-24 stsp view = new_view;
1130 0ae84acc 2022-06-15 tracey }
1131 669b5ffa 2018-10-07 stsp if (view) {
1132 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1133 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1134 e78dc838 2020-12-04 stsp view = view->child;
1135 e78dc838 2020-12-04 stsp } else {
1136 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1137 e78dc838 2020-12-04 stsp view = view->parent;
1138 1a76625f 2018-10-22 stsp }
1139 e78dc838 2020-12-04 stsp show_panel(view->panel);
1140 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1141 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1142 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1143 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1144 669b5ffa 2018-10-07 stsp if (err)
1145 1a76625f 2018-10-22 stsp goto done;
1146 669b5ffa 2018-10-07 stsp }
1147 669b5ffa 2018-10-07 stsp err = view->show(view);
1148 0cf4efb1 2018-09-29 stsp if (err)
1149 1a76625f 2018-10-22 stsp goto done;
1150 669b5ffa 2018-10-07 stsp if (view->child) {
1151 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1152 669b5ffa 2018-10-07 stsp if (err)
1153 1a76625f 2018-10-22 stsp goto done;
1154 669b5ffa 2018-10-07 stsp }
1155 1a76625f 2018-10-22 stsp update_panels();
1156 1a76625f 2018-10-22 stsp doupdate();
1157 0cf4efb1 2018-09-29 stsp }
1158 e5a0f69f 2018-08-18 stsp }
1159 e5a0f69f 2018-08-18 stsp done:
1160 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1161 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1162 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1163 e5a0f69f 2018-08-18 stsp view_close(view);
1164 e5a0f69f 2018-08-18 stsp }
1165 1a76625f 2018-10-22 stsp
1166 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1167 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1168 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1169 1a76625f 2018-10-22 stsp
1170 e5a0f69f 2018-08-18 stsp return err;
1171 ea5e7bb5 2018-08-01 stsp }
1172 ea5e7bb5 2018-08-01 stsp
1173 4ed7e80c 2018-05-20 stsp __dead static void
1174 9f7d7167 2018-04-29 stsp usage_log(void)
1175 9f7d7167 2018-04-29 stsp {
1176 80ddbec8 2018-04-29 stsp endwin();
1177 c70c5802 2018-08-01 stsp fprintf(stderr,
1178 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1179 9f7d7167 2018-04-29 stsp getprogname());
1180 9f7d7167 2018-04-29 stsp exit(1);
1181 80ddbec8 2018-04-29 stsp }
1182 80ddbec8 2018-04-29 stsp
1183 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1184 80ddbec8 2018-04-29 stsp static const struct got_error *
1185 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1186 963b370f 2018-05-20 stsp {
1187 00dfcb92 2018-06-11 stsp char *vis = NULL;
1188 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1189 963b370f 2018-05-20 stsp
1190 963b370f 2018-05-20 stsp *ws = NULL;
1191 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1192 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1193 00dfcb92 2018-06-11 stsp int vislen;
1194 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1195 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1196 00dfcb92 2018-06-11 stsp
1197 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1198 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1199 00dfcb92 2018-06-11 stsp if (err)
1200 00dfcb92 2018-06-11 stsp return err;
1201 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1202 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1203 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1204 a7f50699 2018-06-11 stsp goto done;
1205 a7f50699 2018-06-11 stsp }
1206 00dfcb92 2018-06-11 stsp }
1207 963b370f 2018-05-20 stsp
1208 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1209 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1211 a7f50699 2018-06-11 stsp goto done;
1212 a7f50699 2018-06-11 stsp }
1213 963b370f 2018-05-20 stsp
1214 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1216 a7f50699 2018-06-11 stsp done:
1217 00dfcb92 2018-06-11 stsp free(vis);
1218 963b370f 2018-05-20 stsp if (err) {
1219 963b370f 2018-05-20 stsp free(*ws);
1220 963b370f 2018-05-20 stsp *ws = NULL;
1221 963b370f 2018-05-20 stsp *wlen = 0;
1222 963b370f 2018-05-20 stsp }
1223 963b370f 2018-05-20 stsp return err;
1224 963b370f 2018-05-20 stsp }
1225 963b370f 2018-05-20 stsp
1226 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1227 963b370f 2018-05-20 stsp static const struct got_error *
1228 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1229 27a741e5 2019-09-11 stsp int col_tab_align)
1230 963b370f 2018-05-20 stsp {
1231 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1232 963b370f 2018-05-20 stsp int cols = 0;
1233 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1234 963b370f 2018-05-20 stsp size_t wlen;
1235 963b370f 2018-05-20 stsp int i;
1236 963b370f 2018-05-20 stsp
1237 963b370f 2018-05-20 stsp *wlinep = NULL;
1238 b700b5d6 2018-07-10 stsp *widthp = 0;
1239 963b370f 2018-05-20 stsp
1240 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1241 963b370f 2018-05-20 stsp if (err)
1242 963b370f 2018-05-20 stsp return err;
1243 963b370f 2018-05-20 stsp
1244 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1245 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1246 3f670bfb 2020-12-10 stsp wlen--;
1247 3f670bfb 2020-12-10 stsp }
1248 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1249 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1250 3f670bfb 2020-12-10 stsp wlen--;
1251 3f670bfb 2020-12-10 stsp }
1252 3f670bfb 2020-12-10 stsp
1253 963b370f 2018-05-20 stsp i = 0;
1254 27a741e5 2019-09-11 stsp while (i < wlen) {
1255 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1256 27a741e5 2019-09-11 stsp
1257 27a741e5 2019-09-11 stsp if (width == 0) {
1258 b700b5d6 2018-07-10 stsp i++;
1259 27a741e5 2019-09-11 stsp continue;
1260 27a741e5 2019-09-11 stsp }
1261 27a741e5 2019-09-11 stsp
1262 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1263 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1264 27a741e5 2019-09-11 stsp break;
1265 27a741e5 2019-09-11 stsp cols += width;
1266 3c1f04f1 2018-09-13 stsp i++;
1267 27a741e5 2019-09-11 stsp } else if (width == -1) {
1268 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1269 27a741e5 2019-09-11 stsp width = TABSIZE -
1270 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1271 748d5cab 2020-12-14 naddy } else {
1272 748d5cab 2020-12-14 naddy width = 1;
1273 748d5cab 2020-12-14 naddy wline[i] = L'.';
1274 27a741e5 2019-09-11 stsp }
1275 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1276 748d5cab 2020-12-14 naddy break;
1277 748d5cab 2020-12-14 naddy cols += width;
1278 b700b5d6 2018-07-10 stsp i++;
1279 27a741e5 2019-09-11 stsp } else {
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1281 963b370f 2018-05-20 stsp goto done;
1282 963b370f 2018-05-20 stsp }
1283 963b370f 2018-05-20 stsp }
1284 963b370f 2018-05-20 stsp wline[i] = L'\0';
1285 b700b5d6 2018-07-10 stsp if (widthp)
1286 b700b5d6 2018-07-10 stsp *widthp = cols;
1287 963b370f 2018-05-20 stsp done:
1288 963b370f 2018-05-20 stsp if (err)
1289 963b370f 2018-05-20 stsp free(wline);
1290 963b370f 2018-05-20 stsp else
1291 963b370f 2018-05-20 stsp *wlinep = wline;
1292 963b370f 2018-05-20 stsp return err;
1293 963b370f 2018-05-20 stsp }
1294 963b370f 2018-05-20 stsp
1295 8b473291 2019-02-21 stsp static const struct got_error*
1296 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1297 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1298 8b473291 2019-02-21 stsp {
1299 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1300 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1301 8b473291 2019-02-21 stsp char *s;
1302 8b473291 2019-02-21 stsp const char *name;
1303 8b473291 2019-02-21 stsp
1304 8b473291 2019-02-21 stsp *refs_str = NULL;
1305 8b473291 2019-02-21 stsp
1306 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1307 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1308 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1309 52b5abe1 2019-08-13 stsp int cmp;
1310 52b5abe1 2019-08-13 stsp
1311 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1312 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1313 8b473291 2019-02-21 stsp continue;
1314 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1315 8b473291 2019-02-21 stsp name += 5;
1316 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1317 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1318 7143d404 2019-03-12 stsp continue;
1319 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1320 8b473291 2019-02-21 stsp name += 6;
1321 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1322 8b473291 2019-02-21 stsp name += 8;
1323 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1324 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1325 79cc719f 2020-04-24 stsp continue;
1326 79cc719f 2020-04-24 stsp }
1327 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1328 48cae60d 2020-09-22 stsp if (err)
1329 48cae60d 2020-09-22 stsp break;
1330 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1331 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1332 5d844a1e 2019-08-13 stsp if (err) {
1333 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1334 48cae60d 2020-09-22 stsp free(ref_id);
1335 5d844a1e 2019-08-13 stsp break;
1336 48cae60d 2020-09-22 stsp }
1337 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1338 5d844a1e 2019-08-13 stsp err = NULL;
1339 5d844a1e 2019-08-13 stsp tag = NULL;
1340 5d844a1e 2019-08-13 stsp }
1341 52b5abe1 2019-08-13 stsp }
1342 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1343 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1344 48cae60d 2020-09-22 stsp free(ref_id);
1345 52b5abe1 2019-08-13 stsp if (tag)
1346 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1347 52b5abe1 2019-08-13 stsp if (cmp != 0)
1348 52b5abe1 2019-08-13 stsp continue;
1349 8b473291 2019-02-21 stsp s = *refs_str;
1350 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1351 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1352 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1353 8b473291 2019-02-21 stsp free(s);
1354 8b473291 2019-02-21 stsp *refs_str = NULL;
1355 8b473291 2019-02-21 stsp break;
1356 8b473291 2019-02-21 stsp }
1357 8b473291 2019-02-21 stsp free(s);
1358 8b473291 2019-02-21 stsp }
1359 8b473291 2019-02-21 stsp
1360 8b473291 2019-02-21 stsp return err;
1361 8b473291 2019-02-21 stsp }
1362 8b473291 2019-02-21 stsp
1363 963b370f 2018-05-20 stsp static const struct got_error *
1364 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1365 27a741e5 2019-09-11 stsp int col_tab_align)
1366 5813d178 2019-03-09 stsp {
1367 e6b8b890 2020-12-29 naddy char *smallerthan;
1368 5813d178 2019-03-09 stsp
1369 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1370 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1371 5813d178 2019-03-09 stsp author = smallerthan + 1;
1372 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1373 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1374 5813d178 2019-03-09 stsp }
1375 5813d178 2019-03-09 stsp
1376 5813d178 2019-03-09 stsp static const struct got_error *
1377 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1378 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1379 8fdc79fe 2020-12-01 naddy int author_display_cols)
1380 80ddbec8 2018-04-29 stsp {
1381 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1382 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1383 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1384 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1385 5813d178 2019-03-09 stsp char *author = NULL;
1386 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1387 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1388 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1389 bb737323 2018-05-20 stsp int col, limit;
1390 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1391 ccb26ccd 2018-11-05 stsp struct tm tm;
1392 45d799e2 2018-12-23 stsp time_t committer_time;
1393 11b20872 2019-11-08 stsp struct tog_color *tc;
1394 80ddbec8 2018-04-29 stsp
1395 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1396 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1397 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1398 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1399 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1400 b39d25c7 2018-07-10 stsp
1401 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1402 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1403 b39d25c7 2018-07-10 stsp else
1404 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1405 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1406 11b20872 2019-11-08 stsp if (tc)
1407 11b20872 2019-11-08 stsp wattr_on(view->window,
1408 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1409 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1410 11b20872 2019-11-08 stsp if (tc)
1411 11b20872 2019-11-08 stsp wattr_off(view->window,
1412 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1413 27a741e5 2019-09-11 stsp col = limit;
1414 b39d25c7 2018-07-10 stsp if (col > avail)
1415 b39d25c7 2018-07-10 stsp goto done;
1416 6570a66d 2019-11-08 stsp
1417 6570a66d 2019-11-08 stsp if (avail >= 120) {
1418 6570a66d 2019-11-08 stsp char *id_str;
1419 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1420 6570a66d 2019-11-08 stsp if (err)
1421 6570a66d 2019-11-08 stsp goto done;
1422 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1423 11b20872 2019-11-08 stsp if (tc)
1424 11b20872 2019-11-08 stsp wattr_on(view->window,
1425 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1426 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1427 11b20872 2019-11-08 stsp if (tc)
1428 11b20872 2019-11-08 stsp wattr_off(view->window,
1429 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1430 6570a66d 2019-11-08 stsp free(id_str);
1431 6570a66d 2019-11-08 stsp col += 9;
1432 6570a66d 2019-11-08 stsp if (col > avail)
1433 6570a66d 2019-11-08 stsp goto done;
1434 6570a66d 2019-11-08 stsp }
1435 b39d25c7 2018-07-10 stsp
1436 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1437 5813d178 2019-03-09 stsp if (author == NULL) {
1438 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1439 80ddbec8 2018-04-29 stsp goto done;
1440 80ddbec8 2018-04-29 stsp }
1441 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1442 bb737323 2018-05-20 stsp if (err)
1443 bb737323 2018-05-20 stsp goto done;
1444 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1445 11b20872 2019-11-08 stsp if (tc)
1446 11b20872 2019-11-08 stsp wattr_on(view->window,
1447 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1448 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1449 11b20872 2019-11-08 stsp if (tc)
1450 11b20872 2019-11-08 stsp wattr_off(view->window,
1451 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1452 bb737323 2018-05-20 stsp col += author_width;
1453 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1454 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1455 bb737323 2018-05-20 stsp col++;
1456 bb737323 2018-05-20 stsp author_width++;
1457 bb737323 2018-05-20 stsp }
1458 9c2eaf34 2018-05-20 stsp if (col > avail)
1459 9c2eaf34 2018-05-20 stsp goto done;
1460 80ddbec8 2018-04-29 stsp
1461 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1462 5943eee2 2019-08-13 stsp if (err)
1463 6d9fbc00 2018-04-29 stsp goto done;
1464 bb737323 2018-05-20 stsp logmsg = logmsg0;
1465 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1466 bb737323 2018-05-20 stsp logmsg++;
1467 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1468 bb737323 2018-05-20 stsp if (newline)
1469 bb737323 2018-05-20 stsp *newline = '\0';
1470 bb737323 2018-05-20 stsp limit = avail - col;
1471 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1472 bb737323 2018-05-20 stsp if (err)
1473 bb737323 2018-05-20 stsp goto done;
1474 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1475 bb737323 2018-05-20 stsp col += logmsg_width;
1476 27a741e5 2019-09-11 stsp while (col < avail) {
1477 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1478 bb737323 2018-05-20 stsp col++;
1479 881b2d3e 2018-04-30 stsp }
1480 80ddbec8 2018-04-29 stsp done:
1481 80ddbec8 2018-04-29 stsp free(logmsg0);
1482 bb737323 2018-05-20 stsp free(wlogmsg);
1483 5813d178 2019-03-09 stsp free(author);
1484 bb737323 2018-05-20 stsp free(wauthor);
1485 80ddbec8 2018-04-29 stsp free(line);
1486 80ddbec8 2018-04-29 stsp return err;
1487 80ddbec8 2018-04-29 stsp }
1488 26ed57b2 2018-05-19 stsp
1489 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1490 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1491 899d86c2 2018-05-10 stsp struct got_object_id *id)
1492 80ddbec8 2018-04-29 stsp {
1493 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1494 80ddbec8 2018-04-29 stsp
1495 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1496 80ddbec8 2018-04-29 stsp if (entry == NULL)
1497 899d86c2 2018-05-10 stsp return NULL;
1498 99db9666 2018-05-07 stsp
1499 899d86c2 2018-05-10 stsp entry->id = id;
1500 99db9666 2018-05-07 stsp entry->commit = commit;
1501 899d86c2 2018-05-10 stsp return entry;
1502 99db9666 2018-05-07 stsp }
1503 80ddbec8 2018-04-29 stsp
1504 99db9666 2018-05-07 stsp static void
1505 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1506 99db9666 2018-05-07 stsp {
1507 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1508 99db9666 2018-05-07 stsp
1509 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1510 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1511 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1512 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1513 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1514 99db9666 2018-05-07 stsp free(entry);
1515 99db9666 2018-05-07 stsp }
1516 99db9666 2018-05-07 stsp
1517 99db9666 2018-05-07 stsp static void
1518 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1519 99db9666 2018-05-07 stsp {
1520 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1521 99db9666 2018-05-07 stsp pop_commit(commits);
1522 c4972b91 2018-05-07 stsp }
1523 c4972b91 2018-05-07 stsp
1524 c4972b91 2018-05-07 stsp static const struct got_error *
1525 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1526 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1527 13add988 2019-10-15 stsp {
1528 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1529 13add988 2019-10-15 stsp regmatch_t regmatch;
1530 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1531 13add988 2019-10-15 stsp
1532 13add988 2019-10-15 stsp *have_match = 0;
1533 13add988 2019-10-15 stsp
1534 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1535 13add988 2019-10-15 stsp if (err)
1536 13add988 2019-10-15 stsp return err;
1537 13add988 2019-10-15 stsp
1538 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1539 13add988 2019-10-15 stsp if (err)
1540 13add988 2019-10-15 stsp goto done;
1541 13add988 2019-10-15 stsp
1542 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1543 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1544 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1545 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1546 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1547 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1548 13add988 2019-10-15 stsp *have_match = 1;
1549 13add988 2019-10-15 stsp done:
1550 13add988 2019-10-15 stsp free(id_str);
1551 13add988 2019-10-15 stsp free(logmsg);
1552 13add988 2019-10-15 stsp return err;
1553 13add988 2019-10-15 stsp }
1554 13add988 2019-10-15 stsp
1555 13add988 2019-10-15 stsp static const struct got_error *
1556 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1557 c4972b91 2018-05-07 stsp {
1558 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1559 9ba79e04 2018-06-11 stsp
1560 1a76625f 2018-10-22 stsp /*
1561 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1562 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1563 1a76625f 2018-10-22 stsp * while updating the display.
1564 1a76625f 2018-10-22 stsp */
1565 4e0d2870 2020-12-07 naddy do {
1566 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1567 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1568 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1569 1a76625f 2018-10-22 stsp int errcode;
1570 899d86c2 2018-05-10 stsp
1571 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1572 4e0d2870 2020-12-07 naddy NULL, NULL);
1573 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1574 ecb28ae0 2018-07-16 stsp break;
1575 899d86c2 2018-05-10 stsp
1576 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1577 9ba79e04 2018-06-11 stsp if (err)
1578 9ba79e04 2018-06-11 stsp break;
1579 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1580 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1581 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1582 9ba79e04 2018-06-11 stsp break;
1583 9ba79e04 2018-06-11 stsp }
1584 93e45b7c 2018-09-24 stsp
1585 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1586 1a76625f 2018-10-22 stsp if (errcode) {
1587 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1588 13add988 2019-10-15 stsp "pthread_mutex_lock");
1589 1a76625f 2018-10-22 stsp break;
1590 1a76625f 2018-10-22 stsp }
1591 1a76625f 2018-10-22 stsp
1592 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1593 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1594 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1595 1a76625f 2018-10-22 stsp
1596 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1597 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1598 7c1452c1 2020-03-26 stsp int have_match;
1599 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1600 7c1452c1 2020-03-26 stsp if (err)
1601 7c1452c1 2020-03-26 stsp break;
1602 7c1452c1 2020-03-26 stsp if (have_match)
1603 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1604 13add988 2019-10-15 stsp }
1605 13add988 2019-10-15 stsp
1606 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1607 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1608 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1609 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1610 7c1452c1 2020-03-26 stsp if (err)
1611 13add988 2019-10-15 stsp break;
1612 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1613 899d86c2 2018-05-10 stsp
1614 9ba79e04 2018-06-11 stsp return err;
1615 0553a4e3 2018-04-30 stsp }
1616 0553a4e3 2018-04-30 stsp
1617 2b779855 2020-12-05 naddy static void
1618 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1619 2b779855 2020-12-05 naddy {
1620 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1621 2b779855 2020-12-05 naddy int ncommits = 0;
1622 2b779855 2020-12-05 naddy
1623 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1624 2b779855 2020-12-05 naddy while (entry) {
1625 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1626 2b779855 2020-12-05 naddy s->selected_entry = entry;
1627 2b779855 2020-12-05 naddy break;
1628 2b779855 2020-12-05 naddy }
1629 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1630 2b779855 2020-12-05 naddy ncommits++;
1631 2b779855 2020-12-05 naddy }
1632 2b779855 2020-12-05 naddy }
1633 2b779855 2020-12-05 naddy
1634 0553a4e3 2018-04-30 stsp static const struct got_error *
1635 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1636 0553a4e3 2018-04-30 stsp {
1637 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1638 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1639 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1640 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1641 60493ae3 2019-06-20 stsp int width;
1642 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1643 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1644 8b473291 2019-02-21 stsp char *refs_str = NULL;
1645 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1646 11b20872 2019-11-08 stsp struct tog_color *tc;
1647 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1648 0553a4e3 2018-04-30 stsp
1649 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1650 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1651 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1652 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1653 1a76625f 2018-10-22 stsp if (err)
1654 ecb28ae0 2018-07-16 stsp return err;
1655 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1656 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1657 d2075bf3 2020-12-25 stsp if (refs) {
1658 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1659 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1660 d2075bf3 2020-12-25 stsp if (err)
1661 d2075bf3 2020-12-25 stsp goto done;
1662 d2075bf3 2020-12-25 stsp }
1663 867c6645 2018-07-10 stsp }
1664 359bfafd 2019-02-22 stsp
1665 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1666 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1667 1a76625f 2018-10-22 stsp
1668 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1669 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1670 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1671 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1672 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1673 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1674 8f4ed634 2020-03-26 stsp goto done;
1675 8f4ed634 2020-03-26 stsp }
1676 8f4ed634 2020-03-26 stsp } else {
1677 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1678 f9686aa5 2020-03-27 stsp
1679 f9686aa5 2020-03-27 stsp if (view->searching) {
1680 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1681 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1682 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1683 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1684 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1685 f9686aa5 2020-03-27 stsp search_str = "searching...";
1686 f9686aa5 2020-03-27 stsp }
1687 f9686aa5 2020-03-27 stsp
1688 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1689 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1690 f9686aa5 2020-03-27 stsp search_str ? search_str :
1691 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1692 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1693 8f4ed634 2020-03-26 stsp goto done;
1694 8f4ed634 2020-03-26 stsp }
1695 8b473291 2019-02-21 stsp }
1696 1a76625f 2018-10-22 stsp
1697 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1698 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1699 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1700 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1701 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1702 1a76625f 2018-10-22 stsp header = NULL;
1703 1a76625f 2018-10-22 stsp goto done;
1704 1a76625f 2018-10-22 stsp }
1705 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1706 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1707 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1709 1a76625f 2018-10-22 stsp header = NULL;
1710 1a76625f 2018-10-22 stsp goto done;
1711 ecb28ae0 2018-07-16 stsp }
1712 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1713 1a76625f 2018-10-22 stsp if (err)
1714 1a76625f 2018-10-22 stsp goto done;
1715 867c6645 2018-07-10 stsp
1716 2814baeb 2018-08-01 stsp werase(view->window);
1717 867c6645 2018-07-10 stsp
1718 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1719 a3404814 2018-09-02 stsp wstandout(view->window);
1720 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1721 11b20872 2019-11-08 stsp if (tc)
1722 11b20872 2019-11-08 stsp wattr_on(view->window,
1723 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1724 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1725 11b20872 2019-11-08 stsp if (tc)
1726 11b20872 2019-11-08 stsp wattr_off(view->window,
1727 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1728 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1729 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1730 1a76625f 2018-10-22 stsp width++;
1731 1a76625f 2018-10-22 stsp }
1732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1733 a3404814 2018-09-02 stsp wstandend(view->window);
1734 ecb28ae0 2018-07-16 stsp free(wline);
1735 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1736 1a76625f 2018-10-22 stsp goto done;
1737 0553a4e3 2018-04-30 stsp
1738 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1739 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1740 5813d178 2019-03-09 stsp ncommits = 0;
1741 5813d178 2019-03-09 stsp while (entry) {
1742 5813d178 2019-03-09 stsp char *author;
1743 5813d178 2019-03-09 stsp wchar_t *wauthor;
1744 5813d178 2019-03-09 stsp int width;
1745 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1746 5813d178 2019-03-09 stsp break;
1747 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1748 5813d178 2019-03-09 stsp if (author == NULL) {
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1750 5813d178 2019-03-09 stsp goto done;
1751 5813d178 2019-03-09 stsp }
1752 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1753 27a741e5 2019-09-11 stsp date_display_cols);
1754 5813d178 2019-03-09 stsp if (author_cols < width)
1755 5813d178 2019-03-09 stsp author_cols = width;
1756 5813d178 2019-03-09 stsp free(wauthor);
1757 5813d178 2019-03-09 stsp free(author);
1758 7ca04879 2019-10-19 stsp ncommits++;
1759 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1760 5813d178 2019-03-09 stsp }
1761 5813d178 2019-03-09 stsp
1762 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1763 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1764 867c6645 2018-07-10 stsp ncommits = 0;
1765 899d86c2 2018-05-10 stsp while (entry) {
1766 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1767 899d86c2 2018-05-10 stsp break;
1768 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1769 2814baeb 2018-08-01 stsp wstandout(view->window);
1770 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1771 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1772 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1773 2814baeb 2018-08-01 stsp wstandend(view->window);
1774 0553a4e3 2018-04-30 stsp if (err)
1775 60493ae3 2019-06-20 stsp goto done;
1776 0553a4e3 2018-04-30 stsp ncommits++;
1777 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1778 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1779 80ddbec8 2018-04-29 stsp }
1780 80ddbec8 2018-04-29 stsp
1781 1a57306a 2018-09-02 stsp view_vborder(view);
1782 1a76625f 2018-10-22 stsp done:
1783 1a76625f 2018-10-22 stsp free(id_str);
1784 8b473291 2019-02-21 stsp free(refs_str);
1785 1a76625f 2018-10-22 stsp free(ncommits_str);
1786 1a76625f 2018-10-22 stsp free(header);
1787 80ddbec8 2018-04-29 stsp return err;
1788 9f7d7167 2018-04-29 stsp }
1789 07b55e75 2018-05-10 stsp
1790 07b55e75 2018-05-10 stsp static void
1791 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1792 07b55e75 2018-05-10 stsp {
1793 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1794 07b55e75 2018-05-10 stsp int nscrolled = 0;
1795 07b55e75 2018-05-10 stsp
1796 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1797 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1798 07b55e75 2018-05-10 stsp return;
1799 9f7d7167 2018-04-29 stsp
1800 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1801 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1802 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1803 07b55e75 2018-05-10 stsp if (entry) {
1804 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1805 07b55e75 2018-05-10 stsp nscrolled++;
1806 07b55e75 2018-05-10 stsp }
1807 07b55e75 2018-05-10 stsp }
1808 aa075928 2018-05-10 stsp }
1809 aa075928 2018-05-10 stsp
1810 aa075928 2018-05-10 stsp static const struct got_error *
1811 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1812 aa075928 2018-05-10 stsp {
1813 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1814 5e224a3e 2019-02-22 stsp int errcode;
1815 8a42fca8 2019-02-22 stsp
1816 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1817 aa075928 2018-05-10 stsp
1818 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1819 ffe38506 2020-12-01 naddy if (ta->log_complete)
1820 5e224a3e 2019-02-22 stsp break;
1821 b295e71b 2019-02-22 stsp
1822 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1823 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1824 7aafa0d1 2019-02-22 stsp if (errcode)
1825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1826 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1827 7c1452c1 2020-03-26 stsp
1828 7c1452c1 2020-03-26 stsp /*
1829 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1830 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1831 7c1452c1 2020-03-26 stsp */
1832 7c1452c1 2020-03-26 stsp if (!wait)
1833 7c1452c1 2020-03-26 stsp break;
1834 7c1452c1 2020-03-26 stsp
1835 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1836 ffe38506 2020-12-01 naddy show_log_view(view);
1837 7c1452c1 2020-03-26 stsp update_panels();
1838 7c1452c1 2020-03-26 stsp doupdate();
1839 7c1452c1 2020-03-26 stsp
1840 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1841 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1842 82954512 2020-02-03 stsp if (errcode)
1843 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1844 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1845 82954512 2020-02-03 stsp
1846 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1847 ffe38506 2020-12-01 naddy show_log_view(view);
1848 7c1452c1 2020-03-26 stsp update_panels();
1849 7c1452c1 2020-03-26 stsp doupdate();
1850 5e224a3e 2019-02-22 stsp }
1851 5e224a3e 2019-02-22 stsp
1852 5e224a3e 2019-02-22 stsp return NULL;
1853 5e224a3e 2019-02-22 stsp }
1854 5e224a3e 2019-02-22 stsp
1855 5e224a3e 2019-02-22 stsp static const struct got_error *
1856 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1857 5e224a3e 2019-02-22 stsp {
1858 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1859 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1860 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1861 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1862 5e224a3e 2019-02-22 stsp
1863 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1864 5e224a3e 2019-02-22 stsp return NULL;
1865 5e224a3e 2019-02-22 stsp
1866 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1867 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1868 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1869 08ebd0a9 2019-02-22 stsp /*
1870 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1871 08ebd0a9 2019-02-22 stsp */
1872 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1873 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1874 5e224a3e 2019-02-22 stsp if (err)
1875 5e224a3e 2019-02-22 stsp return err;
1876 7aafa0d1 2019-02-22 stsp }
1877 b295e71b 2019-02-22 stsp
1878 7aafa0d1 2019-02-22 stsp do {
1879 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1880 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1881 88048b54 2019-02-21 stsp break;
1882 88048b54 2019-02-21 stsp
1883 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1884 aa075928 2018-05-10 stsp
1885 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1886 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1887 dd0a52c1 2018-05-20 stsp break;
1888 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1889 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1890 aa075928 2018-05-10 stsp
1891 dd0a52c1 2018-05-20 stsp return err;
1892 07b55e75 2018-05-10 stsp }
1893 4a7f7875 2018-05-10 stsp
1894 cd0acaa7 2018-05-20 stsp static const struct got_error *
1895 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1896 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1897 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1898 cd0acaa7 2018-05-20 stsp {
1899 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1900 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1901 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1902 cd0acaa7 2018-05-20 stsp
1903 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1904 15a94983 2018-12-23 stsp if (diff_view == NULL)
1905 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1906 ea5e7bb5 2018-08-01 stsp
1907 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1908 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1909 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1910 e5a0f69f 2018-08-18 stsp if (err == NULL)
1911 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1912 cd0acaa7 2018-05-20 stsp return err;
1913 4a7f7875 2018-05-10 stsp }
1914 4a7f7875 2018-05-10 stsp
1915 80ddbec8 2018-04-29 stsp static const struct got_error *
1916 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1917 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1918 9343a5fb 2018-06-23 stsp {
1919 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1920 941e9f74 2019-05-21 stsp
1921 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1922 941e9f74 2019-05-21 stsp if (parent == NULL)
1923 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1924 941e9f74 2019-05-21 stsp
1925 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1926 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1927 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1928 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1929 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1930 941e9f74 2019-05-21 stsp s->tree = subtree;
1931 941e9f74 2019-05-21 stsp s->selected = 0;
1932 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1933 941e9f74 2019-05-21 stsp return NULL;
1934 941e9f74 2019-05-21 stsp }
1935 941e9f74 2019-05-21 stsp
1936 941e9f74 2019-05-21 stsp static const struct got_error *
1937 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1938 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
1939 941e9f74 2019-05-21 stsp {
1940 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1941 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1942 941e9f74 2019-05-21 stsp const char *p;
1943 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1944 9343a5fb 2018-06-23 stsp
1945 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1946 941e9f74 2019-05-21 stsp p = path;
1947 941e9f74 2019-05-21 stsp while (*p) {
1948 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1949 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1950 56e0773d 2019-11-28 stsp char *te_name;
1951 33cbf02b 2020-01-12 stsp
1952 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1953 33cbf02b 2020-01-12 stsp p++;
1954 941e9f74 2019-05-21 stsp
1955 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1956 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1957 941e9f74 2019-05-21 stsp if (slash == NULL)
1958 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1959 33cbf02b 2020-01-12 stsp else
1960 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1961 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1962 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1963 56e0773d 2019-11-28 stsp break;
1964 941e9f74 2019-05-21 stsp }
1965 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1966 56e0773d 2019-11-28 stsp if (te == NULL) {
1967 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1968 56e0773d 2019-11-28 stsp free(te_name);
1969 941e9f74 2019-05-21 stsp break;
1970 941e9f74 2019-05-21 stsp }
1971 56e0773d 2019-11-28 stsp free(te_name);
1972 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1973 941e9f74 2019-05-21 stsp
1974 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1975 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1976 b03c880f 2019-05-21 stsp
1977 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1978 941e9f74 2019-05-21 stsp if (slash)
1979 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1980 941e9f74 2019-05-21 stsp else
1981 941e9f74 2019-05-21 stsp subpath = strdup(path);
1982 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1983 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1984 941e9f74 2019-05-21 stsp break;
1985 941e9f74 2019-05-21 stsp }
1986 941e9f74 2019-05-21 stsp
1987 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
1988 941e9f74 2019-05-21 stsp subpath);
1989 941e9f74 2019-05-21 stsp if (err)
1990 941e9f74 2019-05-21 stsp break;
1991 941e9f74 2019-05-21 stsp
1992 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1993 941e9f74 2019-05-21 stsp free(tree_id);
1994 941e9f74 2019-05-21 stsp if (err)
1995 941e9f74 2019-05-21 stsp break;
1996 941e9f74 2019-05-21 stsp
1997 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1998 941e9f74 2019-05-21 stsp if (err) {
1999 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2000 941e9f74 2019-05-21 stsp break;
2001 941e9f74 2019-05-21 stsp }
2002 941e9f74 2019-05-21 stsp if (slash == NULL)
2003 941e9f74 2019-05-21 stsp break;
2004 941e9f74 2019-05-21 stsp free(subpath);
2005 941e9f74 2019-05-21 stsp subpath = NULL;
2006 941e9f74 2019-05-21 stsp p = slash;
2007 941e9f74 2019-05-21 stsp }
2008 941e9f74 2019-05-21 stsp
2009 941e9f74 2019-05-21 stsp free(subpath);
2010 1a76625f 2018-10-22 stsp return err;
2011 61266923 2020-01-14 stsp }
2012 61266923 2020-01-14 stsp
2013 61266923 2020-01-14 stsp static const struct got_error *
2014 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2015 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2016 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2017 55cccc34 2020-02-20 stsp {
2018 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2019 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2020 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2021 55cccc34 2020-02-20 stsp
2022 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2023 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2024 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2025 55cccc34 2020-02-20 stsp
2026 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2027 bc573f3b 2021-07-10 stsp if (err)
2028 55cccc34 2020-02-20 stsp return err;
2029 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2030 55cccc34 2020-02-20 stsp
2031 55cccc34 2020-02-20 stsp *new_view = tree_view;
2032 55cccc34 2020-02-20 stsp
2033 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2034 55cccc34 2020-02-20 stsp return NULL;
2035 55cccc34 2020-02-20 stsp
2036 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2037 55cccc34 2020-02-20 stsp }
2038 55cccc34 2020-02-20 stsp
2039 55cccc34 2020-02-20 stsp static const struct got_error *
2040 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2041 61266923 2020-01-14 stsp {
2042 61266923 2020-01-14 stsp sigset_t sigset;
2043 61266923 2020-01-14 stsp int errcode;
2044 61266923 2020-01-14 stsp
2045 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2046 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2047 61266923 2020-01-14 stsp
2048 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2049 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2050 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2051 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2052 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2053 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2054 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2055 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2056 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2057 61266923 2020-01-14 stsp
2058 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2059 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2060 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2061 61266923 2020-01-14 stsp
2062 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2063 61266923 2020-01-14 stsp if (errcode)
2064 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2065 61266923 2020-01-14 stsp
2066 61266923 2020-01-14 stsp return NULL;
2067 1a76625f 2018-10-22 stsp }
2068 1a76625f 2018-10-22 stsp
2069 1a76625f 2018-10-22 stsp static void *
2070 1a76625f 2018-10-22 stsp log_thread(void *arg)
2071 1a76625f 2018-10-22 stsp {
2072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2073 1a76625f 2018-10-22 stsp int errcode = 0;
2074 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2075 1a76625f 2018-10-22 stsp int done = 0;
2076 61266923 2020-01-14 stsp
2077 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2078 61266923 2020-01-14 stsp if (err)
2079 61266923 2020-01-14 stsp return (void *)err;
2080 1a76625f 2018-10-22 stsp
2081 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2082 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2083 1a76625f 2018-10-22 stsp if (err) {
2084 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2085 1a76625f 2018-10-22 stsp return (void *)err;
2086 1a76625f 2018-10-22 stsp err = NULL;
2087 1a76625f 2018-10-22 stsp done = 1;
2088 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2089 1a76625f 2018-10-22 stsp a->commits_needed--;
2090 1a76625f 2018-10-22 stsp
2091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2092 3abe8080 2019-04-10 stsp if (errcode) {
2093 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2094 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2095 3abe8080 2019-04-10 stsp break;
2096 3abe8080 2019-04-10 stsp } else if (*a->quit)
2097 1a76625f 2018-10-22 stsp done = 1;
2098 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2099 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2100 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2101 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2102 1a76625f 2018-10-22 stsp }
2103 1a76625f 2018-10-22 stsp
2104 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2105 7c1452c1 2020-03-26 stsp if (errcode) {
2106 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2107 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2108 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2109 7c1452c1 2020-03-26 stsp break;
2110 7c1452c1 2020-03-26 stsp }
2111 7c1452c1 2020-03-26 stsp
2112 1a76625f 2018-10-22 stsp if (done)
2113 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2114 7c1452c1 2020-03-26 stsp else {
2115 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2116 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2117 7c1452c1 2020-03-26 stsp &tog_mutex);
2118 7c1452c1 2020-03-26 stsp if (errcode)
2119 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2120 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2121 21355643 2020-12-06 stsp if (*a->quit)
2122 21355643 2020-12-06 stsp done = 1;
2123 7c1452c1 2020-03-26 stsp }
2124 1a76625f 2018-10-22 stsp }
2125 1a76625f 2018-10-22 stsp
2126 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2127 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2128 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2129 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2130 1a76625f 2018-10-22 stsp }
2131 3abe8080 2019-04-10 stsp a->log_complete = 1;
2132 1a76625f 2018-10-22 stsp return (void *)err;
2133 1a76625f 2018-10-22 stsp }
2134 1a76625f 2018-10-22 stsp
2135 1a76625f 2018-10-22 stsp static const struct got_error *
2136 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2137 1a76625f 2018-10-22 stsp {
2138 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2139 1a76625f 2018-10-22 stsp int errcode;
2140 1a76625f 2018-10-22 stsp
2141 1a76625f 2018-10-22 stsp if (s->thread) {
2142 1a76625f 2018-10-22 stsp s->quit = 1;
2143 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2144 1a76625f 2018-10-22 stsp if (errcode)
2145 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2146 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2147 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2148 1a76625f 2018-10-22 stsp if (errcode)
2149 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2150 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2151 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2152 1a76625f 2018-10-22 stsp if (errcode)
2153 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2155 1a76625f 2018-10-22 stsp if (errcode)
2156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2157 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2158 1a76625f 2018-10-22 stsp s->thread = NULL;
2159 1a76625f 2018-10-22 stsp }
2160 1a76625f 2018-10-22 stsp
2161 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2162 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2163 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2164 74467cc8 2022-06-15 stsp }
2165 74467cc8 2022-06-15 stsp
2166 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2167 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2168 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2169 74467cc8 2022-06-15 stsp if (err == NULL)
2170 74467cc8 2022-06-15 stsp err = pack_err;
2171 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2172 1a76625f 2018-10-22 stsp }
2173 1a76625f 2018-10-22 stsp
2174 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2175 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2176 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2177 1a76625f 2018-10-22 stsp }
2178 1a76625f 2018-10-22 stsp
2179 9343a5fb 2018-06-23 stsp return err;
2180 9343a5fb 2018-06-23 stsp }
2181 9343a5fb 2018-06-23 stsp
2182 9343a5fb 2018-06-23 stsp static const struct got_error *
2183 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2184 1a76625f 2018-10-22 stsp {
2185 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2186 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2187 276b94a1 2020-11-13 naddy int errcode;
2188 1a76625f 2018-10-22 stsp
2189 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2190 276b94a1 2020-11-13 naddy
2191 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2192 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2193 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2194 276b94a1 2020-11-13 naddy
2195 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2196 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2197 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2198 276b94a1 2020-11-13 naddy
2199 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2200 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2201 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2202 1a76625f 2018-10-22 stsp free(s->start_id);
2203 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2204 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2205 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2206 1a76625f 2018-10-22 stsp return err;
2207 1a76625f 2018-10-22 stsp }
2208 1a76625f 2018-10-22 stsp
2209 1a76625f 2018-10-22 stsp static const struct got_error *
2210 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2211 60493ae3 2019-06-20 stsp {
2212 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2213 60493ae3 2019-06-20 stsp
2214 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2215 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2216 60493ae3 2019-06-20 stsp return NULL;
2217 60493ae3 2019-06-20 stsp }
2218 60493ae3 2019-06-20 stsp
2219 60493ae3 2019-06-20 stsp static const struct got_error *
2220 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2221 60493ae3 2019-06-20 stsp {
2222 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2223 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2224 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2225 60493ae3 2019-06-20 stsp
2226 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2227 f9686aa5 2020-03-27 stsp show_log_view(view);
2228 f9686aa5 2020-03-27 stsp update_panels();
2229 f9686aa5 2020-03-27 stsp doupdate();
2230 f9686aa5 2020-03-27 stsp
2231 96e2b566 2019-07-08 stsp if (s->search_entry) {
2232 13add988 2019-10-15 stsp int errcode, ch;
2233 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2234 13add988 2019-10-15 stsp if (errcode)
2235 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2236 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2237 13add988 2019-10-15 stsp ch = wgetch(view->window);
2238 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2239 13add988 2019-10-15 stsp if (errcode)
2240 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2241 13add988 2019-10-15 stsp "pthread_mutex_lock");
2242 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2243 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2244 678cbce5 2019-07-28 stsp return NULL;
2245 678cbce5 2019-07-28 stsp }
2246 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2247 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2248 96e2b566 2019-07-08 stsp else
2249 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2250 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2251 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2252 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2253 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2254 b1bf1435 2019-06-21 stsp else
2255 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2256 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2257 20be8d96 2019-06-21 stsp } else {
2258 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2259 20be8d96 2019-06-21 stsp }
2260 60493ae3 2019-06-20 stsp
2261 60493ae3 2019-06-20 stsp while (1) {
2262 13add988 2019-10-15 stsp int have_match = 0;
2263 13add988 2019-10-15 stsp
2264 60493ae3 2019-06-20 stsp if (entry == NULL) {
2265 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2266 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2267 f9967bca 2020-03-27 stsp view->search_next_done =
2268 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2269 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2270 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2271 f801134a 2019-06-25 stsp return NULL;
2272 60493ae3 2019-06-20 stsp }
2273 96e2b566 2019-07-08 stsp /*
2274 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2275 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2276 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2277 96e2b566 2019-07-08 stsp */
2278 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2279 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2280 60493ae3 2019-06-20 stsp }
2281 60493ae3 2019-06-20 stsp
2282 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2283 13add988 2019-10-15 stsp &view->regex);
2284 5943eee2 2019-08-13 stsp if (err)
2285 13add988 2019-10-15 stsp break;
2286 13add988 2019-10-15 stsp if (have_match) {
2287 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2288 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2289 60493ae3 2019-06-20 stsp break;
2290 60493ae3 2019-06-20 stsp }
2291 13add988 2019-10-15 stsp
2292 96e2b566 2019-07-08 stsp s->search_entry = entry;
2293 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2294 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2295 b1bf1435 2019-06-21 stsp else
2296 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2297 60493ae3 2019-06-20 stsp }
2298 60493ae3 2019-06-20 stsp
2299 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2300 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2301 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2302 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2303 60493ae3 2019-06-20 stsp if (err)
2304 60493ae3 2019-06-20 stsp return err;
2305 ead14cbe 2019-06-21 stsp cur++;
2306 ead14cbe 2019-06-21 stsp }
2307 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2308 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2309 60493ae3 2019-06-20 stsp if (err)
2310 60493ae3 2019-06-20 stsp return err;
2311 ead14cbe 2019-06-21 stsp cur--;
2312 60493ae3 2019-06-20 stsp }
2313 60493ae3 2019-06-20 stsp }
2314 60493ae3 2019-06-20 stsp
2315 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2316 96e2b566 2019-07-08 stsp
2317 60493ae3 2019-06-20 stsp return NULL;
2318 60493ae3 2019-06-20 stsp }
2319 60493ae3 2019-06-20 stsp
2320 60493ae3 2019-06-20 stsp static const struct got_error *
2321 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2322 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2323 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2324 80ddbec8 2018-04-29 stsp {
2325 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2326 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2327 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2328 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2329 1a76625f 2018-10-22 stsp int errcode;
2330 80ddbec8 2018-04-29 stsp
2331 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2332 f135c941 2020-02-20 stsp free(s->in_repo_path);
2333 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2334 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2335 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2336 f135c941 2020-02-20 stsp }
2337 ecb28ae0 2018-07-16 stsp
2338 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2339 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2340 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2341 78756c87 2020-11-24 stsp
2342 fb2756b9 2018-08-04 stsp s->repo = repo;
2343 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2344 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2345 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2346 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2347 9cd7cbd1 2020-12-07 stsp goto done;
2348 9cd7cbd1 2020-12-07 stsp }
2349 9cd7cbd1 2020-12-07 stsp }
2350 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2351 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2353 5036bf37 2018-09-24 stsp goto done;
2354 5036bf37 2018-09-24 stsp }
2355 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2356 e5a0f69f 2018-08-18 stsp
2357 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2358 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2359 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2360 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2361 11b20872 2019-11-08 stsp if (err)
2362 11b20872 2019-11-08 stsp goto done;
2363 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2364 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2365 11b20872 2019-11-08 stsp if (err) {
2366 11b20872 2019-11-08 stsp free_colors(&s->colors);
2367 11b20872 2019-11-08 stsp goto done;
2368 11b20872 2019-11-08 stsp }
2369 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2370 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2371 11b20872 2019-11-08 stsp if (err) {
2372 11b20872 2019-11-08 stsp free_colors(&s->colors);
2373 11b20872 2019-11-08 stsp goto done;
2374 11b20872 2019-11-08 stsp }
2375 11b20872 2019-11-08 stsp }
2376 11b20872 2019-11-08 stsp
2377 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2378 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2379 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2380 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2381 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2382 1a76625f 2018-10-22 stsp
2383 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2384 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2385 74467cc8 2022-06-15 stsp if (err)
2386 74467cc8 2022-06-15 stsp goto done;
2387 74467cc8 2022-06-15 stsp }
2388 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2389 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2390 0ae84acc 2022-06-15 tracey if (err)
2391 0ae84acc 2022-06-15 tracey goto done;
2392 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2393 b672a97a 2020-01-27 stsp !s->log_branches);
2394 1a76625f 2018-10-22 stsp if (err)
2395 1a76625f 2018-10-22 stsp goto done;
2396 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2397 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2398 c5b78334 2020-01-12 stsp if (err)
2399 c5b78334 2020-01-12 stsp goto done;
2400 1a76625f 2018-10-22 stsp
2401 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2402 1a76625f 2018-10-22 stsp if (errcode) {
2403 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2404 1a76625f 2018-10-22 stsp goto done;
2405 1a76625f 2018-10-22 stsp }
2406 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2407 7c1452c1 2020-03-26 stsp if (errcode) {
2408 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2409 7c1452c1 2020-03-26 stsp goto done;
2410 7c1452c1 2020-03-26 stsp }
2411 1a76625f 2018-10-22 stsp
2412 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2413 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2414 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2415 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2416 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2417 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2418 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2419 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2420 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2421 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2422 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2423 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2424 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2425 ba4f502b 2018-08-04 stsp done:
2426 1a76625f 2018-10-22 stsp if (err)
2427 1a76625f 2018-10-22 stsp close_log_view(view);
2428 ba4f502b 2018-08-04 stsp return err;
2429 ba4f502b 2018-08-04 stsp }
2430 ba4f502b 2018-08-04 stsp
2431 e5a0f69f 2018-08-18 stsp static const struct got_error *
2432 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2433 ba4f502b 2018-08-04 stsp {
2434 f2f6d207 2020-11-24 stsp const struct got_error *err;
2435 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2436 ba4f502b 2018-08-04 stsp
2437 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2438 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2439 2b380cc8 2018-10-24 stsp &s->thread_args);
2440 2b380cc8 2018-10-24 stsp if (errcode)
2441 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2442 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2443 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2444 f2f6d207 2020-11-24 stsp if (err)
2445 f2f6d207 2020-11-24 stsp return err;
2446 f2f6d207 2020-11-24 stsp }
2447 2b380cc8 2018-10-24 stsp }
2448 2b380cc8 2018-10-24 stsp
2449 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2450 e5a0f69f 2018-08-18 stsp }
2451 04cc582a 2018-08-01 stsp
2452 e5a0f69f 2018-08-18 stsp static const struct got_error *
2453 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2454 e5a0f69f 2018-08-18 stsp {
2455 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2456 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2457 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2458 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2459 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2460 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
2461 80ddbec8 2018-04-29 stsp
2462 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2463 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2464 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2465 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2466 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2467 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2468 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2469 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2470 fb280deb 2021-08-30 stsp select_commit(s);
2471 fb280deb 2021-08-30 stsp }
2472 528dedf3 2021-08-30 stsp return NULL;
2473 528dedf3 2021-08-30 stsp }
2474 528dedf3 2021-08-30 stsp
2475 528dedf3 2021-08-30 stsp switch (ch) {
2476 1e37a5c2 2019-05-12 jcs case 'q':
2477 1e37a5c2 2019-05-12 jcs s->quit = 1;
2478 1e37a5c2 2019-05-12 jcs break;
2479 1e37a5c2 2019-05-12 jcs case 'k':
2480 1e37a5c2 2019-05-12 jcs case KEY_UP:
2481 1e37a5c2 2019-05-12 jcs case '<':
2482 1e37a5c2 2019-05-12 jcs case ',':
2483 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2484 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2485 1a76625f 2018-10-22 stsp break;
2486 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2487 1e37a5c2 2019-05-12 jcs s->selected--;
2488 1144d21a 2019-06-21 stsp else
2489 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2490 912a3f79 2021-08-30 j select_commit(s);
2491 912a3f79 2021-08-30 j break;
2492 912a3f79 2021-08-30 j case 'g':
2493 912a3f79 2021-08-30 j case KEY_HOME:
2494 912a3f79 2021-08-30 j s->selected = 0;
2495 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2496 2b779855 2020-12-05 naddy select_commit(s);
2497 1e37a5c2 2019-05-12 jcs break;
2498 83cc4199 2022-06-13 stsp case CTRL('u'):
2499 33c3719a 2022-06-15 stsp case 'u':
2500 83cc4199 2022-06-13 stsp nscroll /= 2;
2501 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2502 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2503 a4292ac5 2019-05-12 jcs case CTRL('b'):
2504 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2505 e5a0f69f 2018-08-18 stsp break;
2506 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2507 83cc4199 2022-06-13 stsp s->selected = MAX(0, s->selected - nscroll - 1);
2508 2b779855 2020-12-05 naddy else
2509 83cc4199 2022-06-13 stsp log_scroll_up(s, nscroll);
2510 2b779855 2020-12-05 naddy select_commit(s);
2511 1e37a5c2 2019-05-12 jcs break;
2512 1e37a5c2 2019-05-12 jcs case 'j':
2513 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2514 1e37a5c2 2019-05-12 jcs case '>':
2515 1e37a5c2 2019-05-12 jcs case '.':
2516 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2517 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2518 e5a0f69f 2018-08-18 stsp break;
2519 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2520 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2521 1e37a5c2 2019-05-12 jcs s->selected++;
2522 2b779855 2020-12-05 naddy else {
2523 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2524 2b779855 2020-12-05 naddy if (err)
2525 2b779855 2020-12-05 naddy break;
2526 912a3f79 2021-08-30 j }
2527 912a3f79 2021-08-30 j select_commit(s);
2528 912a3f79 2021-08-30 j break;
2529 912a3f79 2021-08-30 j case 'G':
2530 912a3f79 2021-08-30 j case KEY_END: {
2531 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2532 912a3f79 2021-08-30 j * traverse them all. */
2533 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2534 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2535 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2536 80ddbec8 2018-04-29 stsp }
2537 912a3f79 2021-08-30 j
2538 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2539 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2540 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2541 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2542 f3bc9f1d 2021-09-05 naddy break;
2543 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2544 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2545 f3bc9f1d 2021-09-05 naddy }
2546 f3bc9f1d 2021-09-05 naddy if (n > 0)
2547 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2548 2b779855 2020-12-05 naddy select_commit(s);
2549 1e37a5c2 2019-05-12 jcs break;
2550 912a3f79 2021-08-30 j }
2551 80b7e8da 2022-06-11 stsp case CTRL('d'):
2552 33c3719a 2022-06-15 stsp case 'd':
2553 83cc4199 2022-06-13 stsp nscroll /= 2;
2554 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2555 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2556 83cc4199 2022-06-13 stsp case CTRL('f'): {
2557 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2558 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2559 1e37a5c2 2019-05-12 jcs if (first == NULL)
2560 e5a0f69f 2018-08-18 stsp break;
2561 83cc4199 2022-06-13 stsp err = log_scroll_down(view, nscroll);
2562 1cae65b4 2019-09-22 stsp if (err)
2563 1cae65b4 2019-09-22 stsp break;
2564 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2565 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2566 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2567 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2568 83cc4199 2022-06-13 stsp s->selected += MIN(s->last_displayed_entry->idx -
2569 83cc4199 2022-06-13 stsp s->selected_entry->idx, nscroll + 1);
2570 1e37a5c2 2019-05-12 jcs }
2571 2b779855 2020-12-05 naddy select_commit(s);
2572 1e37a5c2 2019-05-12 jcs break;
2573 1e37a5c2 2019-05-12 jcs }
2574 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2575 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2576 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2577 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2578 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2579 2b779855 2020-12-05 naddy select_commit(s);
2580 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2581 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2582 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2583 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2584 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2585 0bf7f153 2020-12-02 naddy }
2586 1e37a5c2 2019-05-12 jcs break;
2587 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2588 87c7274c 2019-05-12 jcs case ' ':
2589 1e37a5c2 2019-05-12 jcs case '\r':
2590 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2591 e5a0f69f 2018-08-18 stsp break;
2592 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2593 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2594 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2595 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2596 78756c87 2020-11-24 stsp view, s->repo);
2597 1e37a5c2 2019-05-12 jcs if (err)
2598 1e37a5c2 2019-05-12 jcs break;
2599 e78dc838 2020-12-04 stsp view->focussed = 0;
2600 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2601 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2602 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2603 f7013a22 2018-10-24 stsp if (err)
2604 1e37a5c2 2019-05-12 jcs return err;
2605 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2606 e78dc838 2020-12-04 stsp view->focus_child = 1;
2607 1e37a5c2 2019-05-12 jcs } else
2608 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2609 1e37a5c2 2019-05-12 jcs break;
2610 1e37a5c2 2019-05-12 jcs case 't':
2611 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2612 5036bf37 2018-09-24 stsp break;
2613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2614 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2615 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2616 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2617 4e97c21c 2020-12-06 stsp s->repo);
2618 1e37a5c2 2019-05-12 jcs if (err)
2619 e5a0f69f 2018-08-18 stsp break;
2620 e78dc838 2020-12-04 stsp view->focussed = 0;
2621 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2624 1e37a5c2 2019-05-12 jcs if (err)
2625 1e37a5c2 2019-05-12 jcs return err;
2626 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2627 e78dc838 2020-12-04 stsp view->focus_child = 1;
2628 1e37a5c2 2019-05-12 jcs } else
2629 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2630 1e37a5c2 2019-05-12 jcs break;
2631 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2632 21355643 2020-12-06 stsp case CTRL('l'):
2633 21355643 2020-12-06 stsp case 'B':
2634 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2635 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2636 1e37a5c2 2019-05-12 jcs break;
2637 21355643 2020-12-06 stsp err = stop_log_thread(s);
2638 74cfe85e 2020-10-20 stsp if (err)
2639 74cfe85e 2020-10-20 stsp return err;
2640 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2641 21355643 2020-12-06 stsp char *parent_path;
2642 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2643 21355643 2020-12-06 stsp if (err)
2644 21355643 2020-12-06 stsp return err;
2645 21355643 2020-12-06 stsp free(s->in_repo_path);
2646 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2647 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2648 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2649 21355643 2020-12-06 stsp struct got_object_id *start_id;
2650 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2651 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2652 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2653 21355643 2020-12-06 stsp if (err)
2654 21355643 2020-12-06 stsp return err;
2655 21355643 2020-12-06 stsp free(s->start_id);
2656 21355643 2020-12-06 stsp s->start_id = start_id;
2657 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2658 21355643 2020-12-06 stsp } else /* 'B' */
2659 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2660 21355643 2020-12-06 stsp
2661 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
2662 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
2663 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2664 74cfe85e 2020-10-20 stsp if (err)
2665 21355643 2020-12-06 stsp return err;
2666 51a10b52 2020-12-26 stsp tog_free_refs();
2667 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2668 ca51c541 2020-12-07 stsp if (err)
2669 ca51c541 2020-12-07 stsp return err;
2670 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2671 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2672 d01904d4 2019-06-25 stsp if (err)
2673 d01904d4 2019-06-25 stsp return err;
2674 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2675 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2676 b672a97a 2020-01-27 stsp if (err)
2677 b672a97a 2020-01-27 stsp return err;
2678 21355643 2020-12-06 stsp free_commits(&s->commits);
2679 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2680 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2681 21355643 2020-12-06 stsp s->selected_entry = NULL;
2682 21355643 2020-12-06 stsp s->selected = 0;
2683 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2684 21355643 2020-12-06 stsp s->quit = 0;
2685 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2686 d01904d4 2019-06-25 stsp break;
2687 6458efa5 2020-11-24 stsp case 'r':
2688 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2689 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2690 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2691 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2692 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2693 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2694 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2695 6458efa5 2020-11-24 stsp if (err) {
2696 6458efa5 2020-11-24 stsp view_close(ref_view);
2697 6458efa5 2020-11-24 stsp return err;
2698 6458efa5 2020-11-24 stsp }
2699 e78dc838 2020-12-04 stsp view->focussed = 0;
2700 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2701 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2702 6458efa5 2020-11-24 stsp err = view_close_child(view);
2703 6458efa5 2020-11-24 stsp if (err)
2704 6458efa5 2020-11-24 stsp return err;
2705 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2706 e78dc838 2020-12-04 stsp view->focus_child = 1;
2707 6458efa5 2020-11-24 stsp } else
2708 6458efa5 2020-11-24 stsp *new_view = ref_view;
2709 6458efa5 2020-11-24 stsp break;
2710 1e37a5c2 2019-05-12 jcs default:
2711 1e37a5c2 2019-05-12 jcs break;
2712 899d86c2 2018-05-10 stsp }
2713 e5a0f69f 2018-08-18 stsp
2714 80ddbec8 2018-04-29 stsp return err;
2715 80ddbec8 2018-04-29 stsp }
2716 80ddbec8 2018-04-29 stsp
2717 4ed7e80c 2018-05-20 stsp static const struct got_error *
2718 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2719 c2db6724 2019-01-04 stsp {
2720 c2db6724 2019-01-04 stsp const struct got_error *error;
2721 c2db6724 2019-01-04 stsp
2722 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2723 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2724 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2725 37c06ea4 2019-07-15 stsp #endif
2726 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2727 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2728 c2db6724 2019-01-04 stsp
2729 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2730 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2731 c2db6724 2019-01-04 stsp
2732 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2733 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2734 c2db6724 2019-01-04 stsp
2735 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2736 c2db6724 2019-01-04 stsp if (error != NULL)
2737 c2db6724 2019-01-04 stsp return error;
2738 c2db6724 2019-01-04 stsp
2739 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2740 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2741 c2db6724 2019-01-04 stsp
2742 c2db6724 2019-01-04 stsp return NULL;
2743 c2db6724 2019-01-04 stsp }
2744 c2db6724 2019-01-04 stsp
2745 a915003a 2019-02-05 stsp static void
2746 a915003a 2019-02-05 stsp init_curses(void)
2747 a915003a 2019-02-05 stsp {
2748 2497f032 2022-05-31 stsp /*
2749 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
2750 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
2751 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
2752 2497f032 2022-05-31 stsp */
2753 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
2754 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
2755 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
2756 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
2757 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
2758 2497f032 2022-05-31 stsp
2759 a915003a 2019-02-05 stsp initscr();
2760 a915003a 2019-02-05 stsp cbreak();
2761 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2762 a915003a 2019-02-05 stsp noecho();
2763 a915003a 2019-02-05 stsp nonl();
2764 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2765 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2766 a915003a 2019-02-05 stsp curs_set(0);
2767 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2768 6d17833f 2019-11-08 stsp start_color();
2769 6d17833f 2019-11-08 stsp use_default_colors();
2770 6d17833f 2019-11-08 stsp }
2771 a915003a 2019-02-05 stsp }
2772 a915003a 2019-02-05 stsp
2773 c2db6724 2019-01-04 stsp static const struct got_error *
2774 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2775 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2776 f135c941 2020-02-20 stsp {
2777 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2778 f135c941 2020-02-20 stsp
2779 f135c941 2020-02-20 stsp if (argc == 0) {
2780 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2781 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2782 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2783 f135c941 2020-02-20 stsp return NULL;
2784 f135c941 2020-02-20 stsp }
2785 f135c941 2020-02-20 stsp
2786 f135c941 2020-02-20 stsp if (worktree) {
2787 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2788 bfd61697 2020-11-14 stsp char *p;
2789 f135c941 2020-02-20 stsp
2790 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2791 f135c941 2020-02-20 stsp if (err)
2792 f135c941 2020-02-20 stsp return err;
2793 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2794 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2795 bfd61697 2020-11-14 stsp p) == -1) {
2796 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2797 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2798 f135c941 2020-02-20 stsp }
2799 f135c941 2020-02-20 stsp free(p);
2800 f135c941 2020-02-20 stsp } else
2801 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2802 f135c941 2020-02-20 stsp
2803 f135c941 2020-02-20 stsp return err;
2804 f135c941 2020-02-20 stsp }
2805 f135c941 2020-02-20 stsp
2806 f135c941 2020-02-20 stsp static const struct got_error *
2807 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2808 9f7d7167 2018-04-29 stsp {
2809 80ddbec8 2018-04-29 stsp const struct got_error *error;
2810 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2811 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2812 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2813 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2814 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2815 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2816 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2817 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2818 04cc582a 2018-08-01 stsp struct tog_view *view;
2819 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2820 80ddbec8 2018-04-29 stsp
2821 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2822 80ddbec8 2018-04-29 stsp switch (ch) {
2823 b672a97a 2020-01-27 stsp case 'b':
2824 b672a97a 2020-01-27 stsp log_branches = 1;
2825 b672a97a 2020-01-27 stsp break;
2826 80ddbec8 2018-04-29 stsp case 'c':
2827 80ddbec8 2018-04-29 stsp start_commit = optarg;
2828 80ddbec8 2018-04-29 stsp break;
2829 ecb28ae0 2018-07-16 stsp case 'r':
2830 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2831 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2833 9ba1d308 2019-10-21 stsp optarg);
2834 ecb28ae0 2018-07-16 stsp break;
2835 80ddbec8 2018-04-29 stsp default:
2836 17020d27 2019-03-07 stsp usage_log();
2837 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2838 80ddbec8 2018-04-29 stsp }
2839 80ddbec8 2018-04-29 stsp }
2840 80ddbec8 2018-04-29 stsp
2841 80ddbec8 2018-04-29 stsp argc -= optind;
2842 80ddbec8 2018-04-29 stsp argv += optind;
2843 80ddbec8 2018-04-29 stsp
2844 f135c941 2020-02-20 stsp if (argc > 1)
2845 f135c941 2020-02-20 stsp usage_log();
2846 963f97a1 2019-03-18 stsp
2847 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2848 0ae84acc 2022-06-15 tracey if (error != NULL)
2849 0ae84acc 2022-06-15 tracey goto done;
2850 0ae84acc 2022-06-15 tracey
2851 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2852 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2853 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2854 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2855 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2856 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2857 c156c7a4 2020-12-18 stsp goto done;
2858 a1fbf39a 2019-08-11 stsp if (worktree)
2859 6962eb72 2020-02-20 stsp repo_path =
2860 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2861 a1fbf39a 2019-08-11 stsp else
2862 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2863 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2864 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2865 c156c7a4 2020-12-18 stsp goto done;
2866 c156c7a4 2020-12-18 stsp }
2867 ecb28ae0 2018-07-16 stsp }
2868 ecb28ae0 2018-07-16 stsp
2869 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2870 80ddbec8 2018-04-29 stsp if (error != NULL)
2871 ecb28ae0 2018-07-16 stsp goto done;
2872 80ddbec8 2018-04-29 stsp
2873 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2874 f135c941 2020-02-20 stsp repo, worktree);
2875 f135c941 2020-02-20 stsp if (error)
2876 f135c941 2020-02-20 stsp goto done;
2877 f135c941 2020-02-20 stsp
2878 f135c941 2020-02-20 stsp init_curses();
2879 f135c941 2020-02-20 stsp
2880 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2881 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2882 c02c541e 2019-03-29 stsp if (error)
2883 c02c541e 2019-03-29 stsp goto done;
2884 c02c541e 2019-03-29 stsp
2885 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2886 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2887 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
2888 87670572 2020-12-26 naddy if (error)
2889 87670572 2020-12-26 naddy goto done;
2890 87670572 2020-12-26 naddy }
2891 51a10b52 2020-12-26 stsp
2892 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2893 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2894 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2895 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2896 d8f38dc4 2020-12-05 stsp if (error)
2897 d8f38dc4 2020-12-05 stsp goto done;
2898 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2899 d8f38dc4 2020-12-05 stsp } else {
2900 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2901 d8f38dc4 2020-12-05 stsp if (error == NULL)
2902 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2903 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2904 d8f38dc4 2020-12-05 stsp goto done;
2905 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2906 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2907 d8f38dc4 2020-12-05 stsp if (error)
2908 d8f38dc4 2020-12-05 stsp goto done;
2909 d8f38dc4 2020-12-05 stsp }
2910 8b473291 2019-02-21 stsp
2911 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2912 04cc582a 2018-08-01 stsp if (view == NULL) {
2913 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2914 04cc582a 2018-08-01 stsp goto done;
2915 04cc582a 2018-08-01 stsp }
2916 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2917 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2918 ba4f502b 2018-08-04 stsp if (error)
2919 ba4f502b 2018-08-04 stsp goto done;
2920 2fc00ff4 2019-08-31 stsp if (worktree) {
2921 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2922 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2923 2fc00ff4 2019-08-31 stsp worktree = NULL;
2924 2fc00ff4 2019-08-31 stsp }
2925 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2926 ecb28ae0 2018-07-16 stsp done:
2927 f135c941 2020-02-20 stsp free(in_repo_path);
2928 ecb28ae0 2018-07-16 stsp free(repo_path);
2929 ecb28ae0 2018-07-16 stsp free(cwd);
2930 899d86c2 2018-05-10 stsp free(start_id);
2931 d8f38dc4 2020-12-05 stsp free(label);
2932 d8f38dc4 2020-12-05 stsp if (ref)
2933 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2934 1d0f4054 2021-06-17 stsp if (repo) {
2935 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2936 1d0f4054 2021-06-17 stsp if (error == NULL)
2937 1d0f4054 2021-06-17 stsp error = close_err;
2938 1d0f4054 2021-06-17 stsp }
2939 ec142235 2019-03-07 stsp if (worktree)
2940 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2941 0ae84acc 2022-06-15 tracey if (pack_fds) {
2942 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
2943 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
2944 0ae84acc 2022-06-15 tracey if (error == NULL)
2945 0ae84acc 2022-06-15 tracey error = pack_err;
2946 bb1c2cf3 2022-06-15 tracey pack_fds = NULL;
2947 0ae84acc 2022-06-15 tracey }
2948 51a10b52 2020-12-26 stsp tog_free_refs();
2949 80ddbec8 2018-04-29 stsp return error;
2950 9f7d7167 2018-04-29 stsp }
2951 9f7d7167 2018-04-29 stsp
2952 4ed7e80c 2018-05-20 stsp __dead static void
2953 9f7d7167 2018-04-29 stsp usage_diff(void)
2954 9f7d7167 2018-04-29 stsp {
2955 80ddbec8 2018-04-29 stsp endwin();
2956 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2957 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2958 9f7d7167 2018-04-29 stsp exit(1);
2959 b304db33 2018-05-20 stsp }
2960 b304db33 2018-05-20 stsp
2961 6d17833f 2019-11-08 stsp static int
2962 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2963 41605754 2020-11-12 stsp regmatch_t *regmatch)
2964 6d17833f 2019-11-08 stsp {
2965 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2966 6d17833f 2019-11-08 stsp }
2967 6d17833f 2019-11-08 stsp
2968 f26dddb7 2019-11-08 stsp struct tog_color *
2969 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2970 6d17833f 2019-11-08 stsp {
2971 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2972 6d17833f 2019-11-08 stsp
2973 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2974 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2975 6d17833f 2019-11-08 stsp return tc;
2976 6d17833f 2019-11-08 stsp }
2977 6d17833f 2019-11-08 stsp
2978 6d17833f 2019-11-08 stsp return NULL;
2979 6d17833f 2019-11-08 stsp }
2980 6d17833f 2019-11-08 stsp
2981 4ed7e80c 2018-05-20 stsp static const struct got_error *
2982 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2983 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2984 41605754 2020-11-12 stsp {
2985 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2986 41605754 2020-11-12 stsp wchar_t *wline;
2987 41605754 2020-11-12 stsp int width;
2988 41605754 2020-11-12 stsp char *s;
2989 41605754 2020-11-12 stsp
2990 41605754 2020-11-12 stsp *wtotal = 0;
2991 41605754 2020-11-12 stsp
2992 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2993 41605754 2020-11-12 stsp if (s == NULL)
2994 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2995 41605754 2020-11-12 stsp
2996 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2997 41605754 2020-11-12 stsp if (err) {
2998 41605754 2020-11-12 stsp free(s);
2999 41605754 2020-11-12 stsp return err;
3000 41605754 2020-11-12 stsp }
3001 41605754 2020-11-12 stsp waddwstr(window, wline);
3002 41605754 2020-11-12 stsp free(wline);
3003 41605754 2020-11-12 stsp free(s);
3004 41605754 2020-11-12 stsp wlimit -= width;
3005 41605754 2020-11-12 stsp *wtotal += width;
3006 41605754 2020-11-12 stsp
3007 41605754 2020-11-12 stsp if (wlimit > 0) {
3008 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
3009 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
3010 41605754 2020-11-12 stsp if (s == NULL) {
3011 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
3012 41605754 2020-11-12 stsp free(s);
3013 41605754 2020-11-12 stsp return err;
3014 41605754 2020-11-12 stsp }
3015 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3016 41605754 2020-11-12 stsp if (err) {
3017 41605754 2020-11-12 stsp free(s);
3018 41605754 2020-11-12 stsp return err;
3019 41605754 2020-11-12 stsp }
3020 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
3021 41605754 2020-11-12 stsp waddwstr(window, wline);
3022 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3023 41605754 2020-11-12 stsp free(wline);
3024 41605754 2020-11-12 stsp free(s);
3025 41605754 2020-11-12 stsp wlimit -= width;
3026 41605754 2020-11-12 stsp *wtotal += width;
3027 41605754 2020-11-12 stsp }
3028 41605754 2020-11-12 stsp
3029 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3030 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3031 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3032 41605754 2020-11-12 stsp if (err)
3033 41605754 2020-11-12 stsp return err;
3034 41605754 2020-11-12 stsp waddwstr(window, wline);
3035 41605754 2020-11-12 stsp free(wline);
3036 41605754 2020-11-12 stsp *wtotal += width;
3037 41605754 2020-11-12 stsp }
3038 41605754 2020-11-12 stsp
3039 41605754 2020-11-12 stsp return NULL;
3040 41605754 2020-11-12 stsp }
3041 41605754 2020-11-12 stsp
3042 41605754 2020-11-12 stsp static const struct got_error *
3043 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3044 26ed57b2 2018-05-19 stsp {
3045 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3046 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3047 61e69b96 2018-05-20 stsp const struct got_error *err;
3048 fe621944 2020-11-10 stsp int nprinted = 0;
3049 b304db33 2018-05-20 stsp char *line;
3050 826082fe 2020-12-10 stsp size_t linesize = 0;
3051 826082fe 2020-12-10 stsp ssize_t linelen;
3052 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3053 61e69b96 2018-05-20 stsp wchar_t *wline;
3054 e0b650dd 2018-05-20 stsp int width;
3055 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3056 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3057 fe621944 2020-11-10 stsp off_t line_offset;
3058 26ed57b2 2018-05-19 stsp
3059 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3060 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3061 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3062 fe621944 2020-11-10 stsp
3063 f7d12f7e 2018-08-01 stsp werase(view->window);
3064 a3404814 2018-09-02 stsp
3065 a3404814 2018-09-02 stsp if (header) {
3066 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3067 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3068 135a2da0 2020-11-11 stsp header) == -1)
3069 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3070 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3071 135a2da0 2020-11-11 stsp free(line);
3072 135a2da0 2020-11-11 stsp if (err)
3073 a3404814 2018-09-02 stsp return err;
3074 a3404814 2018-09-02 stsp
3075 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3076 a3404814 2018-09-02 stsp wstandout(view->window);
3077 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3078 e54cc94a 2020-11-11 stsp free(wline);
3079 e54cc94a 2020-11-11 stsp wline = NULL;
3080 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3081 a3404814 2018-09-02 stsp wstandend(view->window);
3082 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3083 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3084 26ed57b2 2018-05-19 stsp
3085 a3404814 2018-09-02 stsp if (max_lines <= 1)
3086 a3404814 2018-09-02 stsp return NULL;
3087 a3404814 2018-09-02 stsp max_lines--;
3088 a3404814 2018-09-02 stsp }
3089 a3404814 2018-09-02 stsp
3090 89f1a395 2020-12-01 naddy s->eof = 0;
3091 826082fe 2020-12-10 stsp line = NULL;
3092 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3093 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3094 826082fe 2020-12-10 stsp if (linelen == -1) {
3095 826082fe 2020-12-10 stsp if (feof(s->f)) {
3096 826082fe 2020-12-10 stsp s->eof = 1;
3097 826082fe 2020-12-10 stsp break;
3098 826082fe 2020-12-10 stsp }
3099 826082fe 2020-12-10 stsp free(line);
3100 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3101 61e69b96 2018-05-20 stsp }
3102 6d17833f 2019-11-08 stsp
3103 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3104 f26dddb7 2019-11-08 stsp if (tc)
3105 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3106 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3107 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3108 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3109 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3110 41605754 2020-11-12 stsp view->window, regmatch);
3111 41605754 2020-11-12 stsp if (err) {
3112 41605754 2020-11-12 stsp free(line);
3113 41605754 2020-11-12 stsp return err;
3114 41605754 2020-11-12 stsp }
3115 41605754 2020-11-12 stsp } else {
3116 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3117 41605754 2020-11-12 stsp if (err) {
3118 41605754 2020-11-12 stsp free(line);
3119 41605754 2020-11-12 stsp return err;
3120 41605754 2020-11-12 stsp }
3121 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3122 41605754 2020-11-12 stsp free(wline);
3123 41605754 2020-11-12 stsp wline = NULL;
3124 41605754 2020-11-12 stsp }
3125 f26dddb7 2019-11-08 stsp if (tc)
3126 6d17833f 2019-11-08 stsp wattr_off(view->window,
3127 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3128 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3129 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3130 fe621944 2020-11-10 stsp nprinted++;
3131 826082fe 2020-12-10 stsp }
3132 826082fe 2020-12-10 stsp free(line);
3133 fe621944 2020-11-10 stsp if (nprinted >= 1)
3134 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3135 89f1a395 2020-12-01 naddy (nprinted - 1);
3136 fe621944 2020-11-10 stsp else
3137 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3138 26ed57b2 2018-05-19 stsp
3139 1a57306a 2018-09-02 stsp view_vborder(view);
3140 c3e9aa98 2019-05-13 jcs
3141 89f1a395 2020-12-01 naddy if (s->eof) {
3142 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3143 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3144 c3e9aa98 2019-05-13 jcs nprinted++;
3145 c3e9aa98 2019-05-13 jcs }
3146 c3e9aa98 2019-05-13 jcs
3147 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3148 c3e9aa98 2019-05-13 jcs if (err) {
3149 c3e9aa98 2019-05-13 jcs return err;
3150 c3e9aa98 2019-05-13 jcs }
3151 26ed57b2 2018-05-19 stsp
3152 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3153 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3154 e54cc94a 2020-11-11 stsp free(wline);
3155 e54cc94a 2020-11-11 stsp wline = NULL;
3156 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3157 c3e9aa98 2019-05-13 jcs }
3158 c3e9aa98 2019-05-13 jcs
3159 26ed57b2 2018-05-19 stsp return NULL;
3160 abd2672a 2018-12-23 stsp }
3161 abd2672a 2018-12-23 stsp
3162 abd2672a 2018-12-23 stsp static char *
3163 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3164 abd2672a 2018-12-23 stsp {
3165 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3166 09867e48 2019-08-13 stsp char *p, *s;
3167 09867e48 2019-08-13 stsp
3168 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3169 09867e48 2019-08-13 stsp if (tm == NULL)
3170 09867e48 2019-08-13 stsp return NULL;
3171 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3172 09867e48 2019-08-13 stsp if (s == NULL)
3173 09867e48 2019-08-13 stsp return NULL;
3174 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3175 abd2672a 2018-12-23 stsp if (p)
3176 abd2672a 2018-12-23 stsp *p = '\0';
3177 abd2672a 2018-12-23 stsp return s;
3178 9f7d7167 2018-04-29 stsp }
3179 9f7d7167 2018-04-29 stsp
3180 4ed7e80c 2018-05-20 stsp static const struct got_error *
3181 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3182 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3183 0208f208 2020-05-05 stsp {
3184 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3185 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3186 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3187 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3188 0208f208 2020-05-05 stsp
3189 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3190 0208f208 2020-05-05 stsp if (qid != NULL) {
3191 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3192 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3193 d7b5a0e8 2022-04-20 stsp &qid->id);
3194 0208f208 2020-05-05 stsp if (err)
3195 0208f208 2020-05-05 stsp return err;
3196 0208f208 2020-05-05 stsp
3197 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3198 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3199 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3200 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3201 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3202 aa8b5dd0 2021-08-01 stsp }
3203 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3204 0208f208 2020-05-05 stsp
3205 0208f208 2020-05-05 stsp }
3206 0208f208 2020-05-05 stsp
3207 0208f208 2020-05-05 stsp if (tree_id1) {
3208 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3209 0208f208 2020-05-05 stsp if (err)
3210 0208f208 2020-05-05 stsp goto done;
3211 0208f208 2020-05-05 stsp }
3212 0208f208 2020-05-05 stsp
3213 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3214 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3215 0208f208 2020-05-05 stsp if (err)
3216 0208f208 2020-05-05 stsp goto done;
3217 0208f208 2020-05-05 stsp
3218 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3219 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3220 0208f208 2020-05-05 stsp done:
3221 0208f208 2020-05-05 stsp if (tree1)
3222 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3223 0208f208 2020-05-05 stsp if (tree2)
3224 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3225 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3226 0208f208 2020-05-05 stsp return err;
3227 0208f208 2020-05-05 stsp }
3228 0208f208 2020-05-05 stsp
3229 0208f208 2020-05-05 stsp static const struct got_error *
3230 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3231 abd2672a 2018-12-23 stsp {
3232 fe621944 2020-11-10 stsp off_t *p;
3233 fe621944 2020-11-10 stsp
3234 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3235 fe621944 2020-11-10 stsp if (p == NULL)
3236 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3237 fe621944 2020-11-10 stsp *line_offsets = p;
3238 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3239 fe621944 2020-11-10 stsp (*nlines)++;
3240 fe621944 2020-11-10 stsp return NULL;
3241 fe621944 2020-11-10 stsp }
3242 fe621944 2020-11-10 stsp
3243 fe621944 2020-11-10 stsp static const struct got_error *
3244 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3245 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3246 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3247 fe621944 2020-11-10 stsp {
3248 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3249 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3250 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3251 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3252 45d799e2 2018-12-23 stsp time_t committer_time;
3253 45d799e2 2018-12-23 stsp const char *author, *committer;
3254 8b473291 2019-02-21 stsp char *refs_str = NULL;
3255 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3256 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3257 fe621944 2020-11-10 stsp off_t outoff = 0;
3258 fe621944 2020-11-10 stsp int n;
3259 abd2672a 2018-12-23 stsp
3260 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3261 0208f208 2020-05-05 stsp
3262 8b473291 2019-02-21 stsp if (refs) {
3263 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3264 8b473291 2019-02-21 stsp if (err)
3265 8b473291 2019-02-21 stsp return err;
3266 8b473291 2019-02-21 stsp }
3267 8b473291 2019-02-21 stsp
3268 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3269 abd2672a 2018-12-23 stsp if (err)
3270 abd2672a 2018-12-23 stsp return err;
3271 abd2672a 2018-12-23 stsp
3272 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3273 15a94983 2018-12-23 stsp if (err) {
3274 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3275 15a94983 2018-12-23 stsp goto done;
3276 15a94983 2018-12-23 stsp }
3277 abd2672a 2018-12-23 stsp
3278 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3279 fe621944 2020-11-10 stsp if (err)
3280 fe621944 2020-11-10 stsp goto done;
3281 fe621944 2020-11-10 stsp
3282 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3283 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3284 fe621944 2020-11-10 stsp if (n < 0) {
3285 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3286 abd2672a 2018-12-23 stsp goto done;
3287 abd2672a 2018-12-23 stsp }
3288 fe621944 2020-11-10 stsp outoff += n;
3289 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3290 fe621944 2020-11-10 stsp if (err)
3291 fe621944 2020-11-10 stsp goto done;
3292 fe621944 2020-11-10 stsp
3293 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3294 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3295 fe621944 2020-11-10 stsp if (n < 0) {
3296 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3297 abd2672a 2018-12-23 stsp goto done;
3298 abd2672a 2018-12-23 stsp }
3299 fe621944 2020-11-10 stsp outoff += n;
3300 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3301 fe621944 2020-11-10 stsp if (err)
3302 fe621944 2020-11-10 stsp goto done;
3303 fe621944 2020-11-10 stsp
3304 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3305 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3306 fe621944 2020-11-10 stsp if (datestr) {
3307 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3308 fe621944 2020-11-10 stsp if (n < 0) {
3309 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3310 fe621944 2020-11-10 stsp goto done;
3311 fe621944 2020-11-10 stsp }
3312 fe621944 2020-11-10 stsp outoff += n;
3313 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3314 fe621944 2020-11-10 stsp if (err)
3315 fe621944 2020-11-10 stsp goto done;
3316 abd2672a 2018-12-23 stsp }
3317 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3318 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3319 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3320 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3321 fe621944 2020-11-10 stsp if (n < 0) {
3322 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3323 fe621944 2020-11-10 stsp goto done;
3324 fe621944 2020-11-10 stsp }
3325 fe621944 2020-11-10 stsp outoff += n;
3326 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3327 fe621944 2020-11-10 stsp if (err)
3328 fe621944 2020-11-10 stsp goto done;
3329 abd2672a 2018-12-23 stsp }
3330 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3331 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3332 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3333 9f98ca05 2021-09-24 stsp int pn = 1;
3334 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3335 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3336 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3337 9f98ca05 2021-09-24 stsp if (err)
3338 9f98ca05 2021-09-24 stsp goto done;
3339 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3340 9f98ca05 2021-09-24 stsp if (n < 0) {
3341 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3342 9f98ca05 2021-09-24 stsp goto done;
3343 9f98ca05 2021-09-24 stsp }
3344 9f98ca05 2021-09-24 stsp outoff += n;
3345 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3346 9f98ca05 2021-09-24 stsp if (err)
3347 9f98ca05 2021-09-24 stsp goto done;
3348 9f98ca05 2021-09-24 stsp free(id_str);
3349 9f98ca05 2021-09-24 stsp id_str = NULL;
3350 9f98ca05 2021-09-24 stsp }
3351 9f98ca05 2021-09-24 stsp }
3352 9f98ca05 2021-09-24 stsp
3353 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3354 5943eee2 2019-08-13 stsp if (err)
3355 5943eee2 2019-08-13 stsp goto done;
3356 fe621944 2020-11-10 stsp s = logmsg;
3357 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3358 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3359 fe621944 2020-11-10 stsp if (n < 0) {
3360 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3361 fe621944 2020-11-10 stsp goto done;
3362 fe621944 2020-11-10 stsp }
3363 fe621944 2020-11-10 stsp outoff += n;
3364 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3365 fe621944 2020-11-10 stsp if (err)
3366 fe621944 2020-11-10 stsp goto done;
3367 abd2672a 2018-12-23 stsp }
3368 fe621944 2020-11-10 stsp
3369 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3370 0208f208 2020-05-05 stsp if (err)
3371 0208f208 2020-05-05 stsp goto done;
3372 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3373 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3374 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3375 fe621944 2020-11-10 stsp if (n < 0) {
3376 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3377 fe621944 2020-11-10 stsp goto done;
3378 fe621944 2020-11-10 stsp }
3379 fe621944 2020-11-10 stsp outoff += n;
3380 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3381 fe621944 2020-11-10 stsp if (err)
3382 fe621944 2020-11-10 stsp goto done;
3383 0208f208 2020-05-05 stsp free((char *)pe->path);
3384 0208f208 2020-05-05 stsp free(pe->data);
3385 0208f208 2020-05-05 stsp }
3386 fe621944 2020-11-10 stsp
3387 0208f208 2020-05-05 stsp fputc('\n', outfile);
3388 fe621944 2020-11-10 stsp outoff++;
3389 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3390 abd2672a 2018-12-23 stsp done:
3391 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3392 abd2672a 2018-12-23 stsp free(id_str);
3393 5943eee2 2019-08-13 stsp free(logmsg);
3394 8b473291 2019-02-21 stsp free(refs_str);
3395 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3396 7510f233 2020-08-09 stsp if (err) {
3397 ae6a6978 2020-08-09 stsp free(*line_offsets);
3398 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3399 ae6a6978 2020-08-09 stsp *nlines = 0;
3400 7510f233 2020-08-09 stsp }
3401 fe621944 2020-11-10 stsp return err;
3402 abd2672a 2018-12-23 stsp }
3403 abd2672a 2018-12-23 stsp
3404 abd2672a 2018-12-23 stsp static const struct got_error *
3405 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3406 26ed57b2 2018-05-19 stsp {
3407 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3408 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3409 15a94983 2018-12-23 stsp int obj_type;
3410 fe621944 2020-11-10 stsp
3411 fe621944 2020-11-10 stsp free(s->line_offsets);
3412 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3413 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3414 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3415 fe621944 2020-11-10 stsp s->nlines = 0;
3416 26ed57b2 2018-05-19 stsp
3417 511a516b 2018-05-19 stsp f = got_opentemp();
3418 48ae06ee 2018-10-18 stsp if (f == NULL) {
3419 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3420 48ae06ee 2018-10-18 stsp goto done;
3421 48ae06ee 2018-10-18 stsp }
3422 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3423 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3424 fb43ecf1 2019-02-11 stsp goto done;
3425 fb43ecf1 2019-02-11 stsp }
3426 48ae06ee 2018-10-18 stsp s->f = f;
3427 26ed57b2 2018-05-19 stsp
3428 15a94983 2018-12-23 stsp if (s->id1)
3429 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3430 15a94983 2018-12-23 stsp else
3431 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3432 15a94983 2018-12-23 stsp if (err)
3433 15a94983 2018-12-23 stsp goto done;
3434 15a94983 2018-12-23 stsp
3435 15a94983 2018-12-23 stsp switch (obj_type) {
3436 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3437 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3438 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3439 b72706c3 2022-06-01 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3440 b72706c3 2022-06-01 stsp s->repo, s->f);
3441 26ed57b2 2018-05-19 stsp break;
3442 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3443 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3444 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3445 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3446 26ed57b2 2018-05-19 stsp break;
3447 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3448 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3449 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3450 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3451 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3452 abd2672a 2018-12-23 stsp
3453 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3454 abd2672a 2018-12-23 stsp if (err)
3455 3ffacbe1 2020-02-02 tracey goto done;
3456 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3457 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3458 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3459 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3460 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3461 f44b1f58 2020-02-02 tracey if (err)
3462 f44b1f58 2020-02-02 tracey goto done;
3463 f44b1f58 2020-02-02 tracey } else {
3464 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3465 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3466 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3467 fe621944 2020-11-10 stsp err = write_commit_info(
3468 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3469 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3470 f44b1f58 2020-02-02 tracey if (err)
3471 f44b1f58 2020-02-02 tracey goto done;
3472 f5404e4e 2020-02-02 tracey break;
3473 15a087fe 2019-02-21 stsp }
3474 abd2672a 2018-12-23 stsp }
3475 abd2672a 2018-12-23 stsp }
3476 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3477 abd2672a 2018-12-23 stsp
3478 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3479 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3480 b72706c3 2022-06-01 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3481 26ed57b2 2018-05-19 stsp break;
3482 abd2672a 2018-12-23 stsp }
3483 26ed57b2 2018-05-19 stsp default:
3484 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3485 48ae06ee 2018-10-18 stsp break;
3486 26ed57b2 2018-05-19 stsp }
3487 f44b1f58 2020-02-02 tracey if (err)
3488 f44b1f58 2020-02-02 tracey goto done;
3489 48ae06ee 2018-10-18 stsp done:
3490 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3491 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3492 48ae06ee 2018-10-18 stsp return err;
3493 48ae06ee 2018-10-18 stsp }
3494 26ed57b2 2018-05-19 stsp
3495 f5215bb9 2019-02-22 stsp static void
3496 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3497 f5215bb9 2019-02-22 stsp {
3498 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3499 f5215bb9 2019-02-22 stsp update_panels();
3500 f5215bb9 2019-02-22 stsp doupdate();
3501 f44b1f58 2020-02-02 tracey }
3502 f44b1f58 2020-02-02 tracey
3503 f44b1f58 2020-02-02 tracey static const struct got_error *
3504 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3505 f44b1f58 2020-02-02 tracey {
3506 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3507 f44b1f58 2020-02-02 tracey
3508 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3509 f44b1f58 2020-02-02 tracey return NULL;
3510 f44b1f58 2020-02-02 tracey }
3511 f44b1f58 2020-02-02 tracey
3512 f44b1f58 2020-02-02 tracey static const struct got_error *
3513 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3514 f44b1f58 2020-02-02 tracey {
3515 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3516 f44b1f58 2020-02-02 tracey int lineno;
3517 826082fe 2020-12-10 stsp char *line = NULL;
3518 826082fe 2020-12-10 stsp size_t linesize = 0;
3519 826082fe 2020-12-10 stsp ssize_t linelen;
3520 f44b1f58 2020-02-02 tracey
3521 f44b1f58 2020-02-02 tracey if (!view->searching) {
3522 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3523 f44b1f58 2020-02-02 tracey return NULL;
3524 f44b1f58 2020-02-02 tracey }
3525 f44b1f58 2020-02-02 tracey
3526 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3527 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3528 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3529 f44b1f58 2020-02-02 tracey else
3530 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3531 487cd7d2 2021-12-17 stsp } else
3532 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3533 f44b1f58 2020-02-02 tracey
3534 f44b1f58 2020-02-02 tracey while (1) {
3535 f44b1f58 2020-02-02 tracey off_t offset;
3536 f44b1f58 2020-02-02 tracey
3537 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3538 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3539 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3540 f44b1f58 2020-02-02 tracey break;
3541 f44b1f58 2020-02-02 tracey }
3542 f44b1f58 2020-02-02 tracey
3543 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3544 f44b1f58 2020-02-02 tracey lineno = 1;
3545 f44b1f58 2020-02-02 tracey else
3546 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3547 f44b1f58 2020-02-02 tracey }
3548 f44b1f58 2020-02-02 tracey
3549 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3550 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3551 f44b1f58 2020-02-02 tracey free(line);
3552 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3553 f44b1f58 2020-02-02 tracey }
3554 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3555 826082fe 2020-12-10 stsp if (linelen != -1 &&
3556 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3557 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3558 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3559 f44b1f58 2020-02-02 tracey break;
3560 f44b1f58 2020-02-02 tracey }
3561 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3562 f44b1f58 2020-02-02 tracey lineno++;
3563 f44b1f58 2020-02-02 tracey else
3564 f44b1f58 2020-02-02 tracey lineno--;
3565 f44b1f58 2020-02-02 tracey }
3566 826082fe 2020-12-10 stsp free(line);
3567 f44b1f58 2020-02-02 tracey
3568 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3569 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3570 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3571 f44b1f58 2020-02-02 tracey }
3572 f44b1f58 2020-02-02 tracey
3573 f44b1f58 2020-02-02 tracey return NULL;
3574 f5215bb9 2019-02-22 stsp }
3575 f5215bb9 2019-02-22 stsp
3576 48ae06ee 2018-10-18 stsp static const struct got_error *
3577 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
3578 b72706c3 2022-06-01 stsp {
3579 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
3580 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
3581 b72706c3 2022-06-01 stsp
3582 b72706c3 2022-06-01 stsp free(s->id1);
3583 b72706c3 2022-06-01 stsp s->id1 = NULL;
3584 b72706c3 2022-06-01 stsp free(s->id2);
3585 b72706c3 2022-06-01 stsp s->id2 = NULL;
3586 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
3587 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3588 b72706c3 2022-06-01 stsp s->f = NULL;
3589 b72706c3 2022-06-01 stsp if (s->f1 && fclose(s->f1) == EOF)
3590 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3591 b72706c3 2022-06-01 stsp s->f1 = NULL;
3592 b72706c3 2022-06-01 stsp if (s->f2 && fclose(s->f2) == EOF)
3593 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3594 b72706c3 2022-06-01 stsp s->f2 = NULL;
3595 b72706c3 2022-06-01 stsp free_colors(&s->colors);
3596 b72706c3 2022-06-01 stsp free(s->line_offsets);
3597 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
3598 b72706c3 2022-06-01 stsp s->nlines = 0;
3599 b72706c3 2022-06-01 stsp return err;
3600 b72706c3 2022-06-01 stsp }
3601 b72706c3 2022-06-01 stsp
3602 b72706c3 2022-06-01 stsp static const struct got_error *
3603 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3604 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3605 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3606 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3607 48ae06ee 2018-10-18 stsp {
3608 48ae06ee 2018-10-18 stsp const struct got_error *err;
3609 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3610 5dc9f4bc 2018-08-04 stsp
3611 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
3612 b72706c3 2022-06-01 stsp
3613 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3614 15a94983 2018-12-23 stsp int type1, type2;
3615 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3616 15a94983 2018-12-23 stsp if (err)
3617 15a94983 2018-12-23 stsp return err;
3618 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3619 15a94983 2018-12-23 stsp if (err)
3620 15a94983 2018-12-23 stsp return err;
3621 15a94983 2018-12-23 stsp
3622 15a94983 2018-12-23 stsp if (type1 != type2)
3623 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3624 15a94983 2018-12-23 stsp }
3625 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3626 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3627 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3628 f44b1f58 2020-02-02 tracey s->repo = repo;
3629 f44b1f58 2020-02-02 tracey s->id1 = id1;
3630 f44b1f58 2020-02-02 tracey s->id2 = id2;
3631 3dbaef42 2020-11-24 stsp s->label1 = label1;
3632 3dbaef42 2020-11-24 stsp s->label2 = label2;
3633 48ae06ee 2018-10-18 stsp
3634 15a94983 2018-12-23 stsp if (id1) {
3635 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3636 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3637 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3638 b72706c3 2022-06-01 stsp s->f1 = got_opentemp();
3639 b72706c3 2022-06-01 stsp if (s->f1 == NULL) {
3640 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3641 b72706c3 2022-06-01 stsp goto done;
3642 b72706c3 2022-06-01 stsp }
3643 48ae06ee 2018-10-18 stsp } else
3644 5465d566 2020-02-01 tracey s->id1 = NULL;
3645 48ae06ee 2018-10-18 stsp
3646 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3647 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3648 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
3649 b72706c3 2022-06-01 stsp goto done;
3650 48ae06ee 2018-10-18 stsp }
3651 b72706c3 2022-06-01 stsp
3652 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
3653 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
3654 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3655 b72706c3 2022-06-01 stsp goto done;
3656 b72706c3 2022-06-01 stsp }
3657 b72706c3 2022-06-01 stsp
3658 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3659 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3660 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3661 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3662 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3663 5465d566 2020-02-01 tracey s->log_view = log_view;
3664 5465d566 2020-02-01 tracey s->repo = repo;
3665 6d17833f 2019-11-08 stsp
3666 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3667 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3668 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3669 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3670 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3671 6d17833f 2019-11-08 stsp if (err)
3672 b72706c3 2022-06-01 stsp goto done;
3673 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3674 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3675 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3676 b72706c3 2022-06-01 stsp if (err)
3677 b72706c3 2022-06-01 stsp goto done;
3678 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3679 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3680 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3681 b72706c3 2022-06-01 stsp if (err)
3682 b72706c3 2022-06-01 stsp goto done;
3683 6d17833f 2019-11-08 stsp
3684 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3685 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3686 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3687 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3688 b72706c3 2022-06-01 stsp if (err)
3689 b72706c3 2022-06-01 stsp goto done;
3690 11b20872 2019-11-08 stsp
3691 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3692 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3693 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3694 b72706c3 2022-06-01 stsp if (err)
3695 b72706c3 2022-06-01 stsp goto done;
3696 11b20872 2019-11-08 stsp
3697 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3698 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3699 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3700 b72706c3 2022-06-01 stsp if (err)
3701 b72706c3 2022-06-01 stsp goto done;
3702 6d17833f 2019-11-08 stsp }
3703 5dc9f4bc 2018-08-04 stsp
3704 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3705 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3706 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3707 f5215bb9 2019-02-22 stsp
3708 5465d566 2020-02-01 tracey err = create_diff(s);
3709 48ae06ee 2018-10-18 stsp
3710 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3711 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3712 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3713 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3714 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3715 b72706c3 2022-06-01 stsp done:
3716 b72706c3 2022-06-01 stsp if (err)
3717 b72706c3 2022-06-01 stsp close_diff_view(view);
3718 e5a0f69f 2018-08-18 stsp return err;
3719 5dc9f4bc 2018-08-04 stsp }
3720 5dc9f4bc 2018-08-04 stsp
3721 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3722 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3723 5dc9f4bc 2018-08-04 stsp {
3724 a3404814 2018-09-02 stsp const struct got_error *err;
3725 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3726 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3727 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3728 a3404814 2018-09-02 stsp
3729 a3404814 2018-09-02 stsp if (s->id1) {
3730 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3731 a3404814 2018-09-02 stsp if (err)
3732 a3404814 2018-09-02 stsp return err;
3733 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3734 3dbaef42 2020-11-24 stsp } else
3735 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3736 3dbaef42 2020-11-24 stsp
3737 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3738 a3404814 2018-09-02 stsp if (err)
3739 a3404814 2018-09-02 stsp return err;
3740 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3741 26ed57b2 2018-05-19 stsp
3742 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3743 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3744 a3404814 2018-09-02 stsp free(id_str1);
3745 a3404814 2018-09-02 stsp free(id_str2);
3746 a3404814 2018-09-02 stsp return err;
3747 a3404814 2018-09-02 stsp }
3748 a3404814 2018-09-02 stsp free(id_str1);
3749 a3404814 2018-09-02 stsp free(id_str2);
3750 a3404814 2018-09-02 stsp
3751 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3752 267bb3b8 2021-08-01 stsp free(header);
3753 267bb3b8 2021-08-01 stsp return err;
3754 15a087fe 2019-02-21 stsp }
3755 15a087fe 2019-02-21 stsp
3756 15a087fe 2019-02-21 stsp static const struct got_error *
3757 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3758 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3759 15a087fe 2019-02-21 stsp {
3760 d7a04538 2019-02-21 stsp const struct got_error *err;
3761 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3762 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3763 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3764 15a087fe 2019-02-21 stsp
3765 15a087fe 2019-02-21 stsp free(s->id2);
3766 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3767 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3768 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3769 15a087fe 2019-02-21 stsp
3770 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3771 d7a04538 2019-02-21 stsp if (err)
3772 d7a04538 2019-02-21 stsp return err;
3773 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3774 15a087fe 2019-02-21 stsp free(s->id1);
3775 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3776 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3777 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3778 15a087fe 2019-02-21 stsp return NULL;
3779 0cf4efb1 2018-09-29 stsp }
3780 0cf4efb1 2018-09-29 stsp
3781 0cf4efb1 2018-09-29 stsp static const struct got_error *
3782 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3783 e5a0f69f 2018-08-18 stsp {
3784 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3785 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3786 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3787 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3788 826082fe 2020-12-10 stsp char *line = NULL;
3789 826082fe 2020-12-10 stsp size_t linesize = 0;
3790 826082fe 2020-12-10 stsp ssize_t linelen;
3791 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
3792 e5a0f69f 2018-08-18 stsp
3793 e5a0f69f 2018-08-18 stsp switch (ch) {
3794 64453f7e 2020-11-21 stsp case 'a':
3795 3dbaef42 2020-11-24 stsp case 'w':
3796 3dbaef42 2020-11-24 stsp if (ch == 'a')
3797 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3798 3dbaef42 2020-11-24 stsp if (ch == 'w')
3799 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3800 64453f7e 2020-11-21 stsp wclear(view->window);
3801 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3802 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3803 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3804 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3805 64453f7e 2020-11-21 stsp err = create_diff(s);
3806 912a3f79 2021-08-30 j break;
3807 912a3f79 2021-08-30 j case 'g':
3808 912a3f79 2021-08-30 j case KEY_HOME:
3809 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3810 64453f7e 2020-11-21 stsp break;
3811 912a3f79 2021-08-30 j case 'G':
3812 912a3f79 2021-08-30 j case KEY_END:
3813 912a3f79 2021-08-30 j if (s->eof)
3814 912a3f79 2021-08-30 j break;
3815 912a3f79 2021-08-30 j
3816 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3817 912a3f79 2021-08-30 j s->eof = 1;
3818 912a3f79 2021-08-30 j break;
3819 1e37a5c2 2019-05-12 jcs case 'k':
3820 1e37a5c2 2019-05-12 jcs case KEY_UP:
3821 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3822 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3823 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3824 1e37a5c2 2019-05-12 jcs break;
3825 83cc4199 2022-06-13 stsp case CTRL('u'):
3826 33c3719a 2022-06-15 stsp case 'u':
3827 83cc4199 2022-06-13 stsp nscroll /= 2;
3828 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3829 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3830 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3831 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3832 26ed57b2 2018-05-19 stsp break;
3833 1e37a5c2 2019-05-12 jcs i = 0;
3834 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
3835 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3836 1e37a5c2 2019-05-12 jcs break;
3837 1e37a5c2 2019-05-12 jcs case 'j':
3838 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3839 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3840 1e37a5c2 2019-05-12 jcs if (!s->eof)
3841 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3842 1e37a5c2 2019-05-12 jcs break;
3843 83cc4199 2022-06-13 stsp case CTRL('d'):
3844 33c3719a 2022-06-15 stsp case 'd':
3845 83cc4199 2022-06-13 stsp nscroll /= 2;
3846 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3847 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3848 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3849 1e37a5c2 2019-05-12 jcs case ' ':
3850 00ba99a7 2019-05-12 jcs if (s->eof)
3851 1e37a5c2 2019-05-12 jcs break;
3852 1e37a5c2 2019-05-12 jcs i = 0;
3853 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
3854 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3855 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3856 826082fe 2020-12-10 stsp if (linelen == -1) {
3857 826082fe 2020-12-10 stsp if (feof(s->f)) {
3858 826082fe 2020-12-10 stsp s->eof = 1;
3859 826082fe 2020-12-10 stsp } else
3860 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3861 34bc9ec9 2019-02-22 stsp break;
3862 826082fe 2020-12-10 stsp }
3863 1e37a5c2 2019-05-12 jcs }
3864 826082fe 2020-12-10 stsp free(line);
3865 1e37a5c2 2019-05-12 jcs break;
3866 1e37a5c2 2019-05-12 jcs case '[':
3867 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3868 1e37a5c2 2019-05-12 jcs s->diff_context--;
3869 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3870 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3871 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3872 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3873 27829c9e 2020-11-21 stsp s->nlines) {
3874 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3875 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3876 27829c9e 2020-11-21 stsp }
3877 1e37a5c2 2019-05-12 jcs }
3878 1e37a5c2 2019-05-12 jcs break;
3879 1e37a5c2 2019-05-12 jcs case ']':
3880 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3881 1e37a5c2 2019-05-12 jcs s->diff_context++;
3882 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3883 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3884 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3885 1e37a5c2 2019-05-12 jcs }
3886 1e37a5c2 2019-05-12 jcs break;
3887 1e37a5c2 2019-05-12 jcs case '<':
3888 1e37a5c2 2019-05-12 jcs case ',':
3889 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3890 48ae06ee 2018-10-18 stsp break;
3891 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3892 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3893 6524637e 2019-02-21 stsp
3894 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3895 1e37a5c2 2019-05-12 jcs if (err)
3896 1e37a5c2 2019-05-12 jcs break;
3897 15a087fe 2019-02-21 stsp
3898 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3899 5a8b5076 2020-12-05 stsp break;
3900 5a8b5076 2020-12-05 stsp
3901 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3902 1e37a5c2 2019-05-12 jcs if (err)
3903 1e37a5c2 2019-05-12 jcs break;
3904 15a087fe 2019-02-21 stsp
3905 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3906 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3907 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3908 15a087fe 2019-02-21 stsp
3909 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3910 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3911 1e37a5c2 2019-05-12 jcs break;
3912 1e37a5c2 2019-05-12 jcs case '>':
3913 1e37a5c2 2019-05-12 jcs case '.':
3914 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3915 15a087fe 2019-02-21 stsp break;
3916 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3917 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3918 5e224a3e 2019-02-22 stsp
3919 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3920 1e37a5c2 2019-05-12 jcs if (err)
3921 5a8b5076 2020-12-05 stsp break;
3922 5a8b5076 2020-12-05 stsp
3923 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3924 1e37a5c2 2019-05-12 jcs break;
3925 15a087fe 2019-02-21 stsp
3926 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3927 1e37a5c2 2019-05-12 jcs if (err)
3928 1e37a5c2 2019-05-12 jcs break;
3929 15a087fe 2019-02-21 stsp
3930 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3931 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3932 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3933 1e37a5c2 2019-05-12 jcs
3934 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3935 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3936 1e37a5c2 2019-05-12 jcs break;
3937 1e37a5c2 2019-05-12 jcs default:
3938 1e37a5c2 2019-05-12 jcs break;
3939 26ed57b2 2018-05-19 stsp }
3940 e5a0f69f 2018-08-18 stsp
3941 bcbd79e2 2018-08-19 stsp return err;
3942 26ed57b2 2018-05-19 stsp }
3943 26ed57b2 2018-05-19 stsp
3944 4ed7e80c 2018-05-20 stsp static const struct got_error *
3945 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3946 9f7d7167 2018-04-29 stsp {
3947 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3948 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3949 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3950 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3951 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3952 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3953 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3954 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3955 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3956 3dbaef42 2020-11-24 stsp const char *errstr;
3957 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3958 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3959 70ac5f84 2019-03-28 stsp
3960 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3961 26ed57b2 2018-05-19 stsp switch (ch) {
3962 64453f7e 2020-11-21 stsp case 'a':
3963 64453f7e 2020-11-21 stsp force_text_diff = 1;
3964 3dbaef42 2020-11-24 stsp break;
3965 3dbaef42 2020-11-24 stsp case 'C':
3966 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3967 3dbaef42 2020-11-24 stsp &errstr);
3968 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3969 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
3970 5a20d08d 2022-02-09 op errstr, errstr);
3971 64453f7e 2020-11-21 stsp break;
3972 09b5bff8 2020-02-23 naddy case 'r':
3973 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3974 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3975 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3976 09b5bff8 2020-02-23 naddy optarg);
3977 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3978 09b5bff8 2020-02-23 naddy break;
3979 3dbaef42 2020-11-24 stsp case 'w':
3980 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3981 3dbaef42 2020-11-24 stsp break;
3982 26ed57b2 2018-05-19 stsp default:
3983 17020d27 2019-03-07 stsp usage_diff();
3984 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3985 26ed57b2 2018-05-19 stsp }
3986 26ed57b2 2018-05-19 stsp }
3987 26ed57b2 2018-05-19 stsp
3988 26ed57b2 2018-05-19 stsp argc -= optind;
3989 26ed57b2 2018-05-19 stsp argv += optind;
3990 26ed57b2 2018-05-19 stsp
3991 26ed57b2 2018-05-19 stsp if (argc == 0) {
3992 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3993 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3994 15a94983 2018-12-23 stsp id_str1 = argv[0];
3995 15a94983 2018-12-23 stsp id_str2 = argv[1];
3996 26ed57b2 2018-05-19 stsp } else
3997 26ed57b2 2018-05-19 stsp usage_diff();
3998 eb6600df 2019-01-04 stsp
3999 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4000 0ae84acc 2022-06-15 tracey if (error)
4001 0ae84acc 2022-06-15 tracey goto done;
4002 0ae84acc 2022-06-15 tracey
4003 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4004 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4005 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4006 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4007 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4008 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4009 c156c7a4 2020-12-18 stsp goto done;
4010 a273ac94 2020-02-23 naddy if (worktree)
4011 a273ac94 2020-02-23 naddy repo_path =
4012 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4013 a273ac94 2020-02-23 naddy else
4014 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4015 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4016 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4017 c156c7a4 2020-12-18 stsp goto done;
4018 c156c7a4 2020-12-18 stsp }
4019 a273ac94 2020-02-23 naddy }
4020 a273ac94 2020-02-23 naddy
4021 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4022 eb6600df 2019-01-04 stsp if (error)
4023 eb6600df 2019-01-04 stsp goto done;
4024 26ed57b2 2018-05-19 stsp
4025 a273ac94 2020-02-23 naddy init_curses();
4026 a273ac94 2020-02-23 naddy
4027 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4028 51a10b52 2020-12-26 stsp if (error)
4029 51a10b52 2020-12-26 stsp goto done;
4030 51a10b52 2020-12-26 stsp
4031 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4032 26ed57b2 2018-05-19 stsp if (error)
4033 26ed57b2 2018-05-19 stsp goto done;
4034 26ed57b2 2018-05-19 stsp
4035 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4036 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4037 26ed57b2 2018-05-19 stsp if (error)
4038 26ed57b2 2018-05-19 stsp goto done;
4039 26ed57b2 2018-05-19 stsp
4040 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4041 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4042 26ed57b2 2018-05-19 stsp if (error)
4043 26ed57b2 2018-05-19 stsp goto done;
4044 26ed57b2 2018-05-19 stsp
4045 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4046 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4047 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4048 ea5e7bb5 2018-08-01 stsp goto done;
4049 ea5e7bb5 2018-08-01 stsp }
4050 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4051 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4052 5dc9f4bc 2018-08-04 stsp if (error)
4053 5dc9f4bc 2018-08-04 stsp goto done;
4054 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4055 26ed57b2 2018-05-19 stsp done:
4056 3dbaef42 2020-11-24 stsp free(label1);
4057 3dbaef42 2020-11-24 stsp free(label2);
4058 c02c541e 2019-03-29 stsp free(repo_path);
4059 a273ac94 2020-02-23 naddy free(cwd);
4060 1d0f4054 2021-06-17 stsp if (repo) {
4061 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4062 1d0f4054 2021-06-17 stsp if (error == NULL)
4063 1d0f4054 2021-06-17 stsp error = close_err;
4064 1d0f4054 2021-06-17 stsp }
4065 a273ac94 2020-02-23 naddy if (worktree)
4066 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4067 0ae84acc 2022-06-15 tracey if (pack_fds) {
4068 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4069 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4070 0ae84acc 2022-06-15 tracey if (error == NULL)
4071 0ae84acc 2022-06-15 tracey error = pack_err;
4072 bb1c2cf3 2022-06-15 tracey pack_fds = NULL;
4073 0ae84acc 2022-06-15 tracey }
4074 51a10b52 2020-12-26 stsp tog_free_refs();
4075 26ed57b2 2018-05-19 stsp return error;
4076 9f7d7167 2018-04-29 stsp }
4077 9f7d7167 2018-04-29 stsp
4078 4ed7e80c 2018-05-20 stsp __dead static void
4079 9f7d7167 2018-04-29 stsp usage_blame(void)
4080 9f7d7167 2018-04-29 stsp {
4081 80ddbec8 2018-04-29 stsp endwin();
4082 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4083 9f7d7167 2018-04-29 stsp getprogname());
4084 9f7d7167 2018-04-29 stsp exit(1);
4085 9f7d7167 2018-04-29 stsp }
4086 84451b3e 2018-07-10 stsp
4087 84451b3e 2018-07-10 stsp struct tog_blame_line {
4088 84451b3e 2018-07-10 stsp int annotated;
4089 84451b3e 2018-07-10 stsp struct got_object_id *id;
4090 84451b3e 2018-07-10 stsp };
4091 9f7d7167 2018-04-29 stsp
4092 4ed7e80c 2018-05-20 stsp static const struct got_error *
4093 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4094 84451b3e 2018-07-10 stsp {
4095 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4096 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4097 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4098 84451b3e 2018-07-10 stsp const struct got_error *err;
4099 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4100 826082fe 2020-12-10 stsp char *line = NULL;
4101 826082fe 2020-12-10 stsp size_t linesize = 0;
4102 826082fe 2020-12-10 stsp ssize_t linelen;
4103 84451b3e 2018-07-10 stsp wchar_t *wline;
4104 27a741e5 2019-09-11 stsp int width;
4105 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4106 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4107 ab089a2a 2018-07-12 stsp char *id_str;
4108 11b20872 2019-11-08 stsp struct tog_color *tc;
4109 ab089a2a 2018-07-12 stsp
4110 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4111 ab089a2a 2018-07-12 stsp if (err)
4112 ab089a2a 2018-07-12 stsp return err;
4113 84451b3e 2018-07-10 stsp
4114 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4115 f7d12f7e 2018-08-01 stsp werase(view->window);
4116 84451b3e 2018-07-10 stsp
4117 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4118 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4119 ab089a2a 2018-07-12 stsp free(id_str);
4120 ab089a2a 2018-07-12 stsp return err;
4121 ab089a2a 2018-07-12 stsp }
4122 ab089a2a 2018-07-12 stsp
4123 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4124 ab089a2a 2018-07-12 stsp free(line);
4125 2550e4c3 2018-07-13 stsp line = NULL;
4126 1cae65b4 2019-09-22 stsp if (err)
4127 1cae65b4 2019-09-22 stsp return err;
4128 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4129 a3404814 2018-09-02 stsp wstandout(view->window);
4130 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4131 11b20872 2019-11-08 stsp if (tc)
4132 11b20872 2019-11-08 stsp wattr_on(view->window,
4133 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4134 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4135 11b20872 2019-11-08 stsp if (tc)
4136 11b20872 2019-11-08 stsp wattr_off(view->window,
4137 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4138 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4139 a3404814 2018-09-02 stsp wstandend(view->window);
4140 2550e4c3 2018-07-13 stsp free(wline);
4141 2550e4c3 2018-07-13 stsp wline = NULL;
4142 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4143 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4144 ab089a2a 2018-07-12 stsp
4145 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4146 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4147 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4148 ab089a2a 2018-07-12 stsp free(id_str);
4149 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4150 ab089a2a 2018-07-12 stsp }
4151 ab089a2a 2018-07-12 stsp free(id_str);
4152 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4153 3f60a8ef 2018-07-10 stsp free(line);
4154 2550e4c3 2018-07-13 stsp line = NULL;
4155 3f60a8ef 2018-07-10 stsp if (err)
4156 3f60a8ef 2018-07-10 stsp return err;
4157 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4158 2550e4c3 2018-07-13 stsp free(wline);
4159 2550e4c3 2018-07-13 stsp wline = NULL;
4160 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4161 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4162 3f60a8ef 2018-07-10 stsp
4163 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4164 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4165 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4166 826082fe 2020-12-10 stsp if (linelen == -1) {
4167 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4168 826082fe 2020-12-10 stsp s->eof = 1;
4169 826082fe 2020-12-10 stsp break;
4170 826082fe 2020-12-10 stsp }
4171 84451b3e 2018-07-10 stsp free(line);
4172 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4173 84451b3e 2018-07-10 stsp }
4174 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4175 826082fe 2020-12-10 stsp continue;
4176 84451b3e 2018-07-10 stsp
4177 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4178 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4179 b700b5d6 2018-07-10 stsp
4180 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4181 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4182 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4183 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4184 27a741e5 2019-09-11 stsp !(view->focussed &&
4185 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4186 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4187 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4188 8d0fe45a 2019-08-12 stsp char *id_str;
4189 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4190 8d0fe45a 2019-08-12 stsp if (err) {
4191 8d0fe45a 2019-08-12 stsp free(line);
4192 8d0fe45a 2019-08-12 stsp return err;
4193 8d0fe45a 2019-08-12 stsp }
4194 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4195 11b20872 2019-11-08 stsp if (tc)
4196 11b20872 2019-11-08 stsp wattr_on(view->window,
4197 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4198 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4199 11b20872 2019-11-08 stsp if (tc)
4200 11b20872 2019-11-08 stsp wattr_off(view->window,
4201 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4202 8d0fe45a 2019-08-12 stsp free(id_str);
4203 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4204 8d0fe45a 2019-08-12 stsp } else {
4205 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4206 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4207 84451b3e 2018-07-10 stsp }
4208 ee41ec32 2018-07-10 stsp } else {
4209 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4210 ee41ec32 2018-07-10 stsp prev_id = NULL;
4211 ee41ec32 2018-07-10 stsp }
4212 84451b3e 2018-07-10 stsp
4213 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4214 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4215 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4216 27a741e5 2019-09-11 stsp
4217 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4218 41605754 2020-11-12 stsp width = 9;
4219 41605754 2020-11-12 stsp wline = wcsdup(L"");
4220 41605754 2020-11-12 stsp if (wline == NULL) {
4221 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4222 41605754 2020-11-12 stsp free(line);
4223 41605754 2020-11-12 stsp return err;
4224 41605754 2020-11-12 stsp }
4225 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4226 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4227 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4228 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4229 41605754 2020-11-12 stsp view->window, regmatch);
4230 41605754 2020-11-12 stsp if (err) {
4231 41605754 2020-11-12 stsp free(line);
4232 41605754 2020-11-12 stsp return err;
4233 41605754 2020-11-12 stsp }
4234 41605754 2020-11-12 stsp width += 9;
4235 41605754 2020-11-12 stsp } else {
4236 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4237 41605754 2020-11-12 stsp view->ncols - 9, 9);
4238 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4239 41605754 2020-11-12 stsp free(wline);
4240 41605754 2020-11-12 stsp wline = NULL;
4241 41605754 2020-11-12 stsp width += 9;
4242 41605754 2020-11-12 stsp }
4243 41605754 2020-11-12 stsp
4244 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4245 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4246 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4247 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4248 84451b3e 2018-07-10 stsp }
4249 826082fe 2020-12-10 stsp free(line);
4250 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4251 84451b3e 2018-07-10 stsp
4252 1a57306a 2018-09-02 stsp view_vborder(view);
4253 84451b3e 2018-07-10 stsp
4254 84451b3e 2018-07-10 stsp return NULL;
4255 84451b3e 2018-07-10 stsp }
4256 84451b3e 2018-07-10 stsp
4257 84451b3e 2018-07-10 stsp static const struct got_error *
4258 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4259 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4260 84451b3e 2018-07-10 stsp {
4261 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4262 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4263 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4264 1a76625f 2018-10-22 stsp int errcode;
4265 84451b3e 2018-07-10 stsp
4266 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4267 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4268 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4269 84451b3e 2018-07-10 stsp
4270 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4271 1a76625f 2018-10-22 stsp if (errcode)
4272 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4273 84451b3e 2018-07-10 stsp
4274 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4275 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4276 d68a0a7d 2018-07-10 stsp goto done;
4277 d68a0a7d 2018-07-10 stsp }
4278 d68a0a7d 2018-07-10 stsp
4279 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4280 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4281 d68a0a7d 2018-07-10 stsp
4282 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4283 d68a0a7d 2018-07-10 stsp if (line->annotated)
4284 d68a0a7d 2018-07-10 stsp goto done;
4285 d68a0a7d 2018-07-10 stsp
4286 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4287 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4288 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4289 84451b3e 2018-07-10 stsp goto done;
4290 84451b3e 2018-07-10 stsp }
4291 84451b3e 2018-07-10 stsp line->annotated = 1;
4292 84451b3e 2018-07-10 stsp done:
4293 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4294 1a76625f 2018-10-22 stsp if (errcode)
4295 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4296 84451b3e 2018-07-10 stsp return err;
4297 84451b3e 2018-07-10 stsp }
4298 84451b3e 2018-07-10 stsp
4299 84451b3e 2018-07-10 stsp static void *
4300 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4301 84451b3e 2018-07-10 stsp {
4302 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4303 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4304 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4305 1a76625f 2018-10-22 stsp int errcode;
4306 18430de3 2018-07-10 stsp
4307 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4308 61266923 2020-01-14 stsp if (err)
4309 61266923 2020-01-14 stsp return (void *)err;
4310 61266923 2020-01-14 stsp
4311 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4312 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4313 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4314 fc06ba56 2019-08-22 stsp err = NULL;
4315 18430de3 2018-07-10 stsp
4316 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4317 1a76625f 2018-10-22 stsp if (errcode)
4318 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4319 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4320 18430de3 2018-07-10 stsp
4321 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4322 1d0f4054 2021-06-17 stsp if (err == NULL)
4323 1d0f4054 2021-06-17 stsp err = close_err;
4324 c9beca56 2018-07-22 stsp ta->repo = NULL;
4325 c9beca56 2018-07-22 stsp *ta->complete = 1;
4326 18430de3 2018-07-10 stsp
4327 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4328 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4329 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4330 18430de3 2018-07-10 stsp
4331 18430de3 2018-07-10 stsp return (void *)err;
4332 84451b3e 2018-07-10 stsp }
4333 84451b3e 2018-07-10 stsp
4334 245d91c1 2018-07-12 stsp static struct got_object_id *
4335 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4336 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4337 245d91c1 2018-07-12 stsp {
4338 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4339 8d0fe45a 2019-08-12 stsp
4340 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4341 8d0fe45a 2019-08-12 stsp return NULL;
4342 b880a918 2018-07-10 stsp
4343 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4344 245d91c1 2018-07-12 stsp if (!line->annotated)
4345 245d91c1 2018-07-12 stsp return NULL;
4346 245d91c1 2018-07-12 stsp
4347 245d91c1 2018-07-12 stsp return line->id;
4348 b880a918 2018-07-10 stsp }
4349 245d91c1 2018-07-12 stsp
4350 b880a918 2018-07-10 stsp static const struct got_error *
4351 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4352 a70480e0 2018-06-23 stsp {
4353 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4354 245d91c1 2018-07-12 stsp int i;
4355 245d91c1 2018-07-12 stsp
4356 245d91c1 2018-07-12 stsp if (blame->thread) {
4357 1a76625f 2018-10-22 stsp int errcode;
4358 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4359 1a76625f 2018-10-22 stsp if (errcode)
4360 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4361 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4362 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4363 1a76625f 2018-10-22 stsp if (errcode)
4364 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4365 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4366 1a76625f 2018-10-22 stsp if (errcode)
4367 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4368 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4369 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4370 245d91c1 2018-07-12 stsp err = NULL;
4371 245d91c1 2018-07-12 stsp blame->thread = NULL;
4372 245d91c1 2018-07-12 stsp }
4373 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4374 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4375 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4376 1d0f4054 2021-06-17 stsp if (err == NULL)
4377 1d0f4054 2021-06-17 stsp err = close_err;
4378 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4379 245d91c1 2018-07-12 stsp }
4380 245d91c1 2018-07-12 stsp if (blame->f) {
4381 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4382 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4383 245d91c1 2018-07-12 stsp blame->f = NULL;
4384 245d91c1 2018-07-12 stsp }
4385 57670559 2018-12-23 stsp if (blame->lines) {
4386 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4387 57670559 2018-12-23 stsp free(blame->lines[i].id);
4388 57670559 2018-12-23 stsp free(blame->lines);
4389 57670559 2018-12-23 stsp blame->lines = NULL;
4390 57670559 2018-12-23 stsp }
4391 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4392 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4393 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
4394 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4395 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
4396 0ae84acc 2022-06-15 tracey if (err == NULL)
4397 0ae84acc 2022-06-15 tracey err = pack_err;
4398 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
4399 0ae84acc 2022-06-15 tracey }
4400 245d91c1 2018-07-12 stsp return err;
4401 245d91c1 2018-07-12 stsp }
4402 245d91c1 2018-07-12 stsp
4403 245d91c1 2018-07-12 stsp static const struct got_error *
4404 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4405 fc06ba56 2019-08-22 stsp {
4406 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4407 fc06ba56 2019-08-22 stsp int *done = arg;
4408 fc06ba56 2019-08-22 stsp int errcode;
4409 fc06ba56 2019-08-22 stsp
4410 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4411 fc06ba56 2019-08-22 stsp if (errcode)
4412 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4413 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4414 fc06ba56 2019-08-22 stsp
4415 fc06ba56 2019-08-22 stsp if (*done)
4416 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4417 fc06ba56 2019-08-22 stsp
4418 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4419 fc06ba56 2019-08-22 stsp if (errcode)
4420 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4421 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4422 fc06ba56 2019-08-22 stsp
4423 fc06ba56 2019-08-22 stsp return err;
4424 fc06ba56 2019-08-22 stsp }
4425 fc06ba56 2019-08-22 stsp
4426 fc06ba56 2019-08-22 stsp static const struct got_error *
4427 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4428 245d91c1 2018-07-12 stsp {
4429 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4430 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4431 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4432 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4433 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4434 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4435 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4436 15a94983 2018-12-23 stsp int obj_type;
4437 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4438 a70480e0 2018-06-23 stsp
4439 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4440 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4441 27d434c2 2018-09-15 stsp if (err)
4442 15a94983 2018-12-23 stsp return err;
4443 a44927cc 2022-04-07 stsp
4444 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4445 a44927cc 2022-04-07 stsp if (err)
4446 a44927cc 2022-04-07 stsp goto done;
4447 27d434c2 2018-09-15 stsp
4448 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4449 84451b3e 2018-07-10 stsp if (err)
4450 84451b3e 2018-07-10 stsp goto done;
4451 27d434c2 2018-09-15 stsp
4452 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4453 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4454 84451b3e 2018-07-10 stsp goto done;
4455 84451b3e 2018-07-10 stsp }
4456 a70480e0 2018-06-23 stsp
4457 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4458 a70480e0 2018-06-23 stsp if (err)
4459 a70480e0 2018-06-23 stsp goto done;
4460 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4461 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4462 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4463 84451b3e 2018-07-10 stsp goto done;
4464 84451b3e 2018-07-10 stsp }
4465 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4466 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4467 1fddf795 2021-01-20 stsp if (err)
4468 1fddf795 2021-01-20 stsp goto done;
4469 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4470 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4471 84451b3e 2018-07-10 stsp goto done;
4472 1fddf795 2021-01-20 stsp }
4473 b02560ec 2019-08-19 stsp
4474 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4475 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4476 b02560ec 2019-08-19 stsp blame->nlines--;
4477 a70480e0 2018-06-23 stsp
4478 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4479 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4480 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4481 84451b3e 2018-07-10 stsp goto done;
4482 84451b3e 2018-07-10 stsp }
4483 a70480e0 2018-06-23 stsp
4484 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
4485 bd24772e 2018-07-11 stsp if (err)
4486 bd24772e 2018-07-11 stsp goto done;
4487 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4488 0ae84acc 2022-06-15 tracey pack_fds);
4489 0ae84acc 2022-06-15 tracey if (err)
4490 0ae84acc 2022-06-15 tracey goto done;
4491 bd24772e 2018-07-11 stsp
4492 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
4493 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4494 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4495 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4496 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4497 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4498 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4499 245d91c1 2018-07-12 stsp goto done;
4500 245d91c1 2018-07-12 stsp }
4501 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4502 245d91c1 2018-07-12 stsp
4503 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4504 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4505 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4506 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4507 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4508 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4509 a5388363 2020-12-01 naddy s->blame_complete = 0;
4510 f5a09613 2020-12-13 naddy
4511 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4512 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4513 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4514 f5a09613 2020-12-13 naddy s->selected_line = 1;
4515 f5a09613 2020-12-13 naddy }
4516 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4517 245d91c1 2018-07-12 stsp
4518 245d91c1 2018-07-12 stsp done:
4519 a44927cc 2022-04-07 stsp if (commit)
4520 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4521 245d91c1 2018-07-12 stsp if (blob)
4522 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4523 27d434c2 2018-09-15 stsp free(obj_id);
4524 245d91c1 2018-07-12 stsp if (err)
4525 245d91c1 2018-07-12 stsp stop_blame(blame);
4526 245d91c1 2018-07-12 stsp return err;
4527 245d91c1 2018-07-12 stsp }
4528 245d91c1 2018-07-12 stsp
4529 245d91c1 2018-07-12 stsp static const struct got_error *
4530 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4531 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4532 245d91c1 2018-07-12 stsp {
4533 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4534 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4535 dbc6a6b6 2018-07-12 stsp
4536 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4537 245d91c1 2018-07-12 stsp
4538 c4843652 2019-08-12 stsp s->path = strdup(path);
4539 c4843652 2019-08-12 stsp if (s->path == NULL)
4540 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4541 c4843652 2019-08-12 stsp
4542 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4543 c4843652 2019-08-12 stsp if (err) {
4544 c4843652 2019-08-12 stsp free(s->path);
4545 7cbe629d 2018-08-04 stsp return err;
4546 c4843652 2019-08-12 stsp }
4547 245d91c1 2018-07-12 stsp
4548 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4549 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4550 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4551 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4552 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4553 fb2756b9 2018-08-04 stsp s->repo = repo;
4554 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4555 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4556 7cbe629d 2018-08-04 stsp
4557 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4558 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4559 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4560 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4561 11b20872 2019-11-08 stsp if (err)
4562 11b20872 2019-11-08 stsp return err;
4563 11b20872 2019-11-08 stsp }
4564 11b20872 2019-11-08 stsp
4565 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4566 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4567 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4568 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4569 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4570 e5a0f69f 2018-08-18 stsp
4571 a5388363 2020-12-01 naddy return run_blame(view);
4572 7cbe629d 2018-08-04 stsp }
4573 7cbe629d 2018-08-04 stsp
4574 e5a0f69f 2018-08-18 stsp static const struct got_error *
4575 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4576 7cbe629d 2018-08-04 stsp {
4577 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4578 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4579 7cbe629d 2018-08-04 stsp
4580 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4581 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4582 e5a0f69f 2018-08-18 stsp
4583 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4584 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4585 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4586 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4587 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4588 7cbe629d 2018-08-04 stsp }
4589 e5a0f69f 2018-08-18 stsp
4590 e5a0f69f 2018-08-18 stsp free(s->path);
4591 11b20872 2019-11-08 stsp free_colors(&s->colors);
4592 e5a0f69f 2018-08-18 stsp return err;
4593 7cbe629d 2018-08-04 stsp }
4594 7cbe629d 2018-08-04 stsp
4595 7cbe629d 2018-08-04 stsp static const struct got_error *
4596 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4597 6c4c42e0 2019-06-24 stsp {
4598 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4599 6c4c42e0 2019-06-24 stsp
4600 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4601 6c4c42e0 2019-06-24 stsp return NULL;
4602 6c4c42e0 2019-06-24 stsp }
4603 6c4c42e0 2019-06-24 stsp
4604 6c4c42e0 2019-06-24 stsp static const struct got_error *
4605 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4606 6c4c42e0 2019-06-24 stsp {
4607 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4608 6c4c42e0 2019-06-24 stsp int lineno;
4609 826082fe 2020-12-10 stsp char *line = NULL;
4610 826082fe 2020-12-10 stsp size_t linesize = 0;
4611 826082fe 2020-12-10 stsp ssize_t linelen;
4612 6c4c42e0 2019-06-24 stsp
4613 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4614 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4615 6c4c42e0 2019-06-24 stsp return NULL;
4616 6c4c42e0 2019-06-24 stsp }
4617 6c4c42e0 2019-06-24 stsp
4618 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4619 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4620 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4621 6c4c42e0 2019-06-24 stsp else
4622 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4623 487cd7d2 2021-12-17 stsp } else
4624 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4625 6c4c42e0 2019-06-24 stsp
4626 6c4c42e0 2019-06-24 stsp while (1) {
4627 6c4c42e0 2019-06-24 stsp off_t offset;
4628 6c4c42e0 2019-06-24 stsp
4629 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4630 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4631 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4632 6c4c42e0 2019-06-24 stsp break;
4633 6c4c42e0 2019-06-24 stsp }
4634 2246482e 2019-06-25 stsp
4635 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4636 6c4c42e0 2019-06-24 stsp lineno = 1;
4637 6c4c42e0 2019-06-24 stsp else
4638 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4639 6c4c42e0 2019-06-24 stsp }
4640 6c4c42e0 2019-06-24 stsp
4641 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4642 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4643 6c4c42e0 2019-06-24 stsp free(line);
4644 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4645 6c4c42e0 2019-06-24 stsp }
4646 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4647 826082fe 2020-12-10 stsp if (linelen != -1 &&
4648 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4649 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4650 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4651 6c4c42e0 2019-06-24 stsp break;
4652 6c4c42e0 2019-06-24 stsp }
4653 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4654 6c4c42e0 2019-06-24 stsp lineno++;
4655 6c4c42e0 2019-06-24 stsp else
4656 6c4c42e0 2019-06-24 stsp lineno--;
4657 6c4c42e0 2019-06-24 stsp }
4658 826082fe 2020-12-10 stsp free(line);
4659 6c4c42e0 2019-06-24 stsp
4660 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4661 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4662 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4663 6c4c42e0 2019-06-24 stsp }
4664 6c4c42e0 2019-06-24 stsp
4665 6c4c42e0 2019-06-24 stsp return NULL;
4666 6c4c42e0 2019-06-24 stsp }
4667 6c4c42e0 2019-06-24 stsp
4668 6c4c42e0 2019-06-24 stsp static const struct got_error *
4669 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4670 7cbe629d 2018-08-04 stsp {
4671 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4672 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4673 2b380cc8 2018-10-24 stsp int errcode;
4674 2b380cc8 2018-10-24 stsp
4675 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4676 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4677 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4678 2b380cc8 2018-10-24 stsp if (errcode)
4679 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4680 51fe7530 2019-08-19 stsp
4681 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4682 2b380cc8 2018-10-24 stsp }
4683 e5a0f69f 2018-08-18 stsp
4684 51fe7530 2019-08-19 stsp if (s->blame_complete)
4685 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4686 51fe7530 2019-08-19 stsp
4687 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4688 e5a0f69f 2018-08-18 stsp
4689 669b5ffa 2018-10-07 stsp view_vborder(view);
4690 e5a0f69f 2018-08-18 stsp return err;
4691 e5a0f69f 2018-08-18 stsp }
4692 e5a0f69f 2018-08-18 stsp
4693 e5a0f69f 2018-08-18 stsp static const struct got_error *
4694 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 e5a0f69f 2018-08-18 stsp {
4696 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4697 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4698 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4699 83cc4199 2022-06-13 stsp int begin_x = 0, nscroll = view->nlines - 2;
4700 7cbe629d 2018-08-04 stsp
4701 e5a0f69f 2018-08-18 stsp switch (ch) {
4702 1e37a5c2 2019-05-12 jcs case 'q':
4703 1e37a5c2 2019-05-12 jcs s->done = 1;
4704 4deef56f 2021-09-02 naddy break;
4705 4deef56f 2021-09-02 naddy case 'g':
4706 4deef56f 2021-09-02 naddy case KEY_HOME:
4707 4deef56f 2021-09-02 naddy s->selected_line = 1;
4708 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4709 4deef56f 2021-09-02 naddy break;
4710 4deef56f 2021-09-02 naddy case 'G':
4711 4deef56f 2021-09-02 naddy case KEY_END:
4712 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4713 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4714 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4715 4deef56f 2021-09-02 naddy } else {
4716 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4717 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4718 4deef56f 2021-09-02 naddy (view->nlines - 3);
4719 4deef56f 2021-09-02 naddy }
4720 1e37a5c2 2019-05-12 jcs break;
4721 1e37a5c2 2019-05-12 jcs case 'k':
4722 1e37a5c2 2019-05-12 jcs case KEY_UP:
4723 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4724 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4725 1e37a5c2 2019-05-12 jcs s->selected_line--;
4726 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4727 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4728 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4729 1e37a5c2 2019-05-12 jcs break;
4730 83cc4199 2022-06-13 stsp case CTRL('u'):
4731 33c3719a 2022-06-15 stsp case 'u':
4732 83cc4199 2022-06-13 stsp nscroll /= 2;
4733 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4734 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4735 ea025d1d 2020-02-22 naddy case CTRL('b'):
4736 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4737 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
4738 e5a0f69f 2018-08-18 stsp break;
4739 1e37a5c2 2019-05-12 jcs }
4740 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
4741 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
4742 1e37a5c2 2019-05-12 jcs else
4743 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4744 1e37a5c2 2019-05-12 jcs break;
4745 1e37a5c2 2019-05-12 jcs case 'j':
4746 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4747 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4748 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4749 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4750 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4751 1e37a5c2 2019-05-12 jcs s->selected_line++;
4752 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4753 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4754 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4755 1e37a5c2 2019-05-12 jcs break;
4756 1e37a5c2 2019-05-12 jcs case 'b':
4757 1e37a5c2 2019-05-12 jcs case 'p': {
4758 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4759 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4760 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4761 1e37a5c2 2019-05-12 jcs if (id == NULL)
4762 e5a0f69f 2018-08-18 stsp break;
4763 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4764 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
4765 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4766 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4767 1e37a5c2 2019-05-12 jcs int obj_type;
4768 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4769 1e37a5c2 2019-05-12 jcs s->repo, id);
4770 e5a0f69f 2018-08-18 stsp if (err)
4771 e5a0f69f 2018-08-18 stsp break;
4772 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4773 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4774 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4775 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4776 e5a0f69f 2018-08-18 stsp break;
4777 e5a0f69f 2018-08-18 stsp }
4778 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4779 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
4780 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
4781 a44927cc 2022-04-07 stsp if (err)
4782 a44927cc 2022-04-07 stsp break;
4783 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4784 a44927cc 2022-04-07 stsp pcommit, s->path);
4785 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
4786 e5a0f69f 2018-08-18 stsp if (err) {
4787 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4788 1e37a5c2 2019-05-12 jcs err = NULL;
4789 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4790 e5a0f69f 2018-08-18 stsp break;
4791 e5a0f69f 2018-08-18 stsp }
4792 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4793 1e37a5c2 2019-05-12 jcs blob_id);
4794 1e37a5c2 2019-05-12 jcs free(blob_id);
4795 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4796 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4797 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4798 e5a0f69f 2018-08-18 stsp break;
4799 1e37a5c2 2019-05-12 jcs }
4800 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4801 d7b5a0e8 2022-04-20 stsp &pid->id);
4802 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4803 1e37a5c2 2019-05-12 jcs } else {
4804 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4805 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
4806 1e37a5c2 2019-05-12 jcs break;
4807 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4808 1e37a5c2 2019-05-12 jcs id);
4809 1e37a5c2 2019-05-12 jcs }
4810 1e37a5c2 2019-05-12 jcs if (err)
4811 e5a0f69f 2018-08-18 stsp break;
4812 1e37a5c2 2019-05-12 jcs s->done = 1;
4813 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4814 1e37a5c2 2019-05-12 jcs s->done = 0;
4815 1e37a5c2 2019-05-12 jcs if (thread_err)
4816 1e37a5c2 2019-05-12 jcs break;
4817 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4818 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4819 a5388363 2020-12-01 naddy err = run_blame(view);
4820 1e37a5c2 2019-05-12 jcs if (err)
4821 1e37a5c2 2019-05-12 jcs break;
4822 1e37a5c2 2019-05-12 jcs break;
4823 1e37a5c2 2019-05-12 jcs }
4824 1e37a5c2 2019-05-12 jcs case 'B': {
4825 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4826 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4827 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
4828 1e37a5c2 2019-05-12 jcs break;
4829 1e37a5c2 2019-05-12 jcs s->done = 1;
4830 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4831 1e37a5c2 2019-05-12 jcs s->done = 0;
4832 1e37a5c2 2019-05-12 jcs if (thread_err)
4833 1e37a5c2 2019-05-12 jcs break;
4834 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4835 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4836 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4837 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4838 a5388363 2020-12-01 naddy err = run_blame(view);
4839 1e37a5c2 2019-05-12 jcs if (err)
4840 1e37a5c2 2019-05-12 jcs break;
4841 1e37a5c2 2019-05-12 jcs break;
4842 1e37a5c2 2019-05-12 jcs }
4843 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4844 1e37a5c2 2019-05-12 jcs case '\r': {
4845 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4846 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4847 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4848 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4849 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4850 1e37a5c2 2019-05-12 jcs if (id == NULL)
4851 1e37a5c2 2019-05-12 jcs break;
4852 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4853 1e37a5c2 2019-05-12 jcs if (err)
4854 1e37a5c2 2019-05-12 jcs break;
4855 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4856 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4857 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4858 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4859 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4860 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4861 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4862 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4863 1e37a5c2 2019-05-12 jcs break;
4864 15a94983 2018-12-23 stsp }
4865 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4866 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4867 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4868 1e37a5c2 2019-05-12 jcs if (err) {
4869 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4870 1e37a5c2 2019-05-12 jcs break;
4871 1e37a5c2 2019-05-12 jcs }
4872 e78dc838 2020-12-04 stsp view->focussed = 0;
4873 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4874 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4875 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4876 1e37a5c2 2019-05-12 jcs if (err)
4877 34bc9ec9 2019-02-22 stsp break;
4878 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4879 e78dc838 2020-12-04 stsp view->focus_child = 1;
4880 1e37a5c2 2019-05-12 jcs } else
4881 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4882 1e37a5c2 2019-05-12 jcs if (err)
4883 e5a0f69f 2018-08-18 stsp break;
4884 1e37a5c2 2019-05-12 jcs break;
4885 1e37a5c2 2019-05-12 jcs }
4886 83cc4199 2022-06-13 stsp case CTRL('d'):
4887 33c3719a 2022-06-15 stsp case 'd':
4888 83cc4199 2022-06-13 stsp nscroll /= 2;
4889 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4890 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4891 ea025d1d 2020-02-22 naddy case CTRL('f'):
4892 1e37a5c2 2019-05-12 jcs case ' ':
4893 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4894 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4895 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4896 e5a0f69f 2018-08-18 stsp break;
4897 1e37a5c2 2019-05-12 jcs }
4898 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4899 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4900 83cc4199 2022-06-13 stsp s->selected_line +=
4901 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
4902 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
4903 1e37a5c2 2019-05-12 jcs }
4904 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
4905 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
4906 1e37a5c2 2019-05-12 jcs else
4907 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4908 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
4909 1e37a5c2 2019-05-12 jcs break;
4910 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4911 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4912 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4913 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4914 1e37a5c2 2019-05-12 jcs }
4915 1e37a5c2 2019-05-12 jcs break;
4916 1e37a5c2 2019-05-12 jcs default:
4917 1e37a5c2 2019-05-12 jcs break;
4918 a70480e0 2018-06-23 stsp }
4919 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4920 a70480e0 2018-06-23 stsp }
4921 a70480e0 2018-06-23 stsp
4922 a70480e0 2018-06-23 stsp static const struct got_error *
4923 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4924 9f7d7167 2018-04-29 stsp {
4925 a70480e0 2018-06-23 stsp const struct got_error *error;
4926 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4927 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4928 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4929 0587e10c 2020-07-23 stsp char *link_target = NULL;
4930 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4931 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4932 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4933 a70480e0 2018-06-23 stsp int ch;
4934 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4935 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4936 a70480e0 2018-06-23 stsp
4937 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4938 a70480e0 2018-06-23 stsp switch (ch) {
4939 a70480e0 2018-06-23 stsp case 'c':
4940 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4941 a70480e0 2018-06-23 stsp break;
4942 69069811 2018-08-02 stsp case 'r':
4943 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4944 69069811 2018-08-02 stsp if (repo_path == NULL)
4945 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4946 9ba1d308 2019-10-21 stsp optarg);
4947 69069811 2018-08-02 stsp break;
4948 a70480e0 2018-06-23 stsp default:
4949 17020d27 2019-03-07 stsp usage_blame();
4950 a70480e0 2018-06-23 stsp /* NOTREACHED */
4951 a70480e0 2018-06-23 stsp }
4952 a70480e0 2018-06-23 stsp }
4953 a70480e0 2018-06-23 stsp
4954 a70480e0 2018-06-23 stsp argc -= optind;
4955 a70480e0 2018-06-23 stsp argv += optind;
4956 a70480e0 2018-06-23 stsp
4957 f135c941 2020-02-20 stsp if (argc != 1)
4958 a70480e0 2018-06-23 stsp usage_blame();
4959 6962eb72 2020-02-20 stsp
4960 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4961 0ae84acc 2022-06-15 tracey if (error != NULL)
4962 0ae84acc 2022-06-15 tracey goto done;
4963 0ae84acc 2022-06-15 tracey
4964 69069811 2018-08-02 stsp if (repo_path == NULL) {
4965 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4966 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4967 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4968 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4969 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4970 c156c7a4 2020-12-18 stsp goto done;
4971 f135c941 2020-02-20 stsp if (worktree)
4972 eb41ed75 2019-02-05 stsp repo_path =
4973 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4974 f135c941 2020-02-20 stsp else
4975 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4976 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4977 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4978 c156c7a4 2020-12-18 stsp goto done;
4979 c156c7a4 2020-12-18 stsp }
4980 f135c941 2020-02-20 stsp }
4981 a915003a 2019-02-05 stsp
4982 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4983 c02c541e 2019-03-29 stsp if (error != NULL)
4984 8e94dd5b 2019-01-04 stsp goto done;
4985 69069811 2018-08-02 stsp
4986 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4987 f135c941 2020-02-20 stsp worktree);
4988 c02c541e 2019-03-29 stsp if (error)
4989 92205607 2019-01-04 stsp goto done;
4990 69069811 2018-08-02 stsp
4991 f135c941 2020-02-20 stsp init_curses();
4992 f135c941 2020-02-20 stsp
4993 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4994 51a10b52 2020-12-26 stsp if (error)
4995 51a10b52 2020-12-26 stsp goto done;
4996 51a10b52 2020-12-26 stsp
4997 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4998 eb41ed75 2019-02-05 stsp if (error)
4999 69069811 2018-08-02 stsp goto done;
5000 a70480e0 2018-06-23 stsp
5001 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5002 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5003 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5004 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5005 a70480e0 2018-06-23 stsp if (error != NULL)
5006 66b4983c 2018-06-23 stsp goto done;
5007 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5008 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5009 a70480e0 2018-06-23 stsp } else {
5010 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5011 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5012 a70480e0 2018-06-23 stsp }
5013 a19e88aa 2018-06-23 stsp if (error != NULL)
5014 8b473291 2019-02-21 stsp goto done;
5015 8b473291 2019-02-21 stsp
5016 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5017 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5018 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5019 e1cd8fed 2018-08-01 stsp goto done;
5020 e1cd8fed 2018-08-01 stsp }
5021 0587e10c 2020-07-23 stsp
5022 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5023 a44927cc 2022-04-07 stsp if (error)
5024 a44927cc 2022-04-07 stsp goto done;
5025 a44927cc 2022-04-07 stsp
5026 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5027 a44927cc 2022-04-07 stsp commit, repo);
5028 7cbe629d 2018-08-04 stsp if (error)
5029 7cbe629d 2018-08-04 stsp goto done;
5030 0587e10c 2020-07-23 stsp
5031 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5032 78756c87 2020-11-24 stsp commit_id, repo);
5033 0587e10c 2020-07-23 stsp if (error)
5034 0587e10c 2020-07-23 stsp goto done;
5035 12314ad4 2019-08-31 stsp if (worktree) {
5036 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5037 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5038 12314ad4 2019-08-31 stsp worktree = NULL;
5039 12314ad4 2019-08-31 stsp }
5040 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5041 a70480e0 2018-06-23 stsp done:
5042 69069811 2018-08-02 stsp free(repo_path);
5043 f135c941 2020-02-20 stsp free(in_repo_path);
5044 0587e10c 2020-07-23 stsp free(link_target);
5045 69069811 2018-08-02 stsp free(cwd);
5046 a70480e0 2018-06-23 stsp free(commit_id);
5047 a44927cc 2022-04-07 stsp if (commit)
5048 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5049 eb41ed75 2019-02-05 stsp if (worktree)
5050 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5051 1d0f4054 2021-06-17 stsp if (repo) {
5052 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5053 1d0f4054 2021-06-17 stsp if (error == NULL)
5054 1d0f4054 2021-06-17 stsp error = close_err;
5055 1d0f4054 2021-06-17 stsp }
5056 0ae84acc 2022-06-15 tracey if (pack_fds) {
5057 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5058 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5059 0ae84acc 2022-06-15 tracey if (error == NULL)
5060 0ae84acc 2022-06-15 tracey error = pack_err;
5061 bb1c2cf3 2022-06-15 tracey pack_fds = NULL;
5062 0ae84acc 2022-06-15 tracey }
5063 51a10b52 2020-12-26 stsp tog_free_refs();
5064 a70480e0 2018-06-23 stsp return error;
5065 ffd1d5e5 2018-06-23 stsp }
5066 ffd1d5e5 2018-06-23 stsp
5067 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5068 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5069 ffd1d5e5 2018-06-23 stsp {
5070 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5071 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5072 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5073 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5074 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5075 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5076 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5077 ffd1d5e5 2018-06-23 stsp
5078 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5079 ffd1d5e5 2018-06-23 stsp
5080 f7d12f7e 2018-08-01 stsp werase(view->window);
5081 ffd1d5e5 2018-06-23 stsp
5082 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5083 ffd1d5e5 2018-06-23 stsp return NULL;
5084 ffd1d5e5 2018-06-23 stsp
5085 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5086 ffd1d5e5 2018-06-23 stsp if (err)
5087 ffd1d5e5 2018-06-23 stsp return err;
5088 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5089 a3404814 2018-09-02 stsp wstandout(view->window);
5090 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5091 11b20872 2019-11-08 stsp if (tc)
5092 11b20872 2019-11-08 stsp wattr_on(view->window,
5093 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5094 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5095 11b20872 2019-11-08 stsp if (tc)
5096 11b20872 2019-11-08 stsp wattr_off(view->window,
5097 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5098 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5099 a3404814 2018-09-02 stsp wstandend(view->window);
5100 2550e4c3 2018-07-13 stsp free(wline);
5101 2550e4c3 2018-07-13 stsp wline = NULL;
5102 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5103 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5104 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5105 ffd1d5e5 2018-06-23 stsp return NULL;
5106 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5107 ce52c690 2018-06-23 stsp if (err)
5108 ce52c690 2018-06-23 stsp return err;
5109 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5110 2550e4c3 2018-07-13 stsp free(wline);
5111 2550e4c3 2018-07-13 stsp wline = NULL;
5112 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5113 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5114 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5115 ffd1d5e5 2018-06-23 stsp return NULL;
5116 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5117 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5118 a1eca9bb 2018-06-23 stsp return NULL;
5119 ffd1d5e5 2018-06-23 stsp
5120 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5121 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5122 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5123 0cf4efb1 2018-09-29 stsp if (view->focussed)
5124 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5125 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5126 ffd1d5e5 2018-06-23 stsp }
5127 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5128 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5129 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5130 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5131 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5132 ffd1d5e5 2018-06-23 stsp return NULL;
5133 ffd1d5e5 2018-06-23 stsp n = 1;
5134 ffd1d5e5 2018-06-23 stsp } else {
5135 ffd1d5e5 2018-06-23 stsp n = 0;
5136 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5137 ffd1d5e5 2018-06-23 stsp }
5138 ffd1d5e5 2018-06-23 stsp
5139 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5140 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5141 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5142 848d6979 2019-08-12 stsp const char *modestr = "";
5143 56e0773d 2019-11-28 stsp mode_t mode;
5144 1d13200f 2018-07-12 stsp
5145 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5146 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5147 56e0773d 2019-11-28 stsp
5148 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5149 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5150 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5151 1d13200f 2018-07-12 stsp if (err)
5152 638f9024 2019-05-13 stsp return got_error_from_errno(
5153 230a42bd 2019-05-11 jcs "got_object_id_str");
5154 1d13200f 2018-07-12 stsp }
5155 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5156 63c5ca5d 2019-08-24 stsp modestr = "$";
5157 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5158 0d6c6ee3 2020-05-20 stsp int i;
5159 0d6c6ee3 2020-05-20 stsp
5160 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5161 d86d3b18 2020-12-01 naddy te, s->repo);
5162 0d6c6ee3 2020-05-20 stsp if (err) {
5163 0d6c6ee3 2020-05-20 stsp free(id_str);
5164 0d6c6ee3 2020-05-20 stsp return err;
5165 0d6c6ee3 2020-05-20 stsp }
5166 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5167 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5168 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5169 0d6c6ee3 2020-05-20 stsp }
5170 848d6979 2019-08-12 stsp modestr = "@";
5171 0d6c6ee3 2020-05-20 stsp }
5172 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5173 848d6979 2019-08-12 stsp modestr = "/";
5174 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5175 848d6979 2019-08-12 stsp modestr = "*";
5176 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5177 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5178 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5179 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5180 1d13200f 2018-07-12 stsp free(id_str);
5181 0d6c6ee3 2020-05-20 stsp free(link_target);
5182 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5183 1d13200f 2018-07-12 stsp }
5184 1d13200f 2018-07-12 stsp free(id_str);
5185 0d6c6ee3 2020-05-20 stsp free(link_target);
5186 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5187 ffd1d5e5 2018-06-23 stsp if (err) {
5188 ffd1d5e5 2018-06-23 stsp free(line);
5189 ffd1d5e5 2018-06-23 stsp break;
5190 ffd1d5e5 2018-06-23 stsp }
5191 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5192 0cf4efb1 2018-09-29 stsp if (view->focussed)
5193 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5194 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5195 ffd1d5e5 2018-06-23 stsp }
5196 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5197 f26dddb7 2019-11-08 stsp if (tc)
5198 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5199 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5200 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5201 f26dddb7 2019-11-08 stsp if (tc)
5202 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5203 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5204 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5205 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5206 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5207 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5208 ffd1d5e5 2018-06-23 stsp free(line);
5209 2550e4c3 2018-07-13 stsp free(wline);
5210 2550e4c3 2018-07-13 stsp wline = NULL;
5211 ffd1d5e5 2018-06-23 stsp n++;
5212 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5213 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5214 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5215 ffd1d5e5 2018-06-23 stsp break;
5216 ffd1d5e5 2018-06-23 stsp }
5217 ffd1d5e5 2018-06-23 stsp
5218 ffd1d5e5 2018-06-23 stsp return err;
5219 ffd1d5e5 2018-06-23 stsp }
5220 ffd1d5e5 2018-06-23 stsp
5221 ffd1d5e5 2018-06-23 stsp static void
5222 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5223 ffd1d5e5 2018-06-23 stsp {
5224 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5225 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5226 fa86c4bf 2020-11-29 stsp int i = 0;
5227 ffd1d5e5 2018-06-23 stsp
5228 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5229 ffd1d5e5 2018-06-23 stsp return;
5230 ffd1d5e5 2018-06-23 stsp
5231 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5232 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5233 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5234 fa86c4bf 2020-11-29 stsp if (!isroot)
5235 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5236 fa86c4bf 2020-11-29 stsp break;
5237 fa86c4bf 2020-11-29 stsp }
5238 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5239 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5240 ffd1d5e5 2018-06-23 stsp }
5241 ffd1d5e5 2018-06-23 stsp }
5242 ffd1d5e5 2018-06-23 stsp
5243 fa86c4bf 2020-11-29 stsp static void
5244 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5245 ffd1d5e5 2018-06-23 stsp {
5246 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5247 ffd1d5e5 2018-06-23 stsp int n = 0;
5248 ffd1d5e5 2018-06-23 stsp
5249 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5250 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5251 694d3271 2020-12-01 naddy s->first_displayed_entry);
5252 694d3271 2020-12-01 naddy else
5253 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5254 56e0773d 2019-11-28 stsp
5255 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5256 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5257 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5258 768394f3 2019-01-24 stsp if (last) {
5259 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5260 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5261 768394f3 2019-01-24 stsp }
5262 ffd1d5e5 2018-06-23 stsp }
5263 ffd1d5e5 2018-06-23 stsp }
5264 ffd1d5e5 2018-06-23 stsp
5265 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5266 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5267 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5268 ffd1d5e5 2018-06-23 stsp {
5269 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5270 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5271 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5272 ffd1d5e5 2018-06-23 stsp
5273 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5274 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5275 56e0773d 2019-11-28 stsp + 1 /* slash */;
5276 ce52c690 2018-06-23 stsp if (te)
5277 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5278 ce52c690 2018-06-23 stsp
5279 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5280 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5281 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5282 ffd1d5e5 2018-06-23 stsp
5283 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5284 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5285 d9765a41 2018-06-23 stsp while (pt) {
5286 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5287 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5288 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5289 cb2ebc8a 2018-06-23 stsp goto done;
5290 cb2ebc8a 2018-06-23 stsp }
5291 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5292 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5293 cb2ebc8a 2018-06-23 stsp goto done;
5294 cb2ebc8a 2018-06-23 stsp }
5295 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5296 ffd1d5e5 2018-06-23 stsp }
5297 ce52c690 2018-06-23 stsp if (te) {
5298 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5299 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5300 ce52c690 2018-06-23 stsp goto done;
5301 ce52c690 2018-06-23 stsp }
5302 cb2ebc8a 2018-06-23 stsp }
5303 ce52c690 2018-06-23 stsp done:
5304 ce52c690 2018-06-23 stsp if (err) {
5305 ce52c690 2018-06-23 stsp free(*path);
5306 ce52c690 2018-06-23 stsp *path = NULL;
5307 ce52c690 2018-06-23 stsp }
5308 ce52c690 2018-06-23 stsp return err;
5309 ce52c690 2018-06-23 stsp }
5310 ce52c690 2018-06-23 stsp
5311 ce52c690 2018-06-23 stsp static const struct got_error *
5312 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5313 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5314 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5315 ce52c690 2018-06-23 stsp {
5316 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5317 ce52c690 2018-06-23 stsp char *path;
5318 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5319 a0de39f3 2019-08-09 stsp
5320 a0de39f3 2019-08-09 stsp *new_view = NULL;
5321 69efd4c4 2018-07-18 stsp
5322 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5323 ce52c690 2018-06-23 stsp if (err)
5324 ce52c690 2018-06-23 stsp return err;
5325 ffd1d5e5 2018-06-23 stsp
5326 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5327 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5328 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5329 83ce39e3 2019-08-12 stsp goto done;
5330 83ce39e3 2019-08-12 stsp }
5331 cdf1ee82 2018-08-01 stsp
5332 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5333 e5a0f69f 2018-08-18 stsp if (err) {
5334 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5335 fc06ba56 2019-08-22 stsp err = NULL;
5336 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5337 e5a0f69f 2018-08-18 stsp } else
5338 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5339 83ce39e3 2019-08-12 stsp done:
5340 83ce39e3 2019-08-12 stsp free(path);
5341 69efd4c4 2018-07-18 stsp return err;
5342 69efd4c4 2018-07-18 stsp }
5343 69efd4c4 2018-07-18 stsp
5344 69efd4c4 2018-07-18 stsp static const struct got_error *
5345 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5346 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5347 69efd4c4 2018-07-18 stsp {
5348 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5349 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5350 69efd4c4 2018-07-18 stsp char *path;
5351 a0de39f3 2019-08-09 stsp
5352 a0de39f3 2019-08-09 stsp *new_view = NULL;
5353 69efd4c4 2018-07-18 stsp
5354 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5355 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5356 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5357 e5a0f69f 2018-08-18 stsp
5358 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5359 69efd4c4 2018-07-18 stsp if (err)
5360 69efd4c4 2018-07-18 stsp return err;
5361 69efd4c4 2018-07-18 stsp
5362 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5363 4e97c21c 2020-12-06 stsp path, 0);
5364 ba4f502b 2018-08-04 stsp if (err)
5365 e5a0f69f 2018-08-18 stsp view_close(log_view);
5366 e5a0f69f 2018-08-18 stsp else
5367 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5368 cb2ebc8a 2018-06-23 stsp free(path);
5369 cb2ebc8a 2018-06-23 stsp return err;
5370 ffd1d5e5 2018-06-23 stsp }
5371 ffd1d5e5 2018-06-23 stsp
5372 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5373 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5374 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5375 ffd1d5e5 2018-06-23 stsp {
5376 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5377 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5378 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5379 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5380 ffd1d5e5 2018-06-23 stsp
5381 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5382 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5383 bc573f3b 2021-07-10 stsp
5384 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5385 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5386 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5387 ffd1d5e5 2018-06-23 stsp
5388 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5389 bc573f3b 2021-07-10 stsp if (err)
5390 bc573f3b 2021-07-10 stsp goto done;
5391 bc573f3b 2021-07-10 stsp
5392 bc573f3b 2021-07-10 stsp /*
5393 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5394 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5395 bc573f3b 2021-07-10 stsp * closed on demand.
5396 bc573f3b 2021-07-10 stsp */
5397 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5398 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5399 bc573f3b 2021-07-10 stsp if (err)
5400 bc573f3b 2021-07-10 stsp goto done;
5401 bc573f3b 2021-07-10 stsp s->tree = s->root;
5402 bc573f3b 2021-07-10 stsp
5403 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5404 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5405 ffd1d5e5 2018-06-23 stsp goto done;
5406 ffd1d5e5 2018-06-23 stsp
5407 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5408 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5409 ffd1d5e5 2018-06-23 stsp goto done;
5410 ffd1d5e5 2018-06-23 stsp }
5411 ffd1d5e5 2018-06-23 stsp
5412 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5413 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5414 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5415 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5416 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5417 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5418 9cd7cbd1 2020-12-07 stsp goto done;
5419 9cd7cbd1 2020-12-07 stsp }
5420 9cd7cbd1 2020-12-07 stsp }
5421 fb2756b9 2018-08-04 stsp s->repo = repo;
5422 c0b01bdb 2019-11-08 stsp
5423 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5424 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5425 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5426 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5427 c0b01bdb 2019-11-08 stsp if (err)
5428 c0b01bdb 2019-11-08 stsp goto done;
5429 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5430 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5431 bc573f3b 2021-07-10 stsp if (err)
5432 c0b01bdb 2019-11-08 stsp goto done;
5433 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5434 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5435 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5436 bc573f3b 2021-07-10 stsp if (err)
5437 c0b01bdb 2019-11-08 stsp goto done;
5438 e5a0f69f 2018-08-18 stsp
5439 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5440 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5441 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5442 bc573f3b 2021-07-10 stsp if (err)
5443 11b20872 2019-11-08 stsp goto done;
5444 11b20872 2019-11-08 stsp
5445 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5446 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5447 bc573f3b 2021-07-10 stsp if (err)
5448 c0b01bdb 2019-11-08 stsp goto done;
5449 c0b01bdb 2019-11-08 stsp }
5450 c0b01bdb 2019-11-08 stsp
5451 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5452 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5453 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5454 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5455 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5456 ad80ab7b 2018-08-04 stsp done:
5457 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5458 bc573f3b 2021-07-10 stsp if (commit)
5459 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5460 bc573f3b 2021-07-10 stsp if (err)
5461 bc573f3b 2021-07-10 stsp close_tree_view(view);
5462 ad80ab7b 2018-08-04 stsp return err;
5463 ad80ab7b 2018-08-04 stsp }
5464 ad80ab7b 2018-08-04 stsp
5465 e5a0f69f 2018-08-18 stsp static const struct got_error *
5466 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5467 ad80ab7b 2018-08-04 stsp {
5468 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5469 ad80ab7b 2018-08-04 stsp
5470 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5471 fb2756b9 2018-08-04 stsp free(s->tree_label);
5472 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5473 6484ec90 2018-09-29 stsp free(s->commit_id);
5474 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5475 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5476 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5477 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5478 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5479 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5480 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5481 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5482 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5483 ad80ab7b 2018-08-04 stsp free(parent);
5484 ad80ab7b 2018-08-04 stsp
5485 ad80ab7b 2018-08-04 stsp }
5486 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5487 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5488 bc573f3b 2021-07-10 stsp if (s->root)
5489 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5490 7c32bd05 2019-06-22 stsp return NULL;
5491 7c32bd05 2019-06-22 stsp }
5492 7c32bd05 2019-06-22 stsp
5493 7c32bd05 2019-06-22 stsp static const struct got_error *
5494 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5495 7c32bd05 2019-06-22 stsp {
5496 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5497 7c32bd05 2019-06-22 stsp
5498 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5499 7c32bd05 2019-06-22 stsp return NULL;
5500 7c32bd05 2019-06-22 stsp }
5501 7c32bd05 2019-06-22 stsp
5502 7c32bd05 2019-06-22 stsp static int
5503 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5504 7c32bd05 2019-06-22 stsp {
5505 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5506 7c32bd05 2019-06-22 stsp
5507 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5508 56e0773d 2019-11-28 stsp 0) == 0;
5509 7c32bd05 2019-06-22 stsp }
5510 7c32bd05 2019-06-22 stsp
5511 7c32bd05 2019-06-22 stsp static const struct got_error *
5512 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5513 7c32bd05 2019-06-22 stsp {
5514 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5515 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5516 7c32bd05 2019-06-22 stsp
5517 7c32bd05 2019-06-22 stsp if (!view->searching) {
5518 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5519 7c32bd05 2019-06-22 stsp return NULL;
5520 7c32bd05 2019-06-22 stsp }
5521 7c32bd05 2019-06-22 stsp
5522 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5523 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5524 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5525 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5526 56e0773d 2019-11-28 stsp s->selected_entry);
5527 7c32bd05 2019-06-22 stsp else
5528 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5529 56e0773d 2019-11-28 stsp } else {
5530 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5531 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5532 56e0773d 2019-11-28 stsp else
5533 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5534 56e0773d 2019-11-28 stsp s->selected_entry);
5535 7c32bd05 2019-06-22 stsp }
5536 7c32bd05 2019-06-22 stsp } else {
5537 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5538 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5539 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5540 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5541 56e0773d 2019-11-28 stsp else
5542 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5543 7c32bd05 2019-06-22 stsp }
5544 7c32bd05 2019-06-22 stsp
5545 7c32bd05 2019-06-22 stsp while (1) {
5546 56e0773d 2019-11-28 stsp if (te == NULL) {
5547 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5548 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5549 ac66afb8 2019-06-24 stsp return NULL;
5550 ac66afb8 2019-06-24 stsp }
5551 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5552 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5553 56e0773d 2019-11-28 stsp else
5554 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5555 7c32bd05 2019-06-22 stsp }
5556 7c32bd05 2019-06-22 stsp
5557 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5558 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5559 56e0773d 2019-11-28 stsp s->matched_entry = te;
5560 7c32bd05 2019-06-22 stsp break;
5561 7c32bd05 2019-06-22 stsp }
5562 7c32bd05 2019-06-22 stsp
5563 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5564 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5565 56e0773d 2019-11-28 stsp else
5566 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5567 7c32bd05 2019-06-22 stsp }
5568 e5a0f69f 2018-08-18 stsp
5569 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5570 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5571 7c32bd05 2019-06-22 stsp s->selected = 0;
5572 7c32bd05 2019-06-22 stsp }
5573 7c32bd05 2019-06-22 stsp
5574 e5a0f69f 2018-08-18 stsp return NULL;
5575 ad80ab7b 2018-08-04 stsp }
5576 ad80ab7b 2018-08-04 stsp
5577 ad80ab7b 2018-08-04 stsp static const struct got_error *
5578 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5579 ad80ab7b 2018-08-04 stsp {
5580 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5581 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5582 e5a0f69f 2018-08-18 stsp char *parent_path;
5583 ad80ab7b 2018-08-04 stsp
5584 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5585 e5a0f69f 2018-08-18 stsp if (err)
5586 e5a0f69f 2018-08-18 stsp return err;
5587 ffd1d5e5 2018-06-23 stsp
5588 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5589 e5a0f69f 2018-08-18 stsp free(parent_path);
5590 669b5ffa 2018-10-07 stsp
5591 669b5ffa 2018-10-07 stsp view_vborder(view);
5592 e5a0f69f 2018-08-18 stsp return err;
5593 e5a0f69f 2018-08-18 stsp }
5594 ce52c690 2018-06-23 stsp
5595 e5a0f69f 2018-08-18 stsp static const struct got_error *
5596 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5597 e5a0f69f 2018-08-18 stsp {
5598 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5599 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5600 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5601 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5602 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
5603 ffd1d5e5 2018-06-23 stsp
5604 e5a0f69f 2018-08-18 stsp switch (ch) {
5605 1e37a5c2 2019-05-12 jcs case 'i':
5606 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5607 1e37a5c2 2019-05-12 jcs break;
5608 1e37a5c2 2019-05-12 jcs case 'l':
5609 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5610 ffd1d5e5 2018-06-23 stsp break;
5611 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5612 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5613 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5614 e78dc838 2020-12-04 stsp view->focussed = 0;
5615 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5616 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5617 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5618 1e37a5c2 2019-05-12 jcs if (err)
5619 1e37a5c2 2019-05-12 jcs return err;
5620 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5621 e78dc838 2020-12-04 stsp view->focus_child = 1;
5622 1e37a5c2 2019-05-12 jcs } else
5623 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5624 152c1c93 2020-11-29 stsp break;
5625 152c1c93 2020-11-29 stsp case 'r':
5626 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5627 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5628 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5629 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5630 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5631 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5632 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5633 152c1c93 2020-11-29 stsp if (err) {
5634 152c1c93 2020-11-29 stsp view_close(ref_view);
5635 152c1c93 2020-11-29 stsp return err;
5636 152c1c93 2020-11-29 stsp }
5637 e78dc838 2020-12-04 stsp view->focussed = 0;
5638 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5639 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5640 152c1c93 2020-11-29 stsp err = view_close_child(view);
5641 152c1c93 2020-11-29 stsp if (err)
5642 152c1c93 2020-11-29 stsp return err;
5643 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5644 e78dc838 2020-12-04 stsp view->focus_child = 1;
5645 152c1c93 2020-11-29 stsp } else
5646 152c1c93 2020-11-29 stsp *new_view = ref_view;
5647 e4526bf5 2021-09-03 naddy break;
5648 e4526bf5 2021-09-03 naddy case 'g':
5649 e4526bf5 2021-09-03 naddy case KEY_HOME:
5650 e4526bf5 2021-09-03 naddy s->selected = 0;
5651 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5652 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5653 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5654 e4526bf5 2021-09-03 naddy else
5655 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5656 1e37a5c2 2019-05-12 jcs break;
5657 e4526bf5 2021-09-03 naddy case 'G':
5658 e4526bf5 2021-09-03 naddy case KEY_END:
5659 e4526bf5 2021-09-03 naddy s->selected = 0;
5660 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5661 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5662 e4526bf5 2021-09-03 naddy if (te == NULL) {
5663 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5664 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5665 e4526bf5 2021-09-03 naddy n++;
5666 e4526bf5 2021-09-03 naddy }
5667 e4526bf5 2021-09-03 naddy break;
5668 e4526bf5 2021-09-03 naddy }
5669 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5670 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5671 e4526bf5 2021-09-03 naddy }
5672 e4526bf5 2021-09-03 naddy if (n > 0)
5673 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5674 e4526bf5 2021-09-03 naddy break;
5675 1e37a5c2 2019-05-12 jcs case 'k':
5676 1e37a5c2 2019-05-12 jcs case KEY_UP:
5677 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5678 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5679 1e37a5c2 2019-05-12 jcs s->selected--;
5680 fa86c4bf 2020-11-29 stsp break;
5681 1e37a5c2 2019-05-12 jcs }
5682 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5683 1e37a5c2 2019-05-12 jcs break;
5684 83cc4199 2022-06-13 stsp case CTRL('u'):
5685 33c3719a 2022-06-15 stsp case 'u':
5686 83cc4199 2022-06-13 stsp nscroll /= 2;
5687 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5688 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5689 ea025d1d 2020-02-22 naddy case CTRL('b'):
5690 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5691 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5692 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5693 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5694 fa86c4bf 2020-11-29 stsp } else {
5695 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5696 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5697 fa86c4bf 2020-11-29 stsp }
5698 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
5699 1e37a5c2 2019-05-12 jcs break;
5700 1e37a5c2 2019-05-12 jcs case 'j':
5701 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5702 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5703 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5704 1e37a5c2 2019-05-12 jcs s->selected++;
5705 1e37a5c2 2019-05-12 jcs break;
5706 1e37a5c2 2019-05-12 jcs }
5707 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5708 56e0773d 2019-11-28 stsp == NULL)
5709 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5710 1e37a5c2 2019-05-12 jcs break;
5711 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5712 1e37a5c2 2019-05-12 jcs break;
5713 83cc4199 2022-06-13 stsp case CTRL('d'):
5714 33c3719a 2022-06-15 stsp case 'd':
5715 83cc4199 2022-06-13 stsp nscroll /= 2;
5716 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5717 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5718 ea025d1d 2020-02-22 naddy case CTRL('f'):
5719 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5720 1e37a5c2 2019-05-12 jcs == NULL) {
5721 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5722 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5723 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
5724 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
5725 1e37a5c2 2019-05-12 jcs break;
5726 1e37a5c2 2019-05-12 jcs }
5727 83cc4199 2022-06-13 stsp tree_scroll_down(s, nscroll);
5728 1e37a5c2 2019-05-12 jcs break;
5729 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5730 1e37a5c2 2019-05-12 jcs case '\r':
5731 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5732 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5733 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5734 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5735 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5736 1e37a5c2 2019-05-12 jcs break;
5737 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5738 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5739 1e37a5c2 2019-05-12 jcs entry);
5740 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5741 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5742 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5743 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5744 1e37a5c2 2019-05-12 jcs s->selected_entry =
5745 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5746 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5747 1e37a5c2 2019-05-12 jcs free(parent);
5748 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5749 56e0773d 2019-11-28 stsp s->selected_entry))) {
5750 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5751 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5752 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5753 1e37a5c2 2019-05-12 jcs if (err)
5754 1e37a5c2 2019-05-12 jcs break;
5755 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5756 941e9f74 2019-05-21 stsp if (err) {
5757 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5758 1e37a5c2 2019-05-12 jcs break;
5759 1e37a5c2 2019-05-12 jcs }
5760 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5761 56e0773d 2019-11-28 stsp s->selected_entry))) {
5762 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5763 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5764 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5765 1e37a5c2 2019-05-12 jcs
5766 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5767 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5768 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5769 1e37a5c2 2019-05-12 jcs if (err)
5770 1e37a5c2 2019-05-12 jcs break;
5771 e78dc838 2020-12-04 stsp view->focussed = 0;
5772 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5773 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5774 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5775 669b5ffa 2018-10-07 stsp if (err)
5776 669b5ffa 2018-10-07 stsp return err;
5777 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5778 e78dc838 2020-12-04 stsp view->focus_child = 1;
5779 669b5ffa 2018-10-07 stsp } else
5780 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5781 1e37a5c2 2019-05-12 jcs }
5782 1e37a5c2 2019-05-12 jcs break;
5783 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5784 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5785 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5786 1e37a5c2 2019-05-12 jcs break;
5787 1e37a5c2 2019-05-12 jcs default:
5788 1e37a5c2 2019-05-12 jcs break;
5789 ffd1d5e5 2018-06-23 stsp }
5790 e5a0f69f 2018-08-18 stsp
5791 ffd1d5e5 2018-06-23 stsp return err;
5792 9f7d7167 2018-04-29 stsp }
5793 9f7d7167 2018-04-29 stsp
5794 ffd1d5e5 2018-06-23 stsp __dead static void
5795 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5796 ffd1d5e5 2018-06-23 stsp {
5797 ffd1d5e5 2018-06-23 stsp endwin();
5798 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5799 ffd1d5e5 2018-06-23 stsp getprogname());
5800 ffd1d5e5 2018-06-23 stsp exit(1);
5801 ffd1d5e5 2018-06-23 stsp }
5802 ffd1d5e5 2018-06-23 stsp
5803 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5804 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5805 ffd1d5e5 2018-06-23 stsp {
5806 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5807 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5808 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5809 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5810 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5811 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5812 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5813 4e97c21c 2020-12-06 stsp char *label = NULL;
5814 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5815 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5816 ffd1d5e5 2018-06-23 stsp int ch;
5817 5221c383 2018-08-01 stsp struct tog_view *view;
5818 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5819 70ac5f84 2019-03-28 stsp
5820 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5821 ffd1d5e5 2018-06-23 stsp switch (ch) {
5822 ffd1d5e5 2018-06-23 stsp case 'c':
5823 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5824 74283ab8 2020-02-07 stsp break;
5825 74283ab8 2020-02-07 stsp case 'r':
5826 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5827 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5828 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5829 74283ab8 2020-02-07 stsp optarg);
5830 ffd1d5e5 2018-06-23 stsp break;
5831 ffd1d5e5 2018-06-23 stsp default:
5832 e99e2d15 2020-11-24 naddy usage_tree();
5833 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5834 ffd1d5e5 2018-06-23 stsp }
5835 ffd1d5e5 2018-06-23 stsp }
5836 ffd1d5e5 2018-06-23 stsp
5837 ffd1d5e5 2018-06-23 stsp argc -= optind;
5838 ffd1d5e5 2018-06-23 stsp argv += optind;
5839 ffd1d5e5 2018-06-23 stsp
5840 55cccc34 2020-02-20 stsp if (argc > 1)
5841 e99e2d15 2020-11-24 naddy usage_tree();
5842 0ae84acc 2022-06-15 tracey
5843 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5844 0ae84acc 2022-06-15 tracey if (error != NULL)
5845 0ae84acc 2022-06-15 tracey goto done;
5846 74283ab8 2020-02-07 stsp
5847 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5848 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5849 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5850 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5851 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5852 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5853 c156c7a4 2020-12-18 stsp goto done;
5854 55cccc34 2020-02-20 stsp if (worktree)
5855 52185f70 2019-02-05 stsp repo_path =
5856 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5857 55cccc34 2020-02-20 stsp else
5858 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5859 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5860 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5861 c156c7a4 2020-12-18 stsp goto done;
5862 c156c7a4 2020-12-18 stsp }
5863 55cccc34 2020-02-20 stsp }
5864 a915003a 2019-02-05 stsp
5865 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5866 c02c541e 2019-03-29 stsp if (error != NULL)
5867 52185f70 2019-02-05 stsp goto done;
5868 d188b9a6 2019-01-04 stsp
5869 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5870 55cccc34 2020-02-20 stsp repo, worktree);
5871 55cccc34 2020-02-20 stsp if (error)
5872 55cccc34 2020-02-20 stsp goto done;
5873 55cccc34 2020-02-20 stsp
5874 55cccc34 2020-02-20 stsp init_curses();
5875 55cccc34 2020-02-20 stsp
5876 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5877 c02c541e 2019-03-29 stsp if (error)
5878 52185f70 2019-02-05 stsp goto done;
5879 ffd1d5e5 2018-06-23 stsp
5880 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5881 51a10b52 2020-12-26 stsp if (error)
5882 51a10b52 2020-12-26 stsp goto done;
5883 51a10b52 2020-12-26 stsp
5884 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5885 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5886 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5887 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5888 4e97c21c 2020-12-06 stsp if (error)
5889 4e97c21c 2020-12-06 stsp goto done;
5890 4e97c21c 2020-12-06 stsp head_ref_name = label;
5891 4e97c21c 2020-12-06 stsp } else {
5892 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5893 4e97c21c 2020-12-06 stsp if (error == NULL)
5894 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5895 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5896 4e97c21c 2020-12-06 stsp goto done;
5897 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5898 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5899 4e97c21c 2020-12-06 stsp if (error)
5900 4e97c21c 2020-12-06 stsp goto done;
5901 4e97c21c 2020-12-06 stsp }
5902 ffd1d5e5 2018-06-23 stsp
5903 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5904 a44927cc 2022-04-07 stsp if (error)
5905 a44927cc 2022-04-07 stsp goto done;
5906 a44927cc 2022-04-07 stsp
5907 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5908 5221c383 2018-08-01 stsp if (view == NULL) {
5909 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5910 5221c383 2018-08-01 stsp goto done;
5911 5221c383 2018-08-01 stsp }
5912 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5913 ad80ab7b 2018-08-04 stsp if (error)
5914 ad80ab7b 2018-08-04 stsp goto done;
5915 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5916 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
5917 d91faf3b 2020-12-01 naddy in_repo_path);
5918 55cccc34 2020-02-20 stsp if (error)
5919 55cccc34 2020-02-20 stsp goto done;
5920 55cccc34 2020-02-20 stsp }
5921 55cccc34 2020-02-20 stsp
5922 55cccc34 2020-02-20 stsp if (worktree) {
5923 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5924 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5925 55cccc34 2020-02-20 stsp worktree = NULL;
5926 55cccc34 2020-02-20 stsp }
5927 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5928 ffd1d5e5 2018-06-23 stsp done:
5929 52185f70 2019-02-05 stsp free(repo_path);
5930 e4a0e26d 2020-02-20 stsp free(cwd);
5931 ffd1d5e5 2018-06-23 stsp free(commit_id);
5932 4e97c21c 2020-12-06 stsp free(label);
5933 486cd271 2020-12-06 stsp if (ref)
5934 486cd271 2020-12-06 stsp got_ref_close(ref);
5935 1d0f4054 2021-06-17 stsp if (repo) {
5936 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5937 1d0f4054 2021-06-17 stsp if (error == NULL)
5938 1d0f4054 2021-06-17 stsp error = close_err;
5939 1d0f4054 2021-06-17 stsp }
5940 0ae84acc 2022-06-15 tracey if (pack_fds) {
5941 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5942 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5943 0ae84acc 2022-06-15 tracey if (error == NULL)
5944 0ae84acc 2022-06-15 tracey error = pack_err;
5945 bb1c2cf3 2022-06-15 tracey pack_fds = NULL;
5946 0ae84acc 2022-06-15 tracey }
5947 51a10b52 2020-12-26 stsp tog_free_refs();
5948 ffd1d5e5 2018-06-23 stsp return error;
5949 6458efa5 2020-11-24 stsp }
5950 6458efa5 2020-11-24 stsp
5951 6458efa5 2020-11-24 stsp static const struct got_error *
5952 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5953 6458efa5 2020-11-24 stsp {
5954 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5955 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5956 6458efa5 2020-11-24 stsp
5957 6458efa5 2020-11-24 stsp s->nrefs = 0;
5958 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5959 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
5960 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
5961 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
5962 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
5963 6458efa5 2020-11-24 stsp continue;
5964 6458efa5 2020-11-24 stsp
5965 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5966 6458efa5 2020-11-24 stsp if (re == NULL)
5967 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5968 6458efa5 2020-11-24 stsp
5969 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5970 8924d611 2020-12-26 stsp if (re->ref == NULL)
5971 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5972 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5973 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5974 6458efa5 2020-11-24 stsp }
5975 6458efa5 2020-11-24 stsp
5976 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5977 6458efa5 2020-11-24 stsp return NULL;
5978 6458efa5 2020-11-24 stsp }
5979 6458efa5 2020-11-24 stsp
5980 6458efa5 2020-11-24 stsp void
5981 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5982 6458efa5 2020-11-24 stsp {
5983 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5984 6458efa5 2020-11-24 stsp
5985 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5986 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5987 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5988 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5989 6458efa5 2020-11-24 stsp free(re);
5990 6458efa5 2020-11-24 stsp }
5991 6458efa5 2020-11-24 stsp }
5992 6458efa5 2020-11-24 stsp
5993 6458efa5 2020-11-24 stsp static const struct got_error *
5994 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5995 6458efa5 2020-11-24 stsp {
5996 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5997 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5998 6458efa5 2020-11-24 stsp
5999 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6000 6458efa5 2020-11-24 stsp s->repo = repo;
6001 6458efa5 2020-11-24 stsp
6002 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6003 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6004 6458efa5 2020-11-24 stsp
6005 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6006 6458efa5 2020-11-24 stsp if (err)
6007 6458efa5 2020-11-24 stsp return err;
6008 34ba6917 2020-11-30 stsp
6009 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6010 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6011 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6012 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6013 6458efa5 2020-11-24 stsp if (err)
6014 6458efa5 2020-11-24 stsp goto done;
6015 6458efa5 2020-11-24 stsp
6016 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6017 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6018 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6019 6458efa5 2020-11-24 stsp if (err)
6020 6458efa5 2020-11-24 stsp goto done;
6021 6458efa5 2020-11-24 stsp
6022 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6023 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6024 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6025 cc488aa7 2022-01-23 stsp if (err)
6026 cc488aa7 2022-01-23 stsp goto done;
6027 cc488aa7 2022-01-23 stsp
6028 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6029 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6030 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6031 6458efa5 2020-11-24 stsp if (err)
6032 6458efa5 2020-11-24 stsp goto done;
6033 6458efa5 2020-11-24 stsp }
6034 6458efa5 2020-11-24 stsp
6035 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6036 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6037 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6038 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6039 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6040 6458efa5 2020-11-24 stsp done:
6041 6458efa5 2020-11-24 stsp if (err)
6042 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6043 6458efa5 2020-11-24 stsp return err;
6044 6458efa5 2020-11-24 stsp }
6045 6458efa5 2020-11-24 stsp
6046 6458efa5 2020-11-24 stsp static const struct got_error *
6047 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6048 6458efa5 2020-11-24 stsp {
6049 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6050 6458efa5 2020-11-24 stsp
6051 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6052 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6053 6458efa5 2020-11-24 stsp
6054 6458efa5 2020-11-24 stsp return NULL;
6055 9f7d7167 2018-04-29 stsp }
6056 ce5b7c56 2019-07-09 stsp
6057 6458efa5 2020-11-24 stsp static const struct got_error *
6058 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6059 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6060 6458efa5 2020-11-24 stsp {
6061 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6062 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6063 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6064 6458efa5 2020-11-24 stsp int obj_type;
6065 6458efa5 2020-11-24 stsp
6066 c42c9805 2020-11-24 stsp *commit_id = NULL;
6067 6458efa5 2020-11-24 stsp
6068 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6069 6458efa5 2020-11-24 stsp if (err)
6070 6458efa5 2020-11-24 stsp return err;
6071 6458efa5 2020-11-24 stsp
6072 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6073 6458efa5 2020-11-24 stsp if (err)
6074 6458efa5 2020-11-24 stsp goto done;
6075 6458efa5 2020-11-24 stsp
6076 6458efa5 2020-11-24 stsp switch (obj_type) {
6077 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6078 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6079 6458efa5 2020-11-24 stsp break;
6080 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6081 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6082 6458efa5 2020-11-24 stsp if (err)
6083 6458efa5 2020-11-24 stsp goto done;
6084 c42c9805 2020-11-24 stsp free(obj_id);
6085 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6086 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6087 c42c9805 2020-11-24 stsp if (err)
6088 6458efa5 2020-11-24 stsp goto done;
6089 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6090 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6091 c42c9805 2020-11-24 stsp goto done;
6092 c42c9805 2020-11-24 stsp }
6093 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6094 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6095 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6096 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6097 c42c9805 2020-11-24 stsp goto done;
6098 c42c9805 2020-11-24 stsp }
6099 6458efa5 2020-11-24 stsp break;
6100 6458efa5 2020-11-24 stsp default:
6101 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6102 c42c9805 2020-11-24 stsp break;
6103 6458efa5 2020-11-24 stsp }
6104 6458efa5 2020-11-24 stsp
6105 c42c9805 2020-11-24 stsp done:
6106 c42c9805 2020-11-24 stsp if (tag)
6107 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6108 c42c9805 2020-11-24 stsp if (err) {
6109 c42c9805 2020-11-24 stsp free(*commit_id);
6110 c42c9805 2020-11-24 stsp *commit_id = NULL;
6111 c42c9805 2020-11-24 stsp }
6112 c42c9805 2020-11-24 stsp return err;
6113 c42c9805 2020-11-24 stsp }
6114 c42c9805 2020-11-24 stsp
6115 c42c9805 2020-11-24 stsp static const struct got_error *
6116 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6117 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6118 c42c9805 2020-11-24 stsp {
6119 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6120 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6121 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6122 c42c9805 2020-11-24 stsp
6123 c42c9805 2020-11-24 stsp *new_view = NULL;
6124 c42c9805 2020-11-24 stsp
6125 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6126 c42c9805 2020-11-24 stsp if (err) {
6127 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6128 c42c9805 2020-11-24 stsp return err;
6129 c42c9805 2020-11-24 stsp else
6130 c42c9805 2020-11-24 stsp return NULL;
6131 c42c9805 2020-11-24 stsp }
6132 c42c9805 2020-11-24 stsp
6133 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6134 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6135 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6136 6458efa5 2020-11-24 stsp goto done;
6137 6458efa5 2020-11-24 stsp }
6138 6458efa5 2020-11-24 stsp
6139 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6140 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6141 6458efa5 2020-11-24 stsp done:
6142 6458efa5 2020-11-24 stsp if (err)
6143 6458efa5 2020-11-24 stsp view_close(log_view);
6144 6458efa5 2020-11-24 stsp else
6145 6458efa5 2020-11-24 stsp *new_view = log_view;
6146 c42c9805 2020-11-24 stsp free(commit_id);
6147 6458efa5 2020-11-24 stsp return err;
6148 6458efa5 2020-11-24 stsp }
6149 6458efa5 2020-11-24 stsp
6150 ce5b7c56 2019-07-09 stsp static void
6151 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6152 6458efa5 2020-11-24 stsp {
6153 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6154 34ba6917 2020-11-30 stsp int i = 0;
6155 6458efa5 2020-11-24 stsp
6156 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6157 6458efa5 2020-11-24 stsp return;
6158 6458efa5 2020-11-24 stsp
6159 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6160 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6161 34ba6917 2020-11-30 stsp if (re == NULL)
6162 34ba6917 2020-11-30 stsp break;
6163 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6164 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6165 6458efa5 2020-11-24 stsp }
6166 6458efa5 2020-11-24 stsp }
6167 6458efa5 2020-11-24 stsp
6168 34ba6917 2020-11-30 stsp static void
6169 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6170 6458efa5 2020-11-24 stsp {
6171 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6172 6458efa5 2020-11-24 stsp int n = 0;
6173 6458efa5 2020-11-24 stsp
6174 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6175 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6176 6458efa5 2020-11-24 stsp else
6177 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6178 6458efa5 2020-11-24 stsp
6179 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6180 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6181 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6182 6458efa5 2020-11-24 stsp if (last) {
6183 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6184 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6185 6458efa5 2020-11-24 stsp }
6186 6458efa5 2020-11-24 stsp }
6187 6458efa5 2020-11-24 stsp }
6188 6458efa5 2020-11-24 stsp
6189 6458efa5 2020-11-24 stsp static const struct got_error *
6190 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6191 6458efa5 2020-11-24 stsp {
6192 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6193 6458efa5 2020-11-24 stsp
6194 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6195 6458efa5 2020-11-24 stsp return NULL;
6196 6458efa5 2020-11-24 stsp }
6197 6458efa5 2020-11-24 stsp
6198 6458efa5 2020-11-24 stsp static int
6199 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6200 6458efa5 2020-11-24 stsp {
6201 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6202 6458efa5 2020-11-24 stsp
6203 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6204 6458efa5 2020-11-24 stsp 0) == 0;
6205 6458efa5 2020-11-24 stsp }
6206 6458efa5 2020-11-24 stsp
6207 6458efa5 2020-11-24 stsp static const struct got_error *
6208 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6209 6458efa5 2020-11-24 stsp {
6210 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6211 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6212 6458efa5 2020-11-24 stsp
6213 6458efa5 2020-11-24 stsp if (!view->searching) {
6214 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6215 6458efa5 2020-11-24 stsp return NULL;
6216 6458efa5 2020-11-24 stsp }
6217 6458efa5 2020-11-24 stsp
6218 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6219 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6220 6458efa5 2020-11-24 stsp if (s->selected_entry)
6221 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6222 6458efa5 2020-11-24 stsp else
6223 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6224 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6225 6458efa5 2020-11-24 stsp } else {
6226 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6227 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6228 6458efa5 2020-11-24 stsp else
6229 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6230 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6231 6458efa5 2020-11-24 stsp }
6232 6458efa5 2020-11-24 stsp } else {
6233 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6234 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6235 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6236 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6237 6458efa5 2020-11-24 stsp else
6238 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6239 6458efa5 2020-11-24 stsp }
6240 6458efa5 2020-11-24 stsp
6241 6458efa5 2020-11-24 stsp while (1) {
6242 6458efa5 2020-11-24 stsp if (re == NULL) {
6243 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6244 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6245 6458efa5 2020-11-24 stsp return NULL;
6246 6458efa5 2020-11-24 stsp }
6247 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6248 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6249 6458efa5 2020-11-24 stsp else
6250 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6251 6458efa5 2020-11-24 stsp }
6252 6458efa5 2020-11-24 stsp
6253 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6254 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6255 6458efa5 2020-11-24 stsp s->matched_entry = re;
6256 6458efa5 2020-11-24 stsp break;
6257 6458efa5 2020-11-24 stsp }
6258 6458efa5 2020-11-24 stsp
6259 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6260 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6261 6458efa5 2020-11-24 stsp else
6262 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6263 6458efa5 2020-11-24 stsp }
6264 6458efa5 2020-11-24 stsp
6265 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6266 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6267 6458efa5 2020-11-24 stsp s->selected = 0;
6268 6458efa5 2020-11-24 stsp }
6269 6458efa5 2020-11-24 stsp
6270 6458efa5 2020-11-24 stsp return NULL;
6271 6458efa5 2020-11-24 stsp }
6272 6458efa5 2020-11-24 stsp
6273 6458efa5 2020-11-24 stsp static const struct got_error *
6274 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6275 6458efa5 2020-11-24 stsp {
6276 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6277 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6278 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6279 6458efa5 2020-11-24 stsp char *line = NULL;
6280 6458efa5 2020-11-24 stsp wchar_t *wline;
6281 6458efa5 2020-11-24 stsp struct tog_color *tc;
6282 6458efa5 2020-11-24 stsp int width, n;
6283 6458efa5 2020-11-24 stsp int limit = view->nlines;
6284 6458efa5 2020-11-24 stsp
6285 6458efa5 2020-11-24 stsp werase(view->window);
6286 6458efa5 2020-11-24 stsp
6287 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6288 6458efa5 2020-11-24 stsp
6289 6458efa5 2020-11-24 stsp if (limit == 0)
6290 6458efa5 2020-11-24 stsp return NULL;
6291 6458efa5 2020-11-24 stsp
6292 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6293 6458efa5 2020-11-24 stsp
6294 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6295 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6296 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6297 6458efa5 2020-11-24 stsp
6298 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6299 6458efa5 2020-11-24 stsp if (err) {
6300 6458efa5 2020-11-24 stsp free(line);
6301 6458efa5 2020-11-24 stsp return err;
6302 6458efa5 2020-11-24 stsp }
6303 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6304 6458efa5 2020-11-24 stsp wstandout(view->window);
6305 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6306 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6307 6458efa5 2020-11-24 stsp wstandend(view->window);
6308 6458efa5 2020-11-24 stsp free(wline);
6309 6458efa5 2020-11-24 stsp wline = NULL;
6310 6458efa5 2020-11-24 stsp free(line);
6311 6458efa5 2020-11-24 stsp line = NULL;
6312 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6313 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6314 6458efa5 2020-11-24 stsp if (--limit <= 0)
6315 6458efa5 2020-11-24 stsp return NULL;
6316 6458efa5 2020-11-24 stsp
6317 6458efa5 2020-11-24 stsp n = 0;
6318 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6319 6458efa5 2020-11-24 stsp char *line = NULL;
6320 6458efa5 2020-11-24 stsp
6321 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6322 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6323 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6324 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6325 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6326 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6327 6458efa5 2020-11-24 stsp struct got_object_id *id;
6328 6458efa5 2020-11-24 stsp char *id_str;
6329 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6330 6458efa5 2020-11-24 stsp if (err)
6331 6458efa5 2020-11-24 stsp return err;
6332 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6333 6458efa5 2020-11-24 stsp if (err) {
6334 6458efa5 2020-11-24 stsp free(id);
6335 6458efa5 2020-11-24 stsp return err;
6336 6458efa5 2020-11-24 stsp }
6337 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6338 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6339 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6340 6458efa5 2020-11-24 stsp free(id);
6341 6458efa5 2020-11-24 stsp free(id_str);
6342 6458efa5 2020-11-24 stsp return err;
6343 6458efa5 2020-11-24 stsp }
6344 6458efa5 2020-11-24 stsp free(id);
6345 6458efa5 2020-11-24 stsp free(id_str);
6346 6458efa5 2020-11-24 stsp } else {
6347 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6348 6458efa5 2020-11-24 stsp if (line == NULL)
6349 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6350 6458efa5 2020-11-24 stsp }
6351 6458efa5 2020-11-24 stsp
6352 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6353 6458efa5 2020-11-24 stsp if (err) {
6354 6458efa5 2020-11-24 stsp free(line);
6355 6458efa5 2020-11-24 stsp return err;
6356 6458efa5 2020-11-24 stsp }
6357 6458efa5 2020-11-24 stsp if (n == s->selected) {
6358 6458efa5 2020-11-24 stsp if (view->focussed)
6359 6458efa5 2020-11-24 stsp wstandout(view->window);
6360 6458efa5 2020-11-24 stsp s->selected_entry = re;
6361 6458efa5 2020-11-24 stsp }
6362 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6363 6458efa5 2020-11-24 stsp if (tc)
6364 6458efa5 2020-11-24 stsp wattr_on(view->window,
6365 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6366 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6367 6458efa5 2020-11-24 stsp if (tc)
6368 6458efa5 2020-11-24 stsp wattr_off(view->window,
6369 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6370 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6371 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6372 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6373 6458efa5 2020-11-24 stsp wstandend(view->window);
6374 6458efa5 2020-11-24 stsp free(line);
6375 6458efa5 2020-11-24 stsp free(wline);
6376 6458efa5 2020-11-24 stsp wline = NULL;
6377 6458efa5 2020-11-24 stsp n++;
6378 6458efa5 2020-11-24 stsp s->ndisplayed++;
6379 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6380 6458efa5 2020-11-24 stsp
6381 6458efa5 2020-11-24 stsp limit--;
6382 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6383 6458efa5 2020-11-24 stsp }
6384 6458efa5 2020-11-24 stsp
6385 6458efa5 2020-11-24 stsp view_vborder(view);
6386 6458efa5 2020-11-24 stsp return err;
6387 6458efa5 2020-11-24 stsp }
6388 6458efa5 2020-11-24 stsp
6389 6458efa5 2020-11-24 stsp static const struct got_error *
6390 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6391 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6392 c42c9805 2020-11-24 stsp {
6393 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6394 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6395 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6396 c42c9805 2020-11-24 stsp
6397 c42c9805 2020-11-24 stsp *new_view = NULL;
6398 c42c9805 2020-11-24 stsp
6399 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6400 c42c9805 2020-11-24 stsp if (err) {
6401 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6402 c42c9805 2020-11-24 stsp return err;
6403 c42c9805 2020-11-24 stsp else
6404 c42c9805 2020-11-24 stsp return NULL;
6405 c42c9805 2020-11-24 stsp }
6406 c42c9805 2020-11-24 stsp
6407 c42c9805 2020-11-24 stsp
6408 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6409 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6410 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6411 c42c9805 2020-11-24 stsp goto done;
6412 c42c9805 2020-11-24 stsp }
6413 c42c9805 2020-11-24 stsp
6414 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6415 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6416 c42c9805 2020-11-24 stsp if (err)
6417 c42c9805 2020-11-24 stsp goto done;
6418 c42c9805 2020-11-24 stsp
6419 c42c9805 2020-11-24 stsp *new_view = tree_view;
6420 c42c9805 2020-11-24 stsp done:
6421 c42c9805 2020-11-24 stsp free(commit_id);
6422 c42c9805 2020-11-24 stsp return err;
6423 c42c9805 2020-11-24 stsp }
6424 c42c9805 2020-11-24 stsp static const struct got_error *
6425 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6426 6458efa5 2020-11-24 stsp {
6427 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6428 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6429 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6430 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6431 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
6432 6458efa5 2020-11-24 stsp
6433 6458efa5 2020-11-24 stsp switch (ch) {
6434 6458efa5 2020-11-24 stsp case 'i':
6435 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6436 7f66531d 2021-11-16 stsp break;
6437 07a065fe 2021-11-20 stsp case 'o':
6438 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6439 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6440 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6441 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6442 50617b77 2021-11-20 stsp if (err)
6443 50617b77 2021-11-20 stsp break;
6444 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6445 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6446 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6447 7f66531d 2021-11-16 stsp if (err)
6448 7f66531d 2021-11-16 stsp break;
6449 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6450 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6451 6458efa5 2020-11-24 stsp break;
6452 6458efa5 2020-11-24 stsp case KEY_ENTER:
6453 6458efa5 2020-11-24 stsp case '\r':
6454 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6455 6458efa5 2020-11-24 stsp break;
6456 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6457 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6458 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6459 6458efa5 2020-11-24 stsp s->repo);
6460 e78dc838 2020-12-04 stsp view->focussed = 0;
6461 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6462 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6463 6458efa5 2020-11-24 stsp err = view_close_child(view);
6464 6458efa5 2020-11-24 stsp if (err)
6465 6458efa5 2020-11-24 stsp return err;
6466 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6467 e78dc838 2020-12-04 stsp view->focus_child = 1;
6468 6458efa5 2020-11-24 stsp } else
6469 6458efa5 2020-11-24 stsp *new_view = log_view;
6470 6458efa5 2020-11-24 stsp break;
6471 c42c9805 2020-11-24 stsp case 't':
6472 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6473 c42c9805 2020-11-24 stsp break;
6474 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6475 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6476 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6477 c42c9805 2020-11-24 stsp s->repo);
6478 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6479 c42c9805 2020-11-24 stsp break;
6480 e78dc838 2020-12-04 stsp view->focussed = 0;
6481 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6482 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6483 c42c9805 2020-11-24 stsp err = view_close_child(view);
6484 c42c9805 2020-11-24 stsp if (err)
6485 c42c9805 2020-11-24 stsp return err;
6486 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6487 e78dc838 2020-12-04 stsp view->focus_child = 1;
6488 c42c9805 2020-11-24 stsp } else
6489 c42c9805 2020-11-24 stsp *new_view = tree_view;
6490 c42c9805 2020-11-24 stsp break;
6491 e4526bf5 2021-09-03 naddy case 'g':
6492 e4526bf5 2021-09-03 naddy case KEY_HOME:
6493 e4526bf5 2021-09-03 naddy s->selected = 0;
6494 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6495 e4526bf5 2021-09-03 naddy break;
6496 e4526bf5 2021-09-03 naddy case 'G':
6497 e4526bf5 2021-09-03 naddy case KEY_END:
6498 e4526bf5 2021-09-03 naddy s->selected = 0;
6499 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6500 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6501 e4526bf5 2021-09-03 naddy if (re == NULL)
6502 e4526bf5 2021-09-03 naddy break;
6503 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6504 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6505 e4526bf5 2021-09-03 naddy }
6506 e4526bf5 2021-09-03 naddy if (n > 0)
6507 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6508 e4526bf5 2021-09-03 naddy break;
6509 6458efa5 2020-11-24 stsp case 'k':
6510 6458efa5 2020-11-24 stsp case KEY_UP:
6511 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6512 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6513 6458efa5 2020-11-24 stsp s->selected--;
6514 6458efa5 2020-11-24 stsp break;
6515 34ba6917 2020-11-30 stsp }
6516 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6517 6458efa5 2020-11-24 stsp break;
6518 83cc4199 2022-06-13 stsp case CTRL('u'):
6519 33c3719a 2022-06-15 stsp case 'u':
6520 83cc4199 2022-06-13 stsp nscroll /= 2;
6521 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6522 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6523 6458efa5 2020-11-24 stsp case CTRL('b'):
6524 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6525 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
6526 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
6527 6458efa5 2020-11-24 stsp break;
6528 6458efa5 2020-11-24 stsp case 'j':
6529 6458efa5 2020-11-24 stsp case KEY_DOWN:
6530 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6531 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6532 6458efa5 2020-11-24 stsp s->selected++;
6533 6458efa5 2020-11-24 stsp break;
6534 6458efa5 2020-11-24 stsp }
6535 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6536 6458efa5 2020-11-24 stsp /* can't scroll any further */
6537 6458efa5 2020-11-24 stsp break;
6538 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6539 6458efa5 2020-11-24 stsp break;
6540 83cc4199 2022-06-13 stsp case CTRL('d'):
6541 33c3719a 2022-06-15 stsp case 'd':
6542 83cc4199 2022-06-13 stsp nscroll /= 2;
6543 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6544 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6545 6458efa5 2020-11-24 stsp case CTRL('f'):
6546 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6547 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6548 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6549 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6550 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6551 6458efa5 2020-11-24 stsp break;
6552 6458efa5 2020-11-24 stsp }
6553 83cc4199 2022-06-13 stsp ref_scroll_down(s, nscroll);
6554 6458efa5 2020-11-24 stsp break;
6555 6458efa5 2020-11-24 stsp case CTRL('l'):
6556 8924d611 2020-12-26 stsp tog_free_refs();
6557 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6558 8924d611 2020-12-26 stsp if (err)
6559 8924d611 2020-12-26 stsp break;
6560 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6561 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6562 6458efa5 2020-11-24 stsp break;
6563 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6564 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6565 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6566 6458efa5 2020-11-24 stsp break;
6567 6458efa5 2020-11-24 stsp default:
6568 6458efa5 2020-11-24 stsp break;
6569 6458efa5 2020-11-24 stsp }
6570 6458efa5 2020-11-24 stsp
6571 6458efa5 2020-11-24 stsp return err;
6572 6458efa5 2020-11-24 stsp }
6573 6458efa5 2020-11-24 stsp
6574 6458efa5 2020-11-24 stsp __dead static void
6575 6458efa5 2020-11-24 stsp usage_ref(void)
6576 6458efa5 2020-11-24 stsp {
6577 6458efa5 2020-11-24 stsp endwin();
6578 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6579 6458efa5 2020-11-24 stsp getprogname());
6580 6458efa5 2020-11-24 stsp exit(1);
6581 6458efa5 2020-11-24 stsp }
6582 6458efa5 2020-11-24 stsp
6583 6458efa5 2020-11-24 stsp static const struct got_error *
6584 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6585 6458efa5 2020-11-24 stsp {
6586 6458efa5 2020-11-24 stsp const struct got_error *error;
6587 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6588 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6589 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6590 6458efa5 2020-11-24 stsp int ch;
6591 6458efa5 2020-11-24 stsp struct tog_view *view;
6592 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6593 6458efa5 2020-11-24 stsp
6594 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6595 6458efa5 2020-11-24 stsp switch (ch) {
6596 6458efa5 2020-11-24 stsp case 'r':
6597 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6598 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6599 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6600 6458efa5 2020-11-24 stsp optarg);
6601 6458efa5 2020-11-24 stsp break;
6602 6458efa5 2020-11-24 stsp default:
6603 e99e2d15 2020-11-24 naddy usage_ref();
6604 6458efa5 2020-11-24 stsp /* NOTREACHED */
6605 6458efa5 2020-11-24 stsp }
6606 6458efa5 2020-11-24 stsp }
6607 6458efa5 2020-11-24 stsp
6608 6458efa5 2020-11-24 stsp argc -= optind;
6609 6458efa5 2020-11-24 stsp argv += optind;
6610 6458efa5 2020-11-24 stsp
6611 6458efa5 2020-11-24 stsp if (argc > 1)
6612 e99e2d15 2020-11-24 naddy usage_ref();
6613 0ae84acc 2022-06-15 tracey
6614 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6615 0ae84acc 2022-06-15 tracey if (error != NULL)
6616 0ae84acc 2022-06-15 tracey goto done;
6617 6458efa5 2020-11-24 stsp
6618 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6619 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6620 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6621 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6622 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6623 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6624 c156c7a4 2020-12-18 stsp goto done;
6625 6458efa5 2020-11-24 stsp if (worktree)
6626 6458efa5 2020-11-24 stsp repo_path =
6627 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6628 6458efa5 2020-11-24 stsp else
6629 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6630 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6631 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6632 c156c7a4 2020-12-18 stsp goto done;
6633 c156c7a4 2020-12-18 stsp }
6634 6458efa5 2020-11-24 stsp }
6635 6458efa5 2020-11-24 stsp
6636 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6637 6458efa5 2020-11-24 stsp if (error != NULL)
6638 6458efa5 2020-11-24 stsp goto done;
6639 6458efa5 2020-11-24 stsp
6640 6458efa5 2020-11-24 stsp init_curses();
6641 6458efa5 2020-11-24 stsp
6642 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6643 51a10b52 2020-12-26 stsp if (error)
6644 51a10b52 2020-12-26 stsp goto done;
6645 51a10b52 2020-12-26 stsp
6646 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6647 6458efa5 2020-11-24 stsp if (error)
6648 6458efa5 2020-11-24 stsp goto done;
6649 6458efa5 2020-11-24 stsp
6650 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6651 6458efa5 2020-11-24 stsp if (view == NULL) {
6652 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6653 6458efa5 2020-11-24 stsp goto done;
6654 6458efa5 2020-11-24 stsp }
6655 6458efa5 2020-11-24 stsp
6656 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6657 6458efa5 2020-11-24 stsp if (error)
6658 6458efa5 2020-11-24 stsp goto done;
6659 6458efa5 2020-11-24 stsp
6660 6458efa5 2020-11-24 stsp if (worktree) {
6661 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6662 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6663 6458efa5 2020-11-24 stsp worktree = NULL;
6664 6458efa5 2020-11-24 stsp }
6665 6458efa5 2020-11-24 stsp error = view_loop(view);
6666 6458efa5 2020-11-24 stsp done:
6667 6458efa5 2020-11-24 stsp free(repo_path);
6668 6458efa5 2020-11-24 stsp free(cwd);
6669 1d0f4054 2021-06-17 stsp if (repo) {
6670 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6671 1d0f4054 2021-06-17 stsp if (close_err)
6672 1d0f4054 2021-06-17 stsp error = close_err;
6673 1d0f4054 2021-06-17 stsp }
6674 0ae84acc 2022-06-15 tracey if (pack_fds) {
6675 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6676 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6677 0ae84acc 2022-06-15 tracey if (error == NULL)
6678 0ae84acc 2022-06-15 tracey error = pack_err;
6679 bb1c2cf3 2022-06-15 tracey pack_fds = NULL;
6680 0ae84acc 2022-06-15 tracey }
6681 51a10b52 2020-12-26 stsp tog_free_refs();
6682 6458efa5 2020-11-24 stsp return error;
6683 6458efa5 2020-11-24 stsp }
6684 6458efa5 2020-11-24 stsp
6685 6458efa5 2020-11-24 stsp static void
6686 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6687 ce5b7c56 2019-07-09 stsp {
6688 6059809a 2020-12-17 stsp size_t i;
6689 9f7d7167 2018-04-29 stsp
6690 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6691 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6692 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
6693 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6694 ce5b7c56 2019-07-09 stsp }
6695 6879ba42 2020-10-01 naddy fputc('\n', fp);
6696 ce5b7c56 2019-07-09 stsp }
6697 ce5b7c56 2019-07-09 stsp
6698 4ed7e80c 2018-05-20 stsp __dead static void
6699 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6700 9f7d7167 2018-04-29 stsp {
6701 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6702 6879ba42 2020-10-01 naddy
6703 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6704 6879ba42 2020-10-01 naddy getprogname());
6705 6879ba42 2020-10-01 naddy if (hflag) {
6706 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6707 6879ba42 2020-10-01 naddy list_commands(fp);
6708 ee85c5e8 2020-02-29 stsp }
6709 6879ba42 2020-10-01 naddy exit(status);
6710 9f7d7167 2018-04-29 stsp }
6711 9f7d7167 2018-04-29 stsp
6712 c2301be8 2018-04-30 stsp static char **
6713 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6714 c2301be8 2018-04-30 stsp {
6715 ee85c5e8 2020-02-29 stsp va_list ap;
6716 c2301be8 2018-04-30 stsp char **argv;
6717 ee85c5e8 2020-02-29 stsp int i;
6718 c2301be8 2018-04-30 stsp
6719 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6720 ee85c5e8 2020-02-29 stsp
6721 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6722 c2301be8 2018-04-30 stsp if (argv == NULL)
6723 c2301be8 2018-04-30 stsp err(1, "calloc");
6724 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6725 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6726 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6727 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6728 c2301be8 2018-04-30 stsp }
6729 c2301be8 2018-04-30 stsp
6730 ee85c5e8 2020-02-29 stsp va_end(ap);
6731 c2301be8 2018-04-30 stsp return argv;
6732 ee85c5e8 2020-02-29 stsp }
6733 ee85c5e8 2020-02-29 stsp
6734 ee85c5e8 2020-02-29 stsp /*
6735 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6736 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6737 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6738 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6739 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6740 ee85c5e8 2020-02-29 stsp */
6741 ee85c5e8 2020-02-29 stsp static const struct got_error *
6742 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6743 ee85c5e8 2020-02-29 stsp {
6744 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6745 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6746 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6747 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6748 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6749 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6750 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6751 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6752 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6753 84de9106 2020-12-26 stsp
6754 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6755 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6756 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6757 ee85c5e8 2020-02-29 stsp
6758 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6759 0ae84acc 2022-06-15 tracey if (error != NULL)
6760 0ae84acc 2022-06-15 tracey goto done;
6761 0ae84acc 2022-06-15 tracey
6762 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6763 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6764 ee85c5e8 2020-02-29 stsp goto done;
6765 ee85c5e8 2020-02-29 stsp
6766 ee85c5e8 2020-02-29 stsp if (worktree)
6767 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6768 ee85c5e8 2020-02-29 stsp else
6769 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6770 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6771 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6772 ee85c5e8 2020-02-29 stsp goto done;
6773 ee85c5e8 2020-02-29 stsp }
6774 ee85c5e8 2020-02-29 stsp
6775 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6776 ee85c5e8 2020-02-29 stsp if (error != NULL)
6777 ee85c5e8 2020-02-29 stsp goto done;
6778 ee85c5e8 2020-02-29 stsp
6779 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6780 ee85c5e8 2020-02-29 stsp repo, worktree);
6781 ee85c5e8 2020-02-29 stsp if (error)
6782 ee85c5e8 2020-02-29 stsp goto done;
6783 ee85c5e8 2020-02-29 stsp
6784 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6785 84de9106 2020-12-26 stsp if (error)
6786 84de9106 2020-12-26 stsp goto done;
6787 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6788 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6789 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6790 ee85c5e8 2020-02-29 stsp if (error)
6791 ee85c5e8 2020-02-29 stsp goto done;
6792 ee85c5e8 2020-02-29 stsp
6793 ee85c5e8 2020-02-29 stsp if (worktree) {
6794 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6795 ee85c5e8 2020-02-29 stsp worktree = NULL;
6796 ee85c5e8 2020-02-29 stsp }
6797 ee85c5e8 2020-02-29 stsp
6798 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6799 a44927cc 2022-04-07 stsp if (error)
6800 a44927cc 2022-04-07 stsp goto done;
6801 a44927cc 2022-04-07 stsp
6802 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6803 ee85c5e8 2020-02-29 stsp if (error) {
6804 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6805 ee85c5e8 2020-02-29 stsp goto done;
6806 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6807 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6808 6879ba42 2020-10-01 naddy usage(1, 1);
6809 ee85c5e8 2020-02-29 stsp /* not reached */
6810 ee85c5e8 2020-02-29 stsp }
6811 ee85c5e8 2020-02-29 stsp
6812 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6813 1d0f4054 2021-06-17 stsp if (error == NULL)
6814 1d0f4054 2021-06-17 stsp error = close_err;
6815 ee85c5e8 2020-02-29 stsp repo = NULL;
6816 ee85c5e8 2020-02-29 stsp
6817 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6818 ee85c5e8 2020-02-29 stsp if (error)
6819 ee85c5e8 2020-02-29 stsp goto done;
6820 ee85c5e8 2020-02-29 stsp
6821 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6822 ee85c5e8 2020-02-29 stsp argc = 4;
6823 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6824 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6825 ee85c5e8 2020-02-29 stsp done:
6826 1d0f4054 2021-06-17 stsp if (repo) {
6827 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6828 1d0f4054 2021-06-17 stsp if (error == NULL)
6829 1d0f4054 2021-06-17 stsp error = close_err;
6830 1d0f4054 2021-06-17 stsp }
6831 a44927cc 2022-04-07 stsp if (commit)
6832 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6833 ee85c5e8 2020-02-29 stsp if (worktree)
6834 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6835 0ae84acc 2022-06-15 tracey if (pack_fds) {
6836 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6837 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6838 0ae84acc 2022-06-15 tracey if (error == NULL)
6839 0ae84acc 2022-06-15 tracey error = pack_err;
6840 bb1c2cf3 2022-06-15 tracey pack_fds = NULL;
6841 0ae84acc 2022-06-15 tracey }
6842 ee85c5e8 2020-02-29 stsp free(id);
6843 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6844 ee85c5e8 2020-02-29 stsp free(commit_id);
6845 ee85c5e8 2020-02-29 stsp free(cwd);
6846 ee85c5e8 2020-02-29 stsp free(repo_path);
6847 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6848 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6849 ee85c5e8 2020-02-29 stsp int i;
6850 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6851 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6852 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6853 ee85c5e8 2020-02-29 stsp }
6854 87670572 2020-12-26 naddy tog_free_refs();
6855 ee85c5e8 2020-02-29 stsp return error;
6856 c2301be8 2018-04-30 stsp }
6857 c2301be8 2018-04-30 stsp
6858 9f7d7167 2018-04-29 stsp int
6859 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6860 9f7d7167 2018-04-29 stsp {
6861 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6862 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6863 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6864 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6865 3e166534 2022-02-16 naddy static const struct option longopts[] = {
6866 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6867 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6868 83cd27f8 2020-01-13 stsp };
6869 9f7d7167 2018-04-29 stsp
6870 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6871 9f7d7167 2018-04-29 stsp
6872 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6873 9f7d7167 2018-04-29 stsp switch (ch) {
6874 9f7d7167 2018-04-29 stsp case 'h':
6875 9f7d7167 2018-04-29 stsp hflag = 1;
6876 9f7d7167 2018-04-29 stsp break;
6877 53ccebc2 2019-07-30 stsp case 'V':
6878 53ccebc2 2019-07-30 stsp Vflag = 1;
6879 53ccebc2 2019-07-30 stsp break;
6880 9f7d7167 2018-04-29 stsp default:
6881 6879ba42 2020-10-01 naddy usage(hflag, 1);
6882 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6883 9f7d7167 2018-04-29 stsp }
6884 9f7d7167 2018-04-29 stsp }
6885 9f7d7167 2018-04-29 stsp
6886 9f7d7167 2018-04-29 stsp argc -= optind;
6887 9f7d7167 2018-04-29 stsp argv += optind;
6888 9814e6a3 2020-09-27 naddy optind = 1;
6889 c2301be8 2018-04-30 stsp optreset = 1;
6890 9f7d7167 2018-04-29 stsp
6891 53ccebc2 2019-07-30 stsp if (Vflag) {
6892 53ccebc2 2019-07-30 stsp got_version_print_str();
6893 6879ba42 2020-10-01 naddy return 0;
6894 53ccebc2 2019-07-30 stsp }
6895 4010e238 2020-12-04 stsp
6896 4010e238 2020-12-04 stsp #ifndef PROFILE
6897 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6898 4010e238 2020-12-04 stsp NULL) == -1)
6899 4010e238 2020-12-04 stsp err(1, "pledge");
6900 4010e238 2020-12-04 stsp #endif
6901 53ccebc2 2019-07-30 stsp
6902 c2301be8 2018-04-30 stsp if (argc == 0) {
6903 f29d3e89 2018-06-23 stsp if (hflag)
6904 6879ba42 2020-10-01 naddy usage(hflag, 0);
6905 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6906 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6907 c2301be8 2018-04-30 stsp argc = 1;
6908 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6909 c2301be8 2018-04-30 stsp } else {
6910 6059809a 2020-12-17 stsp size_t i;
6911 9f7d7167 2018-04-29 stsp
6912 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6913 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6914 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6915 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6916 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6917 9f7d7167 2018-04-29 stsp break;
6918 9f7d7167 2018-04-29 stsp }
6919 9f7d7167 2018-04-29 stsp }
6920 ee85c5e8 2020-02-29 stsp }
6921 3642c4c6 2019-07-09 stsp
6922 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6923 ee85c5e8 2020-02-29 stsp if (argc != 1)
6924 6879ba42 2020-10-01 naddy usage(0, 1);
6925 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6926 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6927 ee85c5e8 2020-02-29 stsp } else {
6928 ee85c5e8 2020-02-29 stsp if (hflag)
6929 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6930 ee85c5e8 2020-02-29 stsp else
6931 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6932 9f7d7167 2018-04-29 stsp }
6933 9f7d7167 2018-04-29 stsp
6934 9f7d7167 2018-04-29 stsp endwin();
6935 b46c1e04 2020-09-20 naddy putchar('\n');
6936 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6937 a2f4a359 2020-02-28 stsp int i;
6938 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6939 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6940 a2f4a359 2020-02-28 stsp free(cmd_argv);
6941 a2f4a359 2020-02-28 stsp }
6942 a2f4a359 2020-02-28 stsp
6943 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6944 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6945 9f7d7167 2018-04-29 stsp return 0;
6946 9f7d7167 2018-04-29 stsp }