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 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
19 80ddbec8 2018-04-29 stsp
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 def2f970 2021-09-28 thomas #endif
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
45 dd038bc6 2021-09-21 thomas.ad
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 9f7d7167 2018-04-29 stsp
61 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
62 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
63 dd038bc6 2021-09-21 thomas.ad
64 881b2d3e 2018-04-30 stsp #ifndef MIN
65 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 881b2d3e 2018-04-30 stsp #endif
67 881b2d3e 2018-04-30 stsp
68 2bd27830 2018-10-22 stsp #ifndef MAX
69 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 2bd27830 2018-10-22 stsp #endif
71 2bd27830 2018-10-22 stsp
72 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
73 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 acf52a76 2021-09-21 thomas.ad #endif
75 2bd27830 2018-10-22 stsp
76 9f7d7167 2018-04-29 stsp #ifndef nitems
77 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp #endif
79 9f7d7167 2018-04-29 stsp
80 9f7d7167 2018-04-29 stsp struct tog_cmd {
81 c2301be8 2018-04-30 stsp const char *name;
82 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 9f7d7167 2018-04-29 stsp };
85 9f7d7167 2018-04-29 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
90 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
91 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
92 9f7d7167 2018-04-29 stsp
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
97 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
98 9f7d7167 2018-04-29 stsp
99 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
100 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
101 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
102 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
103 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
104 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 9f7d7167 2018-04-29 stsp };
106 9f7d7167 2018-04-29 stsp
107 d6b05b5b 2018-08-04 stsp enum tog_view_type {
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
110 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
111 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
112 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
113 d6b05b5b 2018-08-04 stsp };
114 c3e9aa98 2019-05-13 jcs
115 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
116 d6b05b5b 2018-08-04 stsp
117 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
118 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
119 ba4f502b 2018-08-04 stsp struct got_object_id *id;
120 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
121 1a76625f 2018-10-22 stsp int idx;
122 ba4f502b 2018-08-04 stsp };
123 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 ba4f502b 2018-08-04 stsp struct commit_queue {
125 ba4f502b 2018-08-04 stsp int ncommits;
126 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
127 6d17833f 2019-11-08 stsp };
128 6d17833f 2019-11-08 stsp
129 f26dddb7 2019-11-08 stsp struct tog_color {
130 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
131 6d17833f 2019-11-08 stsp regex_t regex;
132 6d17833f 2019-11-08 stsp short colorpair;
133 15a087fe 2019-02-21 stsp };
134 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
135 11b20872 2019-11-08 stsp
136 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
138 2183bbf6 2022-01-23 thomas
139 2183bbf6 2022-01-23 thomas static const struct got_error *
140 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 2183bbf6 2022-01-23 thomas struct got_reference* re2)
142 2183bbf6 2022-01-23 thomas {
143 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
144 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
145 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
146 2183bbf6 2022-01-23 thomas
147 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
148 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
151 2183bbf6 2022-01-23 thomas *cmp = -1;
152 2183bbf6 2022-01-23 thomas return NULL;
153 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
154 2183bbf6 2022-01-23 thomas *cmp = 1;
155 2183bbf6 2022-01-23 thomas return NULL;
156 2183bbf6 2022-01-23 thomas }
157 2183bbf6 2022-01-23 thomas
158 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas }
161 51a10b52 2020-12-26 stsp
162 11b20872 2019-11-08 stsp static const struct got_error *
163 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
164 51a10b52 2020-12-26 stsp {
165 51a10b52 2020-12-26 stsp const struct got_error *err;
166 51a10b52 2020-12-26 stsp
167 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 3bfadbd4 2021-11-20 thomas repo);
170 51a10b52 2020-12-26 stsp if (err)
171 51a10b52 2020-12-26 stsp return err;
172 51a10b52 2020-12-26 stsp
173 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 51a10b52 2020-12-26 stsp repo);
175 51a10b52 2020-12-26 stsp }
176 51a10b52 2020-12-26 stsp
177 51a10b52 2020-12-26 stsp static void
178 51a10b52 2020-12-26 stsp tog_free_refs(void)
179 51a10b52 2020-12-26 stsp {
180 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
181 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
182 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
183 51a10b52 2020-12-26 stsp }
184 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp
187 51a10b52 2020-12-26 stsp static const struct got_error *
188 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
189 11b20872 2019-11-08 stsp int idx, short color)
190 11b20872 2019-11-08 stsp {
191 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
192 11b20872 2019-11-08 stsp struct tog_color *tc;
193 11b20872 2019-11-08 stsp int regerr = 0;
194 11b20872 2019-11-08 stsp
195 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
196 11b20872 2019-11-08 stsp return NULL;
197 11b20872 2019-11-08 stsp
198 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
201 11b20872 2019-11-08 stsp if (tc == NULL)
202 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
203 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
204 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 11b20872 2019-11-08 stsp if (regerr) {
206 11b20872 2019-11-08 stsp static char regerr_msg[512];
207 11b20872 2019-11-08 stsp static char err_msg[512];
208 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
209 11b20872 2019-11-08 stsp sizeof(regerr_msg));
210 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 11b20872 2019-11-08 stsp regerr_msg);
212 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 11b20872 2019-11-08 stsp free(tc);
214 11b20872 2019-11-08 stsp return err;
215 11b20872 2019-11-08 stsp }
216 11b20872 2019-11-08 stsp tc->colorpair = idx;
217 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
218 11b20872 2019-11-08 stsp return NULL;
219 11b20872 2019-11-08 stsp }
220 11b20872 2019-11-08 stsp
221 11b20872 2019-11-08 stsp static void
222 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
223 11b20872 2019-11-08 stsp {
224 11b20872 2019-11-08 stsp struct tog_color *tc;
225 11b20872 2019-11-08 stsp
226 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
227 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
228 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
229 11b20872 2019-11-08 stsp regfree(&tc->regex);
230 11b20872 2019-11-08 stsp free(tc);
231 11b20872 2019-11-08 stsp }
232 11b20872 2019-11-08 stsp }
233 11b20872 2019-11-08 stsp
234 11b20872 2019-11-08 stsp struct tog_color *
235 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
236 11b20872 2019-11-08 stsp {
237 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
238 11b20872 2019-11-08 stsp
239 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
240 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
241 11b20872 2019-11-08 stsp return tc;
242 11b20872 2019-11-08 stsp }
243 11b20872 2019-11-08 stsp
244 11b20872 2019-11-08 stsp return NULL;
245 11b20872 2019-11-08 stsp }
246 11b20872 2019-11-08 stsp
247 11b20872 2019-11-08 stsp static int
248 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
249 11b20872 2019-11-08 stsp {
250 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_CYAN;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 11b20872 2019-11-08 stsp return COLOR_YELLOW;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 11b20872 2019-11-08 stsp return COLOR_GREEN;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_CYAN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_GREEN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 6458efa5 2020-11-24 stsp return COLOR_GREEN;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
278 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
280 11b20872 2019-11-08 stsp
281 11b20872 2019-11-08 stsp return -1;
282 11b20872 2019-11-08 stsp }
283 11b20872 2019-11-08 stsp
284 11b20872 2019-11-08 stsp static int
285 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
286 11b20872 2019-11-08 stsp {
287 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
288 11b20872 2019-11-08 stsp
289 11b20872 2019-11-08 stsp if (val == NULL)
290 11b20872 2019-11-08 stsp return default_color_value(envvar);
291 15a087fe 2019-02-21 stsp
292 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
293 11b20872 2019-11-08 stsp return COLOR_BLACK;
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
295 11b20872 2019-11-08 stsp return COLOR_RED;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
297 11b20872 2019-11-08 stsp return COLOR_GREEN;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
299 11b20872 2019-11-08 stsp return COLOR_YELLOW;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
301 11b20872 2019-11-08 stsp return COLOR_BLUE;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
303 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
305 11b20872 2019-11-08 stsp return COLOR_CYAN;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
307 11b20872 2019-11-08 stsp return COLOR_WHITE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
309 11b20872 2019-11-08 stsp return -1;
310 11b20872 2019-11-08 stsp
311 11b20872 2019-11-08 stsp return default_color_value(envvar);
312 11b20872 2019-11-08 stsp }
313 11b20872 2019-11-08 stsp
314 11b20872 2019-11-08 stsp
315 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
316 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
317 3dbaef42 2020-11-24 stsp const char *label1, *label2;
318 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
319 15a087fe 2019-02-21 stsp int first_displayed_line;
320 15a087fe 2019-02-21 stsp int last_displayed_line;
321 15a087fe 2019-02-21 stsp int eof;
322 15a087fe 2019-02-21 stsp int diff_context;
323 3dbaef42 2020-11-24 stsp int ignore_whitespace;
324 64453f7e 2020-11-21 stsp int force_text_diff;
325 15a087fe 2019-02-21 stsp struct got_repository *repo;
326 bddb1296 2019-11-08 stsp struct tog_colors colors;
327 fe621944 2020-11-10 stsp size_t nlines;
328 f44b1f58 2020-02-02 tracey off_t *line_offsets;
329 f44b1f58 2020-02-02 tracey int matched_line;
330 f44b1f58 2020-02-02 tracey int selected_line;
331 15a087fe 2019-02-21 stsp
332 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
333 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
334 b01e7d3b 2018-08-04 stsp };
335 b01e7d3b 2018-08-04 stsp
336 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
337 1a76625f 2018-10-22 stsp
338 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
339 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
340 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
341 1a76625f 2018-10-22 stsp int commits_needed;
342 fb280deb 2021-08-30 stsp int load_all;
343 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
344 1a76625f 2018-10-22 stsp struct commit_queue *commits;
345 1a76625f 2018-10-22 stsp const char *in_repo_path;
346 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
347 1a76625f 2018-10-22 stsp struct got_repository *repo;
348 1af5eddf 2022-06-23 thomas int *pack_fds;
349 1a76625f 2018-10-22 stsp int log_complete;
350 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
352 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
353 13add988 2019-10-15 stsp int *searching;
354 13add988 2019-10-15 stsp int *search_next_done;
355 13add988 2019-10-15 stsp regex_t *regex;
356 1a76625f 2018-10-22 stsp };
357 1a76625f 2018-10-22 stsp
358 1a76625f 2018-10-22 stsp struct tog_log_view_state {
359 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
363 b01e7d3b 2018-08-04 stsp int selected;
364 b01e7d3b 2018-08-04 stsp char *in_repo_path;
365 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
366 b672a97a 2020-01-27 stsp int log_branches;
367 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
368 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
369 1a76625f 2018-10-22 stsp sig_atomic_t quit;
370 1a76625f 2018-10-22 stsp pthread_t thread;
371 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
372 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
373 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
374 11b20872 2019-11-08 stsp struct tog_colors colors;
375 ba4f502b 2018-08-04 stsp };
376 11b20872 2019-11-08 stsp
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
385 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
386 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
387 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
391 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
392 ba4f502b 2018-08-04 stsp
393 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
394 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
395 e9424729 2018-08-04 stsp int nlines;
396 e9424729 2018-08-04 stsp
397 e9424729 2018-08-04 stsp struct tog_view *view;
398 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
399 e9424729 2018-08-04 stsp int *quit;
400 e9424729 2018-08-04 stsp };
401 e9424729 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
403 e9424729 2018-08-04 stsp const char *path;
404 e9424729 2018-08-04 stsp struct got_repository *repo;
405 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
406 e9424729 2018-08-04 stsp int *complete;
407 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
408 fc06ba56 2019-08-22 stsp void *cancel_arg;
409 e9424729 2018-08-04 stsp };
410 e9424729 2018-08-04 stsp
411 e9424729 2018-08-04 stsp struct tog_blame {
412 e9424729 2018-08-04 stsp FILE *f;
413 be659d10 2020-11-18 stsp off_t filesize;
414 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
415 6fcac457 2018-11-19 stsp int nlines;
416 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
417 e9424729 2018-08-04 stsp pthread_t thread;
418 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
420 e9424729 2018-08-04 stsp const char *path;
421 7cd52833 2022-06-23 thomas int *pack_fds;
422 e9424729 2018-08-04 stsp };
423 e9424729 2018-08-04 stsp
424 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
425 7cbe629d 2018-08-04 stsp int first_displayed_line;
426 7cbe629d 2018-08-04 stsp int last_displayed_line;
427 7cbe629d 2018-08-04 stsp int selected_line;
428 7cbe629d 2018-08-04 stsp int blame_complete;
429 e5a0f69f 2018-08-18 stsp int eof;
430 e5a0f69f 2018-08-18 stsp int done;
431 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
432 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
433 e5a0f69f 2018-08-18 stsp char *path;
434 7cbe629d 2018-08-04 stsp struct got_repository *repo;
435 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
436 e9424729 2018-08-04 stsp struct tog_blame blame;
437 6c4c42e0 2019-06-24 stsp int matched_line;
438 11b20872 2019-11-08 stsp struct tog_colors colors;
439 ad80ab7b 2018-08-04 stsp };
440 ad80ab7b 2018-08-04 stsp
441 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
442 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
444 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
445 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
446 ad80ab7b 2018-08-04 stsp int selected;
447 ad80ab7b 2018-08-04 stsp };
448 ad80ab7b 2018-08-04 stsp
449 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
450 ad80ab7b 2018-08-04 stsp
451 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
452 ad80ab7b 2018-08-04 stsp char *tree_label;
453 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
454 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
455 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
459 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
460 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
462 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
463 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
464 6458efa5 2020-11-24 stsp struct tog_colors colors;
465 6458efa5 2020-11-24 stsp };
466 6458efa5 2020-11-24 stsp
467 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
468 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
469 6458efa5 2020-11-24 stsp struct got_reference *ref;
470 6458efa5 2020-11-24 stsp int idx;
471 6458efa5 2020-11-24 stsp };
472 6458efa5 2020-11-24 stsp
473 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
474 6458efa5 2020-11-24 stsp
475 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
476 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
480 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
481 6458efa5 2020-11-24 stsp struct got_repository *repo;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
483 bddb1296 2019-11-08 stsp struct tog_colors colors;
484 7cbe629d 2018-08-04 stsp };
485 7cbe629d 2018-08-04 stsp
486 669b5ffa 2018-10-07 stsp /*
487 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
488 669b5ffa 2018-10-07 stsp *
489 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
490 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
491 669b5ffa 2018-10-07 stsp * there is enough screen estate.
492 669b5ffa 2018-10-07 stsp *
493 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
494 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
495 669b5ffa 2018-10-07 stsp *
496 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
497 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
498 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
499 669b5ffa 2018-10-07 stsp *
500 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
501 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
502 669b5ffa 2018-10-07 stsp */
503 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
504 669b5ffa 2018-10-07 stsp
505 cc3c9aac 2018-08-01 stsp struct tog_view {
506 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
507 26ed57b2 2018-05-19 stsp WINDOW *window;
508 26ed57b2 2018-05-19 stsp PANEL *panel;
509 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
510 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
511 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
512 9970f7fc 2020-12-03 stsp int dying;
513 669b5ffa 2018-10-07 stsp struct tog_view *parent;
514 669b5ffa 2018-10-07 stsp struct tog_view *child;
515 5dc9f4bc 2018-08-04 stsp
516 e78dc838 2020-12-04 stsp /*
517 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
518 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
519 e78dc838 2020-12-04 stsp * between parent and child.
520 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
521 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
522 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
523 e78dc838 2020-12-04 stsp * situations.
524 e78dc838 2020-12-04 stsp */
525 e78dc838 2020-12-04 stsp int focus_child;
526 e78dc838 2020-12-04 stsp
527 5dc9f4bc 2018-08-04 stsp /* type-specific state */
528 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
529 5dc9f4bc 2018-08-04 stsp union {
530 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
531 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
532 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
533 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
534 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
535 5dc9f4bc 2018-08-04 stsp } state;
536 e5a0f69f 2018-08-18 stsp
537 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
539 e78dc838 2020-12-04 stsp struct tog_view *, int);
540 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
541 60493ae3 2019-06-20 stsp
542 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
543 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
544 c0c4acc8 2021-01-24 stsp int search_started;
545 60493ae3 2019-06-20 stsp int searching;
546 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
547 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
548 60493ae3 2019-06-20 stsp int search_next_done;
549 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
550 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
551 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
552 1803e47f 2019-06-22 stsp regex_t regex;
553 41605754 2020-11-12 stsp regmatch_t regmatch;
554 cc3c9aac 2018-08-01 stsp };
555 cd0acaa7 2018-05-20 stsp
556 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
557 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
558 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
559 78756c87 2020-11-24 stsp struct got_repository *);
560 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
561 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_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_diff_view(struct tog_view *);
564 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
565 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
566 e5a0f69f 2018-08-18 stsp
567 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
568 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
569 78756c87 2020-11-24 stsp const char *, const char *, int);
570 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
572 e78dc838 2020-12-04 stsp struct tog_view *, int);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
574 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
575 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp
577 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
578 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
579 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
583 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
584 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
585 e5a0f69f 2018-08-18 stsp
586 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
587 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
588 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
592 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
593 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
594 6458efa5 2020-11-24 stsp
595 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
596 6458efa5 2020-11-24 stsp struct got_repository *);
597 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
598 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
599 e78dc838 2020-12-04 stsp struct tog_view *, int);
600 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
601 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
602 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
603 25791caa 2018-10-24 stsp
604 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
605 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
606 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
607 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
608 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
609 25791caa 2018-10-24 stsp
610 25791caa 2018-10-24 stsp static void
611 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
612 25791caa 2018-10-24 stsp {
613 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
614 83baff54 2019-08-12 stsp }
615 83baff54 2019-08-12 stsp
616 83baff54 2019-08-12 stsp static void
617 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
618 83baff54 2019-08-12 stsp {
619 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
620 25791caa 2018-10-24 stsp }
621 26ed57b2 2018-05-19 stsp
622 61266923 2020-01-14 stsp static void
623 61266923 2020-01-14 stsp tog_sigcont(int signo)
624 61266923 2020-01-14 stsp {
625 61266923 2020-01-14 stsp tog_sigcont_received = 1;
626 61266923 2020-01-14 stsp }
627 61266923 2020-01-14 stsp
628 296152d1 2022-05-31 thomas static void
629 296152d1 2022-05-31 thomas tog_sigint(int signo)
630 296152d1 2022-05-31 thomas {
631 296152d1 2022-05-31 thomas tog_sigint_received = 1;
632 296152d1 2022-05-31 thomas }
633 296152d1 2022-05-31 thomas
634 296152d1 2022-05-31 thomas static void
635 296152d1 2022-05-31 thomas tog_sigterm(int signo)
636 296152d1 2022-05-31 thomas {
637 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
638 296152d1 2022-05-31 thomas }
639 296152d1 2022-05-31 thomas
640 296152d1 2022-05-31 thomas static int
641 296152d1 2022-05-31 thomas tog_fatal_signal_received()
642 296152d1 2022-05-31 thomas {
643 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
644 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
645 296152d1 2022-05-31 thomas }
646 296152d1 2022-05-31 thomas
647 296152d1 2022-05-31 thomas
648 e5a0f69f 2018-08-18 stsp static const struct got_error *
649 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
650 ea5e7bb5 2018-08-01 stsp {
651 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
652 e5a0f69f 2018-08-18 stsp
653 669b5ffa 2018-10-07 stsp if (view->child) {
654 669b5ffa 2018-10-07 stsp view_close(view->child);
655 669b5ffa 2018-10-07 stsp view->child = NULL;
656 669b5ffa 2018-10-07 stsp }
657 e5a0f69f 2018-08-18 stsp if (view->close)
658 e5a0f69f 2018-08-18 stsp err = view->close(view);
659 ea5e7bb5 2018-08-01 stsp if (view->panel)
660 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
661 ea5e7bb5 2018-08-01 stsp if (view->window)
662 ea5e7bb5 2018-08-01 stsp delwin(view->window);
663 ea5e7bb5 2018-08-01 stsp free(view);
664 e5a0f69f 2018-08-18 stsp return err;
665 ea5e7bb5 2018-08-01 stsp }
666 ea5e7bb5 2018-08-01 stsp
667 ea5e7bb5 2018-08-01 stsp static struct tog_view *
668 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
669 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
670 ea5e7bb5 2018-08-01 stsp {
671 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
672 ea5e7bb5 2018-08-01 stsp
673 ea5e7bb5 2018-08-01 stsp if (view == NULL)
674 ea5e7bb5 2018-08-01 stsp return NULL;
675 ea5e7bb5 2018-08-01 stsp
676 d6b05b5b 2018-08-04 stsp view->type = type;
677 f7d12f7e 2018-08-01 stsp view->lines = LINES;
678 f7d12f7e 2018-08-01 stsp view->cols = COLS;
679 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
680 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
681 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
682 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
683 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
684 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
685 96a765a8 2018-08-04 stsp view_close(view);
686 ea5e7bb5 2018-08-01 stsp return NULL;
687 ea5e7bb5 2018-08-01 stsp }
688 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
689 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
690 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
691 96a765a8 2018-08-04 stsp view_close(view);
692 ea5e7bb5 2018-08-01 stsp return NULL;
693 ea5e7bb5 2018-08-01 stsp }
694 ea5e7bb5 2018-08-01 stsp
695 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
696 ea5e7bb5 2018-08-01 stsp return view;
697 cdf1ee82 2018-08-01 stsp }
698 cdf1ee82 2018-08-01 stsp
699 0cf4efb1 2018-09-29 stsp static int
700 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
701 0cf4efb1 2018-09-29 stsp {
702 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
703 0cf4efb1 2018-09-29 stsp return 0;
704 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
705 5c60c32a 2018-10-18 stsp }
706 5c60c32a 2018-10-18 stsp
707 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
708 5c60c32a 2018-10-18 stsp
709 5c60c32a 2018-10-18 stsp static const struct got_error *
710 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
711 5c60c32a 2018-10-18 stsp {
712 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
713 5c60c32a 2018-10-18 stsp
714 5c60c32a 2018-10-18 stsp view->begin_y = 0;
715 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
716 5c60c32a 2018-10-18 stsp view->nlines = LINES;
717 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
718 5c60c32a 2018-10-18 stsp view->lines = LINES;
719 5c60c32a 2018-10-18 stsp view->cols = COLS;
720 5c60c32a 2018-10-18 stsp err = view_resize(view);
721 5c60c32a 2018-10-18 stsp if (err)
722 5c60c32a 2018-10-18 stsp return err;
723 5c60c32a 2018-10-18 stsp
724 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
725 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
726 5c60c32a 2018-10-18 stsp
727 5c60c32a 2018-10-18 stsp return NULL;
728 5c60c32a 2018-10-18 stsp }
729 5c60c32a 2018-10-18 stsp
730 5c60c32a 2018-10-18 stsp static const struct got_error *
731 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
732 5c60c32a 2018-10-18 stsp {
733 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
734 5c60c32a 2018-10-18 stsp
735 5c60c32a 2018-10-18 stsp view->begin_x = 0;
736 5c60c32a 2018-10-18 stsp view->begin_y = 0;
737 5c60c32a 2018-10-18 stsp view->nlines = LINES;
738 5c60c32a 2018-10-18 stsp view->ncols = COLS;
739 5c60c32a 2018-10-18 stsp view->lines = LINES;
740 5c60c32a 2018-10-18 stsp view->cols = COLS;
741 5c60c32a 2018-10-18 stsp err = view_resize(view);
742 5c60c32a 2018-10-18 stsp if (err)
743 5c60c32a 2018-10-18 stsp return err;
744 5c60c32a 2018-10-18 stsp
745 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
746 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
747 5c60c32a 2018-10-18 stsp
748 5c60c32a 2018-10-18 stsp return NULL;
749 0cf4efb1 2018-09-29 stsp }
750 0cf4efb1 2018-09-29 stsp
751 5c60c32a 2018-10-18 stsp static int
752 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
753 5c60c32a 2018-10-18 stsp {
754 5c60c32a 2018-10-18 stsp return view->parent == NULL;
755 5c60c32a 2018-10-18 stsp }
756 5c60c32a 2018-10-18 stsp
757 4d8c2215 2018-08-19 stsp static const struct got_error *
758 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
759 f7d12f7e 2018-08-01 stsp {
760 f7d12f7e 2018-08-01 stsp int nlines, ncols;
761 f7d12f7e 2018-08-01 stsp
762 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
763 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
764 0cf4efb1 2018-09-29 stsp else
765 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
766 f7d12f7e 2018-08-01 stsp
767 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
768 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
769 0cf4efb1 2018-09-29 stsp else
770 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
771 f7d12f7e 2018-08-01 stsp
772 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
773 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
774 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
775 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
776 25791caa 2018-10-24 stsp wclear(view->window);
777 f7d12f7e 2018-08-01 stsp
778 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
779 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
780 0cf4efb1 2018-09-29 stsp view->lines = LINES;
781 0cf4efb1 2018-09-29 stsp view->cols = COLS;
782 6d0fee91 2018-08-01 stsp
783 6e3e5d9c 2018-10-18 stsp if (view->child) {
784 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
785 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
786 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
787 5c60c32a 2018-10-18 stsp if (view->child->focussed)
788 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
789 5c60c32a 2018-10-18 stsp else
790 5c60c32a 2018-10-18 stsp show_panel(view->panel);
791 5c60c32a 2018-10-18 stsp } else {
792 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
793 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
794 5c60c32a 2018-10-18 stsp }
795 5c60c32a 2018-10-18 stsp }
796 669b5ffa 2018-10-07 stsp
797 5c60c32a 2018-10-18 stsp return NULL;
798 669b5ffa 2018-10-07 stsp }
799 669b5ffa 2018-10-07 stsp
800 669b5ffa 2018-10-07 stsp static const struct got_error *
801 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
802 669b5ffa 2018-10-07 stsp {
803 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
804 669b5ffa 2018-10-07 stsp
805 669b5ffa 2018-10-07 stsp if (view->child == NULL)
806 669b5ffa 2018-10-07 stsp return NULL;
807 669b5ffa 2018-10-07 stsp
808 669b5ffa 2018-10-07 stsp err = view_close(view->child);
809 669b5ffa 2018-10-07 stsp view->child = NULL;
810 669b5ffa 2018-10-07 stsp return err;
811 669b5ffa 2018-10-07 stsp }
812 669b5ffa 2018-10-07 stsp
813 72a9cb46 2020-12-03 stsp static void
814 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
815 669b5ffa 2018-10-07 stsp {
816 669b5ffa 2018-10-07 stsp view->child = child;
817 669b5ffa 2018-10-07 stsp child->parent = view;
818 bfddd0d9 2018-09-29 stsp }
819 bfddd0d9 2018-09-29 stsp
820 bfddd0d9 2018-09-29 stsp static int
821 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
822 bfddd0d9 2018-09-29 stsp {
823 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
824 34bc9ec9 2019-02-22 stsp }
825 34bc9ec9 2019-02-22 stsp
826 34bc9ec9 2019-02-22 stsp static void
827 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
828 25791caa 2018-10-24 stsp {
829 25791caa 2018-10-24 stsp int cols, lines;
830 25791caa 2018-10-24 stsp struct winsize size;
831 25791caa 2018-10-24 stsp
832 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
833 25791caa 2018-10-24 stsp cols = 80; /* Default */
834 25791caa 2018-10-24 stsp lines = 24;
835 25791caa 2018-10-24 stsp } else {
836 25791caa 2018-10-24 stsp cols = size.ws_col;
837 25791caa 2018-10-24 stsp lines = size.ws_row;
838 25791caa 2018-10-24 stsp }
839 25791caa 2018-10-24 stsp resize_term(lines, cols);
840 2b49a8ae 2019-06-22 stsp }
841 2b49a8ae 2019-06-22 stsp
842 2b49a8ae 2019-06-22 stsp static const struct got_error *
843 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
844 2b49a8ae 2019-06-22 stsp {
845 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
846 2b49a8ae 2019-06-22 stsp char pattern[1024];
847 2b49a8ae 2019-06-22 stsp int ret;
848 c0c4acc8 2021-01-24 stsp
849 c0c4acc8 2021-01-24 stsp if (view->search_started) {
850 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
851 c0c4acc8 2021-01-24 stsp view->searching = 0;
852 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
853 c0c4acc8 2021-01-24 stsp }
854 c0c4acc8 2021-01-24 stsp view->search_started = 0;
855 2b49a8ae 2019-06-22 stsp
856 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
857 2b49a8ae 2019-06-22 stsp return NULL;
858 2b49a8ae 2019-06-22 stsp
859 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
860 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
861 2b49a8ae 2019-06-22 stsp
862 2b49a8ae 2019-06-22 stsp nocbreak();
863 2b49a8ae 2019-06-22 stsp echo();
864 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
865 2b49a8ae 2019-06-22 stsp cbreak();
866 2b49a8ae 2019-06-22 stsp noecho();
867 2b49a8ae 2019-06-22 stsp if (ret == ERR)
868 2b49a8ae 2019-06-22 stsp return NULL;
869 2b49a8ae 2019-06-22 stsp
870 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
871 7c32bd05 2019-06-22 stsp err = view->search_start(view);
872 7c32bd05 2019-06-22 stsp if (err) {
873 7c32bd05 2019-06-22 stsp regfree(&view->regex);
874 7c32bd05 2019-06-22 stsp return err;
875 7c32bd05 2019-06-22 stsp }
876 c0c4acc8 2021-01-24 stsp view->search_started = 1;
877 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
878 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
879 2b49a8ae 2019-06-22 stsp view->search_next(view);
880 2b49a8ae 2019-06-22 stsp }
881 2b49a8ae 2019-06-22 stsp
882 2b49a8ae 2019-06-22 stsp return NULL;
883 0cf4efb1 2018-09-29 stsp }
884 6d0fee91 2018-08-01 stsp
885 0cf4efb1 2018-09-29 stsp static const struct got_error *
886 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
887 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
888 e5a0f69f 2018-08-18 stsp {
889 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
890 669b5ffa 2018-10-07 stsp struct tog_view *v;
891 1a76625f 2018-10-22 stsp int ch, errcode;
892 e5a0f69f 2018-08-18 stsp
893 e5a0f69f 2018-08-18 stsp *new = NULL;
894 8f4ed634 2020-03-26 stsp
895 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
896 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
897 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
898 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
899 e5a0f69f 2018-08-18 stsp
900 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
901 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
902 82954512 2020-02-03 stsp if (errcode)
903 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
904 82954512 2020-02-03 stsp "pthread_mutex_unlock");
905 3da8ef85 2021-09-21 stsp sched_yield();
906 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
907 82954512 2020-02-03 stsp if (errcode)
908 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
909 82954512 2020-02-03 stsp "pthread_mutex_lock");
910 60493ae3 2019-06-20 stsp view->search_next(view);
911 60493ae3 2019-06-20 stsp return NULL;
912 60493ae3 2019-06-20 stsp }
913 60493ae3 2019-06-20 stsp
914 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
915 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
916 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
917 1a76625f 2018-10-22 stsp if (errcode)
918 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
919 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
920 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
921 1a76625f 2018-10-22 stsp if (errcode)
922 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
923 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
924 25791caa 2018-10-24 stsp
925 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
926 25791caa 2018-10-24 stsp tog_resizeterm();
927 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
928 61266923 2020-01-14 stsp tog_sigcont_received = 0;
929 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
930 25791caa 2018-10-24 stsp err = view_resize(v);
931 25791caa 2018-10-24 stsp if (err)
932 25791caa 2018-10-24 stsp return err;
933 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
934 25791caa 2018-10-24 stsp if (err)
935 25791caa 2018-10-24 stsp return err;
936 cdfcfb03 2020-12-06 stsp if (v->child) {
937 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
938 cdfcfb03 2020-12-06 stsp if (err)
939 cdfcfb03 2020-12-06 stsp return err;
940 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
941 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
942 cdfcfb03 2020-12-06 stsp if (err)
943 cdfcfb03 2020-12-06 stsp return err;
944 cdfcfb03 2020-12-06 stsp }
945 25791caa 2018-10-24 stsp }
946 25791caa 2018-10-24 stsp }
947 25791caa 2018-10-24 stsp
948 e5a0f69f 2018-08-18 stsp switch (ch) {
949 1e37a5c2 2019-05-12 jcs case '\t':
950 1e37a5c2 2019-05-12 jcs if (view->child) {
951 e78dc838 2020-12-04 stsp view->focussed = 0;
952 e78dc838 2020-12-04 stsp view->child->focussed = 1;
953 e78dc838 2020-12-04 stsp view->focus_child = 1;
954 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
955 e78dc838 2020-12-04 stsp view->focussed = 0;
956 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
957 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
958 1e37a5c2 2019-05-12 jcs }
959 1e37a5c2 2019-05-12 jcs break;
960 1e37a5c2 2019-05-12 jcs case 'q':
961 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
962 9970f7fc 2020-12-03 stsp view->dying = 1;
963 1e37a5c2 2019-05-12 jcs break;
964 1e37a5c2 2019-05-12 jcs case 'Q':
965 1e37a5c2 2019-05-12 jcs *done = 1;
966 1e37a5c2 2019-05-12 jcs break;
967 1e37a5c2 2019-05-12 jcs case 'f':
968 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
969 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
970 1e37a5c2 2019-05-12 jcs break;
971 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
972 e78dc838 2020-12-04 stsp view->focussed = 0;
973 e78dc838 2020-12-04 stsp view->child->focussed = 1;
974 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
975 1e37a5c2 2019-05-12 jcs } else
976 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
977 1e37a5c2 2019-05-12 jcs if (err)
978 1e37a5c2 2019-05-12 jcs break;
979 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
980 9970f7fc 2020-12-03 stsp KEY_RESIZE);
981 1e37a5c2 2019-05-12 jcs } else {
982 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
983 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
984 e78dc838 2020-12-04 stsp view->focussed = 1;
985 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
986 1e37a5c2 2019-05-12 jcs } else {
987 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
988 669b5ffa 2018-10-07 stsp }
989 1e37a5c2 2019-05-12 jcs if (err)
990 1e37a5c2 2019-05-12 jcs break;
991 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
992 1e37a5c2 2019-05-12 jcs }
993 1e37a5c2 2019-05-12 jcs break;
994 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
995 60493ae3 2019-06-20 stsp break;
996 60493ae3 2019-06-20 stsp case '/':
997 60493ae3 2019-06-20 stsp if (view->search_start)
998 2b49a8ae 2019-06-22 stsp view_search_start(view);
999 60493ae3 2019-06-20 stsp else
1000 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1001 1e37a5c2 2019-05-12 jcs break;
1002 b1bf1435 2019-06-21 stsp case 'N':
1003 60493ae3 2019-06-20 stsp case 'n':
1004 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1005 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1006 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1007 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1008 60493ae3 2019-06-20 stsp view->search_next(view);
1009 60493ae3 2019-06-20 stsp } else
1010 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1011 60493ae3 2019-06-20 stsp break;
1012 1e37a5c2 2019-05-12 jcs default:
1013 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1014 1e37a5c2 2019-05-12 jcs break;
1015 e5a0f69f 2018-08-18 stsp }
1016 e5a0f69f 2018-08-18 stsp
1017 e5a0f69f 2018-08-18 stsp return err;
1018 e5a0f69f 2018-08-18 stsp }
1019 e5a0f69f 2018-08-18 stsp
1020 1a57306a 2018-09-02 stsp void
1021 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1022 1a57306a 2018-09-02 stsp {
1023 0cf4efb1 2018-09-29 stsp PANEL *panel;
1024 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1025 0cf4efb1 2018-09-29 stsp
1026 669b5ffa 2018-10-07 stsp if (view->parent)
1027 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1028 669b5ffa 2018-10-07 stsp
1029 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1030 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1031 1a57306a 2018-09-02 stsp return;
1032 1a57306a 2018-09-02 stsp
1033 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1034 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1035 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1036 bcbd79e2 2018-08-19 stsp }
1037 bcbd79e2 2018-08-19 stsp
1038 a3404814 2018-09-02 stsp int
1039 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1040 a3404814 2018-09-02 stsp {
1041 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1042 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1043 669b5ffa 2018-10-07 stsp return 0;
1044 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1045 669b5ffa 2018-10-07 stsp return 0;
1046 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1047 a3404814 2018-09-02 stsp return 0;
1048 a3404814 2018-09-02 stsp
1049 669b5ffa 2018-10-07 stsp return view->focussed;
1050 a3404814 2018-09-02 stsp }
1051 a3404814 2018-09-02 stsp
1052 bcbd79e2 2018-08-19 stsp static const struct got_error *
1053 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1054 e5a0f69f 2018-08-18 stsp {
1055 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1056 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1057 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1058 fd823528 2018-10-22 stsp int fast_refresh = 10;
1059 1a76625f 2018-10-22 stsp int done = 0, errcode;
1060 e5a0f69f 2018-08-18 stsp
1061 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1062 1a76625f 2018-10-22 stsp if (errcode)
1063 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1064 1a76625f 2018-10-22 stsp
1065 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1066 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1067 e5a0f69f 2018-08-18 stsp
1068 1004088d 2018-09-29 stsp view->focussed = 1;
1069 878940b7 2018-09-29 stsp err = view->show(view);
1070 0cf4efb1 2018-09-29 stsp if (err)
1071 0cf4efb1 2018-09-29 stsp return err;
1072 0cf4efb1 2018-09-29 stsp update_panels();
1073 0cf4efb1 2018-09-29 stsp doupdate();
1074 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1075 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1076 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1077 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1078 fd823528 2018-10-22 stsp
1079 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1080 e5a0f69f 2018-08-18 stsp if (err)
1081 e5a0f69f 2018-08-18 stsp break;
1082 9970f7fc 2020-12-03 stsp if (view->dying) {
1083 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1084 669b5ffa 2018-10-07 stsp
1085 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1086 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1087 9970f7fc 2020-12-03 stsp entry);
1088 e78dc838 2020-12-04 stsp else if (view->parent)
1089 669b5ffa 2018-10-07 stsp prev = view->parent;
1090 669b5ffa 2018-10-07 stsp
1091 e78dc838 2020-12-04 stsp if (view->parent) {
1092 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1093 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1094 e78dc838 2020-12-04 stsp } else
1095 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1096 669b5ffa 2018-10-07 stsp
1097 9970f7fc 2020-12-03 stsp err = view_close(view);
1098 fb59748f 2020-12-05 stsp if (err)
1099 e5a0f69f 2018-08-18 stsp goto done;
1100 669b5ffa 2018-10-07 stsp
1101 e78dc838 2020-12-04 stsp view = NULL;
1102 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1103 e78dc838 2020-12-04 stsp if (v->focussed)
1104 e78dc838 2020-12-04 stsp break;
1105 0cf4efb1 2018-09-29 stsp }
1106 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1107 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1108 e78dc838 2020-12-04 stsp if (prev)
1109 e78dc838 2020-12-04 stsp view = prev;
1110 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1111 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1112 e78dc838 2020-12-04 stsp tog_view_list_head);
1113 e78dc838 2020-12-04 stsp }
1114 e78dc838 2020-12-04 stsp if (view) {
1115 e78dc838 2020-12-04 stsp if (view->focus_child) {
1116 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1117 e78dc838 2020-12-04 stsp view = view->child;
1118 e78dc838 2020-12-04 stsp } else
1119 e78dc838 2020-12-04 stsp view->focussed = 1;
1120 e78dc838 2020-12-04 stsp }
1121 e78dc838 2020-12-04 stsp }
1122 e5a0f69f 2018-08-18 stsp }
1123 bcbd79e2 2018-08-19 stsp if (new_view) {
1124 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1125 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1126 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1127 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1128 86c66b02 2018-10-18 stsp continue;
1129 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1130 86c66b02 2018-10-18 stsp err = view_close(v);
1131 86c66b02 2018-10-18 stsp if (err)
1132 86c66b02 2018-10-18 stsp goto done;
1133 86c66b02 2018-10-18 stsp break;
1134 86c66b02 2018-10-18 stsp }
1135 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1136 fed7eaa8 2018-10-24 stsp view = new_view;
1137 7cd52833 2022-06-23 thomas }
1138 669b5ffa 2018-10-07 stsp if (view) {
1139 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1140 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1141 e78dc838 2020-12-04 stsp view = view->child;
1142 e78dc838 2020-12-04 stsp } else {
1143 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1144 e78dc838 2020-12-04 stsp view = view->parent;
1145 1a76625f 2018-10-22 stsp }
1146 e78dc838 2020-12-04 stsp show_panel(view->panel);
1147 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1148 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1149 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1150 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1151 669b5ffa 2018-10-07 stsp if (err)
1152 1a76625f 2018-10-22 stsp goto done;
1153 669b5ffa 2018-10-07 stsp }
1154 669b5ffa 2018-10-07 stsp err = view->show(view);
1155 0cf4efb1 2018-09-29 stsp if (err)
1156 1a76625f 2018-10-22 stsp goto done;
1157 669b5ffa 2018-10-07 stsp if (view->child) {
1158 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1159 669b5ffa 2018-10-07 stsp if (err)
1160 1a76625f 2018-10-22 stsp goto done;
1161 669b5ffa 2018-10-07 stsp }
1162 1a76625f 2018-10-22 stsp update_panels();
1163 1a76625f 2018-10-22 stsp doupdate();
1164 0cf4efb1 2018-09-29 stsp }
1165 e5a0f69f 2018-08-18 stsp }
1166 e5a0f69f 2018-08-18 stsp done:
1167 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1168 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1169 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1170 e5a0f69f 2018-08-18 stsp view_close(view);
1171 e5a0f69f 2018-08-18 stsp }
1172 1a76625f 2018-10-22 stsp
1173 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1174 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1175 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1176 1a76625f 2018-10-22 stsp
1177 e5a0f69f 2018-08-18 stsp return err;
1178 ea5e7bb5 2018-08-01 stsp }
1179 ea5e7bb5 2018-08-01 stsp
1180 4ed7e80c 2018-05-20 stsp __dead static void
1181 9f7d7167 2018-04-29 stsp usage_log(void)
1182 9f7d7167 2018-04-29 stsp {
1183 80ddbec8 2018-04-29 stsp endwin();
1184 c70c5802 2018-08-01 stsp fprintf(stderr,
1185 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1186 9f7d7167 2018-04-29 stsp getprogname());
1187 9f7d7167 2018-04-29 stsp exit(1);
1188 80ddbec8 2018-04-29 stsp }
1189 80ddbec8 2018-04-29 stsp
1190 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1191 80ddbec8 2018-04-29 stsp static const struct got_error *
1192 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1193 963b370f 2018-05-20 stsp {
1194 00dfcb92 2018-06-11 stsp char *vis = NULL;
1195 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1196 963b370f 2018-05-20 stsp
1197 963b370f 2018-05-20 stsp *ws = NULL;
1198 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1199 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1200 00dfcb92 2018-06-11 stsp int vislen;
1201 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1202 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1203 00dfcb92 2018-06-11 stsp
1204 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1205 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1206 00dfcb92 2018-06-11 stsp if (err)
1207 00dfcb92 2018-06-11 stsp return err;
1208 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1209 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1211 a7f50699 2018-06-11 stsp goto done;
1212 a7f50699 2018-06-11 stsp }
1213 00dfcb92 2018-06-11 stsp }
1214 963b370f 2018-05-20 stsp
1215 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1216 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1217 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1218 a7f50699 2018-06-11 stsp goto done;
1219 a7f50699 2018-06-11 stsp }
1220 963b370f 2018-05-20 stsp
1221 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1222 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1223 a7f50699 2018-06-11 stsp done:
1224 00dfcb92 2018-06-11 stsp free(vis);
1225 963b370f 2018-05-20 stsp if (err) {
1226 963b370f 2018-05-20 stsp free(*ws);
1227 963b370f 2018-05-20 stsp *ws = NULL;
1228 963b370f 2018-05-20 stsp *wlen = 0;
1229 963b370f 2018-05-20 stsp }
1230 963b370f 2018-05-20 stsp return err;
1231 963b370f 2018-05-20 stsp }
1232 963b370f 2018-05-20 stsp
1233 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1234 963b370f 2018-05-20 stsp static const struct got_error *
1235 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1236 27a741e5 2019-09-11 stsp int col_tab_align)
1237 963b370f 2018-05-20 stsp {
1238 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1239 963b370f 2018-05-20 stsp int cols = 0;
1240 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1241 963b370f 2018-05-20 stsp size_t wlen;
1242 963b370f 2018-05-20 stsp int i;
1243 963b370f 2018-05-20 stsp
1244 963b370f 2018-05-20 stsp *wlinep = NULL;
1245 b700b5d6 2018-07-10 stsp *widthp = 0;
1246 963b370f 2018-05-20 stsp
1247 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1248 963b370f 2018-05-20 stsp if (err)
1249 963b370f 2018-05-20 stsp return err;
1250 963b370f 2018-05-20 stsp
1251 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1252 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1253 3f670bfb 2020-12-10 stsp wlen--;
1254 3f670bfb 2020-12-10 stsp }
1255 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1256 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1257 3f670bfb 2020-12-10 stsp wlen--;
1258 3f670bfb 2020-12-10 stsp }
1259 3f670bfb 2020-12-10 stsp
1260 963b370f 2018-05-20 stsp i = 0;
1261 27a741e5 2019-09-11 stsp while (i < wlen) {
1262 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1263 27a741e5 2019-09-11 stsp
1264 27a741e5 2019-09-11 stsp if (width == 0) {
1265 b700b5d6 2018-07-10 stsp i++;
1266 27a741e5 2019-09-11 stsp continue;
1267 27a741e5 2019-09-11 stsp }
1268 27a741e5 2019-09-11 stsp
1269 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1270 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1271 27a741e5 2019-09-11 stsp break;
1272 27a741e5 2019-09-11 stsp cols += width;
1273 3c1f04f1 2018-09-13 stsp i++;
1274 27a741e5 2019-09-11 stsp } else if (width == -1) {
1275 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1276 27a741e5 2019-09-11 stsp width = TABSIZE -
1277 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1278 748d5cab 2020-12-14 naddy } else {
1279 748d5cab 2020-12-14 naddy width = 1;
1280 748d5cab 2020-12-14 naddy wline[i] = L'.';
1281 27a741e5 2019-09-11 stsp }
1282 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1283 748d5cab 2020-12-14 naddy break;
1284 748d5cab 2020-12-14 naddy cols += width;
1285 b700b5d6 2018-07-10 stsp i++;
1286 27a741e5 2019-09-11 stsp } else {
1287 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1288 963b370f 2018-05-20 stsp goto done;
1289 963b370f 2018-05-20 stsp }
1290 963b370f 2018-05-20 stsp }
1291 963b370f 2018-05-20 stsp wline[i] = L'\0';
1292 b700b5d6 2018-07-10 stsp if (widthp)
1293 b700b5d6 2018-07-10 stsp *widthp = cols;
1294 963b370f 2018-05-20 stsp done:
1295 963b370f 2018-05-20 stsp if (err)
1296 963b370f 2018-05-20 stsp free(wline);
1297 963b370f 2018-05-20 stsp else
1298 963b370f 2018-05-20 stsp *wlinep = wline;
1299 963b370f 2018-05-20 stsp return err;
1300 963b370f 2018-05-20 stsp }
1301 963b370f 2018-05-20 stsp
1302 8b473291 2019-02-21 stsp static const struct got_error*
1303 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1304 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1305 8b473291 2019-02-21 stsp {
1306 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1307 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1308 8b473291 2019-02-21 stsp char *s;
1309 8b473291 2019-02-21 stsp const char *name;
1310 8b473291 2019-02-21 stsp
1311 8b473291 2019-02-21 stsp *refs_str = NULL;
1312 8b473291 2019-02-21 stsp
1313 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1314 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1315 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1316 52b5abe1 2019-08-13 stsp int cmp;
1317 52b5abe1 2019-08-13 stsp
1318 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1319 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1320 8b473291 2019-02-21 stsp continue;
1321 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1322 8b473291 2019-02-21 stsp name += 5;
1323 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1324 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1325 7143d404 2019-03-12 stsp continue;
1326 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1327 8b473291 2019-02-21 stsp name += 6;
1328 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1329 8b473291 2019-02-21 stsp name += 8;
1330 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1331 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1332 79cc719f 2020-04-24 stsp continue;
1333 79cc719f 2020-04-24 stsp }
1334 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1335 48cae60d 2020-09-22 stsp if (err)
1336 48cae60d 2020-09-22 stsp break;
1337 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1338 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1339 5d844a1e 2019-08-13 stsp if (err) {
1340 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1341 48cae60d 2020-09-22 stsp free(ref_id);
1342 5d844a1e 2019-08-13 stsp break;
1343 48cae60d 2020-09-22 stsp }
1344 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1345 5d844a1e 2019-08-13 stsp err = NULL;
1346 5d844a1e 2019-08-13 stsp tag = NULL;
1347 5d844a1e 2019-08-13 stsp }
1348 52b5abe1 2019-08-13 stsp }
1349 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1350 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1351 48cae60d 2020-09-22 stsp free(ref_id);
1352 52b5abe1 2019-08-13 stsp if (tag)
1353 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1354 52b5abe1 2019-08-13 stsp if (cmp != 0)
1355 52b5abe1 2019-08-13 stsp continue;
1356 8b473291 2019-02-21 stsp s = *refs_str;
1357 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1358 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1359 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1360 8b473291 2019-02-21 stsp free(s);
1361 8b473291 2019-02-21 stsp *refs_str = NULL;
1362 8b473291 2019-02-21 stsp break;
1363 8b473291 2019-02-21 stsp }
1364 8b473291 2019-02-21 stsp free(s);
1365 8b473291 2019-02-21 stsp }
1366 8b473291 2019-02-21 stsp
1367 8b473291 2019-02-21 stsp return err;
1368 8b473291 2019-02-21 stsp }
1369 8b473291 2019-02-21 stsp
1370 963b370f 2018-05-20 stsp static const struct got_error *
1371 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1372 27a741e5 2019-09-11 stsp int col_tab_align)
1373 5813d178 2019-03-09 stsp {
1374 e6b8b890 2020-12-29 naddy char *smallerthan;
1375 5813d178 2019-03-09 stsp
1376 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1377 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1378 5813d178 2019-03-09 stsp author = smallerthan + 1;
1379 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1380 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1381 5813d178 2019-03-09 stsp }
1382 5813d178 2019-03-09 stsp
1383 5813d178 2019-03-09 stsp static const struct got_error *
1384 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1385 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1386 8fdc79fe 2020-12-01 naddy int author_display_cols)
1387 80ddbec8 2018-04-29 stsp {
1388 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1389 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1390 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1391 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1392 5813d178 2019-03-09 stsp char *author = NULL;
1393 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1394 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1395 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1396 bb737323 2018-05-20 stsp int col, limit;
1397 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1398 ccb26ccd 2018-11-05 stsp struct tm tm;
1399 45d799e2 2018-12-23 stsp time_t committer_time;
1400 11b20872 2019-11-08 stsp struct tog_color *tc;
1401 80ddbec8 2018-04-29 stsp
1402 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1403 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1404 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1405 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1406 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1407 b39d25c7 2018-07-10 stsp
1408 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1409 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1410 b39d25c7 2018-07-10 stsp else
1411 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1412 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1413 11b20872 2019-11-08 stsp if (tc)
1414 11b20872 2019-11-08 stsp wattr_on(view->window,
1415 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1416 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1417 11b20872 2019-11-08 stsp if (tc)
1418 11b20872 2019-11-08 stsp wattr_off(view->window,
1419 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1420 27a741e5 2019-09-11 stsp col = limit;
1421 b39d25c7 2018-07-10 stsp if (col > avail)
1422 b39d25c7 2018-07-10 stsp goto done;
1423 6570a66d 2019-11-08 stsp
1424 6570a66d 2019-11-08 stsp if (avail >= 120) {
1425 6570a66d 2019-11-08 stsp char *id_str;
1426 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1427 6570a66d 2019-11-08 stsp if (err)
1428 6570a66d 2019-11-08 stsp goto done;
1429 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1430 11b20872 2019-11-08 stsp if (tc)
1431 11b20872 2019-11-08 stsp wattr_on(view->window,
1432 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1433 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1434 11b20872 2019-11-08 stsp if (tc)
1435 11b20872 2019-11-08 stsp wattr_off(view->window,
1436 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1437 6570a66d 2019-11-08 stsp free(id_str);
1438 6570a66d 2019-11-08 stsp col += 9;
1439 6570a66d 2019-11-08 stsp if (col > avail)
1440 6570a66d 2019-11-08 stsp goto done;
1441 6570a66d 2019-11-08 stsp }
1442 b39d25c7 2018-07-10 stsp
1443 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1444 5813d178 2019-03-09 stsp if (author == NULL) {
1445 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1446 80ddbec8 2018-04-29 stsp goto done;
1447 80ddbec8 2018-04-29 stsp }
1448 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1449 bb737323 2018-05-20 stsp if (err)
1450 bb737323 2018-05-20 stsp goto done;
1451 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1452 11b20872 2019-11-08 stsp if (tc)
1453 11b20872 2019-11-08 stsp wattr_on(view->window,
1454 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1455 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1456 11b20872 2019-11-08 stsp if (tc)
1457 11b20872 2019-11-08 stsp wattr_off(view->window,
1458 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1459 bb737323 2018-05-20 stsp col += author_width;
1460 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1461 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1462 bb737323 2018-05-20 stsp col++;
1463 bb737323 2018-05-20 stsp author_width++;
1464 bb737323 2018-05-20 stsp }
1465 9c2eaf34 2018-05-20 stsp if (col > avail)
1466 9c2eaf34 2018-05-20 stsp goto done;
1467 80ddbec8 2018-04-29 stsp
1468 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1469 5943eee2 2019-08-13 stsp if (err)
1470 6d9fbc00 2018-04-29 stsp goto done;
1471 bb737323 2018-05-20 stsp logmsg = logmsg0;
1472 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1473 bb737323 2018-05-20 stsp logmsg++;
1474 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1475 bb737323 2018-05-20 stsp if (newline)
1476 bb737323 2018-05-20 stsp *newline = '\0';
1477 bb737323 2018-05-20 stsp limit = avail - col;
1478 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1479 bb737323 2018-05-20 stsp if (err)
1480 bb737323 2018-05-20 stsp goto done;
1481 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1482 bb737323 2018-05-20 stsp col += logmsg_width;
1483 27a741e5 2019-09-11 stsp while (col < avail) {
1484 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1485 bb737323 2018-05-20 stsp col++;
1486 881b2d3e 2018-04-30 stsp }
1487 80ddbec8 2018-04-29 stsp done:
1488 80ddbec8 2018-04-29 stsp free(logmsg0);
1489 bb737323 2018-05-20 stsp free(wlogmsg);
1490 5813d178 2019-03-09 stsp free(author);
1491 bb737323 2018-05-20 stsp free(wauthor);
1492 80ddbec8 2018-04-29 stsp free(line);
1493 80ddbec8 2018-04-29 stsp return err;
1494 80ddbec8 2018-04-29 stsp }
1495 26ed57b2 2018-05-19 stsp
1496 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1497 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1498 899d86c2 2018-05-10 stsp struct got_object_id *id)
1499 80ddbec8 2018-04-29 stsp {
1500 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1501 80ddbec8 2018-04-29 stsp
1502 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1503 80ddbec8 2018-04-29 stsp if (entry == NULL)
1504 899d86c2 2018-05-10 stsp return NULL;
1505 99db9666 2018-05-07 stsp
1506 899d86c2 2018-05-10 stsp entry->id = id;
1507 99db9666 2018-05-07 stsp entry->commit = commit;
1508 899d86c2 2018-05-10 stsp return entry;
1509 99db9666 2018-05-07 stsp }
1510 80ddbec8 2018-04-29 stsp
1511 99db9666 2018-05-07 stsp static void
1512 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1513 99db9666 2018-05-07 stsp {
1514 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1515 99db9666 2018-05-07 stsp
1516 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1517 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1518 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1519 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1520 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1521 99db9666 2018-05-07 stsp free(entry);
1522 99db9666 2018-05-07 stsp }
1523 99db9666 2018-05-07 stsp
1524 99db9666 2018-05-07 stsp static void
1525 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1526 99db9666 2018-05-07 stsp {
1527 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1528 99db9666 2018-05-07 stsp pop_commit(commits);
1529 c4972b91 2018-05-07 stsp }
1530 c4972b91 2018-05-07 stsp
1531 c4972b91 2018-05-07 stsp static const struct got_error *
1532 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1533 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1534 13add988 2019-10-15 stsp {
1535 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1536 13add988 2019-10-15 stsp regmatch_t regmatch;
1537 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1538 13add988 2019-10-15 stsp
1539 13add988 2019-10-15 stsp *have_match = 0;
1540 13add988 2019-10-15 stsp
1541 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1542 13add988 2019-10-15 stsp if (err)
1543 13add988 2019-10-15 stsp return err;
1544 13add988 2019-10-15 stsp
1545 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1546 13add988 2019-10-15 stsp if (err)
1547 13add988 2019-10-15 stsp goto done;
1548 13add988 2019-10-15 stsp
1549 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1550 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1551 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1552 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1553 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1554 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1555 13add988 2019-10-15 stsp *have_match = 1;
1556 13add988 2019-10-15 stsp done:
1557 13add988 2019-10-15 stsp free(id_str);
1558 13add988 2019-10-15 stsp free(logmsg);
1559 13add988 2019-10-15 stsp return err;
1560 13add988 2019-10-15 stsp }
1561 13add988 2019-10-15 stsp
1562 13add988 2019-10-15 stsp static const struct got_error *
1563 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1564 c4972b91 2018-05-07 stsp {
1565 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1566 9ba79e04 2018-06-11 stsp
1567 1a76625f 2018-10-22 stsp /*
1568 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1569 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1570 1a76625f 2018-10-22 stsp * while updating the display.
1571 1a76625f 2018-10-22 stsp */
1572 4e0d2870 2020-12-07 naddy do {
1573 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1574 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1575 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1576 1a76625f 2018-10-22 stsp int errcode;
1577 899d86c2 2018-05-10 stsp
1578 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1579 4e0d2870 2020-12-07 naddy NULL, NULL);
1580 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1581 ecb28ae0 2018-07-16 stsp break;
1582 899d86c2 2018-05-10 stsp
1583 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1584 9ba79e04 2018-06-11 stsp if (err)
1585 9ba79e04 2018-06-11 stsp break;
1586 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1587 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1588 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1589 9ba79e04 2018-06-11 stsp break;
1590 9ba79e04 2018-06-11 stsp }
1591 93e45b7c 2018-09-24 stsp
1592 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1593 1a76625f 2018-10-22 stsp if (errcode) {
1594 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1595 13add988 2019-10-15 stsp "pthread_mutex_lock");
1596 1a76625f 2018-10-22 stsp break;
1597 1a76625f 2018-10-22 stsp }
1598 1a76625f 2018-10-22 stsp
1599 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1600 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1601 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1602 1a76625f 2018-10-22 stsp
1603 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1604 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1605 7c1452c1 2020-03-26 stsp int have_match;
1606 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1607 7c1452c1 2020-03-26 stsp if (err)
1608 7c1452c1 2020-03-26 stsp break;
1609 7c1452c1 2020-03-26 stsp if (have_match)
1610 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1611 13add988 2019-10-15 stsp }
1612 13add988 2019-10-15 stsp
1613 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1614 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1615 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1616 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1617 7c1452c1 2020-03-26 stsp if (err)
1618 13add988 2019-10-15 stsp break;
1619 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1620 899d86c2 2018-05-10 stsp
1621 9ba79e04 2018-06-11 stsp return err;
1622 0553a4e3 2018-04-30 stsp }
1623 0553a4e3 2018-04-30 stsp
1624 2b779855 2020-12-05 naddy static void
1625 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1626 2b779855 2020-12-05 naddy {
1627 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1628 2b779855 2020-12-05 naddy int ncommits = 0;
1629 2b779855 2020-12-05 naddy
1630 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1631 2b779855 2020-12-05 naddy while (entry) {
1632 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1633 2b779855 2020-12-05 naddy s->selected_entry = entry;
1634 2b779855 2020-12-05 naddy break;
1635 2b779855 2020-12-05 naddy }
1636 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1637 2b779855 2020-12-05 naddy ncommits++;
1638 2b779855 2020-12-05 naddy }
1639 2b779855 2020-12-05 naddy }
1640 2b779855 2020-12-05 naddy
1641 0553a4e3 2018-04-30 stsp static const struct got_error *
1642 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1643 0553a4e3 2018-04-30 stsp {
1644 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1645 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1646 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1647 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1648 60493ae3 2019-06-20 stsp int width;
1649 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1650 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1651 8b473291 2019-02-21 stsp char *refs_str = NULL;
1652 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1653 11b20872 2019-11-08 stsp struct tog_color *tc;
1654 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1655 0553a4e3 2018-04-30 stsp
1656 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1657 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1658 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1659 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1660 1a76625f 2018-10-22 stsp if (err)
1661 ecb28ae0 2018-07-16 stsp return err;
1662 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1663 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1664 d2075bf3 2020-12-25 stsp if (refs) {
1665 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1666 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1667 d2075bf3 2020-12-25 stsp if (err)
1668 d2075bf3 2020-12-25 stsp goto done;
1669 d2075bf3 2020-12-25 stsp }
1670 867c6645 2018-07-10 stsp }
1671 359bfafd 2019-02-22 stsp
1672 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1673 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1674 1a76625f 2018-10-22 stsp
1675 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1676 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1677 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1678 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1679 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1680 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1681 8f4ed634 2020-03-26 stsp goto done;
1682 8f4ed634 2020-03-26 stsp }
1683 8f4ed634 2020-03-26 stsp } else {
1684 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1685 f9686aa5 2020-03-27 stsp
1686 f9686aa5 2020-03-27 stsp if (view->searching) {
1687 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1688 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1689 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1690 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1691 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1692 f9686aa5 2020-03-27 stsp search_str = "searching...";
1693 f9686aa5 2020-03-27 stsp }
1694 f9686aa5 2020-03-27 stsp
1695 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1696 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1697 f9686aa5 2020-03-27 stsp search_str ? search_str :
1698 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1699 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1700 8f4ed634 2020-03-26 stsp goto done;
1701 8f4ed634 2020-03-26 stsp }
1702 8b473291 2019-02-21 stsp }
1703 1a76625f 2018-10-22 stsp
1704 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1705 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1706 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1707 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1709 1a76625f 2018-10-22 stsp header = NULL;
1710 1a76625f 2018-10-22 stsp goto done;
1711 1a76625f 2018-10-22 stsp }
1712 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1713 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1714 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1715 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1716 1a76625f 2018-10-22 stsp header = NULL;
1717 1a76625f 2018-10-22 stsp goto done;
1718 ecb28ae0 2018-07-16 stsp }
1719 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1720 1a76625f 2018-10-22 stsp if (err)
1721 1a76625f 2018-10-22 stsp goto done;
1722 867c6645 2018-07-10 stsp
1723 2814baeb 2018-08-01 stsp werase(view->window);
1724 867c6645 2018-07-10 stsp
1725 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1726 a3404814 2018-09-02 stsp wstandout(view->window);
1727 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1728 11b20872 2019-11-08 stsp if (tc)
1729 11b20872 2019-11-08 stsp wattr_on(view->window,
1730 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1731 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1732 11b20872 2019-11-08 stsp if (tc)
1733 11b20872 2019-11-08 stsp wattr_off(view->window,
1734 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1735 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1736 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1737 1a76625f 2018-10-22 stsp width++;
1738 1a76625f 2018-10-22 stsp }
1739 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1740 a3404814 2018-09-02 stsp wstandend(view->window);
1741 ecb28ae0 2018-07-16 stsp free(wline);
1742 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1743 1a76625f 2018-10-22 stsp goto done;
1744 0553a4e3 2018-04-30 stsp
1745 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1746 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1747 5813d178 2019-03-09 stsp ncommits = 0;
1748 5813d178 2019-03-09 stsp while (entry) {
1749 5813d178 2019-03-09 stsp char *author;
1750 5813d178 2019-03-09 stsp wchar_t *wauthor;
1751 5813d178 2019-03-09 stsp int width;
1752 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1753 5813d178 2019-03-09 stsp break;
1754 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1755 5813d178 2019-03-09 stsp if (author == NULL) {
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1757 5813d178 2019-03-09 stsp goto done;
1758 5813d178 2019-03-09 stsp }
1759 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1760 27a741e5 2019-09-11 stsp date_display_cols);
1761 5813d178 2019-03-09 stsp if (author_cols < width)
1762 5813d178 2019-03-09 stsp author_cols = width;
1763 5813d178 2019-03-09 stsp free(wauthor);
1764 5813d178 2019-03-09 stsp free(author);
1765 7ca04879 2019-10-19 stsp ncommits++;
1766 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1767 5813d178 2019-03-09 stsp }
1768 5813d178 2019-03-09 stsp
1769 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1770 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1771 867c6645 2018-07-10 stsp ncommits = 0;
1772 899d86c2 2018-05-10 stsp while (entry) {
1773 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1774 899d86c2 2018-05-10 stsp break;
1775 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1776 2814baeb 2018-08-01 stsp wstandout(view->window);
1777 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1778 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1779 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1780 2814baeb 2018-08-01 stsp wstandend(view->window);
1781 0553a4e3 2018-04-30 stsp if (err)
1782 60493ae3 2019-06-20 stsp goto done;
1783 0553a4e3 2018-04-30 stsp ncommits++;
1784 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1785 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1786 80ddbec8 2018-04-29 stsp }
1787 80ddbec8 2018-04-29 stsp
1788 1a57306a 2018-09-02 stsp view_vborder(view);
1789 dd038bc6 2021-09-21 thomas.ad update_panels();
1790 dd038bc6 2021-09-21 thomas.ad doupdate();
1791 1a76625f 2018-10-22 stsp done:
1792 1a76625f 2018-10-22 stsp free(id_str);
1793 8b473291 2019-02-21 stsp free(refs_str);
1794 1a76625f 2018-10-22 stsp free(ncommits_str);
1795 1a76625f 2018-10-22 stsp free(header);
1796 80ddbec8 2018-04-29 stsp return err;
1797 9f7d7167 2018-04-29 stsp }
1798 07b55e75 2018-05-10 stsp
1799 07b55e75 2018-05-10 stsp static void
1800 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1801 07b55e75 2018-05-10 stsp {
1802 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1803 07b55e75 2018-05-10 stsp int nscrolled = 0;
1804 07b55e75 2018-05-10 stsp
1805 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1806 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1807 07b55e75 2018-05-10 stsp return;
1808 9f7d7167 2018-04-29 stsp
1809 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1810 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1811 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1812 07b55e75 2018-05-10 stsp if (entry) {
1813 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1814 07b55e75 2018-05-10 stsp nscrolled++;
1815 07b55e75 2018-05-10 stsp }
1816 07b55e75 2018-05-10 stsp }
1817 aa075928 2018-05-10 stsp }
1818 aa075928 2018-05-10 stsp
1819 aa075928 2018-05-10 stsp static const struct got_error *
1820 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1821 aa075928 2018-05-10 stsp {
1822 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1823 5e224a3e 2019-02-22 stsp int errcode;
1824 8a42fca8 2019-02-22 stsp
1825 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1826 aa075928 2018-05-10 stsp
1827 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1828 ffe38506 2020-12-01 naddy if (ta->log_complete)
1829 5e224a3e 2019-02-22 stsp break;
1830 b295e71b 2019-02-22 stsp
1831 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1832 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1833 7aafa0d1 2019-02-22 stsp if (errcode)
1834 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1835 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1836 7c1452c1 2020-03-26 stsp
1837 7c1452c1 2020-03-26 stsp /*
1838 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1839 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1840 7c1452c1 2020-03-26 stsp */
1841 7c1452c1 2020-03-26 stsp if (!wait)
1842 7c1452c1 2020-03-26 stsp break;
1843 7c1452c1 2020-03-26 stsp
1844 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1845 ffe38506 2020-12-01 naddy show_log_view(view);
1846 7c1452c1 2020-03-26 stsp update_panels();
1847 7c1452c1 2020-03-26 stsp doupdate();
1848 7c1452c1 2020-03-26 stsp
1849 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1850 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1851 82954512 2020-02-03 stsp if (errcode)
1852 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1853 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1854 82954512 2020-02-03 stsp
1855 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1856 ffe38506 2020-12-01 naddy show_log_view(view);
1857 7c1452c1 2020-03-26 stsp update_panels();
1858 7c1452c1 2020-03-26 stsp doupdate();
1859 5e224a3e 2019-02-22 stsp }
1860 5e224a3e 2019-02-22 stsp
1861 5e224a3e 2019-02-22 stsp return NULL;
1862 5e224a3e 2019-02-22 stsp }
1863 5e224a3e 2019-02-22 stsp
1864 5e224a3e 2019-02-22 stsp static const struct got_error *
1865 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1866 5e224a3e 2019-02-22 stsp {
1867 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1868 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1869 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1870 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1871 5e224a3e 2019-02-22 stsp
1872 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1873 5e224a3e 2019-02-22 stsp return NULL;
1874 5e224a3e 2019-02-22 stsp
1875 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1876 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1877 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1878 08ebd0a9 2019-02-22 stsp /*
1879 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1880 08ebd0a9 2019-02-22 stsp */
1881 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1882 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1883 5e224a3e 2019-02-22 stsp if (err)
1884 5e224a3e 2019-02-22 stsp return err;
1885 7aafa0d1 2019-02-22 stsp }
1886 b295e71b 2019-02-22 stsp
1887 7aafa0d1 2019-02-22 stsp do {
1888 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1889 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1890 88048b54 2019-02-21 stsp break;
1891 88048b54 2019-02-21 stsp
1892 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1893 aa075928 2018-05-10 stsp
1894 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1895 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1896 dd0a52c1 2018-05-20 stsp break;
1897 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1898 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1899 aa075928 2018-05-10 stsp
1900 dd0a52c1 2018-05-20 stsp return err;
1901 07b55e75 2018-05-10 stsp }
1902 4a7f7875 2018-05-10 stsp
1903 cd0acaa7 2018-05-20 stsp static const struct got_error *
1904 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1905 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1906 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1907 cd0acaa7 2018-05-20 stsp {
1908 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1909 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1910 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1911 cd0acaa7 2018-05-20 stsp
1912 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1913 15a94983 2018-12-23 stsp if (diff_view == NULL)
1914 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1915 ea5e7bb5 2018-08-01 stsp
1916 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1917 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1918 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1919 e5a0f69f 2018-08-18 stsp if (err == NULL)
1920 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1921 cd0acaa7 2018-05-20 stsp return err;
1922 4a7f7875 2018-05-10 stsp }
1923 4a7f7875 2018-05-10 stsp
1924 80ddbec8 2018-04-29 stsp static const struct got_error *
1925 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1926 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1927 9343a5fb 2018-06-23 stsp {
1928 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1929 941e9f74 2019-05-21 stsp
1930 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1931 941e9f74 2019-05-21 stsp if (parent == NULL)
1932 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1933 941e9f74 2019-05-21 stsp
1934 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1935 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1936 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1937 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1938 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1939 941e9f74 2019-05-21 stsp s->tree = subtree;
1940 941e9f74 2019-05-21 stsp s->selected = 0;
1941 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1942 941e9f74 2019-05-21 stsp return NULL;
1943 941e9f74 2019-05-21 stsp }
1944 941e9f74 2019-05-21 stsp
1945 941e9f74 2019-05-21 stsp static const struct got_error *
1946 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1947 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1948 941e9f74 2019-05-21 stsp {
1949 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1950 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1951 941e9f74 2019-05-21 stsp const char *p;
1952 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1953 9343a5fb 2018-06-23 stsp
1954 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1955 941e9f74 2019-05-21 stsp p = path;
1956 941e9f74 2019-05-21 stsp while (*p) {
1957 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1958 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1959 56e0773d 2019-11-28 stsp char *te_name;
1960 33cbf02b 2020-01-12 stsp
1961 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1962 33cbf02b 2020-01-12 stsp p++;
1963 941e9f74 2019-05-21 stsp
1964 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1965 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1966 941e9f74 2019-05-21 stsp if (slash == NULL)
1967 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1968 33cbf02b 2020-01-12 stsp else
1969 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1970 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1971 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1972 56e0773d 2019-11-28 stsp break;
1973 941e9f74 2019-05-21 stsp }
1974 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1975 56e0773d 2019-11-28 stsp if (te == NULL) {
1976 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1977 56e0773d 2019-11-28 stsp free(te_name);
1978 941e9f74 2019-05-21 stsp break;
1979 941e9f74 2019-05-21 stsp }
1980 56e0773d 2019-11-28 stsp free(te_name);
1981 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1982 941e9f74 2019-05-21 stsp
1983 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1984 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1985 b03c880f 2019-05-21 stsp
1986 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1987 941e9f74 2019-05-21 stsp if (slash)
1988 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1989 941e9f74 2019-05-21 stsp else
1990 941e9f74 2019-05-21 stsp subpath = strdup(path);
1991 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1992 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1993 941e9f74 2019-05-21 stsp break;
1994 941e9f74 2019-05-21 stsp }
1995 941e9f74 2019-05-21 stsp
1996 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
1997 941e9f74 2019-05-21 stsp subpath);
1998 941e9f74 2019-05-21 stsp if (err)
1999 941e9f74 2019-05-21 stsp break;
2000 941e9f74 2019-05-21 stsp
2001 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2002 941e9f74 2019-05-21 stsp free(tree_id);
2003 941e9f74 2019-05-21 stsp if (err)
2004 941e9f74 2019-05-21 stsp break;
2005 941e9f74 2019-05-21 stsp
2006 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2007 941e9f74 2019-05-21 stsp if (err) {
2008 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2009 941e9f74 2019-05-21 stsp break;
2010 941e9f74 2019-05-21 stsp }
2011 941e9f74 2019-05-21 stsp if (slash == NULL)
2012 941e9f74 2019-05-21 stsp break;
2013 941e9f74 2019-05-21 stsp free(subpath);
2014 941e9f74 2019-05-21 stsp subpath = NULL;
2015 941e9f74 2019-05-21 stsp p = slash;
2016 941e9f74 2019-05-21 stsp }
2017 941e9f74 2019-05-21 stsp
2018 941e9f74 2019-05-21 stsp free(subpath);
2019 1a76625f 2018-10-22 stsp return err;
2020 61266923 2020-01-14 stsp }
2021 61266923 2020-01-14 stsp
2022 61266923 2020-01-14 stsp static const struct got_error *
2023 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2024 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2025 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2026 55cccc34 2020-02-20 stsp {
2027 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2028 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2029 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2030 55cccc34 2020-02-20 stsp
2031 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2032 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2033 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2034 55cccc34 2020-02-20 stsp
2035 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2036 bc573f3b 2021-07-10 stsp if (err)
2037 55cccc34 2020-02-20 stsp return err;
2038 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2039 55cccc34 2020-02-20 stsp
2040 55cccc34 2020-02-20 stsp *new_view = tree_view;
2041 55cccc34 2020-02-20 stsp
2042 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2043 55cccc34 2020-02-20 stsp return NULL;
2044 55cccc34 2020-02-20 stsp
2045 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2046 55cccc34 2020-02-20 stsp }
2047 55cccc34 2020-02-20 stsp
2048 55cccc34 2020-02-20 stsp static const struct got_error *
2049 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2050 61266923 2020-01-14 stsp {
2051 61266923 2020-01-14 stsp sigset_t sigset;
2052 61266923 2020-01-14 stsp int errcode;
2053 61266923 2020-01-14 stsp
2054 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2055 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2056 61266923 2020-01-14 stsp
2057 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2058 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2059 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2060 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2061 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2062 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2063 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2064 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2065 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2066 61266923 2020-01-14 stsp
2067 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2068 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2069 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2070 61266923 2020-01-14 stsp
2071 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2072 61266923 2020-01-14 stsp if (errcode)
2073 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2074 61266923 2020-01-14 stsp
2075 61266923 2020-01-14 stsp return NULL;
2076 1a76625f 2018-10-22 stsp }
2077 1a76625f 2018-10-22 stsp
2078 1a76625f 2018-10-22 stsp static void *
2079 1a76625f 2018-10-22 stsp log_thread(void *arg)
2080 1a76625f 2018-10-22 stsp {
2081 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2082 1a76625f 2018-10-22 stsp int errcode = 0;
2083 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2084 1a76625f 2018-10-22 stsp int done = 0;
2085 61266923 2020-01-14 stsp
2086 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2087 61266923 2020-01-14 stsp if (err)
2088 61266923 2020-01-14 stsp return (void *)err;
2089 1a76625f 2018-10-22 stsp
2090 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2091 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2092 1a76625f 2018-10-22 stsp if (err) {
2093 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2094 1a76625f 2018-10-22 stsp return (void *)err;
2095 1a76625f 2018-10-22 stsp err = NULL;
2096 1a76625f 2018-10-22 stsp done = 1;
2097 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2098 1a76625f 2018-10-22 stsp a->commits_needed--;
2099 1a76625f 2018-10-22 stsp
2100 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2101 3abe8080 2019-04-10 stsp if (errcode) {
2102 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2103 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2104 3abe8080 2019-04-10 stsp break;
2105 3abe8080 2019-04-10 stsp } else if (*a->quit)
2106 1a76625f 2018-10-22 stsp done = 1;
2107 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2108 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2109 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2110 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2111 1a76625f 2018-10-22 stsp }
2112 1a76625f 2018-10-22 stsp
2113 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2114 7c1452c1 2020-03-26 stsp if (errcode) {
2115 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2116 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2117 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2118 7c1452c1 2020-03-26 stsp break;
2119 7c1452c1 2020-03-26 stsp }
2120 7c1452c1 2020-03-26 stsp
2121 1a76625f 2018-10-22 stsp if (done)
2122 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2123 7c1452c1 2020-03-26 stsp else {
2124 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2125 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2126 7c1452c1 2020-03-26 stsp &tog_mutex);
2127 7c1452c1 2020-03-26 stsp if (errcode)
2128 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2129 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2130 21355643 2020-12-06 stsp if (*a->quit)
2131 21355643 2020-12-06 stsp done = 1;
2132 7c1452c1 2020-03-26 stsp }
2133 1a76625f 2018-10-22 stsp }
2134 1a76625f 2018-10-22 stsp
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2137 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2138 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2139 1a76625f 2018-10-22 stsp }
2140 3abe8080 2019-04-10 stsp a->log_complete = 1;
2141 1a76625f 2018-10-22 stsp return (void *)err;
2142 1a76625f 2018-10-22 stsp }
2143 1a76625f 2018-10-22 stsp
2144 1a76625f 2018-10-22 stsp static const struct got_error *
2145 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2146 1a76625f 2018-10-22 stsp {
2147 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2148 1a76625f 2018-10-22 stsp int errcode;
2149 1a76625f 2018-10-22 stsp
2150 1a76625f 2018-10-22 stsp if (s->thread) {
2151 1a76625f 2018-10-22 stsp s->quit = 1;
2152 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2153 1a76625f 2018-10-22 stsp if (errcode)
2154 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2155 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2156 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2157 1a76625f 2018-10-22 stsp if (errcode)
2158 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2159 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2160 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2161 1a76625f 2018-10-22 stsp if (errcode)
2162 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2163 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2164 1a76625f 2018-10-22 stsp if (errcode)
2165 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2166 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2167 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2168 1a76625f 2018-10-22 stsp }
2169 1a76625f 2018-10-22 stsp
2170 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2171 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2172 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2173 1af5eddf 2022-06-23 thomas }
2174 1af5eddf 2022-06-23 thomas
2175 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2176 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2177 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2178 1af5eddf 2022-06-23 thomas if (err == NULL)
2179 1af5eddf 2022-06-23 thomas err = pack_err;
2180 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2181 1a76625f 2018-10-22 stsp }
2182 1a76625f 2018-10-22 stsp
2183 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2184 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2185 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2186 1a76625f 2018-10-22 stsp }
2187 1a76625f 2018-10-22 stsp
2188 9343a5fb 2018-06-23 stsp return err;
2189 9343a5fb 2018-06-23 stsp }
2190 9343a5fb 2018-06-23 stsp
2191 9343a5fb 2018-06-23 stsp static const struct got_error *
2192 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2193 1a76625f 2018-10-22 stsp {
2194 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2195 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2196 276b94a1 2020-11-13 naddy int errcode;
2197 1a76625f 2018-10-22 stsp
2198 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2199 276b94a1 2020-11-13 naddy
2200 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2201 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2202 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2203 276b94a1 2020-11-13 naddy
2204 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2205 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2206 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2207 276b94a1 2020-11-13 naddy
2208 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2209 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2210 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2211 1a76625f 2018-10-22 stsp free(s->start_id);
2212 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2213 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2214 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2215 1a76625f 2018-10-22 stsp return err;
2216 1a76625f 2018-10-22 stsp }
2217 1a76625f 2018-10-22 stsp
2218 1a76625f 2018-10-22 stsp static const struct got_error *
2219 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2220 60493ae3 2019-06-20 stsp {
2221 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2222 60493ae3 2019-06-20 stsp
2223 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2224 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2225 60493ae3 2019-06-20 stsp return NULL;
2226 60493ae3 2019-06-20 stsp }
2227 60493ae3 2019-06-20 stsp
2228 60493ae3 2019-06-20 stsp static const struct got_error *
2229 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2230 60493ae3 2019-06-20 stsp {
2231 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2232 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2233 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2234 60493ae3 2019-06-20 stsp
2235 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2236 f9686aa5 2020-03-27 stsp show_log_view(view);
2237 f9686aa5 2020-03-27 stsp update_panels();
2238 f9686aa5 2020-03-27 stsp doupdate();
2239 f9686aa5 2020-03-27 stsp
2240 96e2b566 2019-07-08 stsp if (s->search_entry) {
2241 13add988 2019-10-15 stsp int errcode, ch;
2242 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2243 13add988 2019-10-15 stsp if (errcode)
2244 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2245 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2246 13add988 2019-10-15 stsp ch = wgetch(view->window);
2247 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2248 13add988 2019-10-15 stsp if (errcode)
2249 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2250 13add988 2019-10-15 stsp "pthread_mutex_lock");
2251 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2252 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2253 678cbce5 2019-07-28 stsp return NULL;
2254 678cbce5 2019-07-28 stsp }
2255 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2256 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2257 96e2b566 2019-07-08 stsp else
2258 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2259 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2260 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2261 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2262 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2263 b1bf1435 2019-06-21 stsp else
2264 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2265 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2266 20be8d96 2019-06-21 stsp } else {
2267 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2268 20be8d96 2019-06-21 stsp }
2269 60493ae3 2019-06-20 stsp
2270 60493ae3 2019-06-20 stsp while (1) {
2271 13add988 2019-10-15 stsp int have_match = 0;
2272 13add988 2019-10-15 stsp
2273 60493ae3 2019-06-20 stsp if (entry == NULL) {
2274 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2275 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2276 f9967bca 2020-03-27 stsp view->search_next_done =
2277 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2278 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2279 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2280 f801134a 2019-06-25 stsp return NULL;
2281 60493ae3 2019-06-20 stsp }
2282 96e2b566 2019-07-08 stsp /*
2283 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2284 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2285 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2286 96e2b566 2019-07-08 stsp */
2287 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2288 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2289 60493ae3 2019-06-20 stsp }
2290 60493ae3 2019-06-20 stsp
2291 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2292 13add988 2019-10-15 stsp &view->regex);
2293 5943eee2 2019-08-13 stsp if (err)
2294 13add988 2019-10-15 stsp break;
2295 13add988 2019-10-15 stsp if (have_match) {
2296 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2297 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2298 60493ae3 2019-06-20 stsp break;
2299 60493ae3 2019-06-20 stsp }
2300 13add988 2019-10-15 stsp
2301 96e2b566 2019-07-08 stsp s->search_entry = entry;
2302 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2303 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2304 b1bf1435 2019-06-21 stsp else
2305 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2306 60493ae3 2019-06-20 stsp }
2307 60493ae3 2019-06-20 stsp
2308 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2309 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2310 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2311 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2312 60493ae3 2019-06-20 stsp if (err)
2313 60493ae3 2019-06-20 stsp return err;
2314 ead14cbe 2019-06-21 stsp cur++;
2315 ead14cbe 2019-06-21 stsp }
2316 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2317 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2318 60493ae3 2019-06-20 stsp if (err)
2319 60493ae3 2019-06-20 stsp return err;
2320 ead14cbe 2019-06-21 stsp cur--;
2321 60493ae3 2019-06-20 stsp }
2322 60493ae3 2019-06-20 stsp }
2323 60493ae3 2019-06-20 stsp
2324 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2325 96e2b566 2019-07-08 stsp
2326 60493ae3 2019-06-20 stsp return NULL;
2327 60493ae3 2019-06-20 stsp }
2328 60493ae3 2019-06-20 stsp
2329 60493ae3 2019-06-20 stsp static const struct got_error *
2330 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2331 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2332 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2333 80ddbec8 2018-04-29 stsp {
2334 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2335 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2336 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2337 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2338 1a76625f 2018-10-22 stsp int errcode;
2339 80ddbec8 2018-04-29 stsp
2340 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2341 f135c941 2020-02-20 stsp free(s->in_repo_path);
2342 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2343 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2344 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2345 f135c941 2020-02-20 stsp }
2346 ecb28ae0 2018-07-16 stsp
2347 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2348 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2349 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2350 78756c87 2020-11-24 stsp
2351 fb2756b9 2018-08-04 stsp s->repo = repo;
2352 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2353 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2354 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2355 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2356 9cd7cbd1 2020-12-07 stsp goto done;
2357 9cd7cbd1 2020-12-07 stsp }
2358 9cd7cbd1 2020-12-07 stsp }
2359 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2360 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2361 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2362 5036bf37 2018-09-24 stsp goto done;
2363 5036bf37 2018-09-24 stsp }
2364 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2365 e5a0f69f 2018-08-18 stsp
2366 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2367 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2368 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2369 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2370 11b20872 2019-11-08 stsp if (err)
2371 11b20872 2019-11-08 stsp goto done;
2372 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2373 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2374 11b20872 2019-11-08 stsp if (err) {
2375 11b20872 2019-11-08 stsp free_colors(&s->colors);
2376 11b20872 2019-11-08 stsp goto done;
2377 11b20872 2019-11-08 stsp }
2378 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2379 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2380 11b20872 2019-11-08 stsp if (err) {
2381 11b20872 2019-11-08 stsp free_colors(&s->colors);
2382 11b20872 2019-11-08 stsp goto done;
2383 11b20872 2019-11-08 stsp }
2384 11b20872 2019-11-08 stsp }
2385 11b20872 2019-11-08 stsp
2386 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2387 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2388 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2389 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2390 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2391 1a76625f 2018-10-22 stsp
2392 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2393 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2394 1af5eddf 2022-06-23 thomas if (err)
2395 1af5eddf 2022-06-23 thomas goto done;
2396 1af5eddf 2022-06-23 thomas }
2397 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2398 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2399 7cd52833 2022-06-23 thomas if (err)
2400 7cd52833 2022-06-23 thomas goto done;
2401 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2402 b672a97a 2020-01-27 stsp !s->log_branches);
2403 1a76625f 2018-10-22 stsp if (err)
2404 1a76625f 2018-10-22 stsp goto done;
2405 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2406 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2407 c5b78334 2020-01-12 stsp if (err)
2408 c5b78334 2020-01-12 stsp goto done;
2409 1a76625f 2018-10-22 stsp
2410 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2411 1a76625f 2018-10-22 stsp if (errcode) {
2412 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2413 1a76625f 2018-10-22 stsp goto done;
2414 1a76625f 2018-10-22 stsp }
2415 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2416 7c1452c1 2020-03-26 stsp if (errcode) {
2417 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2418 7c1452c1 2020-03-26 stsp goto done;
2419 7c1452c1 2020-03-26 stsp }
2420 1a76625f 2018-10-22 stsp
2421 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2422 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2423 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2424 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2425 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2426 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2427 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2428 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2429 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2430 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2431 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2432 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2433 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2434 ba4f502b 2018-08-04 stsp done:
2435 1a76625f 2018-10-22 stsp if (err)
2436 1a76625f 2018-10-22 stsp close_log_view(view);
2437 ba4f502b 2018-08-04 stsp return err;
2438 ba4f502b 2018-08-04 stsp }
2439 ba4f502b 2018-08-04 stsp
2440 e5a0f69f 2018-08-18 stsp static const struct got_error *
2441 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2442 ba4f502b 2018-08-04 stsp {
2443 f2f6d207 2020-11-24 stsp const struct got_error *err;
2444 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2445 ba4f502b 2018-08-04 stsp
2446 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2447 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2448 2b380cc8 2018-10-24 stsp &s->thread_args);
2449 2b380cc8 2018-10-24 stsp if (errcode)
2450 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2451 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2452 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2453 f2f6d207 2020-11-24 stsp if (err)
2454 f2f6d207 2020-11-24 stsp return err;
2455 f2f6d207 2020-11-24 stsp }
2456 2b380cc8 2018-10-24 stsp }
2457 2b380cc8 2018-10-24 stsp
2458 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2459 e5a0f69f 2018-08-18 stsp }
2460 04cc582a 2018-08-01 stsp
2461 e5a0f69f 2018-08-18 stsp static const struct got_error *
2462 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2463 e5a0f69f 2018-08-18 stsp {
2464 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2465 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2466 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2467 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2468 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2469 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2470 80ddbec8 2018-04-29 stsp
2471 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2472 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2473 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2474 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2475 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2476 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2477 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2478 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2479 fb280deb 2021-08-30 stsp select_commit(s);
2480 fb280deb 2021-08-30 stsp }
2481 528dedf3 2021-08-30 stsp return NULL;
2482 528dedf3 2021-08-30 stsp }
2483 528dedf3 2021-08-30 stsp
2484 528dedf3 2021-08-30 stsp switch (ch) {
2485 1e37a5c2 2019-05-12 jcs case 'q':
2486 1e37a5c2 2019-05-12 jcs s->quit = 1;
2487 1e37a5c2 2019-05-12 jcs break;
2488 1e37a5c2 2019-05-12 jcs case 'k':
2489 1e37a5c2 2019-05-12 jcs case KEY_UP:
2490 1e37a5c2 2019-05-12 jcs case '<':
2491 1e37a5c2 2019-05-12 jcs case ',':
2492 f7140bf5 2021-10-17 thomas case CTRL('p'):
2493 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2494 1a76625f 2018-10-22 stsp break;
2495 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2496 1e37a5c2 2019-05-12 jcs s->selected--;
2497 1144d21a 2019-06-21 stsp else
2498 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2499 912a3f79 2021-08-30 j select_commit(s);
2500 912a3f79 2021-08-30 j break;
2501 912a3f79 2021-08-30 j case 'g':
2502 912a3f79 2021-08-30 j case KEY_HOME:
2503 912a3f79 2021-08-30 j s->selected = 0;
2504 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2505 2b779855 2020-12-05 naddy select_commit(s);
2506 1e37a5c2 2019-05-12 jcs break;
2507 70f17a53 2022-06-13 thomas case CTRL('u'):
2508 23427b14 2022-06-23 thomas case 'u':
2509 70f17a53 2022-06-13 thomas nscroll /= 2;
2510 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2511 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2512 a4292ac5 2019-05-12 jcs case CTRL('b'):
2513 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2514 e5a0f69f 2018-08-18 stsp break;
2515 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2516 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2517 2b779855 2020-12-05 naddy else
2518 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2519 2b779855 2020-12-05 naddy select_commit(s);
2520 1e37a5c2 2019-05-12 jcs break;
2521 1e37a5c2 2019-05-12 jcs case 'j':
2522 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2523 1e37a5c2 2019-05-12 jcs case '>':
2524 1e37a5c2 2019-05-12 jcs case '.':
2525 f7140bf5 2021-10-17 thomas case CTRL('n'):
2526 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2527 e5a0f69f 2018-08-18 stsp break;
2528 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2529 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2530 1e37a5c2 2019-05-12 jcs s->selected++;
2531 2b779855 2020-12-05 naddy else {
2532 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2533 2b779855 2020-12-05 naddy if (err)
2534 2b779855 2020-12-05 naddy break;
2535 912a3f79 2021-08-30 j }
2536 912a3f79 2021-08-30 j select_commit(s);
2537 912a3f79 2021-08-30 j break;
2538 912a3f79 2021-08-30 j case 'G':
2539 912a3f79 2021-08-30 j case KEY_END: {
2540 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2541 912a3f79 2021-08-30 j * traverse them all. */
2542 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2543 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2544 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2545 80ddbec8 2018-04-29 stsp }
2546 912a3f79 2021-08-30 j
2547 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2548 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2549 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2550 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2551 f3bc9f1d 2021-09-05 naddy break;
2552 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2553 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2554 f3bc9f1d 2021-09-05 naddy }
2555 f3bc9f1d 2021-09-05 naddy if (n > 0)
2556 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2557 2b779855 2020-12-05 naddy select_commit(s);
2558 1e37a5c2 2019-05-12 jcs break;
2559 912a3f79 2021-08-30 j }
2560 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2561 23427b14 2022-06-23 thomas case 'd':
2562 70f17a53 2022-06-13 thomas nscroll /= 2;
2563 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2564 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2565 70f17a53 2022-06-13 thomas case CTRL('f'): {
2566 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2567 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2568 1e37a5c2 2019-05-12 jcs if (first == NULL)
2569 e5a0f69f 2018-08-18 stsp break;
2570 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2571 1cae65b4 2019-09-22 stsp if (err)
2572 1cae65b4 2019-09-22 stsp break;
2573 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2574 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2575 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2576 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2577 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2578 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2579 1e37a5c2 2019-05-12 jcs }
2580 2b779855 2020-12-05 naddy select_commit(s);
2581 1e37a5c2 2019-05-12 jcs break;
2582 1e37a5c2 2019-05-12 jcs }
2583 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2584 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2585 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2586 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2587 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2588 2b779855 2020-12-05 naddy select_commit(s);
2589 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2590 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2591 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2592 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2593 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2594 0bf7f153 2020-12-02 naddy }
2595 1e37a5c2 2019-05-12 jcs break;
2596 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2597 87c7274c 2019-05-12 jcs case ' ':
2598 1e37a5c2 2019-05-12 jcs case '\r':
2599 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2600 e5a0f69f 2018-08-18 stsp break;
2601 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2602 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2603 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2604 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2605 78756c87 2020-11-24 stsp view, s->repo);
2606 1e37a5c2 2019-05-12 jcs if (err)
2607 1e37a5c2 2019-05-12 jcs break;
2608 e78dc838 2020-12-04 stsp view->focussed = 0;
2609 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2610 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2611 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2612 f7013a22 2018-10-24 stsp if (err)
2613 1e37a5c2 2019-05-12 jcs return err;
2614 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2615 e78dc838 2020-12-04 stsp view->focus_child = 1;
2616 1e37a5c2 2019-05-12 jcs } else
2617 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2618 1e37a5c2 2019-05-12 jcs break;
2619 1e37a5c2 2019-05-12 jcs case 't':
2620 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2621 5036bf37 2018-09-24 stsp break;
2622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2623 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2624 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2625 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2626 4e97c21c 2020-12-06 stsp s->repo);
2627 1e37a5c2 2019-05-12 jcs if (err)
2628 e5a0f69f 2018-08-18 stsp break;
2629 e78dc838 2020-12-04 stsp view->focussed = 0;
2630 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2631 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2632 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2633 1e37a5c2 2019-05-12 jcs if (err)
2634 1e37a5c2 2019-05-12 jcs return err;
2635 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2636 e78dc838 2020-12-04 stsp view->focus_child = 1;
2637 1e37a5c2 2019-05-12 jcs } else
2638 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2639 1e37a5c2 2019-05-12 jcs break;
2640 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2641 21355643 2020-12-06 stsp case CTRL('l'):
2642 21355643 2020-12-06 stsp case 'B':
2643 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2644 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2645 1e37a5c2 2019-05-12 jcs break;
2646 21355643 2020-12-06 stsp err = stop_log_thread(s);
2647 74cfe85e 2020-10-20 stsp if (err)
2648 74cfe85e 2020-10-20 stsp return err;
2649 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2650 21355643 2020-12-06 stsp char *parent_path;
2651 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2652 21355643 2020-12-06 stsp if (err)
2653 21355643 2020-12-06 stsp return err;
2654 21355643 2020-12-06 stsp free(s->in_repo_path);
2655 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2656 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2657 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2658 21355643 2020-12-06 stsp struct got_object_id *start_id;
2659 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2660 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2661 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2662 21355643 2020-12-06 stsp if (err)
2663 21355643 2020-12-06 stsp return err;
2664 21355643 2020-12-06 stsp free(s->start_id);
2665 21355643 2020-12-06 stsp s->start_id = start_id;
2666 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2667 21355643 2020-12-06 stsp } else /* 'B' */
2668 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2669 21355643 2020-12-06 stsp
2670 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
2671 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
2672 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2673 74cfe85e 2020-10-20 stsp if (err)
2674 21355643 2020-12-06 stsp return err;
2675 51a10b52 2020-12-26 stsp tog_free_refs();
2676 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2677 ca51c541 2020-12-07 stsp if (err)
2678 ca51c541 2020-12-07 stsp return err;
2679 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2680 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2681 d01904d4 2019-06-25 stsp if (err)
2682 d01904d4 2019-06-25 stsp return err;
2683 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2684 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2685 b672a97a 2020-01-27 stsp if (err)
2686 b672a97a 2020-01-27 stsp return err;
2687 21355643 2020-12-06 stsp free_commits(&s->commits);
2688 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2689 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2690 21355643 2020-12-06 stsp s->selected_entry = NULL;
2691 21355643 2020-12-06 stsp s->selected = 0;
2692 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2693 21355643 2020-12-06 stsp s->quit = 0;
2694 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2695 d01904d4 2019-06-25 stsp break;
2696 6458efa5 2020-11-24 stsp case 'r':
2697 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2698 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2699 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2700 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2701 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2702 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2703 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2704 6458efa5 2020-11-24 stsp if (err) {
2705 6458efa5 2020-11-24 stsp view_close(ref_view);
2706 6458efa5 2020-11-24 stsp return err;
2707 6458efa5 2020-11-24 stsp }
2708 e78dc838 2020-12-04 stsp view->focussed = 0;
2709 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2710 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2711 6458efa5 2020-11-24 stsp err = view_close_child(view);
2712 6458efa5 2020-11-24 stsp if (err)
2713 6458efa5 2020-11-24 stsp return err;
2714 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2715 e78dc838 2020-12-04 stsp view->focus_child = 1;
2716 6458efa5 2020-11-24 stsp } else
2717 6458efa5 2020-11-24 stsp *new_view = ref_view;
2718 6458efa5 2020-11-24 stsp break;
2719 1e37a5c2 2019-05-12 jcs default:
2720 1e37a5c2 2019-05-12 jcs break;
2721 899d86c2 2018-05-10 stsp }
2722 e5a0f69f 2018-08-18 stsp
2723 80ddbec8 2018-04-29 stsp return err;
2724 80ddbec8 2018-04-29 stsp }
2725 80ddbec8 2018-04-29 stsp
2726 4ed7e80c 2018-05-20 stsp static const struct got_error *
2727 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2728 c2db6724 2019-01-04 stsp {
2729 c2db6724 2019-01-04 stsp const struct got_error *error;
2730 c2db6724 2019-01-04 stsp
2731 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2732 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2733 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2734 37c06ea4 2019-07-15 stsp #endif
2735 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2736 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2737 c2db6724 2019-01-04 stsp
2738 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2739 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2740 c2db6724 2019-01-04 stsp
2741 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2742 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2743 c2db6724 2019-01-04 stsp
2744 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2745 c2db6724 2019-01-04 stsp if (error != NULL)
2746 c2db6724 2019-01-04 stsp return error;
2747 c2db6724 2019-01-04 stsp
2748 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2749 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2750 c2db6724 2019-01-04 stsp
2751 c2db6724 2019-01-04 stsp return NULL;
2752 c2db6724 2019-01-04 stsp }
2753 c2db6724 2019-01-04 stsp
2754 a915003a 2019-02-05 stsp static void
2755 a915003a 2019-02-05 stsp init_curses(void)
2756 a915003a 2019-02-05 stsp {
2757 296152d1 2022-05-31 thomas /*
2758 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2759 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2760 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2761 296152d1 2022-05-31 thomas */
2762 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2763 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2764 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2765 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2766 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2767 296152d1 2022-05-31 thomas
2768 a915003a 2019-02-05 stsp initscr();
2769 a915003a 2019-02-05 stsp cbreak();
2770 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2771 a915003a 2019-02-05 stsp noecho();
2772 a915003a 2019-02-05 stsp nonl();
2773 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2774 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2775 a915003a 2019-02-05 stsp curs_set(0);
2776 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2777 6d17833f 2019-11-08 stsp start_color();
2778 6d17833f 2019-11-08 stsp use_default_colors();
2779 6d17833f 2019-11-08 stsp }
2780 a915003a 2019-02-05 stsp }
2781 a915003a 2019-02-05 stsp
2782 c2db6724 2019-01-04 stsp static const struct got_error *
2783 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2784 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2785 f135c941 2020-02-20 stsp {
2786 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2787 f135c941 2020-02-20 stsp
2788 f135c941 2020-02-20 stsp if (argc == 0) {
2789 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2790 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2791 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2792 f135c941 2020-02-20 stsp return NULL;
2793 f135c941 2020-02-20 stsp }
2794 f135c941 2020-02-20 stsp
2795 f135c941 2020-02-20 stsp if (worktree) {
2796 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2797 bfd61697 2020-11-14 stsp char *p;
2798 f135c941 2020-02-20 stsp
2799 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2800 f135c941 2020-02-20 stsp if (err)
2801 f135c941 2020-02-20 stsp return err;
2802 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2803 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2804 bfd61697 2020-11-14 stsp p) == -1) {
2805 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2806 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2807 f135c941 2020-02-20 stsp }
2808 f135c941 2020-02-20 stsp free(p);
2809 f135c941 2020-02-20 stsp } else
2810 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2811 f135c941 2020-02-20 stsp
2812 f135c941 2020-02-20 stsp return err;
2813 f135c941 2020-02-20 stsp }
2814 f135c941 2020-02-20 stsp
2815 f135c941 2020-02-20 stsp static const struct got_error *
2816 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2817 9f7d7167 2018-04-29 stsp {
2818 80ddbec8 2018-04-29 stsp const struct got_error *error;
2819 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2820 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2821 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2822 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2823 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2824 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2825 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2826 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2827 04cc582a 2018-08-01 stsp struct tog_view *view;
2828 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
2829 80ddbec8 2018-04-29 stsp
2830 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2831 80ddbec8 2018-04-29 stsp switch (ch) {
2832 b672a97a 2020-01-27 stsp case 'b':
2833 b672a97a 2020-01-27 stsp log_branches = 1;
2834 b672a97a 2020-01-27 stsp break;
2835 80ddbec8 2018-04-29 stsp case 'c':
2836 80ddbec8 2018-04-29 stsp start_commit = optarg;
2837 80ddbec8 2018-04-29 stsp break;
2838 ecb28ae0 2018-07-16 stsp case 'r':
2839 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2840 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2841 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2842 9ba1d308 2019-10-21 stsp optarg);
2843 ecb28ae0 2018-07-16 stsp break;
2844 80ddbec8 2018-04-29 stsp default:
2845 17020d27 2019-03-07 stsp usage_log();
2846 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2847 80ddbec8 2018-04-29 stsp }
2848 80ddbec8 2018-04-29 stsp }
2849 80ddbec8 2018-04-29 stsp
2850 80ddbec8 2018-04-29 stsp argc -= optind;
2851 80ddbec8 2018-04-29 stsp argv += optind;
2852 80ddbec8 2018-04-29 stsp
2853 f135c941 2020-02-20 stsp if (argc > 1)
2854 f135c941 2020-02-20 stsp usage_log();
2855 963f97a1 2019-03-18 stsp
2856 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
2857 7cd52833 2022-06-23 thomas if (error != NULL)
2858 7cd52833 2022-06-23 thomas goto done;
2859 7cd52833 2022-06-23 thomas
2860 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2861 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2862 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2863 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2864 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2865 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2866 c156c7a4 2020-12-18 stsp goto done;
2867 a1fbf39a 2019-08-11 stsp if (worktree)
2868 6962eb72 2020-02-20 stsp repo_path =
2869 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2870 a1fbf39a 2019-08-11 stsp else
2871 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2872 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2873 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2874 c156c7a4 2020-12-18 stsp goto done;
2875 c156c7a4 2020-12-18 stsp }
2876 ecb28ae0 2018-07-16 stsp }
2877 ecb28ae0 2018-07-16 stsp
2878 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2879 80ddbec8 2018-04-29 stsp if (error != NULL)
2880 ecb28ae0 2018-07-16 stsp goto done;
2881 80ddbec8 2018-04-29 stsp
2882 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2883 f135c941 2020-02-20 stsp repo, worktree);
2884 f135c941 2020-02-20 stsp if (error)
2885 f135c941 2020-02-20 stsp goto done;
2886 f135c941 2020-02-20 stsp
2887 f135c941 2020-02-20 stsp init_curses();
2888 f135c941 2020-02-20 stsp
2889 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2890 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2891 c02c541e 2019-03-29 stsp if (error)
2892 c02c541e 2019-03-29 stsp goto done;
2893 c02c541e 2019-03-29 stsp
2894 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2895 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2896 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2897 87670572 2020-12-26 naddy if (error)
2898 87670572 2020-12-26 naddy goto done;
2899 87670572 2020-12-26 naddy }
2900 51a10b52 2020-12-26 stsp
2901 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2902 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2903 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2904 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2905 d8f38dc4 2020-12-05 stsp if (error)
2906 d8f38dc4 2020-12-05 stsp goto done;
2907 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2908 d8f38dc4 2020-12-05 stsp } else {
2909 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2910 d8f38dc4 2020-12-05 stsp if (error == NULL)
2911 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2912 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2913 d8f38dc4 2020-12-05 stsp goto done;
2914 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2915 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2916 d8f38dc4 2020-12-05 stsp if (error)
2917 d8f38dc4 2020-12-05 stsp goto done;
2918 d8f38dc4 2020-12-05 stsp }
2919 8b473291 2019-02-21 stsp
2920 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2921 04cc582a 2018-08-01 stsp if (view == NULL) {
2922 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2923 04cc582a 2018-08-01 stsp goto done;
2924 04cc582a 2018-08-01 stsp }
2925 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2926 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2927 ba4f502b 2018-08-04 stsp if (error)
2928 ba4f502b 2018-08-04 stsp goto done;
2929 2fc00ff4 2019-08-31 stsp if (worktree) {
2930 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2931 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2932 2fc00ff4 2019-08-31 stsp worktree = NULL;
2933 2fc00ff4 2019-08-31 stsp }
2934 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2935 ecb28ae0 2018-07-16 stsp done:
2936 f135c941 2020-02-20 stsp free(in_repo_path);
2937 ecb28ae0 2018-07-16 stsp free(repo_path);
2938 ecb28ae0 2018-07-16 stsp free(cwd);
2939 899d86c2 2018-05-10 stsp free(start_id);
2940 d8f38dc4 2020-12-05 stsp free(label);
2941 d8f38dc4 2020-12-05 stsp if (ref)
2942 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2943 1d0f4054 2021-06-17 stsp if (repo) {
2944 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2945 1d0f4054 2021-06-17 stsp if (error == NULL)
2946 1d0f4054 2021-06-17 stsp error = close_err;
2947 1d0f4054 2021-06-17 stsp }
2948 ec142235 2019-03-07 stsp if (worktree)
2949 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2950 7cd52833 2022-06-23 thomas if (pack_fds) {
2951 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
2952 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
2953 7cd52833 2022-06-23 thomas if (error == NULL)
2954 7cd52833 2022-06-23 thomas error = pack_err;
2955 7cd52833 2022-06-23 thomas }
2956 51a10b52 2020-12-26 stsp tog_free_refs();
2957 80ddbec8 2018-04-29 stsp return error;
2958 9f7d7167 2018-04-29 stsp }
2959 9f7d7167 2018-04-29 stsp
2960 4ed7e80c 2018-05-20 stsp __dead static void
2961 9f7d7167 2018-04-29 stsp usage_diff(void)
2962 9f7d7167 2018-04-29 stsp {
2963 80ddbec8 2018-04-29 stsp endwin();
2964 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2965 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2966 9f7d7167 2018-04-29 stsp exit(1);
2967 b304db33 2018-05-20 stsp }
2968 b304db33 2018-05-20 stsp
2969 6d17833f 2019-11-08 stsp static int
2970 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2971 41605754 2020-11-12 stsp regmatch_t *regmatch)
2972 6d17833f 2019-11-08 stsp {
2973 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2974 6d17833f 2019-11-08 stsp }
2975 6d17833f 2019-11-08 stsp
2976 f26dddb7 2019-11-08 stsp struct tog_color *
2977 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2978 6d17833f 2019-11-08 stsp {
2979 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2980 6d17833f 2019-11-08 stsp
2981 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2982 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2983 6d17833f 2019-11-08 stsp return tc;
2984 6d17833f 2019-11-08 stsp }
2985 6d17833f 2019-11-08 stsp
2986 6d17833f 2019-11-08 stsp return NULL;
2987 6d17833f 2019-11-08 stsp }
2988 6d17833f 2019-11-08 stsp
2989 4ed7e80c 2018-05-20 stsp static const struct got_error *
2990 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2991 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2992 41605754 2020-11-12 stsp {
2993 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2994 41605754 2020-11-12 stsp wchar_t *wline;
2995 41605754 2020-11-12 stsp int width;
2996 41605754 2020-11-12 stsp char *s;
2997 41605754 2020-11-12 stsp
2998 41605754 2020-11-12 stsp *wtotal = 0;
2999 41605754 2020-11-12 stsp
3000 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
3001 41605754 2020-11-12 stsp if (s == NULL)
3002 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
3003 41605754 2020-11-12 stsp
3004 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3005 41605754 2020-11-12 stsp if (err) {
3006 41605754 2020-11-12 stsp free(s);
3007 41605754 2020-11-12 stsp return err;
3008 41605754 2020-11-12 stsp }
3009 41605754 2020-11-12 stsp waddwstr(window, wline);
3010 41605754 2020-11-12 stsp free(wline);
3011 41605754 2020-11-12 stsp free(s);
3012 41605754 2020-11-12 stsp wlimit -= width;
3013 41605754 2020-11-12 stsp *wtotal += width;
3014 41605754 2020-11-12 stsp
3015 41605754 2020-11-12 stsp if (wlimit > 0) {
3016 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
3017 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
3018 41605754 2020-11-12 stsp if (s == NULL) {
3019 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
3020 41605754 2020-11-12 stsp free(s);
3021 41605754 2020-11-12 stsp return err;
3022 41605754 2020-11-12 stsp }
3023 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3024 41605754 2020-11-12 stsp if (err) {
3025 41605754 2020-11-12 stsp free(s);
3026 41605754 2020-11-12 stsp return err;
3027 41605754 2020-11-12 stsp }
3028 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
3029 41605754 2020-11-12 stsp waddwstr(window, wline);
3030 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3031 41605754 2020-11-12 stsp free(wline);
3032 41605754 2020-11-12 stsp free(s);
3033 41605754 2020-11-12 stsp wlimit -= width;
3034 41605754 2020-11-12 stsp *wtotal += width;
3035 41605754 2020-11-12 stsp }
3036 41605754 2020-11-12 stsp
3037 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3038 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3039 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3040 41605754 2020-11-12 stsp if (err)
3041 41605754 2020-11-12 stsp return err;
3042 41605754 2020-11-12 stsp waddwstr(window, wline);
3043 41605754 2020-11-12 stsp free(wline);
3044 41605754 2020-11-12 stsp *wtotal += width;
3045 41605754 2020-11-12 stsp }
3046 41605754 2020-11-12 stsp
3047 41605754 2020-11-12 stsp return NULL;
3048 41605754 2020-11-12 stsp }
3049 41605754 2020-11-12 stsp
3050 41605754 2020-11-12 stsp static const struct got_error *
3051 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3052 26ed57b2 2018-05-19 stsp {
3053 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3054 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3055 61e69b96 2018-05-20 stsp const struct got_error *err;
3056 fe621944 2020-11-10 stsp int nprinted = 0;
3057 b304db33 2018-05-20 stsp char *line;
3058 826082fe 2020-12-10 stsp size_t linesize = 0;
3059 826082fe 2020-12-10 stsp ssize_t linelen;
3060 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3061 61e69b96 2018-05-20 stsp wchar_t *wline;
3062 e0b650dd 2018-05-20 stsp int width;
3063 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3064 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3065 fe621944 2020-11-10 stsp off_t line_offset;
3066 26ed57b2 2018-05-19 stsp
3067 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3068 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3069 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3070 fe621944 2020-11-10 stsp
3071 f7d12f7e 2018-08-01 stsp werase(view->window);
3072 a3404814 2018-09-02 stsp
3073 a3404814 2018-09-02 stsp if (header) {
3074 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3075 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3076 135a2da0 2020-11-11 stsp header) == -1)
3077 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3078 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3079 135a2da0 2020-11-11 stsp free(line);
3080 135a2da0 2020-11-11 stsp if (err)
3081 a3404814 2018-09-02 stsp return err;
3082 a3404814 2018-09-02 stsp
3083 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3084 a3404814 2018-09-02 stsp wstandout(view->window);
3085 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3086 e54cc94a 2020-11-11 stsp free(wline);
3087 e54cc94a 2020-11-11 stsp wline = NULL;
3088 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3089 a3404814 2018-09-02 stsp wstandend(view->window);
3090 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3091 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3092 26ed57b2 2018-05-19 stsp
3093 a3404814 2018-09-02 stsp if (max_lines <= 1)
3094 a3404814 2018-09-02 stsp return NULL;
3095 a3404814 2018-09-02 stsp max_lines--;
3096 a3404814 2018-09-02 stsp }
3097 a3404814 2018-09-02 stsp
3098 89f1a395 2020-12-01 naddy s->eof = 0;
3099 826082fe 2020-12-10 stsp line = NULL;
3100 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3101 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3102 826082fe 2020-12-10 stsp if (linelen == -1) {
3103 826082fe 2020-12-10 stsp if (feof(s->f)) {
3104 826082fe 2020-12-10 stsp s->eof = 1;
3105 826082fe 2020-12-10 stsp break;
3106 826082fe 2020-12-10 stsp }
3107 826082fe 2020-12-10 stsp free(line);
3108 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3109 61e69b96 2018-05-20 stsp }
3110 6d17833f 2019-11-08 stsp
3111 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3112 f26dddb7 2019-11-08 stsp if (tc)
3113 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3114 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3115 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3116 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3117 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3118 41605754 2020-11-12 stsp view->window, regmatch);
3119 41605754 2020-11-12 stsp if (err) {
3120 41605754 2020-11-12 stsp free(line);
3121 41605754 2020-11-12 stsp return err;
3122 41605754 2020-11-12 stsp }
3123 41605754 2020-11-12 stsp } else {
3124 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3125 41605754 2020-11-12 stsp if (err) {
3126 41605754 2020-11-12 stsp free(line);
3127 41605754 2020-11-12 stsp return err;
3128 41605754 2020-11-12 stsp }
3129 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3130 41605754 2020-11-12 stsp free(wline);
3131 41605754 2020-11-12 stsp wline = NULL;
3132 41605754 2020-11-12 stsp }
3133 f26dddb7 2019-11-08 stsp if (tc)
3134 6d17833f 2019-11-08 stsp wattr_off(view->window,
3135 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3136 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3137 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3138 fe621944 2020-11-10 stsp nprinted++;
3139 826082fe 2020-12-10 stsp }
3140 826082fe 2020-12-10 stsp free(line);
3141 fe621944 2020-11-10 stsp if (nprinted >= 1)
3142 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3143 89f1a395 2020-12-01 naddy (nprinted - 1);
3144 fe621944 2020-11-10 stsp else
3145 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3146 26ed57b2 2018-05-19 stsp
3147 1a57306a 2018-09-02 stsp view_vborder(view);
3148 c3e9aa98 2019-05-13 jcs
3149 89f1a395 2020-12-01 naddy if (s->eof) {
3150 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3151 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3152 c3e9aa98 2019-05-13 jcs nprinted++;
3153 c3e9aa98 2019-05-13 jcs }
3154 c3e9aa98 2019-05-13 jcs
3155 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3156 c3e9aa98 2019-05-13 jcs if (err) {
3157 c3e9aa98 2019-05-13 jcs return err;
3158 c3e9aa98 2019-05-13 jcs }
3159 26ed57b2 2018-05-19 stsp
3160 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3161 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3162 e54cc94a 2020-11-11 stsp free(wline);
3163 e54cc94a 2020-11-11 stsp wline = NULL;
3164 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3165 c3e9aa98 2019-05-13 jcs }
3166 c3e9aa98 2019-05-13 jcs
3167 26ed57b2 2018-05-19 stsp return NULL;
3168 abd2672a 2018-12-23 stsp }
3169 abd2672a 2018-12-23 stsp
3170 abd2672a 2018-12-23 stsp static char *
3171 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3172 abd2672a 2018-12-23 stsp {
3173 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3174 09867e48 2019-08-13 stsp char *p, *s;
3175 09867e48 2019-08-13 stsp
3176 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3177 09867e48 2019-08-13 stsp if (tm == NULL)
3178 09867e48 2019-08-13 stsp return NULL;
3179 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3180 09867e48 2019-08-13 stsp if (s == NULL)
3181 09867e48 2019-08-13 stsp return NULL;
3182 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3183 abd2672a 2018-12-23 stsp if (p)
3184 abd2672a 2018-12-23 stsp *p = '\0';
3185 abd2672a 2018-12-23 stsp return s;
3186 9f7d7167 2018-04-29 stsp }
3187 9f7d7167 2018-04-29 stsp
3188 4ed7e80c 2018-05-20 stsp static const struct got_error *
3189 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3190 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3191 0208f208 2020-05-05 stsp {
3192 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3193 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3194 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3195 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3196 0208f208 2020-05-05 stsp
3197 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3198 0208f208 2020-05-05 stsp if (qid != NULL) {
3199 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3200 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3201 ec242592 2022-04-22 thomas &qid->id);
3202 0208f208 2020-05-05 stsp if (err)
3203 0208f208 2020-05-05 stsp return err;
3204 0208f208 2020-05-05 stsp
3205 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3206 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3207 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3208 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3209 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3210 aa8b5dd0 2021-08-01 stsp }
3211 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3212 0208f208 2020-05-05 stsp
3213 0208f208 2020-05-05 stsp }
3214 0208f208 2020-05-05 stsp
3215 0208f208 2020-05-05 stsp if (tree_id1) {
3216 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3217 0208f208 2020-05-05 stsp if (err)
3218 0208f208 2020-05-05 stsp goto done;
3219 0208f208 2020-05-05 stsp }
3220 0208f208 2020-05-05 stsp
3221 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3222 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3223 0208f208 2020-05-05 stsp if (err)
3224 0208f208 2020-05-05 stsp goto done;
3225 0208f208 2020-05-05 stsp
3226 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3227 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3228 0208f208 2020-05-05 stsp done:
3229 0208f208 2020-05-05 stsp if (tree1)
3230 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3231 0208f208 2020-05-05 stsp if (tree2)
3232 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3233 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3234 0208f208 2020-05-05 stsp return err;
3235 0208f208 2020-05-05 stsp }
3236 0208f208 2020-05-05 stsp
3237 0208f208 2020-05-05 stsp static const struct got_error *
3238 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3239 abd2672a 2018-12-23 stsp {
3240 fe621944 2020-11-10 stsp off_t *p;
3241 fe621944 2020-11-10 stsp
3242 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3243 fe621944 2020-11-10 stsp if (p == NULL)
3244 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3245 fe621944 2020-11-10 stsp *line_offsets = p;
3246 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3247 fe621944 2020-11-10 stsp (*nlines)++;
3248 fe621944 2020-11-10 stsp return NULL;
3249 fe621944 2020-11-10 stsp }
3250 fe621944 2020-11-10 stsp
3251 fe621944 2020-11-10 stsp static const struct got_error *
3252 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3253 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3254 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3255 fe621944 2020-11-10 stsp {
3256 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3257 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3258 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3259 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3260 45d799e2 2018-12-23 stsp time_t committer_time;
3261 45d799e2 2018-12-23 stsp const char *author, *committer;
3262 8b473291 2019-02-21 stsp char *refs_str = NULL;
3263 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3264 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3265 fe621944 2020-11-10 stsp off_t outoff = 0;
3266 fe621944 2020-11-10 stsp int n;
3267 abd2672a 2018-12-23 stsp
3268 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3269 0208f208 2020-05-05 stsp
3270 8b473291 2019-02-21 stsp if (refs) {
3271 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3272 8b473291 2019-02-21 stsp if (err)
3273 8b473291 2019-02-21 stsp return err;
3274 8b473291 2019-02-21 stsp }
3275 8b473291 2019-02-21 stsp
3276 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3277 abd2672a 2018-12-23 stsp if (err)
3278 abd2672a 2018-12-23 stsp return err;
3279 abd2672a 2018-12-23 stsp
3280 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3281 15a94983 2018-12-23 stsp if (err) {
3282 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3283 15a94983 2018-12-23 stsp goto done;
3284 15a94983 2018-12-23 stsp }
3285 abd2672a 2018-12-23 stsp
3286 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3287 fe621944 2020-11-10 stsp if (err)
3288 fe621944 2020-11-10 stsp goto done;
3289 fe621944 2020-11-10 stsp
3290 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3291 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3292 fe621944 2020-11-10 stsp if (n < 0) {
3293 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3294 abd2672a 2018-12-23 stsp goto done;
3295 abd2672a 2018-12-23 stsp }
3296 fe621944 2020-11-10 stsp outoff += n;
3297 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3298 fe621944 2020-11-10 stsp if (err)
3299 fe621944 2020-11-10 stsp goto done;
3300 fe621944 2020-11-10 stsp
3301 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3302 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3303 fe621944 2020-11-10 stsp if (n < 0) {
3304 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3305 abd2672a 2018-12-23 stsp goto done;
3306 abd2672a 2018-12-23 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 fe621944 2020-11-10 stsp
3312 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3313 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3314 fe621944 2020-11-10 stsp if (datestr) {
3315 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3316 fe621944 2020-11-10 stsp if (n < 0) {
3317 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3318 fe621944 2020-11-10 stsp goto done;
3319 fe621944 2020-11-10 stsp }
3320 fe621944 2020-11-10 stsp outoff += n;
3321 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3322 fe621944 2020-11-10 stsp if (err)
3323 fe621944 2020-11-10 stsp goto done;
3324 abd2672a 2018-12-23 stsp }
3325 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3326 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3327 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3328 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3329 fe621944 2020-11-10 stsp if (n < 0) {
3330 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3331 fe621944 2020-11-10 stsp goto done;
3332 fe621944 2020-11-10 stsp }
3333 fe621944 2020-11-10 stsp outoff += n;
3334 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3335 fe621944 2020-11-10 stsp if (err)
3336 fe621944 2020-11-10 stsp goto done;
3337 abd2672a 2018-12-23 stsp }
3338 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3339 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3340 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3341 ac4dc263 2021-09-24 thomas int pn = 1;
3342 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3343 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3344 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3345 ac4dc263 2021-09-24 thomas if (err)
3346 ac4dc263 2021-09-24 thomas goto done;
3347 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3348 ac4dc263 2021-09-24 thomas if (n < 0) {
3349 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3350 ac4dc263 2021-09-24 thomas goto done;
3351 ac4dc263 2021-09-24 thomas }
3352 ac4dc263 2021-09-24 thomas outoff += n;
3353 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3354 ac4dc263 2021-09-24 thomas if (err)
3355 ac4dc263 2021-09-24 thomas goto done;
3356 ac4dc263 2021-09-24 thomas free(id_str);
3357 ac4dc263 2021-09-24 thomas id_str = NULL;
3358 ac4dc263 2021-09-24 thomas }
3359 ac4dc263 2021-09-24 thomas }
3360 ac4dc263 2021-09-24 thomas
3361 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3362 5943eee2 2019-08-13 stsp if (err)
3363 5943eee2 2019-08-13 stsp goto done;
3364 fe621944 2020-11-10 stsp s = logmsg;
3365 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3366 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3367 fe621944 2020-11-10 stsp if (n < 0) {
3368 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3369 fe621944 2020-11-10 stsp goto done;
3370 fe621944 2020-11-10 stsp }
3371 fe621944 2020-11-10 stsp outoff += n;
3372 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3373 fe621944 2020-11-10 stsp if (err)
3374 fe621944 2020-11-10 stsp goto done;
3375 abd2672a 2018-12-23 stsp }
3376 fe621944 2020-11-10 stsp
3377 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3378 0208f208 2020-05-05 stsp if (err)
3379 0208f208 2020-05-05 stsp goto done;
3380 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3381 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3382 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3383 fe621944 2020-11-10 stsp if (n < 0) {
3384 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3385 fe621944 2020-11-10 stsp goto done;
3386 fe621944 2020-11-10 stsp }
3387 fe621944 2020-11-10 stsp outoff += n;
3388 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3389 fe621944 2020-11-10 stsp if (err)
3390 fe621944 2020-11-10 stsp goto done;
3391 0208f208 2020-05-05 stsp free((char *)pe->path);
3392 0208f208 2020-05-05 stsp free(pe->data);
3393 0208f208 2020-05-05 stsp }
3394 fe621944 2020-11-10 stsp
3395 0208f208 2020-05-05 stsp fputc('\n', outfile);
3396 fe621944 2020-11-10 stsp outoff++;
3397 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3398 abd2672a 2018-12-23 stsp done:
3399 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3400 abd2672a 2018-12-23 stsp free(id_str);
3401 5943eee2 2019-08-13 stsp free(logmsg);
3402 8b473291 2019-02-21 stsp free(refs_str);
3403 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3404 7510f233 2020-08-09 stsp if (err) {
3405 ae6a6978 2020-08-09 stsp free(*line_offsets);
3406 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3407 ae6a6978 2020-08-09 stsp *nlines = 0;
3408 7510f233 2020-08-09 stsp }
3409 fe621944 2020-11-10 stsp return err;
3410 abd2672a 2018-12-23 stsp }
3411 abd2672a 2018-12-23 stsp
3412 abd2672a 2018-12-23 stsp static const struct got_error *
3413 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3414 26ed57b2 2018-05-19 stsp {
3415 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3416 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3417 15a94983 2018-12-23 stsp int obj_type;
3418 fe621944 2020-11-10 stsp
3419 fe621944 2020-11-10 stsp free(s->line_offsets);
3420 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3421 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3422 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3423 fe621944 2020-11-10 stsp s->nlines = 0;
3424 26ed57b2 2018-05-19 stsp
3425 511a516b 2018-05-19 stsp f = got_opentemp();
3426 48ae06ee 2018-10-18 stsp if (f == NULL) {
3427 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3428 48ae06ee 2018-10-18 stsp goto done;
3429 48ae06ee 2018-10-18 stsp }
3430 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3431 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3432 fb43ecf1 2019-02-11 stsp goto done;
3433 fb43ecf1 2019-02-11 stsp }
3434 48ae06ee 2018-10-18 stsp s->f = f;
3435 26ed57b2 2018-05-19 stsp
3436 15a94983 2018-12-23 stsp if (s->id1)
3437 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3438 15a94983 2018-12-23 stsp else
3439 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3440 15a94983 2018-12-23 stsp if (err)
3441 15a94983 2018-12-23 stsp goto done;
3442 15a94983 2018-12-23 stsp
3443 15a94983 2018-12-23 stsp switch (obj_type) {
3444 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3445 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3446 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3447 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3448 a0f32f33 2022-06-13 thomas s->repo, s->f);
3449 26ed57b2 2018-05-19 stsp break;
3450 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3451 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3452 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3453 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3454 26ed57b2 2018-05-19 stsp break;
3455 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3456 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3457 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3458 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3459 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3460 abd2672a 2018-12-23 stsp
3461 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3462 abd2672a 2018-12-23 stsp if (err)
3463 3ffacbe1 2020-02-02 tracey goto done;
3464 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3465 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3466 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3467 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3468 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3469 f44b1f58 2020-02-02 tracey if (err)
3470 f44b1f58 2020-02-02 tracey goto done;
3471 f44b1f58 2020-02-02 tracey } else {
3472 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3473 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3474 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3475 fe621944 2020-11-10 stsp err = write_commit_info(
3476 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3477 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3478 f44b1f58 2020-02-02 tracey if (err)
3479 f44b1f58 2020-02-02 tracey goto done;
3480 f5404e4e 2020-02-02 tracey break;
3481 15a087fe 2019-02-21 stsp }
3482 abd2672a 2018-12-23 stsp }
3483 abd2672a 2018-12-23 stsp }
3484 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3485 abd2672a 2018-12-23 stsp
3486 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3487 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3488 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3489 26ed57b2 2018-05-19 stsp break;
3490 abd2672a 2018-12-23 stsp }
3491 26ed57b2 2018-05-19 stsp default:
3492 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3493 48ae06ee 2018-10-18 stsp break;
3494 26ed57b2 2018-05-19 stsp }
3495 f44b1f58 2020-02-02 tracey if (err)
3496 f44b1f58 2020-02-02 tracey goto done;
3497 48ae06ee 2018-10-18 stsp done:
3498 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3499 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3500 48ae06ee 2018-10-18 stsp return err;
3501 48ae06ee 2018-10-18 stsp }
3502 26ed57b2 2018-05-19 stsp
3503 f5215bb9 2019-02-22 stsp static void
3504 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3505 f5215bb9 2019-02-22 stsp {
3506 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3507 f5215bb9 2019-02-22 stsp update_panels();
3508 f5215bb9 2019-02-22 stsp doupdate();
3509 f44b1f58 2020-02-02 tracey }
3510 f44b1f58 2020-02-02 tracey
3511 f44b1f58 2020-02-02 tracey static const struct got_error *
3512 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3513 f44b1f58 2020-02-02 tracey {
3514 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3515 f44b1f58 2020-02-02 tracey
3516 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3517 f44b1f58 2020-02-02 tracey return NULL;
3518 f44b1f58 2020-02-02 tracey }
3519 f44b1f58 2020-02-02 tracey
3520 f44b1f58 2020-02-02 tracey static const struct got_error *
3521 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3522 f44b1f58 2020-02-02 tracey {
3523 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3524 f44b1f58 2020-02-02 tracey int lineno;
3525 826082fe 2020-12-10 stsp char *line = NULL;
3526 826082fe 2020-12-10 stsp size_t linesize = 0;
3527 826082fe 2020-12-10 stsp ssize_t linelen;
3528 f44b1f58 2020-02-02 tracey
3529 f44b1f58 2020-02-02 tracey if (!view->searching) {
3530 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3531 f44b1f58 2020-02-02 tracey return NULL;
3532 f44b1f58 2020-02-02 tracey }
3533 f44b1f58 2020-02-02 tracey
3534 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3535 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3536 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3537 f44b1f58 2020-02-02 tracey else
3538 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3539 4b3f9dac 2021-12-17 thomas } else
3540 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3541 f44b1f58 2020-02-02 tracey
3542 f44b1f58 2020-02-02 tracey while (1) {
3543 f44b1f58 2020-02-02 tracey off_t offset;
3544 f44b1f58 2020-02-02 tracey
3545 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3546 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3547 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3548 f44b1f58 2020-02-02 tracey break;
3549 f44b1f58 2020-02-02 tracey }
3550 f44b1f58 2020-02-02 tracey
3551 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3552 f44b1f58 2020-02-02 tracey lineno = 1;
3553 f44b1f58 2020-02-02 tracey else
3554 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3555 f44b1f58 2020-02-02 tracey }
3556 f44b1f58 2020-02-02 tracey
3557 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3558 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3559 f44b1f58 2020-02-02 tracey free(line);
3560 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3561 f44b1f58 2020-02-02 tracey }
3562 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3563 826082fe 2020-12-10 stsp if (linelen != -1 &&
3564 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3565 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3566 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3567 f44b1f58 2020-02-02 tracey break;
3568 f44b1f58 2020-02-02 tracey }
3569 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3570 f44b1f58 2020-02-02 tracey lineno++;
3571 f44b1f58 2020-02-02 tracey else
3572 f44b1f58 2020-02-02 tracey lineno--;
3573 f44b1f58 2020-02-02 tracey }
3574 826082fe 2020-12-10 stsp free(line);
3575 f44b1f58 2020-02-02 tracey
3576 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3577 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3578 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3579 f44b1f58 2020-02-02 tracey }
3580 f44b1f58 2020-02-02 tracey
3581 f44b1f58 2020-02-02 tracey return NULL;
3582 f5215bb9 2019-02-22 stsp }
3583 f5215bb9 2019-02-22 stsp
3584 48ae06ee 2018-10-18 stsp static const struct got_error *
3585 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3586 a0f32f33 2022-06-13 thomas {
3587 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3588 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3589 a0f32f33 2022-06-13 thomas
3590 a0f32f33 2022-06-13 thomas free(s->id1);
3591 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3592 a0f32f33 2022-06-13 thomas free(s->id2);
3593 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3594 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3595 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3596 a0f32f33 2022-06-13 thomas s->f = NULL;
3597 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3598 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3599 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3600 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3601 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3602 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3603 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3604 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3605 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3606 a0f32f33 2022-06-13 thomas s->nlines = 0;
3607 a0f32f33 2022-06-13 thomas return err;
3608 a0f32f33 2022-06-13 thomas }
3609 a0f32f33 2022-06-13 thomas
3610 a0f32f33 2022-06-13 thomas static const struct got_error *
3611 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3612 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3613 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3614 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3615 48ae06ee 2018-10-18 stsp {
3616 48ae06ee 2018-10-18 stsp const struct got_error *err;
3617 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3618 5dc9f4bc 2018-08-04 stsp
3619 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3620 a0f32f33 2022-06-13 thomas
3621 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3622 15a94983 2018-12-23 stsp int type1, type2;
3623 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3624 15a94983 2018-12-23 stsp if (err)
3625 15a94983 2018-12-23 stsp return err;
3626 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3627 15a94983 2018-12-23 stsp if (err)
3628 15a94983 2018-12-23 stsp return err;
3629 15a94983 2018-12-23 stsp
3630 15a94983 2018-12-23 stsp if (type1 != type2)
3631 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3632 15a94983 2018-12-23 stsp }
3633 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3634 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3635 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3636 f44b1f58 2020-02-02 tracey s->repo = repo;
3637 f44b1f58 2020-02-02 tracey s->id1 = id1;
3638 f44b1f58 2020-02-02 tracey s->id2 = id2;
3639 3dbaef42 2020-11-24 stsp s->label1 = label1;
3640 3dbaef42 2020-11-24 stsp s->label2 = label2;
3641 48ae06ee 2018-10-18 stsp
3642 15a94983 2018-12-23 stsp if (id1) {
3643 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3644 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3645 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3646 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3647 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3648 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3649 a0f32f33 2022-06-13 thomas goto done;
3650 a0f32f33 2022-06-13 thomas }
3651 48ae06ee 2018-10-18 stsp } else
3652 5465d566 2020-02-01 tracey s->id1 = NULL;
3653 48ae06ee 2018-10-18 stsp
3654 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3655 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3656 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3657 a0f32f33 2022-06-13 thomas goto done;
3658 48ae06ee 2018-10-18 stsp }
3659 a0f32f33 2022-06-13 thomas
3660 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3661 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3662 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3663 a0f32f33 2022-06-13 thomas goto done;
3664 a0f32f33 2022-06-13 thomas }
3665 a0f32f33 2022-06-13 thomas
3666 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3667 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3668 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3669 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3670 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3671 5465d566 2020-02-01 tracey s->log_view = log_view;
3672 5465d566 2020-02-01 tracey s->repo = repo;
3673 6d17833f 2019-11-08 stsp
3674 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3675 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3676 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3677 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3678 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3679 6d17833f 2019-11-08 stsp if (err)
3680 a0f32f33 2022-06-13 thomas goto done;
3681 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3682 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3683 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3684 a0f32f33 2022-06-13 thomas if (err)
3685 a0f32f33 2022-06-13 thomas goto done;
3686 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3687 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3688 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3689 a0f32f33 2022-06-13 thomas if (err)
3690 a0f32f33 2022-06-13 thomas goto done;
3691 6d17833f 2019-11-08 stsp
3692 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3693 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3694 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3695 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3696 a0f32f33 2022-06-13 thomas if (err)
3697 a0f32f33 2022-06-13 thomas goto done;
3698 11b20872 2019-11-08 stsp
3699 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3700 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3701 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3702 a0f32f33 2022-06-13 thomas if (err)
3703 a0f32f33 2022-06-13 thomas goto done;
3704 11b20872 2019-11-08 stsp
3705 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3706 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3707 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3708 a0f32f33 2022-06-13 thomas if (err)
3709 a0f32f33 2022-06-13 thomas goto done;
3710 6d17833f 2019-11-08 stsp }
3711 5dc9f4bc 2018-08-04 stsp
3712 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3713 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3714 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3715 f5215bb9 2019-02-22 stsp
3716 5465d566 2020-02-01 tracey err = create_diff(s);
3717 48ae06ee 2018-10-18 stsp
3718 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3719 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3720 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3721 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3722 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3723 a0f32f33 2022-06-13 thomas done:
3724 a0f32f33 2022-06-13 thomas if (err)
3725 a0f32f33 2022-06-13 thomas close_diff_view(view);
3726 e5a0f69f 2018-08-18 stsp return err;
3727 5dc9f4bc 2018-08-04 stsp }
3728 5dc9f4bc 2018-08-04 stsp
3729 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3730 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3731 5dc9f4bc 2018-08-04 stsp {
3732 a3404814 2018-09-02 stsp const struct got_error *err;
3733 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3734 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3735 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3736 a3404814 2018-09-02 stsp
3737 a3404814 2018-09-02 stsp if (s->id1) {
3738 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3739 a3404814 2018-09-02 stsp if (err)
3740 a3404814 2018-09-02 stsp return err;
3741 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3742 3dbaef42 2020-11-24 stsp } else
3743 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3744 3dbaef42 2020-11-24 stsp
3745 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3746 a3404814 2018-09-02 stsp if (err)
3747 a3404814 2018-09-02 stsp return err;
3748 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3749 26ed57b2 2018-05-19 stsp
3750 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3751 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3752 a3404814 2018-09-02 stsp free(id_str1);
3753 a3404814 2018-09-02 stsp free(id_str2);
3754 a3404814 2018-09-02 stsp return err;
3755 a3404814 2018-09-02 stsp }
3756 a3404814 2018-09-02 stsp free(id_str1);
3757 a3404814 2018-09-02 stsp free(id_str2);
3758 a3404814 2018-09-02 stsp
3759 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3760 267bb3b8 2021-08-01 stsp free(header);
3761 267bb3b8 2021-08-01 stsp return err;
3762 15a087fe 2019-02-21 stsp }
3763 15a087fe 2019-02-21 stsp
3764 15a087fe 2019-02-21 stsp static const struct got_error *
3765 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3766 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3767 15a087fe 2019-02-21 stsp {
3768 d7a04538 2019-02-21 stsp const struct got_error *err;
3769 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3770 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3771 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3772 15a087fe 2019-02-21 stsp
3773 15a087fe 2019-02-21 stsp free(s->id2);
3774 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3775 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3776 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3777 15a087fe 2019-02-21 stsp
3778 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3779 d7a04538 2019-02-21 stsp if (err)
3780 d7a04538 2019-02-21 stsp return err;
3781 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3782 15a087fe 2019-02-21 stsp free(s->id1);
3783 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3784 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3785 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3786 15a087fe 2019-02-21 stsp return NULL;
3787 0cf4efb1 2018-09-29 stsp }
3788 0cf4efb1 2018-09-29 stsp
3789 0cf4efb1 2018-09-29 stsp static const struct got_error *
3790 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3791 e5a0f69f 2018-08-18 stsp {
3792 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3793 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3794 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3795 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3796 826082fe 2020-12-10 stsp char *line = NULL;
3797 826082fe 2020-12-10 stsp size_t linesize = 0;
3798 826082fe 2020-12-10 stsp ssize_t linelen;
3799 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3800 e5a0f69f 2018-08-18 stsp
3801 e5a0f69f 2018-08-18 stsp switch (ch) {
3802 64453f7e 2020-11-21 stsp case 'a':
3803 3dbaef42 2020-11-24 stsp case 'w':
3804 3dbaef42 2020-11-24 stsp if (ch == 'a')
3805 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3806 3dbaef42 2020-11-24 stsp if (ch == 'w')
3807 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3808 64453f7e 2020-11-21 stsp wclear(view->window);
3809 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3810 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3811 aa61903a 2021-12-31 thomas s->matched_line = 0;
3812 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3813 64453f7e 2020-11-21 stsp err = create_diff(s);
3814 912a3f79 2021-08-30 j break;
3815 912a3f79 2021-08-30 j case 'g':
3816 912a3f79 2021-08-30 j case KEY_HOME:
3817 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3818 64453f7e 2020-11-21 stsp break;
3819 912a3f79 2021-08-30 j case 'G':
3820 912a3f79 2021-08-30 j case KEY_END:
3821 912a3f79 2021-08-30 j if (s->eof)
3822 912a3f79 2021-08-30 j break;
3823 912a3f79 2021-08-30 j
3824 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3825 912a3f79 2021-08-30 j s->eof = 1;
3826 912a3f79 2021-08-30 j break;
3827 1e37a5c2 2019-05-12 jcs case 'k':
3828 1e37a5c2 2019-05-12 jcs case KEY_UP:
3829 f7140bf5 2021-10-17 thomas case CTRL('p'):
3830 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3831 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3832 1e37a5c2 2019-05-12 jcs break;
3833 70f17a53 2022-06-13 thomas case CTRL('u'):
3834 23427b14 2022-06-23 thomas case 'u':
3835 70f17a53 2022-06-13 thomas nscroll /= 2;
3836 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3837 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3838 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3839 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3840 26ed57b2 2018-05-19 stsp break;
3841 1e37a5c2 2019-05-12 jcs i = 0;
3842 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
3843 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3844 1e37a5c2 2019-05-12 jcs break;
3845 1e37a5c2 2019-05-12 jcs case 'j':
3846 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3847 f7140bf5 2021-10-17 thomas case CTRL('n'):
3848 1e37a5c2 2019-05-12 jcs if (!s->eof)
3849 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3850 1e37a5c2 2019-05-12 jcs break;
3851 70f17a53 2022-06-13 thomas case CTRL('d'):
3852 23427b14 2022-06-23 thomas case 'd':
3853 70f17a53 2022-06-13 thomas nscroll /= 2;
3854 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3855 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3856 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3857 1e37a5c2 2019-05-12 jcs case ' ':
3858 00ba99a7 2019-05-12 jcs if (s->eof)
3859 1e37a5c2 2019-05-12 jcs break;
3860 1e37a5c2 2019-05-12 jcs i = 0;
3861 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
3862 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3863 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3864 826082fe 2020-12-10 stsp if (linelen == -1) {
3865 826082fe 2020-12-10 stsp if (feof(s->f)) {
3866 826082fe 2020-12-10 stsp s->eof = 1;
3867 826082fe 2020-12-10 stsp } else
3868 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3869 34bc9ec9 2019-02-22 stsp break;
3870 826082fe 2020-12-10 stsp }
3871 1e37a5c2 2019-05-12 jcs }
3872 826082fe 2020-12-10 stsp free(line);
3873 1e37a5c2 2019-05-12 jcs break;
3874 1e37a5c2 2019-05-12 jcs case '[':
3875 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3876 1e37a5c2 2019-05-12 jcs s->diff_context--;
3877 aa61903a 2021-12-31 thomas s->matched_line = 0;
3878 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3879 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3880 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3881 27829c9e 2020-11-21 stsp s->nlines) {
3882 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3883 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3884 27829c9e 2020-11-21 stsp }
3885 1e37a5c2 2019-05-12 jcs }
3886 1e37a5c2 2019-05-12 jcs break;
3887 1e37a5c2 2019-05-12 jcs case ']':
3888 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3889 1e37a5c2 2019-05-12 jcs s->diff_context++;
3890 aa61903a 2021-12-31 thomas s->matched_line = 0;
3891 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3892 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3893 1e37a5c2 2019-05-12 jcs }
3894 1e37a5c2 2019-05-12 jcs break;
3895 1e37a5c2 2019-05-12 jcs case '<':
3896 1e37a5c2 2019-05-12 jcs case ',':
3897 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3898 48ae06ee 2018-10-18 stsp break;
3899 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3900 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3901 6524637e 2019-02-21 stsp
3902 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3903 1e37a5c2 2019-05-12 jcs if (err)
3904 1e37a5c2 2019-05-12 jcs break;
3905 15a087fe 2019-02-21 stsp
3906 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3907 5a8b5076 2020-12-05 stsp break;
3908 5a8b5076 2020-12-05 stsp
3909 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3910 1e37a5c2 2019-05-12 jcs if (err)
3911 1e37a5c2 2019-05-12 jcs break;
3912 15a087fe 2019-02-21 stsp
3913 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3914 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3915 aa61903a 2021-12-31 thomas s->matched_line = 0;
3916 15a087fe 2019-02-21 stsp
3917 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3918 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3919 1e37a5c2 2019-05-12 jcs break;
3920 1e37a5c2 2019-05-12 jcs case '>':
3921 1e37a5c2 2019-05-12 jcs case '.':
3922 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3923 15a087fe 2019-02-21 stsp break;
3924 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3925 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3926 5e224a3e 2019-02-22 stsp
3927 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3928 1e37a5c2 2019-05-12 jcs if (err)
3929 5a8b5076 2020-12-05 stsp break;
3930 5a8b5076 2020-12-05 stsp
3931 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3932 1e37a5c2 2019-05-12 jcs break;
3933 15a087fe 2019-02-21 stsp
3934 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3935 1e37a5c2 2019-05-12 jcs if (err)
3936 1e37a5c2 2019-05-12 jcs break;
3937 15a087fe 2019-02-21 stsp
3938 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3939 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3940 aa61903a 2021-12-31 thomas s->matched_line = 0;
3941 1e37a5c2 2019-05-12 jcs
3942 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3943 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3944 1e37a5c2 2019-05-12 jcs break;
3945 1e37a5c2 2019-05-12 jcs default:
3946 1e37a5c2 2019-05-12 jcs break;
3947 26ed57b2 2018-05-19 stsp }
3948 e5a0f69f 2018-08-18 stsp
3949 bcbd79e2 2018-08-19 stsp return err;
3950 26ed57b2 2018-05-19 stsp }
3951 26ed57b2 2018-05-19 stsp
3952 4ed7e80c 2018-05-20 stsp static const struct got_error *
3953 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3954 9f7d7167 2018-04-29 stsp {
3955 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3956 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3957 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3958 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3959 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3960 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3961 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3962 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3963 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3964 3dbaef42 2020-11-24 stsp const char *errstr;
3965 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3966 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3967 70ac5f84 2019-03-28 stsp
3968 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3969 26ed57b2 2018-05-19 stsp switch (ch) {
3970 64453f7e 2020-11-21 stsp case 'a':
3971 64453f7e 2020-11-21 stsp force_text_diff = 1;
3972 3dbaef42 2020-11-24 stsp break;
3973 3dbaef42 2020-11-24 stsp case 'C':
3974 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3975 3dbaef42 2020-11-24 stsp &errstr);
3976 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3977 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3978 8f666e67 2022-02-12 thomas errstr, errstr);
3979 64453f7e 2020-11-21 stsp break;
3980 09b5bff8 2020-02-23 naddy case 'r':
3981 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3982 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3983 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3984 09b5bff8 2020-02-23 naddy optarg);
3985 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3986 09b5bff8 2020-02-23 naddy break;
3987 3dbaef42 2020-11-24 stsp case 'w':
3988 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3989 3dbaef42 2020-11-24 stsp break;
3990 26ed57b2 2018-05-19 stsp default:
3991 17020d27 2019-03-07 stsp usage_diff();
3992 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3993 26ed57b2 2018-05-19 stsp }
3994 26ed57b2 2018-05-19 stsp }
3995 26ed57b2 2018-05-19 stsp
3996 26ed57b2 2018-05-19 stsp argc -= optind;
3997 26ed57b2 2018-05-19 stsp argv += optind;
3998 26ed57b2 2018-05-19 stsp
3999 26ed57b2 2018-05-19 stsp if (argc == 0) {
4000 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4001 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4002 15a94983 2018-12-23 stsp id_str1 = argv[0];
4003 15a94983 2018-12-23 stsp id_str2 = argv[1];
4004 26ed57b2 2018-05-19 stsp } else
4005 26ed57b2 2018-05-19 stsp usage_diff();
4006 eb6600df 2019-01-04 stsp
4007 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4008 7cd52833 2022-06-23 thomas if (error)
4009 7cd52833 2022-06-23 thomas goto done;
4010 7cd52833 2022-06-23 thomas
4011 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4012 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4013 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4014 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4015 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4016 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4017 c156c7a4 2020-12-18 stsp goto done;
4018 a273ac94 2020-02-23 naddy if (worktree)
4019 a273ac94 2020-02-23 naddy repo_path =
4020 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4021 a273ac94 2020-02-23 naddy else
4022 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4023 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4024 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4025 c156c7a4 2020-12-18 stsp goto done;
4026 c156c7a4 2020-12-18 stsp }
4027 a273ac94 2020-02-23 naddy }
4028 a273ac94 2020-02-23 naddy
4029 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4030 eb6600df 2019-01-04 stsp if (error)
4031 eb6600df 2019-01-04 stsp goto done;
4032 26ed57b2 2018-05-19 stsp
4033 a273ac94 2020-02-23 naddy init_curses();
4034 a273ac94 2020-02-23 naddy
4035 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4036 51a10b52 2020-12-26 stsp if (error)
4037 51a10b52 2020-12-26 stsp goto done;
4038 51a10b52 2020-12-26 stsp
4039 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4040 26ed57b2 2018-05-19 stsp if (error)
4041 26ed57b2 2018-05-19 stsp goto done;
4042 26ed57b2 2018-05-19 stsp
4043 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4044 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4045 26ed57b2 2018-05-19 stsp if (error)
4046 26ed57b2 2018-05-19 stsp goto done;
4047 26ed57b2 2018-05-19 stsp
4048 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4049 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4050 26ed57b2 2018-05-19 stsp if (error)
4051 26ed57b2 2018-05-19 stsp goto done;
4052 26ed57b2 2018-05-19 stsp
4053 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4054 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4055 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4056 ea5e7bb5 2018-08-01 stsp goto done;
4057 ea5e7bb5 2018-08-01 stsp }
4058 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4059 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4060 5dc9f4bc 2018-08-04 stsp if (error)
4061 5dc9f4bc 2018-08-04 stsp goto done;
4062 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4063 26ed57b2 2018-05-19 stsp done:
4064 3dbaef42 2020-11-24 stsp free(label1);
4065 3dbaef42 2020-11-24 stsp free(label2);
4066 c02c541e 2019-03-29 stsp free(repo_path);
4067 a273ac94 2020-02-23 naddy free(cwd);
4068 1d0f4054 2021-06-17 stsp if (repo) {
4069 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4070 1d0f4054 2021-06-17 stsp if (error == NULL)
4071 1d0f4054 2021-06-17 stsp error = close_err;
4072 1d0f4054 2021-06-17 stsp }
4073 a273ac94 2020-02-23 naddy if (worktree)
4074 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4075 7cd52833 2022-06-23 thomas if (pack_fds) {
4076 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4077 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4078 7cd52833 2022-06-23 thomas if (error == NULL)
4079 7cd52833 2022-06-23 thomas error = pack_err;
4080 7cd52833 2022-06-23 thomas }
4081 51a10b52 2020-12-26 stsp tog_free_refs();
4082 26ed57b2 2018-05-19 stsp return error;
4083 9f7d7167 2018-04-29 stsp }
4084 9f7d7167 2018-04-29 stsp
4085 4ed7e80c 2018-05-20 stsp __dead static void
4086 9f7d7167 2018-04-29 stsp usage_blame(void)
4087 9f7d7167 2018-04-29 stsp {
4088 80ddbec8 2018-04-29 stsp endwin();
4089 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4090 9f7d7167 2018-04-29 stsp getprogname());
4091 9f7d7167 2018-04-29 stsp exit(1);
4092 9f7d7167 2018-04-29 stsp }
4093 84451b3e 2018-07-10 stsp
4094 84451b3e 2018-07-10 stsp struct tog_blame_line {
4095 84451b3e 2018-07-10 stsp int annotated;
4096 84451b3e 2018-07-10 stsp struct got_object_id *id;
4097 84451b3e 2018-07-10 stsp };
4098 9f7d7167 2018-04-29 stsp
4099 4ed7e80c 2018-05-20 stsp static const struct got_error *
4100 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4101 84451b3e 2018-07-10 stsp {
4102 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4103 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4104 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4105 84451b3e 2018-07-10 stsp const struct got_error *err;
4106 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4107 826082fe 2020-12-10 stsp char *line = NULL;
4108 826082fe 2020-12-10 stsp size_t linesize = 0;
4109 826082fe 2020-12-10 stsp ssize_t linelen;
4110 84451b3e 2018-07-10 stsp wchar_t *wline;
4111 27a741e5 2019-09-11 stsp int width;
4112 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4113 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4114 ab089a2a 2018-07-12 stsp char *id_str;
4115 11b20872 2019-11-08 stsp struct tog_color *tc;
4116 ab089a2a 2018-07-12 stsp
4117 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4118 ab089a2a 2018-07-12 stsp if (err)
4119 ab089a2a 2018-07-12 stsp return err;
4120 84451b3e 2018-07-10 stsp
4121 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4122 f7d12f7e 2018-08-01 stsp werase(view->window);
4123 84451b3e 2018-07-10 stsp
4124 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4125 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4126 ab089a2a 2018-07-12 stsp free(id_str);
4127 ab089a2a 2018-07-12 stsp return err;
4128 ab089a2a 2018-07-12 stsp }
4129 ab089a2a 2018-07-12 stsp
4130 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4131 ab089a2a 2018-07-12 stsp free(line);
4132 2550e4c3 2018-07-13 stsp line = NULL;
4133 1cae65b4 2019-09-22 stsp if (err)
4134 1cae65b4 2019-09-22 stsp return err;
4135 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4136 a3404814 2018-09-02 stsp wstandout(view->window);
4137 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4138 11b20872 2019-11-08 stsp if (tc)
4139 11b20872 2019-11-08 stsp wattr_on(view->window,
4140 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4141 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4142 11b20872 2019-11-08 stsp if (tc)
4143 11b20872 2019-11-08 stsp wattr_off(view->window,
4144 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4145 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4146 a3404814 2018-09-02 stsp wstandend(view->window);
4147 2550e4c3 2018-07-13 stsp free(wline);
4148 2550e4c3 2018-07-13 stsp wline = NULL;
4149 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4150 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4151 ab089a2a 2018-07-12 stsp
4152 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4153 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4154 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4155 ab089a2a 2018-07-12 stsp free(id_str);
4156 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4157 ab089a2a 2018-07-12 stsp }
4158 ab089a2a 2018-07-12 stsp free(id_str);
4159 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4160 3f60a8ef 2018-07-10 stsp free(line);
4161 2550e4c3 2018-07-13 stsp line = NULL;
4162 3f60a8ef 2018-07-10 stsp if (err)
4163 3f60a8ef 2018-07-10 stsp return err;
4164 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4165 2550e4c3 2018-07-13 stsp free(wline);
4166 2550e4c3 2018-07-13 stsp wline = NULL;
4167 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4168 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4169 3f60a8ef 2018-07-10 stsp
4170 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4171 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4172 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4173 826082fe 2020-12-10 stsp if (linelen == -1) {
4174 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4175 826082fe 2020-12-10 stsp s->eof = 1;
4176 826082fe 2020-12-10 stsp break;
4177 826082fe 2020-12-10 stsp }
4178 84451b3e 2018-07-10 stsp free(line);
4179 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4180 84451b3e 2018-07-10 stsp }
4181 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4182 826082fe 2020-12-10 stsp continue;
4183 84451b3e 2018-07-10 stsp
4184 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4185 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4186 b700b5d6 2018-07-10 stsp
4187 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4188 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4189 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4190 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4191 27a741e5 2019-09-11 stsp !(view->focussed &&
4192 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4193 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4194 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4195 8d0fe45a 2019-08-12 stsp char *id_str;
4196 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4197 8d0fe45a 2019-08-12 stsp if (err) {
4198 8d0fe45a 2019-08-12 stsp free(line);
4199 8d0fe45a 2019-08-12 stsp return err;
4200 8d0fe45a 2019-08-12 stsp }
4201 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4202 11b20872 2019-11-08 stsp if (tc)
4203 11b20872 2019-11-08 stsp wattr_on(view->window,
4204 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4205 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4206 11b20872 2019-11-08 stsp if (tc)
4207 11b20872 2019-11-08 stsp wattr_off(view->window,
4208 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4209 8d0fe45a 2019-08-12 stsp free(id_str);
4210 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4211 8d0fe45a 2019-08-12 stsp } else {
4212 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4213 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4214 84451b3e 2018-07-10 stsp }
4215 ee41ec32 2018-07-10 stsp } else {
4216 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4217 ee41ec32 2018-07-10 stsp prev_id = NULL;
4218 ee41ec32 2018-07-10 stsp }
4219 84451b3e 2018-07-10 stsp
4220 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4221 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4222 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4223 27a741e5 2019-09-11 stsp
4224 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4225 41605754 2020-11-12 stsp width = 9;
4226 41605754 2020-11-12 stsp wline = wcsdup(L"");
4227 41605754 2020-11-12 stsp if (wline == NULL) {
4228 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4229 41605754 2020-11-12 stsp free(line);
4230 41605754 2020-11-12 stsp return err;
4231 41605754 2020-11-12 stsp }
4232 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4233 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4234 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4235 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4236 41605754 2020-11-12 stsp view->window, regmatch);
4237 41605754 2020-11-12 stsp if (err) {
4238 41605754 2020-11-12 stsp free(line);
4239 41605754 2020-11-12 stsp return err;
4240 41605754 2020-11-12 stsp }
4241 41605754 2020-11-12 stsp width += 9;
4242 41605754 2020-11-12 stsp } else {
4243 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4244 41605754 2020-11-12 stsp view->ncols - 9, 9);
4245 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4246 41605754 2020-11-12 stsp free(wline);
4247 41605754 2020-11-12 stsp wline = NULL;
4248 41605754 2020-11-12 stsp width += 9;
4249 41605754 2020-11-12 stsp }
4250 41605754 2020-11-12 stsp
4251 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4252 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4253 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4254 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4255 84451b3e 2018-07-10 stsp }
4256 826082fe 2020-12-10 stsp free(line);
4257 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4258 84451b3e 2018-07-10 stsp
4259 1a57306a 2018-09-02 stsp view_vborder(view);
4260 84451b3e 2018-07-10 stsp
4261 84451b3e 2018-07-10 stsp return NULL;
4262 84451b3e 2018-07-10 stsp }
4263 84451b3e 2018-07-10 stsp
4264 84451b3e 2018-07-10 stsp static const struct got_error *
4265 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4266 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4267 84451b3e 2018-07-10 stsp {
4268 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4269 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4270 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4271 1a76625f 2018-10-22 stsp int errcode;
4272 84451b3e 2018-07-10 stsp
4273 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4274 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4275 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4276 84451b3e 2018-07-10 stsp
4277 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4278 1a76625f 2018-10-22 stsp if (errcode)
4279 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4280 84451b3e 2018-07-10 stsp
4281 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4282 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4283 d68a0a7d 2018-07-10 stsp goto done;
4284 d68a0a7d 2018-07-10 stsp }
4285 d68a0a7d 2018-07-10 stsp
4286 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4287 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4288 d68a0a7d 2018-07-10 stsp
4289 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4290 d68a0a7d 2018-07-10 stsp if (line->annotated)
4291 d68a0a7d 2018-07-10 stsp goto done;
4292 d68a0a7d 2018-07-10 stsp
4293 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4294 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4295 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4296 84451b3e 2018-07-10 stsp goto done;
4297 84451b3e 2018-07-10 stsp }
4298 84451b3e 2018-07-10 stsp line->annotated = 1;
4299 84451b3e 2018-07-10 stsp done:
4300 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4301 1a76625f 2018-10-22 stsp if (errcode)
4302 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4303 84451b3e 2018-07-10 stsp return err;
4304 84451b3e 2018-07-10 stsp }
4305 84451b3e 2018-07-10 stsp
4306 84451b3e 2018-07-10 stsp static void *
4307 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4308 84451b3e 2018-07-10 stsp {
4309 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4310 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4311 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4312 1a76625f 2018-10-22 stsp int errcode;
4313 18430de3 2018-07-10 stsp
4314 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4315 61266923 2020-01-14 stsp if (err)
4316 61266923 2020-01-14 stsp return (void *)err;
4317 61266923 2020-01-14 stsp
4318 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4319 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4320 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4321 fc06ba56 2019-08-22 stsp err = NULL;
4322 18430de3 2018-07-10 stsp
4323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4324 1a76625f 2018-10-22 stsp if (errcode)
4325 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4326 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4327 18430de3 2018-07-10 stsp
4328 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4329 1d0f4054 2021-06-17 stsp if (err == NULL)
4330 1d0f4054 2021-06-17 stsp err = close_err;
4331 c9beca56 2018-07-22 stsp ta->repo = NULL;
4332 c9beca56 2018-07-22 stsp *ta->complete = 1;
4333 18430de3 2018-07-10 stsp
4334 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4335 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4336 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4337 18430de3 2018-07-10 stsp
4338 18430de3 2018-07-10 stsp return (void *)err;
4339 84451b3e 2018-07-10 stsp }
4340 84451b3e 2018-07-10 stsp
4341 245d91c1 2018-07-12 stsp static struct got_object_id *
4342 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4343 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4344 245d91c1 2018-07-12 stsp {
4345 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4346 8d0fe45a 2019-08-12 stsp
4347 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4348 8d0fe45a 2019-08-12 stsp return NULL;
4349 b880a918 2018-07-10 stsp
4350 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4351 245d91c1 2018-07-12 stsp if (!line->annotated)
4352 245d91c1 2018-07-12 stsp return NULL;
4353 245d91c1 2018-07-12 stsp
4354 245d91c1 2018-07-12 stsp return line->id;
4355 b880a918 2018-07-10 stsp }
4356 245d91c1 2018-07-12 stsp
4357 b880a918 2018-07-10 stsp static const struct got_error *
4358 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4359 a70480e0 2018-06-23 stsp {
4360 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4361 245d91c1 2018-07-12 stsp int i;
4362 245d91c1 2018-07-12 stsp
4363 245d91c1 2018-07-12 stsp if (blame->thread) {
4364 1a76625f 2018-10-22 stsp int errcode;
4365 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4366 1a76625f 2018-10-22 stsp if (errcode)
4367 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4368 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4369 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4370 1a76625f 2018-10-22 stsp if (errcode)
4371 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4372 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4373 1a76625f 2018-10-22 stsp if (errcode)
4374 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4375 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4376 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4377 245d91c1 2018-07-12 stsp err = NULL;
4378 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4379 245d91c1 2018-07-12 stsp }
4380 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4381 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4382 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4383 1d0f4054 2021-06-17 stsp if (err == NULL)
4384 1d0f4054 2021-06-17 stsp err = close_err;
4385 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4386 245d91c1 2018-07-12 stsp }
4387 245d91c1 2018-07-12 stsp if (blame->f) {
4388 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4389 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4390 245d91c1 2018-07-12 stsp blame->f = NULL;
4391 245d91c1 2018-07-12 stsp }
4392 57670559 2018-12-23 stsp if (blame->lines) {
4393 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4394 57670559 2018-12-23 stsp free(blame->lines[i].id);
4395 57670559 2018-12-23 stsp free(blame->lines);
4396 57670559 2018-12-23 stsp blame->lines = NULL;
4397 57670559 2018-12-23 stsp }
4398 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4399 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4400 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
4401 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4402 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
4403 7cd52833 2022-06-23 thomas if (err == NULL)
4404 7cd52833 2022-06-23 thomas err = pack_err;
4405 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
4406 7cd52833 2022-06-23 thomas }
4407 245d91c1 2018-07-12 stsp return err;
4408 245d91c1 2018-07-12 stsp }
4409 245d91c1 2018-07-12 stsp
4410 245d91c1 2018-07-12 stsp static const struct got_error *
4411 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4412 fc06ba56 2019-08-22 stsp {
4413 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4414 fc06ba56 2019-08-22 stsp int *done = arg;
4415 fc06ba56 2019-08-22 stsp int errcode;
4416 fc06ba56 2019-08-22 stsp
4417 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4418 fc06ba56 2019-08-22 stsp if (errcode)
4419 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4420 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4421 fc06ba56 2019-08-22 stsp
4422 fc06ba56 2019-08-22 stsp if (*done)
4423 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4424 fc06ba56 2019-08-22 stsp
4425 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4426 fc06ba56 2019-08-22 stsp if (errcode)
4427 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4428 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4429 fc06ba56 2019-08-22 stsp
4430 fc06ba56 2019-08-22 stsp return err;
4431 fc06ba56 2019-08-22 stsp }
4432 fc06ba56 2019-08-22 stsp
4433 fc06ba56 2019-08-22 stsp static const struct got_error *
4434 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4435 245d91c1 2018-07-12 stsp {
4436 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4437 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4438 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4439 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4440 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4441 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4442 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4443 15a94983 2018-12-23 stsp int obj_type;
4444 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4445 a70480e0 2018-06-23 stsp
4446 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4447 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4448 27d434c2 2018-09-15 stsp if (err)
4449 15a94983 2018-12-23 stsp return err;
4450 945f9229 2022-04-16 thomas
4451 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4452 945f9229 2022-04-16 thomas if (err)
4453 945f9229 2022-04-16 thomas goto done;
4454 27d434c2 2018-09-15 stsp
4455 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4456 84451b3e 2018-07-10 stsp if (err)
4457 84451b3e 2018-07-10 stsp goto done;
4458 27d434c2 2018-09-15 stsp
4459 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4460 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4461 84451b3e 2018-07-10 stsp goto done;
4462 84451b3e 2018-07-10 stsp }
4463 a70480e0 2018-06-23 stsp
4464 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4465 a70480e0 2018-06-23 stsp if (err)
4466 a70480e0 2018-06-23 stsp goto done;
4467 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4468 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4469 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4470 84451b3e 2018-07-10 stsp goto done;
4471 84451b3e 2018-07-10 stsp }
4472 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4473 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4474 1fddf795 2021-01-20 stsp if (err)
4475 1fddf795 2021-01-20 stsp goto done;
4476 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4477 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4478 84451b3e 2018-07-10 stsp goto done;
4479 1fddf795 2021-01-20 stsp }
4480 b02560ec 2019-08-19 stsp
4481 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4482 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4483 b02560ec 2019-08-19 stsp blame->nlines--;
4484 a70480e0 2018-06-23 stsp
4485 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4486 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4487 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4488 84451b3e 2018-07-10 stsp goto done;
4489 84451b3e 2018-07-10 stsp }
4490 a70480e0 2018-06-23 stsp
4491 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
4492 bd24772e 2018-07-11 stsp if (err)
4493 bd24772e 2018-07-11 stsp goto done;
4494 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4495 7cd52833 2022-06-23 thomas pack_fds);
4496 7cd52833 2022-06-23 thomas if (err)
4497 7cd52833 2022-06-23 thomas goto done;
4498 bd24772e 2018-07-11 stsp
4499 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
4500 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4501 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4502 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4503 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4504 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4505 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4506 245d91c1 2018-07-12 stsp goto done;
4507 245d91c1 2018-07-12 stsp }
4508 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4509 245d91c1 2018-07-12 stsp
4510 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4511 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4512 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4513 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4514 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4515 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4516 a5388363 2020-12-01 naddy s->blame_complete = 0;
4517 f5a09613 2020-12-13 naddy
4518 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4519 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4520 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4521 f5a09613 2020-12-13 naddy s->selected_line = 1;
4522 f5a09613 2020-12-13 naddy }
4523 aa61903a 2021-12-31 thomas s->matched_line = 0;
4524 245d91c1 2018-07-12 stsp
4525 245d91c1 2018-07-12 stsp done:
4526 945f9229 2022-04-16 thomas if (commit)
4527 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4528 245d91c1 2018-07-12 stsp if (blob)
4529 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4530 27d434c2 2018-09-15 stsp free(obj_id);
4531 245d91c1 2018-07-12 stsp if (err)
4532 245d91c1 2018-07-12 stsp stop_blame(blame);
4533 245d91c1 2018-07-12 stsp return err;
4534 245d91c1 2018-07-12 stsp }
4535 245d91c1 2018-07-12 stsp
4536 245d91c1 2018-07-12 stsp static const struct got_error *
4537 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4538 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4539 245d91c1 2018-07-12 stsp {
4540 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4541 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4542 dbc6a6b6 2018-07-12 stsp
4543 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4544 245d91c1 2018-07-12 stsp
4545 c4843652 2019-08-12 stsp s->path = strdup(path);
4546 c4843652 2019-08-12 stsp if (s->path == NULL)
4547 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4548 c4843652 2019-08-12 stsp
4549 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4550 c4843652 2019-08-12 stsp if (err) {
4551 c4843652 2019-08-12 stsp free(s->path);
4552 7cbe629d 2018-08-04 stsp return err;
4553 c4843652 2019-08-12 stsp }
4554 245d91c1 2018-07-12 stsp
4555 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4556 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4557 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4558 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4559 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4560 fb2756b9 2018-08-04 stsp s->repo = repo;
4561 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4562 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4563 7cbe629d 2018-08-04 stsp
4564 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4565 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4566 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4567 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4568 11b20872 2019-11-08 stsp if (err)
4569 11b20872 2019-11-08 stsp return err;
4570 11b20872 2019-11-08 stsp }
4571 11b20872 2019-11-08 stsp
4572 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4573 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4574 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4575 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4576 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4577 e5a0f69f 2018-08-18 stsp
4578 a5388363 2020-12-01 naddy return run_blame(view);
4579 7cbe629d 2018-08-04 stsp }
4580 7cbe629d 2018-08-04 stsp
4581 e5a0f69f 2018-08-18 stsp static const struct got_error *
4582 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4583 7cbe629d 2018-08-04 stsp {
4584 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4585 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4586 7cbe629d 2018-08-04 stsp
4587 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4588 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4589 e5a0f69f 2018-08-18 stsp
4590 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4591 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4592 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4593 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4594 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4595 7cbe629d 2018-08-04 stsp }
4596 e5a0f69f 2018-08-18 stsp
4597 e5a0f69f 2018-08-18 stsp free(s->path);
4598 11b20872 2019-11-08 stsp free_colors(&s->colors);
4599 e5a0f69f 2018-08-18 stsp return err;
4600 7cbe629d 2018-08-04 stsp }
4601 7cbe629d 2018-08-04 stsp
4602 7cbe629d 2018-08-04 stsp static const struct got_error *
4603 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4604 6c4c42e0 2019-06-24 stsp {
4605 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4606 6c4c42e0 2019-06-24 stsp
4607 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4608 6c4c42e0 2019-06-24 stsp return NULL;
4609 6c4c42e0 2019-06-24 stsp }
4610 6c4c42e0 2019-06-24 stsp
4611 6c4c42e0 2019-06-24 stsp static const struct got_error *
4612 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4613 6c4c42e0 2019-06-24 stsp {
4614 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4615 6c4c42e0 2019-06-24 stsp int lineno;
4616 826082fe 2020-12-10 stsp char *line = NULL;
4617 826082fe 2020-12-10 stsp size_t linesize = 0;
4618 826082fe 2020-12-10 stsp ssize_t linelen;
4619 6c4c42e0 2019-06-24 stsp
4620 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4621 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4622 6c4c42e0 2019-06-24 stsp return NULL;
4623 6c4c42e0 2019-06-24 stsp }
4624 6c4c42e0 2019-06-24 stsp
4625 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4626 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4627 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4628 6c4c42e0 2019-06-24 stsp else
4629 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4630 4b3f9dac 2021-12-17 thomas } else
4631 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4632 6c4c42e0 2019-06-24 stsp
4633 6c4c42e0 2019-06-24 stsp while (1) {
4634 6c4c42e0 2019-06-24 stsp off_t offset;
4635 6c4c42e0 2019-06-24 stsp
4636 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4637 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4638 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4639 6c4c42e0 2019-06-24 stsp break;
4640 6c4c42e0 2019-06-24 stsp }
4641 2246482e 2019-06-25 stsp
4642 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4643 6c4c42e0 2019-06-24 stsp lineno = 1;
4644 6c4c42e0 2019-06-24 stsp else
4645 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4646 6c4c42e0 2019-06-24 stsp }
4647 6c4c42e0 2019-06-24 stsp
4648 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4649 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4650 6c4c42e0 2019-06-24 stsp free(line);
4651 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4652 6c4c42e0 2019-06-24 stsp }
4653 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4654 826082fe 2020-12-10 stsp if (linelen != -1 &&
4655 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4656 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4657 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4658 6c4c42e0 2019-06-24 stsp break;
4659 6c4c42e0 2019-06-24 stsp }
4660 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4661 6c4c42e0 2019-06-24 stsp lineno++;
4662 6c4c42e0 2019-06-24 stsp else
4663 6c4c42e0 2019-06-24 stsp lineno--;
4664 6c4c42e0 2019-06-24 stsp }
4665 826082fe 2020-12-10 stsp free(line);
4666 6c4c42e0 2019-06-24 stsp
4667 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4668 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4669 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4670 6c4c42e0 2019-06-24 stsp }
4671 6c4c42e0 2019-06-24 stsp
4672 6c4c42e0 2019-06-24 stsp return NULL;
4673 6c4c42e0 2019-06-24 stsp }
4674 6c4c42e0 2019-06-24 stsp
4675 6c4c42e0 2019-06-24 stsp static const struct got_error *
4676 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4677 7cbe629d 2018-08-04 stsp {
4678 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4679 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4680 2b380cc8 2018-10-24 stsp int errcode;
4681 2b380cc8 2018-10-24 stsp
4682 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4683 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4684 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4685 2b380cc8 2018-10-24 stsp if (errcode)
4686 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4687 51fe7530 2019-08-19 stsp
4688 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4689 2b380cc8 2018-10-24 stsp }
4690 e5a0f69f 2018-08-18 stsp
4691 51fe7530 2019-08-19 stsp if (s->blame_complete)
4692 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4693 51fe7530 2019-08-19 stsp
4694 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4695 e5a0f69f 2018-08-18 stsp
4696 669b5ffa 2018-10-07 stsp view_vborder(view);
4697 e5a0f69f 2018-08-18 stsp return err;
4698 e5a0f69f 2018-08-18 stsp }
4699 e5a0f69f 2018-08-18 stsp
4700 e5a0f69f 2018-08-18 stsp static const struct got_error *
4701 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4702 e5a0f69f 2018-08-18 stsp {
4703 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4704 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4705 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4706 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4707 7cbe629d 2018-08-04 stsp
4708 e5a0f69f 2018-08-18 stsp switch (ch) {
4709 1e37a5c2 2019-05-12 jcs case 'q':
4710 1e37a5c2 2019-05-12 jcs s->done = 1;
4711 4deef56f 2021-09-02 naddy break;
4712 4deef56f 2021-09-02 naddy case 'g':
4713 4deef56f 2021-09-02 naddy case KEY_HOME:
4714 4deef56f 2021-09-02 naddy s->selected_line = 1;
4715 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4716 4deef56f 2021-09-02 naddy break;
4717 4deef56f 2021-09-02 naddy case 'G':
4718 4deef56f 2021-09-02 naddy case KEY_END:
4719 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4720 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4721 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4722 4deef56f 2021-09-02 naddy } else {
4723 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4724 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4725 4deef56f 2021-09-02 naddy (view->nlines - 3);
4726 4deef56f 2021-09-02 naddy }
4727 1e37a5c2 2019-05-12 jcs break;
4728 1e37a5c2 2019-05-12 jcs case 'k':
4729 1e37a5c2 2019-05-12 jcs case KEY_UP:
4730 f7140bf5 2021-10-17 thomas case CTRL('p'):
4731 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4732 1e37a5c2 2019-05-12 jcs s->selected_line--;
4733 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4734 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4735 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4736 1e37a5c2 2019-05-12 jcs break;
4737 70f17a53 2022-06-13 thomas case CTRL('u'):
4738 23427b14 2022-06-23 thomas case 'u':
4739 70f17a53 2022-06-13 thomas nscroll /= 2;
4740 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4741 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4742 ea025d1d 2020-02-22 naddy case CTRL('b'):
4743 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4744 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4745 e5a0f69f 2018-08-18 stsp break;
4746 1e37a5c2 2019-05-12 jcs }
4747 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4748 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4749 1e37a5c2 2019-05-12 jcs else
4750 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4751 1e37a5c2 2019-05-12 jcs break;
4752 1e37a5c2 2019-05-12 jcs case 'j':
4753 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4754 f7140bf5 2021-10-17 thomas case CTRL('n'):
4755 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4756 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4757 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4758 1e37a5c2 2019-05-12 jcs s->selected_line++;
4759 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4760 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4761 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4762 1e37a5c2 2019-05-12 jcs break;
4763 1e37a5c2 2019-05-12 jcs case 'b':
4764 1e37a5c2 2019-05-12 jcs case 'p': {
4765 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4766 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4767 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4768 1e37a5c2 2019-05-12 jcs if (id == NULL)
4769 e5a0f69f 2018-08-18 stsp break;
4770 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4771 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4772 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4773 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4774 1e37a5c2 2019-05-12 jcs int obj_type;
4775 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4776 1e37a5c2 2019-05-12 jcs s->repo, id);
4777 e5a0f69f 2018-08-18 stsp if (err)
4778 e5a0f69f 2018-08-18 stsp break;
4779 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4780 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4781 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4782 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4783 e5a0f69f 2018-08-18 stsp break;
4784 e5a0f69f 2018-08-18 stsp }
4785 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4786 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4787 ec242592 2022-04-22 thomas s->repo, &pid->id);
4788 945f9229 2022-04-16 thomas if (err)
4789 945f9229 2022-04-16 thomas break;
4790 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4791 945f9229 2022-04-16 thomas pcommit, s->path);
4792 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4793 e5a0f69f 2018-08-18 stsp if (err) {
4794 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4795 1e37a5c2 2019-05-12 jcs err = NULL;
4796 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4797 e5a0f69f 2018-08-18 stsp break;
4798 e5a0f69f 2018-08-18 stsp }
4799 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4800 1e37a5c2 2019-05-12 jcs blob_id);
4801 1e37a5c2 2019-05-12 jcs free(blob_id);
4802 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4803 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4804 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4805 e5a0f69f 2018-08-18 stsp break;
4806 1e37a5c2 2019-05-12 jcs }
4807 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4808 ec242592 2022-04-22 thomas &pid->id);
4809 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4810 1e37a5c2 2019-05-12 jcs } else {
4811 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4812 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4813 1e37a5c2 2019-05-12 jcs break;
4814 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4815 1e37a5c2 2019-05-12 jcs id);
4816 1e37a5c2 2019-05-12 jcs }
4817 1e37a5c2 2019-05-12 jcs if (err)
4818 e5a0f69f 2018-08-18 stsp break;
4819 1e37a5c2 2019-05-12 jcs s->done = 1;
4820 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4821 1e37a5c2 2019-05-12 jcs s->done = 0;
4822 1e37a5c2 2019-05-12 jcs if (thread_err)
4823 1e37a5c2 2019-05-12 jcs break;
4824 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4825 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4826 a5388363 2020-12-01 naddy err = run_blame(view);
4827 1e37a5c2 2019-05-12 jcs if (err)
4828 1e37a5c2 2019-05-12 jcs break;
4829 1e37a5c2 2019-05-12 jcs break;
4830 1e37a5c2 2019-05-12 jcs }
4831 1e37a5c2 2019-05-12 jcs case 'B': {
4832 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4833 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4834 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
4835 1e37a5c2 2019-05-12 jcs break;
4836 1e37a5c2 2019-05-12 jcs s->done = 1;
4837 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4838 1e37a5c2 2019-05-12 jcs s->done = 0;
4839 1e37a5c2 2019-05-12 jcs if (thread_err)
4840 1e37a5c2 2019-05-12 jcs break;
4841 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4842 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4843 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4844 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4845 a5388363 2020-12-01 naddy err = run_blame(view);
4846 1e37a5c2 2019-05-12 jcs if (err)
4847 1e37a5c2 2019-05-12 jcs break;
4848 1e37a5c2 2019-05-12 jcs break;
4849 1e37a5c2 2019-05-12 jcs }
4850 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4851 1e37a5c2 2019-05-12 jcs case '\r': {
4852 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4853 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4854 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4855 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4856 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4857 1e37a5c2 2019-05-12 jcs if (id == NULL)
4858 1e37a5c2 2019-05-12 jcs break;
4859 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4860 1e37a5c2 2019-05-12 jcs if (err)
4861 1e37a5c2 2019-05-12 jcs break;
4862 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4863 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4864 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4865 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4866 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4867 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4868 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4869 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4870 1e37a5c2 2019-05-12 jcs break;
4871 15a94983 2018-12-23 stsp }
4872 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4873 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4874 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4875 1e37a5c2 2019-05-12 jcs if (err) {
4876 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4877 1e37a5c2 2019-05-12 jcs break;
4878 1e37a5c2 2019-05-12 jcs }
4879 e78dc838 2020-12-04 stsp view->focussed = 0;
4880 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4881 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4882 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4883 1e37a5c2 2019-05-12 jcs if (err)
4884 34bc9ec9 2019-02-22 stsp break;
4885 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4886 e78dc838 2020-12-04 stsp view->focus_child = 1;
4887 1e37a5c2 2019-05-12 jcs } else
4888 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4889 1e37a5c2 2019-05-12 jcs if (err)
4890 e5a0f69f 2018-08-18 stsp break;
4891 1e37a5c2 2019-05-12 jcs break;
4892 1e37a5c2 2019-05-12 jcs }
4893 70f17a53 2022-06-13 thomas case CTRL('d'):
4894 23427b14 2022-06-23 thomas case 'd':
4895 70f17a53 2022-06-13 thomas nscroll /= 2;
4896 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4897 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4898 ea025d1d 2020-02-22 naddy case CTRL('f'):
4899 1e37a5c2 2019-05-12 jcs case ' ':
4900 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4901 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4902 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4903 e5a0f69f 2018-08-18 stsp break;
4904 1e37a5c2 2019-05-12 jcs }
4905 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4906 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4907 70f17a53 2022-06-13 thomas s->selected_line +=
4908 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
4909 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
4910 1e37a5c2 2019-05-12 jcs }
4911 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
4912 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
4913 1e37a5c2 2019-05-12 jcs else
4914 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4915 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
4916 1e37a5c2 2019-05-12 jcs break;
4917 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4918 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4919 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4920 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4921 1e37a5c2 2019-05-12 jcs }
4922 1e37a5c2 2019-05-12 jcs break;
4923 1e37a5c2 2019-05-12 jcs default:
4924 1e37a5c2 2019-05-12 jcs break;
4925 a70480e0 2018-06-23 stsp }
4926 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4927 a70480e0 2018-06-23 stsp }
4928 a70480e0 2018-06-23 stsp
4929 a70480e0 2018-06-23 stsp static const struct got_error *
4930 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4931 9f7d7167 2018-04-29 stsp {
4932 a70480e0 2018-06-23 stsp const struct got_error *error;
4933 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4934 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4935 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4936 0587e10c 2020-07-23 stsp char *link_target = NULL;
4937 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4938 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4939 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4940 a70480e0 2018-06-23 stsp int ch;
4941 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4942 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4943 a70480e0 2018-06-23 stsp
4944 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4945 a70480e0 2018-06-23 stsp switch (ch) {
4946 a70480e0 2018-06-23 stsp case 'c':
4947 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4948 a70480e0 2018-06-23 stsp break;
4949 69069811 2018-08-02 stsp case 'r':
4950 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4951 69069811 2018-08-02 stsp if (repo_path == NULL)
4952 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4953 9ba1d308 2019-10-21 stsp optarg);
4954 69069811 2018-08-02 stsp break;
4955 a70480e0 2018-06-23 stsp default:
4956 17020d27 2019-03-07 stsp usage_blame();
4957 a70480e0 2018-06-23 stsp /* NOTREACHED */
4958 a70480e0 2018-06-23 stsp }
4959 a70480e0 2018-06-23 stsp }
4960 a70480e0 2018-06-23 stsp
4961 a70480e0 2018-06-23 stsp argc -= optind;
4962 a70480e0 2018-06-23 stsp argv += optind;
4963 a70480e0 2018-06-23 stsp
4964 f135c941 2020-02-20 stsp if (argc != 1)
4965 a70480e0 2018-06-23 stsp usage_blame();
4966 6962eb72 2020-02-20 stsp
4967 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4968 7cd52833 2022-06-23 thomas if (error != NULL)
4969 7cd52833 2022-06-23 thomas goto done;
4970 7cd52833 2022-06-23 thomas
4971 69069811 2018-08-02 stsp if (repo_path == NULL) {
4972 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4973 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4974 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4975 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4976 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4977 c156c7a4 2020-12-18 stsp goto done;
4978 f135c941 2020-02-20 stsp if (worktree)
4979 eb41ed75 2019-02-05 stsp repo_path =
4980 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4981 f135c941 2020-02-20 stsp else
4982 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4983 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4984 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4985 c156c7a4 2020-12-18 stsp goto done;
4986 c156c7a4 2020-12-18 stsp }
4987 f135c941 2020-02-20 stsp }
4988 a915003a 2019-02-05 stsp
4989 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4990 c02c541e 2019-03-29 stsp if (error != NULL)
4991 8e94dd5b 2019-01-04 stsp goto done;
4992 69069811 2018-08-02 stsp
4993 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4994 f135c941 2020-02-20 stsp worktree);
4995 c02c541e 2019-03-29 stsp if (error)
4996 92205607 2019-01-04 stsp goto done;
4997 69069811 2018-08-02 stsp
4998 f135c941 2020-02-20 stsp init_curses();
4999 f135c941 2020-02-20 stsp
5000 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5001 51a10b52 2020-12-26 stsp if (error)
5002 51a10b52 2020-12-26 stsp goto done;
5003 51a10b52 2020-12-26 stsp
5004 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5005 eb41ed75 2019-02-05 stsp if (error)
5006 69069811 2018-08-02 stsp goto done;
5007 a70480e0 2018-06-23 stsp
5008 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5009 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5010 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5011 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5012 a70480e0 2018-06-23 stsp if (error != NULL)
5013 66b4983c 2018-06-23 stsp goto done;
5014 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5015 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5016 a70480e0 2018-06-23 stsp } else {
5017 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5018 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5019 a70480e0 2018-06-23 stsp }
5020 a19e88aa 2018-06-23 stsp if (error != NULL)
5021 8b473291 2019-02-21 stsp goto done;
5022 8b473291 2019-02-21 stsp
5023 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5024 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5025 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5026 e1cd8fed 2018-08-01 stsp goto done;
5027 e1cd8fed 2018-08-01 stsp }
5028 0587e10c 2020-07-23 stsp
5029 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5030 945f9229 2022-04-16 thomas if (error)
5031 945f9229 2022-04-16 thomas goto done;
5032 945f9229 2022-04-16 thomas
5033 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5034 945f9229 2022-04-16 thomas commit, repo);
5035 7cbe629d 2018-08-04 stsp if (error)
5036 7cbe629d 2018-08-04 stsp goto done;
5037 0587e10c 2020-07-23 stsp
5038 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5039 78756c87 2020-11-24 stsp commit_id, repo);
5040 0587e10c 2020-07-23 stsp if (error)
5041 0587e10c 2020-07-23 stsp goto done;
5042 12314ad4 2019-08-31 stsp if (worktree) {
5043 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5044 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5045 12314ad4 2019-08-31 stsp worktree = NULL;
5046 12314ad4 2019-08-31 stsp }
5047 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5048 a70480e0 2018-06-23 stsp done:
5049 69069811 2018-08-02 stsp free(repo_path);
5050 f135c941 2020-02-20 stsp free(in_repo_path);
5051 0587e10c 2020-07-23 stsp free(link_target);
5052 69069811 2018-08-02 stsp free(cwd);
5053 a70480e0 2018-06-23 stsp free(commit_id);
5054 945f9229 2022-04-16 thomas if (commit)
5055 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5056 eb41ed75 2019-02-05 stsp if (worktree)
5057 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5058 1d0f4054 2021-06-17 stsp if (repo) {
5059 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5060 1d0f4054 2021-06-17 stsp if (error == NULL)
5061 1d0f4054 2021-06-17 stsp error = close_err;
5062 1d0f4054 2021-06-17 stsp }
5063 7cd52833 2022-06-23 thomas if (pack_fds) {
5064 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5065 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5066 7cd52833 2022-06-23 thomas if (error == NULL)
5067 7cd52833 2022-06-23 thomas error = pack_err;
5068 7cd52833 2022-06-23 thomas }
5069 51a10b52 2020-12-26 stsp tog_free_refs();
5070 a70480e0 2018-06-23 stsp return error;
5071 ffd1d5e5 2018-06-23 stsp }
5072 ffd1d5e5 2018-06-23 stsp
5073 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5074 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5075 ffd1d5e5 2018-06-23 stsp {
5076 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5077 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5078 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5079 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5080 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5081 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5082 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5083 ffd1d5e5 2018-06-23 stsp
5084 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5085 ffd1d5e5 2018-06-23 stsp
5086 f7d12f7e 2018-08-01 stsp werase(view->window);
5087 ffd1d5e5 2018-06-23 stsp
5088 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5089 ffd1d5e5 2018-06-23 stsp return NULL;
5090 ffd1d5e5 2018-06-23 stsp
5091 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5092 ffd1d5e5 2018-06-23 stsp if (err)
5093 ffd1d5e5 2018-06-23 stsp return err;
5094 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5095 a3404814 2018-09-02 stsp wstandout(view->window);
5096 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5097 11b20872 2019-11-08 stsp if (tc)
5098 11b20872 2019-11-08 stsp wattr_on(view->window,
5099 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5100 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5101 11b20872 2019-11-08 stsp if (tc)
5102 11b20872 2019-11-08 stsp wattr_off(view->window,
5103 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5104 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5105 a3404814 2018-09-02 stsp wstandend(view->window);
5106 2550e4c3 2018-07-13 stsp free(wline);
5107 2550e4c3 2018-07-13 stsp wline = NULL;
5108 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5109 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5110 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5111 ffd1d5e5 2018-06-23 stsp return NULL;
5112 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5113 ce52c690 2018-06-23 stsp if (err)
5114 ce52c690 2018-06-23 stsp return err;
5115 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5116 2550e4c3 2018-07-13 stsp free(wline);
5117 2550e4c3 2018-07-13 stsp wline = NULL;
5118 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5119 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5120 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5121 ffd1d5e5 2018-06-23 stsp return NULL;
5122 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5123 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5124 a1eca9bb 2018-06-23 stsp return NULL;
5125 ffd1d5e5 2018-06-23 stsp
5126 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5127 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5128 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5129 0cf4efb1 2018-09-29 stsp if (view->focussed)
5130 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5131 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5132 ffd1d5e5 2018-06-23 stsp }
5133 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5134 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5135 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5136 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5137 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5138 ffd1d5e5 2018-06-23 stsp return NULL;
5139 ffd1d5e5 2018-06-23 stsp n = 1;
5140 ffd1d5e5 2018-06-23 stsp } else {
5141 ffd1d5e5 2018-06-23 stsp n = 0;
5142 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5143 ffd1d5e5 2018-06-23 stsp }
5144 ffd1d5e5 2018-06-23 stsp
5145 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5146 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5147 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5148 848d6979 2019-08-12 stsp const char *modestr = "";
5149 56e0773d 2019-11-28 stsp mode_t mode;
5150 1d13200f 2018-07-12 stsp
5151 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5152 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5153 56e0773d 2019-11-28 stsp
5154 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5155 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5156 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5157 1d13200f 2018-07-12 stsp if (err)
5158 638f9024 2019-05-13 stsp return got_error_from_errno(
5159 230a42bd 2019-05-11 jcs "got_object_id_str");
5160 1d13200f 2018-07-12 stsp }
5161 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5162 63c5ca5d 2019-08-24 stsp modestr = "$";
5163 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5164 0d6c6ee3 2020-05-20 stsp int i;
5165 0d6c6ee3 2020-05-20 stsp
5166 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5167 d86d3b18 2020-12-01 naddy te, s->repo);
5168 0d6c6ee3 2020-05-20 stsp if (err) {
5169 0d6c6ee3 2020-05-20 stsp free(id_str);
5170 0d6c6ee3 2020-05-20 stsp return err;
5171 0d6c6ee3 2020-05-20 stsp }
5172 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5173 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5174 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5175 0d6c6ee3 2020-05-20 stsp }
5176 848d6979 2019-08-12 stsp modestr = "@";
5177 0d6c6ee3 2020-05-20 stsp }
5178 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5179 848d6979 2019-08-12 stsp modestr = "/";
5180 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5181 848d6979 2019-08-12 stsp modestr = "*";
5182 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5183 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5184 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5185 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5186 1d13200f 2018-07-12 stsp free(id_str);
5187 0d6c6ee3 2020-05-20 stsp free(link_target);
5188 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5189 1d13200f 2018-07-12 stsp }
5190 1d13200f 2018-07-12 stsp free(id_str);
5191 0d6c6ee3 2020-05-20 stsp free(link_target);
5192 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5193 ffd1d5e5 2018-06-23 stsp if (err) {
5194 ffd1d5e5 2018-06-23 stsp free(line);
5195 ffd1d5e5 2018-06-23 stsp break;
5196 ffd1d5e5 2018-06-23 stsp }
5197 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5198 0cf4efb1 2018-09-29 stsp if (view->focussed)
5199 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5200 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5201 ffd1d5e5 2018-06-23 stsp }
5202 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5203 f26dddb7 2019-11-08 stsp if (tc)
5204 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5205 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5206 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5207 f26dddb7 2019-11-08 stsp if (tc)
5208 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5209 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5210 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5211 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5212 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5213 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5214 ffd1d5e5 2018-06-23 stsp free(line);
5215 2550e4c3 2018-07-13 stsp free(wline);
5216 2550e4c3 2018-07-13 stsp wline = NULL;
5217 ffd1d5e5 2018-06-23 stsp n++;
5218 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5219 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5220 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5221 ffd1d5e5 2018-06-23 stsp break;
5222 ffd1d5e5 2018-06-23 stsp }
5223 ffd1d5e5 2018-06-23 stsp
5224 ffd1d5e5 2018-06-23 stsp return err;
5225 ffd1d5e5 2018-06-23 stsp }
5226 ffd1d5e5 2018-06-23 stsp
5227 ffd1d5e5 2018-06-23 stsp static void
5228 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5229 ffd1d5e5 2018-06-23 stsp {
5230 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5231 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5232 fa86c4bf 2020-11-29 stsp int i = 0;
5233 ffd1d5e5 2018-06-23 stsp
5234 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5235 ffd1d5e5 2018-06-23 stsp return;
5236 ffd1d5e5 2018-06-23 stsp
5237 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5238 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5239 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5240 fa86c4bf 2020-11-29 stsp if (!isroot)
5241 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5242 fa86c4bf 2020-11-29 stsp break;
5243 fa86c4bf 2020-11-29 stsp }
5244 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5245 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5246 ffd1d5e5 2018-06-23 stsp }
5247 ffd1d5e5 2018-06-23 stsp }
5248 ffd1d5e5 2018-06-23 stsp
5249 fa86c4bf 2020-11-29 stsp static void
5250 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5251 ffd1d5e5 2018-06-23 stsp {
5252 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5253 ffd1d5e5 2018-06-23 stsp int n = 0;
5254 ffd1d5e5 2018-06-23 stsp
5255 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5256 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5257 694d3271 2020-12-01 naddy s->first_displayed_entry);
5258 694d3271 2020-12-01 naddy else
5259 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5260 56e0773d 2019-11-28 stsp
5261 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5262 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5263 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5264 768394f3 2019-01-24 stsp if (last) {
5265 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5266 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5267 768394f3 2019-01-24 stsp }
5268 ffd1d5e5 2018-06-23 stsp }
5269 ffd1d5e5 2018-06-23 stsp }
5270 ffd1d5e5 2018-06-23 stsp
5271 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5272 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5273 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5274 ffd1d5e5 2018-06-23 stsp {
5275 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5276 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5277 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5278 ffd1d5e5 2018-06-23 stsp
5279 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5280 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5281 56e0773d 2019-11-28 stsp + 1 /* slash */;
5282 ce52c690 2018-06-23 stsp if (te)
5283 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5284 ce52c690 2018-06-23 stsp
5285 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5286 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5287 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5288 ffd1d5e5 2018-06-23 stsp
5289 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5290 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5291 d9765a41 2018-06-23 stsp while (pt) {
5292 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5293 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5294 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5295 cb2ebc8a 2018-06-23 stsp goto done;
5296 cb2ebc8a 2018-06-23 stsp }
5297 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5298 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5299 cb2ebc8a 2018-06-23 stsp goto done;
5300 cb2ebc8a 2018-06-23 stsp }
5301 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5302 ffd1d5e5 2018-06-23 stsp }
5303 ce52c690 2018-06-23 stsp if (te) {
5304 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5305 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5306 ce52c690 2018-06-23 stsp goto done;
5307 ce52c690 2018-06-23 stsp }
5308 cb2ebc8a 2018-06-23 stsp }
5309 ce52c690 2018-06-23 stsp done:
5310 ce52c690 2018-06-23 stsp if (err) {
5311 ce52c690 2018-06-23 stsp free(*path);
5312 ce52c690 2018-06-23 stsp *path = NULL;
5313 ce52c690 2018-06-23 stsp }
5314 ce52c690 2018-06-23 stsp return err;
5315 ce52c690 2018-06-23 stsp }
5316 ce52c690 2018-06-23 stsp
5317 ce52c690 2018-06-23 stsp static const struct got_error *
5318 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5319 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5320 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5321 ce52c690 2018-06-23 stsp {
5322 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5323 ce52c690 2018-06-23 stsp char *path;
5324 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5325 a0de39f3 2019-08-09 stsp
5326 a0de39f3 2019-08-09 stsp *new_view = NULL;
5327 69efd4c4 2018-07-18 stsp
5328 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5329 ce52c690 2018-06-23 stsp if (err)
5330 ce52c690 2018-06-23 stsp return err;
5331 ffd1d5e5 2018-06-23 stsp
5332 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5333 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5334 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5335 83ce39e3 2019-08-12 stsp goto done;
5336 83ce39e3 2019-08-12 stsp }
5337 cdf1ee82 2018-08-01 stsp
5338 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5339 e5a0f69f 2018-08-18 stsp if (err) {
5340 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5341 fc06ba56 2019-08-22 stsp err = NULL;
5342 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5343 e5a0f69f 2018-08-18 stsp } else
5344 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5345 83ce39e3 2019-08-12 stsp done:
5346 83ce39e3 2019-08-12 stsp free(path);
5347 69efd4c4 2018-07-18 stsp return err;
5348 69efd4c4 2018-07-18 stsp }
5349 69efd4c4 2018-07-18 stsp
5350 69efd4c4 2018-07-18 stsp static const struct got_error *
5351 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5352 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5353 69efd4c4 2018-07-18 stsp {
5354 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5355 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5356 69efd4c4 2018-07-18 stsp char *path;
5357 a0de39f3 2019-08-09 stsp
5358 a0de39f3 2019-08-09 stsp *new_view = NULL;
5359 69efd4c4 2018-07-18 stsp
5360 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5361 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5362 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5363 e5a0f69f 2018-08-18 stsp
5364 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5365 69efd4c4 2018-07-18 stsp if (err)
5366 69efd4c4 2018-07-18 stsp return err;
5367 69efd4c4 2018-07-18 stsp
5368 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5369 4e97c21c 2020-12-06 stsp path, 0);
5370 ba4f502b 2018-08-04 stsp if (err)
5371 e5a0f69f 2018-08-18 stsp view_close(log_view);
5372 e5a0f69f 2018-08-18 stsp else
5373 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5374 cb2ebc8a 2018-06-23 stsp free(path);
5375 cb2ebc8a 2018-06-23 stsp return err;
5376 ffd1d5e5 2018-06-23 stsp }
5377 ffd1d5e5 2018-06-23 stsp
5378 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5379 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5380 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5381 ffd1d5e5 2018-06-23 stsp {
5382 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5383 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5384 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5385 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5386 ffd1d5e5 2018-06-23 stsp
5387 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5388 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5389 bc573f3b 2021-07-10 stsp
5390 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5391 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5392 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5393 ffd1d5e5 2018-06-23 stsp
5394 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5395 bc573f3b 2021-07-10 stsp if (err)
5396 bc573f3b 2021-07-10 stsp goto done;
5397 bc573f3b 2021-07-10 stsp
5398 bc573f3b 2021-07-10 stsp /*
5399 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5400 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5401 bc573f3b 2021-07-10 stsp * closed on demand.
5402 bc573f3b 2021-07-10 stsp */
5403 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5404 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5405 bc573f3b 2021-07-10 stsp if (err)
5406 bc573f3b 2021-07-10 stsp goto done;
5407 bc573f3b 2021-07-10 stsp s->tree = s->root;
5408 bc573f3b 2021-07-10 stsp
5409 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5410 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5411 ffd1d5e5 2018-06-23 stsp goto done;
5412 ffd1d5e5 2018-06-23 stsp
5413 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5414 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5415 ffd1d5e5 2018-06-23 stsp goto done;
5416 ffd1d5e5 2018-06-23 stsp }
5417 ffd1d5e5 2018-06-23 stsp
5418 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5419 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5420 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5421 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5422 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5423 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5424 9cd7cbd1 2020-12-07 stsp goto done;
5425 9cd7cbd1 2020-12-07 stsp }
5426 9cd7cbd1 2020-12-07 stsp }
5427 fb2756b9 2018-08-04 stsp s->repo = repo;
5428 c0b01bdb 2019-11-08 stsp
5429 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5430 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5431 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5432 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5433 c0b01bdb 2019-11-08 stsp if (err)
5434 c0b01bdb 2019-11-08 stsp goto done;
5435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5436 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5437 bc573f3b 2021-07-10 stsp if (err)
5438 c0b01bdb 2019-11-08 stsp goto done;
5439 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5440 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5441 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5442 bc573f3b 2021-07-10 stsp if (err)
5443 c0b01bdb 2019-11-08 stsp goto done;
5444 e5a0f69f 2018-08-18 stsp
5445 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5446 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5447 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5448 bc573f3b 2021-07-10 stsp if (err)
5449 11b20872 2019-11-08 stsp goto done;
5450 11b20872 2019-11-08 stsp
5451 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5452 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5453 bc573f3b 2021-07-10 stsp if (err)
5454 c0b01bdb 2019-11-08 stsp goto done;
5455 c0b01bdb 2019-11-08 stsp }
5456 c0b01bdb 2019-11-08 stsp
5457 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5458 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5459 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5460 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5461 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5462 ad80ab7b 2018-08-04 stsp done:
5463 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5464 bc573f3b 2021-07-10 stsp if (commit)
5465 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5466 bc573f3b 2021-07-10 stsp if (err)
5467 bc573f3b 2021-07-10 stsp close_tree_view(view);
5468 ad80ab7b 2018-08-04 stsp return err;
5469 ad80ab7b 2018-08-04 stsp }
5470 ad80ab7b 2018-08-04 stsp
5471 e5a0f69f 2018-08-18 stsp static const struct got_error *
5472 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5473 ad80ab7b 2018-08-04 stsp {
5474 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5475 ad80ab7b 2018-08-04 stsp
5476 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5477 fb2756b9 2018-08-04 stsp free(s->tree_label);
5478 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5479 6484ec90 2018-09-29 stsp free(s->commit_id);
5480 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5481 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5482 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5483 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5484 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5485 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5486 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5487 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5488 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5489 ad80ab7b 2018-08-04 stsp free(parent);
5490 ad80ab7b 2018-08-04 stsp
5491 ad80ab7b 2018-08-04 stsp }
5492 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5493 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5494 bc573f3b 2021-07-10 stsp if (s->root)
5495 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5496 7c32bd05 2019-06-22 stsp return NULL;
5497 7c32bd05 2019-06-22 stsp }
5498 7c32bd05 2019-06-22 stsp
5499 7c32bd05 2019-06-22 stsp static const struct got_error *
5500 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5501 7c32bd05 2019-06-22 stsp {
5502 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5503 7c32bd05 2019-06-22 stsp
5504 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5505 7c32bd05 2019-06-22 stsp return NULL;
5506 7c32bd05 2019-06-22 stsp }
5507 7c32bd05 2019-06-22 stsp
5508 7c32bd05 2019-06-22 stsp static int
5509 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5510 7c32bd05 2019-06-22 stsp {
5511 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5512 7c32bd05 2019-06-22 stsp
5513 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5514 56e0773d 2019-11-28 stsp 0) == 0;
5515 7c32bd05 2019-06-22 stsp }
5516 7c32bd05 2019-06-22 stsp
5517 7c32bd05 2019-06-22 stsp static const struct got_error *
5518 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5519 7c32bd05 2019-06-22 stsp {
5520 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5521 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5522 7c32bd05 2019-06-22 stsp
5523 7c32bd05 2019-06-22 stsp if (!view->searching) {
5524 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5525 7c32bd05 2019-06-22 stsp return NULL;
5526 7c32bd05 2019-06-22 stsp }
5527 7c32bd05 2019-06-22 stsp
5528 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5529 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5530 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5531 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5532 56e0773d 2019-11-28 stsp s->selected_entry);
5533 7c32bd05 2019-06-22 stsp else
5534 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5535 56e0773d 2019-11-28 stsp } else {
5536 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5537 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5538 56e0773d 2019-11-28 stsp else
5539 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5540 56e0773d 2019-11-28 stsp s->selected_entry);
5541 7c32bd05 2019-06-22 stsp }
5542 7c32bd05 2019-06-22 stsp } else {
5543 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5544 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5545 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5546 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5547 56e0773d 2019-11-28 stsp else
5548 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5549 7c32bd05 2019-06-22 stsp }
5550 7c32bd05 2019-06-22 stsp
5551 7c32bd05 2019-06-22 stsp while (1) {
5552 56e0773d 2019-11-28 stsp if (te == NULL) {
5553 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5554 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5555 ac66afb8 2019-06-24 stsp return NULL;
5556 ac66afb8 2019-06-24 stsp }
5557 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5558 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5559 56e0773d 2019-11-28 stsp else
5560 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5561 7c32bd05 2019-06-22 stsp }
5562 7c32bd05 2019-06-22 stsp
5563 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5564 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5565 56e0773d 2019-11-28 stsp s->matched_entry = te;
5566 7c32bd05 2019-06-22 stsp break;
5567 7c32bd05 2019-06-22 stsp }
5568 7c32bd05 2019-06-22 stsp
5569 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5570 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5571 56e0773d 2019-11-28 stsp else
5572 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5573 7c32bd05 2019-06-22 stsp }
5574 e5a0f69f 2018-08-18 stsp
5575 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5576 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5577 7c32bd05 2019-06-22 stsp s->selected = 0;
5578 7c32bd05 2019-06-22 stsp }
5579 7c32bd05 2019-06-22 stsp
5580 e5a0f69f 2018-08-18 stsp return NULL;
5581 ad80ab7b 2018-08-04 stsp }
5582 ad80ab7b 2018-08-04 stsp
5583 ad80ab7b 2018-08-04 stsp static const struct got_error *
5584 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5585 ad80ab7b 2018-08-04 stsp {
5586 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5587 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5588 e5a0f69f 2018-08-18 stsp char *parent_path;
5589 ad80ab7b 2018-08-04 stsp
5590 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5591 e5a0f69f 2018-08-18 stsp if (err)
5592 e5a0f69f 2018-08-18 stsp return err;
5593 ffd1d5e5 2018-06-23 stsp
5594 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5595 e5a0f69f 2018-08-18 stsp free(parent_path);
5596 669b5ffa 2018-10-07 stsp
5597 669b5ffa 2018-10-07 stsp view_vborder(view);
5598 e5a0f69f 2018-08-18 stsp return err;
5599 e5a0f69f 2018-08-18 stsp }
5600 ce52c690 2018-06-23 stsp
5601 e5a0f69f 2018-08-18 stsp static const struct got_error *
5602 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5603 e5a0f69f 2018-08-18 stsp {
5604 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5605 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5606 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5607 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5608 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5609 ffd1d5e5 2018-06-23 stsp
5610 e5a0f69f 2018-08-18 stsp switch (ch) {
5611 1e37a5c2 2019-05-12 jcs case 'i':
5612 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5613 1e37a5c2 2019-05-12 jcs break;
5614 1e37a5c2 2019-05-12 jcs case 'l':
5615 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5616 ffd1d5e5 2018-06-23 stsp break;
5617 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5618 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5619 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5620 e78dc838 2020-12-04 stsp view->focussed = 0;
5621 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5624 1e37a5c2 2019-05-12 jcs if (err)
5625 1e37a5c2 2019-05-12 jcs return err;
5626 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5627 e78dc838 2020-12-04 stsp view->focus_child = 1;
5628 1e37a5c2 2019-05-12 jcs } else
5629 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5630 152c1c93 2020-11-29 stsp break;
5631 152c1c93 2020-11-29 stsp case 'r':
5632 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5633 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5634 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5635 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5636 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5637 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5638 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5639 152c1c93 2020-11-29 stsp if (err) {
5640 152c1c93 2020-11-29 stsp view_close(ref_view);
5641 152c1c93 2020-11-29 stsp return err;
5642 152c1c93 2020-11-29 stsp }
5643 e78dc838 2020-12-04 stsp view->focussed = 0;
5644 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5645 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5646 152c1c93 2020-11-29 stsp err = view_close_child(view);
5647 152c1c93 2020-11-29 stsp if (err)
5648 152c1c93 2020-11-29 stsp return err;
5649 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5650 e78dc838 2020-12-04 stsp view->focus_child = 1;
5651 152c1c93 2020-11-29 stsp } else
5652 152c1c93 2020-11-29 stsp *new_view = ref_view;
5653 e4526bf5 2021-09-03 naddy break;
5654 e4526bf5 2021-09-03 naddy case 'g':
5655 e4526bf5 2021-09-03 naddy case KEY_HOME:
5656 e4526bf5 2021-09-03 naddy s->selected = 0;
5657 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5658 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5659 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5660 e4526bf5 2021-09-03 naddy else
5661 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5662 1e37a5c2 2019-05-12 jcs break;
5663 e4526bf5 2021-09-03 naddy case 'G':
5664 e4526bf5 2021-09-03 naddy case KEY_END:
5665 e4526bf5 2021-09-03 naddy s->selected = 0;
5666 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5667 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5668 e4526bf5 2021-09-03 naddy if (te == NULL) {
5669 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5670 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5671 e4526bf5 2021-09-03 naddy n++;
5672 e4526bf5 2021-09-03 naddy }
5673 e4526bf5 2021-09-03 naddy break;
5674 e4526bf5 2021-09-03 naddy }
5675 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5676 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5677 e4526bf5 2021-09-03 naddy }
5678 e4526bf5 2021-09-03 naddy if (n > 0)
5679 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5680 e4526bf5 2021-09-03 naddy break;
5681 1e37a5c2 2019-05-12 jcs case 'k':
5682 1e37a5c2 2019-05-12 jcs case KEY_UP:
5683 f7140bf5 2021-10-17 thomas case CTRL('p'):
5684 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5685 1e37a5c2 2019-05-12 jcs s->selected--;
5686 fa86c4bf 2020-11-29 stsp break;
5687 1e37a5c2 2019-05-12 jcs }
5688 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5689 1e37a5c2 2019-05-12 jcs break;
5690 70f17a53 2022-06-13 thomas case CTRL('u'):
5691 23427b14 2022-06-23 thomas case 'u':
5692 70f17a53 2022-06-13 thomas nscroll /= 2;
5693 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5694 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5695 ea025d1d 2020-02-22 naddy case CTRL('b'):
5696 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5697 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5698 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5699 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5700 fa86c4bf 2020-11-29 stsp } else {
5701 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5702 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5703 fa86c4bf 2020-11-29 stsp }
5704 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5705 1e37a5c2 2019-05-12 jcs break;
5706 1e37a5c2 2019-05-12 jcs case 'j':
5707 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5708 f7140bf5 2021-10-17 thomas case CTRL('n'):
5709 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5710 1e37a5c2 2019-05-12 jcs s->selected++;
5711 1e37a5c2 2019-05-12 jcs break;
5712 1e37a5c2 2019-05-12 jcs }
5713 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5714 56e0773d 2019-11-28 stsp == NULL)
5715 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5716 1e37a5c2 2019-05-12 jcs break;
5717 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5718 1e37a5c2 2019-05-12 jcs break;
5719 70f17a53 2022-06-13 thomas case CTRL('d'):
5720 23427b14 2022-06-23 thomas case 'd':
5721 70f17a53 2022-06-13 thomas nscroll /= 2;
5722 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5723 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5724 ea025d1d 2020-02-22 naddy case CTRL('f'):
5725 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5726 1e37a5c2 2019-05-12 jcs == NULL) {
5727 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5728 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5729 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5730 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5731 1e37a5c2 2019-05-12 jcs break;
5732 1e37a5c2 2019-05-12 jcs }
5733 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5734 1e37a5c2 2019-05-12 jcs break;
5735 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5736 1e37a5c2 2019-05-12 jcs case '\r':
5737 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5738 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5739 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5740 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5741 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5742 1e37a5c2 2019-05-12 jcs break;
5743 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5744 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5745 1e37a5c2 2019-05-12 jcs entry);
5746 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5747 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5748 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5749 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5750 1e37a5c2 2019-05-12 jcs s->selected_entry =
5751 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5752 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5753 1e37a5c2 2019-05-12 jcs free(parent);
5754 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5755 56e0773d 2019-11-28 stsp s->selected_entry))) {
5756 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5757 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5758 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5759 1e37a5c2 2019-05-12 jcs if (err)
5760 1e37a5c2 2019-05-12 jcs break;
5761 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5762 941e9f74 2019-05-21 stsp if (err) {
5763 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5764 1e37a5c2 2019-05-12 jcs break;
5765 1e37a5c2 2019-05-12 jcs }
5766 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5767 56e0773d 2019-11-28 stsp s->selected_entry))) {
5768 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5769 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5770 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5771 1e37a5c2 2019-05-12 jcs
5772 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5773 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5774 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5775 1e37a5c2 2019-05-12 jcs if (err)
5776 1e37a5c2 2019-05-12 jcs break;
5777 e78dc838 2020-12-04 stsp view->focussed = 0;
5778 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5779 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5780 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5781 669b5ffa 2018-10-07 stsp if (err)
5782 669b5ffa 2018-10-07 stsp return err;
5783 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5784 e78dc838 2020-12-04 stsp view->focus_child = 1;
5785 669b5ffa 2018-10-07 stsp } else
5786 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5787 1e37a5c2 2019-05-12 jcs }
5788 1e37a5c2 2019-05-12 jcs break;
5789 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5790 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5791 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5792 1e37a5c2 2019-05-12 jcs break;
5793 1e37a5c2 2019-05-12 jcs default:
5794 1e37a5c2 2019-05-12 jcs break;
5795 ffd1d5e5 2018-06-23 stsp }
5796 e5a0f69f 2018-08-18 stsp
5797 ffd1d5e5 2018-06-23 stsp return err;
5798 9f7d7167 2018-04-29 stsp }
5799 9f7d7167 2018-04-29 stsp
5800 ffd1d5e5 2018-06-23 stsp __dead static void
5801 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5802 ffd1d5e5 2018-06-23 stsp {
5803 ffd1d5e5 2018-06-23 stsp endwin();
5804 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5805 ffd1d5e5 2018-06-23 stsp getprogname());
5806 ffd1d5e5 2018-06-23 stsp exit(1);
5807 ffd1d5e5 2018-06-23 stsp }
5808 ffd1d5e5 2018-06-23 stsp
5809 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5810 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5811 ffd1d5e5 2018-06-23 stsp {
5812 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5813 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5814 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5815 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5816 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5817 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5818 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5819 4e97c21c 2020-12-06 stsp char *label = NULL;
5820 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5821 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5822 ffd1d5e5 2018-06-23 stsp int ch;
5823 5221c383 2018-08-01 stsp struct tog_view *view;
5824 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5825 70ac5f84 2019-03-28 stsp
5826 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5827 ffd1d5e5 2018-06-23 stsp switch (ch) {
5828 ffd1d5e5 2018-06-23 stsp case 'c':
5829 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5830 74283ab8 2020-02-07 stsp break;
5831 74283ab8 2020-02-07 stsp case 'r':
5832 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5833 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5834 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5835 74283ab8 2020-02-07 stsp optarg);
5836 ffd1d5e5 2018-06-23 stsp break;
5837 ffd1d5e5 2018-06-23 stsp default:
5838 e99e2d15 2020-11-24 naddy usage_tree();
5839 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5840 ffd1d5e5 2018-06-23 stsp }
5841 ffd1d5e5 2018-06-23 stsp }
5842 ffd1d5e5 2018-06-23 stsp
5843 ffd1d5e5 2018-06-23 stsp argc -= optind;
5844 ffd1d5e5 2018-06-23 stsp argv += optind;
5845 ffd1d5e5 2018-06-23 stsp
5846 55cccc34 2020-02-20 stsp if (argc > 1)
5847 e99e2d15 2020-11-24 naddy usage_tree();
5848 7cd52833 2022-06-23 thomas
5849 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5850 7cd52833 2022-06-23 thomas if (error != NULL)
5851 7cd52833 2022-06-23 thomas goto done;
5852 74283ab8 2020-02-07 stsp
5853 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5854 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5855 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5856 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5857 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5858 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5859 c156c7a4 2020-12-18 stsp goto done;
5860 55cccc34 2020-02-20 stsp if (worktree)
5861 52185f70 2019-02-05 stsp repo_path =
5862 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5863 55cccc34 2020-02-20 stsp else
5864 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5865 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5866 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5867 c156c7a4 2020-12-18 stsp goto done;
5868 c156c7a4 2020-12-18 stsp }
5869 55cccc34 2020-02-20 stsp }
5870 a915003a 2019-02-05 stsp
5871 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5872 c02c541e 2019-03-29 stsp if (error != NULL)
5873 52185f70 2019-02-05 stsp goto done;
5874 d188b9a6 2019-01-04 stsp
5875 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5876 55cccc34 2020-02-20 stsp repo, worktree);
5877 55cccc34 2020-02-20 stsp if (error)
5878 55cccc34 2020-02-20 stsp goto done;
5879 55cccc34 2020-02-20 stsp
5880 55cccc34 2020-02-20 stsp init_curses();
5881 55cccc34 2020-02-20 stsp
5882 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5883 c02c541e 2019-03-29 stsp if (error)
5884 52185f70 2019-02-05 stsp goto done;
5885 ffd1d5e5 2018-06-23 stsp
5886 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5887 51a10b52 2020-12-26 stsp if (error)
5888 51a10b52 2020-12-26 stsp goto done;
5889 51a10b52 2020-12-26 stsp
5890 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5891 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5892 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5893 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5894 4e97c21c 2020-12-06 stsp if (error)
5895 4e97c21c 2020-12-06 stsp goto done;
5896 4e97c21c 2020-12-06 stsp head_ref_name = label;
5897 4e97c21c 2020-12-06 stsp } else {
5898 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5899 4e97c21c 2020-12-06 stsp if (error == NULL)
5900 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5901 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5902 4e97c21c 2020-12-06 stsp goto done;
5903 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5904 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5905 4e97c21c 2020-12-06 stsp if (error)
5906 4e97c21c 2020-12-06 stsp goto done;
5907 4e97c21c 2020-12-06 stsp }
5908 ffd1d5e5 2018-06-23 stsp
5909 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5910 945f9229 2022-04-16 thomas if (error)
5911 945f9229 2022-04-16 thomas goto done;
5912 945f9229 2022-04-16 thomas
5913 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5914 5221c383 2018-08-01 stsp if (view == NULL) {
5915 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5916 5221c383 2018-08-01 stsp goto done;
5917 5221c383 2018-08-01 stsp }
5918 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5919 ad80ab7b 2018-08-04 stsp if (error)
5920 ad80ab7b 2018-08-04 stsp goto done;
5921 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5922 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
5923 d91faf3b 2020-12-01 naddy in_repo_path);
5924 55cccc34 2020-02-20 stsp if (error)
5925 55cccc34 2020-02-20 stsp goto done;
5926 55cccc34 2020-02-20 stsp }
5927 55cccc34 2020-02-20 stsp
5928 55cccc34 2020-02-20 stsp if (worktree) {
5929 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5930 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5931 55cccc34 2020-02-20 stsp worktree = NULL;
5932 55cccc34 2020-02-20 stsp }
5933 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5934 ffd1d5e5 2018-06-23 stsp done:
5935 52185f70 2019-02-05 stsp free(repo_path);
5936 e4a0e26d 2020-02-20 stsp free(cwd);
5937 ffd1d5e5 2018-06-23 stsp free(commit_id);
5938 4e97c21c 2020-12-06 stsp free(label);
5939 486cd271 2020-12-06 stsp if (ref)
5940 486cd271 2020-12-06 stsp got_ref_close(ref);
5941 1d0f4054 2021-06-17 stsp if (repo) {
5942 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5943 1d0f4054 2021-06-17 stsp if (error == NULL)
5944 1d0f4054 2021-06-17 stsp error = close_err;
5945 1d0f4054 2021-06-17 stsp }
5946 7cd52833 2022-06-23 thomas if (pack_fds) {
5947 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5948 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5949 7cd52833 2022-06-23 thomas if (error == NULL)
5950 7cd52833 2022-06-23 thomas error = pack_err;
5951 7cd52833 2022-06-23 thomas }
5952 51a10b52 2020-12-26 stsp tog_free_refs();
5953 ffd1d5e5 2018-06-23 stsp return error;
5954 6458efa5 2020-11-24 stsp }
5955 6458efa5 2020-11-24 stsp
5956 6458efa5 2020-11-24 stsp static const struct got_error *
5957 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5958 6458efa5 2020-11-24 stsp {
5959 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5960 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5961 6458efa5 2020-11-24 stsp
5962 6458efa5 2020-11-24 stsp s->nrefs = 0;
5963 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5964 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5965 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5966 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5967 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5968 6458efa5 2020-11-24 stsp continue;
5969 6458efa5 2020-11-24 stsp
5970 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5971 6458efa5 2020-11-24 stsp if (re == NULL)
5972 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5973 6458efa5 2020-11-24 stsp
5974 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5975 8924d611 2020-12-26 stsp if (re->ref == NULL)
5976 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5977 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5978 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5979 6458efa5 2020-11-24 stsp }
5980 6458efa5 2020-11-24 stsp
5981 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5982 6458efa5 2020-11-24 stsp return NULL;
5983 6458efa5 2020-11-24 stsp }
5984 6458efa5 2020-11-24 stsp
5985 6458efa5 2020-11-24 stsp void
5986 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5987 6458efa5 2020-11-24 stsp {
5988 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5989 6458efa5 2020-11-24 stsp
5990 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5991 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5992 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5993 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5994 6458efa5 2020-11-24 stsp free(re);
5995 6458efa5 2020-11-24 stsp }
5996 6458efa5 2020-11-24 stsp }
5997 6458efa5 2020-11-24 stsp
5998 6458efa5 2020-11-24 stsp static const struct got_error *
5999 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6000 6458efa5 2020-11-24 stsp {
6001 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6002 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6003 6458efa5 2020-11-24 stsp
6004 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6005 6458efa5 2020-11-24 stsp s->repo = repo;
6006 6458efa5 2020-11-24 stsp
6007 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6008 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6009 6458efa5 2020-11-24 stsp
6010 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6011 6458efa5 2020-11-24 stsp if (err)
6012 6458efa5 2020-11-24 stsp return err;
6013 34ba6917 2020-11-30 stsp
6014 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6015 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6016 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6017 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6018 6458efa5 2020-11-24 stsp if (err)
6019 6458efa5 2020-11-24 stsp goto done;
6020 6458efa5 2020-11-24 stsp
6021 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6022 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6023 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6024 6458efa5 2020-11-24 stsp if (err)
6025 6458efa5 2020-11-24 stsp goto done;
6026 6458efa5 2020-11-24 stsp
6027 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6028 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6029 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6030 2183bbf6 2022-01-23 thomas if (err)
6031 2183bbf6 2022-01-23 thomas goto done;
6032 2183bbf6 2022-01-23 thomas
6033 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6034 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6035 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6036 6458efa5 2020-11-24 stsp if (err)
6037 6458efa5 2020-11-24 stsp goto done;
6038 6458efa5 2020-11-24 stsp }
6039 6458efa5 2020-11-24 stsp
6040 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6041 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6042 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6043 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6044 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6045 6458efa5 2020-11-24 stsp done:
6046 6458efa5 2020-11-24 stsp if (err)
6047 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6048 6458efa5 2020-11-24 stsp return err;
6049 6458efa5 2020-11-24 stsp }
6050 6458efa5 2020-11-24 stsp
6051 6458efa5 2020-11-24 stsp static const struct got_error *
6052 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6053 6458efa5 2020-11-24 stsp {
6054 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6055 6458efa5 2020-11-24 stsp
6056 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6057 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6058 6458efa5 2020-11-24 stsp
6059 6458efa5 2020-11-24 stsp return NULL;
6060 9f7d7167 2018-04-29 stsp }
6061 ce5b7c56 2019-07-09 stsp
6062 6458efa5 2020-11-24 stsp static const struct got_error *
6063 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6064 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6065 6458efa5 2020-11-24 stsp {
6066 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6067 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6068 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6069 6458efa5 2020-11-24 stsp int obj_type;
6070 6458efa5 2020-11-24 stsp
6071 c42c9805 2020-11-24 stsp *commit_id = NULL;
6072 6458efa5 2020-11-24 stsp
6073 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6074 6458efa5 2020-11-24 stsp if (err)
6075 6458efa5 2020-11-24 stsp return err;
6076 6458efa5 2020-11-24 stsp
6077 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6078 6458efa5 2020-11-24 stsp if (err)
6079 6458efa5 2020-11-24 stsp goto done;
6080 6458efa5 2020-11-24 stsp
6081 6458efa5 2020-11-24 stsp switch (obj_type) {
6082 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6083 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6084 6458efa5 2020-11-24 stsp break;
6085 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6086 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6087 6458efa5 2020-11-24 stsp if (err)
6088 6458efa5 2020-11-24 stsp goto done;
6089 c42c9805 2020-11-24 stsp free(obj_id);
6090 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6091 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6092 c42c9805 2020-11-24 stsp if (err)
6093 6458efa5 2020-11-24 stsp goto done;
6094 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6095 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6096 c42c9805 2020-11-24 stsp goto done;
6097 c42c9805 2020-11-24 stsp }
6098 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6099 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6100 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6101 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6102 c42c9805 2020-11-24 stsp goto done;
6103 c42c9805 2020-11-24 stsp }
6104 6458efa5 2020-11-24 stsp break;
6105 6458efa5 2020-11-24 stsp default:
6106 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6107 c42c9805 2020-11-24 stsp break;
6108 6458efa5 2020-11-24 stsp }
6109 6458efa5 2020-11-24 stsp
6110 c42c9805 2020-11-24 stsp done:
6111 c42c9805 2020-11-24 stsp if (tag)
6112 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6113 c42c9805 2020-11-24 stsp if (err) {
6114 c42c9805 2020-11-24 stsp free(*commit_id);
6115 c42c9805 2020-11-24 stsp *commit_id = NULL;
6116 c42c9805 2020-11-24 stsp }
6117 c42c9805 2020-11-24 stsp return err;
6118 c42c9805 2020-11-24 stsp }
6119 c42c9805 2020-11-24 stsp
6120 c42c9805 2020-11-24 stsp static const struct got_error *
6121 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6122 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6123 c42c9805 2020-11-24 stsp {
6124 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6125 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6126 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6127 c42c9805 2020-11-24 stsp
6128 c42c9805 2020-11-24 stsp *new_view = NULL;
6129 c42c9805 2020-11-24 stsp
6130 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6131 c42c9805 2020-11-24 stsp if (err) {
6132 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6133 c42c9805 2020-11-24 stsp return err;
6134 c42c9805 2020-11-24 stsp else
6135 c42c9805 2020-11-24 stsp return NULL;
6136 c42c9805 2020-11-24 stsp }
6137 c42c9805 2020-11-24 stsp
6138 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6139 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6140 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6141 6458efa5 2020-11-24 stsp goto done;
6142 6458efa5 2020-11-24 stsp }
6143 6458efa5 2020-11-24 stsp
6144 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6145 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6146 6458efa5 2020-11-24 stsp done:
6147 6458efa5 2020-11-24 stsp if (err)
6148 6458efa5 2020-11-24 stsp view_close(log_view);
6149 6458efa5 2020-11-24 stsp else
6150 6458efa5 2020-11-24 stsp *new_view = log_view;
6151 c42c9805 2020-11-24 stsp free(commit_id);
6152 6458efa5 2020-11-24 stsp return err;
6153 6458efa5 2020-11-24 stsp }
6154 6458efa5 2020-11-24 stsp
6155 ce5b7c56 2019-07-09 stsp static void
6156 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6157 6458efa5 2020-11-24 stsp {
6158 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6159 34ba6917 2020-11-30 stsp int i = 0;
6160 6458efa5 2020-11-24 stsp
6161 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6162 6458efa5 2020-11-24 stsp return;
6163 6458efa5 2020-11-24 stsp
6164 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6165 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6166 34ba6917 2020-11-30 stsp if (re == NULL)
6167 34ba6917 2020-11-30 stsp break;
6168 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6169 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6170 6458efa5 2020-11-24 stsp }
6171 6458efa5 2020-11-24 stsp }
6172 6458efa5 2020-11-24 stsp
6173 34ba6917 2020-11-30 stsp static void
6174 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6175 6458efa5 2020-11-24 stsp {
6176 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6177 6458efa5 2020-11-24 stsp int n = 0;
6178 6458efa5 2020-11-24 stsp
6179 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6180 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6181 6458efa5 2020-11-24 stsp else
6182 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6183 6458efa5 2020-11-24 stsp
6184 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6185 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6186 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6187 6458efa5 2020-11-24 stsp if (last) {
6188 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6189 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6190 6458efa5 2020-11-24 stsp }
6191 6458efa5 2020-11-24 stsp }
6192 6458efa5 2020-11-24 stsp }
6193 6458efa5 2020-11-24 stsp
6194 6458efa5 2020-11-24 stsp static const struct got_error *
6195 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6196 6458efa5 2020-11-24 stsp {
6197 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6198 6458efa5 2020-11-24 stsp
6199 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6200 6458efa5 2020-11-24 stsp return NULL;
6201 6458efa5 2020-11-24 stsp }
6202 6458efa5 2020-11-24 stsp
6203 6458efa5 2020-11-24 stsp static int
6204 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6205 6458efa5 2020-11-24 stsp {
6206 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6207 6458efa5 2020-11-24 stsp
6208 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6209 6458efa5 2020-11-24 stsp 0) == 0;
6210 6458efa5 2020-11-24 stsp }
6211 6458efa5 2020-11-24 stsp
6212 6458efa5 2020-11-24 stsp static const struct got_error *
6213 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6214 6458efa5 2020-11-24 stsp {
6215 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6216 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6217 6458efa5 2020-11-24 stsp
6218 6458efa5 2020-11-24 stsp if (!view->searching) {
6219 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6220 6458efa5 2020-11-24 stsp return NULL;
6221 6458efa5 2020-11-24 stsp }
6222 6458efa5 2020-11-24 stsp
6223 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6224 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6225 6458efa5 2020-11-24 stsp if (s->selected_entry)
6226 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6227 6458efa5 2020-11-24 stsp else
6228 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6229 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6230 6458efa5 2020-11-24 stsp } else {
6231 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6232 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6233 6458efa5 2020-11-24 stsp else
6234 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6235 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6236 6458efa5 2020-11-24 stsp }
6237 6458efa5 2020-11-24 stsp } else {
6238 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6239 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6240 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6241 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6242 6458efa5 2020-11-24 stsp else
6243 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6244 6458efa5 2020-11-24 stsp }
6245 6458efa5 2020-11-24 stsp
6246 6458efa5 2020-11-24 stsp while (1) {
6247 6458efa5 2020-11-24 stsp if (re == NULL) {
6248 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6249 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6250 6458efa5 2020-11-24 stsp return NULL;
6251 6458efa5 2020-11-24 stsp }
6252 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6253 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6254 6458efa5 2020-11-24 stsp else
6255 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6256 6458efa5 2020-11-24 stsp }
6257 6458efa5 2020-11-24 stsp
6258 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6259 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6260 6458efa5 2020-11-24 stsp s->matched_entry = re;
6261 6458efa5 2020-11-24 stsp break;
6262 6458efa5 2020-11-24 stsp }
6263 6458efa5 2020-11-24 stsp
6264 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6265 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6266 6458efa5 2020-11-24 stsp else
6267 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6268 6458efa5 2020-11-24 stsp }
6269 6458efa5 2020-11-24 stsp
6270 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6271 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6272 6458efa5 2020-11-24 stsp s->selected = 0;
6273 6458efa5 2020-11-24 stsp }
6274 6458efa5 2020-11-24 stsp
6275 6458efa5 2020-11-24 stsp return NULL;
6276 6458efa5 2020-11-24 stsp }
6277 6458efa5 2020-11-24 stsp
6278 6458efa5 2020-11-24 stsp static const struct got_error *
6279 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6280 6458efa5 2020-11-24 stsp {
6281 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6282 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6283 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6284 6458efa5 2020-11-24 stsp char *line = NULL;
6285 6458efa5 2020-11-24 stsp wchar_t *wline;
6286 6458efa5 2020-11-24 stsp struct tog_color *tc;
6287 6458efa5 2020-11-24 stsp int width, n;
6288 6458efa5 2020-11-24 stsp int limit = view->nlines;
6289 6458efa5 2020-11-24 stsp
6290 6458efa5 2020-11-24 stsp werase(view->window);
6291 6458efa5 2020-11-24 stsp
6292 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6293 6458efa5 2020-11-24 stsp
6294 6458efa5 2020-11-24 stsp if (limit == 0)
6295 6458efa5 2020-11-24 stsp return NULL;
6296 6458efa5 2020-11-24 stsp
6297 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6298 6458efa5 2020-11-24 stsp
6299 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6300 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6301 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6302 6458efa5 2020-11-24 stsp
6303 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6304 6458efa5 2020-11-24 stsp if (err) {
6305 6458efa5 2020-11-24 stsp free(line);
6306 6458efa5 2020-11-24 stsp return err;
6307 6458efa5 2020-11-24 stsp }
6308 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6309 6458efa5 2020-11-24 stsp wstandout(view->window);
6310 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6311 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6312 6458efa5 2020-11-24 stsp wstandend(view->window);
6313 6458efa5 2020-11-24 stsp free(wline);
6314 6458efa5 2020-11-24 stsp wline = NULL;
6315 6458efa5 2020-11-24 stsp free(line);
6316 6458efa5 2020-11-24 stsp line = NULL;
6317 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6318 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6319 6458efa5 2020-11-24 stsp if (--limit <= 0)
6320 6458efa5 2020-11-24 stsp return NULL;
6321 6458efa5 2020-11-24 stsp
6322 6458efa5 2020-11-24 stsp n = 0;
6323 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6324 6458efa5 2020-11-24 stsp char *line = NULL;
6325 6458efa5 2020-11-24 stsp
6326 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6327 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6328 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6329 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6330 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6331 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6332 6458efa5 2020-11-24 stsp struct got_object_id *id;
6333 6458efa5 2020-11-24 stsp char *id_str;
6334 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6335 6458efa5 2020-11-24 stsp if (err)
6336 6458efa5 2020-11-24 stsp return err;
6337 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6338 6458efa5 2020-11-24 stsp if (err) {
6339 6458efa5 2020-11-24 stsp free(id);
6340 6458efa5 2020-11-24 stsp return err;
6341 6458efa5 2020-11-24 stsp }
6342 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6343 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6344 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6345 6458efa5 2020-11-24 stsp free(id);
6346 6458efa5 2020-11-24 stsp free(id_str);
6347 6458efa5 2020-11-24 stsp return err;
6348 6458efa5 2020-11-24 stsp }
6349 6458efa5 2020-11-24 stsp free(id);
6350 6458efa5 2020-11-24 stsp free(id_str);
6351 6458efa5 2020-11-24 stsp } else {
6352 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6353 6458efa5 2020-11-24 stsp if (line == NULL)
6354 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6355 6458efa5 2020-11-24 stsp }
6356 6458efa5 2020-11-24 stsp
6357 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6358 6458efa5 2020-11-24 stsp if (err) {
6359 6458efa5 2020-11-24 stsp free(line);
6360 6458efa5 2020-11-24 stsp return err;
6361 6458efa5 2020-11-24 stsp }
6362 6458efa5 2020-11-24 stsp if (n == s->selected) {
6363 6458efa5 2020-11-24 stsp if (view->focussed)
6364 6458efa5 2020-11-24 stsp wstandout(view->window);
6365 6458efa5 2020-11-24 stsp s->selected_entry = re;
6366 6458efa5 2020-11-24 stsp }
6367 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6368 6458efa5 2020-11-24 stsp if (tc)
6369 6458efa5 2020-11-24 stsp wattr_on(view->window,
6370 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6371 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6372 6458efa5 2020-11-24 stsp if (tc)
6373 6458efa5 2020-11-24 stsp wattr_off(view->window,
6374 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6375 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6376 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6377 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6378 6458efa5 2020-11-24 stsp wstandend(view->window);
6379 6458efa5 2020-11-24 stsp free(line);
6380 6458efa5 2020-11-24 stsp free(wline);
6381 6458efa5 2020-11-24 stsp wline = NULL;
6382 6458efa5 2020-11-24 stsp n++;
6383 6458efa5 2020-11-24 stsp s->ndisplayed++;
6384 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6385 6458efa5 2020-11-24 stsp
6386 6458efa5 2020-11-24 stsp limit--;
6387 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6388 6458efa5 2020-11-24 stsp }
6389 6458efa5 2020-11-24 stsp
6390 6458efa5 2020-11-24 stsp view_vborder(view);
6391 6458efa5 2020-11-24 stsp return err;
6392 6458efa5 2020-11-24 stsp }
6393 6458efa5 2020-11-24 stsp
6394 6458efa5 2020-11-24 stsp static const struct got_error *
6395 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6396 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6397 c42c9805 2020-11-24 stsp {
6398 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6399 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6400 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6401 c42c9805 2020-11-24 stsp
6402 c42c9805 2020-11-24 stsp *new_view = NULL;
6403 c42c9805 2020-11-24 stsp
6404 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6405 c42c9805 2020-11-24 stsp if (err) {
6406 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6407 c42c9805 2020-11-24 stsp return err;
6408 c42c9805 2020-11-24 stsp else
6409 c42c9805 2020-11-24 stsp return NULL;
6410 c42c9805 2020-11-24 stsp }
6411 c42c9805 2020-11-24 stsp
6412 c42c9805 2020-11-24 stsp
6413 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6414 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6415 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6416 c42c9805 2020-11-24 stsp goto done;
6417 c42c9805 2020-11-24 stsp }
6418 c42c9805 2020-11-24 stsp
6419 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6420 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6421 c42c9805 2020-11-24 stsp if (err)
6422 c42c9805 2020-11-24 stsp goto done;
6423 c42c9805 2020-11-24 stsp
6424 c42c9805 2020-11-24 stsp *new_view = tree_view;
6425 c42c9805 2020-11-24 stsp done:
6426 c42c9805 2020-11-24 stsp free(commit_id);
6427 c42c9805 2020-11-24 stsp return err;
6428 c42c9805 2020-11-24 stsp }
6429 c42c9805 2020-11-24 stsp static const struct got_error *
6430 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6431 6458efa5 2020-11-24 stsp {
6432 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6433 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6434 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6435 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6436 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6437 6458efa5 2020-11-24 stsp
6438 6458efa5 2020-11-24 stsp switch (ch) {
6439 6458efa5 2020-11-24 stsp case 'i':
6440 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6441 3bfadbd4 2021-11-20 thomas break;
6442 98182bd0 2021-11-20 thomas case 'o':
6443 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6444 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6445 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6446 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6447 c591440f 2021-11-20 thomas if (err)
6448 c591440f 2021-11-20 thomas break;
6449 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6450 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6451 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6452 3bfadbd4 2021-11-20 thomas if (err)
6453 3bfadbd4 2021-11-20 thomas break;
6454 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6455 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6456 6458efa5 2020-11-24 stsp break;
6457 6458efa5 2020-11-24 stsp case KEY_ENTER:
6458 6458efa5 2020-11-24 stsp case '\r':
6459 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6460 6458efa5 2020-11-24 stsp break;
6461 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6462 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6463 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6464 6458efa5 2020-11-24 stsp s->repo);
6465 e78dc838 2020-12-04 stsp view->focussed = 0;
6466 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6467 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6468 6458efa5 2020-11-24 stsp err = view_close_child(view);
6469 6458efa5 2020-11-24 stsp if (err)
6470 6458efa5 2020-11-24 stsp return err;
6471 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6472 e78dc838 2020-12-04 stsp view->focus_child = 1;
6473 6458efa5 2020-11-24 stsp } else
6474 6458efa5 2020-11-24 stsp *new_view = log_view;
6475 6458efa5 2020-11-24 stsp break;
6476 c42c9805 2020-11-24 stsp case 't':
6477 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6478 c42c9805 2020-11-24 stsp break;
6479 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6480 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6481 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6482 c42c9805 2020-11-24 stsp s->repo);
6483 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6484 c42c9805 2020-11-24 stsp break;
6485 e78dc838 2020-12-04 stsp view->focussed = 0;
6486 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6487 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6488 c42c9805 2020-11-24 stsp err = view_close_child(view);
6489 c42c9805 2020-11-24 stsp if (err)
6490 c42c9805 2020-11-24 stsp return err;
6491 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6492 e78dc838 2020-12-04 stsp view->focus_child = 1;
6493 c42c9805 2020-11-24 stsp } else
6494 c42c9805 2020-11-24 stsp *new_view = tree_view;
6495 c42c9805 2020-11-24 stsp break;
6496 e4526bf5 2021-09-03 naddy case 'g':
6497 e4526bf5 2021-09-03 naddy case KEY_HOME:
6498 e4526bf5 2021-09-03 naddy s->selected = 0;
6499 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6500 e4526bf5 2021-09-03 naddy break;
6501 e4526bf5 2021-09-03 naddy case 'G':
6502 e4526bf5 2021-09-03 naddy case KEY_END:
6503 e4526bf5 2021-09-03 naddy s->selected = 0;
6504 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6505 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6506 e4526bf5 2021-09-03 naddy if (re == NULL)
6507 e4526bf5 2021-09-03 naddy break;
6508 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6509 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6510 e4526bf5 2021-09-03 naddy }
6511 e4526bf5 2021-09-03 naddy if (n > 0)
6512 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6513 e4526bf5 2021-09-03 naddy break;
6514 6458efa5 2020-11-24 stsp case 'k':
6515 6458efa5 2020-11-24 stsp case KEY_UP:
6516 f7140bf5 2021-10-17 thomas case CTRL('p'):
6517 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6518 6458efa5 2020-11-24 stsp s->selected--;
6519 6458efa5 2020-11-24 stsp break;
6520 34ba6917 2020-11-30 stsp }
6521 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6522 6458efa5 2020-11-24 stsp break;
6523 70f17a53 2022-06-13 thomas case CTRL('u'):
6524 23427b14 2022-06-23 thomas case 'u':
6525 70f17a53 2022-06-13 thomas nscroll /= 2;
6526 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6527 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6528 6458efa5 2020-11-24 stsp case CTRL('b'):
6529 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6530 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6531 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6532 6458efa5 2020-11-24 stsp break;
6533 6458efa5 2020-11-24 stsp case 'j':
6534 6458efa5 2020-11-24 stsp case KEY_DOWN:
6535 f7140bf5 2021-10-17 thomas case CTRL('n'):
6536 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6537 6458efa5 2020-11-24 stsp s->selected++;
6538 6458efa5 2020-11-24 stsp break;
6539 6458efa5 2020-11-24 stsp }
6540 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6541 6458efa5 2020-11-24 stsp /* can't scroll any further */
6542 6458efa5 2020-11-24 stsp break;
6543 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6544 6458efa5 2020-11-24 stsp break;
6545 70f17a53 2022-06-13 thomas case CTRL('d'):
6546 23427b14 2022-06-23 thomas case 'd':
6547 70f17a53 2022-06-13 thomas nscroll /= 2;
6548 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6549 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6550 6458efa5 2020-11-24 stsp case CTRL('f'):
6551 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6552 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6553 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6554 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6555 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6556 6458efa5 2020-11-24 stsp break;
6557 6458efa5 2020-11-24 stsp }
6558 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6559 6458efa5 2020-11-24 stsp break;
6560 6458efa5 2020-11-24 stsp case CTRL('l'):
6561 8924d611 2020-12-26 stsp tog_free_refs();
6562 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6563 8924d611 2020-12-26 stsp if (err)
6564 8924d611 2020-12-26 stsp break;
6565 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6566 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6567 6458efa5 2020-11-24 stsp break;
6568 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6569 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6570 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6571 6458efa5 2020-11-24 stsp break;
6572 6458efa5 2020-11-24 stsp default:
6573 6458efa5 2020-11-24 stsp break;
6574 6458efa5 2020-11-24 stsp }
6575 6458efa5 2020-11-24 stsp
6576 6458efa5 2020-11-24 stsp return err;
6577 6458efa5 2020-11-24 stsp }
6578 6458efa5 2020-11-24 stsp
6579 6458efa5 2020-11-24 stsp __dead static void
6580 6458efa5 2020-11-24 stsp usage_ref(void)
6581 6458efa5 2020-11-24 stsp {
6582 6458efa5 2020-11-24 stsp endwin();
6583 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6584 6458efa5 2020-11-24 stsp getprogname());
6585 6458efa5 2020-11-24 stsp exit(1);
6586 6458efa5 2020-11-24 stsp }
6587 6458efa5 2020-11-24 stsp
6588 6458efa5 2020-11-24 stsp static const struct got_error *
6589 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6590 6458efa5 2020-11-24 stsp {
6591 6458efa5 2020-11-24 stsp const struct got_error *error;
6592 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6593 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6594 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6595 6458efa5 2020-11-24 stsp int ch;
6596 6458efa5 2020-11-24 stsp struct tog_view *view;
6597 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6598 6458efa5 2020-11-24 stsp
6599 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6600 6458efa5 2020-11-24 stsp switch (ch) {
6601 6458efa5 2020-11-24 stsp case 'r':
6602 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6603 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6604 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6605 6458efa5 2020-11-24 stsp optarg);
6606 6458efa5 2020-11-24 stsp break;
6607 6458efa5 2020-11-24 stsp default:
6608 e99e2d15 2020-11-24 naddy usage_ref();
6609 6458efa5 2020-11-24 stsp /* NOTREACHED */
6610 6458efa5 2020-11-24 stsp }
6611 6458efa5 2020-11-24 stsp }
6612 6458efa5 2020-11-24 stsp
6613 6458efa5 2020-11-24 stsp argc -= optind;
6614 6458efa5 2020-11-24 stsp argv += optind;
6615 6458efa5 2020-11-24 stsp
6616 6458efa5 2020-11-24 stsp if (argc > 1)
6617 e99e2d15 2020-11-24 naddy usage_ref();
6618 7cd52833 2022-06-23 thomas
6619 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6620 7cd52833 2022-06-23 thomas if (error != NULL)
6621 7cd52833 2022-06-23 thomas goto done;
6622 6458efa5 2020-11-24 stsp
6623 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6624 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6625 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6626 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6627 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6628 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6629 c156c7a4 2020-12-18 stsp goto done;
6630 6458efa5 2020-11-24 stsp if (worktree)
6631 6458efa5 2020-11-24 stsp repo_path =
6632 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6633 6458efa5 2020-11-24 stsp else
6634 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6635 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6636 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6637 c156c7a4 2020-12-18 stsp goto done;
6638 c156c7a4 2020-12-18 stsp }
6639 6458efa5 2020-11-24 stsp }
6640 6458efa5 2020-11-24 stsp
6641 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 6458efa5 2020-11-24 stsp if (error != NULL)
6643 6458efa5 2020-11-24 stsp goto done;
6644 6458efa5 2020-11-24 stsp
6645 6458efa5 2020-11-24 stsp init_curses();
6646 6458efa5 2020-11-24 stsp
6647 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6648 51a10b52 2020-12-26 stsp if (error)
6649 51a10b52 2020-12-26 stsp goto done;
6650 51a10b52 2020-12-26 stsp
6651 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6652 6458efa5 2020-11-24 stsp if (error)
6653 6458efa5 2020-11-24 stsp goto done;
6654 6458efa5 2020-11-24 stsp
6655 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6656 6458efa5 2020-11-24 stsp if (view == NULL) {
6657 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6658 6458efa5 2020-11-24 stsp goto done;
6659 6458efa5 2020-11-24 stsp }
6660 6458efa5 2020-11-24 stsp
6661 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6662 6458efa5 2020-11-24 stsp if (error)
6663 6458efa5 2020-11-24 stsp goto done;
6664 6458efa5 2020-11-24 stsp
6665 6458efa5 2020-11-24 stsp if (worktree) {
6666 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6667 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6668 6458efa5 2020-11-24 stsp worktree = NULL;
6669 6458efa5 2020-11-24 stsp }
6670 6458efa5 2020-11-24 stsp error = view_loop(view);
6671 6458efa5 2020-11-24 stsp done:
6672 6458efa5 2020-11-24 stsp free(repo_path);
6673 6458efa5 2020-11-24 stsp free(cwd);
6674 1d0f4054 2021-06-17 stsp if (repo) {
6675 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6676 1d0f4054 2021-06-17 stsp if (close_err)
6677 1d0f4054 2021-06-17 stsp error = close_err;
6678 1d0f4054 2021-06-17 stsp }
6679 7cd52833 2022-06-23 thomas if (pack_fds) {
6680 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6681 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6682 7cd52833 2022-06-23 thomas if (error == NULL)
6683 7cd52833 2022-06-23 thomas error = pack_err;
6684 7cd52833 2022-06-23 thomas }
6685 51a10b52 2020-12-26 stsp tog_free_refs();
6686 6458efa5 2020-11-24 stsp return error;
6687 6458efa5 2020-11-24 stsp }
6688 6458efa5 2020-11-24 stsp
6689 6458efa5 2020-11-24 stsp static void
6690 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6691 ce5b7c56 2019-07-09 stsp {
6692 6059809a 2020-12-17 stsp size_t i;
6693 9f7d7167 2018-04-29 stsp
6694 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6695 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6696 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6697 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6698 ce5b7c56 2019-07-09 stsp }
6699 6879ba42 2020-10-01 naddy fputc('\n', fp);
6700 ce5b7c56 2019-07-09 stsp }
6701 ce5b7c56 2019-07-09 stsp
6702 4ed7e80c 2018-05-20 stsp __dead static void
6703 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6704 9f7d7167 2018-04-29 stsp {
6705 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6706 6879ba42 2020-10-01 naddy
6707 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6708 6879ba42 2020-10-01 naddy getprogname());
6709 6879ba42 2020-10-01 naddy if (hflag) {
6710 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6711 6879ba42 2020-10-01 naddy list_commands(fp);
6712 ee85c5e8 2020-02-29 stsp }
6713 6879ba42 2020-10-01 naddy exit(status);
6714 9f7d7167 2018-04-29 stsp }
6715 9f7d7167 2018-04-29 stsp
6716 c2301be8 2018-04-30 stsp static char **
6717 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6718 c2301be8 2018-04-30 stsp {
6719 ee85c5e8 2020-02-29 stsp va_list ap;
6720 c2301be8 2018-04-30 stsp char **argv;
6721 ee85c5e8 2020-02-29 stsp int i;
6722 c2301be8 2018-04-30 stsp
6723 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6724 ee85c5e8 2020-02-29 stsp
6725 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6726 c2301be8 2018-04-30 stsp if (argv == NULL)
6727 c2301be8 2018-04-30 stsp err(1, "calloc");
6728 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6729 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6730 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6731 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6732 c2301be8 2018-04-30 stsp }
6733 c2301be8 2018-04-30 stsp
6734 ee85c5e8 2020-02-29 stsp va_end(ap);
6735 c2301be8 2018-04-30 stsp return argv;
6736 ee85c5e8 2020-02-29 stsp }
6737 ee85c5e8 2020-02-29 stsp
6738 ee85c5e8 2020-02-29 stsp /*
6739 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6740 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6741 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6742 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6743 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6744 ee85c5e8 2020-02-29 stsp */
6745 ee85c5e8 2020-02-29 stsp static const struct got_error *
6746 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6747 ee85c5e8 2020-02-29 stsp {
6748 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6749 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6750 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6751 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6752 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6753 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6754 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6755 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6756 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6757 84de9106 2020-12-26 stsp
6758 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6759 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6760 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6761 ee85c5e8 2020-02-29 stsp
6762 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6763 7cd52833 2022-06-23 thomas if (error != NULL)
6764 7cd52833 2022-06-23 thomas goto done;
6765 7cd52833 2022-06-23 thomas
6766 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6767 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6768 ee85c5e8 2020-02-29 stsp goto done;
6769 ee85c5e8 2020-02-29 stsp
6770 ee85c5e8 2020-02-29 stsp if (worktree)
6771 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6772 ee85c5e8 2020-02-29 stsp else
6773 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6774 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6775 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6776 ee85c5e8 2020-02-29 stsp goto done;
6777 ee85c5e8 2020-02-29 stsp }
6778 ee85c5e8 2020-02-29 stsp
6779 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6780 ee85c5e8 2020-02-29 stsp if (error != NULL)
6781 ee85c5e8 2020-02-29 stsp goto done;
6782 ee85c5e8 2020-02-29 stsp
6783 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6784 ee85c5e8 2020-02-29 stsp repo, worktree);
6785 ee85c5e8 2020-02-29 stsp if (error)
6786 ee85c5e8 2020-02-29 stsp goto done;
6787 ee85c5e8 2020-02-29 stsp
6788 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6789 84de9106 2020-12-26 stsp if (error)
6790 84de9106 2020-12-26 stsp goto done;
6791 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6792 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6793 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6794 ee85c5e8 2020-02-29 stsp if (error)
6795 ee85c5e8 2020-02-29 stsp goto done;
6796 ee85c5e8 2020-02-29 stsp
6797 ee85c5e8 2020-02-29 stsp if (worktree) {
6798 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6799 ee85c5e8 2020-02-29 stsp worktree = NULL;
6800 ee85c5e8 2020-02-29 stsp }
6801 ee85c5e8 2020-02-29 stsp
6802 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6803 945f9229 2022-04-16 thomas if (error)
6804 945f9229 2022-04-16 thomas goto done;
6805 945f9229 2022-04-16 thomas
6806 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6807 ee85c5e8 2020-02-29 stsp if (error) {
6808 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6809 ee85c5e8 2020-02-29 stsp goto done;
6810 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6811 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6812 6879ba42 2020-10-01 naddy usage(1, 1);
6813 ee85c5e8 2020-02-29 stsp /* not reached */
6814 ee85c5e8 2020-02-29 stsp }
6815 ee85c5e8 2020-02-29 stsp
6816 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6817 1d0f4054 2021-06-17 stsp if (error == NULL)
6818 1d0f4054 2021-06-17 stsp error = close_err;
6819 ee85c5e8 2020-02-29 stsp repo = NULL;
6820 ee85c5e8 2020-02-29 stsp
6821 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6822 ee85c5e8 2020-02-29 stsp if (error)
6823 ee85c5e8 2020-02-29 stsp goto done;
6824 ee85c5e8 2020-02-29 stsp
6825 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6826 ee85c5e8 2020-02-29 stsp argc = 4;
6827 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6828 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6829 ee85c5e8 2020-02-29 stsp done:
6830 1d0f4054 2021-06-17 stsp if (repo) {
6831 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6832 1d0f4054 2021-06-17 stsp if (error == NULL)
6833 1d0f4054 2021-06-17 stsp error = close_err;
6834 1d0f4054 2021-06-17 stsp }
6835 945f9229 2022-04-16 thomas if (commit)
6836 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6837 ee85c5e8 2020-02-29 stsp if (worktree)
6838 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6839 7cd52833 2022-06-23 thomas if (pack_fds) {
6840 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6841 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6842 7cd52833 2022-06-23 thomas if (error == NULL)
6843 7cd52833 2022-06-23 thomas error = pack_err;
6844 7cd52833 2022-06-23 thomas }
6845 ee85c5e8 2020-02-29 stsp free(id);
6846 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6847 ee85c5e8 2020-02-29 stsp free(commit_id);
6848 ee85c5e8 2020-02-29 stsp free(cwd);
6849 ee85c5e8 2020-02-29 stsp free(repo_path);
6850 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6851 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6852 ee85c5e8 2020-02-29 stsp int i;
6853 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6854 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6855 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6856 ee85c5e8 2020-02-29 stsp }
6857 87670572 2020-12-26 naddy tog_free_refs();
6858 ee85c5e8 2020-02-29 stsp return error;
6859 c2301be8 2018-04-30 stsp }
6860 c2301be8 2018-04-30 stsp
6861 9f7d7167 2018-04-29 stsp int
6862 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6863 9f7d7167 2018-04-29 stsp {
6864 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6865 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6866 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6867 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6868 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6869 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6870 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6871 83cd27f8 2020-01-13 stsp };
6872 9f7d7167 2018-04-29 stsp
6873 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6874 9f7d7167 2018-04-29 stsp
6875 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6876 9f7d7167 2018-04-29 stsp switch (ch) {
6877 9f7d7167 2018-04-29 stsp case 'h':
6878 9f7d7167 2018-04-29 stsp hflag = 1;
6879 9f7d7167 2018-04-29 stsp break;
6880 53ccebc2 2019-07-30 stsp case 'V':
6881 53ccebc2 2019-07-30 stsp Vflag = 1;
6882 53ccebc2 2019-07-30 stsp break;
6883 9f7d7167 2018-04-29 stsp default:
6884 6879ba42 2020-10-01 naddy usage(hflag, 1);
6885 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6886 9f7d7167 2018-04-29 stsp }
6887 9f7d7167 2018-04-29 stsp }
6888 9f7d7167 2018-04-29 stsp
6889 9f7d7167 2018-04-29 stsp argc -= optind;
6890 9f7d7167 2018-04-29 stsp argv += optind;
6891 9814e6a3 2020-09-27 naddy optind = 1;
6892 c2301be8 2018-04-30 stsp optreset = 1;
6893 9f7d7167 2018-04-29 stsp
6894 53ccebc2 2019-07-30 stsp if (Vflag) {
6895 53ccebc2 2019-07-30 stsp got_version_print_str();
6896 6879ba42 2020-10-01 naddy return 0;
6897 53ccebc2 2019-07-30 stsp }
6898 4010e238 2020-12-04 stsp
6899 4010e238 2020-12-04 stsp #ifndef PROFILE
6900 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6901 4010e238 2020-12-04 stsp NULL) == -1)
6902 4010e238 2020-12-04 stsp err(1, "pledge");
6903 4010e238 2020-12-04 stsp #endif
6904 53ccebc2 2019-07-30 stsp
6905 c2301be8 2018-04-30 stsp if (argc == 0) {
6906 f29d3e89 2018-06-23 stsp if (hflag)
6907 6879ba42 2020-10-01 naddy usage(hflag, 0);
6908 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6909 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6910 c2301be8 2018-04-30 stsp argc = 1;
6911 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6912 c2301be8 2018-04-30 stsp } else {
6913 6059809a 2020-12-17 stsp size_t i;
6914 9f7d7167 2018-04-29 stsp
6915 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6916 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6917 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6918 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6919 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6920 9f7d7167 2018-04-29 stsp break;
6921 9f7d7167 2018-04-29 stsp }
6922 9f7d7167 2018-04-29 stsp }
6923 ee85c5e8 2020-02-29 stsp }
6924 3642c4c6 2019-07-09 stsp
6925 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6926 ee85c5e8 2020-02-29 stsp if (argc != 1)
6927 6879ba42 2020-10-01 naddy usage(0, 1);
6928 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6929 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6930 ee85c5e8 2020-02-29 stsp } else {
6931 ee85c5e8 2020-02-29 stsp if (hflag)
6932 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6933 ee85c5e8 2020-02-29 stsp else
6934 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6935 9f7d7167 2018-04-29 stsp }
6936 9f7d7167 2018-04-29 stsp
6937 9f7d7167 2018-04-29 stsp endwin();
6938 b46c1e04 2020-09-20 naddy putchar('\n');
6939 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6940 a2f4a359 2020-02-28 stsp int i;
6941 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6942 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6943 a2f4a359 2020-02-28 stsp free(cmd_argv);
6944 a2f4a359 2020-02-28 stsp }
6945 a2f4a359 2020-02-28 stsp
6946 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6947 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6948 9f7d7167 2018-04-29 stsp return 0;
6949 9f7d7167 2018-04-29 stsp }