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