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