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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.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 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 11b20872 2019-11-08 stsp
322 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
323 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
324 3dbaef42 2020-11-24 stsp const char *label1, *label2;
325 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
326 19a6a6b5 2022-07-01 thomas int fd1, fd2;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 bddb1296 2019-11-08 stsp struct tog_colors colors;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey off_t *line_offsets;
337 f44b1f58 2020-02-02 tracey int matched_line;
338 f44b1f58 2020-02-02 tracey int selected_line;
339 15a087fe 2019-02-21 stsp
340 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
341 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
342 b01e7d3b 2018-08-04 stsp };
343 b01e7d3b 2018-08-04 stsp
344 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
346 1a76625f 2018-10-22 stsp
347 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
348 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
349 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
350 1a76625f 2018-10-22 stsp int commits_needed;
351 fb280deb 2021-08-30 stsp int load_all;
352 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
353 1a76625f 2018-10-22 stsp struct commit_queue *commits;
354 1a76625f 2018-10-22 stsp const char *in_repo_path;
355 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
356 1a76625f 2018-10-22 stsp struct got_repository *repo;
357 1af5eddf 2022-06-23 thomas int *pack_fds;
358 1a76625f 2018-10-22 stsp int log_complete;
359 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
362 13add988 2019-10-15 stsp int *searching;
363 13add988 2019-10-15 stsp int *search_next_done;
364 13add988 2019-10-15 stsp regex_t *regex;
365 1a76625f 2018-10-22 stsp };
366 1a76625f 2018-10-22 stsp
367 1a76625f 2018-10-22 stsp struct tog_log_view_state {
368 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
372 b01e7d3b 2018-08-04 stsp int selected;
373 b01e7d3b 2018-08-04 stsp char *in_repo_path;
374 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
375 b672a97a 2020-01-27 stsp int log_branches;
376 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
377 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
378 1a76625f 2018-10-22 stsp sig_atomic_t quit;
379 1a76625f 2018-10-22 stsp pthread_t thread;
380 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
381 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
382 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
383 11b20872 2019-11-08 stsp struct tog_colors colors;
384 f69c5a46 2022-07-19 thomas int use_committer;
385 ba4f502b 2018-08-04 stsp };
386 11b20872 2019-11-08 stsp
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
402 ba4f502b 2018-08-04 stsp
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
406 e9424729 2018-08-04 stsp
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
409 e9424729 2018-08-04 stsp int *quit;
410 e9424729 2018-08-04 stsp };
411 e9424729 2018-08-04 stsp
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
419 e9424729 2018-08-04 stsp };
420 e9424729 2018-08-04 stsp
421 e9424729 2018-08-04 stsp struct tog_blame {
422 e9424729 2018-08-04 stsp FILE *f;
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
432 e9424729 2018-08-04 stsp };
433 e9424729 2018-08-04 stsp
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 4fc71f3b 2022-07-12 thomas int last_diffed_line;
439 7cbe629d 2018-08-04 stsp int blame_complete;
440 e5a0f69f 2018-08-18 stsp int eof;
441 e5a0f69f 2018-08-18 stsp int done;
442 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
443 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
444 e5a0f69f 2018-08-18 stsp char *path;
445 7cbe629d 2018-08-04 stsp struct got_repository *repo;
446 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 ad80ab7b 2018-08-04 stsp
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
476 6458efa5 2020-11-24 stsp };
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
481 6458efa5 2020-11-24 stsp int idx;
482 6458efa5 2020-11-24 stsp };
483 6458efa5 2020-11-24 stsp
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
495 7cbe629d 2018-08-04 stsp };
496 7cbe629d 2018-08-04 stsp
497 669b5ffa 2018-10-07 stsp /*
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
499 669b5ffa 2018-10-07 stsp *
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
503 669b5ffa 2018-10-07 stsp *
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
510 669b5ffa 2018-10-07 stsp *
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
513 669b5ffa 2018-10-07 stsp */
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
515 669b5ffa 2018-10-07 stsp
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
526 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
527 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
528 9970f7fc 2020-12-03 stsp int dying;
529 669b5ffa 2018-10-07 stsp struct tog_view *parent;
530 669b5ffa 2018-10-07 stsp struct tog_view *child;
531 5dc9f4bc 2018-08-04 stsp
532 e78dc838 2020-12-04 stsp /*
533 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
534 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
535 e78dc838 2020-12-04 stsp * between parent and child.
536 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
537 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
538 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
539 e78dc838 2020-12-04 stsp * situations.
540 e78dc838 2020-12-04 stsp */
541 e78dc838 2020-12-04 stsp int focus_child;
542 e78dc838 2020-12-04 stsp
543 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
544 5dc9f4bc 2018-08-04 stsp /* type-specific state */
545 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
546 5dc9f4bc 2018-08-04 stsp union {
547 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
548 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
549 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
550 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
551 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
552 5dc9f4bc 2018-08-04 stsp } state;
553 e5a0f69f 2018-08-18 stsp
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
556 e78dc838 2020-12-04 stsp struct tog_view *, int);
557 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
558 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
559 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
560 60493ae3 2019-06-20 stsp
561 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
563 c0c4acc8 2021-01-24 stsp int search_started;
564 60493ae3 2019-06-20 stsp int searching;
565 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
567 60493ae3 2019-06-20 stsp int search_next_done;
568 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
570 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
571 1803e47f 2019-06-22 stsp regex_t regex;
572 41605754 2020-11-12 stsp regmatch_t regmatch;
573 cc3c9aac 2018-08-01 stsp };
574 cd0acaa7 2018-05-20 stsp
575 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
576 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
577 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
578 78756c87 2020-11-24 stsp struct got_repository *);
579 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
584 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp
587 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
588 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
589 78756c87 2020-11-24 stsp const char *, const char *, int);
590 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
594 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
595 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp
598 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
599 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
600 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
602 e78dc838 2020-12-04 stsp struct tog_view *, int);
603 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
604 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
605 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp
608 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
609 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
610 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
611 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
612 e78dc838 2020-12-04 stsp struct tog_view *, int);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
614 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp
617 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
618 6458efa5 2020-11-24 stsp struct got_repository *);
619 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
623 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
625 25791caa 2018-10-24 stsp
626 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
627 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
628 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
629 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
631 25791caa 2018-10-24 stsp
632 25791caa 2018-10-24 stsp static void
633 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
634 25791caa 2018-10-24 stsp {
635 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
636 83baff54 2019-08-12 stsp }
637 83baff54 2019-08-12 stsp
638 83baff54 2019-08-12 stsp static void
639 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
640 83baff54 2019-08-12 stsp {
641 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
642 25791caa 2018-10-24 stsp }
643 26ed57b2 2018-05-19 stsp
644 61266923 2020-01-14 stsp static void
645 61266923 2020-01-14 stsp tog_sigcont(int signo)
646 61266923 2020-01-14 stsp {
647 61266923 2020-01-14 stsp tog_sigcont_received = 1;
648 61266923 2020-01-14 stsp }
649 61266923 2020-01-14 stsp
650 296152d1 2022-05-31 thomas static void
651 296152d1 2022-05-31 thomas tog_sigint(int signo)
652 296152d1 2022-05-31 thomas {
653 296152d1 2022-05-31 thomas tog_sigint_received = 1;
654 296152d1 2022-05-31 thomas }
655 296152d1 2022-05-31 thomas
656 296152d1 2022-05-31 thomas static void
657 296152d1 2022-05-31 thomas tog_sigterm(int signo)
658 296152d1 2022-05-31 thomas {
659 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
660 296152d1 2022-05-31 thomas }
661 296152d1 2022-05-31 thomas
662 296152d1 2022-05-31 thomas static int
663 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
664 296152d1 2022-05-31 thomas {
665 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
666 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
667 296152d1 2022-05-31 thomas }
668 296152d1 2022-05-31 thomas
669 e5a0f69f 2018-08-18 stsp static const struct got_error *
670 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
671 ea5e7bb5 2018-08-01 stsp {
672 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
673 e5a0f69f 2018-08-18 stsp
674 669b5ffa 2018-10-07 stsp if (view->child) {
675 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
676 669b5ffa 2018-10-07 stsp view->child = NULL;
677 669b5ffa 2018-10-07 stsp }
678 e5a0f69f 2018-08-18 stsp if (view->close)
679 e5a0f69f 2018-08-18 stsp err = view->close(view);
680 ea5e7bb5 2018-08-01 stsp if (view->panel)
681 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
682 ea5e7bb5 2018-08-01 stsp if (view->window)
683 ea5e7bb5 2018-08-01 stsp delwin(view->window);
684 ea5e7bb5 2018-08-01 stsp free(view);
685 f2d749db 2022-07-12 thomas return err ? err : child_err;
686 ea5e7bb5 2018-08-01 stsp }
687 ea5e7bb5 2018-08-01 stsp
688 ea5e7bb5 2018-08-01 stsp static struct tog_view *
689 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
690 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
691 ea5e7bb5 2018-08-01 stsp {
692 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
693 ea5e7bb5 2018-08-01 stsp
694 ea5e7bb5 2018-08-01 stsp if (view == NULL)
695 ea5e7bb5 2018-08-01 stsp return NULL;
696 ea5e7bb5 2018-08-01 stsp
697 d6b05b5b 2018-08-04 stsp view->type = type;
698 f7d12f7e 2018-08-01 stsp view->lines = LINES;
699 f7d12f7e 2018-08-01 stsp view->cols = COLS;
700 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
701 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
702 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
703 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
704 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
705 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
706 96a765a8 2018-08-04 stsp view_close(view);
707 ea5e7bb5 2018-08-01 stsp return NULL;
708 ea5e7bb5 2018-08-01 stsp }
709 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
710 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
711 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
712 96a765a8 2018-08-04 stsp view_close(view);
713 ea5e7bb5 2018-08-01 stsp return NULL;
714 ea5e7bb5 2018-08-01 stsp }
715 ea5e7bb5 2018-08-01 stsp
716 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
717 ea5e7bb5 2018-08-01 stsp return view;
718 cdf1ee82 2018-08-01 stsp }
719 cdf1ee82 2018-08-01 stsp
720 0cf4efb1 2018-09-29 stsp static int
721 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
722 0cf4efb1 2018-09-29 stsp {
723 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
724 0cf4efb1 2018-09-29 stsp return 0;
725 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
726 5c60c32a 2018-10-18 stsp }
727 5c60c32a 2018-10-18 stsp
728 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
729 a5d43cac 2022-07-01 thomas static int
730 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
731 a5d43cac 2022-07-01 thomas {
732 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
733 a5d43cac 2022-07-01 thomas }
734 a5d43cac 2022-07-01 thomas
735 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
736 5c60c32a 2018-10-18 stsp
737 5c60c32a 2018-10-18 stsp static const struct got_error *
738 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
739 5c60c32a 2018-10-18 stsp {
740 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
741 5c60c32a 2018-10-18 stsp
742 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
744 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
745 53d2bdd3 2022-07-10 thomas else
746 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
747 a5d43cac 2022-07-01 thomas view->begin_x = 0;
748 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
749 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
750 53d2bdd3 2022-07-10 thomas view->cols > 119)
751 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
752 53d2bdd3 2022-07-10 thomas else
753 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
754 a5d43cac 2022-07-01 thomas view->begin_y = 0;
755 a5d43cac 2022-07-01 thomas }
756 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
757 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
758 5c60c32a 2018-10-18 stsp view->lines = LINES;
759 5c60c32a 2018-10-18 stsp view->cols = COLS;
760 5c60c32a 2018-10-18 stsp err = view_resize(view);
761 5c60c32a 2018-10-18 stsp if (err)
762 5c60c32a 2018-10-18 stsp return err;
763 5c60c32a 2018-10-18 stsp
764 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
766 a5d43cac 2022-07-01 thomas
767 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
769 5c60c32a 2018-10-18 stsp
770 5c60c32a 2018-10-18 stsp return NULL;
771 5c60c32a 2018-10-18 stsp }
772 5c60c32a 2018-10-18 stsp
773 5c60c32a 2018-10-18 stsp static const struct got_error *
774 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
775 5c60c32a 2018-10-18 stsp {
776 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
777 5c60c32a 2018-10-18 stsp
778 5c60c32a 2018-10-18 stsp view->begin_x = 0;
779 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
780 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
781 5c60c32a 2018-10-18 stsp view->ncols = COLS;
782 5c60c32a 2018-10-18 stsp view->lines = LINES;
783 5c60c32a 2018-10-18 stsp view->cols = COLS;
784 5c60c32a 2018-10-18 stsp err = view_resize(view);
785 5c60c32a 2018-10-18 stsp if (err)
786 5c60c32a 2018-10-18 stsp return err;
787 5c60c32a 2018-10-18 stsp
788 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
790 5c60c32a 2018-10-18 stsp
791 5c60c32a 2018-10-18 stsp return NULL;
792 0cf4efb1 2018-09-29 stsp }
793 0cf4efb1 2018-09-29 stsp
794 5c60c32a 2018-10-18 stsp static int
795 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
796 5c60c32a 2018-10-18 stsp {
797 5c60c32a 2018-10-18 stsp return view->parent == NULL;
798 5c60c32a 2018-10-18 stsp }
799 5c60c32a 2018-10-18 stsp
800 b65b3ea0 2022-06-23 thomas static int
801 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
802 b65b3ea0 2022-06-23 thomas {
803 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
804 e44940c3 2022-07-01 thomas }
805 e44940c3 2022-07-01 thomas
806 e44940c3 2022-07-01 thomas static int
807 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
808 e44940c3 2022-07-01 thomas {
809 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
810 b65b3ea0 2022-06-23 thomas }
811 b65b3ea0 2022-06-23 thomas
812 444d5325 2022-07-03 thomas static int
813 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
814 444d5325 2022-07-03 thomas {
815 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
817 444d5325 2022-07-03 thomas }
818 444d5325 2022-07-03 thomas
819 a5d43cac 2022-07-01 thomas static void
820 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
821 a5d43cac 2022-07-01 thomas {
822 a5d43cac 2022-07-01 thomas PANEL *panel;
823 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
824 b65b3ea0 2022-06-23 thomas
825 a5d43cac 2022-07-01 thomas if (view->parent)
826 a5d43cac 2022-07-01 thomas return view_border(view->parent);
827 a5d43cac 2022-07-01 thomas
828 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
829 a5d43cac 2022-07-01 thomas if (panel == NULL)
830 a5d43cac 2022-07-01 thomas return;
831 a5d43cac 2022-07-01 thomas
832 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
833 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
835 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
836 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
837 a5d43cac 2022-07-01 thomas else
838 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
840 a5d43cac 2022-07-01 thomas }
841 a5d43cac 2022-07-01 thomas
842 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
844 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
846 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
847 a5d43cac 2022-07-01 thomas
848 4d8c2215 2018-08-19 stsp static const struct got_error *
849 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
850 f7d12f7e 2018-08-01 stsp {
851 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
852 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
853 f7d12f7e 2018-08-01 stsp
854 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
855 a5d43cac 2022-07-01 thomas
856 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
857 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
858 0cf4efb1 2018-09-29 stsp else
859 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
860 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
861 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
862 0cf4efb1 2018-09-29 stsp else
863 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
864 6d0fee91 2018-08-01 stsp
865 4918811f 2022-07-01 thomas if (view->child) {
866 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
867 a5d43cac 2022-07-01 thomas
868 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
869 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
870 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
872 40236d76 2022-06-23 thomas ncols = COLS;
873 40236d76 2022-06-23 thomas
874 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
875 5c60c32a 2018-10-18 stsp if (view->child->focussed)
876 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
877 5c60c32a 2018-10-18 stsp else
878 5c60c32a 2018-10-18 stsp show_panel(view->panel);
879 5c60c32a 2018-10-18 stsp } else {
880 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
881 40236d76 2022-06-23 thomas
882 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
883 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
884 a5d43cac 2022-07-01 thomas }
885 a5d43cac 2022-07-01 thomas /*
886 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
887 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
888 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
889 a5d43cac 2022-07-01 thomas */
890 a5d43cac 2022-07-01 thomas if (hs) {
891 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
892 a5d43cac 2022-07-01 thomas if (err)
893 a5d43cac 2022-07-01 thomas return err;
894 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
895 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
896 a5d43cac 2022-07-01 thomas if (err)
897 a5d43cac 2022-07-01 thomas return err;
898 a5d43cac 2022-07-01 thomas }
899 a5d43cac 2022-07-01 thomas view_border(view);
900 a5d43cac 2022-07-01 thomas update_panels();
901 a5d43cac 2022-07-01 thomas doupdate();
902 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
903 a5d43cac 2022-07-01 thomas nlines = view->nlines;
904 a5d43cac 2022-07-01 thomas }
905 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
906 40236d76 2022-06-23 thomas ncols = COLS;
907 669b5ffa 2018-10-07 stsp
908 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
909 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
910 ea0bff04 2022-07-19 thomas if (err)
911 ea0bff04 2022-07-19 thomas return err;
912 ea0bff04 2022-07-19 thomas }
913 ea0bff04 2022-07-19 thomas
914 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
915 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
916 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
917 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
918 40236d76 2022-06-23 thomas wclear(view->window);
919 40236d76 2022-06-23 thomas
920 40236d76 2022-06-23 thomas view->nlines = nlines;
921 40236d76 2022-06-23 thomas view->ncols = ncols;
922 40236d76 2022-06-23 thomas view->lines = LINES;
923 40236d76 2022-06-23 thomas view->cols = COLS;
924 40236d76 2022-06-23 thomas
925 5c60c32a 2018-10-18 stsp return NULL;
926 ea0bff04 2022-07-19 thomas }
927 ea0bff04 2022-07-19 thomas
928 ea0bff04 2022-07-19 thomas static const struct got_error *
929 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
930 ea0bff04 2022-07-19 thomas {
931 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
932 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
933 3a0139e8 2022-07-22 thomas int n = 0;
934 ea0bff04 2022-07-19 thomas
935 3a0139e8 2022-07-22 thomas if (s->selected_entry)
936 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
937 3a0139e8 2022-07-22 thomas
938 ea0bff04 2022-07-19 thomas /*
939 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
940 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
941 ea0bff04 2022-07-19 thomas */
942 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
943 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
944 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
945 ea0bff04 2022-07-19 thomas }
946 ea0bff04 2022-07-19 thomas
947 ea0bff04 2022-07-19 thomas return err;
948 97cb21cd 2022-07-12 thomas }
949 97cb21cd 2022-07-12 thomas
950 97cb21cd 2022-07-12 thomas static void
951 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
952 97cb21cd 2022-07-12 thomas {
953 97cb21cd 2022-07-12 thomas if (n == 0)
954 97cb21cd 2022-07-12 thomas return;
955 97cb21cd 2022-07-12 thomas
956 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
957 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
958 97cb21cd 2022-07-12 thomas view->parent->offset += n;
959 97cb21cd 2022-07-12 thomas else
960 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
961 97cb21cd 2022-07-12 thomas } else if (view->offset) {
962 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
963 97cb21cd 2022-07-12 thomas view->offset -= n;
964 97cb21cd 2022-07-12 thomas else
965 97cb21cd 2022-07-12 thomas view->offset = 0;
966 97cb21cd 2022-07-12 thomas }
967 53d2bdd3 2022-07-10 thomas }
968 53d2bdd3 2022-07-10 thomas
969 53d2bdd3 2022-07-10 thomas static const struct got_error *
970 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
971 53d2bdd3 2022-07-10 thomas {
972 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
973 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
974 53d2bdd3 2022-07-10 thomas
975 53d2bdd3 2022-07-10 thomas if (view->parent)
976 53d2bdd3 2022-07-10 thomas v = view->parent;
977 53d2bdd3 2022-07-10 thomas else
978 53d2bdd3 2022-07-10 thomas v = view;
979 53d2bdd3 2022-07-10 thomas
980 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
981 53d2bdd3 2022-07-10 thomas return NULL;
982 53d2bdd3 2022-07-10 thomas
983 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
984 53d2bdd3 2022-07-10 thomas
985 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
986 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
987 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
988 53d2bdd3 2022-07-10 thomas if (view->parent)
989 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
990 53d2bdd3 2022-07-10 thomas else
991 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
992 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
993 53d2bdd3 2022-07-10 thomas view->count = 0;
994 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
995 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
996 53d2bdd3 2022-07-10 thomas view->count = 0;
997 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
998 53d2bdd3 2022-07-10 thomas }
999 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1000 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1001 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1002 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1003 53d2bdd3 2022-07-10 thomas if (err)
1004 53d2bdd3 2022-07-10 thomas return err;
1005 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1006 53d2bdd3 2022-07-10 thomas } else {
1007 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1008 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1009 53d2bdd3 2022-07-10 thomas if (view->parent)
1010 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1011 53d2bdd3 2022-07-10 thomas else
1012 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1013 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1014 53d2bdd3 2022-07-10 thomas view->count = 0;
1015 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1016 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1017 53d2bdd3 2022-07-10 thomas view->count = 0;
1018 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1019 53d2bdd3 2022-07-10 thomas }
1020 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1021 53d2bdd3 2022-07-10 thomas }
1022 53d2bdd3 2022-07-10 thomas
1023 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1024 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1025 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1026 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1027 53d2bdd3 2022-07-10 thomas
1028 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1029 53d2bdd3 2022-07-10 thomas if (err)
1030 53d2bdd3 2022-07-10 thomas return err;
1031 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1032 53d2bdd3 2022-07-10 thomas if (err)
1033 53d2bdd3 2022-07-10 thomas return err;
1034 53d2bdd3 2022-07-10 thomas
1035 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1036 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1037 53d2bdd3 2022-07-10 thomas if (err)
1038 53d2bdd3 2022-07-10 thomas return err;
1039 53d2bdd3 2022-07-10 thomas }
1040 53d2bdd3 2022-07-10 thomas
1041 3a0139e8 2022-07-22 thomas if (v->resize)
1042 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1043 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1044 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1045 53d2bdd3 2022-07-10 thomas
1046 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1047 53d2bdd3 2022-07-10 thomas
1048 53d2bdd3 2022-07-10 thomas return err;
1049 53d2bdd3 2022-07-10 thomas }
1050 53d2bdd3 2022-07-10 thomas
1051 53d2bdd3 2022-07-10 thomas static void
1052 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1053 53d2bdd3 2022-07-10 thomas {
1054 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1055 53d2bdd3 2022-07-10 thomas
1056 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1057 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1058 669b5ffa 2018-10-07 stsp }
1059 669b5ffa 2018-10-07 stsp
1060 669b5ffa 2018-10-07 stsp static const struct got_error *
1061 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1062 669b5ffa 2018-10-07 stsp {
1063 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1064 669b5ffa 2018-10-07 stsp
1065 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1066 669b5ffa 2018-10-07 stsp return NULL;
1067 669b5ffa 2018-10-07 stsp
1068 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1069 669b5ffa 2018-10-07 stsp view->child = NULL;
1070 669b5ffa 2018-10-07 stsp return err;
1071 669b5ffa 2018-10-07 stsp }
1072 669b5ffa 2018-10-07 stsp
1073 40236d76 2022-06-23 thomas static const struct got_error *
1074 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1075 669b5ffa 2018-10-07 stsp {
1076 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1077 53d2bdd3 2022-07-10 thomas
1078 669b5ffa 2018-10-07 stsp view->child = child;
1079 669b5ffa 2018-10-07 stsp child->parent = view;
1080 40236d76 2022-06-23 thomas
1081 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1082 53d2bdd3 2022-07-10 thomas if (err)
1083 53d2bdd3 2022-07-10 thomas return err;
1084 53d2bdd3 2022-07-10 thomas
1085 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1086 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1087 53d2bdd3 2022-07-10 thomas
1088 53d2bdd3 2022-07-10 thomas return err;
1089 bfddd0d9 2018-09-29 stsp }
1090 bfddd0d9 2018-09-29 stsp
1091 34bc9ec9 2019-02-22 stsp static void
1092 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1093 25791caa 2018-10-24 stsp {
1094 25791caa 2018-10-24 stsp int cols, lines;
1095 25791caa 2018-10-24 stsp struct winsize size;
1096 25791caa 2018-10-24 stsp
1097 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1098 25791caa 2018-10-24 stsp cols = 80; /* Default */
1099 25791caa 2018-10-24 stsp lines = 24;
1100 25791caa 2018-10-24 stsp } else {
1101 25791caa 2018-10-24 stsp cols = size.ws_col;
1102 25791caa 2018-10-24 stsp lines = size.ws_row;
1103 25791caa 2018-10-24 stsp }
1104 25791caa 2018-10-24 stsp resize_term(lines, cols);
1105 2b49a8ae 2019-06-22 stsp }
1106 2b49a8ae 2019-06-22 stsp
1107 2b49a8ae 2019-06-22 stsp static const struct got_error *
1108 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1109 2b49a8ae 2019-06-22 stsp {
1110 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1111 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1112 2b49a8ae 2019-06-22 stsp char pattern[1024];
1113 2b49a8ae 2019-06-22 stsp int ret;
1114 c0c4acc8 2021-01-24 stsp
1115 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1116 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1117 c0c4acc8 2021-01-24 stsp view->searching = 0;
1118 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1119 c0c4acc8 2021-01-24 stsp }
1120 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1121 2b49a8ae 2019-06-22 stsp
1122 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1123 2b49a8ae 2019-06-22 stsp return NULL;
1124 2b49a8ae 2019-06-22 stsp
1125 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1126 a5d43cac 2022-07-01 thomas v = view->child;
1127 2b49a8ae 2019-06-22 stsp
1128 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1129 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1130 a5d43cac 2022-07-01 thomas
1131 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1132 2b49a8ae 2019-06-22 stsp nocbreak();
1133 2b49a8ae 2019-06-22 stsp echo();
1134 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1135 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1136 2b49a8ae 2019-06-22 stsp cbreak();
1137 2b49a8ae 2019-06-22 stsp noecho();
1138 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1139 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1140 2b49a8ae 2019-06-22 stsp return NULL;
1141 2b49a8ae 2019-06-22 stsp
1142 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1143 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1144 7c32bd05 2019-06-22 stsp if (err) {
1145 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1146 7c32bd05 2019-06-22 stsp return err;
1147 7c32bd05 2019-06-22 stsp }
1148 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1149 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1150 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1151 2b49a8ae 2019-06-22 stsp view->search_next(view);
1152 2b49a8ae 2019-06-22 stsp }
1153 2b49a8ae 2019-06-22 stsp
1154 2b49a8ae 2019-06-22 stsp return NULL;
1155 64486692 2022-07-07 thomas }
1156 64486692 2022-07-07 thomas
1157 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1158 64486692 2022-07-07 thomas static const struct got_error *
1159 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1160 64486692 2022-07-07 thomas {
1161 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1162 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1163 64486692 2022-07-07 thomas
1164 64486692 2022-07-07 thomas if (view->parent)
1165 64486692 2022-07-07 thomas v = view->parent;
1166 64486692 2022-07-07 thomas else
1167 64486692 2022-07-07 thomas v = view;
1168 64486692 2022-07-07 thomas
1169 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1170 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1171 ddbc4d37 2022-07-12 thomas else
1172 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1173 64486692 2022-07-07 thomas
1174 ddbc4d37 2022-07-12 thomas if (!v->child)
1175 ddbc4d37 2022-07-12 thomas return NULL;
1176 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1177 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1178 ddbc4d37 2022-07-12 thomas
1179 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1180 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1181 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1182 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1183 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1184 64486692 2022-07-07 thomas
1185 ddbc4d37 2022-07-12 thomas
1186 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1187 64486692 2022-07-07 thomas v->ncols = COLS;
1188 64486692 2022-07-07 thomas v->child->ncols = COLS;
1189 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1190 64486692 2022-07-07 thomas
1191 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1192 64486692 2022-07-07 thomas if (err)
1193 64486692 2022-07-07 thomas return err;
1194 64486692 2022-07-07 thomas }
1195 64486692 2022-07-07 thomas v->child->mode = v->mode;
1196 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1197 64486692 2022-07-07 thomas v->focus_child = 1;
1198 64486692 2022-07-07 thomas
1199 64486692 2022-07-07 thomas err = view_fullscreen(v);
1200 64486692 2022-07-07 thomas if (err)
1201 64486692 2022-07-07 thomas return err;
1202 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1203 64486692 2022-07-07 thomas if (err)
1204 64486692 2022-07-07 thomas return err;
1205 64486692 2022-07-07 thomas
1206 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1207 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1208 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1209 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1210 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1211 ddbc4d37 2022-07-12 thomas } else {
1212 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1213 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1214 64486692 2022-07-07 thomas }
1215 f4e6231a 2022-07-22 thomas if (v->resize)
1216 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1217 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1218 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1219 64486692 2022-07-07 thomas
1220 64486692 2022-07-07 thomas return err;
1221 0cf4efb1 2018-09-29 stsp }
1222 6d0fee91 2018-08-01 stsp
1223 07b0611c 2022-06-23 thomas /*
1224 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1225 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1226 07b0611c 2022-06-23 thomas */
1227 07b0611c 2022-06-23 thomas static int
1228 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1229 07b0611c 2022-06-23 thomas {
1230 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1231 a5d43cac 2022-07-01 thomas int x, n = 0;
1232 07b0611c 2022-06-23 thomas
1233 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1234 a5d43cac 2022-07-01 thomas v = view->child;
1235 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1236 a5d43cac 2022-07-01 thomas v = view->parent;
1237 a5d43cac 2022-07-01 thomas
1238 07b0611c 2022-06-23 thomas view->count = 0;
1239 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1240 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1241 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1242 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1243 07b0611c 2022-06-23 thomas
1244 07b0611c 2022-06-23 thomas do {
1245 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1246 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1247 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1248 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1249 a5d43cac 2022-07-01 thomas }
1250 a5d43cac 2022-07-01 thomas
1251 07b0611c 2022-06-23 thomas /*
1252 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1253 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1254 07b0611c 2022-06-23 thomas */
1255 07b0611c 2022-06-23 thomas if (n >= 9999999)
1256 07b0611c 2022-06-23 thomas n = 9999999;
1257 07b0611c 2022-06-23 thomas else
1258 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1259 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1260 07b0611c 2022-06-23 thomas
1261 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1262 07b0611c 2022-06-23 thomas view->count = n;
1263 07b0611c 2022-06-23 thomas
1264 07b0611c 2022-06-23 thomas return c;
1265 07b0611c 2022-06-23 thomas }
1266 07b0611c 2022-06-23 thomas
1267 0cf4efb1 2018-09-29 stsp static const struct got_error *
1268 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1269 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1270 e5a0f69f 2018-08-18 stsp {
1271 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1272 669b5ffa 2018-10-07 stsp struct tog_view *v;
1273 1a76625f 2018-10-22 stsp int ch, errcode;
1274 e5a0f69f 2018-08-18 stsp
1275 e5a0f69f 2018-08-18 stsp *new = NULL;
1276 8f4ed634 2020-03-26 stsp
1277 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1278 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1279 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1280 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1281 07b0611c 2022-06-23 thomas view->count = 0;
1282 07b0611c 2022-06-23 thomas }
1283 e5a0f69f 2018-08-18 stsp
1284 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1285 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1286 82954512 2020-02-03 stsp if (errcode)
1287 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1288 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1289 3da8ef85 2021-09-21 stsp sched_yield();
1290 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1291 82954512 2020-02-03 stsp if (errcode)
1292 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1293 82954512 2020-02-03 stsp "pthread_mutex_lock");
1294 60493ae3 2019-06-20 stsp view->search_next(view);
1295 60493ae3 2019-06-20 stsp return NULL;
1296 60493ae3 2019-06-20 stsp }
1297 60493ae3 2019-06-20 stsp
1298 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1299 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1300 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1301 1a76625f 2018-10-22 stsp if (errcode)
1302 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1303 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1304 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1305 634cb454 2022-07-03 thomas cbreak();
1306 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1307 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1308 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1309 634cb454 2022-07-03 thomas view->count = 0;
1310 634cb454 2022-07-03 thomas else
1311 634cb454 2022-07-03 thomas ch = view->ch;
1312 634cb454 2022-07-03 thomas } else {
1313 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1314 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1315 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1316 07b0611c 2022-06-23 thomas }
1317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1318 1a76625f 2018-10-22 stsp if (errcode)
1319 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1320 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1321 25791caa 2018-10-24 stsp
1322 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1323 25791caa 2018-10-24 stsp tog_resizeterm();
1324 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1325 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1326 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1327 25791caa 2018-10-24 stsp err = view_resize(v);
1328 25791caa 2018-10-24 stsp if (err)
1329 25791caa 2018-10-24 stsp return err;
1330 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1331 25791caa 2018-10-24 stsp if (err)
1332 25791caa 2018-10-24 stsp return err;
1333 cdfcfb03 2020-12-06 stsp if (v->child) {
1334 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1335 cdfcfb03 2020-12-06 stsp if (err)
1336 cdfcfb03 2020-12-06 stsp return err;
1337 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1338 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1339 cdfcfb03 2020-12-06 stsp if (err)
1340 cdfcfb03 2020-12-06 stsp return err;
1341 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1342 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1343 53d2bdd3 2022-07-10 thomas if (err)
1344 53d2bdd3 2022-07-10 thomas return err;
1345 53d2bdd3 2022-07-10 thomas }
1346 cdfcfb03 2020-12-06 stsp }
1347 25791caa 2018-10-24 stsp }
1348 25791caa 2018-10-24 stsp }
1349 25791caa 2018-10-24 stsp
1350 e5a0f69f 2018-08-18 stsp switch (ch) {
1351 1e37a5c2 2019-05-12 jcs case '\t':
1352 07b0611c 2022-06-23 thomas view->count = 0;
1353 1e37a5c2 2019-05-12 jcs if (view->child) {
1354 e78dc838 2020-12-04 stsp view->focussed = 0;
1355 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1356 e78dc838 2020-12-04 stsp view->focus_child = 1;
1357 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1358 e78dc838 2020-12-04 stsp view->focussed = 0;
1359 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1360 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1361 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1362 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1363 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1364 3a0139e8 2022-07-22 thomas 0);
1365 a5d43cac 2022-07-01 thomas if (err)
1366 a5d43cac 2022-07-01 thomas return err;
1367 a5d43cac 2022-07-01 thomas }
1368 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1369 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1370 a5d43cac 2022-07-01 thomas if (err)
1371 a5d43cac 2022-07-01 thomas return err;
1372 a5d43cac 2022-07-01 thomas }
1373 1e37a5c2 2019-05-12 jcs }
1374 1e37a5c2 2019-05-12 jcs break;
1375 1e37a5c2 2019-05-12 jcs case 'q':
1376 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1377 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1378 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1379 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1380 a5d43cac 2022-07-01 thomas if (err)
1381 a5d43cac 2022-07-01 thomas break;
1382 a5d43cac 2022-07-01 thomas }
1383 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1384 a5d43cac 2022-07-01 thomas }
1385 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1386 9970f7fc 2020-12-03 stsp view->dying = 1;
1387 1e37a5c2 2019-05-12 jcs break;
1388 1e37a5c2 2019-05-12 jcs case 'Q':
1389 1e37a5c2 2019-05-12 jcs *done = 1;
1390 1e37a5c2 2019-05-12 jcs break;
1391 1c5e5faa 2022-06-23 thomas case 'F':
1392 07b0611c 2022-06-23 thomas view->count = 0;
1393 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1394 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1395 1e37a5c2 2019-05-12 jcs break;
1396 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1397 e78dc838 2020-12-04 stsp view->focussed = 0;
1398 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1399 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1400 53d2bdd3 2022-07-10 thomas } else {
1401 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1402 53d2bdd3 2022-07-10 thomas if (!err)
1403 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1404 53d2bdd3 2022-07-10 thomas }
1405 1e37a5c2 2019-05-12 jcs if (err)
1406 1e37a5c2 2019-05-12 jcs break;
1407 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1408 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1409 1e37a5c2 2019-05-12 jcs } else {
1410 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1411 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1412 e78dc838 2020-12-04 stsp view->focussed = 1;
1413 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1414 1e37a5c2 2019-05-12 jcs } else {
1415 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1416 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1417 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1418 53d2bdd3 2022-07-10 thomas if (!err)
1419 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1420 669b5ffa 2018-10-07 stsp }
1421 1e37a5c2 2019-05-12 jcs if (err)
1422 1e37a5c2 2019-05-12 jcs break;
1423 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1424 a5d43cac 2022-07-01 thomas }
1425 a5d43cac 2022-07-01 thomas if (err)
1426 a5d43cac 2022-07-01 thomas break;
1427 3a0139e8 2022-07-22 thomas if (view->resize) {
1428 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1429 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas break;
1431 1e37a5c2 2019-05-12 jcs }
1432 a5d43cac 2022-07-01 thomas if (view->parent)
1433 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1434 a5d43cac 2022-07-01 thomas if (!err)
1435 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1436 1e37a5c2 2019-05-12 jcs break;
1437 64486692 2022-07-07 thomas case 'S':
1438 53d2bdd3 2022-07-10 thomas view->count = 0;
1439 64486692 2022-07-07 thomas err = switch_split(view);
1440 53d2bdd3 2022-07-10 thomas break;
1441 53d2bdd3 2022-07-10 thomas case '-':
1442 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1443 64486692 2022-07-07 thomas break;
1444 53d2bdd3 2022-07-10 thomas case '+':
1445 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1446 53d2bdd3 2022-07-10 thomas break;
1447 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1448 60493ae3 2019-06-20 stsp break;
1449 60493ae3 2019-06-20 stsp case '/':
1450 07b0611c 2022-06-23 thomas view->count = 0;
1451 60493ae3 2019-06-20 stsp if (view->search_start)
1452 2b49a8ae 2019-06-22 stsp view_search_start(view);
1453 60493ae3 2019-06-20 stsp else
1454 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1455 1e37a5c2 2019-05-12 jcs break;
1456 b1bf1435 2019-06-21 stsp case 'N':
1457 60493ae3 2019-06-20 stsp case 'n':
1458 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1459 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1460 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1461 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1462 60493ae3 2019-06-20 stsp view->search_next(view);
1463 60493ae3 2019-06-20 stsp } else
1464 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1465 adf4c9e0 2022-07-03 thomas break;
1466 adf4c9e0 2022-07-03 thomas case 'A':
1467 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1468 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1469 adf4c9e0 2022-07-03 thomas else
1470 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1471 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1472 adf4c9e0 2022-07-03 thomas if (v->reset) {
1473 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1474 adf4c9e0 2022-07-03 thomas if (err)
1475 adf4c9e0 2022-07-03 thomas return err;
1476 adf4c9e0 2022-07-03 thomas }
1477 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1478 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1479 adf4c9e0 2022-07-03 thomas if (err)
1480 adf4c9e0 2022-07-03 thomas return err;
1481 adf4c9e0 2022-07-03 thomas }
1482 adf4c9e0 2022-07-03 thomas }
1483 60493ae3 2019-06-20 stsp break;
1484 1e37a5c2 2019-05-12 jcs default:
1485 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1486 1e37a5c2 2019-05-12 jcs break;
1487 e5a0f69f 2018-08-18 stsp }
1488 e5a0f69f 2018-08-18 stsp
1489 e5a0f69f 2018-08-18 stsp return err;
1490 bcbd79e2 2018-08-19 stsp }
1491 bcbd79e2 2018-08-19 stsp
1492 ef20f542 2022-06-26 thomas static int
1493 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1494 a3404814 2018-09-02 stsp {
1495 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1496 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1497 669b5ffa 2018-10-07 stsp return 0;
1498 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1499 669b5ffa 2018-10-07 stsp return 0;
1500 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1501 a3404814 2018-09-02 stsp return 0;
1502 a3404814 2018-09-02 stsp
1503 669b5ffa 2018-10-07 stsp return view->focussed;
1504 a3404814 2018-09-02 stsp }
1505 a3404814 2018-09-02 stsp
1506 bcbd79e2 2018-08-19 stsp static const struct got_error *
1507 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1508 e5a0f69f 2018-08-18 stsp {
1509 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1510 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1511 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1512 64486692 2022-07-07 thomas char *mode;
1513 fd823528 2018-10-22 stsp int fast_refresh = 10;
1514 1a76625f 2018-10-22 stsp int done = 0, errcode;
1515 e5a0f69f 2018-08-18 stsp
1516 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1517 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1518 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1519 64486692 2022-07-07 thomas else
1520 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1521 64486692 2022-07-07 thomas
1522 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1523 1a76625f 2018-10-22 stsp if (errcode)
1524 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1525 1a76625f 2018-10-22 stsp
1526 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1527 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1528 e5a0f69f 2018-08-18 stsp
1529 1004088d 2018-09-29 stsp view->focussed = 1;
1530 878940b7 2018-09-29 stsp err = view->show(view);
1531 0cf4efb1 2018-09-29 stsp if (err)
1532 0cf4efb1 2018-09-29 stsp return err;
1533 0cf4efb1 2018-09-29 stsp update_panels();
1534 0cf4efb1 2018-09-29 stsp doupdate();
1535 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1536 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1537 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1538 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1539 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1540 fd823528 2018-10-22 stsp
1541 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1542 e5a0f69f 2018-08-18 stsp if (err)
1543 e5a0f69f 2018-08-18 stsp break;
1544 9970f7fc 2020-12-03 stsp if (view->dying) {
1545 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1546 669b5ffa 2018-10-07 stsp
1547 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1548 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1549 9970f7fc 2020-12-03 stsp entry);
1550 e78dc838 2020-12-04 stsp else if (view->parent)
1551 669b5ffa 2018-10-07 stsp prev = view->parent;
1552 669b5ffa 2018-10-07 stsp
1553 e78dc838 2020-12-04 stsp if (view->parent) {
1554 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1555 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1556 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1557 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1558 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1559 40236d76 2022-06-23 thomas if (err)
1560 40236d76 2022-06-23 thomas break;
1561 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1562 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1563 e78dc838 2020-12-04 stsp } else
1564 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1565 669b5ffa 2018-10-07 stsp
1566 9970f7fc 2020-12-03 stsp err = view_close(view);
1567 fb59748f 2020-12-05 stsp if (err)
1568 e5a0f69f 2018-08-18 stsp goto done;
1569 669b5ffa 2018-10-07 stsp
1570 e78dc838 2020-12-04 stsp view = NULL;
1571 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1572 e78dc838 2020-12-04 stsp if (v->focussed)
1573 e78dc838 2020-12-04 stsp break;
1574 0cf4efb1 2018-09-29 stsp }
1575 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1576 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1577 e78dc838 2020-12-04 stsp if (prev)
1578 e78dc838 2020-12-04 stsp view = prev;
1579 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1580 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1581 e78dc838 2020-12-04 stsp tog_view_list_head);
1582 e78dc838 2020-12-04 stsp }
1583 e78dc838 2020-12-04 stsp if (view) {
1584 e78dc838 2020-12-04 stsp if (view->focus_child) {
1585 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1586 e78dc838 2020-12-04 stsp view = view->child;
1587 e78dc838 2020-12-04 stsp } else
1588 e78dc838 2020-12-04 stsp view->focussed = 1;
1589 e78dc838 2020-12-04 stsp }
1590 e78dc838 2020-12-04 stsp }
1591 e5a0f69f 2018-08-18 stsp }
1592 bcbd79e2 2018-08-19 stsp if (new_view) {
1593 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1594 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1595 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1596 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1597 86c66b02 2018-10-18 stsp continue;
1598 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1599 86c66b02 2018-10-18 stsp err = view_close(v);
1600 86c66b02 2018-10-18 stsp if (err)
1601 86c66b02 2018-10-18 stsp goto done;
1602 86c66b02 2018-10-18 stsp break;
1603 86c66b02 2018-10-18 stsp }
1604 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1605 fed7eaa8 2018-10-24 stsp view = new_view;
1606 7cd52833 2022-06-23 thomas }
1607 669b5ffa 2018-10-07 stsp if (view) {
1608 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1609 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1610 e78dc838 2020-12-04 stsp view = view->child;
1611 e78dc838 2020-12-04 stsp } else {
1612 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1613 e78dc838 2020-12-04 stsp view = view->parent;
1614 1a76625f 2018-10-22 stsp }
1615 e78dc838 2020-12-04 stsp show_panel(view->panel);
1616 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1617 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1618 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1619 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1620 669b5ffa 2018-10-07 stsp if (err)
1621 1a76625f 2018-10-22 stsp goto done;
1622 669b5ffa 2018-10-07 stsp }
1623 669b5ffa 2018-10-07 stsp err = view->show(view);
1624 0cf4efb1 2018-09-29 stsp if (err)
1625 1a76625f 2018-10-22 stsp goto done;
1626 669b5ffa 2018-10-07 stsp if (view->child) {
1627 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1628 669b5ffa 2018-10-07 stsp if (err)
1629 1a76625f 2018-10-22 stsp goto done;
1630 669b5ffa 2018-10-07 stsp }
1631 1a76625f 2018-10-22 stsp update_panels();
1632 1a76625f 2018-10-22 stsp doupdate();
1633 0cf4efb1 2018-09-29 stsp }
1634 e5a0f69f 2018-08-18 stsp }
1635 e5a0f69f 2018-08-18 stsp done:
1636 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1637 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1638 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1639 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1640 f2d749db 2022-07-12 thomas close_err = view_close(view);
1641 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1642 f2d749db 2022-07-12 thomas err = close_err;
1643 e5a0f69f 2018-08-18 stsp }
1644 1a76625f 2018-10-22 stsp
1645 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1646 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1647 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1648 1a76625f 2018-10-22 stsp
1649 e5a0f69f 2018-08-18 stsp return err;
1650 ea5e7bb5 2018-08-01 stsp }
1651 ea5e7bb5 2018-08-01 stsp
1652 4ed7e80c 2018-05-20 stsp __dead static void
1653 9f7d7167 2018-04-29 stsp usage_log(void)
1654 9f7d7167 2018-04-29 stsp {
1655 80ddbec8 2018-04-29 stsp endwin();
1656 c70c5802 2018-08-01 stsp fprintf(stderr,
1657 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1658 9f7d7167 2018-04-29 stsp getprogname());
1659 9f7d7167 2018-04-29 stsp exit(1);
1660 80ddbec8 2018-04-29 stsp }
1661 80ddbec8 2018-04-29 stsp
1662 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1663 80ddbec8 2018-04-29 stsp static const struct got_error *
1664 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1665 963b370f 2018-05-20 stsp {
1666 00dfcb92 2018-06-11 stsp char *vis = NULL;
1667 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1668 963b370f 2018-05-20 stsp
1669 963b370f 2018-05-20 stsp *ws = NULL;
1670 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1671 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1672 00dfcb92 2018-06-11 stsp int vislen;
1673 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1674 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1675 00dfcb92 2018-06-11 stsp
1676 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1677 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1678 00dfcb92 2018-06-11 stsp if (err)
1679 00dfcb92 2018-06-11 stsp return err;
1680 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1681 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1682 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1683 a7f50699 2018-06-11 stsp goto done;
1684 a7f50699 2018-06-11 stsp }
1685 00dfcb92 2018-06-11 stsp }
1686 963b370f 2018-05-20 stsp
1687 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1688 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1689 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1690 a7f50699 2018-06-11 stsp goto done;
1691 a7f50699 2018-06-11 stsp }
1692 963b370f 2018-05-20 stsp
1693 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1694 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1695 a7f50699 2018-06-11 stsp done:
1696 00dfcb92 2018-06-11 stsp free(vis);
1697 963b370f 2018-05-20 stsp if (err) {
1698 963b370f 2018-05-20 stsp free(*ws);
1699 963b370f 2018-05-20 stsp *ws = NULL;
1700 963b370f 2018-05-20 stsp *wlen = 0;
1701 963b370f 2018-05-20 stsp }
1702 963b370f 2018-05-20 stsp return err;
1703 05171be4 2022-06-23 thomas }
1704 05171be4 2022-06-23 thomas
1705 05171be4 2022-06-23 thomas static const struct got_error *
1706 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1707 05171be4 2022-06-23 thomas {
1708 05171be4 2022-06-23 thomas char *dst;
1709 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1710 05171be4 2022-06-23 thomas
1711 05171be4 2022-06-23 thomas *ptr = NULL;
1712 05171be4 2022-06-23 thomas n = len = strlen(src);
1713 83316834 2022-06-23 thomas dst = malloc(n + 1);
1714 05171be4 2022-06-23 thomas if (dst == NULL)
1715 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1716 05171be4 2022-06-23 thomas
1717 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1718 05171be4 2022-06-23 thomas const char c = src[idx];
1719 05171be4 2022-06-23 thomas
1720 05171be4 2022-06-23 thomas if (c == '\t') {
1721 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1722 b235ad5d 2022-06-23 thomas char *p;
1723 b235ad5d 2022-06-23 thomas
1724 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1725 83316834 2022-06-23 thomas if (p == NULL) {
1726 83316834 2022-06-23 thomas free(dst);
1727 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1728 83316834 2022-06-23 thomas
1729 83316834 2022-06-23 thomas }
1730 83316834 2022-06-23 thomas dst = p;
1731 05171be4 2022-06-23 thomas n += nb;
1732 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1733 05171be4 2022-06-23 thomas sz += nb;
1734 05171be4 2022-06-23 thomas } else
1735 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1736 05171be4 2022-06-23 thomas ++idx;
1737 05171be4 2022-06-23 thomas }
1738 05171be4 2022-06-23 thomas
1739 05171be4 2022-06-23 thomas dst[sz] = '\0';
1740 05171be4 2022-06-23 thomas *ptr = dst;
1741 05171be4 2022-06-23 thomas return NULL;
1742 963b370f 2018-05-20 stsp }
1743 963b370f 2018-05-20 stsp
1744 8d208d34 2022-06-23 thomas /*
1745 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1746 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1747 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1748 8d208d34 2022-06-23 thomas * *rcol.
1749 f91a2b48 2022-06-23 thomas */
1750 8d208d34 2022-06-23 thomas static int
1751 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1752 8d208d34 2022-06-23 thomas {
1753 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1754 f91a2b48 2022-06-23 thomas
1755 8d208d34 2022-06-23 thomas if (n == 0) {
1756 8d208d34 2022-06-23 thomas *rcol = cols;
1757 8d208d34 2022-06-23 thomas return off;
1758 8d208d34 2022-06-23 thomas }
1759 f91a2b48 2022-06-23 thomas
1760 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1761 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1762 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1763 8d208d34 2022-06-23 thomas else
1764 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1765 f91a2b48 2022-06-23 thomas
1766 8d208d34 2022-06-23 thomas if (width == -1) {
1767 8d208d34 2022-06-23 thomas width = 1;
1768 8d208d34 2022-06-23 thomas wline[i] = L'.';
1769 f91a2b48 2022-06-23 thomas }
1770 f91a2b48 2022-06-23 thomas
1771 8d208d34 2022-06-23 thomas if (cols + width > n)
1772 8d208d34 2022-06-23 thomas break;
1773 8d208d34 2022-06-23 thomas cols += width;
1774 f91a2b48 2022-06-23 thomas }
1775 f91a2b48 2022-06-23 thomas
1776 8d208d34 2022-06-23 thomas *rcol = cols;
1777 8d208d34 2022-06-23 thomas return i;
1778 f91a2b48 2022-06-23 thomas }
1779 f91a2b48 2022-06-23 thomas
1780 f91a2b48 2022-06-23 thomas /*
1781 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1782 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1783 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1784 f91a2b48 2022-06-23 thomas */
1785 f91a2b48 2022-06-23 thomas static const struct got_error *
1786 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1787 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1788 f91a2b48 2022-06-23 thomas {
1789 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1790 8d208d34 2022-06-23 thomas int cols;
1791 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1792 05171be4 2022-06-23 thomas char *exstr = NULL;
1793 963b370f 2018-05-20 stsp size_t wlen;
1794 8d208d34 2022-06-23 thomas int i, scrollx;
1795 963b370f 2018-05-20 stsp
1796 963b370f 2018-05-20 stsp *wlinep = NULL;
1797 b700b5d6 2018-07-10 stsp *widthp = 0;
1798 963b370f 2018-05-20 stsp
1799 05171be4 2022-06-23 thomas if (expand) {
1800 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1801 05171be4 2022-06-23 thomas if (err)
1802 05171be4 2022-06-23 thomas return err;
1803 05171be4 2022-06-23 thomas }
1804 05171be4 2022-06-23 thomas
1805 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1806 05171be4 2022-06-23 thomas free(exstr);
1807 963b370f 2018-05-20 stsp if (err)
1808 963b370f 2018-05-20 stsp return err;
1809 963b370f 2018-05-20 stsp
1810 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1811 f91a2b48 2022-06-23 thomas
1812 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1813 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1814 3f670bfb 2020-12-10 stsp wlen--;
1815 3f670bfb 2020-12-10 stsp }
1816 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1817 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1818 3f670bfb 2020-12-10 stsp wlen--;
1819 3f670bfb 2020-12-10 stsp }
1820 3f670bfb 2020-12-10 stsp
1821 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1822 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1823 27a741e5 2019-09-11 stsp
1824 b700b5d6 2018-07-10 stsp if (widthp)
1825 b700b5d6 2018-07-10 stsp *widthp = cols;
1826 f91a2b48 2022-06-23 thomas if (scrollxp)
1827 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1828 963b370f 2018-05-20 stsp if (err)
1829 963b370f 2018-05-20 stsp free(wline);
1830 963b370f 2018-05-20 stsp else
1831 963b370f 2018-05-20 stsp *wlinep = wline;
1832 963b370f 2018-05-20 stsp return err;
1833 963b370f 2018-05-20 stsp }
1834 963b370f 2018-05-20 stsp
1835 8b473291 2019-02-21 stsp static const struct got_error*
1836 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1837 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1838 8b473291 2019-02-21 stsp {
1839 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1840 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1841 8b473291 2019-02-21 stsp char *s;
1842 8b473291 2019-02-21 stsp const char *name;
1843 8b473291 2019-02-21 stsp
1844 8b473291 2019-02-21 stsp *refs_str = NULL;
1845 8b473291 2019-02-21 stsp
1846 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1847 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1848 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1849 52b5abe1 2019-08-13 stsp int cmp;
1850 52b5abe1 2019-08-13 stsp
1851 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1852 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1853 8b473291 2019-02-21 stsp continue;
1854 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1855 8b473291 2019-02-21 stsp name += 5;
1856 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1857 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1858 7143d404 2019-03-12 stsp continue;
1859 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1860 8b473291 2019-02-21 stsp name += 6;
1861 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1862 8b473291 2019-02-21 stsp name += 8;
1863 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1864 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1865 79cc719f 2020-04-24 stsp continue;
1866 79cc719f 2020-04-24 stsp }
1867 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1868 48cae60d 2020-09-22 stsp if (err)
1869 48cae60d 2020-09-22 stsp break;
1870 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1871 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1872 5d844a1e 2019-08-13 stsp if (err) {
1873 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1874 48cae60d 2020-09-22 stsp free(ref_id);
1875 5d844a1e 2019-08-13 stsp break;
1876 48cae60d 2020-09-22 stsp }
1877 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1878 5d844a1e 2019-08-13 stsp err = NULL;
1879 5d844a1e 2019-08-13 stsp tag = NULL;
1880 5d844a1e 2019-08-13 stsp }
1881 52b5abe1 2019-08-13 stsp }
1882 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1883 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1884 48cae60d 2020-09-22 stsp free(ref_id);
1885 52b5abe1 2019-08-13 stsp if (tag)
1886 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1887 52b5abe1 2019-08-13 stsp if (cmp != 0)
1888 52b5abe1 2019-08-13 stsp continue;
1889 8b473291 2019-02-21 stsp s = *refs_str;
1890 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1891 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1892 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1893 8b473291 2019-02-21 stsp free(s);
1894 8b473291 2019-02-21 stsp *refs_str = NULL;
1895 8b473291 2019-02-21 stsp break;
1896 8b473291 2019-02-21 stsp }
1897 8b473291 2019-02-21 stsp free(s);
1898 8b473291 2019-02-21 stsp }
1899 8b473291 2019-02-21 stsp
1900 8b473291 2019-02-21 stsp return err;
1901 8b473291 2019-02-21 stsp }
1902 8b473291 2019-02-21 stsp
1903 963b370f 2018-05-20 stsp static const struct got_error *
1904 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1905 27a741e5 2019-09-11 stsp int col_tab_align)
1906 5813d178 2019-03-09 stsp {
1907 e6b8b890 2020-12-29 naddy char *smallerthan;
1908 5813d178 2019-03-09 stsp
1909 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1910 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1911 5813d178 2019-03-09 stsp author = smallerthan + 1;
1912 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1913 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1914 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1915 5813d178 2019-03-09 stsp }
1916 5813d178 2019-03-09 stsp
1917 5813d178 2019-03-09 stsp static const struct got_error *
1918 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1919 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1920 8fdc79fe 2020-12-01 naddy int author_display_cols)
1921 80ddbec8 2018-04-29 stsp {
1922 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1923 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1924 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1925 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1926 5813d178 2019-03-09 stsp char *author = NULL;
1927 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1928 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1929 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1930 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1931 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1932 ccb26ccd 2018-11-05 stsp struct tm tm;
1933 45d799e2 2018-12-23 stsp time_t committer_time;
1934 11b20872 2019-11-08 stsp struct tog_color *tc;
1935 80ddbec8 2018-04-29 stsp
1936 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1937 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1938 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1939 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1940 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1941 b39d25c7 2018-07-10 stsp
1942 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1943 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1944 b39d25c7 2018-07-10 stsp else
1945 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1946 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1947 11b20872 2019-11-08 stsp if (tc)
1948 11b20872 2019-11-08 stsp wattr_on(view->window,
1949 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1950 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1951 11b20872 2019-11-08 stsp if (tc)
1952 11b20872 2019-11-08 stsp wattr_off(view->window,
1953 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1954 27a741e5 2019-09-11 stsp col = limit;
1955 b39d25c7 2018-07-10 stsp if (col > avail)
1956 b39d25c7 2018-07-10 stsp goto done;
1957 6570a66d 2019-11-08 stsp
1958 6570a66d 2019-11-08 stsp if (avail >= 120) {
1959 6570a66d 2019-11-08 stsp char *id_str;
1960 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1961 6570a66d 2019-11-08 stsp if (err)
1962 6570a66d 2019-11-08 stsp goto done;
1963 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1964 11b20872 2019-11-08 stsp if (tc)
1965 11b20872 2019-11-08 stsp wattr_on(view->window,
1966 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1967 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1968 11b20872 2019-11-08 stsp if (tc)
1969 11b20872 2019-11-08 stsp wattr_off(view->window,
1970 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1971 6570a66d 2019-11-08 stsp free(id_str);
1972 6570a66d 2019-11-08 stsp col += 9;
1973 6570a66d 2019-11-08 stsp if (col > avail)
1974 6570a66d 2019-11-08 stsp goto done;
1975 6570a66d 2019-11-08 stsp }
1976 b39d25c7 2018-07-10 stsp
1977 f69c5a46 2022-07-19 thomas if (s->use_committer)
1978 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
1979 f69c5a46 2022-07-19 thomas else
1980 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
1981 5813d178 2019-03-09 stsp if (author == NULL) {
1982 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1983 80ddbec8 2018-04-29 stsp goto done;
1984 80ddbec8 2018-04-29 stsp }
1985 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1986 bb737323 2018-05-20 stsp if (err)
1987 bb737323 2018-05-20 stsp goto done;
1988 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1989 11b20872 2019-11-08 stsp if (tc)
1990 11b20872 2019-11-08 stsp wattr_on(view->window,
1991 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1992 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1993 11b20872 2019-11-08 stsp if (tc)
1994 11b20872 2019-11-08 stsp wattr_off(view->window,
1995 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1996 bb737323 2018-05-20 stsp col += author_width;
1997 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1998 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1999 bb737323 2018-05-20 stsp col++;
2000 bb737323 2018-05-20 stsp author_width++;
2001 bb737323 2018-05-20 stsp }
2002 9c2eaf34 2018-05-20 stsp if (col > avail)
2003 9c2eaf34 2018-05-20 stsp goto done;
2004 80ddbec8 2018-04-29 stsp
2005 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2006 5943eee2 2019-08-13 stsp if (err)
2007 6d9fbc00 2018-04-29 stsp goto done;
2008 bb737323 2018-05-20 stsp logmsg = logmsg0;
2009 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2010 bb737323 2018-05-20 stsp logmsg++;
2011 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2012 bb737323 2018-05-20 stsp if (newline)
2013 bb737323 2018-05-20 stsp *newline = '\0';
2014 f91a2b48 2022-06-23 thomas limit = avail - col;
2015 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2016 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2017 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2018 f91a2b48 2022-06-23 thomas limit, col, 1);
2019 331b1a16 2022-06-23 thomas if (err)
2020 331b1a16 2022-06-23 thomas goto done;
2021 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2022 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2023 27a741e5 2019-09-11 stsp while (col < avail) {
2024 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2025 bb737323 2018-05-20 stsp col++;
2026 881b2d3e 2018-04-30 stsp }
2027 80ddbec8 2018-04-29 stsp done:
2028 80ddbec8 2018-04-29 stsp free(logmsg0);
2029 bb737323 2018-05-20 stsp free(wlogmsg);
2030 5813d178 2019-03-09 stsp free(author);
2031 bb737323 2018-05-20 stsp free(wauthor);
2032 80ddbec8 2018-04-29 stsp free(line);
2033 80ddbec8 2018-04-29 stsp return err;
2034 80ddbec8 2018-04-29 stsp }
2035 26ed57b2 2018-05-19 stsp
2036 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2037 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2038 899d86c2 2018-05-10 stsp struct got_object_id *id)
2039 80ddbec8 2018-04-29 stsp {
2040 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2041 80ddbec8 2018-04-29 stsp
2042 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2043 80ddbec8 2018-04-29 stsp if (entry == NULL)
2044 899d86c2 2018-05-10 stsp return NULL;
2045 99db9666 2018-05-07 stsp
2046 899d86c2 2018-05-10 stsp entry->id = id;
2047 99db9666 2018-05-07 stsp entry->commit = commit;
2048 899d86c2 2018-05-10 stsp return entry;
2049 99db9666 2018-05-07 stsp }
2050 80ddbec8 2018-04-29 stsp
2051 99db9666 2018-05-07 stsp static void
2052 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2053 99db9666 2018-05-07 stsp {
2054 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2055 99db9666 2018-05-07 stsp
2056 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2057 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2058 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2059 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2060 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2061 99db9666 2018-05-07 stsp free(entry);
2062 99db9666 2018-05-07 stsp }
2063 99db9666 2018-05-07 stsp
2064 99db9666 2018-05-07 stsp static void
2065 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2066 99db9666 2018-05-07 stsp {
2067 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2068 99db9666 2018-05-07 stsp pop_commit(commits);
2069 c4972b91 2018-05-07 stsp }
2070 c4972b91 2018-05-07 stsp
2071 c4972b91 2018-05-07 stsp static const struct got_error *
2072 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2073 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2074 13add988 2019-10-15 stsp {
2075 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2076 13add988 2019-10-15 stsp regmatch_t regmatch;
2077 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2078 13add988 2019-10-15 stsp
2079 13add988 2019-10-15 stsp *have_match = 0;
2080 13add988 2019-10-15 stsp
2081 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2082 13add988 2019-10-15 stsp if (err)
2083 13add988 2019-10-15 stsp return err;
2084 13add988 2019-10-15 stsp
2085 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2086 13add988 2019-10-15 stsp if (err)
2087 13add988 2019-10-15 stsp goto done;
2088 13add988 2019-10-15 stsp
2089 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2090 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2091 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2092 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2093 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2094 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2095 13add988 2019-10-15 stsp *have_match = 1;
2096 13add988 2019-10-15 stsp done:
2097 13add988 2019-10-15 stsp free(id_str);
2098 13add988 2019-10-15 stsp free(logmsg);
2099 13add988 2019-10-15 stsp return err;
2100 13add988 2019-10-15 stsp }
2101 13add988 2019-10-15 stsp
2102 13add988 2019-10-15 stsp static const struct got_error *
2103 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2104 c4972b91 2018-05-07 stsp {
2105 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2106 9ba79e04 2018-06-11 stsp
2107 1a76625f 2018-10-22 stsp /*
2108 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2109 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2110 1a76625f 2018-10-22 stsp * while updating the display.
2111 1a76625f 2018-10-22 stsp */
2112 4e0d2870 2020-12-07 naddy do {
2113 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2114 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2115 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2116 1a76625f 2018-10-22 stsp int errcode;
2117 899d86c2 2018-05-10 stsp
2118 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2119 4e0d2870 2020-12-07 naddy NULL, NULL);
2120 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2121 ecb28ae0 2018-07-16 stsp break;
2122 899d86c2 2018-05-10 stsp
2123 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2124 9ba79e04 2018-06-11 stsp if (err)
2125 9ba79e04 2018-06-11 stsp break;
2126 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2127 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2128 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2129 9ba79e04 2018-06-11 stsp break;
2130 9ba79e04 2018-06-11 stsp }
2131 93e45b7c 2018-09-24 stsp
2132 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2133 1a76625f 2018-10-22 stsp if (errcode) {
2134 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2135 13add988 2019-10-15 stsp "pthread_mutex_lock");
2136 1a76625f 2018-10-22 stsp break;
2137 1a76625f 2018-10-22 stsp }
2138 1a76625f 2018-10-22 stsp
2139 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2140 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2141 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2142 1a76625f 2018-10-22 stsp
2143 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2144 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2145 7c1452c1 2020-03-26 stsp int have_match;
2146 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2147 7c1452c1 2020-03-26 stsp if (err)
2148 7c1452c1 2020-03-26 stsp break;
2149 7c1452c1 2020-03-26 stsp if (have_match)
2150 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2151 13add988 2019-10-15 stsp }
2152 13add988 2019-10-15 stsp
2153 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2154 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2155 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2156 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2157 7c1452c1 2020-03-26 stsp if (err)
2158 13add988 2019-10-15 stsp break;
2159 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2160 899d86c2 2018-05-10 stsp
2161 9ba79e04 2018-06-11 stsp return err;
2162 0553a4e3 2018-04-30 stsp }
2163 0553a4e3 2018-04-30 stsp
2164 2b779855 2020-12-05 naddy static void
2165 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2166 2b779855 2020-12-05 naddy {
2167 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2168 2b779855 2020-12-05 naddy int ncommits = 0;
2169 2b779855 2020-12-05 naddy
2170 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2171 2b779855 2020-12-05 naddy while (entry) {
2172 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2173 2b779855 2020-12-05 naddy s->selected_entry = entry;
2174 2b779855 2020-12-05 naddy break;
2175 2b779855 2020-12-05 naddy }
2176 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2177 2b779855 2020-12-05 naddy ncommits++;
2178 2b779855 2020-12-05 naddy }
2179 2b779855 2020-12-05 naddy }
2180 2b779855 2020-12-05 naddy
2181 0553a4e3 2018-04-30 stsp static const struct got_error *
2182 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2183 0553a4e3 2018-04-30 stsp {
2184 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2185 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2186 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2187 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2188 60493ae3 2019-06-20 stsp int width;
2189 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2190 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2191 8b473291 2019-02-21 stsp char *refs_str = NULL;
2192 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2193 11b20872 2019-11-08 stsp struct tog_color *tc;
2194 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2195 0553a4e3 2018-04-30 stsp
2196 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2197 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2198 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2199 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2200 1a76625f 2018-10-22 stsp if (err)
2201 ecb28ae0 2018-07-16 stsp return err;
2202 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2203 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2204 d2075bf3 2020-12-25 stsp if (refs) {
2205 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2206 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2207 d2075bf3 2020-12-25 stsp if (err)
2208 d2075bf3 2020-12-25 stsp goto done;
2209 d2075bf3 2020-12-25 stsp }
2210 867c6645 2018-07-10 stsp }
2211 359bfafd 2019-02-22 stsp
2212 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2213 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2214 1a76625f 2018-10-22 stsp
2215 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2216 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2217 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2218 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2219 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2220 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2221 8f4ed634 2020-03-26 stsp goto done;
2222 8f4ed634 2020-03-26 stsp }
2223 8f4ed634 2020-03-26 stsp } else {
2224 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2225 f9686aa5 2020-03-27 stsp
2226 f9686aa5 2020-03-27 stsp if (view->searching) {
2227 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2228 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2229 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2230 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2231 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2232 f9686aa5 2020-03-27 stsp search_str = "searching...";
2233 f9686aa5 2020-03-27 stsp }
2234 f9686aa5 2020-03-27 stsp
2235 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2236 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2237 f9686aa5 2020-03-27 stsp search_str ? search_str :
2238 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2239 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2240 8f4ed634 2020-03-26 stsp goto done;
2241 8f4ed634 2020-03-26 stsp }
2242 8b473291 2019-02-21 stsp }
2243 1a76625f 2018-10-22 stsp
2244 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2245 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2246 91198554 2022-06-23 thomas "........................................",
2247 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2248 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2249 1a76625f 2018-10-22 stsp header = NULL;
2250 1a76625f 2018-10-22 stsp goto done;
2251 1a76625f 2018-10-22 stsp }
2252 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2253 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2254 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2255 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2256 1a76625f 2018-10-22 stsp header = NULL;
2257 1a76625f 2018-10-22 stsp goto done;
2258 ecb28ae0 2018-07-16 stsp }
2259 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2260 1a76625f 2018-10-22 stsp if (err)
2261 1a76625f 2018-10-22 stsp goto done;
2262 867c6645 2018-07-10 stsp
2263 2814baeb 2018-08-01 stsp werase(view->window);
2264 867c6645 2018-07-10 stsp
2265 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2266 a3404814 2018-09-02 stsp wstandout(view->window);
2267 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2268 11b20872 2019-11-08 stsp if (tc)
2269 11b20872 2019-11-08 stsp wattr_on(view->window,
2270 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2271 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2272 11b20872 2019-11-08 stsp if (tc)
2273 11b20872 2019-11-08 stsp wattr_off(view->window,
2274 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2275 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2276 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2277 1a76625f 2018-10-22 stsp width++;
2278 1a76625f 2018-10-22 stsp }
2279 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2280 a3404814 2018-09-02 stsp wstandend(view->window);
2281 ecb28ae0 2018-07-16 stsp free(wline);
2282 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2283 1a76625f 2018-10-22 stsp goto done;
2284 0553a4e3 2018-04-30 stsp
2285 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2286 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2287 5813d178 2019-03-09 stsp ncommits = 0;
2288 05171be4 2022-06-23 thomas view->maxx = 0;
2289 5813d178 2019-03-09 stsp while (entry) {
2290 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2291 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2292 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2293 5813d178 2019-03-09 stsp int width;
2294 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2295 5813d178 2019-03-09 stsp break;
2296 f69c5a46 2022-07-19 thomas if (s->use_committer)
2297 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2298 f69c5a46 2022-07-19 thomas else
2299 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2300 5813d178 2019-03-09 stsp if (author == NULL) {
2301 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2302 5813d178 2019-03-09 stsp goto done;
2303 5813d178 2019-03-09 stsp }
2304 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2305 27a741e5 2019-09-11 stsp date_display_cols);
2306 5813d178 2019-03-09 stsp if (author_cols < width)
2307 5813d178 2019-03-09 stsp author_cols = width;
2308 5813d178 2019-03-09 stsp free(wauthor);
2309 5813d178 2019-03-09 stsp free(author);
2310 ef944b8b 2022-07-21 thomas if (err)
2311 ef944b8b 2022-07-21 thomas goto done;
2312 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2313 05171be4 2022-06-23 thomas if (err)
2314 05171be4 2022-06-23 thomas goto done;
2315 05171be4 2022-06-23 thomas msg = msg0;
2316 05171be4 2022-06-23 thomas while (*msg == '\n')
2317 05171be4 2022-06-23 thomas ++msg;
2318 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2319 331b1a16 2022-06-23 thomas *eol = '\0';
2320 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2321 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2322 331b1a16 2022-06-23 thomas if (err)
2323 331b1a16 2022-06-23 thomas goto done;
2324 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2325 05171be4 2022-06-23 thomas free(msg0);
2326 331b1a16 2022-06-23 thomas free(wmsg);
2327 7ca04879 2019-10-19 stsp ncommits++;
2328 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2329 5813d178 2019-03-09 stsp }
2330 5813d178 2019-03-09 stsp
2331 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2332 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2333 867c6645 2018-07-10 stsp ncommits = 0;
2334 899d86c2 2018-05-10 stsp while (entry) {
2335 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2336 899d86c2 2018-05-10 stsp break;
2337 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2338 2814baeb 2018-08-01 stsp wstandout(view->window);
2339 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2340 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2341 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2342 2814baeb 2018-08-01 stsp wstandend(view->window);
2343 0553a4e3 2018-04-30 stsp if (err)
2344 60493ae3 2019-06-20 stsp goto done;
2345 0553a4e3 2018-04-30 stsp ncommits++;
2346 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2347 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2348 80ddbec8 2018-04-29 stsp }
2349 80ddbec8 2018-04-29 stsp
2350 a5d43cac 2022-07-01 thomas view_border(view);
2351 1a76625f 2018-10-22 stsp done:
2352 1a76625f 2018-10-22 stsp free(id_str);
2353 8b473291 2019-02-21 stsp free(refs_str);
2354 1a76625f 2018-10-22 stsp free(ncommits_str);
2355 1a76625f 2018-10-22 stsp free(header);
2356 80ddbec8 2018-04-29 stsp return err;
2357 9f7d7167 2018-04-29 stsp }
2358 07b55e75 2018-05-10 stsp
2359 07b55e75 2018-05-10 stsp static void
2360 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2361 07b55e75 2018-05-10 stsp {
2362 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2363 07b55e75 2018-05-10 stsp int nscrolled = 0;
2364 07b55e75 2018-05-10 stsp
2365 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2366 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2367 07b55e75 2018-05-10 stsp return;
2368 9f7d7167 2018-04-29 stsp
2369 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2370 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2371 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2372 07b55e75 2018-05-10 stsp if (entry) {
2373 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2374 07b55e75 2018-05-10 stsp nscrolled++;
2375 07b55e75 2018-05-10 stsp }
2376 07b55e75 2018-05-10 stsp }
2377 aa075928 2018-05-10 stsp }
2378 aa075928 2018-05-10 stsp
2379 aa075928 2018-05-10 stsp static const struct got_error *
2380 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2381 aa075928 2018-05-10 stsp {
2382 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2383 5e224a3e 2019-02-22 stsp int errcode;
2384 8a42fca8 2019-02-22 stsp
2385 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2386 aa075928 2018-05-10 stsp
2387 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2388 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2389 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2390 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2391 7aafa0d1 2019-02-22 stsp if (errcode)
2392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2393 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2394 7c1452c1 2020-03-26 stsp
2395 7c1452c1 2020-03-26 stsp /*
2396 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2397 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2398 7c1452c1 2020-03-26 stsp */
2399 7c1452c1 2020-03-26 stsp if (!wait)
2400 7c1452c1 2020-03-26 stsp break;
2401 7c1452c1 2020-03-26 stsp
2402 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2403 ffe38506 2020-12-01 naddy show_log_view(view);
2404 7c1452c1 2020-03-26 stsp update_panels();
2405 7c1452c1 2020-03-26 stsp doupdate();
2406 7c1452c1 2020-03-26 stsp
2407 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2408 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2409 82954512 2020-02-03 stsp if (errcode)
2410 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2411 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2412 82954512 2020-02-03 stsp
2413 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2414 ffe38506 2020-12-01 naddy show_log_view(view);
2415 7c1452c1 2020-03-26 stsp update_panels();
2416 7c1452c1 2020-03-26 stsp doupdate();
2417 5e224a3e 2019-02-22 stsp }
2418 5e224a3e 2019-02-22 stsp
2419 5e224a3e 2019-02-22 stsp return NULL;
2420 a5d43cac 2022-07-01 thomas }
2421 a5d43cac 2022-07-01 thomas
2422 a5d43cac 2022-07-01 thomas static const struct got_error *
2423 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2424 a5d43cac 2022-07-01 thomas {
2425 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2426 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2427 fe731b51 2022-07-14 thomas
2428 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2429 fe731b51 2022-07-14 thomas return NULL;
2430 a5d43cac 2022-07-01 thomas
2431 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2432 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2433 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2434 a5d43cac 2022-07-01 thomas
2435 a5d43cac 2022-07-01 thomas return err;
2436 5e224a3e 2019-02-22 stsp }
2437 5e224a3e 2019-02-22 stsp
2438 5e224a3e 2019-02-22 stsp static const struct got_error *
2439 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2440 5e224a3e 2019-02-22 stsp {
2441 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2442 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2443 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2444 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2445 5e224a3e 2019-02-22 stsp
2446 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2447 5e224a3e 2019-02-22 stsp return NULL;
2448 5e224a3e 2019-02-22 stsp
2449 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2450 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2451 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2452 08ebd0a9 2019-02-22 stsp /*
2453 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2454 08ebd0a9 2019-02-22 stsp */
2455 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2456 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2457 5e224a3e 2019-02-22 stsp if (err)
2458 5e224a3e 2019-02-22 stsp return err;
2459 7aafa0d1 2019-02-22 stsp }
2460 b295e71b 2019-02-22 stsp
2461 7aafa0d1 2019-02-22 stsp do {
2462 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2463 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2464 88048b54 2019-02-21 stsp break;
2465 88048b54 2019-02-21 stsp
2466 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2467 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2468 aa075928 2018-05-10 stsp
2469 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2470 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2471 dd0a52c1 2018-05-20 stsp break;
2472 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2473 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2474 aa075928 2018-05-10 stsp
2475 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2476 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2477 a5d43cac 2022-07-01 thomas else
2478 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2479 a5d43cac 2022-07-01 thomas
2480 dd0a52c1 2018-05-20 stsp return err;
2481 07b55e75 2018-05-10 stsp }
2482 4a7f7875 2018-05-10 stsp
2483 cd0acaa7 2018-05-20 stsp static const struct got_error *
2484 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2485 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2486 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2487 cd0acaa7 2018-05-20 stsp {
2488 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2489 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2490 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2491 cd0acaa7 2018-05-20 stsp
2492 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2493 15a94983 2018-12-23 stsp if (diff_view == NULL)
2494 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2495 ea5e7bb5 2018-08-01 stsp
2496 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2497 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2498 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2499 e5a0f69f 2018-08-18 stsp if (err == NULL)
2500 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2501 cd0acaa7 2018-05-20 stsp return err;
2502 4a7f7875 2018-05-10 stsp }
2503 4a7f7875 2018-05-10 stsp
2504 80ddbec8 2018-04-29 stsp static const struct got_error *
2505 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2506 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2507 9343a5fb 2018-06-23 stsp {
2508 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2509 941e9f74 2019-05-21 stsp
2510 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2511 941e9f74 2019-05-21 stsp if (parent == NULL)
2512 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2513 941e9f74 2019-05-21 stsp
2514 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2515 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2516 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2517 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2518 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2519 941e9f74 2019-05-21 stsp s->tree = subtree;
2520 941e9f74 2019-05-21 stsp s->selected = 0;
2521 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2522 941e9f74 2019-05-21 stsp return NULL;
2523 941e9f74 2019-05-21 stsp }
2524 941e9f74 2019-05-21 stsp
2525 941e9f74 2019-05-21 stsp static const struct got_error *
2526 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2527 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2528 941e9f74 2019-05-21 stsp {
2529 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2530 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2531 941e9f74 2019-05-21 stsp const char *p;
2532 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2533 9343a5fb 2018-06-23 stsp
2534 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2535 941e9f74 2019-05-21 stsp p = path;
2536 941e9f74 2019-05-21 stsp while (*p) {
2537 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2538 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2539 56e0773d 2019-11-28 stsp char *te_name;
2540 33cbf02b 2020-01-12 stsp
2541 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2542 33cbf02b 2020-01-12 stsp p++;
2543 941e9f74 2019-05-21 stsp
2544 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2545 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2546 941e9f74 2019-05-21 stsp if (slash == NULL)
2547 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2548 33cbf02b 2020-01-12 stsp else
2549 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2550 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2551 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2552 56e0773d 2019-11-28 stsp break;
2553 941e9f74 2019-05-21 stsp }
2554 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2555 56e0773d 2019-11-28 stsp if (te == NULL) {
2556 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2557 56e0773d 2019-11-28 stsp free(te_name);
2558 941e9f74 2019-05-21 stsp break;
2559 941e9f74 2019-05-21 stsp }
2560 56e0773d 2019-11-28 stsp free(te_name);
2561 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2562 941e9f74 2019-05-21 stsp
2563 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2564 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2565 b03c880f 2019-05-21 stsp
2566 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2567 941e9f74 2019-05-21 stsp if (slash)
2568 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2569 941e9f74 2019-05-21 stsp else
2570 941e9f74 2019-05-21 stsp subpath = strdup(path);
2571 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2572 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2573 941e9f74 2019-05-21 stsp break;
2574 941e9f74 2019-05-21 stsp }
2575 941e9f74 2019-05-21 stsp
2576 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2577 941e9f74 2019-05-21 stsp subpath);
2578 941e9f74 2019-05-21 stsp if (err)
2579 941e9f74 2019-05-21 stsp break;
2580 941e9f74 2019-05-21 stsp
2581 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2582 941e9f74 2019-05-21 stsp free(tree_id);
2583 941e9f74 2019-05-21 stsp if (err)
2584 941e9f74 2019-05-21 stsp break;
2585 941e9f74 2019-05-21 stsp
2586 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2587 941e9f74 2019-05-21 stsp if (err) {
2588 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2589 941e9f74 2019-05-21 stsp break;
2590 941e9f74 2019-05-21 stsp }
2591 941e9f74 2019-05-21 stsp if (slash == NULL)
2592 941e9f74 2019-05-21 stsp break;
2593 941e9f74 2019-05-21 stsp free(subpath);
2594 941e9f74 2019-05-21 stsp subpath = NULL;
2595 941e9f74 2019-05-21 stsp p = slash;
2596 941e9f74 2019-05-21 stsp }
2597 941e9f74 2019-05-21 stsp
2598 941e9f74 2019-05-21 stsp free(subpath);
2599 1a76625f 2018-10-22 stsp return err;
2600 61266923 2020-01-14 stsp }
2601 61266923 2020-01-14 stsp
2602 61266923 2020-01-14 stsp static const struct got_error *
2603 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2604 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2605 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2606 55cccc34 2020-02-20 stsp {
2607 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2608 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2609 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2610 55cccc34 2020-02-20 stsp
2611 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2612 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2613 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2614 55cccc34 2020-02-20 stsp
2615 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2616 bc573f3b 2021-07-10 stsp if (err)
2617 55cccc34 2020-02-20 stsp return err;
2618 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2619 55cccc34 2020-02-20 stsp
2620 55cccc34 2020-02-20 stsp *new_view = tree_view;
2621 55cccc34 2020-02-20 stsp
2622 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2623 55cccc34 2020-02-20 stsp return NULL;
2624 55cccc34 2020-02-20 stsp
2625 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2626 55cccc34 2020-02-20 stsp }
2627 55cccc34 2020-02-20 stsp
2628 55cccc34 2020-02-20 stsp static const struct got_error *
2629 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2630 61266923 2020-01-14 stsp {
2631 61266923 2020-01-14 stsp sigset_t sigset;
2632 61266923 2020-01-14 stsp int errcode;
2633 61266923 2020-01-14 stsp
2634 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2635 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2636 61266923 2020-01-14 stsp
2637 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2638 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2639 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2640 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2641 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2642 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2643 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2644 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2645 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2646 61266923 2020-01-14 stsp
2647 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2648 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2649 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2650 61266923 2020-01-14 stsp
2651 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2652 61266923 2020-01-14 stsp if (errcode)
2653 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2654 61266923 2020-01-14 stsp
2655 61266923 2020-01-14 stsp return NULL;
2656 1a76625f 2018-10-22 stsp }
2657 1a76625f 2018-10-22 stsp
2658 1a76625f 2018-10-22 stsp static void *
2659 1a76625f 2018-10-22 stsp log_thread(void *arg)
2660 1a76625f 2018-10-22 stsp {
2661 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2662 1a76625f 2018-10-22 stsp int errcode = 0;
2663 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2664 1a76625f 2018-10-22 stsp int done = 0;
2665 61266923 2020-01-14 stsp
2666 f2d749db 2022-07-12 thomas /*
2667 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2668 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2669 f2d749db 2022-07-12 thomas */
2670 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2671 f2d749db 2022-07-12 thomas if (errcode) {
2672 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2673 61266923 2020-01-14 stsp return (void *)err;
2674 f2d749db 2022-07-12 thomas }
2675 1a76625f 2018-10-22 stsp
2676 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2677 f2d749db 2022-07-12 thomas if (err) {
2678 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2679 f2d749db 2022-07-12 thomas goto done;
2680 f2d749db 2022-07-12 thomas }
2681 f2d749db 2022-07-12 thomas
2682 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2683 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2684 f2d749db 2022-07-12 thomas if (errcode) {
2685 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2686 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2687 f2d749db 2022-07-12 thomas goto done;
2688 f2d749db 2022-07-12 thomas }
2689 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2690 1a76625f 2018-10-22 stsp if (err) {
2691 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2692 f2d749db 2022-07-12 thomas goto done;
2693 1a76625f 2018-10-22 stsp err = NULL;
2694 1a76625f 2018-10-22 stsp done = 1;
2695 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2696 1a76625f 2018-10-22 stsp a->commits_needed--;
2697 1a76625f 2018-10-22 stsp
2698 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2699 3abe8080 2019-04-10 stsp if (errcode) {
2700 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2701 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2702 f2d749db 2022-07-12 thomas goto done;
2703 3abe8080 2019-04-10 stsp } else if (*a->quit)
2704 1a76625f 2018-10-22 stsp done = 1;
2705 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2706 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2707 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2708 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2709 1a76625f 2018-10-22 stsp }
2710 1a76625f 2018-10-22 stsp
2711 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2712 7c1452c1 2020-03-26 stsp if (errcode) {
2713 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2714 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2715 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2716 f2d749db 2022-07-12 thomas goto done;
2717 7c1452c1 2020-03-26 stsp }
2718 7c1452c1 2020-03-26 stsp
2719 1a76625f 2018-10-22 stsp if (done)
2720 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2721 7c1452c1 2020-03-26 stsp else {
2722 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2723 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2724 7c1452c1 2020-03-26 stsp &tog_mutex);
2725 f2d749db 2022-07-12 thomas if (errcode) {
2726 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2727 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2728 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2729 f2d749db 2022-07-12 thomas goto done;
2730 f2d749db 2022-07-12 thomas }
2731 21355643 2020-12-06 stsp if (*a->quit)
2732 21355643 2020-12-06 stsp done = 1;
2733 7c1452c1 2020-03-26 stsp }
2734 1a76625f 2018-10-22 stsp }
2735 1a76625f 2018-10-22 stsp }
2736 3abe8080 2019-04-10 stsp a->log_complete = 1;
2737 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2738 f2d749db 2022-07-12 thomas if (errcode)
2739 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2740 f2d749db 2022-07-12 thomas done:
2741 f2d749db 2022-07-12 thomas if (err) {
2742 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2743 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2744 f2d749db 2022-07-12 thomas }
2745 1a76625f 2018-10-22 stsp return (void *)err;
2746 1a76625f 2018-10-22 stsp }
2747 1a76625f 2018-10-22 stsp
2748 1a76625f 2018-10-22 stsp static const struct got_error *
2749 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2750 1a76625f 2018-10-22 stsp {
2751 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2752 1a76625f 2018-10-22 stsp int errcode;
2753 1a76625f 2018-10-22 stsp
2754 1a76625f 2018-10-22 stsp if (s->thread) {
2755 1a76625f 2018-10-22 stsp s->quit = 1;
2756 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2757 1a76625f 2018-10-22 stsp if (errcode)
2758 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2759 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2760 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2761 1a76625f 2018-10-22 stsp if (errcode)
2762 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2763 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2764 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2765 1a76625f 2018-10-22 stsp if (errcode)
2766 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2767 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2768 1a76625f 2018-10-22 stsp if (errcode)
2769 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2770 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2771 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2772 1a76625f 2018-10-22 stsp }
2773 1a76625f 2018-10-22 stsp
2774 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2775 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2776 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2777 1af5eddf 2022-06-23 thomas }
2778 1af5eddf 2022-06-23 thomas
2779 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2780 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2781 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2782 1af5eddf 2022-06-23 thomas if (err == NULL)
2783 1af5eddf 2022-06-23 thomas err = pack_err;
2784 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2785 1a76625f 2018-10-22 stsp }
2786 1a76625f 2018-10-22 stsp
2787 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2788 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2789 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2790 1a76625f 2018-10-22 stsp }
2791 1a76625f 2018-10-22 stsp
2792 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2793 9343a5fb 2018-06-23 stsp }
2794 9343a5fb 2018-06-23 stsp
2795 9343a5fb 2018-06-23 stsp static const struct got_error *
2796 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2797 1a76625f 2018-10-22 stsp {
2798 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2799 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2800 276b94a1 2020-11-13 naddy int errcode;
2801 1a76625f 2018-10-22 stsp
2802 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2803 276b94a1 2020-11-13 naddy
2804 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2805 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2806 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2807 276b94a1 2020-11-13 naddy
2808 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2809 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2810 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2811 276b94a1 2020-11-13 naddy
2812 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2813 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2814 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2815 1a76625f 2018-10-22 stsp free(s->start_id);
2816 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2817 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2818 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2819 1a76625f 2018-10-22 stsp return err;
2820 1a76625f 2018-10-22 stsp }
2821 1a76625f 2018-10-22 stsp
2822 1a76625f 2018-10-22 stsp static const struct got_error *
2823 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2824 60493ae3 2019-06-20 stsp {
2825 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2826 60493ae3 2019-06-20 stsp
2827 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2828 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2829 60493ae3 2019-06-20 stsp return NULL;
2830 60493ae3 2019-06-20 stsp }
2831 60493ae3 2019-06-20 stsp
2832 60493ae3 2019-06-20 stsp static const struct got_error *
2833 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2834 60493ae3 2019-06-20 stsp {
2835 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2836 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2837 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2838 60493ae3 2019-06-20 stsp
2839 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2840 f9686aa5 2020-03-27 stsp show_log_view(view);
2841 f9686aa5 2020-03-27 stsp update_panels();
2842 f9686aa5 2020-03-27 stsp doupdate();
2843 f9686aa5 2020-03-27 stsp
2844 96e2b566 2019-07-08 stsp if (s->search_entry) {
2845 13add988 2019-10-15 stsp int errcode, ch;
2846 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2847 13add988 2019-10-15 stsp if (errcode)
2848 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2849 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2850 13add988 2019-10-15 stsp ch = wgetch(view->window);
2851 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2852 13add988 2019-10-15 stsp if (errcode)
2853 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2854 13add988 2019-10-15 stsp "pthread_mutex_lock");
2855 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2856 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2857 678cbce5 2019-07-28 stsp return NULL;
2858 678cbce5 2019-07-28 stsp }
2859 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2860 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2861 96e2b566 2019-07-08 stsp else
2862 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2863 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2864 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2865 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2866 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2867 df5e841d 2022-06-23 thomas
2868 df5e841d 2022-06-23 thomas /*
2869 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2870 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2871 1f4d5162 2022-06-23 thomas * might have changed.
2872 df5e841d 2022-06-23 thomas */
2873 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2874 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2875 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2876 df5e841d 2022-06-23 thomas else
2877 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2878 b74feda6 2022-06-23 thomas } else {
2879 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2880 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2881 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2882 df5e841d 2022-06-23 thomas else
2883 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2884 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2885 b74feda6 2022-06-23 thomas }
2886 20be8d96 2019-06-21 stsp } else {
2887 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2888 20be8d96 2019-06-21 stsp }
2889 60493ae3 2019-06-20 stsp
2890 60493ae3 2019-06-20 stsp while (1) {
2891 13add988 2019-10-15 stsp int have_match = 0;
2892 13add988 2019-10-15 stsp
2893 60493ae3 2019-06-20 stsp if (entry == NULL) {
2894 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2895 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2896 f9967bca 2020-03-27 stsp view->search_next_done =
2897 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2898 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2899 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2900 f801134a 2019-06-25 stsp return NULL;
2901 60493ae3 2019-06-20 stsp }
2902 96e2b566 2019-07-08 stsp /*
2903 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2904 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2905 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2906 96e2b566 2019-07-08 stsp */
2907 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2908 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2909 60493ae3 2019-06-20 stsp }
2910 60493ae3 2019-06-20 stsp
2911 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2912 13add988 2019-10-15 stsp &view->regex);
2913 5943eee2 2019-08-13 stsp if (err)
2914 13add988 2019-10-15 stsp break;
2915 13add988 2019-10-15 stsp if (have_match) {
2916 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2917 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2918 60493ae3 2019-06-20 stsp break;
2919 60493ae3 2019-06-20 stsp }
2920 13add988 2019-10-15 stsp
2921 96e2b566 2019-07-08 stsp s->search_entry = entry;
2922 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2923 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2924 b1bf1435 2019-06-21 stsp else
2925 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2926 60493ae3 2019-06-20 stsp }
2927 60493ae3 2019-06-20 stsp
2928 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2929 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2930 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2931 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2932 60493ae3 2019-06-20 stsp if (err)
2933 60493ae3 2019-06-20 stsp return err;
2934 ead14cbe 2019-06-21 stsp cur++;
2935 ead14cbe 2019-06-21 stsp }
2936 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2937 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2938 60493ae3 2019-06-20 stsp if (err)
2939 60493ae3 2019-06-20 stsp return err;
2940 ead14cbe 2019-06-21 stsp cur--;
2941 60493ae3 2019-06-20 stsp }
2942 60493ae3 2019-06-20 stsp }
2943 60493ae3 2019-06-20 stsp
2944 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2945 96e2b566 2019-07-08 stsp
2946 60493ae3 2019-06-20 stsp return NULL;
2947 60493ae3 2019-06-20 stsp }
2948 60493ae3 2019-06-20 stsp
2949 60493ae3 2019-06-20 stsp static const struct got_error *
2950 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2951 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2952 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2953 80ddbec8 2018-04-29 stsp {
2954 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2955 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2956 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2957 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2958 1a76625f 2018-10-22 stsp int errcode;
2959 80ddbec8 2018-04-29 stsp
2960 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2961 f135c941 2020-02-20 stsp free(s->in_repo_path);
2962 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2963 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2964 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2965 f135c941 2020-02-20 stsp }
2966 ecb28ae0 2018-07-16 stsp
2967 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2968 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2969 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2970 78756c87 2020-11-24 stsp
2971 fb2756b9 2018-08-04 stsp s->repo = repo;
2972 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2973 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2974 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2975 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2976 9cd7cbd1 2020-12-07 stsp goto done;
2977 9cd7cbd1 2020-12-07 stsp }
2978 9cd7cbd1 2020-12-07 stsp }
2979 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2980 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2981 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2982 5036bf37 2018-09-24 stsp goto done;
2983 5036bf37 2018-09-24 stsp }
2984 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2985 e5a0f69f 2018-08-18 stsp
2986 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2987 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2988 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2989 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2990 11b20872 2019-11-08 stsp if (err)
2991 11b20872 2019-11-08 stsp goto done;
2992 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2993 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2994 11b20872 2019-11-08 stsp if (err) {
2995 11b20872 2019-11-08 stsp free_colors(&s->colors);
2996 11b20872 2019-11-08 stsp goto done;
2997 11b20872 2019-11-08 stsp }
2998 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2999 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3000 11b20872 2019-11-08 stsp if (err) {
3001 11b20872 2019-11-08 stsp free_colors(&s->colors);
3002 11b20872 2019-11-08 stsp goto done;
3003 11b20872 2019-11-08 stsp }
3004 11b20872 2019-11-08 stsp }
3005 11b20872 2019-11-08 stsp
3006 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3007 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3008 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3009 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3010 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3011 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3012 1a76625f 2018-10-22 stsp
3013 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3014 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3015 1af5eddf 2022-06-23 thomas if (err)
3016 1af5eddf 2022-06-23 thomas goto done;
3017 1af5eddf 2022-06-23 thomas }
3018 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3019 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3020 7cd52833 2022-06-23 thomas if (err)
3021 7cd52833 2022-06-23 thomas goto done;
3022 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3023 b672a97a 2020-01-27 stsp !s->log_branches);
3024 1a76625f 2018-10-22 stsp if (err)
3025 1a76625f 2018-10-22 stsp goto done;
3026 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3027 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3028 c5b78334 2020-01-12 stsp if (err)
3029 c5b78334 2020-01-12 stsp goto done;
3030 1a76625f 2018-10-22 stsp
3031 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3032 1a76625f 2018-10-22 stsp if (errcode) {
3033 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3034 1a76625f 2018-10-22 stsp goto done;
3035 1a76625f 2018-10-22 stsp }
3036 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3037 7c1452c1 2020-03-26 stsp if (errcode) {
3038 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3039 7c1452c1 2020-03-26 stsp goto done;
3040 7c1452c1 2020-03-26 stsp }
3041 1a76625f 2018-10-22 stsp
3042 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3043 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3044 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3045 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3046 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3047 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3048 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3049 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3050 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3051 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3052 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3053 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3054 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3055 ba4f502b 2018-08-04 stsp done:
3056 1a76625f 2018-10-22 stsp if (err)
3057 1a76625f 2018-10-22 stsp close_log_view(view);
3058 ba4f502b 2018-08-04 stsp return err;
3059 ba4f502b 2018-08-04 stsp }
3060 ba4f502b 2018-08-04 stsp
3061 e5a0f69f 2018-08-18 stsp static const struct got_error *
3062 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3063 ba4f502b 2018-08-04 stsp {
3064 f2f6d207 2020-11-24 stsp const struct got_error *err;
3065 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3066 ba4f502b 2018-08-04 stsp
3067 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3068 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3069 2b380cc8 2018-10-24 stsp &s->thread_args);
3070 2b380cc8 2018-10-24 stsp if (errcode)
3071 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3072 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3073 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3074 f2f6d207 2020-11-24 stsp if (err)
3075 f2f6d207 2020-11-24 stsp return err;
3076 f2f6d207 2020-11-24 stsp }
3077 2b380cc8 2018-10-24 stsp }
3078 2b380cc8 2018-10-24 stsp
3079 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3080 b31f89ff 2022-07-01 thomas }
3081 b31f89ff 2022-07-01 thomas
3082 b31f89ff 2022-07-01 thomas static void
3083 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3084 b31f89ff 2022-07-01 thomas {
3085 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3086 b31f89ff 2022-07-01 thomas
3087 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3088 b31f89ff 2022-07-01 thomas view->count = 0;
3089 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3090 b31f89ff 2022-07-01 thomas return;
3091 b31f89ff 2022-07-01 thomas
3092 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3093 b31f89ff 2022-07-01 thomas || home)
3094 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3095 b31f89ff 2022-07-01 thomas
3096 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3097 b31f89ff 2022-07-01 thomas --s->selected;
3098 b31f89ff 2022-07-01 thomas else
3099 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3100 b31f89ff 2022-07-01 thomas
3101 b31f89ff 2022-07-01 thomas select_commit(s);
3102 b31f89ff 2022-07-01 thomas return;
3103 b31f89ff 2022-07-01 thomas }
3104 b31f89ff 2022-07-01 thomas
3105 b31f89ff 2022-07-01 thomas static const struct got_error *
3106 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3107 b31f89ff 2022-07-01 thomas {
3108 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3109 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3110 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3111 b31f89ff 2022-07-01 thomas
3112 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3113 b31f89ff 2022-07-01 thomas if (first == NULL) {
3114 b31f89ff 2022-07-01 thomas view->count = 0;
3115 b31f89ff 2022-07-01 thomas return NULL;
3116 b31f89ff 2022-07-01 thomas }
3117 b31f89ff 2022-07-01 thomas
3118 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3119 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3120 b31f89ff 2022-07-01 thomas return NULL;
3121 b31f89ff 2022-07-01 thomas
3122 b31f89ff 2022-07-01 thomas if (!page) {
3123 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3124 b31f89ff 2022-07-01 thomas
3125 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3126 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3127 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3128 b31f89ff 2022-07-01 thomas ++s->selected;
3129 b31f89ff 2022-07-01 thomas else
3130 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3131 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3132 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3133 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3134 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3135 b31f89ff 2022-07-01 thomas else
3136 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3137 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3138 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3139 b31f89ff 2022-07-01 thomas } else {
3140 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3141 b31f89ff 2022-07-01 thomas if (err)
3142 b31f89ff 2022-07-01 thomas return err;
3143 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3144 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3145 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3146 b31f89ff 2022-07-01 thomas }
3147 b31f89ff 2022-07-01 thomas }
3148 b31f89ff 2022-07-01 thomas if (err)
3149 b31f89ff 2022-07-01 thomas return err;
3150 b31f89ff 2022-07-01 thomas
3151 a5d43cac 2022-07-01 thomas /*
3152 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3153 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3154 a5d43cac 2022-07-01 thomas */
3155 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3156 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3157 a5d43cac 2022-07-01 thomas
3158 b31f89ff 2022-07-01 thomas select_commit(s);
3159 b31f89ff 2022-07-01 thomas
3160 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3161 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3162 b31f89ff 2022-07-01 thomas view->count = 0;
3163 b31f89ff 2022-07-01 thomas
3164 b31f89ff 2022-07-01 thomas return NULL;
3165 e5a0f69f 2018-08-18 stsp }
3166 04cc582a 2018-08-01 stsp
3167 a5d43cac 2022-07-01 thomas static void
3168 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3169 a5d43cac 2022-07-01 thomas {
3170 24415785 2022-07-03 thomas *x = 0;
3171 24415785 2022-07-03 thomas *y = 0;
3172 24415785 2022-07-03 thomas
3173 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3174 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3175 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3176 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3177 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3178 53d2bdd3 2022-07-10 thomas else
3179 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3180 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3181 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3182 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3183 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3184 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3185 53d2bdd3 2022-07-10 thomas else
3186 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3187 53d2bdd3 2022-07-10 thomas }
3188 a5d43cac 2022-07-01 thomas }
3189 a5d43cac 2022-07-01 thomas
3190 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3191 e5a0f69f 2018-08-18 stsp static const struct got_error *
3192 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3193 a5d43cac 2022-07-01 thomas {
3194 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3195 a5d43cac 2022-07-01 thomas
3196 a5d43cac 2022-07-01 thomas view->nlines = y;
3197 64486692 2022-07-07 thomas view->ncols = COLS;
3198 a5d43cac 2022-07-01 thomas err = view_resize(view);
3199 a5d43cac 2022-07-01 thomas if (err)
3200 a5d43cac 2022-07-01 thomas return err;
3201 a5d43cac 2022-07-01 thomas
3202 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3203 a5d43cac 2022-07-01 thomas
3204 a5d43cac 2022-07-01 thomas return err;
3205 a5d43cac 2022-07-01 thomas }
3206 a5d43cac 2022-07-01 thomas
3207 a5d43cac 2022-07-01 thomas static const struct got_error *
3208 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3209 e5a0f69f 2018-08-18 stsp {
3210 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3211 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3212 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3213 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3214 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3215 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3216 80ddbec8 2018-04-29 stsp
3217 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3218 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3219 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3220 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3221 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3222 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3223 fb280deb 2021-08-30 stsp }
3224 b31f89ff 2022-07-01 thomas return err;
3225 528dedf3 2021-08-30 stsp }
3226 068ab281 2022-07-01 thomas
3227 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3228 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3229 068ab281 2022-07-01 thomas --eos; /* border */
3230 068ab281 2022-07-01 thomas
3231 528dedf3 2021-08-30 stsp switch (ch) {
3232 1e37a5c2 2019-05-12 jcs case 'q':
3233 1e37a5c2 2019-05-12 jcs s->quit = 1;
3234 05171be4 2022-06-23 thomas break;
3235 05171be4 2022-06-23 thomas case '0':
3236 05171be4 2022-06-23 thomas view->x = 0;
3237 05171be4 2022-06-23 thomas break;
3238 05171be4 2022-06-23 thomas case '$':
3239 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3240 07b0611c 2022-06-23 thomas view->count = 0;
3241 05171be4 2022-06-23 thomas break;
3242 05171be4 2022-06-23 thomas case KEY_RIGHT:
3243 05171be4 2022-06-23 thomas case 'l':
3244 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3245 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3246 07b0611c 2022-06-23 thomas else
3247 07b0611c 2022-06-23 thomas view->count = 0;
3248 1e37a5c2 2019-05-12 jcs break;
3249 05171be4 2022-06-23 thomas case KEY_LEFT:
3250 05171be4 2022-06-23 thomas case 'h':
3251 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3252 07b0611c 2022-06-23 thomas if (view->x <= 0)
3253 07b0611c 2022-06-23 thomas view->count = 0;
3254 05171be4 2022-06-23 thomas break;
3255 1e37a5c2 2019-05-12 jcs case 'k':
3256 1e37a5c2 2019-05-12 jcs case KEY_UP:
3257 1e37a5c2 2019-05-12 jcs case '<':
3258 1e37a5c2 2019-05-12 jcs case ',':
3259 f7140bf5 2021-10-17 thomas case CTRL('p'):
3260 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3261 912a3f79 2021-08-30 j break;
3262 912a3f79 2021-08-30 j case 'g':
3263 912a3f79 2021-08-30 j case KEY_HOME:
3264 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3265 07b0611c 2022-06-23 thomas view->count = 0;
3266 1e37a5c2 2019-05-12 jcs break;
3267 70f17a53 2022-06-13 thomas case CTRL('u'):
3268 23427b14 2022-06-23 thomas case 'u':
3269 70f17a53 2022-06-13 thomas nscroll /= 2;
3270 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3271 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3272 a4292ac5 2019-05-12 jcs case CTRL('b'):
3273 1c5e5faa 2022-06-23 thomas case 'b':
3274 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3275 1e37a5c2 2019-05-12 jcs break;
3276 1e37a5c2 2019-05-12 jcs case 'j':
3277 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3278 1e37a5c2 2019-05-12 jcs case '>':
3279 1e37a5c2 2019-05-12 jcs case '.':
3280 f7140bf5 2021-10-17 thomas case CTRL('n'):
3281 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3282 912a3f79 2021-08-30 j break;
3283 f69c5a46 2022-07-19 thomas case '@':
3284 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3285 f69c5a46 2022-07-19 thomas break;
3286 912a3f79 2021-08-30 j case 'G':
3287 912a3f79 2021-08-30 j case KEY_END: {
3288 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3289 912a3f79 2021-08-30 j * traverse them all. */
3290 07b0611c 2022-06-23 thomas view->count = 0;
3291 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3292 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3293 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3294 80ddbec8 2018-04-29 stsp }
3295 912a3f79 2021-08-30 j
3296 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3297 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3298 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3299 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3300 f3bc9f1d 2021-09-05 naddy break;
3301 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3302 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3303 f3bc9f1d 2021-09-05 naddy }
3304 f3bc9f1d 2021-09-05 naddy if (n > 0)
3305 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3306 2b779855 2020-12-05 naddy select_commit(s);
3307 1e37a5c2 2019-05-12 jcs break;
3308 912a3f79 2021-08-30 j }
3309 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3310 23427b14 2022-06-23 thomas case 'd':
3311 70f17a53 2022-06-13 thomas nscroll /= 2;
3312 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3313 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3314 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3315 4c2d69cb 2022-06-23 thomas case 'f':
3316 b31f89ff 2022-07-01 thomas case ' ':
3317 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3318 1e37a5c2 2019-05-12 jcs break;
3319 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3320 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3321 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3322 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3323 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3324 2b779855 2020-12-05 naddy select_commit(s);
3325 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3326 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3327 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3328 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3329 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3330 0bf7f153 2020-12-02 naddy }
3331 1e37a5c2 2019-05-12 jcs break;
3332 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3333 444d5325 2022-07-03 thomas case '\r':
3334 07b0611c 2022-06-23 thomas view->count = 0;
3335 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3336 e5a0f69f 2018-08-18 stsp break;
3337 a5d43cac 2022-07-01 thomas
3338 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3339 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3340 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3341 a5d43cac 2022-07-01 thomas
3342 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3343 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3344 78756c87 2020-11-24 stsp view, s->repo);
3345 1e37a5c2 2019-05-12 jcs if (err)
3346 1e37a5c2 2019-05-12 jcs break;
3347 a5d43cac 2022-07-01 thomas
3348 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3349 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3350 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3351 a5d43cac 2022-07-01 thomas if (err)
3352 a5d43cac 2022-07-01 thomas break;
3353 a5d43cac 2022-07-01 thomas }
3354 a5d43cac 2022-07-01 thomas
3355 e78dc838 2020-12-04 stsp view->focussed = 0;
3356 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3357 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3358 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3359 a5d43cac 2022-07-01 thomas
3360 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3361 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3362 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3363 f7013a22 2018-10-24 stsp if (err)
3364 1e37a5c2 2019-05-12 jcs return err;
3365 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3366 40236d76 2022-06-23 thomas if (err)
3367 40236d76 2022-06-23 thomas return err;
3368 e78dc838 2020-12-04 stsp view->focus_child = 1;
3369 1e37a5c2 2019-05-12 jcs } else
3370 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3371 1e37a5c2 2019-05-12 jcs break;
3372 1e37a5c2 2019-05-12 jcs case 't':
3373 07b0611c 2022-06-23 thomas view->count = 0;
3374 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3375 5036bf37 2018-09-24 stsp break;
3376 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3377 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3378 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3379 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3380 4e97c21c 2020-12-06 stsp s->repo);
3381 1e37a5c2 2019-05-12 jcs if (err)
3382 e5a0f69f 2018-08-18 stsp break;
3383 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3384 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3385 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3386 444d5325 2022-07-03 thomas if (err)
3387 444d5325 2022-07-03 thomas break;
3388 444d5325 2022-07-03 thomas }
3389 e78dc838 2020-12-04 stsp view->focussed = 0;
3390 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3391 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3392 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3393 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3394 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3395 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3396 1e37a5c2 2019-05-12 jcs if (err)
3397 1e37a5c2 2019-05-12 jcs return err;
3398 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3399 40236d76 2022-06-23 thomas if (err)
3400 40236d76 2022-06-23 thomas return err;
3401 e78dc838 2020-12-04 stsp view->focus_child = 1;
3402 1e37a5c2 2019-05-12 jcs } else
3403 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3404 1e37a5c2 2019-05-12 jcs break;
3405 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3406 21355643 2020-12-06 stsp case CTRL('l'):
3407 21355643 2020-12-06 stsp case 'B':
3408 07b0611c 2022-06-23 thomas view->count = 0;
3409 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3410 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3411 1e37a5c2 2019-05-12 jcs break;
3412 21355643 2020-12-06 stsp err = stop_log_thread(s);
3413 74cfe85e 2020-10-20 stsp if (err)
3414 74cfe85e 2020-10-20 stsp return err;
3415 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3416 21355643 2020-12-06 stsp char *parent_path;
3417 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3418 21355643 2020-12-06 stsp if (err)
3419 21355643 2020-12-06 stsp return err;
3420 21355643 2020-12-06 stsp free(s->in_repo_path);
3421 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3422 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3423 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3424 21355643 2020-12-06 stsp struct got_object_id *start_id;
3425 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3426 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3427 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3428 21355643 2020-12-06 stsp if (err)
3429 21355643 2020-12-06 stsp return err;
3430 21355643 2020-12-06 stsp free(s->start_id);
3431 21355643 2020-12-06 stsp s->start_id = start_id;
3432 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3433 21355643 2020-12-06 stsp } else /* 'B' */
3434 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3435 21355643 2020-12-06 stsp
3436 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3437 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3438 bf7e79b3 2022-06-23 thomas if (err)
3439 bf7e79b3 2022-06-23 thomas return err;
3440 bf7e79b3 2022-06-23 thomas }
3441 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3442 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3443 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3444 74cfe85e 2020-10-20 stsp if (err)
3445 21355643 2020-12-06 stsp return err;
3446 51a10b52 2020-12-26 stsp tog_free_refs();
3447 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3448 ca51c541 2020-12-07 stsp if (err)
3449 ca51c541 2020-12-07 stsp return err;
3450 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3451 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3452 d01904d4 2019-06-25 stsp if (err)
3453 d01904d4 2019-06-25 stsp return err;
3454 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3455 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3456 b672a97a 2020-01-27 stsp if (err)
3457 b672a97a 2020-01-27 stsp return err;
3458 21355643 2020-12-06 stsp free_commits(&s->commits);
3459 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3460 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3461 21355643 2020-12-06 stsp s->selected_entry = NULL;
3462 21355643 2020-12-06 stsp s->selected = 0;
3463 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3464 21355643 2020-12-06 stsp s->quit = 0;
3465 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3466 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3467 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3468 d01904d4 2019-06-25 stsp break;
3469 6458efa5 2020-11-24 stsp case 'r':
3470 07b0611c 2022-06-23 thomas view->count = 0;
3471 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3472 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3473 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3474 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3475 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3476 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3477 6458efa5 2020-11-24 stsp if (err) {
3478 6458efa5 2020-11-24 stsp view_close(ref_view);
3479 6458efa5 2020-11-24 stsp return err;
3480 6458efa5 2020-11-24 stsp }
3481 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3482 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3483 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3484 444d5325 2022-07-03 thomas if (err)
3485 444d5325 2022-07-03 thomas break;
3486 444d5325 2022-07-03 thomas }
3487 e78dc838 2020-12-04 stsp view->focussed = 0;
3488 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3489 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3490 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3491 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3492 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3493 6458efa5 2020-11-24 stsp err = view_close_child(view);
3494 6458efa5 2020-11-24 stsp if (err)
3495 6458efa5 2020-11-24 stsp return err;
3496 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3497 40236d76 2022-06-23 thomas if (err)
3498 40236d76 2022-06-23 thomas return err;
3499 e78dc838 2020-12-04 stsp view->focus_child = 1;
3500 6458efa5 2020-11-24 stsp } else
3501 6458efa5 2020-11-24 stsp *new_view = ref_view;
3502 6458efa5 2020-11-24 stsp break;
3503 1e37a5c2 2019-05-12 jcs default:
3504 07b0611c 2022-06-23 thomas view->count = 0;
3505 1e37a5c2 2019-05-12 jcs break;
3506 899d86c2 2018-05-10 stsp }
3507 e5a0f69f 2018-08-18 stsp
3508 80ddbec8 2018-04-29 stsp return err;
3509 80ddbec8 2018-04-29 stsp }
3510 80ddbec8 2018-04-29 stsp
3511 4ed7e80c 2018-05-20 stsp static const struct got_error *
3512 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3513 c2db6724 2019-01-04 stsp {
3514 c2db6724 2019-01-04 stsp const struct got_error *error;
3515 c2db6724 2019-01-04 stsp
3516 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3517 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3518 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3519 37c06ea4 2019-07-15 stsp #endif
3520 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3521 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3522 c2db6724 2019-01-04 stsp
3523 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3524 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3525 c2db6724 2019-01-04 stsp
3526 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3527 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3528 c2db6724 2019-01-04 stsp
3529 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3530 c2db6724 2019-01-04 stsp if (error != NULL)
3531 c2db6724 2019-01-04 stsp return error;
3532 c2db6724 2019-01-04 stsp
3533 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3534 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3535 c2db6724 2019-01-04 stsp
3536 c2db6724 2019-01-04 stsp return NULL;
3537 c2db6724 2019-01-04 stsp }
3538 c2db6724 2019-01-04 stsp
3539 a915003a 2019-02-05 stsp static void
3540 a915003a 2019-02-05 stsp init_curses(void)
3541 a915003a 2019-02-05 stsp {
3542 296152d1 2022-05-31 thomas /*
3543 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3544 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3545 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3546 296152d1 2022-05-31 thomas */
3547 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3548 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3549 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3550 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3551 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3552 296152d1 2022-05-31 thomas
3553 a915003a 2019-02-05 stsp initscr();
3554 a915003a 2019-02-05 stsp cbreak();
3555 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3556 a915003a 2019-02-05 stsp noecho();
3557 a915003a 2019-02-05 stsp nonl();
3558 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3559 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3560 a915003a 2019-02-05 stsp curs_set(0);
3561 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3562 6d17833f 2019-11-08 stsp start_color();
3563 6d17833f 2019-11-08 stsp use_default_colors();
3564 6d17833f 2019-11-08 stsp }
3565 a915003a 2019-02-05 stsp }
3566 a915003a 2019-02-05 stsp
3567 c2db6724 2019-01-04 stsp static const struct got_error *
3568 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3569 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3570 f135c941 2020-02-20 stsp {
3571 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3572 f135c941 2020-02-20 stsp
3573 f135c941 2020-02-20 stsp if (argc == 0) {
3574 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3575 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3576 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3577 f135c941 2020-02-20 stsp return NULL;
3578 f135c941 2020-02-20 stsp }
3579 f135c941 2020-02-20 stsp
3580 f135c941 2020-02-20 stsp if (worktree) {
3581 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3582 bfd61697 2020-11-14 stsp char *p;
3583 f135c941 2020-02-20 stsp
3584 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3585 f135c941 2020-02-20 stsp if (err)
3586 f135c941 2020-02-20 stsp return err;
3587 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3588 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3589 bfd61697 2020-11-14 stsp p) == -1) {
3590 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3591 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3592 f135c941 2020-02-20 stsp }
3593 f135c941 2020-02-20 stsp free(p);
3594 f135c941 2020-02-20 stsp } else
3595 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3596 f135c941 2020-02-20 stsp
3597 f135c941 2020-02-20 stsp return err;
3598 f135c941 2020-02-20 stsp }
3599 f135c941 2020-02-20 stsp
3600 f135c941 2020-02-20 stsp static const struct got_error *
3601 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3602 9f7d7167 2018-04-29 stsp {
3603 80ddbec8 2018-04-29 stsp const struct got_error *error;
3604 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3605 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3606 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3607 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3608 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3609 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3610 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3611 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3612 04cc582a 2018-08-01 stsp struct tog_view *view;
3613 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3614 80ddbec8 2018-04-29 stsp
3615 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3616 80ddbec8 2018-04-29 stsp switch (ch) {
3617 b672a97a 2020-01-27 stsp case 'b':
3618 b672a97a 2020-01-27 stsp log_branches = 1;
3619 b672a97a 2020-01-27 stsp break;
3620 80ddbec8 2018-04-29 stsp case 'c':
3621 80ddbec8 2018-04-29 stsp start_commit = optarg;
3622 80ddbec8 2018-04-29 stsp break;
3623 ecb28ae0 2018-07-16 stsp case 'r':
3624 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3625 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3626 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3627 9ba1d308 2019-10-21 stsp optarg);
3628 ecb28ae0 2018-07-16 stsp break;
3629 80ddbec8 2018-04-29 stsp default:
3630 17020d27 2019-03-07 stsp usage_log();
3631 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3632 80ddbec8 2018-04-29 stsp }
3633 80ddbec8 2018-04-29 stsp }
3634 80ddbec8 2018-04-29 stsp
3635 80ddbec8 2018-04-29 stsp argc -= optind;
3636 80ddbec8 2018-04-29 stsp argv += optind;
3637 80ddbec8 2018-04-29 stsp
3638 f135c941 2020-02-20 stsp if (argc > 1)
3639 f135c941 2020-02-20 stsp usage_log();
3640 963f97a1 2019-03-18 stsp
3641 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3642 7cd52833 2022-06-23 thomas if (error != NULL)
3643 7cd52833 2022-06-23 thomas goto done;
3644 7cd52833 2022-06-23 thomas
3645 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3646 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3647 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3648 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3649 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3650 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3651 c156c7a4 2020-12-18 stsp goto done;
3652 a1fbf39a 2019-08-11 stsp if (worktree)
3653 6962eb72 2020-02-20 stsp repo_path =
3654 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3655 a1fbf39a 2019-08-11 stsp else
3656 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3657 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3658 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3659 c156c7a4 2020-12-18 stsp goto done;
3660 c156c7a4 2020-12-18 stsp }
3661 ecb28ae0 2018-07-16 stsp }
3662 ecb28ae0 2018-07-16 stsp
3663 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3664 80ddbec8 2018-04-29 stsp if (error != NULL)
3665 ecb28ae0 2018-07-16 stsp goto done;
3666 80ddbec8 2018-04-29 stsp
3667 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3668 f135c941 2020-02-20 stsp repo, worktree);
3669 f135c941 2020-02-20 stsp if (error)
3670 f135c941 2020-02-20 stsp goto done;
3671 f135c941 2020-02-20 stsp
3672 f135c941 2020-02-20 stsp init_curses();
3673 f135c941 2020-02-20 stsp
3674 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3675 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3676 c02c541e 2019-03-29 stsp if (error)
3677 c02c541e 2019-03-29 stsp goto done;
3678 c02c541e 2019-03-29 stsp
3679 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3680 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3681 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3682 87670572 2020-12-26 naddy if (error)
3683 87670572 2020-12-26 naddy goto done;
3684 87670572 2020-12-26 naddy }
3685 51a10b52 2020-12-26 stsp
3686 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3687 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3688 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3689 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3690 d8f38dc4 2020-12-05 stsp if (error)
3691 d8f38dc4 2020-12-05 stsp goto done;
3692 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3693 d8f38dc4 2020-12-05 stsp } else {
3694 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3695 d8f38dc4 2020-12-05 stsp if (error == NULL)
3696 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3697 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3698 d8f38dc4 2020-12-05 stsp goto done;
3699 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3700 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3701 d8f38dc4 2020-12-05 stsp if (error)
3702 d8f38dc4 2020-12-05 stsp goto done;
3703 d8f38dc4 2020-12-05 stsp }
3704 8b473291 2019-02-21 stsp
3705 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3706 04cc582a 2018-08-01 stsp if (view == NULL) {
3707 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3708 04cc582a 2018-08-01 stsp goto done;
3709 04cc582a 2018-08-01 stsp }
3710 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3711 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3712 ba4f502b 2018-08-04 stsp if (error)
3713 ba4f502b 2018-08-04 stsp goto done;
3714 2fc00ff4 2019-08-31 stsp if (worktree) {
3715 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3716 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3717 2fc00ff4 2019-08-31 stsp worktree = NULL;
3718 2fc00ff4 2019-08-31 stsp }
3719 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3720 ecb28ae0 2018-07-16 stsp done:
3721 f135c941 2020-02-20 stsp free(in_repo_path);
3722 ecb28ae0 2018-07-16 stsp free(repo_path);
3723 ecb28ae0 2018-07-16 stsp free(cwd);
3724 899d86c2 2018-05-10 stsp free(start_id);
3725 d8f38dc4 2020-12-05 stsp free(label);
3726 d8f38dc4 2020-12-05 stsp if (ref)
3727 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3728 1d0f4054 2021-06-17 stsp if (repo) {
3729 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3730 1d0f4054 2021-06-17 stsp if (error == NULL)
3731 1d0f4054 2021-06-17 stsp error = close_err;
3732 1d0f4054 2021-06-17 stsp }
3733 ec142235 2019-03-07 stsp if (worktree)
3734 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3735 7cd52833 2022-06-23 thomas if (pack_fds) {
3736 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3737 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3738 7cd52833 2022-06-23 thomas if (error == NULL)
3739 7cd52833 2022-06-23 thomas error = pack_err;
3740 7cd52833 2022-06-23 thomas }
3741 51a10b52 2020-12-26 stsp tog_free_refs();
3742 80ddbec8 2018-04-29 stsp return error;
3743 9f7d7167 2018-04-29 stsp }
3744 9f7d7167 2018-04-29 stsp
3745 4ed7e80c 2018-05-20 stsp __dead static void
3746 9f7d7167 2018-04-29 stsp usage_diff(void)
3747 9f7d7167 2018-04-29 stsp {
3748 80ddbec8 2018-04-29 stsp endwin();
3749 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3750 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3751 9f7d7167 2018-04-29 stsp exit(1);
3752 b304db33 2018-05-20 stsp }
3753 b304db33 2018-05-20 stsp
3754 6d17833f 2019-11-08 stsp static int
3755 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3756 41605754 2020-11-12 stsp regmatch_t *regmatch)
3757 6d17833f 2019-11-08 stsp {
3758 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3759 6d17833f 2019-11-08 stsp }
3760 6d17833f 2019-11-08 stsp
3761 ef20f542 2022-06-26 thomas static struct tog_color *
3762 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3763 6d17833f 2019-11-08 stsp {
3764 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3765 6d17833f 2019-11-08 stsp
3766 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3767 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3768 6d17833f 2019-11-08 stsp return tc;
3769 6d17833f 2019-11-08 stsp }
3770 6d17833f 2019-11-08 stsp
3771 6d17833f 2019-11-08 stsp return NULL;
3772 6d17833f 2019-11-08 stsp }
3773 6d17833f 2019-11-08 stsp
3774 4ed7e80c 2018-05-20 stsp static const struct got_error *
3775 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3776 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3777 41605754 2020-11-12 stsp {
3778 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3779 666f7b10 2022-06-23 thomas char *exstr = NULL;
3780 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3781 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3782 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3783 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3784 41605754 2020-11-12 stsp
3785 41605754 2020-11-12 stsp *wtotal = 0;
3786 cbea3800 2022-06-23 thomas
3787 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3788 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3789 41605754 2020-11-12 stsp
3790 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3791 666f7b10 2022-06-23 thomas if (err)
3792 666f7b10 2022-06-23 thomas return err;
3793 666f7b10 2022-06-23 thomas
3794 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3795 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3796 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3797 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3798 666f7b10 2022-06-23 thomas goto done;
3799 666f7b10 2022-06-23 thomas }
3800 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3801 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3802 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3803 cbea3800 2022-06-23 thomas goto done;
3804 cbea3800 2022-06-23 thomas }
3805 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3806 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3807 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3808 cbea3800 2022-06-23 thomas goto done;
3809 cbea3800 2022-06-23 thomas }
3810 05171be4 2022-06-23 thomas
3811 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3812 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3813 cbea3800 2022-06-23 thomas col_tab_align, 1);
3814 cbea3800 2022-06-23 thomas if (err)
3815 cbea3800 2022-06-23 thomas goto done;
3816 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3817 05171be4 2022-06-23 thomas if (n) {
3818 cbea3800 2022-06-23 thomas free(wline);
3819 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3820 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3821 cbea3800 2022-06-23 thomas if (err)
3822 cbea3800 2022-06-23 thomas goto done;
3823 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3824 cbea3800 2022-06-23 thomas wlimit -= width;
3825 cbea3800 2022-06-23 thomas *wtotal += width;
3826 41605754 2020-11-12 stsp }
3827 41605754 2020-11-12 stsp
3828 41605754 2020-11-12 stsp if (wlimit > 0) {
3829 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3830 cbea3800 2022-06-23 thomas size_t wlen;
3831 cbea3800 2022-06-23 thomas
3832 cbea3800 2022-06-23 thomas free(wline);
3833 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3834 cbea3800 2022-06-23 thomas col_tab_align, 1);
3835 cbea3800 2022-06-23 thomas if (err)
3836 cbea3800 2022-06-23 thomas goto done;
3837 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3838 cbea3800 2022-06-23 thomas while (i < wlen) {
3839 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3840 cbea3800 2022-06-23 thomas if (width == -1) {
3841 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3842 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3843 cbea3800 2022-06-23 thomas goto done;
3844 cbea3800 2022-06-23 thomas }
3845 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3846 cbea3800 2022-06-23 thomas break;
3847 1c5e5faa 2022-06-23 thomas w += width;
3848 cbea3800 2022-06-23 thomas i++;
3849 41605754 2020-11-12 stsp }
3850 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3851 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3852 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3853 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3854 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3855 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3856 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3857 41605754 2020-11-12 stsp }
3858 41605754 2020-11-12 stsp }
3859 41605754 2020-11-12 stsp
3860 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3861 cbea3800 2022-06-23 thomas free(wline);
3862 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3863 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3864 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3865 cbea3800 2022-06-23 thomas col_tab_align, 1);
3866 cbea3800 2022-06-23 thomas if (err)
3867 cbea3800 2022-06-23 thomas goto done;
3868 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3869 cbea3800 2022-06-23 thomas } else {
3870 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3871 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3872 cbea3800 2022-06-23 thomas if (err)
3873 cbea3800 2022-06-23 thomas goto done;
3874 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3875 cbea3800 2022-06-23 thomas }
3876 cbea3800 2022-06-23 thomas *wtotal += width2;
3877 41605754 2020-11-12 stsp }
3878 cbea3800 2022-06-23 thomas done:
3879 05171be4 2022-06-23 thomas free(wline);
3880 666f7b10 2022-06-23 thomas free(exstr);
3881 cbea3800 2022-06-23 thomas free(seg0);
3882 cbea3800 2022-06-23 thomas free(seg1);
3883 cbea3800 2022-06-23 thomas free(seg2);
3884 cbea3800 2022-06-23 thomas return err;
3885 41605754 2020-11-12 stsp }
3886 41605754 2020-11-12 stsp
3887 41605754 2020-11-12 stsp static const struct got_error *
3888 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3889 26ed57b2 2018-05-19 stsp {
3890 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3891 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3892 61e69b96 2018-05-20 stsp const struct got_error *err;
3893 fe621944 2020-11-10 stsp int nprinted = 0;
3894 b304db33 2018-05-20 stsp char *line;
3895 826082fe 2020-12-10 stsp size_t linesize = 0;
3896 826082fe 2020-12-10 stsp ssize_t linelen;
3897 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3898 61e69b96 2018-05-20 stsp wchar_t *wline;
3899 e0b650dd 2018-05-20 stsp int width;
3900 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3901 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3902 fe621944 2020-11-10 stsp off_t line_offset;
3903 26ed57b2 2018-05-19 stsp
3904 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3905 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3906 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3907 fe621944 2020-11-10 stsp
3908 f7d12f7e 2018-08-01 stsp werase(view->window);
3909 a3404814 2018-09-02 stsp
3910 a3404814 2018-09-02 stsp if (header) {
3911 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3912 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3913 135a2da0 2020-11-11 stsp header) == -1)
3914 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3915 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3916 f91a2b48 2022-06-23 thomas 0, 0);
3917 135a2da0 2020-11-11 stsp free(line);
3918 135a2da0 2020-11-11 stsp if (err)
3919 a3404814 2018-09-02 stsp return err;
3920 a3404814 2018-09-02 stsp
3921 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3922 a3404814 2018-09-02 stsp wstandout(view->window);
3923 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3924 e54cc94a 2020-11-11 stsp free(wline);
3925 e54cc94a 2020-11-11 stsp wline = NULL;
3926 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3927 a3404814 2018-09-02 stsp wstandend(view->window);
3928 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3929 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3930 26ed57b2 2018-05-19 stsp
3931 a3404814 2018-09-02 stsp if (max_lines <= 1)
3932 a3404814 2018-09-02 stsp return NULL;
3933 a3404814 2018-09-02 stsp max_lines--;
3934 a3404814 2018-09-02 stsp }
3935 a3404814 2018-09-02 stsp
3936 89f1a395 2020-12-01 naddy s->eof = 0;
3937 05171be4 2022-06-23 thomas view->maxx = 0;
3938 826082fe 2020-12-10 stsp line = NULL;
3939 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3940 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3941 826082fe 2020-12-10 stsp if (linelen == -1) {
3942 826082fe 2020-12-10 stsp if (feof(s->f)) {
3943 826082fe 2020-12-10 stsp s->eof = 1;
3944 826082fe 2020-12-10 stsp break;
3945 826082fe 2020-12-10 stsp }
3946 826082fe 2020-12-10 stsp free(line);
3947 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3948 61e69b96 2018-05-20 stsp }
3949 05171be4 2022-06-23 thomas
3950 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3951 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3952 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3953 cbea3800 2022-06-23 thomas if (err) {
3954 cbea3800 2022-06-23 thomas free(line);
3955 cbea3800 2022-06-23 thomas return err;
3956 cbea3800 2022-06-23 thomas }
3957 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3958 cbea3800 2022-06-23 thomas free(wline);
3959 cbea3800 2022-06-23 thomas wline = NULL;
3960 cbea3800 2022-06-23 thomas
3961 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3962 f26dddb7 2019-11-08 stsp if (tc)
3963 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3964 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3965 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3966 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3967 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3968 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3969 41605754 2020-11-12 stsp if (err) {
3970 41605754 2020-11-12 stsp free(line);
3971 41605754 2020-11-12 stsp return err;
3972 41605754 2020-11-12 stsp }
3973 41605754 2020-11-12 stsp } else {
3974 f1c0ec19 2022-06-23 thomas int skip;
3975 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3976 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3977 f1c0ec19 2022-06-23 thomas if (err) {
3978 f1c0ec19 2022-06-23 thomas free(line);
3979 f1c0ec19 2022-06-23 thomas return err;
3980 f1c0ec19 2022-06-23 thomas }
3981 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3982 f1c0ec19 2022-06-23 thomas free(wline);
3983 41605754 2020-11-12 stsp wline = NULL;
3984 41605754 2020-11-12 stsp }
3985 f26dddb7 2019-11-08 stsp if (tc)
3986 6d17833f 2019-11-08 stsp wattr_off(view->window,
3987 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3988 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3989 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3990 fe621944 2020-11-10 stsp nprinted++;
3991 826082fe 2020-12-10 stsp }
3992 826082fe 2020-12-10 stsp free(line);
3993 fe621944 2020-11-10 stsp if (nprinted >= 1)
3994 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3995 89f1a395 2020-12-01 naddy (nprinted - 1);
3996 fe621944 2020-11-10 stsp else
3997 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3998 26ed57b2 2018-05-19 stsp
3999 a5d43cac 2022-07-01 thomas view_border(view);
4000 c3e9aa98 2019-05-13 jcs
4001 89f1a395 2020-12-01 naddy if (s->eof) {
4002 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4003 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4004 c3e9aa98 2019-05-13 jcs nprinted++;
4005 c3e9aa98 2019-05-13 jcs }
4006 c3e9aa98 2019-05-13 jcs
4007 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4008 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4009 c3e9aa98 2019-05-13 jcs if (err) {
4010 c3e9aa98 2019-05-13 jcs return err;
4011 c3e9aa98 2019-05-13 jcs }
4012 26ed57b2 2018-05-19 stsp
4013 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4014 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4015 e54cc94a 2020-11-11 stsp free(wline);
4016 e54cc94a 2020-11-11 stsp wline = NULL;
4017 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4018 c3e9aa98 2019-05-13 jcs }
4019 c3e9aa98 2019-05-13 jcs
4020 26ed57b2 2018-05-19 stsp return NULL;
4021 abd2672a 2018-12-23 stsp }
4022 abd2672a 2018-12-23 stsp
4023 abd2672a 2018-12-23 stsp static char *
4024 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4025 abd2672a 2018-12-23 stsp {
4026 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4027 09867e48 2019-08-13 stsp char *p, *s;
4028 09867e48 2019-08-13 stsp
4029 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4030 09867e48 2019-08-13 stsp if (tm == NULL)
4031 09867e48 2019-08-13 stsp return NULL;
4032 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4033 09867e48 2019-08-13 stsp if (s == NULL)
4034 09867e48 2019-08-13 stsp return NULL;
4035 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4036 abd2672a 2018-12-23 stsp if (p)
4037 abd2672a 2018-12-23 stsp *p = '\0';
4038 abd2672a 2018-12-23 stsp return s;
4039 9f7d7167 2018-04-29 stsp }
4040 9f7d7167 2018-04-29 stsp
4041 4ed7e80c 2018-05-20 stsp static const struct got_error *
4042 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4043 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4044 0208f208 2020-05-05 stsp {
4045 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4046 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4047 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4048 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4049 0208f208 2020-05-05 stsp
4050 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4051 0208f208 2020-05-05 stsp if (qid != NULL) {
4052 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4053 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4054 ec242592 2022-04-22 thomas &qid->id);
4055 0208f208 2020-05-05 stsp if (err)
4056 0208f208 2020-05-05 stsp return err;
4057 0208f208 2020-05-05 stsp
4058 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4059 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4060 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4061 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4062 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4063 aa8b5dd0 2021-08-01 stsp }
4064 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4065 0208f208 2020-05-05 stsp
4066 0208f208 2020-05-05 stsp }
4067 0208f208 2020-05-05 stsp
4068 0208f208 2020-05-05 stsp if (tree_id1) {
4069 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4070 0208f208 2020-05-05 stsp if (err)
4071 0208f208 2020-05-05 stsp goto done;
4072 0208f208 2020-05-05 stsp }
4073 0208f208 2020-05-05 stsp
4074 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4075 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4076 0208f208 2020-05-05 stsp if (err)
4077 0208f208 2020-05-05 stsp goto done;
4078 0208f208 2020-05-05 stsp
4079 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4080 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4081 0208f208 2020-05-05 stsp done:
4082 0208f208 2020-05-05 stsp if (tree1)
4083 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4084 0208f208 2020-05-05 stsp if (tree2)
4085 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4086 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4087 0208f208 2020-05-05 stsp return err;
4088 0208f208 2020-05-05 stsp }
4089 0208f208 2020-05-05 stsp
4090 0208f208 2020-05-05 stsp static const struct got_error *
4091 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4092 abd2672a 2018-12-23 stsp {
4093 fe621944 2020-11-10 stsp off_t *p;
4094 fe621944 2020-11-10 stsp
4095 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4096 fe621944 2020-11-10 stsp if (p == NULL)
4097 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4098 fe621944 2020-11-10 stsp *line_offsets = p;
4099 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4100 fe621944 2020-11-10 stsp (*nlines)++;
4101 fe621944 2020-11-10 stsp return NULL;
4102 fe621944 2020-11-10 stsp }
4103 fe621944 2020-11-10 stsp
4104 fe621944 2020-11-10 stsp static const struct got_error *
4105 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4106 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4107 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4108 fe621944 2020-11-10 stsp {
4109 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4110 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4111 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4112 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4113 45d799e2 2018-12-23 stsp time_t committer_time;
4114 45d799e2 2018-12-23 stsp const char *author, *committer;
4115 8b473291 2019-02-21 stsp char *refs_str = NULL;
4116 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4117 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4118 fe621944 2020-11-10 stsp off_t outoff = 0;
4119 fe621944 2020-11-10 stsp int n;
4120 abd2672a 2018-12-23 stsp
4121 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4122 0208f208 2020-05-05 stsp
4123 8b473291 2019-02-21 stsp if (refs) {
4124 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4125 8b473291 2019-02-21 stsp if (err)
4126 8b473291 2019-02-21 stsp return err;
4127 8b473291 2019-02-21 stsp }
4128 8b473291 2019-02-21 stsp
4129 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4130 abd2672a 2018-12-23 stsp if (err)
4131 abd2672a 2018-12-23 stsp return err;
4132 abd2672a 2018-12-23 stsp
4133 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4134 15a94983 2018-12-23 stsp if (err) {
4135 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4136 15a94983 2018-12-23 stsp goto done;
4137 15a94983 2018-12-23 stsp }
4138 abd2672a 2018-12-23 stsp
4139 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4140 fe621944 2020-11-10 stsp if (err)
4141 fe621944 2020-11-10 stsp goto done;
4142 fe621944 2020-11-10 stsp
4143 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4144 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4145 fe621944 2020-11-10 stsp if (n < 0) {
4146 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4147 abd2672a 2018-12-23 stsp goto done;
4148 abd2672a 2018-12-23 stsp }
4149 fe621944 2020-11-10 stsp outoff += n;
4150 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4151 fe621944 2020-11-10 stsp if (err)
4152 fe621944 2020-11-10 stsp goto done;
4153 fe621944 2020-11-10 stsp
4154 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4155 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4156 fe621944 2020-11-10 stsp if (n < 0) {
4157 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4158 abd2672a 2018-12-23 stsp goto done;
4159 abd2672a 2018-12-23 stsp }
4160 fe621944 2020-11-10 stsp outoff += n;
4161 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4162 fe621944 2020-11-10 stsp if (err)
4163 fe621944 2020-11-10 stsp goto done;
4164 fe621944 2020-11-10 stsp
4165 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4166 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4167 fe621944 2020-11-10 stsp if (datestr) {
4168 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4169 fe621944 2020-11-10 stsp if (n < 0) {
4170 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4171 fe621944 2020-11-10 stsp goto done;
4172 fe621944 2020-11-10 stsp }
4173 fe621944 2020-11-10 stsp outoff += n;
4174 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4175 fe621944 2020-11-10 stsp if (err)
4176 fe621944 2020-11-10 stsp goto done;
4177 abd2672a 2018-12-23 stsp }
4178 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4179 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4180 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4181 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4182 fe621944 2020-11-10 stsp if (n < 0) {
4183 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4184 fe621944 2020-11-10 stsp goto done;
4185 fe621944 2020-11-10 stsp }
4186 fe621944 2020-11-10 stsp outoff += n;
4187 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4188 fe621944 2020-11-10 stsp if (err)
4189 fe621944 2020-11-10 stsp goto done;
4190 abd2672a 2018-12-23 stsp }
4191 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4192 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4193 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4194 ac4dc263 2021-09-24 thomas int pn = 1;
4195 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4196 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4197 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4198 ac4dc263 2021-09-24 thomas if (err)
4199 ac4dc263 2021-09-24 thomas goto done;
4200 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4201 ac4dc263 2021-09-24 thomas if (n < 0) {
4202 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4203 ac4dc263 2021-09-24 thomas goto done;
4204 ac4dc263 2021-09-24 thomas }
4205 ac4dc263 2021-09-24 thomas outoff += n;
4206 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4207 ac4dc263 2021-09-24 thomas if (err)
4208 ac4dc263 2021-09-24 thomas goto done;
4209 ac4dc263 2021-09-24 thomas free(id_str);
4210 ac4dc263 2021-09-24 thomas id_str = NULL;
4211 ac4dc263 2021-09-24 thomas }
4212 ac4dc263 2021-09-24 thomas }
4213 ac4dc263 2021-09-24 thomas
4214 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4215 5943eee2 2019-08-13 stsp if (err)
4216 5943eee2 2019-08-13 stsp goto done;
4217 fe621944 2020-11-10 stsp s = logmsg;
4218 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4219 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4220 fe621944 2020-11-10 stsp if (n < 0) {
4221 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4222 fe621944 2020-11-10 stsp goto done;
4223 fe621944 2020-11-10 stsp }
4224 fe621944 2020-11-10 stsp outoff += n;
4225 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4226 fe621944 2020-11-10 stsp if (err)
4227 fe621944 2020-11-10 stsp goto done;
4228 abd2672a 2018-12-23 stsp }
4229 fe621944 2020-11-10 stsp
4230 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4231 0208f208 2020-05-05 stsp if (err)
4232 0208f208 2020-05-05 stsp goto done;
4233 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4234 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4235 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4236 fe621944 2020-11-10 stsp if (n < 0) {
4237 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4238 fe621944 2020-11-10 stsp goto done;
4239 fe621944 2020-11-10 stsp }
4240 fe621944 2020-11-10 stsp outoff += n;
4241 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4242 fe621944 2020-11-10 stsp if (err)
4243 fe621944 2020-11-10 stsp goto done;
4244 0208f208 2020-05-05 stsp free((char *)pe->path);
4245 0208f208 2020-05-05 stsp free(pe->data);
4246 0208f208 2020-05-05 stsp }
4247 fe621944 2020-11-10 stsp
4248 0208f208 2020-05-05 stsp fputc('\n', outfile);
4249 fe621944 2020-11-10 stsp outoff++;
4250 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4251 abd2672a 2018-12-23 stsp done:
4252 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4253 abd2672a 2018-12-23 stsp free(id_str);
4254 5943eee2 2019-08-13 stsp free(logmsg);
4255 8b473291 2019-02-21 stsp free(refs_str);
4256 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4257 7510f233 2020-08-09 stsp if (err) {
4258 ae6a6978 2020-08-09 stsp free(*line_offsets);
4259 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4260 ae6a6978 2020-08-09 stsp *nlines = 0;
4261 7510f233 2020-08-09 stsp }
4262 fe621944 2020-11-10 stsp return err;
4263 abd2672a 2018-12-23 stsp }
4264 abd2672a 2018-12-23 stsp
4265 abd2672a 2018-12-23 stsp static const struct got_error *
4266 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4267 26ed57b2 2018-05-19 stsp {
4268 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4269 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4270 15a94983 2018-12-23 stsp int obj_type;
4271 fe621944 2020-11-10 stsp
4272 fe621944 2020-11-10 stsp free(s->line_offsets);
4273 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4274 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4275 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4276 fe621944 2020-11-10 stsp s->nlines = 0;
4277 26ed57b2 2018-05-19 stsp
4278 511a516b 2018-05-19 stsp f = got_opentemp();
4279 48ae06ee 2018-10-18 stsp if (f == NULL) {
4280 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4281 48ae06ee 2018-10-18 stsp goto done;
4282 48ae06ee 2018-10-18 stsp }
4283 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4284 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4285 fb43ecf1 2019-02-11 stsp goto done;
4286 fb43ecf1 2019-02-11 stsp }
4287 48ae06ee 2018-10-18 stsp s->f = f;
4288 26ed57b2 2018-05-19 stsp
4289 15a94983 2018-12-23 stsp if (s->id1)
4290 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4291 15a94983 2018-12-23 stsp else
4292 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4293 15a94983 2018-12-23 stsp if (err)
4294 15a94983 2018-12-23 stsp goto done;
4295 15a94983 2018-12-23 stsp
4296 15a94983 2018-12-23 stsp switch (obj_type) {
4297 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4298 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4299 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4300 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4301 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4302 26ed57b2 2018-05-19 stsp break;
4303 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4304 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4305 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4306 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4307 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4308 26ed57b2 2018-05-19 stsp break;
4309 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4310 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4311 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4312 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4313 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4314 abd2672a 2018-12-23 stsp
4315 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4316 abd2672a 2018-12-23 stsp if (err)
4317 3ffacbe1 2020-02-02 tracey goto done;
4318 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4319 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4320 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4321 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4322 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4323 f44b1f58 2020-02-02 tracey if (err)
4324 f44b1f58 2020-02-02 tracey goto done;
4325 f44b1f58 2020-02-02 tracey } else {
4326 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4327 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4328 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4329 fe621944 2020-11-10 stsp err = write_commit_info(
4330 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4331 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4332 f44b1f58 2020-02-02 tracey if (err)
4333 f44b1f58 2020-02-02 tracey goto done;
4334 f5404e4e 2020-02-02 tracey break;
4335 15a087fe 2019-02-21 stsp }
4336 abd2672a 2018-12-23 stsp }
4337 abd2672a 2018-12-23 stsp }
4338 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4339 abd2672a 2018-12-23 stsp
4340 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4341 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4342 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4343 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4344 26ed57b2 2018-05-19 stsp break;
4345 abd2672a 2018-12-23 stsp }
4346 26ed57b2 2018-05-19 stsp default:
4347 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4348 48ae06ee 2018-10-18 stsp break;
4349 26ed57b2 2018-05-19 stsp }
4350 f44b1f58 2020-02-02 tracey if (err)
4351 f44b1f58 2020-02-02 tracey goto done;
4352 48ae06ee 2018-10-18 stsp done:
4353 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4354 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4355 48ae06ee 2018-10-18 stsp return err;
4356 48ae06ee 2018-10-18 stsp }
4357 26ed57b2 2018-05-19 stsp
4358 f5215bb9 2019-02-22 stsp static void
4359 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4360 f5215bb9 2019-02-22 stsp {
4361 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4362 f5215bb9 2019-02-22 stsp update_panels();
4363 f5215bb9 2019-02-22 stsp doupdate();
4364 f44b1f58 2020-02-02 tracey }
4365 f44b1f58 2020-02-02 tracey
4366 f44b1f58 2020-02-02 tracey static const struct got_error *
4367 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4368 f44b1f58 2020-02-02 tracey {
4369 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4370 f44b1f58 2020-02-02 tracey
4371 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4372 f44b1f58 2020-02-02 tracey return NULL;
4373 f44b1f58 2020-02-02 tracey }
4374 f44b1f58 2020-02-02 tracey
4375 f44b1f58 2020-02-02 tracey static const struct got_error *
4376 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4377 f44b1f58 2020-02-02 tracey {
4378 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4379 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4380 f44b1f58 2020-02-02 tracey int lineno;
4381 78eecffc 2022-06-23 thomas char *line = NULL;
4382 826082fe 2020-12-10 stsp size_t linesize = 0;
4383 826082fe 2020-12-10 stsp ssize_t linelen;
4384 f44b1f58 2020-02-02 tracey
4385 f44b1f58 2020-02-02 tracey if (!view->searching) {
4386 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4387 f44b1f58 2020-02-02 tracey return NULL;
4388 f44b1f58 2020-02-02 tracey }
4389 f44b1f58 2020-02-02 tracey
4390 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4391 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4392 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4393 f44b1f58 2020-02-02 tracey else
4394 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4395 4b3f9dac 2021-12-17 thomas } else
4396 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4397 f44b1f58 2020-02-02 tracey
4398 f44b1f58 2020-02-02 tracey while (1) {
4399 f44b1f58 2020-02-02 tracey off_t offset;
4400 f44b1f58 2020-02-02 tracey
4401 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4402 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4403 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4404 f44b1f58 2020-02-02 tracey break;
4405 f44b1f58 2020-02-02 tracey }
4406 f44b1f58 2020-02-02 tracey
4407 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4408 f44b1f58 2020-02-02 tracey lineno = 1;
4409 f44b1f58 2020-02-02 tracey else
4410 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4411 f44b1f58 2020-02-02 tracey }
4412 f44b1f58 2020-02-02 tracey
4413 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4414 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4415 f44b1f58 2020-02-02 tracey free(line);
4416 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4417 f44b1f58 2020-02-02 tracey }
4418 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4419 78eecffc 2022-06-23 thomas if (linelen != -1) {
4420 78eecffc 2022-06-23 thomas char *exstr;
4421 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4422 78eecffc 2022-06-23 thomas if (err)
4423 78eecffc 2022-06-23 thomas break;
4424 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4425 78eecffc 2022-06-23 thomas &view->regmatch)) {
4426 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4427 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4428 78eecffc 2022-06-23 thomas free(exstr);
4429 78eecffc 2022-06-23 thomas break;
4430 78eecffc 2022-06-23 thomas }
4431 78eecffc 2022-06-23 thomas free(exstr);
4432 f44b1f58 2020-02-02 tracey }
4433 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4434 f44b1f58 2020-02-02 tracey lineno++;
4435 f44b1f58 2020-02-02 tracey else
4436 f44b1f58 2020-02-02 tracey lineno--;
4437 f44b1f58 2020-02-02 tracey }
4438 826082fe 2020-12-10 stsp free(line);
4439 f44b1f58 2020-02-02 tracey
4440 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4441 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4442 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4443 f44b1f58 2020-02-02 tracey }
4444 f44b1f58 2020-02-02 tracey
4445 05171be4 2022-06-23 thomas return err;
4446 f5215bb9 2019-02-22 stsp }
4447 f5215bb9 2019-02-22 stsp
4448 48ae06ee 2018-10-18 stsp static const struct got_error *
4449 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4450 a0f32f33 2022-06-13 thomas {
4451 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4452 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4453 a0f32f33 2022-06-13 thomas
4454 a0f32f33 2022-06-13 thomas free(s->id1);
4455 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4456 a0f32f33 2022-06-13 thomas free(s->id2);
4457 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4458 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4459 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4460 a0f32f33 2022-06-13 thomas s->f = NULL;
4461 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4462 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4463 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4464 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4465 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4466 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4467 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4468 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4469 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4470 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4471 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4472 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4473 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4474 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4475 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4476 a0f32f33 2022-06-13 thomas s->nlines = 0;
4477 a0f32f33 2022-06-13 thomas return err;
4478 a0f32f33 2022-06-13 thomas }
4479 a0f32f33 2022-06-13 thomas
4480 a0f32f33 2022-06-13 thomas static const struct got_error *
4481 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4482 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4483 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4484 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4485 48ae06ee 2018-10-18 stsp {
4486 48ae06ee 2018-10-18 stsp const struct got_error *err;
4487 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4488 5dc9f4bc 2018-08-04 stsp
4489 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4490 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4491 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4492 a0f32f33 2022-06-13 thomas
4493 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4494 15a94983 2018-12-23 stsp int type1, type2;
4495 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4496 15a94983 2018-12-23 stsp if (err)
4497 15a94983 2018-12-23 stsp return err;
4498 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4499 15a94983 2018-12-23 stsp if (err)
4500 15a94983 2018-12-23 stsp return err;
4501 15a94983 2018-12-23 stsp
4502 15a94983 2018-12-23 stsp if (type1 != type2)
4503 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4504 15a94983 2018-12-23 stsp }
4505 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4506 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4507 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4508 f44b1f58 2020-02-02 tracey s->repo = repo;
4509 f44b1f58 2020-02-02 tracey s->id1 = id1;
4510 f44b1f58 2020-02-02 tracey s->id2 = id2;
4511 3dbaef42 2020-11-24 stsp s->label1 = label1;
4512 3dbaef42 2020-11-24 stsp s->label2 = label2;
4513 48ae06ee 2018-10-18 stsp
4514 15a94983 2018-12-23 stsp if (id1) {
4515 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4516 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4517 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4518 48ae06ee 2018-10-18 stsp } else
4519 5465d566 2020-02-01 tracey s->id1 = NULL;
4520 48ae06ee 2018-10-18 stsp
4521 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4522 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4523 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4524 a0f32f33 2022-06-13 thomas goto done;
4525 48ae06ee 2018-10-18 stsp }
4526 a0f32f33 2022-06-13 thomas
4527 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4528 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4529 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4530 9a1d7435 2022-06-23 thomas goto done;
4531 9a1d7435 2022-06-23 thomas }
4532 9a1d7435 2022-06-23 thomas
4533 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4534 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4535 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4536 a0f32f33 2022-06-13 thomas goto done;
4537 a0f32f33 2022-06-13 thomas }
4538 a0f32f33 2022-06-13 thomas
4539 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4540 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4541 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4542 19a6a6b5 2022-07-01 thomas goto done;
4543 19a6a6b5 2022-07-01 thomas }
4544 19a6a6b5 2022-07-01 thomas
4545 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4546 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4547 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4548 19a6a6b5 2022-07-01 thomas goto done;
4549 19a6a6b5 2022-07-01 thomas }
4550 19a6a6b5 2022-07-01 thomas
4551 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4552 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4553 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4554 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4555 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4556 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4557 5465d566 2020-02-01 tracey s->repo = repo;
4558 6d17833f 2019-11-08 stsp
4559 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4560 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4561 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4562 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4563 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4564 6d17833f 2019-11-08 stsp if (err)
4565 a0f32f33 2022-06-13 thomas goto done;
4566 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4567 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4568 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4569 a0f32f33 2022-06-13 thomas if (err)
4570 a0f32f33 2022-06-13 thomas goto done;
4571 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4572 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4573 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4574 a0f32f33 2022-06-13 thomas if (err)
4575 a0f32f33 2022-06-13 thomas goto done;
4576 6d17833f 2019-11-08 stsp
4577 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4578 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4579 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4580 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4581 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4582 a0f32f33 2022-06-13 thomas if (err)
4583 a0f32f33 2022-06-13 thomas goto done;
4584 11b20872 2019-11-08 stsp
4585 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4586 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4587 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4588 a0f32f33 2022-06-13 thomas if (err)
4589 a0f32f33 2022-06-13 thomas goto done;
4590 11b20872 2019-11-08 stsp
4591 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4592 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4593 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4594 a0f32f33 2022-06-13 thomas if (err)
4595 a0f32f33 2022-06-13 thomas goto done;
4596 6d17833f 2019-11-08 stsp }
4597 5dc9f4bc 2018-08-04 stsp
4598 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4599 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4600 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4601 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4602 f5215bb9 2019-02-22 stsp
4603 5465d566 2020-02-01 tracey err = create_diff(s);
4604 48ae06ee 2018-10-18 stsp
4605 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4606 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4607 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4608 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4609 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4610 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4611 a0f32f33 2022-06-13 thomas done:
4612 a0f32f33 2022-06-13 thomas if (err)
4613 a0f32f33 2022-06-13 thomas close_diff_view(view);
4614 e5a0f69f 2018-08-18 stsp return err;
4615 5dc9f4bc 2018-08-04 stsp }
4616 5dc9f4bc 2018-08-04 stsp
4617 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4618 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4619 5dc9f4bc 2018-08-04 stsp {
4620 a3404814 2018-09-02 stsp const struct got_error *err;
4621 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4622 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4623 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4624 a3404814 2018-09-02 stsp
4625 a3404814 2018-09-02 stsp if (s->id1) {
4626 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4627 a3404814 2018-09-02 stsp if (err)
4628 a3404814 2018-09-02 stsp return err;
4629 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4630 3dbaef42 2020-11-24 stsp } else
4631 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4632 3dbaef42 2020-11-24 stsp
4633 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4634 a3404814 2018-09-02 stsp if (err)
4635 a3404814 2018-09-02 stsp return err;
4636 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4637 26ed57b2 2018-05-19 stsp
4638 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4639 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4640 a3404814 2018-09-02 stsp free(id_str1);
4641 a3404814 2018-09-02 stsp free(id_str2);
4642 a3404814 2018-09-02 stsp return err;
4643 a3404814 2018-09-02 stsp }
4644 a3404814 2018-09-02 stsp free(id_str1);
4645 a3404814 2018-09-02 stsp free(id_str2);
4646 a3404814 2018-09-02 stsp
4647 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4648 267bb3b8 2021-08-01 stsp free(header);
4649 267bb3b8 2021-08-01 stsp return err;
4650 15a087fe 2019-02-21 stsp }
4651 15a087fe 2019-02-21 stsp
4652 15a087fe 2019-02-21 stsp static const struct got_error *
4653 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4654 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4655 15a087fe 2019-02-21 stsp {
4656 d7a04538 2019-02-21 stsp const struct got_error *err;
4657 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4658 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4659 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4660 15a087fe 2019-02-21 stsp
4661 15a087fe 2019-02-21 stsp free(s->id2);
4662 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4663 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4664 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4665 15a087fe 2019-02-21 stsp
4666 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4667 d7a04538 2019-02-21 stsp if (err)
4668 d7a04538 2019-02-21 stsp return err;
4669 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4670 15a087fe 2019-02-21 stsp free(s->id1);
4671 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4672 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4673 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4674 15a087fe 2019-02-21 stsp return NULL;
4675 0cf4efb1 2018-09-29 stsp }
4676 0cf4efb1 2018-09-29 stsp
4677 0cf4efb1 2018-09-29 stsp static const struct got_error *
4678 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4679 adf4c9e0 2022-07-03 thomas {
4680 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4681 adf4c9e0 2022-07-03 thomas
4682 adf4c9e0 2022-07-03 thomas view->count = 0;
4683 adf4c9e0 2022-07-03 thomas wclear(view->window);
4684 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4685 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4686 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4687 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4688 adf4c9e0 2022-07-03 thomas return create_diff(s);
4689 adf4c9e0 2022-07-03 thomas }
4690 adf4c9e0 2022-07-03 thomas
4691 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4692 4fc71f3b 2022-07-12 thomas int, int, int);
4693 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4694 4fc71f3b 2022-07-12 thomas int, int);
4695 4fc71f3b 2022-07-12 thomas
4696 adf4c9e0 2022-07-03 thomas static const struct got_error *
4697 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4698 e5a0f69f 2018-08-18 stsp {
4699 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4700 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4701 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4702 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4703 826082fe 2020-12-10 stsp char *line = NULL;
4704 826082fe 2020-12-10 stsp size_t linesize = 0;
4705 826082fe 2020-12-10 stsp ssize_t linelen;
4706 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4707 e5a0f69f 2018-08-18 stsp
4708 e5a0f69f 2018-08-18 stsp switch (ch) {
4709 05171be4 2022-06-23 thomas case '0':
4710 05171be4 2022-06-23 thomas view->x = 0;
4711 05171be4 2022-06-23 thomas break;
4712 05171be4 2022-06-23 thomas case '$':
4713 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4714 07b0611c 2022-06-23 thomas view->count = 0;
4715 05171be4 2022-06-23 thomas break;
4716 05171be4 2022-06-23 thomas case KEY_RIGHT:
4717 05171be4 2022-06-23 thomas case 'l':
4718 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4719 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4720 07b0611c 2022-06-23 thomas else
4721 07b0611c 2022-06-23 thomas view->count = 0;
4722 05171be4 2022-06-23 thomas break;
4723 05171be4 2022-06-23 thomas case KEY_LEFT:
4724 05171be4 2022-06-23 thomas case 'h':
4725 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4726 07b0611c 2022-06-23 thomas if (view->x <= 0)
4727 07b0611c 2022-06-23 thomas view->count = 0;
4728 05171be4 2022-06-23 thomas break;
4729 64453f7e 2020-11-21 stsp case 'a':
4730 3dbaef42 2020-11-24 stsp case 'w':
4731 3dbaef42 2020-11-24 stsp if (ch == 'a')
4732 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4733 3dbaef42 2020-11-24 stsp if (ch == 'w')
4734 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4735 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4736 912a3f79 2021-08-30 j break;
4737 912a3f79 2021-08-30 j case 'g':
4738 912a3f79 2021-08-30 j case KEY_HOME:
4739 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4740 07b0611c 2022-06-23 thomas view->count = 0;
4741 64453f7e 2020-11-21 stsp break;
4742 912a3f79 2021-08-30 j case 'G':
4743 912a3f79 2021-08-30 j case KEY_END:
4744 07b0611c 2022-06-23 thomas view->count = 0;
4745 912a3f79 2021-08-30 j if (s->eof)
4746 912a3f79 2021-08-30 j break;
4747 912a3f79 2021-08-30 j
4748 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4749 912a3f79 2021-08-30 j s->eof = 1;
4750 912a3f79 2021-08-30 j break;
4751 1e37a5c2 2019-05-12 jcs case 'k':
4752 1e37a5c2 2019-05-12 jcs case KEY_UP:
4753 f7140bf5 2021-10-17 thomas case CTRL('p'):
4754 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4755 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4756 07b0611c 2022-06-23 thomas else
4757 07b0611c 2022-06-23 thomas view->count = 0;
4758 1e37a5c2 2019-05-12 jcs break;
4759 70f17a53 2022-06-13 thomas case CTRL('u'):
4760 23427b14 2022-06-23 thomas case 'u':
4761 70f17a53 2022-06-13 thomas nscroll /= 2;
4762 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4763 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4764 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4765 1c5e5faa 2022-06-23 thomas case 'b':
4766 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4767 07b0611c 2022-06-23 thomas view->count = 0;
4768 26ed57b2 2018-05-19 stsp break;
4769 07b0611c 2022-06-23 thomas }
4770 1e37a5c2 2019-05-12 jcs i = 0;
4771 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4772 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4773 1e37a5c2 2019-05-12 jcs break;
4774 1e37a5c2 2019-05-12 jcs case 'j':
4775 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4776 f7140bf5 2021-10-17 thomas case CTRL('n'):
4777 1e37a5c2 2019-05-12 jcs if (!s->eof)
4778 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4779 07b0611c 2022-06-23 thomas else
4780 07b0611c 2022-06-23 thomas view->count = 0;
4781 1e37a5c2 2019-05-12 jcs break;
4782 70f17a53 2022-06-13 thomas case CTRL('d'):
4783 23427b14 2022-06-23 thomas case 'd':
4784 70f17a53 2022-06-13 thomas nscroll /= 2;
4785 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4786 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4787 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4788 1c5e5faa 2022-06-23 thomas case 'f':
4789 1e37a5c2 2019-05-12 jcs case ' ':
4790 07b0611c 2022-06-23 thomas if (s->eof) {
4791 07b0611c 2022-06-23 thomas view->count = 0;
4792 1e37a5c2 2019-05-12 jcs break;
4793 07b0611c 2022-06-23 thomas }
4794 1e37a5c2 2019-05-12 jcs i = 0;
4795 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4796 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4797 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4798 826082fe 2020-12-10 stsp if (linelen == -1) {
4799 826082fe 2020-12-10 stsp if (feof(s->f)) {
4800 826082fe 2020-12-10 stsp s->eof = 1;
4801 826082fe 2020-12-10 stsp } else
4802 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4803 34bc9ec9 2019-02-22 stsp break;
4804 826082fe 2020-12-10 stsp }
4805 1e37a5c2 2019-05-12 jcs }
4806 826082fe 2020-12-10 stsp free(line);
4807 1e37a5c2 2019-05-12 jcs break;
4808 1e37a5c2 2019-05-12 jcs case '[':
4809 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4810 1e37a5c2 2019-05-12 jcs s->diff_context--;
4811 aa61903a 2021-12-31 thomas s->matched_line = 0;
4812 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4813 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4814 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4815 27829c9e 2020-11-21 stsp s->nlines) {
4816 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4817 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4818 27829c9e 2020-11-21 stsp }
4819 07b0611c 2022-06-23 thomas } else
4820 07b0611c 2022-06-23 thomas view->count = 0;
4821 1e37a5c2 2019-05-12 jcs break;
4822 1e37a5c2 2019-05-12 jcs case ']':
4823 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4824 1e37a5c2 2019-05-12 jcs s->diff_context++;
4825 aa61903a 2021-12-31 thomas s->matched_line = 0;
4826 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4827 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4828 07b0611c 2022-06-23 thomas } else
4829 07b0611c 2022-06-23 thomas view->count = 0;
4830 1e37a5c2 2019-05-12 jcs break;
4831 1e37a5c2 2019-05-12 jcs case '<':
4832 1e37a5c2 2019-05-12 jcs case ',':
4833 777aae21 2022-07-20 thomas case 'K':
4834 4fc71f3b 2022-07-12 thomas up = 1;
4835 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4836 4fc71f3b 2022-07-12 thomas case '>':
4837 4fc71f3b 2022-07-12 thomas case '.':
4838 777aae21 2022-07-20 thomas case 'J':
4839 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4840 07b0611c 2022-06-23 thomas view->count = 0;
4841 48ae06ee 2018-10-18 stsp break;
4842 07b0611c 2022-06-23 thomas }
4843 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4844 6524637e 2019-02-21 stsp
4845 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4846 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4847 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4848 15a087fe 2019-02-21 stsp
4849 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4850 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4851 4fc71f3b 2022-07-12 thomas if (err)
4852 4fc71f3b 2022-07-12 thomas break;
4853 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4854 15a087fe 2019-02-21 stsp
4855 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4856 4fc71f3b 2022-07-12 thomas break;
4857 15a087fe 2019-02-21 stsp
4858 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4859 4fc71f3b 2022-07-12 thomas if (err)
4860 4fc71f3b 2022-07-12 thomas break;
4861 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4862 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4863 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4864 5e224a3e 2019-02-22 stsp
4865 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4866 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4867 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4868 4fc71f3b 2022-07-12 thomas
4869 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4870 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4871 4fc71f3b 2022-07-12 thomas if (err)
4872 4fc71f3b 2022-07-12 thomas break;
4873 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4874 5a8b5076 2020-12-05 stsp
4875 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4876 4fc71f3b 2022-07-12 thomas break;
4877 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4878 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4879 4fc71f3b 2022-07-12 thomas bs->selected_line);
4880 4fc71f3b 2022-07-12 thomas if (id == NULL)
4881 4fc71f3b 2022-07-12 thomas break;
4882 15a087fe 2019-02-21 stsp
4883 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4884 4fc71f3b 2022-07-12 thomas break;
4885 15a087fe 2019-02-21 stsp
4886 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4887 4fc71f3b 2022-07-12 thomas if (err)
4888 4fc71f3b 2022-07-12 thomas break;
4889 4fc71f3b 2022-07-12 thomas }
4890 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4891 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4892 aa61903a 2021-12-31 thomas s->matched_line = 0;
4893 05171be4 2022-06-23 thomas view->x = 0;
4894 1e37a5c2 2019-05-12 jcs
4895 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4896 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4897 1e37a5c2 2019-05-12 jcs break;
4898 1e37a5c2 2019-05-12 jcs default:
4899 07b0611c 2022-06-23 thomas view->count = 0;
4900 1e37a5c2 2019-05-12 jcs break;
4901 26ed57b2 2018-05-19 stsp }
4902 e5a0f69f 2018-08-18 stsp
4903 bcbd79e2 2018-08-19 stsp return err;
4904 26ed57b2 2018-05-19 stsp }
4905 26ed57b2 2018-05-19 stsp
4906 4ed7e80c 2018-05-20 stsp static const struct got_error *
4907 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4908 9f7d7167 2018-04-29 stsp {
4909 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4910 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4911 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4912 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4913 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4914 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4915 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4916 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4917 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4918 3dbaef42 2020-11-24 stsp const char *errstr;
4919 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4920 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4921 70ac5f84 2019-03-28 stsp
4922 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4923 26ed57b2 2018-05-19 stsp switch (ch) {
4924 64453f7e 2020-11-21 stsp case 'a':
4925 64453f7e 2020-11-21 stsp force_text_diff = 1;
4926 3dbaef42 2020-11-24 stsp break;
4927 3dbaef42 2020-11-24 stsp case 'C':
4928 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4929 3dbaef42 2020-11-24 stsp &errstr);
4930 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4931 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4932 8f666e67 2022-02-12 thomas errstr, errstr);
4933 64453f7e 2020-11-21 stsp break;
4934 09b5bff8 2020-02-23 naddy case 'r':
4935 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4936 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4937 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4938 09b5bff8 2020-02-23 naddy optarg);
4939 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4940 09b5bff8 2020-02-23 naddy break;
4941 3dbaef42 2020-11-24 stsp case 'w':
4942 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4943 3dbaef42 2020-11-24 stsp break;
4944 26ed57b2 2018-05-19 stsp default:
4945 17020d27 2019-03-07 stsp usage_diff();
4946 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4947 26ed57b2 2018-05-19 stsp }
4948 26ed57b2 2018-05-19 stsp }
4949 26ed57b2 2018-05-19 stsp
4950 26ed57b2 2018-05-19 stsp argc -= optind;
4951 26ed57b2 2018-05-19 stsp argv += optind;
4952 26ed57b2 2018-05-19 stsp
4953 26ed57b2 2018-05-19 stsp if (argc == 0) {
4954 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4955 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4956 15a94983 2018-12-23 stsp id_str1 = argv[0];
4957 15a94983 2018-12-23 stsp id_str2 = argv[1];
4958 26ed57b2 2018-05-19 stsp } else
4959 26ed57b2 2018-05-19 stsp usage_diff();
4960 eb6600df 2019-01-04 stsp
4961 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4962 7cd52833 2022-06-23 thomas if (error)
4963 7cd52833 2022-06-23 thomas goto done;
4964 7cd52833 2022-06-23 thomas
4965 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4966 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4967 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4968 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4969 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4970 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4971 c156c7a4 2020-12-18 stsp goto done;
4972 a273ac94 2020-02-23 naddy if (worktree)
4973 a273ac94 2020-02-23 naddy repo_path =
4974 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4975 a273ac94 2020-02-23 naddy else
4976 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4977 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4978 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4979 c156c7a4 2020-12-18 stsp goto done;
4980 c156c7a4 2020-12-18 stsp }
4981 a273ac94 2020-02-23 naddy }
4982 a273ac94 2020-02-23 naddy
4983 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4984 eb6600df 2019-01-04 stsp if (error)
4985 eb6600df 2019-01-04 stsp goto done;
4986 26ed57b2 2018-05-19 stsp
4987 a273ac94 2020-02-23 naddy init_curses();
4988 a273ac94 2020-02-23 naddy
4989 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4990 51a10b52 2020-12-26 stsp if (error)
4991 51a10b52 2020-12-26 stsp goto done;
4992 51a10b52 2020-12-26 stsp
4993 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4994 26ed57b2 2018-05-19 stsp if (error)
4995 26ed57b2 2018-05-19 stsp goto done;
4996 26ed57b2 2018-05-19 stsp
4997 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4998 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4999 26ed57b2 2018-05-19 stsp if (error)
5000 26ed57b2 2018-05-19 stsp goto done;
5001 26ed57b2 2018-05-19 stsp
5002 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5003 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5004 26ed57b2 2018-05-19 stsp if (error)
5005 26ed57b2 2018-05-19 stsp goto done;
5006 26ed57b2 2018-05-19 stsp
5007 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5008 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5009 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5010 ea5e7bb5 2018-08-01 stsp goto done;
5011 ea5e7bb5 2018-08-01 stsp }
5012 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5013 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5014 5dc9f4bc 2018-08-04 stsp if (error)
5015 5dc9f4bc 2018-08-04 stsp goto done;
5016 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5017 26ed57b2 2018-05-19 stsp done:
5018 3dbaef42 2020-11-24 stsp free(label1);
5019 3dbaef42 2020-11-24 stsp free(label2);
5020 c02c541e 2019-03-29 stsp free(repo_path);
5021 a273ac94 2020-02-23 naddy free(cwd);
5022 1d0f4054 2021-06-17 stsp if (repo) {
5023 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5024 1d0f4054 2021-06-17 stsp if (error == NULL)
5025 1d0f4054 2021-06-17 stsp error = close_err;
5026 1d0f4054 2021-06-17 stsp }
5027 a273ac94 2020-02-23 naddy if (worktree)
5028 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5029 7cd52833 2022-06-23 thomas if (pack_fds) {
5030 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5031 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5032 7cd52833 2022-06-23 thomas if (error == NULL)
5033 7cd52833 2022-06-23 thomas error = pack_err;
5034 7cd52833 2022-06-23 thomas }
5035 51a10b52 2020-12-26 stsp tog_free_refs();
5036 26ed57b2 2018-05-19 stsp return error;
5037 9f7d7167 2018-04-29 stsp }
5038 9f7d7167 2018-04-29 stsp
5039 4ed7e80c 2018-05-20 stsp __dead static void
5040 9f7d7167 2018-04-29 stsp usage_blame(void)
5041 9f7d7167 2018-04-29 stsp {
5042 80ddbec8 2018-04-29 stsp endwin();
5043 91198554 2022-06-23 thomas fprintf(stderr,
5044 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5045 9f7d7167 2018-04-29 stsp getprogname());
5046 9f7d7167 2018-04-29 stsp exit(1);
5047 9f7d7167 2018-04-29 stsp }
5048 84451b3e 2018-07-10 stsp
5049 84451b3e 2018-07-10 stsp struct tog_blame_line {
5050 84451b3e 2018-07-10 stsp int annotated;
5051 84451b3e 2018-07-10 stsp struct got_object_id *id;
5052 84451b3e 2018-07-10 stsp };
5053 9f7d7167 2018-04-29 stsp
5054 4ed7e80c 2018-05-20 stsp static const struct got_error *
5055 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5056 84451b3e 2018-07-10 stsp {
5057 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5058 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5059 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5060 84451b3e 2018-07-10 stsp const struct got_error *err;
5061 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5062 826082fe 2020-12-10 stsp char *line = NULL;
5063 826082fe 2020-12-10 stsp size_t linesize = 0;
5064 826082fe 2020-12-10 stsp ssize_t linelen;
5065 84451b3e 2018-07-10 stsp wchar_t *wline;
5066 27a741e5 2019-09-11 stsp int width;
5067 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5068 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5069 ab089a2a 2018-07-12 stsp char *id_str;
5070 11b20872 2019-11-08 stsp struct tog_color *tc;
5071 ab089a2a 2018-07-12 stsp
5072 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5073 ab089a2a 2018-07-12 stsp if (err)
5074 ab089a2a 2018-07-12 stsp return err;
5075 84451b3e 2018-07-10 stsp
5076 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5077 f7d12f7e 2018-08-01 stsp werase(view->window);
5078 84451b3e 2018-07-10 stsp
5079 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5080 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5081 ab089a2a 2018-07-12 stsp free(id_str);
5082 ab089a2a 2018-07-12 stsp return err;
5083 ab089a2a 2018-07-12 stsp }
5084 ab089a2a 2018-07-12 stsp
5085 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5086 ab089a2a 2018-07-12 stsp free(line);
5087 2550e4c3 2018-07-13 stsp line = NULL;
5088 1cae65b4 2019-09-22 stsp if (err)
5089 1cae65b4 2019-09-22 stsp return err;
5090 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5091 a3404814 2018-09-02 stsp wstandout(view->window);
5092 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5093 11b20872 2019-11-08 stsp if (tc)
5094 11b20872 2019-11-08 stsp wattr_on(view->window,
5095 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5096 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5097 11b20872 2019-11-08 stsp if (tc)
5098 11b20872 2019-11-08 stsp wattr_off(view->window,
5099 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5100 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5101 a3404814 2018-09-02 stsp wstandend(view->window);
5102 2550e4c3 2018-07-13 stsp free(wline);
5103 2550e4c3 2018-07-13 stsp wline = NULL;
5104 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5105 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5106 ab089a2a 2018-07-12 stsp
5107 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5108 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5109 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5110 ab089a2a 2018-07-12 stsp free(id_str);
5111 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5112 ab089a2a 2018-07-12 stsp }
5113 ab089a2a 2018-07-12 stsp free(id_str);
5114 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5115 3f60a8ef 2018-07-10 stsp free(line);
5116 2550e4c3 2018-07-13 stsp line = NULL;
5117 3f60a8ef 2018-07-10 stsp if (err)
5118 3f60a8ef 2018-07-10 stsp return err;
5119 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5120 2550e4c3 2018-07-13 stsp free(wline);
5121 2550e4c3 2018-07-13 stsp wline = NULL;
5122 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5123 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5124 3f60a8ef 2018-07-10 stsp
5125 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5126 05171be4 2022-06-23 thomas view->maxx = 0;
5127 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5128 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5129 826082fe 2020-12-10 stsp if (linelen == -1) {
5130 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5131 826082fe 2020-12-10 stsp s->eof = 1;
5132 826082fe 2020-12-10 stsp break;
5133 826082fe 2020-12-10 stsp }
5134 84451b3e 2018-07-10 stsp free(line);
5135 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5136 84451b3e 2018-07-10 stsp }
5137 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5138 826082fe 2020-12-10 stsp continue;
5139 cbea3800 2022-06-23 thomas
5140 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5141 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5142 cbea3800 2022-06-23 thomas if (err) {
5143 cbea3800 2022-06-23 thomas free(line);
5144 cbea3800 2022-06-23 thomas return err;
5145 cbea3800 2022-06-23 thomas }
5146 cbea3800 2022-06-23 thomas free(wline);
5147 cbea3800 2022-06-23 thomas wline = NULL;
5148 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5149 84451b3e 2018-07-10 stsp
5150 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5151 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5152 b700b5d6 2018-07-10 stsp
5153 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5154 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5155 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5156 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5157 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5158 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5159 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5160 8d0fe45a 2019-08-12 stsp char *id_str;
5161 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5162 91198554 2022-06-23 thomas blame_line->id);
5163 8d0fe45a 2019-08-12 stsp if (err) {
5164 8d0fe45a 2019-08-12 stsp free(line);
5165 8d0fe45a 2019-08-12 stsp return err;
5166 8d0fe45a 2019-08-12 stsp }
5167 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5168 11b20872 2019-11-08 stsp if (tc)
5169 11b20872 2019-11-08 stsp wattr_on(view->window,
5170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5171 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5172 11b20872 2019-11-08 stsp if (tc)
5173 11b20872 2019-11-08 stsp wattr_off(view->window,
5174 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5175 8d0fe45a 2019-08-12 stsp free(id_str);
5176 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5177 8d0fe45a 2019-08-12 stsp } else {
5178 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5179 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5180 84451b3e 2018-07-10 stsp }
5181 ee41ec32 2018-07-10 stsp } else {
5182 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5183 ee41ec32 2018-07-10 stsp prev_id = NULL;
5184 ee41ec32 2018-07-10 stsp }
5185 84451b3e 2018-07-10 stsp
5186 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5187 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5188 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5189 27a741e5 2019-09-11 stsp
5190 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5191 41605754 2020-11-12 stsp width = 9;
5192 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5193 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5194 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5195 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5196 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5197 41605754 2020-11-12 stsp if (err) {
5198 41605754 2020-11-12 stsp free(line);
5199 41605754 2020-11-12 stsp return err;
5200 41605754 2020-11-12 stsp }
5201 41605754 2020-11-12 stsp width += 9;
5202 41605754 2020-11-12 stsp } else {
5203 923086fd 2022-06-23 thomas int skip;
5204 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5205 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5206 923086fd 2022-06-23 thomas if (err) {
5207 923086fd 2022-06-23 thomas free(line);
5208 923086fd 2022-06-23 thomas return err;
5209 05171be4 2022-06-23 thomas }
5210 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5211 02bce7e2 2022-06-23 thomas width += 9;
5212 41605754 2020-11-12 stsp free(wline);
5213 41605754 2020-11-12 stsp wline = NULL;
5214 41605754 2020-11-12 stsp }
5215 41605754 2020-11-12 stsp
5216 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5217 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5218 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5219 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5220 84451b3e 2018-07-10 stsp }
5221 826082fe 2020-12-10 stsp free(line);
5222 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5223 84451b3e 2018-07-10 stsp
5224 a5d43cac 2022-07-01 thomas view_border(view);
5225 84451b3e 2018-07-10 stsp
5226 84451b3e 2018-07-10 stsp return NULL;
5227 84451b3e 2018-07-10 stsp }
5228 84451b3e 2018-07-10 stsp
5229 84451b3e 2018-07-10 stsp static const struct got_error *
5230 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5231 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5232 84451b3e 2018-07-10 stsp {
5233 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5234 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5235 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5236 1a76625f 2018-10-22 stsp int errcode;
5237 84451b3e 2018-07-10 stsp
5238 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5239 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5240 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5241 84451b3e 2018-07-10 stsp
5242 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5243 1a76625f 2018-10-22 stsp if (errcode)
5244 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5245 84451b3e 2018-07-10 stsp
5246 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5247 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5248 d68a0a7d 2018-07-10 stsp goto done;
5249 d68a0a7d 2018-07-10 stsp }
5250 d68a0a7d 2018-07-10 stsp
5251 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5252 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5253 d68a0a7d 2018-07-10 stsp
5254 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5255 d68a0a7d 2018-07-10 stsp if (line->annotated)
5256 d68a0a7d 2018-07-10 stsp goto done;
5257 d68a0a7d 2018-07-10 stsp
5258 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5259 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5260 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5261 84451b3e 2018-07-10 stsp goto done;
5262 84451b3e 2018-07-10 stsp }
5263 84451b3e 2018-07-10 stsp line->annotated = 1;
5264 84451b3e 2018-07-10 stsp done:
5265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5266 1a76625f 2018-10-22 stsp if (errcode)
5267 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5268 84451b3e 2018-07-10 stsp return err;
5269 84451b3e 2018-07-10 stsp }
5270 84451b3e 2018-07-10 stsp
5271 84451b3e 2018-07-10 stsp static void *
5272 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5273 84451b3e 2018-07-10 stsp {
5274 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5275 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5276 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5277 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5278 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5279 00a8878e 2022-07-01 thomas
5280 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5281 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5282 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5283 9117a7b7 2022-07-01 thomas
5284 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5285 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5286 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5287 9117a7b7 2022-07-01 thomas goto done;
5288 9117a7b7 2022-07-01 thomas }
5289 18430de3 2018-07-10 stsp
5290 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5291 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5292 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5293 9117a7b7 2022-07-01 thomas goto done;
5294 9117a7b7 2022-07-01 thomas }
5295 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5296 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5297 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5298 9117a7b7 2022-07-01 thomas goto done;
5299 9117a7b7 2022-07-01 thomas }
5300 9117a7b7 2022-07-01 thomas
5301 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5302 61266923 2020-01-14 stsp if (err)
5303 9117a7b7 2022-07-01 thomas goto done;
5304 61266923 2020-01-14 stsp
5305 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5306 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5307 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5308 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5309 fc06ba56 2019-08-22 stsp err = NULL;
5310 18430de3 2018-07-10 stsp
5311 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5312 9117a7b7 2022-07-01 thomas if (errcode) {
5313 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5314 9117a7b7 2022-07-01 thomas goto done;
5315 9117a7b7 2022-07-01 thomas }
5316 18430de3 2018-07-10 stsp
5317 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5318 1d0f4054 2021-06-17 stsp if (err == NULL)
5319 1d0f4054 2021-06-17 stsp err = close_err;
5320 c9beca56 2018-07-22 stsp ta->repo = NULL;
5321 c9beca56 2018-07-22 stsp *ta->complete = 1;
5322 18430de3 2018-07-10 stsp
5323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5324 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5325 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5326 18430de3 2018-07-10 stsp
5327 9117a7b7 2022-07-01 thomas done:
5328 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5329 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5330 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5331 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5332 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5333 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5334 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5335 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5336 00a8878e 2022-07-01 thomas
5337 18430de3 2018-07-10 stsp return (void *)err;
5338 84451b3e 2018-07-10 stsp }
5339 84451b3e 2018-07-10 stsp
5340 245d91c1 2018-07-12 stsp static struct got_object_id *
5341 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5342 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5343 245d91c1 2018-07-12 stsp {
5344 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5345 8d0fe45a 2019-08-12 stsp
5346 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5347 8d0fe45a 2019-08-12 stsp return NULL;
5348 b880a918 2018-07-10 stsp
5349 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5350 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5351 4fc71f3b 2022-07-12 thomas return NULL;
5352 4fc71f3b 2022-07-12 thomas
5353 4fc71f3b 2022-07-12 thomas return line->id;
5354 4fc71f3b 2022-07-12 thomas }
5355 4fc71f3b 2022-07-12 thomas
5356 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5357 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5358 4fc71f3b 2022-07-12 thomas int lineno)
5359 4fc71f3b 2022-07-12 thomas {
5360 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5361 4fc71f3b 2022-07-12 thomas
5362 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5363 4fc71f3b 2022-07-12 thomas return NULL;
5364 4fc71f3b 2022-07-12 thomas
5365 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5366 245d91c1 2018-07-12 stsp if (!line->annotated)
5367 245d91c1 2018-07-12 stsp return NULL;
5368 245d91c1 2018-07-12 stsp
5369 245d91c1 2018-07-12 stsp return line->id;
5370 b880a918 2018-07-10 stsp }
5371 245d91c1 2018-07-12 stsp
5372 b880a918 2018-07-10 stsp static const struct got_error *
5373 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5374 a70480e0 2018-06-23 stsp {
5375 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5376 245d91c1 2018-07-12 stsp int i;
5377 245d91c1 2018-07-12 stsp
5378 245d91c1 2018-07-12 stsp if (blame->thread) {
5379 1a76625f 2018-10-22 stsp int errcode;
5380 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5381 1a76625f 2018-10-22 stsp if (errcode)
5382 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5383 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5384 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5385 1a76625f 2018-10-22 stsp if (errcode)
5386 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5387 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5388 1a76625f 2018-10-22 stsp if (errcode)
5389 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5390 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5391 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5392 245d91c1 2018-07-12 stsp err = NULL;
5393 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5394 245d91c1 2018-07-12 stsp }
5395 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5396 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5397 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5398 1d0f4054 2021-06-17 stsp if (err == NULL)
5399 1d0f4054 2021-06-17 stsp err = close_err;
5400 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5401 245d91c1 2018-07-12 stsp }
5402 245d91c1 2018-07-12 stsp if (blame->f) {
5403 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5404 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5405 245d91c1 2018-07-12 stsp blame->f = NULL;
5406 245d91c1 2018-07-12 stsp }
5407 57670559 2018-12-23 stsp if (blame->lines) {
5408 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5409 57670559 2018-12-23 stsp free(blame->lines[i].id);
5410 57670559 2018-12-23 stsp free(blame->lines);
5411 57670559 2018-12-23 stsp blame->lines = NULL;
5412 57670559 2018-12-23 stsp }
5413 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5414 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5415 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5416 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5417 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5418 7cd52833 2022-06-23 thomas if (err == NULL)
5419 7cd52833 2022-06-23 thomas err = pack_err;
5420 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5421 7cd52833 2022-06-23 thomas }
5422 245d91c1 2018-07-12 stsp return err;
5423 245d91c1 2018-07-12 stsp }
5424 245d91c1 2018-07-12 stsp
5425 245d91c1 2018-07-12 stsp static const struct got_error *
5426 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5427 fc06ba56 2019-08-22 stsp {
5428 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5429 fc06ba56 2019-08-22 stsp int *done = arg;
5430 fc06ba56 2019-08-22 stsp int errcode;
5431 fc06ba56 2019-08-22 stsp
5432 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5433 fc06ba56 2019-08-22 stsp if (errcode)
5434 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5435 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5436 fc06ba56 2019-08-22 stsp
5437 fc06ba56 2019-08-22 stsp if (*done)
5438 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5439 fc06ba56 2019-08-22 stsp
5440 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5441 fc06ba56 2019-08-22 stsp if (errcode)
5442 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5443 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5444 fc06ba56 2019-08-22 stsp
5445 fc06ba56 2019-08-22 stsp return err;
5446 fc06ba56 2019-08-22 stsp }
5447 fc06ba56 2019-08-22 stsp
5448 fc06ba56 2019-08-22 stsp static const struct got_error *
5449 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5450 245d91c1 2018-07-12 stsp {
5451 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5452 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5453 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5454 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5455 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5456 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5457 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5458 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5459 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5460 a70480e0 2018-06-23 stsp
5461 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5462 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5463 27d434c2 2018-09-15 stsp if (err)
5464 15a94983 2018-12-23 stsp return err;
5465 f4ae6ddb 2022-07-01 thomas
5466 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5467 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5468 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5469 f4ae6ddb 2022-07-01 thomas goto done;
5470 f4ae6ddb 2022-07-01 thomas }
5471 945f9229 2022-04-16 thomas
5472 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5473 945f9229 2022-04-16 thomas if (err)
5474 945f9229 2022-04-16 thomas goto done;
5475 27d434c2 2018-09-15 stsp
5476 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5477 84451b3e 2018-07-10 stsp if (err)
5478 84451b3e 2018-07-10 stsp goto done;
5479 27d434c2 2018-09-15 stsp
5480 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5481 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5482 84451b3e 2018-07-10 stsp goto done;
5483 84451b3e 2018-07-10 stsp }
5484 a70480e0 2018-06-23 stsp
5485 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5486 a70480e0 2018-06-23 stsp if (err)
5487 a70480e0 2018-06-23 stsp goto done;
5488 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5489 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5490 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5491 84451b3e 2018-07-10 stsp goto done;
5492 84451b3e 2018-07-10 stsp }
5493 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5494 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5495 1fddf795 2021-01-20 stsp if (err)
5496 1fddf795 2021-01-20 stsp goto done;
5497 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5498 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5499 84451b3e 2018-07-10 stsp goto done;
5500 1fddf795 2021-01-20 stsp }
5501 b02560ec 2019-08-19 stsp
5502 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5503 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5504 b02560ec 2019-08-19 stsp blame->nlines--;
5505 a70480e0 2018-06-23 stsp
5506 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5507 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5508 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5509 84451b3e 2018-07-10 stsp goto done;
5510 84451b3e 2018-07-10 stsp }
5511 a70480e0 2018-06-23 stsp
5512 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5513 bd24772e 2018-07-11 stsp if (err)
5514 bd24772e 2018-07-11 stsp goto done;
5515 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5516 7cd52833 2022-06-23 thomas pack_fds);
5517 7cd52833 2022-06-23 thomas if (err)
5518 7cd52833 2022-06-23 thomas goto done;
5519 bd24772e 2018-07-11 stsp
5520 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5521 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5522 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5523 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5524 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5525 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5526 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5527 245d91c1 2018-07-12 stsp goto done;
5528 245d91c1 2018-07-12 stsp }
5529 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5530 245d91c1 2018-07-12 stsp
5531 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5532 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5533 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5534 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5535 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5536 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5537 a5388363 2020-12-01 naddy s->blame_complete = 0;
5538 f5a09613 2020-12-13 naddy
5539 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5540 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5541 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5542 f5a09613 2020-12-13 naddy s->selected_line = 1;
5543 f5a09613 2020-12-13 naddy }
5544 aa61903a 2021-12-31 thomas s->matched_line = 0;
5545 245d91c1 2018-07-12 stsp
5546 245d91c1 2018-07-12 stsp done:
5547 945f9229 2022-04-16 thomas if (commit)
5548 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5549 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5550 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5551 245d91c1 2018-07-12 stsp if (blob)
5552 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5553 27d434c2 2018-09-15 stsp free(obj_id);
5554 245d91c1 2018-07-12 stsp if (err)
5555 245d91c1 2018-07-12 stsp stop_blame(blame);
5556 245d91c1 2018-07-12 stsp return err;
5557 245d91c1 2018-07-12 stsp }
5558 245d91c1 2018-07-12 stsp
5559 245d91c1 2018-07-12 stsp static const struct got_error *
5560 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5561 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5562 245d91c1 2018-07-12 stsp {
5563 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5564 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5565 dbc6a6b6 2018-07-12 stsp
5566 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5567 245d91c1 2018-07-12 stsp
5568 c4843652 2019-08-12 stsp s->path = strdup(path);
5569 c4843652 2019-08-12 stsp if (s->path == NULL)
5570 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5571 c4843652 2019-08-12 stsp
5572 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5573 c4843652 2019-08-12 stsp if (err) {
5574 c4843652 2019-08-12 stsp free(s->path);
5575 7cbe629d 2018-08-04 stsp return err;
5576 c4843652 2019-08-12 stsp }
5577 245d91c1 2018-07-12 stsp
5578 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5579 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5580 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5581 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5582 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5583 fb2756b9 2018-08-04 stsp s->repo = repo;
5584 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5585 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5586 7cbe629d 2018-08-04 stsp
5587 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5588 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5589 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5590 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5591 11b20872 2019-11-08 stsp if (err)
5592 11b20872 2019-11-08 stsp return err;
5593 11b20872 2019-11-08 stsp }
5594 11b20872 2019-11-08 stsp
5595 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5596 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5597 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5598 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5599 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5600 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5601 e5a0f69f 2018-08-18 stsp
5602 a5388363 2020-12-01 naddy return run_blame(view);
5603 7cbe629d 2018-08-04 stsp }
5604 7cbe629d 2018-08-04 stsp
5605 e5a0f69f 2018-08-18 stsp static const struct got_error *
5606 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5607 7cbe629d 2018-08-04 stsp {
5608 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5609 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5610 7cbe629d 2018-08-04 stsp
5611 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5612 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5613 e5a0f69f 2018-08-18 stsp
5614 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5615 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5616 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5617 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5618 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5619 7cbe629d 2018-08-04 stsp }
5620 e5a0f69f 2018-08-18 stsp
5621 e5a0f69f 2018-08-18 stsp free(s->path);
5622 11b20872 2019-11-08 stsp free_colors(&s->colors);
5623 e5a0f69f 2018-08-18 stsp return err;
5624 7cbe629d 2018-08-04 stsp }
5625 7cbe629d 2018-08-04 stsp
5626 7cbe629d 2018-08-04 stsp static const struct got_error *
5627 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5628 6c4c42e0 2019-06-24 stsp {
5629 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5630 6c4c42e0 2019-06-24 stsp
5631 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5632 6c4c42e0 2019-06-24 stsp return NULL;
5633 6c4c42e0 2019-06-24 stsp }
5634 6c4c42e0 2019-06-24 stsp
5635 6c4c42e0 2019-06-24 stsp static const struct got_error *
5636 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5637 6c4c42e0 2019-06-24 stsp {
5638 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5639 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5640 6c4c42e0 2019-06-24 stsp int lineno;
5641 78eecffc 2022-06-23 thomas char *line = NULL;
5642 826082fe 2020-12-10 stsp size_t linesize = 0;
5643 826082fe 2020-12-10 stsp ssize_t linelen;
5644 6c4c42e0 2019-06-24 stsp
5645 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5646 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5647 6c4c42e0 2019-06-24 stsp return NULL;
5648 6c4c42e0 2019-06-24 stsp }
5649 6c4c42e0 2019-06-24 stsp
5650 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5651 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5652 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5653 6c4c42e0 2019-06-24 stsp else
5654 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5655 4b3f9dac 2021-12-17 thomas } else
5656 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5657 6c4c42e0 2019-06-24 stsp
5658 6c4c42e0 2019-06-24 stsp while (1) {
5659 6c4c42e0 2019-06-24 stsp off_t offset;
5660 6c4c42e0 2019-06-24 stsp
5661 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5662 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5663 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5664 6c4c42e0 2019-06-24 stsp break;
5665 6c4c42e0 2019-06-24 stsp }
5666 2246482e 2019-06-25 stsp
5667 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5668 6c4c42e0 2019-06-24 stsp lineno = 1;
5669 6c4c42e0 2019-06-24 stsp else
5670 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5671 6c4c42e0 2019-06-24 stsp }
5672 6c4c42e0 2019-06-24 stsp
5673 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5674 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5675 6c4c42e0 2019-06-24 stsp free(line);
5676 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5677 6c4c42e0 2019-06-24 stsp }
5678 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5679 78eecffc 2022-06-23 thomas if (linelen != -1) {
5680 78eecffc 2022-06-23 thomas char *exstr;
5681 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5682 78eecffc 2022-06-23 thomas if (err)
5683 78eecffc 2022-06-23 thomas break;
5684 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5685 78eecffc 2022-06-23 thomas &view->regmatch)) {
5686 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5687 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5688 78eecffc 2022-06-23 thomas free(exstr);
5689 78eecffc 2022-06-23 thomas break;
5690 78eecffc 2022-06-23 thomas }
5691 78eecffc 2022-06-23 thomas free(exstr);
5692 6c4c42e0 2019-06-24 stsp }
5693 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5694 6c4c42e0 2019-06-24 stsp lineno++;
5695 6c4c42e0 2019-06-24 stsp else
5696 6c4c42e0 2019-06-24 stsp lineno--;
5697 6c4c42e0 2019-06-24 stsp }
5698 826082fe 2020-12-10 stsp free(line);
5699 6c4c42e0 2019-06-24 stsp
5700 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5701 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5702 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5703 6c4c42e0 2019-06-24 stsp }
5704 6c4c42e0 2019-06-24 stsp
5705 05171be4 2022-06-23 thomas return err;
5706 6c4c42e0 2019-06-24 stsp }
5707 6c4c42e0 2019-06-24 stsp
5708 6c4c42e0 2019-06-24 stsp static const struct got_error *
5709 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5710 7cbe629d 2018-08-04 stsp {
5711 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5712 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5713 2b380cc8 2018-10-24 stsp int errcode;
5714 2b380cc8 2018-10-24 stsp
5715 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5716 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5717 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5718 2b380cc8 2018-10-24 stsp if (errcode)
5719 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5720 51fe7530 2019-08-19 stsp
5721 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5722 2b380cc8 2018-10-24 stsp }
5723 e5a0f69f 2018-08-18 stsp
5724 51fe7530 2019-08-19 stsp if (s->blame_complete)
5725 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5726 51fe7530 2019-08-19 stsp
5727 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5728 e5a0f69f 2018-08-18 stsp
5729 a5d43cac 2022-07-01 thomas view_border(view);
5730 e5a0f69f 2018-08-18 stsp return err;
5731 e5a0f69f 2018-08-18 stsp }
5732 e5a0f69f 2018-08-18 stsp
5733 e5a0f69f 2018-08-18 stsp static const struct got_error *
5734 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5735 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5736 eaeaa612 2022-07-20 thomas {
5737 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5738 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5739 eaeaa612 2022-07-20 thomas
5740 eaeaa612 2022-07-20 thomas *new_view = NULL;
5741 eaeaa612 2022-07-20 thomas
5742 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5743 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5744 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5745 eaeaa612 2022-07-20 thomas
5746 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5747 eaeaa612 2022-07-20 thomas if (err)
5748 eaeaa612 2022-07-20 thomas view_close(log_view);
5749 eaeaa612 2022-07-20 thomas else
5750 eaeaa612 2022-07-20 thomas *new_view = log_view;
5751 eaeaa612 2022-07-20 thomas
5752 eaeaa612 2022-07-20 thomas return err;
5753 eaeaa612 2022-07-20 thomas }
5754 eaeaa612 2022-07-20 thomas
5755 eaeaa612 2022-07-20 thomas static const struct got_error *
5756 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5757 e5a0f69f 2018-08-18 stsp {
5758 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5759 eaeaa612 2022-07-20 thomas struct tog_view *diff_view, *log_view;
5760 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5761 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5762 a5d43cac 2022-07-01 thomas
5763 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5764 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5765 a5d43cac 2022-07-01 thomas --eos; /* border */
5766 7cbe629d 2018-08-04 stsp
5767 e5a0f69f 2018-08-18 stsp switch (ch) {
5768 05171be4 2022-06-23 thomas case '0':
5769 05171be4 2022-06-23 thomas view->x = 0;
5770 05171be4 2022-06-23 thomas break;
5771 05171be4 2022-06-23 thomas case '$':
5772 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5773 07b0611c 2022-06-23 thomas view->count = 0;
5774 05171be4 2022-06-23 thomas break;
5775 05171be4 2022-06-23 thomas case KEY_RIGHT:
5776 05171be4 2022-06-23 thomas case 'l':
5777 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5778 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5779 07b0611c 2022-06-23 thomas else
5780 07b0611c 2022-06-23 thomas view->count = 0;
5781 05171be4 2022-06-23 thomas break;
5782 05171be4 2022-06-23 thomas case KEY_LEFT:
5783 05171be4 2022-06-23 thomas case 'h':
5784 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5785 07b0611c 2022-06-23 thomas if (view->x <= 0)
5786 07b0611c 2022-06-23 thomas view->count = 0;
5787 05171be4 2022-06-23 thomas break;
5788 1e37a5c2 2019-05-12 jcs case 'q':
5789 1e37a5c2 2019-05-12 jcs s->done = 1;
5790 4deef56f 2021-09-02 naddy break;
5791 4deef56f 2021-09-02 naddy case 'g':
5792 4deef56f 2021-09-02 naddy case KEY_HOME:
5793 4deef56f 2021-09-02 naddy s->selected_line = 1;
5794 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5795 07b0611c 2022-06-23 thomas view->count = 0;
5796 4deef56f 2021-09-02 naddy break;
5797 4deef56f 2021-09-02 naddy case 'G':
5798 4deef56f 2021-09-02 naddy case KEY_END:
5799 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5800 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5801 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5802 4deef56f 2021-09-02 naddy } else {
5803 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5804 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5805 4deef56f 2021-09-02 naddy }
5806 07b0611c 2022-06-23 thomas view->count = 0;
5807 1e37a5c2 2019-05-12 jcs break;
5808 1e37a5c2 2019-05-12 jcs case 'k':
5809 1e37a5c2 2019-05-12 jcs case KEY_UP:
5810 f7140bf5 2021-10-17 thomas case CTRL('p'):
5811 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5812 1e37a5c2 2019-05-12 jcs s->selected_line--;
5813 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5814 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5815 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5816 07b0611c 2022-06-23 thomas else
5817 07b0611c 2022-06-23 thomas view->count = 0;
5818 1e37a5c2 2019-05-12 jcs break;
5819 70f17a53 2022-06-13 thomas case CTRL('u'):
5820 23427b14 2022-06-23 thomas case 'u':
5821 70f17a53 2022-06-13 thomas nscroll /= 2;
5822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5823 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5824 ea025d1d 2020-02-22 naddy case CTRL('b'):
5825 1c5e5faa 2022-06-23 thomas case 'b':
5826 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5827 07b0611c 2022-06-23 thomas if (view->count > 1)
5828 07b0611c 2022-06-23 thomas nscroll += nscroll;
5829 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5830 07b0611c 2022-06-23 thomas view->count = 0;
5831 e5a0f69f 2018-08-18 stsp break;
5832 1e37a5c2 2019-05-12 jcs }
5833 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5834 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5835 1e37a5c2 2019-05-12 jcs else
5836 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5837 1e37a5c2 2019-05-12 jcs break;
5838 1e37a5c2 2019-05-12 jcs case 'j':
5839 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5840 f7140bf5 2021-10-17 thomas case CTRL('n'):
5841 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5842 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5843 1e37a5c2 2019-05-12 jcs s->selected_line++;
5844 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5845 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5846 07b0611c 2022-06-23 thomas else
5847 07b0611c 2022-06-23 thomas view->count = 0;
5848 1e37a5c2 2019-05-12 jcs break;
5849 1c5e5faa 2022-06-23 thomas case 'c':
5850 1e37a5c2 2019-05-12 jcs case 'p': {
5851 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5852 07b0611c 2022-06-23 thomas
5853 07b0611c 2022-06-23 thomas view->count = 0;
5854 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5855 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5856 1e37a5c2 2019-05-12 jcs if (id == NULL)
5857 e5a0f69f 2018-08-18 stsp break;
5858 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5859 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5860 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5861 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5862 1e37a5c2 2019-05-12 jcs int obj_type;
5863 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5864 1e37a5c2 2019-05-12 jcs s->repo, id);
5865 e5a0f69f 2018-08-18 stsp if (err)
5866 e5a0f69f 2018-08-18 stsp break;
5867 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5868 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5869 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5870 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5871 e5a0f69f 2018-08-18 stsp break;
5872 e5a0f69f 2018-08-18 stsp }
5873 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5874 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5875 ec242592 2022-04-22 thomas s->repo, &pid->id);
5876 945f9229 2022-04-16 thomas if (err)
5877 945f9229 2022-04-16 thomas break;
5878 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5879 945f9229 2022-04-16 thomas pcommit, s->path);
5880 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5881 e5a0f69f 2018-08-18 stsp if (err) {
5882 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5883 1e37a5c2 2019-05-12 jcs err = NULL;
5884 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5885 e5a0f69f 2018-08-18 stsp break;
5886 e5a0f69f 2018-08-18 stsp }
5887 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5888 1e37a5c2 2019-05-12 jcs blob_id);
5889 1e37a5c2 2019-05-12 jcs free(blob_id);
5890 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5891 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5892 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5893 e5a0f69f 2018-08-18 stsp break;
5894 1e37a5c2 2019-05-12 jcs }
5895 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5896 ec242592 2022-04-22 thomas &pid->id);
5897 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5898 1e37a5c2 2019-05-12 jcs } else {
5899 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5900 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5901 1e37a5c2 2019-05-12 jcs break;
5902 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5903 1e37a5c2 2019-05-12 jcs id);
5904 1e37a5c2 2019-05-12 jcs }
5905 1e37a5c2 2019-05-12 jcs if (err)
5906 e5a0f69f 2018-08-18 stsp break;
5907 1e37a5c2 2019-05-12 jcs s->done = 1;
5908 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5909 1e37a5c2 2019-05-12 jcs s->done = 0;
5910 1e37a5c2 2019-05-12 jcs if (thread_err)
5911 1e37a5c2 2019-05-12 jcs break;
5912 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5913 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5914 a5388363 2020-12-01 naddy err = run_blame(view);
5915 1e37a5c2 2019-05-12 jcs if (err)
5916 1e37a5c2 2019-05-12 jcs break;
5917 1e37a5c2 2019-05-12 jcs break;
5918 1e37a5c2 2019-05-12 jcs }
5919 1c5e5faa 2022-06-23 thomas case 'C': {
5920 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5921 07b0611c 2022-06-23 thomas
5922 07b0611c 2022-06-23 thomas view->count = 0;
5923 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5924 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5925 1e37a5c2 2019-05-12 jcs break;
5926 1e37a5c2 2019-05-12 jcs s->done = 1;
5927 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5928 1e37a5c2 2019-05-12 jcs s->done = 0;
5929 1e37a5c2 2019-05-12 jcs if (thread_err)
5930 1e37a5c2 2019-05-12 jcs break;
5931 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5932 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5933 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5934 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5935 a5388363 2020-12-01 naddy err = run_blame(view);
5936 1e37a5c2 2019-05-12 jcs if (err)
5937 1e37a5c2 2019-05-12 jcs break;
5938 1e37a5c2 2019-05-12 jcs break;
5939 1e37a5c2 2019-05-12 jcs }
5940 eaeaa612 2022-07-20 thomas case 'L': {
5941 eaeaa612 2022-07-20 thomas struct got_object_id *id = NULL;
5942 eaeaa612 2022-07-20 thomas
5943 eaeaa612 2022-07-20 thomas view->count = 0;
5944 eaeaa612 2022-07-20 thomas id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5945 eaeaa612 2022-07-20 thomas s->first_displayed_line, s->selected_line);
5946 eaeaa612 2022-07-20 thomas if (id == NULL)
5947 eaeaa612 2022-07-20 thomas break;
5948 eaeaa612 2022-07-20 thomas
5949 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view))
5950 eaeaa612 2022-07-20 thomas view_get_split(view, &begin_y, &begin_x);
5951 eaeaa612 2022-07-20 thomas err = log_annotated_line(&log_view, begin_y, begin_x,
5952 eaeaa612 2022-07-20 thomas s->repo, id);
5953 eaeaa612 2022-07-20 thomas if (err)
5954 eaeaa612 2022-07-20 thomas break;
5955 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view) &&
5956 eaeaa612 2022-07-20 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5957 eaeaa612 2022-07-20 thomas err = view_init_hsplit(view, begin_y);
5958 eaeaa612 2022-07-20 thomas if (err)
5959 eaeaa612 2022-07-20 thomas break;
5960 eaeaa612 2022-07-20 thomas }
5961 eaeaa612 2022-07-20 thomas
5962 eaeaa612 2022-07-20 thomas view->focussed = 0;
5963 eaeaa612 2022-07-20 thomas log_view->focussed = 1;
5964 eaeaa612 2022-07-20 thomas log_view->mode = view->mode;
5965 eaeaa612 2022-07-20 thomas log_view->nlines = view->lines - begin_y;
5966 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view)) {
5967 eaeaa612 2022-07-20 thomas view_transfer_size(log_view, view);
5968 eaeaa612 2022-07-20 thomas err = view_close_child(view);
5969 eaeaa612 2022-07-20 thomas if (err)
5970 eaeaa612 2022-07-20 thomas return err;
5971 eaeaa612 2022-07-20 thomas err = view_set_child(view, log_view);
5972 eaeaa612 2022-07-20 thomas if (err)
5973 eaeaa612 2022-07-20 thomas return err;
5974 eaeaa612 2022-07-20 thomas view->focus_child = 1;
5975 eaeaa612 2022-07-20 thomas } else
5976 eaeaa612 2022-07-20 thomas *new_view = log_view;
5977 eaeaa612 2022-07-20 thomas break;
5978 eaeaa612 2022-07-20 thomas }
5979 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5980 1e37a5c2 2019-05-12 jcs case '\r': {
5981 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5982 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5983 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5984 07b0611c 2022-06-23 thomas
5985 07b0611c 2022-06-23 thomas view->count = 0;
5986 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5987 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5988 1e37a5c2 2019-05-12 jcs if (id == NULL)
5989 1e37a5c2 2019-05-12 jcs break;
5990 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5991 1e37a5c2 2019-05-12 jcs if (err)
5992 1e37a5c2 2019-05-12 jcs break;
5993 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5994 4fc71f3b 2022-07-12 thomas if (*new_view) {
5995 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5996 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5997 4fc71f3b 2022-07-12 thomas if (err)
5998 4fc71f3b 2022-07-12 thomas break;
5999 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6000 4fc71f3b 2022-07-12 thomas } else {
6001 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6002 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6003 a5d43cac 2022-07-01 thomas
6004 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6005 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6006 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6007 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6008 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6009 4fc71f3b 2022-07-12 thomas break;
6010 4fc71f3b 2022-07-12 thomas }
6011 15a94983 2018-12-23 stsp }
6012 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6013 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6014 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6015 1e37a5c2 2019-05-12 jcs if (err) {
6016 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6017 1e37a5c2 2019-05-12 jcs break;
6018 1e37a5c2 2019-05-12 jcs }
6019 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6020 4fc71f3b 2022-07-12 thomas s->selected_line;
6021 4fc71f3b 2022-07-12 thomas if (*new_view)
6022 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6023 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6024 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6025 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6026 a5d43cac 2022-07-01 thomas if (err)
6027 a5d43cac 2022-07-01 thomas break;
6028 a5d43cac 2022-07-01 thomas }
6029 a5d43cac 2022-07-01 thomas
6030 e78dc838 2020-12-04 stsp view->focussed = 0;
6031 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6032 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6033 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6034 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6035 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6036 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6037 1e37a5c2 2019-05-12 jcs if (err)
6038 34bc9ec9 2019-02-22 stsp break;
6039 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6040 40236d76 2022-06-23 thomas if (err)
6041 40236d76 2022-06-23 thomas break;
6042 e78dc838 2020-12-04 stsp view->focus_child = 1;
6043 1e37a5c2 2019-05-12 jcs } else
6044 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6045 1e37a5c2 2019-05-12 jcs if (err)
6046 e5a0f69f 2018-08-18 stsp break;
6047 1e37a5c2 2019-05-12 jcs break;
6048 1e37a5c2 2019-05-12 jcs }
6049 70f17a53 2022-06-13 thomas case CTRL('d'):
6050 23427b14 2022-06-23 thomas case 'd':
6051 70f17a53 2022-06-13 thomas nscroll /= 2;
6052 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6053 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6054 ea025d1d 2020-02-22 naddy case CTRL('f'):
6055 1c5e5faa 2022-06-23 thomas case 'f':
6056 1e37a5c2 2019-05-12 jcs case ' ':
6057 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6058 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6059 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6060 07b0611c 2022-06-23 thomas view->count = 0;
6061 e5a0f69f 2018-08-18 stsp break;
6062 1e37a5c2 2019-05-12 jcs }
6063 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6064 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6065 70f17a53 2022-06-13 thomas s->selected_line +=
6066 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6067 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6068 1e37a5c2 2019-05-12 jcs }
6069 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6070 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6071 1e37a5c2 2019-05-12 jcs else
6072 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6073 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6074 1e37a5c2 2019-05-12 jcs break;
6075 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6076 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6077 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6078 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6079 1e37a5c2 2019-05-12 jcs }
6080 1e37a5c2 2019-05-12 jcs break;
6081 1e37a5c2 2019-05-12 jcs default:
6082 07b0611c 2022-06-23 thomas view->count = 0;
6083 1e37a5c2 2019-05-12 jcs break;
6084 a70480e0 2018-06-23 stsp }
6085 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6086 adf4c9e0 2022-07-03 thomas }
6087 adf4c9e0 2022-07-03 thomas
6088 adf4c9e0 2022-07-03 thomas static const struct got_error *
6089 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6090 adf4c9e0 2022-07-03 thomas {
6091 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6092 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6093 adf4c9e0 2022-07-03 thomas
6094 adf4c9e0 2022-07-03 thomas view->count = 0;
6095 adf4c9e0 2022-07-03 thomas s->done = 1;
6096 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6097 adf4c9e0 2022-07-03 thomas s->done = 0;
6098 adf4c9e0 2022-07-03 thomas if (err)
6099 adf4c9e0 2022-07-03 thomas return err;
6100 adf4c9e0 2022-07-03 thomas return run_blame(view);
6101 a70480e0 2018-06-23 stsp }
6102 a70480e0 2018-06-23 stsp
6103 a70480e0 2018-06-23 stsp static const struct got_error *
6104 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6105 9f7d7167 2018-04-29 stsp {
6106 a70480e0 2018-06-23 stsp const struct got_error *error;
6107 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6108 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6109 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6110 0587e10c 2020-07-23 stsp char *link_target = NULL;
6111 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6112 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6113 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6114 a70480e0 2018-06-23 stsp int ch;
6115 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6116 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6117 a70480e0 2018-06-23 stsp
6118 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6119 a70480e0 2018-06-23 stsp switch (ch) {
6120 a70480e0 2018-06-23 stsp case 'c':
6121 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6122 a70480e0 2018-06-23 stsp break;
6123 69069811 2018-08-02 stsp case 'r':
6124 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6125 69069811 2018-08-02 stsp if (repo_path == NULL)
6126 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6127 9ba1d308 2019-10-21 stsp optarg);
6128 69069811 2018-08-02 stsp break;
6129 a70480e0 2018-06-23 stsp default:
6130 17020d27 2019-03-07 stsp usage_blame();
6131 a70480e0 2018-06-23 stsp /* NOTREACHED */
6132 a70480e0 2018-06-23 stsp }
6133 a70480e0 2018-06-23 stsp }
6134 a70480e0 2018-06-23 stsp
6135 a70480e0 2018-06-23 stsp argc -= optind;
6136 a70480e0 2018-06-23 stsp argv += optind;
6137 a70480e0 2018-06-23 stsp
6138 f135c941 2020-02-20 stsp if (argc != 1)
6139 a70480e0 2018-06-23 stsp usage_blame();
6140 6962eb72 2020-02-20 stsp
6141 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6142 7cd52833 2022-06-23 thomas if (error != NULL)
6143 7cd52833 2022-06-23 thomas goto done;
6144 7cd52833 2022-06-23 thomas
6145 69069811 2018-08-02 stsp if (repo_path == NULL) {
6146 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6147 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6148 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6149 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6150 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6151 c156c7a4 2020-12-18 stsp goto done;
6152 f135c941 2020-02-20 stsp if (worktree)
6153 eb41ed75 2019-02-05 stsp repo_path =
6154 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6155 f135c941 2020-02-20 stsp else
6156 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6157 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6158 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6159 c156c7a4 2020-12-18 stsp goto done;
6160 c156c7a4 2020-12-18 stsp }
6161 f135c941 2020-02-20 stsp }
6162 a915003a 2019-02-05 stsp
6163 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6164 c02c541e 2019-03-29 stsp if (error != NULL)
6165 8e94dd5b 2019-01-04 stsp goto done;
6166 69069811 2018-08-02 stsp
6167 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6168 f135c941 2020-02-20 stsp worktree);
6169 c02c541e 2019-03-29 stsp if (error)
6170 92205607 2019-01-04 stsp goto done;
6171 69069811 2018-08-02 stsp
6172 f135c941 2020-02-20 stsp init_curses();
6173 f135c941 2020-02-20 stsp
6174 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6175 51a10b52 2020-12-26 stsp if (error)
6176 51a10b52 2020-12-26 stsp goto done;
6177 51a10b52 2020-12-26 stsp
6178 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6179 eb41ed75 2019-02-05 stsp if (error)
6180 69069811 2018-08-02 stsp goto done;
6181 a70480e0 2018-06-23 stsp
6182 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6183 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6184 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6185 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6186 a70480e0 2018-06-23 stsp if (error != NULL)
6187 66b4983c 2018-06-23 stsp goto done;
6188 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6189 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6190 a70480e0 2018-06-23 stsp } else {
6191 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6192 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6193 a70480e0 2018-06-23 stsp }
6194 a19e88aa 2018-06-23 stsp if (error != NULL)
6195 8b473291 2019-02-21 stsp goto done;
6196 8b473291 2019-02-21 stsp
6197 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6198 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6199 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6200 e1cd8fed 2018-08-01 stsp goto done;
6201 e1cd8fed 2018-08-01 stsp }
6202 0587e10c 2020-07-23 stsp
6203 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6204 945f9229 2022-04-16 thomas if (error)
6205 945f9229 2022-04-16 thomas goto done;
6206 945f9229 2022-04-16 thomas
6207 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6208 945f9229 2022-04-16 thomas commit, repo);
6209 7cbe629d 2018-08-04 stsp if (error)
6210 7cbe629d 2018-08-04 stsp goto done;
6211 0587e10c 2020-07-23 stsp
6212 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6213 78756c87 2020-11-24 stsp commit_id, repo);
6214 0587e10c 2020-07-23 stsp if (error)
6215 0587e10c 2020-07-23 stsp goto done;
6216 12314ad4 2019-08-31 stsp if (worktree) {
6217 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6218 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6219 12314ad4 2019-08-31 stsp worktree = NULL;
6220 12314ad4 2019-08-31 stsp }
6221 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6222 a70480e0 2018-06-23 stsp done:
6223 69069811 2018-08-02 stsp free(repo_path);
6224 f135c941 2020-02-20 stsp free(in_repo_path);
6225 0587e10c 2020-07-23 stsp free(link_target);
6226 69069811 2018-08-02 stsp free(cwd);
6227 a70480e0 2018-06-23 stsp free(commit_id);
6228 945f9229 2022-04-16 thomas if (commit)
6229 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6230 eb41ed75 2019-02-05 stsp if (worktree)
6231 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6232 1d0f4054 2021-06-17 stsp if (repo) {
6233 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6234 1d0f4054 2021-06-17 stsp if (error == NULL)
6235 1d0f4054 2021-06-17 stsp error = close_err;
6236 1d0f4054 2021-06-17 stsp }
6237 7cd52833 2022-06-23 thomas if (pack_fds) {
6238 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6239 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6240 7cd52833 2022-06-23 thomas if (error == NULL)
6241 7cd52833 2022-06-23 thomas error = pack_err;
6242 7cd52833 2022-06-23 thomas }
6243 51a10b52 2020-12-26 stsp tog_free_refs();
6244 a70480e0 2018-06-23 stsp return error;
6245 ffd1d5e5 2018-06-23 stsp }
6246 ffd1d5e5 2018-06-23 stsp
6247 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6248 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6249 ffd1d5e5 2018-06-23 stsp {
6250 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6251 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6252 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6253 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6254 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6255 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6256 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6257 ffd1d5e5 2018-06-23 stsp
6258 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6259 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6260 a5d43cac 2022-07-01 thomas --limit; /* border */
6261 ffd1d5e5 2018-06-23 stsp
6262 f7d12f7e 2018-08-01 stsp werase(view->window);
6263 ffd1d5e5 2018-06-23 stsp
6264 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6265 ffd1d5e5 2018-06-23 stsp return NULL;
6266 ffd1d5e5 2018-06-23 stsp
6267 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6268 f91a2b48 2022-06-23 thomas 0, 0);
6269 ffd1d5e5 2018-06-23 stsp if (err)
6270 ffd1d5e5 2018-06-23 stsp return err;
6271 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6272 a3404814 2018-09-02 stsp wstandout(view->window);
6273 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6274 11b20872 2019-11-08 stsp if (tc)
6275 11b20872 2019-11-08 stsp wattr_on(view->window,
6276 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6277 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6278 11b20872 2019-11-08 stsp if (tc)
6279 11b20872 2019-11-08 stsp wattr_off(view->window,
6280 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6281 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6282 a3404814 2018-09-02 stsp wstandend(view->window);
6283 2550e4c3 2018-07-13 stsp free(wline);
6284 2550e4c3 2018-07-13 stsp wline = NULL;
6285 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6286 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6287 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6288 ffd1d5e5 2018-06-23 stsp return NULL;
6289 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6290 f91a2b48 2022-06-23 thomas 0, 0);
6291 ce52c690 2018-06-23 stsp if (err)
6292 ce52c690 2018-06-23 stsp return err;
6293 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6294 2550e4c3 2018-07-13 stsp free(wline);
6295 2550e4c3 2018-07-13 stsp wline = NULL;
6296 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6297 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6298 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6299 ffd1d5e5 2018-06-23 stsp return NULL;
6300 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6301 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6302 a1eca9bb 2018-06-23 stsp return NULL;
6303 ffd1d5e5 2018-06-23 stsp
6304 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6305 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6306 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6307 0cf4efb1 2018-09-29 stsp if (view->focussed)
6308 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6309 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6310 ffd1d5e5 2018-06-23 stsp }
6311 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6312 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6313 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6314 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6315 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6316 ffd1d5e5 2018-06-23 stsp return NULL;
6317 ffd1d5e5 2018-06-23 stsp n = 1;
6318 ffd1d5e5 2018-06-23 stsp } else {
6319 ffd1d5e5 2018-06-23 stsp n = 0;
6320 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6321 ffd1d5e5 2018-06-23 stsp }
6322 ffd1d5e5 2018-06-23 stsp
6323 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6324 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6325 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6326 848d6979 2019-08-12 stsp const char *modestr = "";
6327 56e0773d 2019-11-28 stsp mode_t mode;
6328 1d13200f 2018-07-12 stsp
6329 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6330 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6331 56e0773d 2019-11-28 stsp
6332 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6333 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6334 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6335 1d13200f 2018-07-12 stsp if (err)
6336 638f9024 2019-05-13 stsp return got_error_from_errno(
6337 230a42bd 2019-05-11 jcs "got_object_id_str");
6338 1d13200f 2018-07-12 stsp }
6339 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6340 63c5ca5d 2019-08-24 stsp modestr = "$";
6341 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6342 0d6c6ee3 2020-05-20 stsp int i;
6343 0d6c6ee3 2020-05-20 stsp
6344 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6345 d86d3b18 2020-12-01 naddy te, s->repo);
6346 0d6c6ee3 2020-05-20 stsp if (err) {
6347 0d6c6ee3 2020-05-20 stsp free(id_str);
6348 0d6c6ee3 2020-05-20 stsp return err;
6349 0d6c6ee3 2020-05-20 stsp }
6350 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6351 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6352 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6353 0d6c6ee3 2020-05-20 stsp }
6354 848d6979 2019-08-12 stsp modestr = "@";
6355 0d6c6ee3 2020-05-20 stsp }
6356 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6357 848d6979 2019-08-12 stsp modestr = "/";
6358 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6359 848d6979 2019-08-12 stsp modestr = "*";
6360 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6361 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6362 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6363 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6364 1d13200f 2018-07-12 stsp free(id_str);
6365 0d6c6ee3 2020-05-20 stsp free(link_target);
6366 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6367 1d13200f 2018-07-12 stsp }
6368 1d13200f 2018-07-12 stsp free(id_str);
6369 0d6c6ee3 2020-05-20 stsp free(link_target);
6370 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6371 f91a2b48 2022-06-23 thomas 0, 0);
6372 ffd1d5e5 2018-06-23 stsp if (err) {
6373 ffd1d5e5 2018-06-23 stsp free(line);
6374 ffd1d5e5 2018-06-23 stsp break;
6375 ffd1d5e5 2018-06-23 stsp }
6376 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6377 0cf4efb1 2018-09-29 stsp if (view->focussed)
6378 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6379 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6380 ffd1d5e5 2018-06-23 stsp }
6381 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6382 f26dddb7 2019-11-08 stsp if (tc)
6383 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6384 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6385 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6386 f26dddb7 2019-11-08 stsp if (tc)
6387 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6388 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6389 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6390 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6391 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6392 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6393 ffd1d5e5 2018-06-23 stsp free(line);
6394 2550e4c3 2018-07-13 stsp free(wline);
6395 2550e4c3 2018-07-13 stsp wline = NULL;
6396 ffd1d5e5 2018-06-23 stsp n++;
6397 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6398 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6399 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6400 ffd1d5e5 2018-06-23 stsp break;
6401 ffd1d5e5 2018-06-23 stsp }
6402 ffd1d5e5 2018-06-23 stsp
6403 ffd1d5e5 2018-06-23 stsp return err;
6404 ffd1d5e5 2018-06-23 stsp }
6405 ffd1d5e5 2018-06-23 stsp
6406 ffd1d5e5 2018-06-23 stsp static void
6407 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6408 ffd1d5e5 2018-06-23 stsp {
6409 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6410 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6411 fa86c4bf 2020-11-29 stsp int i = 0;
6412 ffd1d5e5 2018-06-23 stsp
6413 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6414 ffd1d5e5 2018-06-23 stsp return;
6415 ffd1d5e5 2018-06-23 stsp
6416 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6417 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6418 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6419 fa86c4bf 2020-11-29 stsp if (!isroot)
6420 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6421 fa86c4bf 2020-11-29 stsp break;
6422 fa86c4bf 2020-11-29 stsp }
6423 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6424 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6425 ffd1d5e5 2018-06-23 stsp }
6426 ffd1d5e5 2018-06-23 stsp }
6427 ffd1d5e5 2018-06-23 stsp
6428 a5d43cac 2022-07-01 thomas static const struct got_error *
6429 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6430 ffd1d5e5 2018-06-23 stsp {
6431 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6432 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6433 ffd1d5e5 2018-06-23 stsp int n = 0;
6434 ffd1d5e5 2018-06-23 stsp
6435 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6436 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6437 694d3271 2020-12-01 naddy s->first_displayed_entry);
6438 694d3271 2020-12-01 naddy else
6439 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6440 56e0773d 2019-11-28 stsp
6441 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6442 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6443 a5d43cac 2022-07-01 thomas if (last)
6444 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6445 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6446 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6447 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6448 768394f3 2019-01-24 stsp }
6449 ffd1d5e5 2018-06-23 stsp }
6450 a5d43cac 2022-07-01 thomas
6451 a5d43cac 2022-07-01 thomas return NULL;
6452 ffd1d5e5 2018-06-23 stsp }
6453 ffd1d5e5 2018-06-23 stsp
6454 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6455 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6456 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6457 ffd1d5e5 2018-06-23 stsp {
6458 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6459 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6460 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6461 ffd1d5e5 2018-06-23 stsp
6462 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6463 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6464 56e0773d 2019-11-28 stsp + 1 /* slash */;
6465 ce52c690 2018-06-23 stsp if (te)
6466 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6467 ce52c690 2018-06-23 stsp
6468 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6469 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6470 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6471 ffd1d5e5 2018-06-23 stsp
6472 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6473 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6474 d9765a41 2018-06-23 stsp while (pt) {
6475 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6476 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6477 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6478 cb2ebc8a 2018-06-23 stsp goto done;
6479 cb2ebc8a 2018-06-23 stsp }
6480 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6481 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6482 cb2ebc8a 2018-06-23 stsp goto done;
6483 cb2ebc8a 2018-06-23 stsp }
6484 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6485 ffd1d5e5 2018-06-23 stsp }
6486 ce52c690 2018-06-23 stsp if (te) {
6487 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6488 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6489 ce52c690 2018-06-23 stsp goto done;
6490 ce52c690 2018-06-23 stsp }
6491 cb2ebc8a 2018-06-23 stsp }
6492 ce52c690 2018-06-23 stsp done:
6493 ce52c690 2018-06-23 stsp if (err) {
6494 ce52c690 2018-06-23 stsp free(*path);
6495 ce52c690 2018-06-23 stsp *path = NULL;
6496 ce52c690 2018-06-23 stsp }
6497 ce52c690 2018-06-23 stsp return err;
6498 ce52c690 2018-06-23 stsp }
6499 ce52c690 2018-06-23 stsp
6500 ce52c690 2018-06-23 stsp static const struct got_error *
6501 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6502 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6503 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6504 ce52c690 2018-06-23 stsp {
6505 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6506 ce52c690 2018-06-23 stsp char *path;
6507 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6508 a0de39f3 2019-08-09 stsp
6509 a0de39f3 2019-08-09 stsp *new_view = NULL;
6510 69efd4c4 2018-07-18 stsp
6511 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6512 ce52c690 2018-06-23 stsp if (err)
6513 ce52c690 2018-06-23 stsp return err;
6514 ffd1d5e5 2018-06-23 stsp
6515 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6516 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6517 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6518 83ce39e3 2019-08-12 stsp goto done;
6519 83ce39e3 2019-08-12 stsp }
6520 cdf1ee82 2018-08-01 stsp
6521 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6522 e5a0f69f 2018-08-18 stsp if (err) {
6523 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6524 fc06ba56 2019-08-22 stsp err = NULL;
6525 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6526 e5a0f69f 2018-08-18 stsp } else
6527 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6528 83ce39e3 2019-08-12 stsp done:
6529 83ce39e3 2019-08-12 stsp free(path);
6530 69efd4c4 2018-07-18 stsp return err;
6531 69efd4c4 2018-07-18 stsp }
6532 69efd4c4 2018-07-18 stsp
6533 69efd4c4 2018-07-18 stsp static const struct got_error *
6534 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6535 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6536 69efd4c4 2018-07-18 stsp {
6537 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6538 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6539 69efd4c4 2018-07-18 stsp char *path;
6540 a0de39f3 2019-08-09 stsp
6541 a0de39f3 2019-08-09 stsp *new_view = NULL;
6542 69efd4c4 2018-07-18 stsp
6543 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6544 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6545 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6546 e5a0f69f 2018-08-18 stsp
6547 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6548 69efd4c4 2018-07-18 stsp if (err)
6549 69efd4c4 2018-07-18 stsp return err;
6550 69efd4c4 2018-07-18 stsp
6551 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6552 4e97c21c 2020-12-06 stsp path, 0);
6553 ba4f502b 2018-08-04 stsp if (err)
6554 e5a0f69f 2018-08-18 stsp view_close(log_view);
6555 e5a0f69f 2018-08-18 stsp else
6556 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6557 cb2ebc8a 2018-06-23 stsp free(path);
6558 cb2ebc8a 2018-06-23 stsp return err;
6559 ffd1d5e5 2018-06-23 stsp }
6560 ffd1d5e5 2018-06-23 stsp
6561 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6562 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6563 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6564 ffd1d5e5 2018-06-23 stsp {
6565 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6566 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6567 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6568 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6569 ffd1d5e5 2018-06-23 stsp
6570 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6571 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6572 bc573f3b 2021-07-10 stsp
6573 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6574 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6575 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6576 ffd1d5e5 2018-06-23 stsp
6577 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6578 bc573f3b 2021-07-10 stsp if (err)
6579 bc573f3b 2021-07-10 stsp goto done;
6580 bc573f3b 2021-07-10 stsp
6581 bc573f3b 2021-07-10 stsp /*
6582 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6583 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6584 bc573f3b 2021-07-10 stsp * closed on demand.
6585 bc573f3b 2021-07-10 stsp */
6586 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6587 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6588 bc573f3b 2021-07-10 stsp if (err)
6589 bc573f3b 2021-07-10 stsp goto done;
6590 bc573f3b 2021-07-10 stsp s->tree = s->root;
6591 bc573f3b 2021-07-10 stsp
6592 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6593 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6594 ffd1d5e5 2018-06-23 stsp goto done;
6595 ffd1d5e5 2018-06-23 stsp
6596 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6597 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6598 ffd1d5e5 2018-06-23 stsp goto done;
6599 ffd1d5e5 2018-06-23 stsp }
6600 ffd1d5e5 2018-06-23 stsp
6601 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6602 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6603 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6604 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6605 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6606 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6607 9cd7cbd1 2020-12-07 stsp goto done;
6608 9cd7cbd1 2020-12-07 stsp }
6609 9cd7cbd1 2020-12-07 stsp }
6610 fb2756b9 2018-08-04 stsp s->repo = repo;
6611 c0b01bdb 2019-11-08 stsp
6612 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6613 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6614 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6615 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6616 c0b01bdb 2019-11-08 stsp if (err)
6617 c0b01bdb 2019-11-08 stsp goto done;
6618 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6619 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6620 bc573f3b 2021-07-10 stsp if (err)
6621 c0b01bdb 2019-11-08 stsp goto done;
6622 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6623 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6624 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6625 bc573f3b 2021-07-10 stsp if (err)
6626 c0b01bdb 2019-11-08 stsp goto done;
6627 e5a0f69f 2018-08-18 stsp
6628 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6629 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6630 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6631 bc573f3b 2021-07-10 stsp if (err)
6632 11b20872 2019-11-08 stsp goto done;
6633 11b20872 2019-11-08 stsp
6634 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6635 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6636 bc573f3b 2021-07-10 stsp if (err)
6637 c0b01bdb 2019-11-08 stsp goto done;
6638 c0b01bdb 2019-11-08 stsp }
6639 c0b01bdb 2019-11-08 stsp
6640 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6641 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6642 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6643 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6644 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6645 ad80ab7b 2018-08-04 stsp done:
6646 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6647 bc573f3b 2021-07-10 stsp if (commit)
6648 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6649 bc573f3b 2021-07-10 stsp if (err)
6650 bc573f3b 2021-07-10 stsp close_tree_view(view);
6651 ad80ab7b 2018-08-04 stsp return err;
6652 ad80ab7b 2018-08-04 stsp }
6653 ad80ab7b 2018-08-04 stsp
6654 e5a0f69f 2018-08-18 stsp static const struct got_error *
6655 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6656 ad80ab7b 2018-08-04 stsp {
6657 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6658 ad80ab7b 2018-08-04 stsp
6659 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6660 fb2756b9 2018-08-04 stsp free(s->tree_label);
6661 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6662 6484ec90 2018-09-29 stsp free(s->commit_id);
6663 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6664 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6665 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6666 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6667 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6668 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6669 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6670 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6671 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6672 ad80ab7b 2018-08-04 stsp free(parent);
6673 ad80ab7b 2018-08-04 stsp
6674 ad80ab7b 2018-08-04 stsp }
6675 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6676 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6677 bc573f3b 2021-07-10 stsp if (s->root)
6678 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6679 7c32bd05 2019-06-22 stsp return NULL;
6680 7c32bd05 2019-06-22 stsp }
6681 7c32bd05 2019-06-22 stsp
6682 7c32bd05 2019-06-22 stsp static const struct got_error *
6683 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6684 7c32bd05 2019-06-22 stsp {
6685 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6686 7c32bd05 2019-06-22 stsp
6687 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6688 7c32bd05 2019-06-22 stsp return NULL;
6689 7c32bd05 2019-06-22 stsp }
6690 7c32bd05 2019-06-22 stsp
6691 7c32bd05 2019-06-22 stsp static int
6692 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6693 7c32bd05 2019-06-22 stsp {
6694 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6695 7c32bd05 2019-06-22 stsp
6696 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6697 56e0773d 2019-11-28 stsp 0) == 0;
6698 7c32bd05 2019-06-22 stsp }
6699 7c32bd05 2019-06-22 stsp
6700 7c32bd05 2019-06-22 stsp static const struct got_error *
6701 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6702 7c32bd05 2019-06-22 stsp {
6703 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6704 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6705 7c32bd05 2019-06-22 stsp
6706 7c32bd05 2019-06-22 stsp if (!view->searching) {
6707 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6708 7c32bd05 2019-06-22 stsp return NULL;
6709 7c32bd05 2019-06-22 stsp }
6710 7c32bd05 2019-06-22 stsp
6711 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6712 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6713 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6714 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6715 56e0773d 2019-11-28 stsp s->selected_entry);
6716 7c32bd05 2019-06-22 stsp else
6717 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6718 56e0773d 2019-11-28 stsp } else {
6719 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6720 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6721 56e0773d 2019-11-28 stsp else
6722 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6723 56e0773d 2019-11-28 stsp s->selected_entry);
6724 7c32bd05 2019-06-22 stsp }
6725 7c32bd05 2019-06-22 stsp } else {
6726 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6727 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6728 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6729 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6730 56e0773d 2019-11-28 stsp else
6731 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6732 7c32bd05 2019-06-22 stsp }
6733 7c32bd05 2019-06-22 stsp
6734 7c32bd05 2019-06-22 stsp while (1) {
6735 56e0773d 2019-11-28 stsp if (te == NULL) {
6736 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6737 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6738 ac66afb8 2019-06-24 stsp return NULL;
6739 ac66afb8 2019-06-24 stsp }
6740 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6741 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6742 56e0773d 2019-11-28 stsp else
6743 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6744 7c32bd05 2019-06-22 stsp }
6745 7c32bd05 2019-06-22 stsp
6746 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6747 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6748 56e0773d 2019-11-28 stsp s->matched_entry = te;
6749 7c32bd05 2019-06-22 stsp break;
6750 7c32bd05 2019-06-22 stsp }
6751 7c32bd05 2019-06-22 stsp
6752 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6753 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6754 56e0773d 2019-11-28 stsp else
6755 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6756 7c32bd05 2019-06-22 stsp }
6757 e5a0f69f 2018-08-18 stsp
6758 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6759 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6760 7c32bd05 2019-06-22 stsp s->selected = 0;
6761 7c32bd05 2019-06-22 stsp }
6762 7c32bd05 2019-06-22 stsp
6763 e5a0f69f 2018-08-18 stsp return NULL;
6764 ad80ab7b 2018-08-04 stsp }
6765 ad80ab7b 2018-08-04 stsp
6766 ad80ab7b 2018-08-04 stsp static const struct got_error *
6767 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6768 ad80ab7b 2018-08-04 stsp {
6769 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6770 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6771 e5a0f69f 2018-08-18 stsp char *parent_path;
6772 ad80ab7b 2018-08-04 stsp
6773 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6774 e5a0f69f 2018-08-18 stsp if (err)
6775 e5a0f69f 2018-08-18 stsp return err;
6776 ffd1d5e5 2018-06-23 stsp
6777 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6778 e5a0f69f 2018-08-18 stsp free(parent_path);
6779 669b5ffa 2018-10-07 stsp
6780 a5d43cac 2022-07-01 thomas view_border(view);
6781 e5a0f69f 2018-08-18 stsp return err;
6782 e5a0f69f 2018-08-18 stsp }
6783 ce52c690 2018-06-23 stsp
6784 e5a0f69f 2018-08-18 stsp static const struct got_error *
6785 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6786 e5a0f69f 2018-08-18 stsp {
6787 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6788 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6789 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6790 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6791 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6792 ffd1d5e5 2018-06-23 stsp
6793 e5a0f69f 2018-08-18 stsp switch (ch) {
6794 1e37a5c2 2019-05-12 jcs case 'i':
6795 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6796 07b0611c 2022-06-23 thomas view->count = 0;
6797 1e37a5c2 2019-05-12 jcs break;
6798 1e37a5c2 2019-05-12 jcs case 'l':
6799 07b0611c 2022-06-23 thomas view->count = 0;
6800 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6801 ffd1d5e5 2018-06-23 stsp break;
6802 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6803 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6804 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6805 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6806 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6807 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6808 444d5325 2022-07-03 thomas if (err)
6809 444d5325 2022-07-03 thomas break;
6810 444d5325 2022-07-03 thomas }
6811 e78dc838 2020-12-04 stsp view->focussed = 0;
6812 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6813 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6814 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6815 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6816 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6817 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6818 1e37a5c2 2019-05-12 jcs if (err)
6819 1e37a5c2 2019-05-12 jcs return err;
6820 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6821 40236d76 2022-06-23 thomas if (err)
6822 40236d76 2022-06-23 thomas return err;
6823 e78dc838 2020-12-04 stsp view->focus_child = 1;
6824 1e37a5c2 2019-05-12 jcs } else
6825 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6826 152c1c93 2020-11-29 stsp break;
6827 152c1c93 2020-11-29 stsp case 'r':
6828 07b0611c 2022-06-23 thomas view->count = 0;
6829 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6830 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6831 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6832 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6833 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6834 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6835 152c1c93 2020-11-29 stsp if (err) {
6836 152c1c93 2020-11-29 stsp view_close(ref_view);
6837 152c1c93 2020-11-29 stsp return err;
6838 152c1c93 2020-11-29 stsp }
6839 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6840 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6841 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6842 444d5325 2022-07-03 thomas if (err)
6843 444d5325 2022-07-03 thomas break;
6844 444d5325 2022-07-03 thomas }
6845 e78dc838 2020-12-04 stsp view->focussed = 0;
6846 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6847 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6848 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6849 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6850 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6851 152c1c93 2020-11-29 stsp err = view_close_child(view);
6852 152c1c93 2020-11-29 stsp if (err)
6853 152c1c93 2020-11-29 stsp return err;
6854 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6855 40236d76 2022-06-23 thomas if (err)
6856 40236d76 2022-06-23 thomas return err;
6857 e78dc838 2020-12-04 stsp view->focus_child = 1;
6858 152c1c93 2020-11-29 stsp } else
6859 152c1c93 2020-11-29 stsp *new_view = ref_view;
6860 e4526bf5 2021-09-03 naddy break;
6861 e4526bf5 2021-09-03 naddy case 'g':
6862 e4526bf5 2021-09-03 naddy case KEY_HOME:
6863 e4526bf5 2021-09-03 naddy s->selected = 0;
6864 07b0611c 2022-06-23 thomas view->count = 0;
6865 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6866 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6867 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6868 e4526bf5 2021-09-03 naddy else
6869 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6870 1e37a5c2 2019-05-12 jcs break;
6871 e4526bf5 2021-09-03 naddy case 'G':
6872 a5d43cac 2022-07-01 thomas case KEY_END: {
6873 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6874 a5d43cac 2022-07-01 thomas
6875 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6876 a5d43cac 2022-07-01 thomas --eos; /* border */
6877 e4526bf5 2021-09-03 naddy s->selected = 0;
6878 07b0611c 2022-06-23 thomas view->count = 0;
6879 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6880 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6881 e4526bf5 2021-09-03 naddy if (te == NULL) {
6882 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6883 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6884 e4526bf5 2021-09-03 naddy n++;
6885 e4526bf5 2021-09-03 naddy }
6886 e4526bf5 2021-09-03 naddy break;
6887 e4526bf5 2021-09-03 naddy }
6888 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6889 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6890 e4526bf5 2021-09-03 naddy }
6891 e4526bf5 2021-09-03 naddy if (n > 0)
6892 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6893 e4526bf5 2021-09-03 naddy break;
6894 a5d43cac 2022-07-01 thomas }
6895 1e37a5c2 2019-05-12 jcs case 'k':
6896 1e37a5c2 2019-05-12 jcs case KEY_UP:
6897 f7140bf5 2021-10-17 thomas case CTRL('p'):
6898 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6899 1e37a5c2 2019-05-12 jcs s->selected--;
6900 fa86c4bf 2020-11-29 stsp break;
6901 1e37a5c2 2019-05-12 jcs }
6902 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6903 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6904 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6905 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6906 07b0611c 2022-06-23 thomas view->count = 0;
6907 1e37a5c2 2019-05-12 jcs break;
6908 70f17a53 2022-06-13 thomas case CTRL('u'):
6909 23427b14 2022-06-23 thomas case 'u':
6910 70f17a53 2022-06-13 thomas nscroll /= 2;
6911 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6912 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6913 ea025d1d 2020-02-22 naddy case CTRL('b'):
6914 1c5e5faa 2022-06-23 thomas case 'b':
6915 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6916 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6917 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6918 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6919 fa86c4bf 2020-11-29 stsp } else {
6920 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6921 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6922 fa86c4bf 2020-11-29 stsp }
6923 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6924 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6925 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6926 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6927 07b0611c 2022-06-23 thomas view->count = 0;
6928 1e37a5c2 2019-05-12 jcs break;
6929 1e37a5c2 2019-05-12 jcs case 'j':
6930 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6931 f7140bf5 2021-10-17 thomas case CTRL('n'):
6932 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6933 1e37a5c2 2019-05-12 jcs s->selected++;
6934 1e37a5c2 2019-05-12 jcs break;
6935 1e37a5c2 2019-05-12 jcs }
6936 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6937 07b0611c 2022-06-23 thomas == NULL) {
6938 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6939 07b0611c 2022-06-23 thomas view->count = 0;
6940 1e37a5c2 2019-05-12 jcs break;
6941 07b0611c 2022-06-23 thomas }
6942 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6943 1e37a5c2 2019-05-12 jcs break;
6944 70f17a53 2022-06-13 thomas case CTRL('d'):
6945 23427b14 2022-06-23 thomas case 'd':
6946 70f17a53 2022-06-13 thomas nscroll /= 2;
6947 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6948 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6949 ea025d1d 2020-02-22 naddy case CTRL('f'):
6950 1c5e5faa 2022-06-23 thomas case 'f':
6951 4c2d69cb 2022-06-23 thomas case ' ':
6952 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6953 1e37a5c2 2019-05-12 jcs == NULL) {
6954 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6955 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6956 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6957 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6958 07b0611c 2022-06-23 thomas else
6959 07b0611c 2022-06-23 thomas view->count = 0;
6960 1e37a5c2 2019-05-12 jcs break;
6961 1e37a5c2 2019-05-12 jcs }
6962 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6963 1e37a5c2 2019-05-12 jcs break;
6964 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6965 1e37a5c2 2019-05-12 jcs case '\r':
6966 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6967 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6968 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6969 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6970 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6971 07b0611c 2022-06-23 thomas view->count = 0;
6972 1e37a5c2 2019-05-12 jcs break;
6973 07b0611c 2022-06-23 thomas }
6974 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6975 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6976 1e37a5c2 2019-05-12 jcs entry);
6977 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6978 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6979 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6980 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6981 1e37a5c2 2019-05-12 jcs s->selected_entry =
6982 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6983 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6984 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6985 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6986 a5d43cac 2022-07-01 thomas if (err)
6987 a5d43cac 2022-07-01 thomas break;
6988 a5d43cac 2022-07-01 thomas }
6989 1e37a5c2 2019-05-12 jcs free(parent);
6990 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6991 56e0773d 2019-11-28 stsp s->selected_entry))) {
6992 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6993 07b0611c 2022-06-23 thomas view->count = 0;
6994 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6995 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6996 1e37a5c2 2019-05-12 jcs if (err)
6997 1e37a5c2 2019-05-12 jcs break;
6998 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6999 941e9f74 2019-05-21 stsp if (err) {
7000 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7001 1e37a5c2 2019-05-12 jcs break;
7002 1e37a5c2 2019-05-12 jcs }
7003 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7004 56e0773d 2019-11-28 stsp s->selected_entry))) {
7005 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7006 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
7007 1e37a5c2 2019-05-12 jcs
7008 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
7009 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7010 a5d43cac 2022-07-01 thomas
7011 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
7012 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7013 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7014 1e37a5c2 2019-05-12 jcs if (err)
7015 1e37a5c2 2019-05-12 jcs break;
7016 a5d43cac 2022-07-01 thomas
7017 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7018 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7019 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7020 a5d43cac 2022-07-01 thomas if (err)
7021 a5d43cac 2022-07-01 thomas break;
7022 a5d43cac 2022-07-01 thomas }
7023 a5d43cac 2022-07-01 thomas
7024 07b0611c 2022-06-23 thomas view->count = 0;
7025 e78dc838 2020-12-04 stsp view->focussed = 0;
7026 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7027 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
7028 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
7029 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7030 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
7031 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7032 669b5ffa 2018-10-07 stsp if (err)
7033 669b5ffa 2018-10-07 stsp return err;
7034 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
7035 40236d76 2022-06-23 thomas if (err)
7036 40236d76 2022-06-23 thomas return err;
7037 e78dc838 2020-12-04 stsp view->focus_child = 1;
7038 669b5ffa 2018-10-07 stsp } else
7039 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7040 1e37a5c2 2019-05-12 jcs }
7041 1e37a5c2 2019-05-12 jcs break;
7042 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7043 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7044 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7045 07b0611c 2022-06-23 thomas view->count = 0;
7046 1e37a5c2 2019-05-12 jcs break;
7047 1e37a5c2 2019-05-12 jcs default:
7048 07b0611c 2022-06-23 thomas view->count = 0;
7049 1e37a5c2 2019-05-12 jcs break;
7050 ffd1d5e5 2018-06-23 stsp }
7051 e5a0f69f 2018-08-18 stsp
7052 ffd1d5e5 2018-06-23 stsp return err;
7053 9f7d7167 2018-04-29 stsp }
7054 9f7d7167 2018-04-29 stsp
7055 ffd1d5e5 2018-06-23 stsp __dead static void
7056 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7057 ffd1d5e5 2018-06-23 stsp {
7058 ffd1d5e5 2018-06-23 stsp endwin();
7059 91198554 2022-06-23 thomas fprintf(stderr,
7060 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7061 ffd1d5e5 2018-06-23 stsp getprogname());
7062 ffd1d5e5 2018-06-23 stsp exit(1);
7063 ffd1d5e5 2018-06-23 stsp }
7064 ffd1d5e5 2018-06-23 stsp
7065 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7066 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7067 ffd1d5e5 2018-06-23 stsp {
7068 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7069 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7070 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7071 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7072 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7073 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7074 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7075 4e97c21c 2020-12-06 stsp char *label = NULL;
7076 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7077 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7078 ffd1d5e5 2018-06-23 stsp int ch;
7079 5221c383 2018-08-01 stsp struct tog_view *view;
7080 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7081 70ac5f84 2019-03-28 stsp
7082 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7083 ffd1d5e5 2018-06-23 stsp switch (ch) {
7084 ffd1d5e5 2018-06-23 stsp case 'c':
7085 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7086 74283ab8 2020-02-07 stsp break;
7087 74283ab8 2020-02-07 stsp case 'r':
7088 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7089 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7090 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7091 74283ab8 2020-02-07 stsp optarg);
7092 ffd1d5e5 2018-06-23 stsp break;
7093 ffd1d5e5 2018-06-23 stsp default:
7094 e99e2d15 2020-11-24 naddy usage_tree();
7095 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7096 ffd1d5e5 2018-06-23 stsp }
7097 ffd1d5e5 2018-06-23 stsp }
7098 ffd1d5e5 2018-06-23 stsp
7099 ffd1d5e5 2018-06-23 stsp argc -= optind;
7100 ffd1d5e5 2018-06-23 stsp argv += optind;
7101 ffd1d5e5 2018-06-23 stsp
7102 55cccc34 2020-02-20 stsp if (argc > 1)
7103 e99e2d15 2020-11-24 naddy usage_tree();
7104 7cd52833 2022-06-23 thomas
7105 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7106 7cd52833 2022-06-23 thomas if (error != NULL)
7107 7cd52833 2022-06-23 thomas goto done;
7108 74283ab8 2020-02-07 stsp
7109 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7110 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7111 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7112 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7113 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7114 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7115 c156c7a4 2020-12-18 stsp goto done;
7116 55cccc34 2020-02-20 stsp if (worktree)
7117 52185f70 2019-02-05 stsp repo_path =
7118 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7119 55cccc34 2020-02-20 stsp else
7120 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7121 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7122 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7123 c156c7a4 2020-12-18 stsp goto done;
7124 c156c7a4 2020-12-18 stsp }
7125 55cccc34 2020-02-20 stsp }
7126 a915003a 2019-02-05 stsp
7127 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7128 c02c541e 2019-03-29 stsp if (error != NULL)
7129 52185f70 2019-02-05 stsp goto done;
7130 d188b9a6 2019-01-04 stsp
7131 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7132 55cccc34 2020-02-20 stsp repo, worktree);
7133 55cccc34 2020-02-20 stsp if (error)
7134 55cccc34 2020-02-20 stsp goto done;
7135 55cccc34 2020-02-20 stsp
7136 55cccc34 2020-02-20 stsp init_curses();
7137 55cccc34 2020-02-20 stsp
7138 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7139 c02c541e 2019-03-29 stsp if (error)
7140 52185f70 2019-02-05 stsp goto done;
7141 ffd1d5e5 2018-06-23 stsp
7142 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7143 51a10b52 2020-12-26 stsp if (error)
7144 51a10b52 2020-12-26 stsp goto done;
7145 51a10b52 2020-12-26 stsp
7146 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7147 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7148 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7149 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7150 4e97c21c 2020-12-06 stsp if (error)
7151 4e97c21c 2020-12-06 stsp goto done;
7152 4e97c21c 2020-12-06 stsp head_ref_name = label;
7153 4e97c21c 2020-12-06 stsp } else {
7154 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7155 4e97c21c 2020-12-06 stsp if (error == NULL)
7156 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7157 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7158 4e97c21c 2020-12-06 stsp goto done;
7159 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7160 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7161 4e97c21c 2020-12-06 stsp if (error)
7162 4e97c21c 2020-12-06 stsp goto done;
7163 4e97c21c 2020-12-06 stsp }
7164 ffd1d5e5 2018-06-23 stsp
7165 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7166 945f9229 2022-04-16 thomas if (error)
7167 945f9229 2022-04-16 thomas goto done;
7168 945f9229 2022-04-16 thomas
7169 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7170 5221c383 2018-08-01 stsp if (view == NULL) {
7171 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7172 5221c383 2018-08-01 stsp goto done;
7173 5221c383 2018-08-01 stsp }
7174 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7175 ad80ab7b 2018-08-04 stsp if (error)
7176 ad80ab7b 2018-08-04 stsp goto done;
7177 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7178 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7179 d91faf3b 2020-12-01 naddy in_repo_path);
7180 55cccc34 2020-02-20 stsp if (error)
7181 55cccc34 2020-02-20 stsp goto done;
7182 55cccc34 2020-02-20 stsp }
7183 55cccc34 2020-02-20 stsp
7184 55cccc34 2020-02-20 stsp if (worktree) {
7185 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7186 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7187 55cccc34 2020-02-20 stsp worktree = NULL;
7188 55cccc34 2020-02-20 stsp }
7189 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7190 ffd1d5e5 2018-06-23 stsp done:
7191 52185f70 2019-02-05 stsp free(repo_path);
7192 e4a0e26d 2020-02-20 stsp free(cwd);
7193 ffd1d5e5 2018-06-23 stsp free(commit_id);
7194 4e97c21c 2020-12-06 stsp free(label);
7195 486cd271 2020-12-06 stsp if (ref)
7196 486cd271 2020-12-06 stsp got_ref_close(ref);
7197 1d0f4054 2021-06-17 stsp if (repo) {
7198 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7199 1d0f4054 2021-06-17 stsp if (error == NULL)
7200 1d0f4054 2021-06-17 stsp error = close_err;
7201 1d0f4054 2021-06-17 stsp }
7202 7cd52833 2022-06-23 thomas if (pack_fds) {
7203 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7204 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7205 7cd52833 2022-06-23 thomas if (error == NULL)
7206 7cd52833 2022-06-23 thomas error = pack_err;
7207 7cd52833 2022-06-23 thomas }
7208 51a10b52 2020-12-26 stsp tog_free_refs();
7209 ffd1d5e5 2018-06-23 stsp return error;
7210 6458efa5 2020-11-24 stsp }
7211 6458efa5 2020-11-24 stsp
7212 6458efa5 2020-11-24 stsp static const struct got_error *
7213 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7214 6458efa5 2020-11-24 stsp {
7215 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7216 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7217 6458efa5 2020-11-24 stsp
7218 6458efa5 2020-11-24 stsp s->nrefs = 0;
7219 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7220 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7221 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7222 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7223 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7224 6458efa5 2020-11-24 stsp continue;
7225 6458efa5 2020-11-24 stsp
7226 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7227 6458efa5 2020-11-24 stsp if (re == NULL)
7228 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7229 6458efa5 2020-11-24 stsp
7230 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7231 8924d611 2020-12-26 stsp if (re->ref == NULL)
7232 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7233 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7234 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7235 6458efa5 2020-11-24 stsp }
7236 6458efa5 2020-11-24 stsp
7237 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7238 6458efa5 2020-11-24 stsp return NULL;
7239 6458efa5 2020-11-24 stsp }
7240 6458efa5 2020-11-24 stsp
7241 ef20f542 2022-06-26 thomas static void
7242 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7243 6458efa5 2020-11-24 stsp {
7244 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7245 6458efa5 2020-11-24 stsp
7246 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7247 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7248 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7249 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7250 6458efa5 2020-11-24 stsp free(re);
7251 6458efa5 2020-11-24 stsp }
7252 6458efa5 2020-11-24 stsp }
7253 6458efa5 2020-11-24 stsp
7254 6458efa5 2020-11-24 stsp static const struct got_error *
7255 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7256 6458efa5 2020-11-24 stsp {
7257 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7258 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7259 6458efa5 2020-11-24 stsp
7260 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7261 6458efa5 2020-11-24 stsp s->repo = repo;
7262 6458efa5 2020-11-24 stsp
7263 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7264 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7265 6458efa5 2020-11-24 stsp
7266 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7267 6458efa5 2020-11-24 stsp if (err)
7268 6458efa5 2020-11-24 stsp return err;
7269 34ba6917 2020-11-30 stsp
7270 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7271 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7272 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7273 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7274 6458efa5 2020-11-24 stsp if (err)
7275 6458efa5 2020-11-24 stsp goto done;
7276 6458efa5 2020-11-24 stsp
7277 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7278 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7279 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7280 6458efa5 2020-11-24 stsp if (err)
7281 6458efa5 2020-11-24 stsp goto done;
7282 6458efa5 2020-11-24 stsp
7283 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7284 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7285 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7286 2183bbf6 2022-01-23 thomas if (err)
7287 2183bbf6 2022-01-23 thomas goto done;
7288 2183bbf6 2022-01-23 thomas
7289 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7290 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7291 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7292 6458efa5 2020-11-24 stsp if (err)
7293 6458efa5 2020-11-24 stsp goto done;
7294 6458efa5 2020-11-24 stsp }
7295 6458efa5 2020-11-24 stsp
7296 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7297 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7298 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7299 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7300 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7301 6458efa5 2020-11-24 stsp done:
7302 6458efa5 2020-11-24 stsp if (err)
7303 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7304 6458efa5 2020-11-24 stsp return err;
7305 6458efa5 2020-11-24 stsp }
7306 6458efa5 2020-11-24 stsp
7307 6458efa5 2020-11-24 stsp static const struct got_error *
7308 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7309 6458efa5 2020-11-24 stsp {
7310 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7311 6458efa5 2020-11-24 stsp
7312 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7313 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7314 6458efa5 2020-11-24 stsp
7315 6458efa5 2020-11-24 stsp return NULL;
7316 9f7d7167 2018-04-29 stsp }
7317 ce5b7c56 2019-07-09 stsp
7318 6458efa5 2020-11-24 stsp static const struct got_error *
7319 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7320 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7321 6458efa5 2020-11-24 stsp {
7322 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7323 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7324 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7325 6458efa5 2020-11-24 stsp int obj_type;
7326 6458efa5 2020-11-24 stsp
7327 c42c9805 2020-11-24 stsp *commit_id = NULL;
7328 6458efa5 2020-11-24 stsp
7329 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7330 6458efa5 2020-11-24 stsp if (err)
7331 6458efa5 2020-11-24 stsp return err;
7332 6458efa5 2020-11-24 stsp
7333 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7334 6458efa5 2020-11-24 stsp if (err)
7335 6458efa5 2020-11-24 stsp goto done;
7336 6458efa5 2020-11-24 stsp
7337 6458efa5 2020-11-24 stsp switch (obj_type) {
7338 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7339 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7340 6458efa5 2020-11-24 stsp break;
7341 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7342 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7343 6458efa5 2020-11-24 stsp if (err)
7344 6458efa5 2020-11-24 stsp goto done;
7345 c42c9805 2020-11-24 stsp free(obj_id);
7346 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7347 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7348 c42c9805 2020-11-24 stsp if (err)
7349 6458efa5 2020-11-24 stsp goto done;
7350 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7351 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7352 c42c9805 2020-11-24 stsp goto done;
7353 c42c9805 2020-11-24 stsp }
7354 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7355 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7356 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7357 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7358 c42c9805 2020-11-24 stsp goto done;
7359 c42c9805 2020-11-24 stsp }
7360 6458efa5 2020-11-24 stsp break;
7361 6458efa5 2020-11-24 stsp default:
7362 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7363 c42c9805 2020-11-24 stsp break;
7364 6458efa5 2020-11-24 stsp }
7365 6458efa5 2020-11-24 stsp
7366 c42c9805 2020-11-24 stsp done:
7367 c42c9805 2020-11-24 stsp if (tag)
7368 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7369 c42c9805 2020-11-24 stsp if (err) {
7370 c42c9805 2020-11-24 stsp free(*commit_id);
7371 c42c9805 2020-11-24 stsp *commit_id = NULL;
7372 c42c9805 2020-11-24 stsp }
7373 c42c9805 2020-11-24 stsp return err;
7374 c42c9805 2020-11-24 stsp }
7375 c42c9805 2020-11-24 stsp
7376 c42c9805 2020-11-24 stsp static const struct got_error *
7377 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7378 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7379 c42c9805 2020-11-24 stsp {
7380 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7381 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7382 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7383 c42c9805 2020-11-24 stsp
7384 c42c9805 2020-11-24 stsp *new_view = NULL;
7385 c42c9805 2020-11-24 stsp
7386 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7387 c42c9805 2020-11-24 stsp if (err) {
7388 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7389 c42c9805 2020-11-24 stsp return err;
7390 c42c9805 2020-11-24 stsp else
7391 c42c9805 2020-11-24 stsp return NULL;
7392 c42c9805 2020-11-24 stsp }
7393 c42c9805 2020-11-24 stsp
7394 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7395 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7396 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7397 6458efa5 2020-11-24 stsp goto done;
7398 6458efa5 2020-11-24 stsp }
7399 6458efa5 2020-11-24 stsp
7400 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7401 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7402 6458efa5 2020-11-24 stsp done:
7403 6458efa5 2020-11-24 stsp if (err)
7404 6458efa5 2020-11-24 stsp view_close(log_view);
7405 6458efa5 2020-11-24 stsp else
7406 6458efa5 2020-11-24 stsp *new_view = log_view;
7407 c42c9805 2020-11-24 stsp free(commit_id);
7408 6458efa5 2020-11-24 stsp return err;
7409 6458efa5 2020-11-24 stsp }
7410 6458efa5 2020-11-24 stsp
7411 ce5b7c56 2019-07-09 stsp static void
7412 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7413 6458efa5 2020-11-24 stsp {
7414 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7415 34ba6917 2020-11-30 stsp int i = 0;
7416 6458efa5 2020-11-24 stsp
7417 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7418 6458efa5 2020-11-24 stsp return;
7419 6458efa5 2020-11-24 stsp
7420 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7421 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7422 34ba6917 2020-11-30 stsp if (re == NULL)
7423 34ba6917 2020-11-30 stsp break;
7424 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7425 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7426 6458efa5 2020-11-24 stsp }
7427 6458efa5 2020-11-24 stsp }
7428 6458efa5 2020-11-24 stsp
7429 a5d43cac 2022-07-01 thomas static const struct got_error *
7430 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7431 6458efa5 2020-11-24 stsp {
7432 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7433 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7434 6458efa5 2020-11-24 stsp int n = 0;
7435 6458efa5 2020-11-24 stsp
7436 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7437 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7438 6458efa5 2020-11-24 stsp else
7439 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7440 6458efa5 2020-11-24 stsp
7441 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7442 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7443 a5d43cac 2022-07-01 thomas if (last)
7444 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7445 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7446 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7447 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7448 6458efa5 2020-11-24 stsp }
7449 6458efa5 2020-11-24 stsp }
7450 a5d43cac 2022-07-01 thomas
7451 a5d43cac 2022-07-01 thomas return NULL;
7452 6458efa5 2020-11-24 stsp }
7453 6458efa5 2020-11-24 stsp
7454 6458efa5 2020-11-24 stsp static const struct got_error *
7455 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7456 6458efa5 2020-11-24 stsp {
7457 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7458 6458efa5 2020-11-24 stsp
7459 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7460 6458efa5 2020-11-24 stsp return NULL;
7461 6458efa5 2020-11-24 stsp }
7462 6458efa5 2020-11-24 stsp
7463 6458efa5 2020-11-24 stsp static int
7464 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7465 6458efa5 2020-11-24 stsp {
7466 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7467 6458efa5 2020-11-24 stsp
7468 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7469 6458efa5 2020-11-24 stsp 0) == 0;
7470 6458efa5 2020-11-24 stsp }
7471 6458efa5 2020-11-24 stsp
7472 6458efa5 2020-11-24 stsp static const struct got_error *
7473 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7474 6458efa5 2020-11-24 stsp {
7475 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7477 6458efa5 2020-11-24 stsp
7478 6458efa5 2020-11-24 stsp if (!view->searching) {
7479 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7480 6458efa5 2020-11-24 stsp return NULL;
7481 6458efa5 2020-11-24 stsp }
7482 6458efa5 2020-11-24 stsp
7483 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7484 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7485 6458efa5 2020-11-24 stsp if (s->selected_entry)
7486 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7487 6458efa5 2020-11-24 stsp else
7488 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7489 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7490 6458efa5 2020-11-24 stsp } else {
7491 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7492 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7493 6458efa5 2020-11-24 stsp else
7494 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7495 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7496 6458efa5 2020-11-24 stsp }
7497 6458efa5 2020-11-24 stsp } else {
7498 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7499 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7500 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7501 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7502 6458efa5 2020-11-24 stsp else
7503 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7504 6458efa5 2020-11-24 stsp }
7505 6458efa5 2020-11-24 stsp
7506 6458efa5 2020-11-24 stsp while (1) {
7507 6458efa5 2020-11-24 stsp if (re == NULL) {
7508 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7509 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7510 6458efa5 2020-11-24 stsp return NULL;
7511 6458efa5 2020-11-24 stsp }
7512 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7513 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7514 6458efa5 2020-11-24 stsp else
7515 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7516 6458efa5 2020-11-24 stsp }
7517 6458efa5 2020-11-24 stsp
7518 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7519 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7520 6458efa5 2020-11-24 stsp s->matched_entry = re;
7521 6458efa5 2020-11-24 stsp break;
7522 6458efa5 2020-11-24 stsp }
7523 6458efa5 2020-11-24 stsp
7524 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7525 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7526 6458efa5 2020-11-24 stsp else
7527 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7528 6458efa5 2020-11-24 stsp }
7529 6458efa5 2020-11-24 stsp
7530 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7531 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7532 6458efa5 2020-11-24 stsp s->selected = 0;
7533 6458efa5 2020-11-24 stsp }
7534 6458efa5 2020-11-24 stsp
7535 6458efa5 2020-11-24 stsp return NULL;
7536 6458efa5 2020-11-24 stsp }
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp static const struct got_error *
7539 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7540 6458efa5 2020-11-24 stsp {
7541 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7542 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7543 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7544 6458efa5 2020-11-24 stsp char *line = NULL;
7545 6458efa5 2020-11-24 stsp wchar_t *wline;
7546 6458efa5 2020-11-24 stsp struct tog_color *tc;
7547 6458efa5 2020-11-24 stsp int width, n;
7548 6458efa5 2020-11-24 stsp int limit = view->nlines;
7549 6458efa5 2020-11-24 stsp
7550 6458efa5 2020-11-24 stsp werase(view->window);
7551 6458efa5 2020-11-24 stsp
7552 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7553 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7554 a5d43cac 2022-07-01 thomas --limit; /* border */
7555 6458efa5 2020-11-24 stsp
7556 6458efa5 2020-11-24 stsp if (limit == 0)
7557 6458efa5 2020-11-24 stsp return NULL;
7558 6458efa5 2020-11-24 stsp
7559 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7562 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7563 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7564 6458efa5 2020-11-24 stsp
7565 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7566 6458efa5 2020-11-24 stsp if (err) {
7567 6458efa5 2020-11-24 stsp free(line);
7568 6458efa5 2020-11-24 stsp return err;
7569 6458efa5 2020-11-24 stsp }
7570 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7571 6458efa5 2020-11-24 stsp wstandout(view->window);
7572 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7573 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7574 6458efa5 2020-11-24 stsp wstandend(view->window);
7575 6458efa5 2020-11-24 stsp free(wline);
7576 6458efa5 2020-11-24 stsp wline = NULL;
7577 6458efa5 2020-11-24 stsp free(line);
7578 6458efa5 2020-11-24 stsp line = NULL;
7579 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7580 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7581 6458efa5 2020-11-24 stsp if (--limit <= 0)
7582 6458efa5 2020-11-24 stsp return NULL;
7583 6458efa5 2020-11-24 stsp
7584 6458efa5 2020-11-24 stsp n = 0;
7585 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7586 6458efa5 2020-11-24 stsp char *line = NULL;
7587 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7588 6458efa5 2020-11-24 stsp
7589 84227eb1 2022-06-23 thomas if (s->show_date) {
7590 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7591 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7592 84227eb1 2022-06-23 thomas struct got_object_id *id;
7593 84227eb1 2022-06-23 thomas struct tm tm;
7594 84227eb1 2022-06-23 thomas time_t t;
7595 84227eb1 2022-06-23 thomas
7596 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7597 84227eb1 2022-06-23 thomas if (err)
7598 84227eb1 2022-06-23 thomas return err;
7599 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7600 84227eb1 2022-06-23 thomas if (err) {
7601 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7602 84227eb1 2022-06-23 thomas free(id);
7603 84227eb1 2022-06-23 thomas return err;
7604 84227eb1 2022-06-23 thomas }
7605 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7606 84227eb1 2022-06-23 thomas id);
7607 84227eb1 2022-06-23 thomas if (err) {
7608 84227eb1 2022-06-23 thomas free(id);
7609 84227eb1 2022-06-23 thomas return err;
7610 84227eb1 2022-06-23 thomas }
7611 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7612 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7613 84227eb1 2022-06-23 thomas } else {
7614 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7615 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7616 84227eb1 2022-06-23 thomas }
7617 84227eb1 2022-06-23 thomas free(id);
7618 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7619 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7620 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7621 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7622 84227eb1 2022-06-23 thomas }
7623 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7624 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7625 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7626 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7627 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7628 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7629 6458efa5 2020-11-24 stsp struct got_object_id *id;
7630 6458efa5 2020-11-24 stsp char *id_str;
7631 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7632 6458efa5 2020-11-24 stsp if (err)
7633 6458efa5 2020-11-24 stsp return err;
7634 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7635 6458efa5 2020-11-24 stsp if (err) {
7636 6458efa5 2020-11-24 stsp free(id);
7637 6458efa5 2020-11-24 stsp return err;
7638 6458efa5 2020-11-24 stsp }
7639 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7640 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7641 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7642 6458efa5 2020-11-24 stsp free(id);
7643 6458efa5 2020-11-24 stsp free(id_str);
7644 6458efa5 2020-11-24 stsp return err;
7645 6458efa5 2020-11-24 stsp }
7646 6458efa5 2020-11-24 stsp free(id);
7647 6458efa5 2020-11-24 stsp free(id_str);
7648 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7649 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7650 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7651 6458efa5 2020-11-24 stsp
7652 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7653 f91a2b48 2022-06-23 thomas 0, 0);
7654 6458efa5 2020-11-24 stsp if (err) {
7655 6458efa5 2020-11-24 stsp free(line);
7656 6458efa5 2020-11-24 stsp return err;
7657 6458efa5 2020-11-24 stsp }
7658 6458efa5 2020-11-24 stsp if (n == s->selected) {
7659 6458efa5 2020-11-24 stsp if (view->focussed)
7660 6458efa5 2020-11-24 stsp wstandout(view->window);
7661 6458efa5 2020-11-24 stsp s->selected_entry = re;
7662 6458efa5 2020-11-24 stsp }
7663 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7664 6458efa5 2020-11-24 stsp if (tc)
7665 6458efa5 2020-11-24 stsp wattr_on(view->window,
7666 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7667 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7668 6458efa5 2020-11-24 stsp if (tc)
7669 6458efa5 2020-11-24 stsp wattr_off(view->window,
7670 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7671 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7672 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7673 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7674 6458efa5 2020-11-24 stsp wstandend(view->window);
7675 6458efa5 2020-11-24 stsp free(line);
7676 6458efa5 2020-11-24 stsp free(wline);
7677 6458efa5 2020-11-24 stsp wline = NULL;
7678 6458efa5 2020-11-24 stsp n++;
7679 6458efa5 2020-11-24 stsp s->ndisplayed++;
7680 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7681 6458efa5 2020-11-24 stsp
7682 6458efa5 2020-11-24 stsp limit--;
7683 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7684 6458efa5 2020-11-24 stsp }
7685 6458efa5 2020-11-24 stsp
7686 a5d43cac 2022-07-01 thomas view_border(view);
7687 6458efa5 2020-11-24 stsp return err;
7688 6458efa5 2020-11-24 stsp }
7689 6458efa5 2020-11-24 stsp
7690 6458efa5 2020-11-24 stsp static const struct got_error *
7691 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7692 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7693 c42c9805 2020-11-24 stsp {
7694 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7695 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7696 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7697 c42c9805 2020-11-24 stsp
7698 c42c9805 2020-11-24 stsp *new_view = NULL;
7699 c42c9805 2020-11-24 stsp
7700 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7701 c42c9805 2020-11-24 stsp if (err) {
7702 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7703 c42c9805 2020-11-24 stsp return err;
7704 c42c9805 2020-11-24 stsp else
7705 c42c9805 2020-11-24 stsp return NULL;
7706 c42c9805 2020-11-24 stsp }
7707 c42c9805 2020-11-24 stsp
7708 c42c9805 2020-11-24 stsp
7709 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7710 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7711 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7712 c42c9805 2020-11-24 stsp goto done;
7713 c42c9805 2020-11-24 stsp }
7714 c42c9805 2020-11-24 stsp
7715 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7716 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7717 c42c9805 2020-11-24 stsp if (err)
7718 c42c9805 2020-11-24 stsp goto done;
7719 c42c9805 2020-11-24 stsp
7720 c42c9805 2020-11-24 stsp *new_view = tree_view;
7721 c42c9805 2020-11-24 stsp done:
7722 c42c9805 2020-11-24 stsp free(commit_id);
7723 c42c9805 2020-11-24 stsp return err;
7724 c42c9805 2020-11-24 stsp }
7725 c42c9805 2020-11-24 stsp static const struct got_error *
7726 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7727 6458efa5 2020-11-24 stsp {
7728 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7729 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7730 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7731 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7732 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7733 6458efa5 2020-11-24 stsp
7734 6458efa5 2020-11-24 stsp switch (ch) {
7735 6458efa5 2020-11-24 stsp case 'i':
7736 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7737 07b0611c 2022-06-23 thomas view->count = 0;
7738 3bfadbd4 2021-11-20 thomas break;
7739 84227eb1 2022-06-23 thomas case 'm':
7740 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7741 07b0611c 2022-06-23 thomas view->count = 0;
7742 84227eb1 2022-06-23 thomas break;
7743 98182bd0 2021-11-20 thomas case 'o':
7744 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7745 07b0611c 2022-06-23 thomas view->count = 0;
7746 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7747 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7748 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7749 c591440f 2021-11-20 thomas if (err)
7750 c591440f 2021-11-20 thomas break;
7751 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7752 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7753 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7754 3bfadbd4 2021-11-20 thomas if (err)
7755 3bfadbd4 2021-11-20 thomas break;
7756 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7757 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7758 6458efa5 2020-11-24 stsp break;
7759 6458efa5 2020-11-24 stsp case KEY_ENTER:
7760 6458efa5 2020-11-24 stsp case '\r':
7761 07b0611c 2022-06-23 thomas view->count = 0;
7762 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7763 6458efa5 2020-11-24 stsp break;
7764 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7765 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7766 a5d43cac 2022-07-01 thomas
7767 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7768 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7769 a5d43cac 2022-07-01 thomas if (err)
7770 a5d43cac 2022-07-01 thomas break;
7771 a5d43cac 2022-07-01 thomas
7772 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7773 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7774 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7775 a5d43cac 2022-07-01 thomas if (err)
7776 a5d43cac 2022-07-01 thomas break;
7777 a5d43cac 2022-07-01 thomas }
7778 a5d43cac 2022-07-01 thomas
7779 e78dc838 2020-12-04 stsp view->focussed = 0;
7780 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7781 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7782 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7783 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7784 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7785 6458efa5 2020-11-24 stsp err = view_close_child(view);
7786 6458efa5 2020-11-24 stsp if (err)
7787 6458efa5 2020-11-24 stsp return err;
7788 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7789 40236d76 2022-06-23 thomas if (err)
7790 40236d76 2022-06-23 thomas return err;
7791 e78dc838 2020-12-04 stsp view->focus_child = 1;
7792 6458efa5 2020-11-24 stsp } else
7793 6458efa5 2020-11-24 stsp *new_view = log_view;
7794 6458efa5 2020-11-24 stsp break;
7795 c42c9805 2020-11-24 stsp case 't':
7796 07b0611c 2022-06-23 thomas view->count = 0;
7797 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7798 c42c9805 2020-11-24 stsp break;
7799 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7800 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7801 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7802 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7803 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7804 c42c9805 2020-11-24 stsp break;
7805 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7806 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7807 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7808 444d5325 2022-07-03 thomas if (err)
7809 444d5325 2022-07-03 thomas break;
7810 444d5325 2022-07-03 thomas }
7811 e78dc838 2020-12-04 stsp view->focussed = 0;
7812 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7813 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7814 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7815 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7816 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7817 c42c9805 2020-11-24 stsp err = view_close_child(view);
7818 c42c9805 2020-11-24 stsp if (err)
7819 c42c9805 2020-11-24 stsp return err;
7820 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7821 40236d76 2022-06-23 thomas if (err)
7822 40236d76 2022-06-23 thomas return err;
7823 e78dc838 2020-12-04 stsp view->focus_child = 1;
7824 c42c9805 2020-11-24 stsp } else
7825 c42c9805 2020-11-24 stsp *new_view = tree_view;
7826 c42c9805 2020-11-24 stsp break;
7827 e4526bf5 2021-09-03 naddy case 'g':
7828 e4526bf5 2021-09-03 naddy case KEY_HOME:
7829 e4526bf5 2021-09-03 naddy s->selected = 0;
7830 07b0611c 2022-06-23 thomas view->count = 0;
7831 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7832 e4526bf5 2021-09-03 naddy break;
7833 e4526bf5 2021-09-03 naddy case 'G':
7834 a5d43cac 2022-07-01 thomas case KEY_END: {
7835 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7836 a5d43cac 2022-07-01 thomas
7837 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7838 a5d43cac 2022-07-01 thomas --eos; /* border */
7839 e4526bf5 2021-09-03 naddy s->selected = 0;
7840 07b0611c 2022-06-23 thomas view->count = 0;
7841 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7842 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7843 e4526bf5 2021-09-03 naddy if (re == NULL)
7844 e4526bf5 2021-09-03 naddy break;
7845 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7846 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7847 e4526bf5 2021-09-03 naddy }
7848 e4526bf5 2021-09-03 naddy if (n > 0)
7849 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7850 e4526bf5 2021-09-03 naddy break;
7851 a5d43cac 2022-07-01 thomas }
7852 6458efa5 2020-11-24 stsp case 'k':
7853 6458efa5 2020-11-24 stsp case KEY_UP:
7854 f7140bf5 2021-10-17 thomas case CTRL('p'):
7855 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7856 6458efa5 2020-11-24 stsp s->selected--;
7857 6458efa5 2020-11-24 stsp break;
7858 34ba6917 2020-11-30 stsp }
7859 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7860 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7861 07b0611c 2022-06-23 thomas view->count = 0;
7862 6458efa5 2020-11-24 stsp break;
7863 70f17a53 2022-06-13 thomas case CTRL('u'):
7864 23427b14 2022-06-23 thomas case 'u':
7865 70f17a53 2022-06-13 thomas nscroll /= 2;
7866 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7867 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7868 6458efa5 2020-11-24 stsp case CTRL('b'):
7869 1c5e5faa 2022-06-23 thomas case 'b':
7870 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7871 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7872 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7873 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7874 07b0611c 2022-06-23 thomas view->count = 0;
7875 6458efa5 2020-11-24 stsp break;
7876 6458efa5 2020-11-24 stsp case 'j':
7877 6458efa5 2020-11-24 stsp case KEY_DOWN:
7878 f7140bf5 2021-10-17 thomas case CTRL('n'):
7879 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7880 6458efa5 2020-11-24 stsp s->selected++;
7881 6458efa5 2020-11-24 stsp break;
7882 6458efa5 2020-11-24 stsp }
7883 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7884 6458efa5 2020-11-24 stsp /* can't scroll any further */
7885 07b0611c 2022-06-23 thomas view->count = 0;
7886 6458efa5 2020-11-24 stsp break;
7887 07b0611c 2022-06-23 thomas }
7888 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7889 6458efa5 2020-11-24 stsp break;
7890 70f17a53 2022-06-13 thomas case CTRL('d'):
7891 23427b14 2022-06-23 thomas case 'd':
7892 70f17a53 2022-06-13 thomas nscroll /= 2;
7893 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7894 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7895 6458efa5 2020-11-24 stsp case CTRL('f'):
7896 1c5e5faa 2022-06-23 thomas case 'f':
7897 4c2d69cb 2022-06-23 thomas case ' ':
7898 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7899 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7900 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7901 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7902 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7903 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7904 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7905 07b0611c 2022-06-23 thomas view->count = 0;
7906 6458efa5 2020-11-24 stsp break;
7907 6458efa5 2020-11-24 stsp }
7908 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7909 6458efa5 2020-11-24 stsp break;
7910 6458efa5 2020-11-24 stsp case CTRL('l'):
7911 07b0611c 2022-06-23 thomas view->count = 0;
7912 8924d611 2020-12-26 stsp tog_free_refs();
7913 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7914 8924d611 2020-12-26 stsp if (err)
7915 8924d611 2020-12-26 stsp break;
7916 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7917 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7918 6458efa5 2020-11-24 stsp break;
7919 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7920 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7921 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7922 6458efa5 2020-11-24 stsp break;
7923 6458efa5 2020-11-24 stsp default:
7924 07b0611c 2022-06-23 thomas view->count = 0;
7925 6458efa5 2020-11-24 stsp break;
7926 6458efa5 2020-11-24 stsp }
7927 6458efa5 2020-11-24 stsp
7928 6458efa5 2020-11-24 stsp return err;
7929 6458efa5 2020-11-24 stsp }
7930 6458efa5 2020-11-24 stsp
7931 6458efa5 2020-11-24 stsp __dead static void
7932 6458efa5 2020-11-24 stsp usage_ref(void)
7933 6458efa5 2020-11-24 stsp {
7934 6458efa5 2020-11-24 stsp endwin();
7935 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7936 6458efa5 2020-11-24 stsp getprogname());
7937 6458efa5 2020-11-24 stsp exit(1);
7938 6458efa5 2020-11-24 stsp }
7939 6458efa5 2020-11-24 stsp
7940 6458efa5 2020-11-24 stsp static const struct got_error *
7941 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7942 6458efa5 2020-11-24 stsp {
7943 6458efa5 2020-11-24 stsp const struct got_error *error;
7944 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7945 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7946 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7947 6458efa5 2020-11-24 stsp int ch;
7948 6458efa5 2020-11-24 stsp struct tog_view *view;
7949 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7950 6458efa5 2020-11-24 stsp
7951 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7952 6458efa5 2020-11-24 stsp switch (ch) {
7953 6458efa5 2020-11-24 stsp case 'r':
7954 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7955 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7956 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7957 6458efa5 2020-11-24 stsp optarg);
7958 6458efa5 2020-11-24 stsp break;
7959 6458efa5 2020-11-24 stsp default:
7960 e99e2d15 2020-11-24 naddy usage_ref();
7961 6458efa5 2020-11-24 stsp /* NOTREACHED */
7962 6458efa5 2020-11-24 stsp }
7963 6458efa5 2020-11-24 stsp }
7964 6458efa5 2020-11-24 stsp
7965 6458efa5 2020-11-24 stsp argc -= optind;
7966 6458efa5 2020-11-24 stsp argv += optind;
7967 6458efa5 2020-11-24 stsp
7968 6458efa5 2020-11-24 stsp if (argc > 1)
7969 e99e2d15 2020-11-24 naddy usage_ref();
7970 7cd52833 2022-06-23 thomas
7971 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7972 7cd52833 2022-06-23 thomas if (error != NULL)
7973 7cd52833 2022-06-23 thomas goto done;
7974 6458efa5 2020-11-24 stsp
7975 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7976 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7977 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7978 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7979 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7980 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7981 c156c7a4 2020-12-18 stsp goto done;
7982 6458efa5 2020-11-24 stsp if (worktree)
7983 6458efa5 2020-11-24 stsp repo_path =
7984 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7985 6458efa5 2020-11-24 stsp else
7986 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7987 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7988 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7989 c156c7a4 2020-12-18 stsp goto done;
7990 c156c7a4 2020-12-18 stsp }
7991 6458efa5 2020-11-24 stsp }
7992 6458efa5 2020-11-24 stsp
7993 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7994 6458efa5 2020-11-24 stsp if (error != NULL)
7995 6458efa5 2020-11-24 stsp goto done;
7996 6458efa5 2020-11-24 stsp
7997 6458efa5 2020-11-24 stsp init_curses();
7998 6458efa5 2020-11-24 stsp
7999 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8000 51a10b52 2020-12-26 stsp if (error)
8001 51a10b52 2020-12-26 stsp goto done;
8002 51a10b52 2020-12-26 stsp
8003 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8004 6458efa5 2020-11-24 stsp if (error)
8005 6458efa5 2020-11-24 stsp goto done;
8006 6458efa5 2020-11-24 stsp
8007 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8008 6458efa5 2020-11-24 stsp if (view == NULL) {
8009 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8010 6458efa5 2020-11-24 stsp goto done;
8011 6458efa5 2020-11-24 stsp }
8012 6458efa5 2020-11-24 stsp
8013 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8014 6458efa5 2020-11-24 stsp if (error)
8015 6458efa5 2020-11-24 stsp goto done;
8016 6458efa5 2020-11-24 stsp
8017 6458efa5 2020-11-24 stsp if (worktree) {
8018 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8019 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8020 6458efa5 2020-11-24 stsp worktree = NULL;
8021 6458efa5 2020-11-24 stsp }
8022 6458efa5 2020-11-24 stsp error = view_loop(view);
8023 6458efa5 2020-11-24 stsp done:
8024 6458efa5 2020-11-24 stsp free(repo_path);
8025 6458efa5 2020-11-24 stsp free(cwd);
8026 1d0f4054 2021-06-17 stsp if (repo) {
8027 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8028 1d0f4054 2021-06-17 stsp if (close_err)
8029 1d0f4054 2021-06-17 stsp error = close_err;
8030 1d0f4054 2021-06-17 stsp }
8031 7cd52833 2022-06-23 thomas if (pack_fds) {
8032 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8033 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8034 7cd52833 2022-06-23 thomas if (error == NULL)
8035 7cd52833 2022-06-23 thomas error = pack_err;
8036 7cd52833 2022-06-23 thomas }
8037 51a10b52 2020-12-26 stsp tog_free_refs();
8038 6458efa5 2020-11-24 stsp return error;
8039 6458efa5 2020-11-24 stsp }
8040 6458efa5 2020-11-24 stsp
8041 a5d43cac 2022-07-01 thomas /*
8042 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8043 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8044 a5d43cac 2022-07-01 thomas */
8045 6458efa5 2020-11-24 stsp static void
8046 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8047 a5d43cac 2022-07-01 thomas {
8048 a5d43cac 2022-07-01 thomas switch (view->type) {
8049 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8050 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8051 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8052 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8053 a5d43cac 2022-07-01 thomas 1);
8054 a5d43cac 2022-07-01 thomas break;
8055 a5d43cac 2022-07-01 thomas }
8056 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8057 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8058 a5d43cac 2022-07-01 thomas else
8059 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8060 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8061 a5d43cac 2022-07-01 thomas break;
8062 a5d43cac 2022-07-01 thomas }
8063 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8064 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8065 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8066 a5d43cac 2022-07-01 thomas break;
8067 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8068 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8069 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8070 a5d43cac 2022-07-01 thomas break;
8071 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8072 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8073 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8074 a5d43cac 2022-07-01 thomas break;
8075 a5d43cac 2022-07-01 thomas default:
8076 a5d43cac 2022-07-01 thomas break;
8077 a5d43cac 2022-07-01 thomas }
8078 a5d43cac 2022-07-01 thomas
8079 a5d43cac 2022-07-01 thomas view->offset = 0;
8080 a5d43cac 2022-07-01 thomas }
8081 a5d43cac 2022-07-01 thomas
8082 a5d43cac 2022-07-01 thomas /*
8083 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8084 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8085 a5d43cac 2022-07-01 thomas */
8086 a5d43cac 2022-07-01 thomas static const struct got_error *
8087 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8088 a5d43cac 2022-07-01 thomas {
8089 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8090 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8091 a5d43cac 2022-07-01 thomas int *selected = NULL;
8092 a5d43cac 2022-07-01 thomas int header, offset;
8093 a5d43cac 2022-07-01 thomas
8094 a5d43cac 2022-07-01 thomas switch (view->type) {
8095 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8096 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8097 a5d43cac 2022-07-01 thomas header = 3;
8098 a5d43cac 2022-07-01 thomas scrolld = NULL;
8099 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8100 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8101 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8102 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8103 a5d43cac 2022-07-01 thomas view->offset = offset;
8104 a5d43cac 2022-07-01 thomas }
8105 a5d43cac 2022-07-01 thomas break;
8106 a5d43cac 2022-07-01 thomas }
8107 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8108 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8109 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8110 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8111 a5d43cac 2022-07-01 thomas selected = &s->selected;
8112 a5d43cac 2022-07-01 thomas break;
8113 a5d43cac 2022-07-01 thomas }
8114 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8115 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8116 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8117 a5d43cac 2022-07-01 thomas header = 3;
8118 a5d43cac 2022-07-01 thomas selected = &s->selected;
8119 a5d43cac 2022-07-01 thomas break;
8120 a5d43cac 2022-07-01 thomas }
8121 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8122 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8123 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8124 a5d43cac 2022-07-01 thomas header = 5;
8125 a5d43cac 2022-07-01 thomas selected = &s->selected;
8126 a5d43cac 2022-07-01 thomas break;
8127 a5d43cac 2022-07-01 thomas }
8128 a5d43cac 2022-07-01 thomas default:
8129 a5d43cac 2022-07-01 thomas selected = NULL;
8130 a5d43cac 2022-07-01 thomas scrolld = NULL;
8131 a5d43cac 2022-07-01 thomas header = 0;
8132 a5d43cac 2022-07-01 thomas break;
8133 a5d43cac 2022-07-01 thomas }
8134 a5d43cac 2022-07-01 thomas
8135 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8136 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8137 a5d43cac 2022-07-01 thomas view->offset = offset;
8138 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8139 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8140 a5d43cac 2022-07-01 thomas *selected -= offset;
8141 a5d43cac 2022-07-01 thomas }
8142 a5d43cac 2022-07-01 thomas }
8143 a5d43cac 2022-07-01 thomas
8144 a5d43cac 2022-07-01 thomas return err;
8145 a5d43cac 2022-07-01 thomas }
8146 a5d43cac 2022-07-01 thomas
8147 a5d43cac 2022-07-01 thomas static void
8148 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8149 ce5b7c56 2019-07-09 stsp {
8150 6059809a 2020-12-17 stsp size_t i;
8151 9f7d7167 2018-04-29 stsp
8152 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8153 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8154 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8155 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8156 ce5b7c56 2019-07-09 stsp }
8157 6879ba42 2020-10-01 naddy fputc('\n', fp);
8158 ce5b7c56 2019-07-09 stsp }
8159 ce5b7c56 2019-07-09 stsp
8160 4ed7e80c 2018-05-20 stsp __dead static void
8161 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8162 9f7d7167 2018-04-29 stsp {
8163 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8164 6879ba42 2020-10-01 naddy
8165 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8166 6879ba42 2020-10-01 naddy getprogname());
8167 6879ba42 2020-10-01 naddy if (hflag) {
8168 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8169 6879ba42 2020-10-01 naddy list_commands(fp);
8170 ee85c5e8 2020-02-29 stsp }
8171 6879ba42 2020-10-01 naddy exit(status);
8172 9f7d7167 2018-04-29 stsp }
8173 9f7d7167 2018-04-29 stsp
8174 c2301be8 2018-04-30 stsp static char **
8175 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8176 c2301be8 2018-04-30 stsp {
8177 ee85c5e8 2020-02-29 stsp va_list ap;
8178 c2301be8 2018-04-30 stsp char **argv;
8179 ee85c5e8 2020-02-29 stsp int i;
8180 c2301be8 2018-04-30 stsp
8181 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8182 ee85c5e8 2020-02-29 stsp
8183 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8184 c2301be8 2018-04-30 stsp if (argv == NULL)
8185 c2301be8 2018-04-30 stsp err(1, "calloc");
8186 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8187 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8188 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8189 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8190 c2301be8 2018-04-30 stsp }
8191 c2301be8 2018-04-30 stsp
8192 ee85c5e8 2020-02-29 stsp va_end(ap);
8193 c2301be8 2018-04-30 stsp return argv;
8194 ee85c5e8 2020-02-29 stsp }
8195 ee85c5e8 2020-02-29 stsp
8196 ee85c5e8 2020-02-29 stsp /*
8197 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8198 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8199 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8200 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8201 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8202 ee85c5e8 2020-02-29 stsp */
8203 ee85c5e8 2020-02-29 stsp static const struct got_error *
8204 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8205 ee85c5e8 2020-02-29 stsp {
8206 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8207 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8208 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8209 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8210 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8211 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8212 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8213 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8214 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8215 84de9106 2020-12-26 stsp
8216 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8217 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8218 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8219 ee85c5e8 2020-02-29 stsp
8220 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8221 7cd52833 2022-06-23 thomas if (error != NULL)
8222 7cd52833 2022-06-23 thomas goto done;
8223 7cd52833 2022-06-23 thomas
8224 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8225 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8226 ee85c5e8 2020-02-29 stsp goto done;
8227 ee85c5e8 2020-02-29 stsp
8228 ee85c5e8 2020-02-29 stsp if (worktree)
8229 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8230 ee85c5e8 2020-02-29 stsp else
8231 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8232 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8233 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8234 ee85c5e8 2020-02-29 stsp goto done;
8235 ee85c5e8 2020-02-29 stsp }
8236 ee85c5e8 2020-02-29 stsp
8237 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8238 ee85c5e8 2020-02-29 stsp if (error != NULL)
8239 ee85c5e8 2020-02-29 stsp goto done;
8240 ee85c5e8 2020-02-29 stsp
8241 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8242 ee85c5e8 2020-02-29 stsp repo, worktree);
8243 ee85c5e8 2020-02-29 stsp if (error)
8244 ee85c5e8 2020-02-29 stsp goto done;
8245 ee85c5e8 2020-02-29 stsp
8246 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8247 84de9106 2020-12-26 stsp if (error)
8248 84de9106 2020-12-26 stsp goto done;
8249 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8250 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8251 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8252 ee85c5e8 2020-02-29 stsp if (error)
8253 ee85c5e8 2020-02-29 stsp goto done;
8254 ee85c5e8 2020-02-29 stsp
8255 ee85c5e8 2020-02-29 stsp if (worktree) {
8256 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8257 ee85c5e8 2020-02-29 stsp worktree = NULL;
8258 ee85c5e8 2020-02-29 stsp }
8259 ee85c5e8 2020-02-29 stsp
8260 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8261 945f9229 2022-04-16 thomas if (error)
8262 945f9229 2022-04-16 thomas goto done;
8263 945f9229 2022-04-16 thomas
8264 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8265 ee85c5e8 2020-02-29 stsp if (error) {
8266 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8267 ee85c5e8 2020-02-29 stsp goto done;
8268 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8269 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8270 6879ba42 2020-10-01 naddy usage(1, 1);
8271 ee85c5e8 2020-02-29 stsp /* not reached */
8272 ee85c5e8 2020-02-29 stsp }
8273 ee85c5e8 2020-02-29 stsp
8274 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8275 1d0f4054 2021-06-17 stsp if (error == NULL)
8276 1d0f4054 2021-06-17 stsp error = close_err;
8277 ee85c5e8 2020-02-29 stsp repo = NULL;
8278 ee85c5e8 2020-02-29 stsp
8279 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8280 ee85c5e8 2020-02-29 stsp if (error)
8281 ee85c5e8 2020-02-29 stsp goto done;
8282 ee85c5e8 2020-02-29 stsp
8283 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8284 ee85c5e8 2020-02-29 stsp argc = 4;
8285 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8286 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8287 ee85c5e8 2020-02-29 stsp done:
8288 1d0f4054 2021-06-17 stsp if (repo) {
8289 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8290 1d0f4054 2021-06-17 stsp if (error == NULL)
8291 1d0f4054 2021-06-17 stsp error = close_err;
8292 1d0f4054 2021-06-17 stsp }
8293 945f9229 2022-04-16 thomas if (commit)
8294 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8295 ee85c5e8 2020-02-29 stsp if (worktree)
8296 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8297 7cd52833 2022-06-23 thomas if (pack_fds) {
8298 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8299 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8300 7cd52833 2022-06-23 thomas if (error == NULL)
8301 7cd52833 2022-06-23 thomas error = pack_err;
8302 7cd52833 2022-06-23 thomas }
8303 ee85c5e8 2020-02-29 stsp free(id);
8304 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8305 ee85c5e8 2020-02-29 stsp free(commit_id);
8306 ee85c5e8 2020-02-29 stsp free(cwd);
8307 ee85c5e8 2020-02-29 stsp free(repo_path);
8308 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8309 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8310 ee85c5e8 2020-02-29 stsp int i;
8311 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8312 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8313 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8314 ee85c5e8 2020-02-29 stsp }
8315 87670572 2020-12-26 naddy tog_free_refs();
8316 ee85c5e8 2020-02-29 stsp return error;
8317 c2301be8 2018-04-30 stsp }
8318 c2301be8 2018-04-30 stsp
8319 9f7d7167 2018-04-29 stsp int
8320 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8321 9f7d7167 2018-04-29 stsp {
8322 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8323 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8324 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8325 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8326 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8327 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8328 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8329 83cd27f8 2020-01-13 stsp };
8330 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8331 9f7d7167 2018-04-29 stsp
8332 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8333 9f7d7167 2018-04-29 stsp
8334 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8335 9f7d7167 2018-04-29 stsp switch (ch) {
8336 9f7d7167 2018-04-29 stsp case 'h':
8337 9f7d7167 2018-04-29 stsp hflag = 1;
8338 9f7d7167 2018-04-29 stsp break;
8339 53ccebc2 2019-07-30 stsp case 'V':
8340 53ccebc2 2019-07-30 stsp Vflag = 1;
8341 53ccebc2 2019-07-30 stsp break;
8342 9f7d7167 2018-04-29 stsp default:
8343 6879ba42 2020-10-01 naddy usage(hflag, 1);
8344 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8345 9f7d7167 2018-04-29 stsp }
8346 9f7d7167 2018-04-29 stsp }
8347 9f7d7167 2018-04-29 stsp
8348 9f7d7167 2018-04-29 stsp argc -= optind;
8349 9f7d7167 2018-04-29 stsp argv += optind;
8350 9814e6a3 2020-09-27 naddy optind = 1;
8351 c2301be8 2018-04-30 stsp optreset = 1;
8352 9f7d7167 2018-04-29 stsp
8353 53ccebc2 2019-07-30 stsp if (Vflag) {
8354 53ccebc2 2019-07-30 stsp got_version_print_str();
8355 6879ba42 2020-10-01 naddy return 0;
8356 53ccebc2 2019-07-30 stsp }
8357 4010e238 2020-12-04 stsp
8358 4010e238 2020-12-04 stsp #ifndef PROFILE
8359 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8360 4010e238 2020-12-04 stsp NULL) == -1)
8361 4010e238 2020-12-04 stsp err(1, "pledge");
8362 4010e238 2020-12-04 stsp #endif
8363 53ccebc2 2019-07-30 stsp
8364 c2301be8 2018-04-30 stsp if (argc == 0) {
8365 f29d3e89 2018-06-23 stsp if (hflag)
8366 6879ba42 2020-10-01 naddy usage(hflag, 0);
8367 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8368 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8369 c2301be8 2018-04-30 stsp argc = 1;
8370 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8371 c2301be8 2018-04-30 stsp } else {
8372 6059809a 2020-12-17 stsp size_t i;
8373 9f7d7167 2018-04-29 stsp
8374 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8375 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8376 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8377 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8378 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8379 9f7d7167 2018-04-29 stsp break;
8380 9f7d7167 2018-04-29 stsp }
8381 9f7d7167 2018-04-29 stsp }
8382 ee85c5e8 2020-02-29 stsp }
8383 3642c4c6 2019-07-09 stsp
8384 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8385 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8386 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8387 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8388 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8389 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8390 adf4c9e0 2022-07-03 thomas }
8391 adf4c9e0 2022-07-03 thomas
8392 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8393 ee85c5e8 2020-02-29 stsp if (argc != 1)
8394 6879ba42 2020-10-01 naddy usage(0, 1);
8395 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8396 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8397 ee85c5e8 2020-02-29 stsp } else {
8398 ee85c5e8 2020-02-29 stsp if (hflag)
8399 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8400 ee85c5e8 2020-02-29 stsp else
8401 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8402 9f7d7167 2018-04-29 stsp }
8403 9f7d7167 2018-04-29 stsp
8404 9f7d7167 2018-04-29 stsp endwin();
8405 b46c1e04 2020-09-20 naddy putchar('\n');
8406 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8407 a2f4a359 2020-02-28 stsp int i;
8408 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8409 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8410 a2f4a359 2020-02-28 stsp free(cmd_argv);
8411 a2f4a359 2020-02-28 stsp }
8412 a2f4a359 2020-02-28 stsp
8413 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8414 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8415 9f7d7167 2018-04-29 stsp return 0;
8416 9f7d7167 2018-04-29 stsp }