Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
19 80ddbec8 2018-04-29 stsp
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 def2f970 2021-09-28 thomas #endif
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
45 dd038bc6 2021-09-21 thomas.ad
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 9f7d7167 2018-04-29 stsp
61 881b2d3e 2018-04-30 stsp #ifndef MIN
62 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 881b2d3e 2018-04-30 stsp #endif
64 881b2d3e 2018-04-30 stsp
65 2bd27830 2018-10-22 stsp #ifndef MAX
66 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 2bd27830 2018-10-22 stsp #endif
68 2bd27830 2018-10-22 stsp
69 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
70 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
71 acf52a76 2021-09-21 thomas.ad #endif
72 2bd27830 2018-10-22 stsp
73 9f7d7167 2018-04-29 stsp #ifndef nitems
74 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 9f7d7167 2018-04-29 stsp #endif
76 9f7d7167 2018-04-29 stsp
77 9f7d7167 2018-04-29 stsp struct tog_cmd {
78 c2301be8 2018-04-30 stsp const char *name;
79 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
81 9f7d7167 2018-04-29 stsp };
82 9f7d7167 2018-04-29 stsp
83 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
87 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
88 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
89 9f7d7167 2018-04-29 stsp
90 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
93 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
94 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
95 9f7d7167 2018-04-29 stsp
96 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
97 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
98 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
99 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
100 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
101 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
102 9f7d7167 2018-04-29 stsp };
103 9f7d7167 2018-04-29 stsp
104 d6b05b5b 2018-08-04 stsp enum tog_view_type {
105 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
107 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
108 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
109 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
110 d6b05b5b 2018-08-04 stsp };
111 c3e9aa98 2019-05-13 jcs
112 a5d43cac 2022-07-01 thomas enum tog_view_mode {
113 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
116 a5d43cac 2022-07-01 thomas };
117 a5d43cac 2022-07-01 thomas
118 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
119 a5d43cac 2022-07-01 thomas
120 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
121 d6b05b5b 2018-08-04 stsp
122 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
123 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
124 ba4f502b 2018-08-04 stsp struct got_object_id *id;
125 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
126 1a76625f 2018-10-22 stsp int idx;
127 ba4f502b 2018-08-04 stsp };
128 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 ba4f502b 2018-08-04 stsp struct commit_queue {
130 ba4f502b 2018-08-04 stsp int ncommits;
131 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
132 6d17833f 2019-11-08 stsp };
133 6d17833f 2019-11-08 stsp
134 f26dddb7 2019-11-08 stsp struct tog_color {
135 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
136 6d17833f 2019-11-08 stsp regex_t regex;
137 6d17833f 2019-11-08 stsp short colorpair;
138 15a087fe 2019-02-21 stsp };
139 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
140 11b20872 2019-11-08 stsp
141 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
143 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
144 2183bbf6 2022-01-23 thomas
145 2183bbf6 2022-01-23 thomas static const struct got_error *
146 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 2183bbf6 2022-01-23 thomas struct got_reference* re2)
148 2183bbf6 2022-01-23 thomas {
149 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
150 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
151 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
152 2183bbf6 2022-01-23 thomas
153 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
154 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
157 2183bbf6 2022-01-23 thomas *cmp = -1;
158 2183bbf6 2022-01-23 thomas return NULL;
159 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
160 2183bbf6 2022-01-23 thomas *cmp = 1;
161 2183bbf6 2022-01-23 thomas return NULL;
162 2183bbf6 2022-01-23 thomas }
163 2183bbf6 2022-01-23 thomas
164 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
165 2183bbf6 2022-01-23 thomas return NULL;
166 2183bbf6 2022-01-23 thomas }
167 51a10b52 2020-12-26 stsp
168 11b20872 2019-11-08 stsp static const struct got_error *
169 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
170 51a10b52 2020-12-26 stsp {
171 51a10b52 2020-12-26 stsp const struct got_error *err;
172 51a10b52 2020-12-26 stsp
173 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 3bfadbd4 2021-11-20 thomas repo);
176 51a10b52 2020-12-26 stsp if (err)
177 51a10b52 2020-12-26 stsp return err;
178 51a10b52 2020-12-26 stsp
179 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
180 51a10b52 2020-12-26 stsp repo);
181 51a10b52 2020-12-26 stsp }
182 51a10b52 2020-12-26 stsp
183 51a10b52 2020-12-26 stsp static void
184 51a10b52 2020-12-26 stsp tog_free_refs(void)
185 51a10b52 2020-12-26 stsp {
186 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
187 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
188 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
189 51a10b52 2020-12-26 stsp }
190 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
191 51a10b52 2020-12-26 stsp }
192 51a10b52 2020-12-26 stsp
193 51a10b52 2020-12-26 stsp static const struct got_error *
194 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
195 11b20872 2019-11-08 stsp int idx, short color)
196 11b20872 2019-11-08 stsp {
197 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
198 11b20872 2019-11-08 stsp struct tog_color *tc;
199 11b20872 2019-11-08 stsp int regerr = 0;
200 11b20872 2019-11-08 stsp
201 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
202 11b20872 2019-11-08 stsp return NULL;
203 11b20872 2019-11-08 stsp
204 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
205 11b20872 2019-11-08 stsp
206 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
207 11b20872 2019-11-08 stsp if (tc == NULL)
208 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
209 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
210 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
211 11b20872 2019-11-08 stsp if (regerr) {
212 11b20872 2019-11-08 stsp static char regerr_msg[512];
213 11b20872 2019-11-08 stsp static char err_msg[512];
214 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
215 11b20872 2019-11-08 stsp sizeof(regerr_msg));
216 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
217 11b20872 2019-11-08 stsp regerr_msg);
218 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 11b20872 2019-11-08 stsp free(tc);
220 11b20872 2019-11-08 stsp return err;
221 11b20872 2019-11-08 stsp }
222 11b20872 2019-11-08 stsp tc->colorpair = idx;
223 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
224 11b20872 2019-11-08 stsp return NULL;
225 11b20872 2019-11-08 stsp }
226 11b20872 2019-11-08 stsp
227 11b20872 2019-11-08 stsp static void
228 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
229 11b20872 2019-11-08 stsp {
230 11b20872 2019-11-08 stsp struct tog_color *tc;
231 11b20872 2019-11-08 stsp
232 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
233 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
234 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
235 11b20872 2019-11-08 stsp regfree(&tc->regex);
236 11b20872 2019-11-08 stsp free(tc);
237 11b20872 2019-11-08 stsp }
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp
240 ef20f542 2022-06-26 thomas static struct tog_color *
241 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
242 11b20872 2019-11-08 stsp {
243 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
244 11b20872 2019-11-08 stsp
245 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
246 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
247 11b20872 2019-11-08 stsp return tc;
248 11b20872 2019-11-08 stsp }
249 11b20872 2019-11-08 stsp
250 11b20872 2019-11-08 stsp return NULL;
251 11b20872 2019-11-08 stsp }
252 11b20872 2019-11-08 stsp
253 11b20872 2019-11-08 stsp static int
254 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
255 11b20872 2019-11-08 stsp {
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
259 11b20872 2019-11-08 stsp return COLOR_CYAN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
261 11b20872 2019-11-08 stsp return COLOR_YELLOW;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
263 11b20872 2019-11-08 stsp return COLOR_GREEN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
269 91b8c405 2020-01-25 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_GREEN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
273 11b20872 2019-11-08 stsp return COLOR_GREEN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
275 11b20872 2019-11-08 stsp return COLOR_CYAN;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
277 11b20872 2019-11-08 stsp return COLOR_YELLOW;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_GREEN;
280 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
282 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
283 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
284 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
286 11b20872 2019-11-08 stsp
287 11b20872 2019-11-08 stsp return -1;
288 11b20872 2019-11-08 stsp }
289 11b20872 2019-11-08 stsp
290 11b20872 2019-11-08 stsp static int
291 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
292 11b20872 2019-11-08 stsp {
293 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
294 11b20872 2019-11-08 stsp
295 11b20872 2019-11-08 stsp if (val == NULL)
296 11b20872 2019-11-08 stsp return default_color_value(envvar);
297 15a087fe 2019-02-21 stsp
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
299 11b20872 2019-11-08 stsp return COLOR_BLACK;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
301 11b20872 2019-11-08 stsp return COLOR_RED;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
303 11b20872 2019-11-08 stsp return COLOR_GREEN;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
305 11b20872 2019-11-08 stsp return COLOR_YELLOW;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
307 11b20872 2019-11-08 stsp return COLOR_BLUE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
309 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
311 11b20872 2019-11-08 stsp return COLOR_CYAN;
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
313 11b20872 2019-11-08 stsp return COLOR_WHITE;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
315 11b20872 2019-11-08 stsp return -1;
316 11b20872 2019-11-08 stsp
317 11b20872 2019-11-08 stsp return default_color_value(envvar);
318 11b20872 2019-11-08 stsp }
319 11b20872 2019-11-08 stsp
320 11b20872 2019-11-08 stsp
321 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
322 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
323 3dbaef42 2020-11-24 stsp const char *label1, *label2;
324 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
325 19a6a6b5 2022-07-01 thomas int fd1, fd2;
326 15a087fe 2019-02-21 stsp int first_displayed_line;
327 15a087fe 2019-02-21 stsp int last_displayed_line;
328 15a087fe 2019-02-21 stsp int eof;
329 15a087fe 2019-02-21 stsp int diff_context;
330 3dbaef42 2020-11-24 stsp int ignore_whitespace;
331 64453f7e 2020-11-21 stsp int force_text_diff;
332 15a087fe 2019-02-21 stsp struct got_repository *repo;
333 bddb1296 2019-11-08 stsp struct tog_colors colors;
334 fe621944 2020-11-10 stsp size_t nlines;
335 f44b1f58 2020-02-02 tracey off_t *line_offsets;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
338 15a087fe 2019-02-21 stsp
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
341 b01e7d3b 2018-08-04 stsp };
342 b01e7d3b 2018-08-04 stsp
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
345 1a76625f 2018-10-22 stsp
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 1a76625f 2018-10-22 stsp struct commit_queue *commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
364 1a76625f 2018-10-22 stsp };
365 1a76625f 2018-10-22 stsp
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 ba4f502b 2018-08-04 stsp };
384 11b20872 2019-11-08 stsp
385 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
389 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
393 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
394 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
395 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
396 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
399 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
400 ba4f502b 2018-08-04 stsp
401 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
402 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
403 e9424729 2018-08-04 stsp int nlines;
404 e9424729 2018-08-04 stsp
405 e9424729 2018-08-04 stsp struct tog_view *view;
406 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
407 e9424729 2018-08-04 stsp int *quit;
408 e9424729 2018-08-04 stsp };
409 e9424729 2018-08-04 stsp
410 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
411 e9424729 2018-08-04 stsp const char *path;
412 e9424729 2018-08-04 stsp struct got_repository *repo;
413 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
414 e9424729 2018-08-04 stsp int *complete;
415 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
416 fc06ba56 2019-08-22 stsp void *cancel_arg;
417 e9424729 2018-08-04 stsp };
418 e9424729 2018-08-04 stsp
419 e9424729 2018-08-04 stsp struct tog_blame {
420 e9424729 2018-08-04 stsp FILE *f;
421 be659d10 2020-11-18 stsp off_t filesize;
422 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
423 6fcac457 2018-11-19 stsp int nlines;
424 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
425 e9424729 2018-08-04 stsp pthread_t thread;
426 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
427 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
428 e9424729 2018-08-04 stsp const char *path;
429 7cd52833 2022-06-23 thomas int *pack_fds;
430 e9424729 2018-08-04 stsp };
431 e9424729 2018-08-04 stsp
432 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
433 7cbe629d 2018-08-04 stsp int first_displayed_line;
434 7cbe629d 2018-08-04 stsp int last_displayed_line;
435 7cbe629d 2018-08-04 stsp int selected_line;
436 4fc71f3b 2022-07-12 thomas int last_diffed_line;
437 7cbe629d 2018-08-04 stsp int blame_complete;
438 e5a0f69f 2018-08-18 stsp int eof;
439 e5a0f69f 2018-08-18 stsp int done;
440 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
441 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
442 e5a0f69f 2018-08-18 stsp char *path;
443 7cbe629d 2018-08-04 stsp struct got_repository *repo;
444 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
445 e9424729 2018-08-04 stsp struct tog_blame blame;
446 6c4c42e0 2019-06-24 stsp int matched_line;
447 11b20872 2019-11-08 stsp struct tog_colors colors;
448 ad80ab7b 2018-08-04 stsp };
449 ad80ab7b 2018-08-04 stsp
450 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
451 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
452 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
453 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
455 ad80ab7b 2018-08-04 stsp int selected;
456 ad80ab7b 2018-08-04 stsp };
457 ad80ab7b 2018-08-04 stsp
458 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
461 ad80ab7b 2018-08-04 stsp char *tree_label;
462 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
463 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
464 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
465 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
466 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
468 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
469 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
470 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
471 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
472 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
473 6458efa5 2020-11-24 stsp struct tog_colors colors;
474 6458efa5 2020-11-24 stsp };
475 6458efa5 2020-11-24 stsp
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
477 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
478 6458efa5 2020-11-24 stsp struct got_reference *ref;
479 6458efa5 2020-11-24 stsp int idx;
480 6458efa5 2020-11-24 stsp };
481 6458efa5 2020-11-24 stsp
482 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
483 6458efa5 2020-11-24 stsp
484 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
485 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
487 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
489 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
490 6458efa5 2020-11-24 stsp struct got_repository *repo;
491 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
492 bddb1296 2019-11-08 stsp struct tog_colors colors;
493 7cbe629d 2018-08-04 stsp };
494 7cbe629d 2018-08-04 stsp
495 669b5ffa 2018-10-07 stsp /*
496 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
497 669b5ffa 2018-10-07 stsp *
498 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
499 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
500 669b5ffa 2018-10-07 stsp * there is enough screen estate.
501 669b5ffa 2018-10-07 stsp *
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
504 669b5ffa 2018-10-07 stsp *
505 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
506 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
507 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
508 669b5ffa 2018-10-07 stsp *
509 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
510 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
511 669b5ffa 2018-10-07 stsp */
512 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
513 669b5ffa 2018-10-07 stsp
514 cc3c9aac 2018-08-01 stsp struct tog_view {
515 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
516 26ed57b2 2018-05-19 stsp WINDOW *window;
517 26ed57b2 2018-05-19 stsp PANEL *panel;
518 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
519 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
520 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
521 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
522 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
523 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
524 53d2bdd3 2022-07-10 thomas int resize; /* set when in a resize event */
525 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
526 9970f7fc 2020-12-03 stsp int dying;
527 669b5ffa 2018-10-07 stsp struct tog_view *parent;
528 669b5ffa 2018-10-07 stsp struct tog_view *child;
529 5dc9f4bc 2018-08-04 stsp
530 e78dc838 2020-12-04 stsp /*
531 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
532 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
533 e78dc838 2020-12-04 stsp * between parent and child.
534 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
535 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
536 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
537 e78dc838 2020-12-04 stsp * situations.
538 e78dc838 2020-12-04 stsp */
539 e78dc838 2020-12-04 stsp int focus_child;
540 e78dc838 2020-12-04 stsp
541 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
542 5dc9f4bc 2018-08-04 stsp /* type-specific state */
543 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
544 5dc9f4bc 2018-08-04 stsp union {
545 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
546 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
547 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
548 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
549 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
550 5dc9f4bc 2018-08-04 stsp } state;
551 e5a0f69f 2018-08-18 stsp
552 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
553 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
554 e78dc838 2020-12-04 stsp struct tog_view *, int);
555 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
557 60493ae3 2019-06-20 stsp
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
559 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
560 c0c4acc8 2021-01-24 stsp int search_started;
561 60493ae3 2019-06-20 stsp int searching;
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
563 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
564 60493ae3 2019-06-20 stsp int search_next_done;
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
566 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
567 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
568 1803e47f 2019-06-22 stsp regex_t regex;
569 41605754 2020-11-12 stsp regmatch_t regmatch;
570 cc3c9aac 2018-08-01 stsp };
571 cd0acaa7 2018-05-20 stsp
572 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
573 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
574 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
575 78756c87 2020-11-24 stsp struct got_repository *);
576 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
578 e78dc838 2020-12-04 stsp struct tog_view *, int);
579 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
582 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp
584 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
585 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
586 78756c87 2020-11-24 stsp const char *, const char *, int);
587 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
589 e78dc838 2020-12-04 stsp struct tog_view *, int);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
592 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp
594 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
595 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
596 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
598 e78dc838 2020-12-04 stsp struct tog_view *, int);
599 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
602 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
603 e5a0f69f 2018-08-18 stsp
604 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
605 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
606 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
608 e78dc838 2020-12-04 stsp struct tog_view *, int);
609 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
611 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp
613 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
614 6458efa5 2020-11-24 stsp struct got_repository *);
615 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
617 e78dc838 2020-12-04 stsp struct tog_view *, int);
618 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
621 25791caa 2018-10-24 stsp
622 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
623 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
624 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
625 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
626 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
627 25791caa 2018-10-24 stsp
628 25791caa 2018-10-24 stsp static void
629 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
630 25791caa 2018-10-24 stsp {
631 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
632 83baff54 2019-08-12 stsp }
633 83baff54 2019-08-12 stsp
634 83baff54 2019-08-12 stsp static void
635 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
636 83baff54 2019-08-12 stsp {
637 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
638 25791caa 2018-10-24 stsp }
639 26ed57b2 2018-05-19 stsp
640 61266923 2020-01-14 stsp static void
641 61266923 2020-01-14 stsp tog_sigcont(int signo)
642 61266923 2020-01-14 stsp {
643 61266923 2020-01-14 stsp tog_sigcont_received = 1;
644 61266923 2020-01-14 stsp }
645 61266923 2020-01-14 stsp
646 296152d1 2022-05-31 thomas static void
647 296152d1 2022-05-31 thomas tog_sigint(int signo)
648 296152d1 2022-05-31 thomas {
649 296152d1 2022-05-31 thomas tog_sigint_received = 1;
650 296152d1 2022-05-31 thomas }
651 296152d1 2022-05-31 thomas
652 296152d1 2022-05-31 thomas static void
653 296152d1 2022-05-31 thomas tog_sigterm(int signo)
654 296152d1 2022-05-31 thomas {
655 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
656 296152d1 2022-05-31 thomas }
657 296152d1 2022-05-31 thomas
658 296152d1 2022-05-31 thomas static int
659 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
660 296152d1 2022-05-31 thomas {
661 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
662 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
663 296152d1 2022-05-31 thomas }
664 296152d1 2022-05-31 thomas
665 e5a0f69f 2018-08-18 stsp static const struct got_error *
666 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
667 ea5e7bb5 2018-08-01 stsp {
668 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
669 e5a0f69f 2018-08-18 stsp
670 669b5ffa 2018-10-07 stsp if (view->child) {
671 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
672 669b5ffa 2018-10-07 stsp view->child = NULL;
673 669b5ffa 2018-10-07 stsp }
674 e5a0f69f 2018-08-18 stsp if (view->close)
675 e5a0f69f 2018-08-18 stsp err = view->close(view);
676 ea5e7bb5 2018-08-01 stsp if (view->panel)
677 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
678 ea5e7bb5 2018-08-01 stsp if (view->window)
679 ea5e7bb5 2018-08-01 stsp delwin(view->window);
680 ea5e7bb5 2018-08-01 stsp free(view);
681 f2d749db 2022-07-12 thomas return err ? err : child_err;
682 ea5e7bb5 2018-08-01 stsp }
683 ea5e7bb5 2018-08-01 stsp
684 ea5e7bb5 2018-08-01 stsp static struct tog_view *
685 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
686 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
687 ea5e7bb5 2018-08-01 stsp {
688 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
689 ea5e7bb5 2018-08-01 stsp
690 ea5e7bb5 2018-08-01 stsp if (view == NULL)
691 ea5e7bb5 2018-08-01 stsp return NULL;
692 ea5e7bb5 2018-08-01 stsp
693 d6b05b5b 2018-08-04 stsp view->type = type;
694 f7d12f7e 2018-08-01 stsp view->lines = LINES;
695 f7d12f7e 2018-08-01 stsp view->cols = COLS;
696 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
697 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
698 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
699 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
700 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
701 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
702 96a765a8 2018-08-04 stsp view_close(view);
703 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp }
705 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
706 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
707 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
708 96a765a8 2018-08-04 stsp view_close(view);
709 ea5e7bb5 2018-08-01 stsp return NULL;
710 ea5e7bb5 2018-08-01 stsp }
711 ea5e7bb5 2018-08-01 stsp
712 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
713 ea5e7bb5 2018-08-01 stsp return view;
714 cdf1ee82 2018-08-01 stsp }
715 cdf1ee82 2018-08-01 stsp
716 0cf4efb1 2018-09-29 stsp static int
717 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
718 0cf4efb1 2018-09-29 stsp {
719 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
720 0cf4efb1 2018-09-29 stsp return 0;
721 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
722 5c60c32a 2018-10-18 stsp }
723 5c60c32a 2018-10-18 stsp
724 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
725 a5d43cac 2022-07-01 thomas static int
726 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
727 a5d43cac 2022-07-01 thomas {
728 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
729 a5d43cac 2022-07-01 thomas }
730 a5d43cac 2022-07-01 thomas
731 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
732 5c60c32a 2018-10-18 stsp
733 5c60c32a 2018-10-18 stsp static const struct got_error *
734 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
735 5c60c32a 2018-10-18 stsp {
736 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
737 5c60c32a 2018-10-18 stsp
738 53d2bdd3 2022-07-10 thomas if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
740 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
741 53d2bdd3 2022-07-10 thomas else
742 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
743 a5d43cac 2022-07-01 thomas view->begin_x = 0;
744 53d2bdd3 2022-07-10 thomas } else if (!view->resize) {
745 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
746 53d2bdd3 2022-07-10 thomas view->cols > 119)
747 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
748 53d2bdd3 2022-07-10 thomas else
749 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
750 a5d43cac 2022-07-01 thomas view->begin_y = 0;
751 a5d43cac 2022-07-01 thomas }
752 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
753 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
754 5c60c32a 2018-10-18 stsp view->lines = LINES;
755 5c60c32a 2018-10-18 stsp view->cols = COLS;
756 5c60c32a 2018-10-18 stsp err = view_resize(view);
757 5c60c32a 2018-10-18 stsp if (err)
758 5c60c32a 2018-10-18 stsp return err;
759 5c60c32a 2018-10-18 stsp
760 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
762 a5d43cac 2022-07-01 thomas
763 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
765 5c60c32a 2018-10-18 stsp
766 5c60c32a 2018-10-18 stsp return NULL;
767 5c60c32a 2018-10-18 stsp }
768 5c60c32a 2018-10-18 stsp
769 5c60c32a 2018-10-18 stsp static const struct got_error *
770 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
771 5c60c32a 2018-10-18 stsp {
772 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp view->begin_x = 0;
775 53d2bdd3 2022-07-10 thomas view->begin_y = view->resize ? view->begin_y : 0;
776 53d2bdd3 2022-07-10 thomas view->nlines = view->resize ? view->nlines : LINES;
777 5c60c32a 2018-10-18 stsp view->ncols = COLS;
778 5c60c32a 2018-10-18 stsp view->lines = LINES;
779 5c60c32a 2018-10-18 stsp view->cols = COLS;
780 5c60c32a 2018-10-18 stsp err = view_resize(view);
781 5c60c32a 2018-10-18 stsp if (err)
782 5c60c32a 2018-10-18 stsp return err;
783 5c60c32a 2018-10-18 stsp
784 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
786 5c60c32a 2018-10-18 stsp
787 5c60c32a 2018-10-18 stsp return NULL;
788 0cf4efb1 2018-09-29 stsp }
789 0cf4efb1 2018-09-29 stsp
790 5c60c32a 2018-10-18 stsp static int
791 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
792 5c60c32a 2018-10-18 stsp {
793 5c60c32a 2018-10-18 stsp return view->parent == NULL;
794 5c60c32a 2018-10-18 stsp }
795 5c60c32a 2018-10-18 stsp
796 b65b3ea0 2022-06-23 thomas static int
797 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
798 b65b3ea0 2022-06-23 thomas {
799 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
800 e44940c3 2022-07-01 thomas }
801 e44940c3 2022-07-01 thomas
802 e44940c3 2022-07-01 thomas static int
803 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
804 e44940c3 2022-07-01 thomas {
805 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
806 b65b3ea0 2022-06-23 thomas }
807 b65b3ea0 2022-06-23 thomas
808 444d5325 2022-07-03 thomas static int
809 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
810 444d5325 2022-07-03 thomas {
811 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
813 444d5325 2022-07-03 thomas }
814 444d5325 2022-07-03 thomas
815 a5d43cac 2022-07-01 thomas static void
816 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
817 a5d43cac 2022-07-01 thomas {
818 a5d43cac 2022-07-01 thomas PANEL *panel;
819 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
820 b65b3ea0 2022-06-23 thomas
821 a5d43cac 2022-07-01 thomas if (view->parent)
822 a5d43cac 2022-07-01 thomas return view_border(view->parent);
823 a5d43cac 2022-07-01 thomas
824 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
825 a5d43cac 2022-07-01 thomas if (panel == NULL)
826 a5d43cac 2022-07-01 thomas return;
827 a5d43cac 2022-07-01 thomas
828 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
829 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
831 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
832 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
833 a5d43cac 2022-07-01 thomas else
834 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
836 a5d43cac 2022-07-01 thomas }
837 a5d43cac 2022-07-01 thomas
838 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
840 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
841 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
842 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
843 a5d43cac 2022-07-01 thomas
844 4d8c2215 2018-08-19 stsp static const struct got_error *
845 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
846 f7d12f7e 2018-08-01 stsp {
847 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
848 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
849 f7d12f7e 2018-08-01 stsp
850 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
851 a5d43cac 2022-07-01 thomas
852 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
853 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
854 0cf4efb1 2018-09-29 stsp else
855 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
856 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
857 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
858 0cf4efb1 2018-09-29 stsp else
859 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
860 6d0fee91 2018-08-01 stsp
861 4918811f 2022-07-01 thomas if (view->child) {
862 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
863 a5d43cac 2022-07-01 thomas
864 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
865 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
866 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
868 40236d76 2022-06-23 thomas ncols = COLS;
869 40236d76 2022-06-23 thomas
870 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
871 5c60c32a 2018-10-18 stsp if (view->child->focussed)
872 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
873 5c60c32a 2018-10-18 stsp else
874 5c60c32a 2018-10-18 stsp show_panel(view->panel);
875 5c60c32a 2018-10-18 stsp } else {
876 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
877 40236d76 2022-06-23 thomas
878 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
879 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
880 5c60c32a 2018-10-18 stsp }
881 a5d43cac 2022-07-01 thomas /*
882 a5d43cac 2022-07-01 thomas * Request commits if terminal height was increased in a log
883 a5d43cac 2022-07-01 thomas * view so we have enough commits loaded to populate the view.
884 a5d43cac 2022-07-01 thomas */
885 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG && dif > 0) {
886 a5d43cac 2022-07-01 thomas struct tog_log_view_state *ts = &view->state.log;
887 a5d43cac 2022-07-01 thomas
888 a5d43cac 2022-07-01 thomas if (ts->commits.ncommits < ts->selected_entry->idx +
889 a5d43cac 2022-07-01 thomas view->lines - ts->selected) {
890 a5d43cac 2022-07-01 thomas view->nscrolled = ts->selected_entry->idx +
891 a5d43cac 2022-07-01 thomas view->lines - ts->selected -
892 a5d43cac 2022-07-01 thomas ts->commits.ncommits + dif;
893 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
894 a5d43cac 2022-07-01 thomas if (err)
895 a5d43cac 2022-07-01 thomas return err;
896 a5d43cac 2022-07-01 thomas }
897 a5d43cac 2022-07-01 thomas }
898 a5d43cac 2022-07-01 thomas
899 a5d43cac 2022-07-01 thomas /*
900 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
901 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
902 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
903 a5d43cac 2022-07-01 thomas */
904 a5d43cac 2022-07-01 thomas if (hs) {
905 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
906 a5d43cac 2022-07-01 thomas if (err)
907 a5d43cac 2022-07-01 thomas return err;
908 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
909 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
910 a5d43cac 2022-07-01 thomas if (err)
911 a5d43cac 2022-07-01 thomas return err;
912 a5d43cac 2022-07-01 thomas }
913 a5d43cac 2022-07-01 thomas view_border(view);
914 a5d43cac 2022-07-01 thomas update_panels();
915 a5d43cac 2022-07-01 thomas doupdate();
916 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
917 a5d43cac 2022-07-01 thomas nlines = view->nlines;
918 a5d43cac 2022-07-01 thomas }
919 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
920 40236d76 2022-06-23 thomas ncols = COLS;
921 669b5ffa 2018-10-07 stsp
922 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
923 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
924 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
925 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
926 40236d76 2022-06-23 thomas wclear(view->window);
927 40236d76 2022-06-23 thomas
928 40236d76 2022-06-23 thomas view->nlines = nlines;
929 40236d76 2022-06-23 thomas view->ncols = ncols;
930 40236d76 2022-06-23 thomas view->lines = LINES;
931 40236d76 2022-06-23 thomas view->cols = COLS;
932 40236d76 2022-06-23 thomas
933 5c60c32a 2018-10-18 stsp return NULL;
934 97cb21cd 2022-07-12 thomas }
935 97cb21cd 2022-07-12 thomas
936 97cb21cd 2022-07-12 thomas static void
937 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
938 97cb21cd 2022-07-12 thomas {
939 97cb21cd 2022-07-12 thomas if (n == 0)
940 97cb21cd 2022-07-12 thomas return;
941 97cb21cd 2022-07-12 thomas
942 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
943 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
944 97cb21cd 2022-07-12 thomas view->parent->offset += n;
945 97cb21cd 2022-07-12 thomas else
946 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
947 97cb21cd 2022-07-12 thomas } else if (view->offset) {
948 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
949 97cb21cd 2022-07-12 thomas view->offset -= n;
950 97cb21cd 2022-07-12 thomas else
951 97cb21cd 2022-07-12 thomas view->offset = 0;
952 97cb21cd 2022-07-12 thomas }
953 53d2bdd3 2022-07-10 thomas }
954 53d2bdd3 2022-07-10 thomas
955 53d2bdd3 2022-07-10 thomas static const struct got_error *
956 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
957 53d2bdd3 2022-07-10 thomas {
958 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
959 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
960 53d2bdd3 2022-07-10 thomas
961 53d2bdd3 2022-07-10 thomas if (view->parent)
962 53d2bdd3 2022-07-10 thomas v = view->parent;
963 53d2bdd3 2022-07-10 thomas else
964 53d2bdd3 2022-07-10 thomas v = view;
965 53d2bdd3 2022-07-10 thomas
966 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
967 53d2bdd3 2022-07-10 thomas return NULL;
968 53d2bdd3 2022-07-10 thomas
969 53d2bdd3 2022-07-10 thomas v->resize = v->child->resize = resize; /* lock for resize event */
970 53d2bdd3 2022-07-10 thomas
971 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
972 ae98518f 2022-07-12 thomas int y = v->child->begin_y;
973 ae98518f 2022-07-12 thomas
974 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
975 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
978 53d2bdd3 2022-07-10 thomas else
979 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
980 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
981 53d2bdd3 2022-07-10 thomas view->count = 0;
982 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
983 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
984 53d2bdd3 2022-07-10 thomas view->count = 0;
985 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
986 53d2bdd3 2022-07-10 thomas }
987 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
988 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
989 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
990 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
991 53d2bdd3 2022-07-10 thomas if (err)
992 53d2bdd3 2022-07-10 thomas return err;
993 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
994 ae98518f 2022-07-12 thomas if (y > v->child->begin_y) /* split increased */
995 ae98518f 2022-07-12 thomas v->child->nscrolled = y - v->child->begin_y;
996 53d2bdd3 2022-07-10 thomas } else {
997 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
998 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
999 53d2bdd3 2022-07-10 thomas if (view->parent)
1000 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1001 53d2bdd3 2022-07-10 thomas else
1002 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1003 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1004 53d2bdd3 2022-07-10 thomas view->count = 0;
1005 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1006 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1007 53d2bdd3 2022-07-10 thomas view->count = 0;
1008 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1009 53d2bdd3 2022-07-10 thomas }
1010 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1011 53d2bdd3 2022-07-10 thomas }
1012 53d2bdd3 2022-07-10 thomas
1013 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1014 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1015 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1016 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1017 53d2bdd3 2022-07-10 thomas
1018 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1019 53d2bdd3 2022-07-10 thomas if (err)
1020 53d2bdd3 2022-07-10 thomas return err;
1021 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1022 53d2bdd3 2022-07-10 thomas if (err)
1023 53d2bdd3 2022-07-10 thomas return err;
1024 53d2bdd3 2022-07-10 thomas
1025 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1026 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1027 53d2bdd3 2022-07-10 thomas if (err)
1028 53d2bdd3 2022-07-10 thomas return err;
1029 53d2bdd3 2022-07-10 thomas }
1030 53d2bdd3 2022-07-10 thomas
1031 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1032 53d2bdd3 2022-07-10 thomas err = request_log_commits(v);
1033 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1034 53d2bdd3 2022-07-10 thomas err = request_log_commits(v->child);
1035 53d2bdd3 2022-07-10 thomas
1036 53d2bdd3 2022-07-10 thomas v->resize = v->child->resize = 0;
1037 53d2bdd3 2022-07-10 thomas
1038 53d2bdd3 2022-07-10 thomas return err;
1039 53d2bdd3 2022-07-10 thomas }
1040 53d2bdd3 2022-07-10 thomas
1041 53d2bdd3 2022-07-10 thomas static void
1042 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1043 53d2bdd3 2022-07-10 thomas {
1044 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1045 53d2bdd3 2022-07-10 thomas
1046 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1047 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1048 669b5ffa 2018-10-07 stsp }
1049 669b5ffa 2018-10-07 stsp
1050 669b5ffa 2018-10-07 stsp static const struct got_error *
1051 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1052 669b5ffa 2018-10-07 stsp {
1053 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1054 669b5ffa 2018-10-07 stsp
1055 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1056 669b5ffa 2018-10-07 stsp return NULL;
1057 669b5ffa 2018-10-07 stsp
1058 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1059 669b5ffa 2018-10-07 stsp view->child = NULL;
1060 669b5ffa 2018-10-07 stsp return err;
1061 669b5ffa 2018-10-07 stsp }
1062 669b5ffa 2018-10-07 stsp
1063 40236d76 2022-06-23 thomas static const struct got_error *
1064 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1065 669b5ffa 2018-10-07 stsp {
1066 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1067 53d2bdd3 2022-07-10 thomas
1068 669b5ffa 2018-10-07 stsp view->child = child;
1069 669b5ffa 2018-10-07 stsp child->parent = view;
1070 40236d76 2022-06-23 thomas
1071 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1072 53d2bdd3 2022-07-10 thomas if (err)
1073 53d2bdd3 2022-07-10 thomas return err;
1074 53d2bdd3 2022-07-10 thomas
1075 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1076 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1077 53d2bdd3 2022-07-10 thomas
1078 53d2bdd3 2022-07-10 thomas return err;
1079 bfddd0d9 2018-09-29 stsp }
1080 bfddd0d9 2018-09-29 stsp
1081 34bc9ec9 2019-02-22 stsp static void
1082 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1083 25791caa 2018-10-24 stsp {
1084 25791caa 2018-10-24 stsp int cols, lines;
1085 25791caa 2018-10-24 stsp struct winsize size;
1086 25791caa 2018-10-24 stsp
1087 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1088 25791caa 2018-10-24 stsp cols = 80; /* Default */
1089 25791caa 2018-10-24 stsp lines = 24;
1090 25791caa 2018-10-24 stsp } else {
1091 25791caa 2018-10-24 stsp cols = size.ws_col;
1092 25791caa 2018-10-24 stsp lines = size.ws_row;
1093 25791caa 2018-10-24 stsp }
1094 25791caa 2018-10-24 stsp resize_term(lines, cols);
1095 2b49a8ae 2019-06-22 stsp }
1096 2b49a8ae 2019-06-22 stsp
1097 2b49a8ae 2019-06-22 stsp static const struct got_error *
1098 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1099 2b49a8ae 2019-06-22 stsp {
1100 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1101 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1102 2b49a8ae 2019-06-22 stsp char pattern[1024];
1103 2b49a8ae 2019-06-22 stsp int ret;
1104 c0c4acc8 2021-01-24 stsp
1105 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1106 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1107 c0c4acc8 2021-01-24 stsp view->searching = 0;
1108 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1109 c0c4acc8 2021-01-24 stsp }
1110 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1111 2b49a8ae 2019-06-22 stsp
1112 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1113 2b49a8ae 2019-06-22 stsp return NULL;
1114 2b49a8ae 2019-06-22 stsp
1115 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1116 a5d43cac 2022-07-01 thomas v = view->child;
1117 2b49a8ae 2019-06-22 stsp
1118 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1119 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1120 a5d43cac 2022-07-01 thomas
1121 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1122 2b49a8ae 2019-06-22 stsp nocbreak();
1123 2b49a8ae 2019-06-22 stsp echo();
1124 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1125 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1126 2b49a8ae 2019-06-22 stsp cbreak();
1127 2b49a8ae 2019-06-22 stsp noecho();
1128 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1129 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1130 2b49a8ae 2019-06-22 stsp return NULL;
1131 2b49a8ae 2019-06-22 stsp
1132 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1133 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1134 7c32bd05 2019-06-22 stsp if (err) {
1135 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1136 7c32bd05 2019-06-22 stsp return err;
1137 7c32bd05 2019-06-22 stsp }
1138 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1139 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1140 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1141 2b49a8ae 2019-06-22 stsp view->search_next(view);
1142 2b49a8ae 2019-06-22 stsp }
1143 2b49a8ae 2019-06-22 stsp
1144 2b49a8ae 2019-06-22 stsp return NULL;
1145 64486692 2022-07-07 thomas }
1146 64486692 2022-07-07 thomas
1147 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1148 64486692 2022-07-07 thomas static const struct got_error *
1149 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1150 64486692 2022-07-07 thomas {
1151 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1152 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1153 64486692 2022-07-07 thomas
1154 64486692 2022-07-07 thomas if (view->parent)
1155 64486692 2022-07-07 thomas v = view->parent;
1156 64486692 2022-07-07 thomas else
1157 64486692 2022-07-07 thomas v = view;
1158 64486692 2022-07-07 thomas
1159 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1160 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1161 ddbc4d37 2022-07-12 thomas else
1162 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1163 64486692 2022-07-07 thomas
1164 ddbc4d37 2022-07-12 thomas if (!v->child)
1165 ddbc4d37 2022-07-12 thomas return NULL;
1166 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1167 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1168 ddbc4d37 2022-07-12 thomas
1169 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1170 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1171 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1172 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1173 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1174 64486692 2022-07-07 thomas
1175 ddbc4d37 2022-07-12 thomas
1176 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1177 64486692 2022-07-07 thomas v->ncols = COLS;
1178 64486692 2022-07-07 thomas v->child->ncols = COLS;
1179 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1180 64486692 2022-07-07 thomas
1181 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1182 64486692 2022-07-07 thomas if (err)
1183 64486692 2022-07-07 thomas return err;
1184 64486692 2022-07-07 thomas }
1185 64486692 2022-07-07 thomas v->child->mode = v->mode;
1186 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1187 64486692 2022-07-07 thomas v->focus_child = 1;
1188 64486692 2022-07-07 thomas
1189 64486692 2022-07-07 thomas err = view_fullscreen(v);
1190 64486692 2022-07-07 thomas if (err)
1191 64486692 2022-07-07 thomas return err;
1192 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1193 64486692 2022-07-07 thomas if (err)
1194 64486692 2022-07-07 thomas return err;
1195 64486692 2022-07-07 thomas
1196 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1197 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1198 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1199 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1200 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1201 ddbc4d37 2022-07-12 thomas } else {
1202 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1203 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1204 64486692 2022-07-07 thomas }
1205 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1206 ddbc4d37 2022-07-12 thomas err = request_log_commits(v);
1207 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1208 ddbc4d37 2022-07-12 thomas err = request_log_commits(v->child);
1209 64486692 2022-07-07 thomas
1210 64486692 2022-07-07 thomas return err;
1211 0cf4efb1 2018-09-29 stsp }
1212 6d0fee91 2018-08-01 stsp
1213 07b0611c 2022-06-23 thomas /*
1214 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1215 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1216 07b0611c 2022-06-23 thomas */
1217 07b0611c 2022-06-23 thomas static int
1218 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1219 07b0611c 2022-06-23 thomas {
1220 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1221 a5d43cac 2022-07-01 thomas int x, n = 0;
1222 07b0611c 2022-06-23 thomas
1223 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1224 a5d43cac 2022-07-01 thomas v = view->child;
1225 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1226 a5d43cac 2022-07-01 thomas v = view->parent;
1227 a5d43cac 2022-07-01 thomas
1228 07b0611c 2022-06-23 thomas view->count = 0;
1229 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1230 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1231 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1232 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1233 07b0611c 2022-06-23 thomas
1234 07b0611c 2022-06-23 thomas do {
1235 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1236 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1237 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1238 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1239 a5d43cac 2022-07-01 thomas }
1240 a5d43cac 2022-07-01 thomas
1241 07b0611c 2022-06-23 thomas /*
1242 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1243 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1244 07b0611c 2022-06-23 thomas */
1245 07b0611c 2022-06-23 thomas if (n >= 9999999)
1246 07b0611c 2022-06-23 thomas n = 9999999;
1247 07b0611c 2022-06-23 thomas else
1248 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1249 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1250 07b0611c 2022-06-23 thomas
1251 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1252 07b0611c 2022-06-23 thomas view->count = n;
1253 07b0611c 2022-06-23 thomas
1254 07b0611c 2022-06-23 thomas return c;
1255 07b0611c 2022-06-23 thomas }
1256 07b0611c 2022-06-23 thomas
1257 0cf4efb1 2018-09-29 stsp static const struct got_error *
1258 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1259 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1260 e5a0f69f 2018-08-18 stsp {
1261 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1262 669b5ffa 2018-10-07 stsp struct tog_view *v;
1263 1a76625f 2018-10-22 stsp int ch, errcode;
1264 e5a0f69f 2018-08-18 stsp
1265 e5a0f69f 2018-08-18 stsp *new = NULL;
1266 8f4ed634 2020-03-26 stsp
1267 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1268 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1269 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1271 07b0611c 2022-06-23 thomas view->count = 0;
1272 07b0611c 2022-06-23 thomas }
1273 e5a0f69f 2018-08-18 stsp
1274 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1275 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1276 82954512 2020-02-03 stsp if (errcode)
1277 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1278 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1279 3da8ef85 2021-09-21 stsp sched_yield();
1280 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1281 82954512 2020-02-03 stsp if (errcode)
1282 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1283 82954512 2020-02-03 stsp "pthread_mutex_lock");
1284 60493ae3 2019-06-20 stsp view->search_next(view);
1285 60493ae3 2019-06-20 stsp return NULL;
1286 60493ae3 2019-06-20 stsp }
1287 60493ae3 2019-06-20 stsp
1288 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1289 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1290 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1291 1a76625f 2018-10-22 stsp if (errcode)
1292 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1293 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1294 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1295 634cb454 2022-07-03 thomas cbreak();
1296 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1297 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1298 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1299 634cb454 2022-07-03 thomas view->count = 0;
1300 634cb454 2022-07-03 thomas else
1301 634cb454 2022-07-03 thomas ch = view->ch;
1302 634cb454 2022-07-03 thomas } else {
1303 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1304 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1305 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1306 07b0611c 2022-06-23 thomas }
1307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1308 1a76625f 2018-10-22 stsp if (errcode)
1309 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1310 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1311 25791caa 2018-10-24 stsp
1312 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1313 25791caa 2018-10-24 stsp tog_resizeterm();
1314 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1315 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1316 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1317 25791caa 2018-10-24 stsp err = view_resize(v);
1318 25791caa 2018-10-24 stsp if (err)
1319 25791caa 2018-10-24 stsp return err;
1320 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1321 25791caa 2018-10-24 stsp if (err)
1322 25791caa 2018-10-24 stsp return err;
1323 cdfcfb03 2020-12-06 stsp if (v->child) {
1324 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1325 cdfcfb03 2020-12-06 stsp if (err)
1326 cdfcfb03 2020-12-06 stsp return err;
1327 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1328 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1329 cdfcfb03 2020-12-06 stsp if (err)
1330 cdfcfb03 2020-12-06 stsp return err;
1331 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1332 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1333 53d2bdd3 2022-07-10 thomas if (err)
1334 53d2bdd3 2022-07-10 thomas return err;
1335 53d2bdd3 2022-07-10 thomas }
1336 cdfcfb03 2020-12-06 stsp }
1337 25791caa 2018-10-24 stsp }
1338 25791caa 2018-10-24 stsp }
1339 25791caa 2018-10-24 stsp
1340 e5a0f69f 2018-08-18 stsp switch (ch) {
1341 1e37a5c2 2019-05-12 jcs case '\t':
1342 07b0611c 2022-06-23 thomas view->count = 0;
1343 1e37a5c2 2019-05-12 jcs if (view->child) {
1344 e78dc838 2020-12-04 stsp view->focussed = 0;
1345 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1346 e78dc838 2020-12-04 stsp view->focus_child = 1;
1347 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1348 e78dc838 2020-12-04 stsp view->focussed = 0;
1349 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1350 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1351 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1352 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1353 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1354 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1355 a5d43cac 2022-07-01 thomas if (err)
1356 a5d43cac 2022-07-01 thomas return err;
1357 a5d43cac 2022-07-01 thomas }
1358 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1359 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1360 a5d43cac 2022-07-01 thomas if (err)
1361 a5d43cac 2022-07-01 thomas return err;
1362 a5d43cac 2022-07-01 thomas }
1363 1e37a5c2 2019-05-12 jcs }
1364 1e37a5c2 2019-05-12 jcs break;
1365 1e37a5c2 2019-05-12 jcs case 'q':
1366 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1367 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1368 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1369 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1370 a5d43cac 2022-07-01 thomas if (err)
1371 a5d43cac 2022-07-01 thomas break;
1372 a5d43cac 2022-07-01 thomas }
1373 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1374 a5d43cac 2022-07-01 thomas }
1375 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1376 9970f7fc 2020-12-03 stsp view->dying = 1;
1377 1e37a5c2 2019-05-12 jcs break;
1378 1e37a5c2 2019-05-12 jcs case 'Q':
1379 1e37a5c2 2019-05-12 jcs *done = 1;
1380 1e37a5c2 2019-05-12 jcs break;
1381 1c5e5faa 2022-06-23 thomas case 'F':
1382 07b0611c 2022-06-23 thomas view->count = 0;
1383 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1384 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1385 1e37a5c2 2019-05-12 jcs break;
1386 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1387 e78dc838 2020-12-04 stsp view->focussed = 0;
1388 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1389 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1390 53d2bdd3 2022-07-10 thomas } else {
1391 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1392 53d2bdd3 2022-07-10 thomas if (!err)
1393 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1394 53d2bdd3 2022-07-10 thomas }
1395 1e37a5c2 2019-05-12 jcs if (err)
1396 1e37a5c2 2019-05-12 jcs break;
1397 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1398 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1399 1e37a5c2 2019-05-12 jcs } else {
1400 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1401 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1402 e78dc838 2020-12-04 stsp view->focussed = 1;
1403 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1404 1e37a5c2 2019-05-12 jcs } else {
1405 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1406 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1407 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1408 53d2bdd3 2022-07-10 thomas if (!err)
1409 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1410 669b5ffa 2018-10-07 stsp }
1411 1e37a5c2 2019-05-12 jcs if (err)
1412 1e37a5c2 2019-05-12 jcs break;
1413 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1414 a5d43cac 2022-07-01 thomas }
1415 a5d43cac 2022-07-01 thomas if (err)
1416 a5d43cac 2022-07-01 thomas break;
1417 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1418 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1419 a5d43cac 2022-07-01 thomas if (err)
1420 a5d43cac 2022-07-01 thomas break;
1421 1e37a5c2 2019-05-12 jcs }
1422 a5d43cac 2022-07-01 thomas if (view->parent)
1423 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1424 a5d43cac 2022-07-01 thomas if (!err)
1425 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1426 1e37a5c2 2019-05-12 jcs break;
1427 64486692 2022-07-07 thomas case 'S':
1428 53d2bdd3 2022-07-10 thomas view->count = 0;
1429 64486692 2022-07-07 thomas err = switch_split(view);
1430 53d2bdd3 2022-07-10 thomas break;
1431 53d2bdd3 2022-07-10 thomas case '-':
1432 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1433 64486692 2022-07-07 thomas break;
1434 53d2bdd3 2022-07-10 thomas case '+':
1435 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1436 53d2bdd3 2022-07-10 thomas break;
1437 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1438 60493ae3 2019-06-20 stsp break;
1439 60493ae3 2019-06-20 stsp case '/':
1440 07b0611c 2022-06-23 thomas view->count = 0;
1441 60493ae3 2019-06-20 stsp if (view->search_start)
1442 2b49a8ae 2019-06-22 stsp view_search_start(view);
1443 60493ae3 2019-06-20 stsp else
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1445 1e37a5c2 2019-05-12 jcs break;
1446 b1bf1435 2019-06-21 stsp case 'N':
1447 60493ae3 2019-06-20 stsp case 'n':
1448 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1449 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1450 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1451 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1452 60493ae3 2019-06-20 stsp view->search_next(view);
1453 60493ae3 2019-06-20 stsp } else
1454 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1455 adf4c9e0 2022-07-03 thomas break;
1456 adf4c9e0 2022-07-03 thomas case 'A':
1457 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1458 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1459 adf4c9e0 2022-07-03 thomas else
1460 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1461 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1462 adf4c9e0 2022-07-03 thomas if (v->reset) {
1463 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1464 adf4c9e0 2022-07-03 thomas if (err)
1465 adf4c9e0 2022-07-03 thomas return err;
1466 adf4c9e0 2022-07-03 thomas }
1467 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1468 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1469 adf4c9e0 2022-07-03 thomas if (err)
1470 adf4c9e0 2022-07-03 thomas return err;
1471 adf4c9e0 2022-07-03 thomas }
1472 adf4c9e0 2022-07-03 thomas }
1473 60493ae3 2019-06-20 stsp break;
1474 1e37a5c2 2019-05-12 jcs default:
1475 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1476 1e37a5c2 2019-05-12 jcs break;
1477 e5a0f69f 2018-08-18 stsp }
1478 e5a0f69f 2018-08-18 stsp
1479 e5a0f69f 2018-08-18 stsp return err;
1480 bcbd79e2 2018-08-19 stsp }
1481 bcbd79e2 2018-08-19 stsp
1482 ef20f542 2022-06-26 thomas static int
1483 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1484 a3404814 2018-09-02 stsp {
1485 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1486 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1487 669b5ffa 2018-10-07 stsp return 0;
1488 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1489 669b5ffa 2018-10-07 stsp return 0;
1490 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1491 a3404814 2018-09-02 stsp return 0;
1492 a3404814 2018-09-02 stsp
1493 669b5ffa 2018-10-07 stsp return view->focussed;
1494 a3404814 2018-09-02 stsp }
1495 a3404814 2018-09-02 stsp
1496 bcbd79e2 2018-08-19 stsp static const struct got_error *
1497 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1498 e5a0f69f 2018-08-18 stsp {
1499 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1500 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1501 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1502 64486692 2022-07-07 thomas char *mode;
1503 fd823528 2018-10-22 stsp int fast_refresh = 10;
1504 1a76625f 2018-10-22 stsp int done = 0, errcode;
1505 e5a0f69f 2018-08-18 stsp
1506 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1507 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1508 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1509 64486692 2022-07-07 thomas else
1510 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1511 64486692 2022-07-07 thomas
1512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1513 1a76625f 2018-10-22 stsp if (errcode)
1514 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1515 1a76625f 2018-10-22 stsp
1516 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1517 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1518 e5a0f69f 2018-08-18 stsp
1519 1004088d 2018-09-29 stsp view->focussed = 1;
1520 878940b7 2018-09-29 stsp err = view->show(view);
1521 0cf4efb1 2018-09-29 stsp if (err)
1522 0cf4efb1 2018-09-29 stsp return err;
1523 0cf4efb1 2018-09-29 stsp update_panels();
1524 0cf4efb1 2018-09-29 stsp doupdate();
1525 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1526 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1527 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1528 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1529 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1530 fd823528 2018-10-22 stsp
1531 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1532 e5a0f69f 2018-08-18 stsp if (err)
1533 e5a0f69f 2018-08-18 stsp break;
1534 9970f7fc 2020-12-03 stsp if (view->dying) {
1535 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1536 669b5ffa 2018-10-07 stsp
1537 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1538 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1539 9970f7fc 2020-12-03 stsp entry);
1540 e78dc838 2020-12-04 stsp else if (view->parent)
1541 669b5ffa 2018-10-07 stsp prev = view->parent;
1542 669b5ffa 2018-10-07 stsp
1543 e78dc838 2020-12-04 stsp if (view->parent) {
1544 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1545 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1546 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1547 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1548 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1549 40236d76 2022-06-23 thomas if (err)
1550 40236d76 2022-06-23 thomas break;
1551 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1552 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1553 e78dc838 2020-12-04 stsp } else
1554 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1555 669b5ffa 2018-10-07 stsp
1556 9970f7fc 2020-12-03 stsp err = view_close(view);
1557 fb59748f 2020-12-05 stsp if (err)
1558 e5a0f69f 2018-08-18 stsp goto done;
1559 669b5ffa 2018-10-07 stsp
1560 e78dc838 2020-12-04 stsp view = NULL;
1561 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1562 e78dc838 2020-12-04 stsp if (v->focussed)
1563 e78dc838 2020-12-04 stsp break;
1564 0cf4efb1 2018-09-29 stsp }
1565 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1566 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1567 e78dc838 2020-12-04 stsp if (prev)
1568 e78dc838 2020-12-04 stsp view = prev;
1569 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1570 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1571 e78dc838 2020-12-04 stsp tog_view_list_head);
1572 e78dc838 2020-12-04 stsp }
1573 e78dc838 2020-12-04 stsp if (view) {
1574 e78dc838 2020-12-04 stsp if (view->focus_child) {
1575 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1576 e78dc838 2020-12-04 stsp view = view->child;
1577 e78dc838 2020-12-04 stsp } else
1578 e78dc838 2020-12-04 stsp view->focussed = 1;
1579 e78dc838 2020-12-04 stsp }
1580 e78dc838 2020-12-04 stsp }
1581 e5a0f69f 2018-08-18 stsp }
1582 bcbd79e2 2018-08-19 stsp if (new_view) {
1583 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1584 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1585 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1586 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1587 86c66b02 2018-10-18 stsp continue;
1588 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1589 86c66b02 2018-10-18 stsp err = view_close(v);
1590 86c66b02 2018-10-18 stsp if (err)
1591 86c66b02 2018-10-18 stsp goto done;
1592 86c66b02 2018-10-18 stsp break;
1593 86c66b02 2018-10-18 stsp }
1594 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1595 fed7eaa8 2018-10-24 stsp view = new_view;
1596 7cd52833 2022-06-23 thomas }
1597 669b5ffa 2018-10-07 stsp if (view) {
1598 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1599 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1600 e78dc838 2020-12-04 stsp view = view->child;
1601 e78dc838 2020-12-04 stsp } else {
1602 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1603 e78dc838 2020-12-04 stsp view = view->parent;
1604 1a76625f 2018-10-22 stsp }
1605 e78dc838 2020-12-04 stsp show_panel(view->panel);
1606 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1607 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1608 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1609 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1610 669b5ffa 2018-10-07 stsp if (err)
1611 1a76625f 2018-10-22 stsp goto done;
1612 669b5ffa 2018-10-07 stsp }
1613 669b5ffa 2018-10-07 stsp err = view->show(view);
1614 0cf4efb1 2018-09-29 stsp if (err)
1615 1a76625f 2018-10-22 stsp goto done;
1616 669b5ffa 2018-10-07 stsp if (view->child) {
1617 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1618 669b5ffa 2018-10-07 stsp if (err)
1619 1a76625f 2018-10-22 stsp goto done;
1620 669b5ffa 2018-10-07 stsp }
1621 1a76625f 2018-10-22 stsp update_panels();
1622 1a76625f 2018-10-22 stsp doupdate();
1623 0cf4efb1 2018-09-29 stsp }
1624 e5a0f69f 2018-08-18 stsp }
1625 e5a0f69f 2018-08-18 stsp done:
1626 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1627 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1628 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1629 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1630 f2d749db 2022-07-12 thomas close_err = view_close(view);
1631 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1632 f2d749db 2022-07-12 thomas err = close_err;
1633 e5a0f69f 2018-08-18 stsp }
1634 1a76625f 2018-10-22 stsp
1635 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1636 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1637 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1638 1a76625f 2018-10-22 stsp
1639 e5a0f69f 2018-08-18 stsp return err;
1640 ea5e7bb5 2018-08-01 stsp }
1641 ea5e7bb5 2018-08-01 stsp
1642 4ed7e80c 2018-05-20 stsp __dead static void
1643 9f7d7167 2018-04-29 stsp usage_log(void)
1644 9f7d7167 2018-04-29 stsp {
1645 80ddbec8 2018-04-29 stsp endwin();
1646 c70c5802 2018-08-01 stsp fprintf(stderr,
1647 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1648 9f7d7167 2018-04-29 stsp getprogname());
1649 9f7d7167 2018-04-29 stsp exit(1);
1650 80ddbec8 2018-04-29 stsp }
1651 80ddbec8 2018-04-29 stsp
1652 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1653 80ddbec8 2018-04-29 stsp static const struct got_error *
1654 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1655 963b370f 2018-05-20 stsp {
1656 00dfcb92 2018-06-11 stsp char *vis = NULL;
1657 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1658 963b370f 2018-05-20 stsp
1659 963b370f 2018-05-20 stsp *ws = NULL;
1660 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1661 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1662 00dfcb92 2018-06-11 stsp int vislen;
1663 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1664 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1665 00dfcb92 2018-06-11 stsp
1666 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1667 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1668 00dfcb92 2018-06-11 stsp if (err)
1669 00dfcb92 2018-06-11 stsp return err;
1670 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1671 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1672 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1673 a7f50699 2018-06-11 stsp goto done;
1674 a7f50699 2018-06-11 stsp }
1675 00dfcb92 2018-06-11 stsp }
1676 963b370f 2018-05-20 stsp
1677 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1678 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1679 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1680 a7f50699 2018-06-11 stsp goto done;
1681 a7f50699 2018-06-11 stsp }
1682 963b370f 2018-05-20 stsp
1683 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1685 a7f50699 2018-06-11 stsp done:
1686 00dfcb92 2018-06-11 stsp free(vis);
1687 963b370f 2018-05-20 stsp if (err) {
1688 963b370f 2018-05-20 stsp free(*ws);
1689 963b370f 2018-05-20 stsp *ws = NULL;
1690 963b370f 2018-05-20 stsp *wlen = 0;
1691 963b370f 2018-05-20 stsp }
1692 963b370f 2018-05-20 stsp return err;
1693 05171be4 2022-06-23 thomas }
1694 05171be4 2022-06-23 thomas
1695 05171be4 2022-06-23 thomas static const struct got_error *
1696 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1697 05171be4 2022-06-23 thomas {
1698 05171be4 2022-06-23 thomas char *dst;
1699 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1700 05171be4 2022-06-23 thomas
1701 05171be4 2022-06-23 thomas *ptr = NULL;
1702 05171be4 2022-06-23 thomas n = len = strlen(src);
1703 83316834 2022-06-23 thomas dst = malloc(n + 1);
1704 05171be4 2022-06-23 thomas if (dst == NULL)
1705 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1706 05171be4 2022-06-23 thomas
1707 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1708 05171be4 2022-06-23 thomas const char c = src[idx];
1709 05171be4 2022-06-23 thomas
1710 05171be4 2022-06-23 thomas if (c == '\t') {
1711 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1712 b235ad5d 2022-06-23 thomas char *p;
1713 b235ad5d 2022-06-23 thomas
1714 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1715 83316834 2022-06-23 thomas if (p == NULL) {
1716 83316834 2022-06-23 thomas free(dst);
1717 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1718 83316834 2022-06-23 thomas
1719 83316834 2022-06-23 thomas }
1720 83316834 2022-06-23 thomas dst = p;
1721 05171be4 2022-06-23 thomas n += nb;
1722 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1723 05171be4 2022-06-23 thomas sz += nb;
1724 05171be4 2022-06-23 thomas } else
1725 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1726 05171be4 2022-06-23 thomas ++idx;
1727 05171be4 2022-06-23 thomas }
1728 05171be4 2022-06-23 thomas
1729 05171be4 2022-06-23 thomas dst[sz] = '\0';
1730 05171be4 2022-06-23 thomas *ptr = dst;
1731 05171be4 2022-06-23 thomas return NULL;
1732 963b370f 2018-05-20 stsp }
1733 963b370f 2018-05-20 stsp
1734 8d208d34 2022-06-23 thomas /*
1735 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1736 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1737 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1738 8d208d34 2022-06-23 thomas * *rcol.
1739 f91a2b48 2022-06-23 thomas */
1740 8d208d34 2022-06-23 thomas static int
1741 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1742 8d208d34 2022-06-23 thomas {
1743 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1744 f91a2b48 2022-06-23 thomas
1745 8d208d34 2022-06-23 thomas if (n == 0) {
1746 8d208d34 2022-06-23 thomas *rcol = cols;
1747 8d208d34 2022-06-23 thomas return off;
1748 8d208d34 2022-06-23 thomas }
1749 f91a2b48 2022-06-23 thomas
1750 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1751 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1752 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1753 8d208d34 2022-06-23 thomas else
1754 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1755 f91a2b48 2022-06-23 thomas
1756 8d208d34 2022-06-23 thomas if (width == -1) {
1757 8d208d34 2022-06-23 thomas width = 1;
1758 8d208d34 2022-06-23 thomas wline[i] = L'.';
1759 f91a2b48 2022-06-23 thomas }
1760 f91a2b48 2022-06-23 thomas
1761 8d208d34 2022-06-23 thomas if (cols + width > n)
1762 8d208d34 2022-06-23 thomas break;
1763 8d208d34 2022-06-23 thomas cols += width;
1764 f91a2b48 2022-06-23 thomas }
1765 f91a2b48 2022-06-23 thomas
1766 8d208d34 2022-06-23 thomas *rcol = cols;
1767 8d208d34 2022-06-23 thomas return i;
1768 f91a2b48 2022-06-23 thomas }
1769 f91a2b48 2022-06-23 thomas
1770 f91a2b48 2022-06-23 thomas /*
1771 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1772 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1773 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1774 f91a2b48 2022-06-23 thomas */
1775 f91a2b48 2022-06-23 thomas static const struct got_error *
1776 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1777 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1778 f91a2b48 2022-06-23 thomas {
1779 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1780 8d208d34 2022-06-23 thomas int cols;
1781 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1782 05171be4 2022-06-23 thomas char *exstr = NULL;
1783 963b370f 2018-05-20 stsp size_t wlen;
1784 8d208d34 2022-06-23 thomas int i, scrollx;
1785 963b370f 2018-05-20 stsp
1786 963b370f 2018-05-20 stsp *wlinep = NULL;
1787 b700b5d6 2018-07-10 stsp *widthp = 0;
1788 963b370f 2018-05-20 stsp
1789 05171be4 2022-06-23 thomas if (expand) {
1790 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1791 05171be4 2022-06-23 thomas if (err)
1792 05171be4 2022-06-23 thomas return err;
1793 05171be4 2022-06-23 thomas }
1794 05171be4 2022-06-23 thomas
1795 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1796 05171be4 2022-06-23 thomas free(exstr);
1797 963b370f 2018-05-20 stsp if (err)
1798 963b370f 2018-05-20 stsp return err;
1799 963b370f 2018-05-20 stsp
1800 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1801 f91a2b48 2022-06-23 thomas
1802 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1803 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1804 3f670bfb 2020-12-10 stsp wlen--;
1805 3f670bfb 2020-12-10 stsp }
1806 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1807 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1808 3f670bfb 2020-12-10 stsp wlen--;
1809 3f670bfb 2020-12-10 stsp }
1810 3f670bfb 2020-12-10 stsp
1811 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1812 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1813 27a741e5 2019-09-11 stsp
1814 b700b5d6 2018-07-10 stsp if (widthp)
1815 b700b5d6 2018-07-10 stsp *widthp = cols;
1816 f91a2b48 2022-06-23 thomas if (scrollxp)
1817 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1818 963b370f 2018-05-20 stsp if (err)
1819 963b370f 2018-05-20 stsp free(wline);
1820 963b370f 2018-05-20 stsp else
1821 963b370f 2018-05-20 stsp *wlinep = wline;
1822 963b370f 2018-05-20 stsp return err;
1823 963b370f 2018-05-20 stsp }
1824 963b370f 2018-05-20 stsp
1825 8b473291 2019-02-21 stsp static const struct got_error*
1826 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1827 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1828 8b473291 2019-02-21 stsp {
1829 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1830 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1831 8b473291 2019-02-21 stsp char *s;
1832 8b473291 2019-02-21 stsp const char *name;
1833 8b473291 2019-02-21 stsp
1834 8b473291 2019-02-21 stsp *refs_str = NULL;
1835 8b473291 2019-02-21 stsp
1836 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1837 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1838 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1839 52b5abe1 2019-08-13 stsp int cmp;
1840 52b5abe1 2019-08-13 stsp
1841 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1842 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1843 8b473291 2019-02-21 stsp continue;
1844 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1845 8b473291 2019-02-21 stsp name += 5;
1846 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1847 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1848 7143d404 2019-03-12 stsp continue;
1849 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1850 8b473291 2019-02-21 stsp name += 6;
1851 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1852 8b473291 2019-02-21 stsp name += 8;
1853 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1854 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1855 79cc719f 2020-04-24 stsp continue;
1856 79cc719f 2020-04-24 stsp }
1857 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1858 48cae60d 2020-09-22 stsp if (err)
1859 48cae60d 2020-09-22 stsp break;
1860 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1861 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1862 5d844a1e 2019-08-13 stsp if (err) {
1863 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1864 48cae60d 2020-09-22 stsp free(ref_id);
1865 5d844a1e 2019-08-13 stsp break;
1866 48cae60d 2020-09-22 stsp }
1867 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1868 5d844a1e 2019-08-13 stsp err = NULL;
1869 5d844a1e 2019-08-13 stsp tag = NULL;
1870 5d844a1e 2019-08-13 stsp }
1871 52b5abe1 2019-08-13 stsp }
1872 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1873 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1874 48cae60d 2020-09-22 stsp free(ref_id);
1875 52b5abe1 2019-08-13 stsp if (tag)
1876 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1877 52b5abe1 2019-08-13 stsp if (cmp != 0)
1878 52b5abe1 2019-08-13 stsp continue;
1879 8b473291 2019-02-21 stsp s = *refs_str;
1880 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1881 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1882 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1883 8b473291 2019-02-21 stsp free(s);
1884 8b473291 2019-02-21 stsp *refs_str = NULL;
1885 8b473291 2019-02-21 stsp break;
1886 8b473291 2019-02-21 stsp }
1887 8b473291 2019-02-21 stsp free(s);
1888 8b473291 2019-02-21 stsp }
1889 8b473291 2019-02-21 stsp
1890 8b473291 2019-02-21 stsp return err;
1891 8b473291 2019-02-21 stsp }
1892 8b473291 2019-02-21 stsp
1893 963b370f 2018-05-20 stsp static const struct got_error *
1894 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1895 27a741e5 2019-09-11 stsp int col_tab_align)
1896 5813d178 2019-03-09 stsp {
1897 e6b8b890 2020-12-29 naddy char *smallerthan;
1898 5813d178 2019-03-09 stsp
1899 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1900 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1901 5813d178 2019-03-09 stsp author = smallerthan + 1;
1902 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1903 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1904 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1905 5813d178 2019-03-09 stsp }
1906 5813d178 2019-03-09 stsp
1907 5813d178 2019-03-09 stsp static const struct got_error *
1908 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1909 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1910 8fdc79fe 2020-12-01 naddy int author_display_cols)
1911 80ddbec8 2018-04-29 stsp {
1912 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1913 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1914 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1915 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1916 5813d178 2019-03-09 stsp char *author = NULL;
1917 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1918 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1919 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1920 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1921 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1922 ccb26ccd 2018-11-05 stsp struct tm tm;
1923 45d799e2 2018-12-23 stsp time_t committer_time;
1924 11b20872 2019-11-08 stsp struct tog_color *tc;
1925 80ddbec8 2018-04-29 stsp
1926 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1927 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1928 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1929 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1930 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1931 b39d25c7 2018-07-10 stsp
1932 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1933 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1934 b39d25c7 2018-07-10 stsp else
1935 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1936 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1937 11b20872 2019-11-08 stsp if (tc)
1938 11b20872 2019-11-08 stsp wattr_on(view->window,
1939 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1940 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1941 11b20872 2019-11-08 stsp if (tc)
1942 11b20872 2019-11-08 stsp wattr_off(view->window,
1943 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1944 27a741e5 2019-09-11 stsp col = limit;
1945 b39d25c7 2018-07-10 stsp if (col > avail)
1946 b39d25c7 2018-07-10 stsp goto done;
1947 6570a66d 2019-11-08 stsp
1948 6570a66d 2019-11-08 stsp if (avail >= 120) {
1949 6570a66d 2019-11-08 stsp char *id_str;
1950 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1951 6570a66d 2019-11-08 stsp if (err)
1952 6570a66d 2019-11-08 stsp goto done;
1953 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1954 11b20872 2019-11-08 stsp if (tc)
1955 11b20872 2019-11-08 stsp wattr_on(view->window,
1956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1957 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1958 11b20872 2019-11-08 stsp if (tc)
1959 11b20872 2019-11-08 stsp wattr_off(view->window,
1960 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1961 6570a66d 2019-11-08 stsp free(id_str);
1962 6570a66d 2019-11-08 stsp col += 9;
1963 6570a66d 2019-11-08 stsp if (col > avail)
1964 6570a66d 2019-11-08 stsp goto done;
1965 6570a66d 2019-11-08 stsp }
1966 b39d25c7 2018-07-10 stsp
1967 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1968 5813d178 2019-03-09 stsp if (author == NULL) {
1969 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1970 80ddbec8 2018-04-29 stsp goto done;
1971 80ddbec8 2018-04-29 stsp }
1972 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1973 bb737323 2018-05-20 stsp if (err)
1974 bb737323 2018-05-20 stsp goto done;
1975 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1976 11b20872 2019-11-08 stsp if (tc)
1977 11b20872 2019-11-08 stsp wattr_on(view->window,
1978 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1979 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1980 11b20872 2019-11-08 stsp if (tc)
1981 11b20872 2019-11-08 stsp wattr_off(view->window,
1982 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1983 bb737323 2018-05-20 stsp col += author_width;
1984 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1985 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1986 bb737323 2018-05-20 stsp col++;
1987 bb737323 2018-05-20 stsp author_width++;
1988 bb737323 2018-05-20 stsp }
1989 9c2eaf34 2018-05-20 stsp if (col > avail)
1990 9c2eaf34 2018-05-20 stsp goto done;
1991 80ddbec8 2018-04-29 stsp
1992 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1993 5943eee2 2019-08-13 stsp if (err)
1994 6d9fbc00 2018-04-29 stsp goto done;
1995 bb737323 2018-05-20 stsp logmsg = logmsg0;
1996 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1997 bb737323 2018-05-20 stsp logmsg++;
1998 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1999 bb737323 2018-05-20 stsp if (newline)
2000 bb737323 2018-05-20 stsp *newline = '\0';
2001 f91a2b48 2022-06-23 thomas limit = avail - col;
2002 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2003 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2004 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2005 f91a2b48 2022-06-23 thomas limit, col, 1);
2006 331b1a16 2022-06-23 thomas if (err)
2007 331b1a16 2022-06-23 thomas goto done;
2008 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2009 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2010 27a741e5 2019-09-11 stsp while (col < avail) {
2011 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2012 bb737323 2018-05-20 stsp col++;
2013 881b2d3e 2018-04-30 stsp }
2014 80ddbec8 2018-04-29 stsp done:
2015 80ddbec8 2018-04-29 stsp free(logmsg0);
2016 bb737323 2018-05-20 stsp free(wlogmsg);
2017 5813d178 2019-03-09 stsp free(author);
2018 bb737323 2018-05-20 stsp free(wauthor);
2019 80ddbec8 2018-04-29 stsp free(line);
2020 80ddbec8 2018-04-29 stsp return err;
2021 80ddbec8 2018-04-29 stsp }
2022 26ed57b2 2018-05-19 stsp
2023 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2024 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2025 899d86c2 2018-05-10 stsp struct got_object_id *id)
2026 80ddbec8 2018-04-29 stsp {
2027 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2028 80ddbec8 2018-04-29 stsp
2029 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2030 80ddbec8 2018-04-29 stsp if (entry == NULL)
2031 899d86c2 2018-05-10 stsp return NULL;
2032 99db9666 2018-05-07 stsp
2033 899d86c2 2018-05-10 stsp entry->id = id;
2034 99db9666 2018-05-07 stsp entry->commit = commit;
2035 899d86c2 2018-05-10 stsp return entry;
2036 99db9666 2018-05-07 stsp }
2037 80ddbec8 2018-04-29 stsp
2038 99db9666 2018-05-07 stsp static void
2039 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2040 99db9666 2018-05-07 stsp {
2041 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2042 99db9666 2018-05-07 stsp
2043 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2044 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2045 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2046 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2047 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2048 99db9666 2018-05-07 stsp free(entry);
2049 99db9666 2018-05-07 stsp }
2050 99db9666 2018-05-07 stsp
2051 99db9666 2018-05-07 stsp static void
2052 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2053 99db9666 2018-05-07 stsp {
2054 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2055 99db9666 2018-05-07 stsp pop_commit(commits);
2056 c4972b91 2018-05-07 stsp }
2057 c4972b91 2018-05-07 stsp
2058 c4972b91 2018-05-07 stsp static const struct got_error *
2059 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2060 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2061 13add988 2019-10-15 stsp {
2062 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2063 13add988 2019-10-15 stsp regmatch_t regmatch;
2064 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2065 13add988 2019-10-15 stsp
2066 13add988 2019-10-15 stsp *have_match = 0;
2067 13add988 2019-10-15 stsp
2068 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2069 13add988 2019-10-15 stsp if (err)
2070 13add988 2019-10-15 stsp return err;
2071 13add988 2019-10-15 stsp
2072 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2073 13add988 2019-10-15 stsp if (err)
2074 13add988 2019-10-15 stsp goto done;
2075 13add988 2019-10-15 stsp
2076 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2077 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2078 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2079 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2080 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2081 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2082 13add988 2019-10-15 stsp *have_match = 1;
2083 13add988 2019-10-15 stsp done:
2084 13add988 2019-10-15 stsp free(id_str);
2085 13add988 2019-10-15 stsp free(logmsg);
2086 13add988 2019-10-15 stsp return err;
2087 13add988 2019-10-15 stsp }
2088 13add988 2019-10-15 stsp
2089 13add988 2019-10-15 stsp static const struct got_error *
2090 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2091 c4972b91 2018-05-07 stsp {
2092 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2093 9ba79e04 2018-06-11 stsp
2094 1a76625f 2018-10-22 stsp /*
2095 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2096 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2097 1a76625f 2018-10-22 stsp * while updating the display.
2098 1a76625f 2018-10-22 stsp */
2099 4e0d2870 2020-12-07 naddy do {
2100 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2101 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2102 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2103 1a76625f 2018-10-22 stsp int errcode;
2104 899d86c2 2018-05-10 stsp
2105 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2106 4e0d2870 2020-12-07 naddy NULL, NULL);
2107 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2108 ecb28ae0 2018-07-16 stsp break;
2109 899d86c2 2018-05-10 stsp
2110 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2111 9ba79e04 2018-06-11 stsp if (err)
2112 9ba79e04 2018-06-11 stsp break;
2113 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2114 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2115 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2116 9ba79e04 2018-06-11 stsp break;
2117 9ba79e04 2018-06-11 stsp }
2118 93e45b7c 2018-09-24 stsp
2119 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2120 1a76625f 2018-10-22 stsp if (errcode) {
2121 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2122 13add988 2019-10-15 stsp "pthread_mutex_lock");
2123 1a76625f 2018-10-22 stsp break;
2124 1a76625f 2018-10-22 stsp }
2125 1a76625f 2018-10-22 stsp
2126 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2127 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2128 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2129 1a76625f 2018-10-22 stsp
2130 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2131 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2132 7c1452c1 2020-03-26 stsp int have_match;
2133 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2134 7c1452c1 2020-03-26 stsp if (err)
2135 7c1452c1 2020-03-26 stsp break;
2136 7c1452c1 2020-03-26 stsp if (have_match)
2137 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2138 13add988 2019-10-15 stsp }
2139 13add988 2019-10-15 stsp
2140 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2141 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2142 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2143 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2144 7c1452c1 2020-03-26 stsp if (err)
2145 13add988 2019-10-15 stsp break;
2146 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2147 899d86c2 2018-05-10 stsp
2148 9ba79e04 2018-06-11 stsp return err;
2149 0553a4e3 2018-04-30 stsp }
2150 0553a4e3 2018-04-30 stsp
2151 2b779855 2020-12-05 naddy static void
2152 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2153 2b779855 2020-12-05 naddy {
2154 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2155 2b779855 2020-12-05 naddy int ncommits = 0;
2156 2b779855 2020-12-05 naddy
2157 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2158 2b779855 2020-12-05 naddy while (entry) {
2159 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2160 2b779855 2020-12-05 naddy s->selected_entry = entry;
2161 2b779855 2020-12-05 naddy break;
2162 2b779855 2020-12-05 naddy }
2163 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2164 2b779855 2020-12-05 naddy ncommits++;
2165 2b779855 2020-12-05 naddy }
2166 2b779855 2020-12-05 naddy }
2167 2b779855 2020-12-05 naddy
2168 0553a4e3 2018-04-30 stsp static const struct got_error *
2169 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2170 0553a4e3 2018-04-30 stsp {
2171 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2172 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2173 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2174 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2175 60493ae3 2019-06-20 stsp int width;
2176 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2177 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2178 8b473291 2019-02-21 stsp char *refs_str = NULL;
2179 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2180 11b20872 2019-11-08 stsp struct tog_color *tc;
2181 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2182 0553a4e3 2018-04-30 stsp
2183 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2184 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2185 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2186 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2187 1a76625f 2018-10-22 stsp if (err)
2188 ecb28ae0 2018-07-16 stsp return err;
2189 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2190 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2191 d2075bf3 2020-12-25 stsp if (refs) {
2192 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2193 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2194 d2075bf3 2020-12-25 stsp if (err)
2195 d2075bf3 2020-12-25 stsp goto done;
2196 d2075bf3 2020-12-25 stsp }
2197 867c6645 2018-07-10 stsp }
2198 359bfafd 2019-02-22 stsp
2199 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2200 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2201 1a76625f 2018-10-22 stsp
2202 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2203 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2204 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2205 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2206 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2207 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2208 8f4ed634 2020-03-26 stsp goto done;
2209 8f4ed634 2020-03-26 stsp }
2210 8f4ed634 2020-03-26 stsp } else {
2211 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2212 f9686aa5 2020-03-27 stsp
2213 f9686aa5 2020-03-27 stsp if (view->searching) {
2214 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2215 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2216 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2217 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2218 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2219 f9686aa5 2020-03-27 stsp search_str = "searching...";
2220 f9686aa5 2020-03-27 stsp }
2221 f9686aa5 2020-03-27 stsp
2222 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2223 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2224 f9686aa5 2020-03-27 stsp search_str ? search_str :
2225 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2226 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2227 8f4ed634 2020-03-26 stsp goto done;
2228 8f4ed634 2020-03-26 stsp }
2229 8b473291 2019-02-21 stsp }
2230 1a76625f 2018-10-22 stsp
2231 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2232 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2233 91198554 2022-06-23 thomas "........................................",
2234 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2235 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2236 1a76625f 2018-10-22 stsp header = NULL;
2237 1a76625f 2018-10-22 stsp goto done;
2238 1a76625f 2018-10-22 stsp }
2239 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2240 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2241 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2243 1a76625f 2018-10-22 stsp header = NULL;
2244 1a76625f 2018-10-22 stsp goto done;
2245 ecb28ae0 2018-07-16 stsp }
2246 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2247 1a76625f 2018-10-22 stsp if (err)
2248 1a76625f 2018-10-22 stsp goto done;
2249 867c6645 2018-07-10 stsp
2250 2814baeb 2018-08-01 stsp werase(view->window);
2251 867c6645 2018-07-10 stsp
2252 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2253 a3404814 2018-09-02 stsp wstandout(view->window);
2254 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2255 11b20872 2019-11-08 stsp if (tc)
2256 11b20872 2019-11-08 stsp wattr_on(view->window,
2257 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2258 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2259 11b20872 2019-11-08 stsp if (tc)
2260 11b20872 2019-11-08 stsp wattr_off(view->window,
2261 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2262 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2263 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2264 1a76625f 2018-10-22 stsp width++;
2265 1a76625f 2018-10-22 stsp }
2266 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2267 a3404814 2018-09-02 stsp wstandend(view->window);
2268 ecb28ae0 2018-07-16 stsp free(wline);
2269 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2270 1a76625f 2018-10-22 stsp goto done;
2271 0553a4e3 2018-04-30 stsp
2272 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2273 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2274 5813d178 2019-03-09 stsp ncommits = 0;
2275 05171be4 2022-06-23 thomas view->maxx = 0;
2276 5813d178 2019-03-09 stsp while (entry) {
2277 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2278 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2279 5813d178 2019-03-09 stsp int width;
2280 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2281 5813d178 2019-03-09 stsp break;
2282 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2283 5813d178 2019-03-09 stsp if (author == NULL) {
2284 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2285 5813d178 2019-03-09 stsp goto done;
2286 5813d178 2019-03-09 stsp }
2287 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2288 27a741e5 2019-09-11 stsp date_display_cols);
2289 5813d178 2019-03-09 stsp if (author_cols < width)
2290 5813d178 2019-03-09 stsp author_cols = width;
2291 5813d178 2019-03-09 stsp free(wauthor);
2292 5813d178 2019-03-09 stsp free(author);
2293 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
2294 05171be4 2022-06-23 thomas if (err)
2295 05171be4 2022-06-23 thomas goto done;
2296 05171be4 2022-06-23 thomas msg = msg0;
2297 05171be4 2022-06-23 thomas while (*msg == '\n')
2298 05171be4 2022-06-23 thomas ++msg;
2299 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2300 331b1a16 2022-06-23 thomas *eol = '\0';
2301 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2302 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2303 331b1a16 2022-06-23 thomas if (err)
2304 331b1a16 2022-06-23 thomas goto done;
2305 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2306 05171be4 2022-06-23 thomas free(msg0);
2307 331b1a16 2022-06-23 thomas free(wmsg);
2308 7ca04879 2019-10-19 stsp ncommits++;
2309 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2310 5813d178 2019-03-09 stsp }
2311 5813d178 2019-03-09 stsp
2312 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2313 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2314 867c6645 2018-07-10 stsp ncommits = 0;
2315 899d86c2 2018-05-10 stsp while (entry) {
2316 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2317 899d86c2 2018-05-10 stsp break;
2318 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2319 2814baeb 2018-08-01 stsp wstandout(view->window);
2320 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2321 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2322 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2323 2814baeb 2018-08-01 stsp wstandend(view->window);
2324 0553a4e3 2018-04-30 stsp if (err)
2325 60493ae3 2019-06-20 stsp goto done;
2326 0553a4e3 2018-04-30 stsp ncommits++;
2327 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2328 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2329 80ddbec8 2018-04-29 stsp }
2330 80ddbec8 2018-04-29 stsp
2331 a5d43cac 2022-07-01 thomas view_border(view);
2332 1a76625f 2018-10-22 stsp done:
2333 1a76625f 2018-10-22 stsp free(id_str);
2334 8b473291 2019-02-21 stsp free(refs_str);
2335 1a76625f 2018-10-22 stsp free(ncommits_str);
2336 1a76625f 2018-10-22 stsp free(header);
2337 80ddbec8 2018-04-29 stsp return err;
2338 9f7d7167 2018-04-29 stsp }
2339 07b55e75 2018-05-10 stsp
2340 07b55e75 2018-05-10 stsp static void
2341 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2342 07b55e75 2018-05-10 stsp {
2343 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2344 07b55e75 2018-05-10 stsp int nscrolled = 0;
2345 07b55e75 2018-05-10 stsp
2346 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2347 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2348 07b55e75 2018-05-10 stsp return;
2349 9f7d7167 2018-04-29 stsp
2350 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2351 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2352 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2353 07b55e75 2018-05-10 stsp if (entry) {
2354 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2355 07b55e75 2018-05-10 stsp nscrolled++;
2356 07b55e75 2018-05-10 stsp }
2357 07b55e75 2018-05-10 stsp }
2358 aa075928 2018-05-10 stsp }
2359 aa075928 2018-05-10 stsp
2360 aa075928 2018-05-10 stsp static const struct got_error *
2361 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2362 aa075928 2018-05-10 stsp {
2363 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2364 5e224a3e 2019-02-22 stsp int errcode;
2365 8a42fca8 2019-02-22 stsp
2366 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2367 aa075928 2018-05-10 stsp
2368 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2369 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2370 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2371 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2372 7aafa0d1 2019-02-22 stsp if (errcode)
2373 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2374 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2375 7c1452c1 2020-03-26 stsp
2376 7c1452c1 2020-03-26 stsp /*
2377 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2378 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2379 7c1452c1 2020-03-26 stsp */
2380 7c1452c1 2020-03-26 stsp if (!wait)
2381 7c1452c1 2020-03-26 stsp break;
2382 7c1452c1 2020-03-26 stsp
2383 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2384 ffe38506 2020-12-01 naddy show_log_view(view);
2385 7c1452c1 2020-03-26 stsp update_panels();
2386 7c1452c1 2020-03-26 stsp doupdate();
2387 7c1452c1 2020-03-26 stsp
2388 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2389 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2390 82954512 2020-02-03 stsp if (errcode)
2391 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2392 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2393 82954512 2020-02-03 stsp
2394 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2395 ffe38506 2020-12-01 naddy show_log_view(view);
2396 7c1452c1 2020-03-26 stsp update_panels();
2397 7c1452c1 2020-03-26 stsp doupdate();
2398 5e224a3e 2019-02-22 stsp }
2399 5e224a3e 2019-02-22 stsp
2400 5e224a3e 2019-02-22 stsp return NULL;
2401 a5d43cac 2022-07-01 thomas }
2402 a5d43cac 2022-07-01 thomas
2403 a5d43cac 2022-07-01 thomas static const struct got_error *
2404 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2405 a5d43cac 2022-07-01 thomas {
2406 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2407 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2408 a5d43cac 2022-07-01 thomas
2409 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2410 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2411 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2412 a5d43cac 2022-07-01 thomas
2413 a5d43cac 2022-07-01 thomas return err;
2414 5e224a3e 2019-02-22 stsp }
2415 5e224a3e 2019-02-22 stsp
2416 5e224a3e 2019-02-22 stsp static const struct got_error *
2417 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2418 5e224a3e 2019-02-22 stsp {
2419 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2420 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2421 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2422 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2423 5e224a3e 2019-02-22 stsp
2424 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2425 5e224a3e 2019-02-22 stsp return NULL;
2426 5e224a3e 2019-02-22 stsp
2427 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2428 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2429 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2430 08ebd0a9 2019-02-22 stsp /*
2431 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2432 08ebd0a9 2019-02-22 stsp */
2433 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2434 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2435 5e224a3e 2019-02-22 stsp if (err)
2436 5e224a3e 2019-02-22 stsp return err;
2437 7aafa0d1 2019-02-22 stsp }
2438 b295e71b 2019-02-22 stsp
2439 7aafa0d1 2019-02-22 stsp do {
2440 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2441 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2442 88048b54 2019-02-21 stsp break;
2443 88048b54 2019-02-21 stsp
2444 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2445 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2446 aa075928 2018-05-10 stsp
2447 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2448 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2449 dd0a52c1 2018-05-20 stsp break;
2450 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2451 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2452 aa075928 2018-05-10 stsp
2453 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
2454 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2455 a5d43cac 2022-07-01 thomas else
2456 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2457 a5d43cac 2022-07-01 thomas
2458 dd0a52c1 2018-05-20 stsp return err;
2459 07b55e75 2018-05-10 stsp }
2460 4a7f7875 2018-05-10 stsp
2461 cd0acaa7 2018-05-20 stsp static const struct got_error *
2462 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2463 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2464 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2465 cd0acaa7 2018-05-20 stsp {
2466 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2467 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2468 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2469 cd0acaa7 2018-05-20 stsp
2470 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2471 15a94983 2018-12-23 stsp if (diff_view == NULL)
2472 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2473 ea5e7bb5 2018-08-01 stsp
2474 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2475 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2476 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2477 e5a0f69f 2018-08-18 stsp if (err == NULL)
2478 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2479 cd0acaa7 2018-05-20 stsp return err;
2480 4a7f7875 2018-05-10 stsp }
2481 4a7f7875 2018-05-10 stsp
2482 80ddbec8 2018-04-29 stsp static const struct got_error *
2483 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2484 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2485 9343a5fb 2018-06-23 stsp {
2486 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2487 941e9f74 2019-05-21 stsp
2488 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2489 941e9f74 2019-05-21 stsp if (parent == NULL)
2490 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2491 941e9f74 2019-05-21 stsp
2492 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2493 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2494 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2495 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2496 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2497 941e9f74 2019-05-21 stsp s->tree = subtree;
2498 941e9f74 2019-05-21 stsp s->selected = 0;
2499 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2500 941e9f74 2019-05-21 stsp return NULL;
2501 941e9f74 2019-05-21 stsp }
2502 941e9f74 2019-05-21 stsp
2503 941e9f74 2019-05-21 stsp static const struct got_error *
2504 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2505 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2506 941e9f74 2019-05-21 stsp {
2507 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2508 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2509 941e9f74 2019-05-21 stsp const char *p;
2510 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2511 9343a5fb 2018-06-23 stsp
2512 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2513 941e9f74 2019-05-21 stsp p = path;
2514 941e9f74 2019-05-21 stsp while (*p) {
2515 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2516 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2517 56e0773d 2019-11-28 stsp char *te_name;
2518 33cbf02b 2020-01-12 stsp
2519 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2520 33cbf02b 2020-01-12 stsp p++;
2521 941e9f74 2019-05-21 stsp
2522 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2523 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2524 941e9f74 2019-05-21 stsp if (slash == NULL)
2525 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2526 33cbf02b 2020-01-12 stsp else
2527 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2528 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2529 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2530 56e0773d 2019-11-28 stsp break;
2531 941e9f74 2019-05-21 stsp }
2532 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2533 56e0773d 2019-11-28 stsp if (te == NULL) {
2534 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2535 56e0773d 2019-11-28 stsp free(te_name);
2536 941e9f74 2019-05-21 stsp break;
2537 941e9f74 2019-05-21 stsp }
2538 56e0773d 2019-11-28 stsp free(te_name);
2539 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2540 941e9f74 2019-05-21 stsp
2541 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2542 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2543 b03c880f 2019-05-21 stsp
2544 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2545 941e9f74 2019-05-21 stsp if (slash)
2546 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2547 941e9f74 2019-05-21 stsp else
2548 941e9f74 2019-05-21 stsp subpath = strdup(path);
2549 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2550 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2551 941e9f74 2019-05-21 stsp break;
2552 941e9f74 2019-05-21 stsp }
2553 941e9f74 2019-05-21 stsp
2554 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2555 941e9f74 2019-05-21 stsp subpath);
2556 941e9f74 2019-05-21 stsp if (err)
2557 941e9f74 2019-05-21 stsp break;
2558 941e9f74 2019-05-21 stsp
2559 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2560 941e9f74 2019-05-21 stsp free(tree_id);
2561 941e9f74 2019-05-21 stsp if (err)
2562 941e9f74 2019-05-21 stsp break;
2563 941e9f74 2019-05-21 stsp
2564 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2565 941e9f74 2019-05-21 stsp if (err) {
2566 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2567 941e9f74 2019-05-21 stsp break;
2568 941e9f74 2019-05-21 stsp }
2569 941e9f74 2019-05-21 stsp if (slash == NULL)
2570 941e9f74 2019-05-21 stsp break;
2571 941e9f74 2019-05-21 stsp free(subpath);
2572 941e9f74 2019-05-21 stsp subpath = NULL;
2573 941e9f74 2019-05-21 stsp p = slash;
2574 941e9f74 2019-05-21 stsp }
2575 941e9f74 2019-05-21 stsp
2576 941e9f74 2019-05-21 stsp free(subpath);
2577 1a76625f 2018-10-22 stsp return err;
2578 61266923 2020-01-14 stsp }
2579 61266923 2020-01-14 stsp
2580 61266923 2020-01-14 stsp static const struct got_error *
2581 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2582 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2583 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2584 55cccc34 2020-02-20 stsp {
2585 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2586 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2587 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2588 55cccc34 2020-02-20 stsp
2589 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2590 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2591 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2592 55cccc34 2020-02-20 stsp
2593 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2594 bc573f3b 2021-07-10 stsp if (err)
2595 55cccc34 2020-02-20 stsp return err;
2596 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2597 55cccc34 2020-02-20 stsp
2598 55cccc34 2020-02-20 stsp *new_view = tree_view;
2599 55cccc34 2020-02-20 stsp
2600 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2601 55cccc34 2020-02-20 stsp return NULL;
2602 55cccc34 2020-02-20 stsp
2603 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2604 55cccc34 2020-02-20 stsp }
2605 55cccc34 2020-02-20 stsp
2606 55cccc34 2020-02-20 stsp static const struct got_error *
2607 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2608 61266923 2020-01-14 stsp {
2609 61266923 2020-01-14 stsp sigset_t sigset;
2610 61266923 2020-01-14 stsp int errcode;
2611 61266923 2020-01-14 stsp
2612 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2613 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2614 61266923 2020-01-14 stsp
2615 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2616 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2617 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2618 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2619 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2620 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2621 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2622 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2623 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2624 61266923 2020-01-14 stsp
2625 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2626 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2627 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2628 61266923 2020-01-14 stsp
2629 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2630 61266923 2020-01-14 stsp if (errcode)
2631 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2632 61266923 2020-01-14 stsp
2633 61266923 2020-01-14 stsp return NULL;
2634 1a76625f 2018-10-22 stsp }
2635 1a76625f 2018-10-22 stsp
2636 1a76625f 2018-10-22 stsp static void *
2637 1a76625f 2018-10-22 stsp log_thread(void *arg)
2638 1a76625f 2018-10-22 stsp {
2639 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2640 1a76625f 2018-10-22 stsp int errcode = 0;
2641 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2642 1a76625f 2018-10-22 stsp int done = 0;
2643 61266923 2020-01-14 stsp
2644 f2d749db 2022-07-12 thomas /*
2645 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2646 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2647 f2d749db 2022-07-12 thomas */
2648 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2649 f2d749db 2022-07-12 thomas if (errcode) {
2650 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2651 61266923 2020-01-14 stsp return (void *)err;
2652 f2d749db 2022-07-12 thomas }
2653 1a76625f 2018-10-22 stsp
2654 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2655 f2d749db 2022-07-12 thomas if (err) {
2656 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2657 f2d749db 2022-07-12 thomas goto done;
2658 f2d749db 2022-07-12 thomas }
2659 f2d749db 2022-07-12 thomas
2660 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2661 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2662 f2d749db 2022-07-12 thomas if (errcode) {
2663 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2664 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2665 f2d749db 2022-07-12 thomas goto done;
2666 f2d749db 2022-07-12 thomas }
2667 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2668 1a76625f 2018-10-22 stsp if (err) {
2669 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2670 f2d749db 2022-07-12 thomas goto done;
2671 1a76625f 2018-10-22 stsp err = NULL;
2672 1a76625f 2018-10-22 stsp done = 1;
2673 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2674 1a76625f 2018-10-22 stsp a->commits_needed--;
2675 1a76625f 2018-10-22 stsp
2676 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2677 3abe8080 2019-04-10 stsp if (errcode) {
2678 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2679 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2680 f2d749db 2022-07-12 thomas goto done;
2681 3abe8080 2019-04-10 stsp } else if (*a->quit)
2682 1a76625f 2018-10-22 stsp done = 1;
2683 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2684 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2685 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2686 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2687 1a76625f 2018-10-22 stsp }
2688 1a76625f 2018-10-22 stsp
2689 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2690 7c1452c1 2020-03-26 stsp if (errcode) {
2691 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2692 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2693 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2694 f2d749db 2022-07-12 thomas goto done;
2695 7c1452c1 2020-03-26 stsp }
2696 7c1452c1 2020-03-26 stsp
2697 1a76625f 2018-10-22 stsp if (done)
2698 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2699 7c1452c1 2020-03-26 stsp else {
2700 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2701 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2702 7c1452c1 2020-03-26 stsp &tog_mutex);
2703 f2d749db 2022-07-12 thomas if (errcode) {
2704 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2705 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2706 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2707 f2d749db 2022-07-12 thomas goto done;
2708 f2d749db 2022-07-12 thomas }
2709 21355643 2020-12-06 stsp if (*a->quit)
2710 21355643 2020-12-06 stsp done = 1;
2711 7c1452c1 2020-03-26 stsp }
2712 1a76625f 2018-10-22 stsp }
2713 1a76625f 2018-10-22 stsp }
2714 3abe8080 2019-04-10 stsp a->log_complete = 1;
2715 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2716 f2d749db 2022-07-12 thomas if (errcode)
2717 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2718 f2d749db 2022-07-12 thomas done:
2719 f2d749db 2022-07-12 thomas if (err) {
2720 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2721 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2722 f2d749db 2022-07-12 thomas }
2723 1a76625f 2018-10-22 stsp return (void *)err;
2724 1a76625f 2018-10-22 stsp }
2725 1a76625f 2018-10-22 stsp
2726 1a76625f 2018-10-22 stsp static const struct got_error *
2727 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2728 1a76625f 2018-10-22 stsp {
2729 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2730 1a76625f 2018-10-22 stsp int errcode;
2731 1a76625f 2018-10-22 stsp
2732 1a76625f 2018-10-22 stsp if (s->thread) {
2733 1a76625f 2018-10-22 stsp s->quit = 1;
2734 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2735 1a76625f 2018-10-22 stsp if (errcode)
2736 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2737 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2738 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2739 1a76625f 2018-10-22 stsp if (errcode)
2740 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2741 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2742 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2743 1a76625f 2018-10-22 stsp if (errcode)
2744 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2746 1a76625f 2018-10-22 stsp if (errcode)
2747 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2748 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2749 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2750 1a76625f 2018-10-22 stsp }
2751 1a76625f 2018-10-22 stsp
2752 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2753 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2754 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2755 1af5eddf 2022-06-23 thomas }
2756 1af5eddf 2022-06-23 thomas
2757 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2758 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2759 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2760 1af5eddf 2022-06-23 thomas if (err == NULL)
2761 1af5eddf 2022-06-23 thomas err = pack_err;
2762 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2763 1a76625f 2018-10-22 stsp }
2764 1a76625f 2018-10-22 stsp
2765 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2766 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2767 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2768 1a76625f 2018-10-22 stsp }
2769 1a76625f 2018-10-22 stsp
2770 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2771 9343a5fb 2018-06-23 stsp }
2772 9343a5fb 2018-06-23 stsp
2773 9343a5fb 2018-06-23 stsp static const struct got_error *
2774 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2775 1a76625f 2018-10-22 stsp {
2776 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2777 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2778 276b94a1 2020-11-13 naddy int errcode;
2779 1a76625f 2018-10-22 stsp
2780 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2781 276b94a1 2020-11-13 naddy
2782 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2783 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2784 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2785 276b94a1 2020-11-13 naddy
2786 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2787 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2788 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2789 276b94a1 2020-11-13 naddy
2790 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2791 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2792 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2793 1a76625f 2018-10-22 stsp free(s->start_id);
2794 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2795 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2796 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2797 1a76625f 2018-10-22 stsp return err;
2798 1a76625f 2018-10-22 stsp }
2799 1a76625f 2018-10-22 stsp
2800 1a76625f 2018-10-22 stsp static const struct got_error *
2801 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2802 60493ae3 2019-06-20 stsp {
2803 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2804 60493ae3 2019-06-20 stsp
2805 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2806 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2807 60493ae3 2019-06-20 stsp return NULL;
2808 60493ae3 2019-06-20 stsp }
2809 60493ae3 2019-06-20 stsp
2810 60493ae3 2019-06-20 stsp static const struct got_error *
2811 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2812 60493ae3 2019-06-20 stsp {
2813 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2814 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2815 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2816 60493ae3 2019-06-20 stsp
2817 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2818 f9686aa5 2020-03-27 stsp show_log_view(view);
2819 f9686aa5 2020-03-27 stsp update_panels();
2820 f9686aa5 2020-03-27 stsp doupdate();
2821 f9686aa5 2020-03-27 stsp
2822 96e2b566 2019-07-08 stsp if (s->search_entry) {
2823 13add988 2019-10-15 stsp int errcode, ch;
2824 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2825 13add988 2019-10-15 stsp if (errcode)
2826 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2827 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2828 13add988 2019-10-15 stsp ch = wgetch(view->window);
2829 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2830 13add988 2019-10-15 stsp if (errcode)
2831 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2832 13add988 2019-10-15 stsp "pthread_mutex_lock");
2833 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2834 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2835 678cbce5 2019-07-28 stsp return NULL;
2836 678cbce5 2019-07-28 stsp }
2837 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2838 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2839 96e2b566 2019-07-08 stsp else
2840 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2841 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2842 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2843 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2844 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2845 df5e841d 2022-06-23 thomas
2846 df5e841d 2022-06-23 thomas /*
2847 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2848 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2849 1f4d5162 2022-06-23 thomas * might have changed.
2850 df5e841d 2022-06-23 thomas */
2851 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2852 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2853 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2854 df5e841d 2022-06-23 thomas else
2855 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2856 b74feda6 2022-06-23 thomas } else {
2857 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2858 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2859 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2860 df5e841d 2022-06-23 thomas else
2861 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2862 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2863 b74feda6 2022-06-23 thomas }
2864 20be8d96 2019-06-21 stsp } else {
2865 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2866 20be8d96 2019-06-21 stsp }
2867 60493ae3 2019-06-20 stsp
2868 60493ae3 2019-06-20 stsp while (1) {
2869 13add988 2019-10-15 stsp int have_match = 0;
2870 13add988 2019-10-15 stsp
2871 60493ae3 2019-06-20 stsp if (entry == NULL) {
2872 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2873 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2874 f9967bca 2020-03-27 stsp view->search_next_done =
2875 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2876 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2877 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2878 f801134a 2019-06-25 stsp return NULL;
2879 60493ae3 2019-06-20 stsp }
2880 96e2b566 2019-07-08 stsp /*
2881 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2882 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2883 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2884 96e2b566 2019-07-08 stsp */
2885 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2886 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2887 60493ae3 2019-06-20 stsp }
2888 60493ae3 2019-06-20 stsp
2889 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2890 13add988 2019-10-15 stsp &view->regex);
2891 5943eee2 2019-08-13 stsp if (err)
2892 13add988 2019-10-15 stsp break;
2893 13add988 2019-10-15 stsp if (have_match) {
2894 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2895 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2896 60493ae3 2019-06-20 stsp break;
2897 60493ae3 2019-06-20 stsp }
2898 13add988 2019-10-15 stsp
2899 96e2b566 2019-07-08 stsp s->search_entry = entry;
2900 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2901 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2902 b1bf1435 2019-06-21 stsp else
2903 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2904 60493ae3 2019-06-20 stsp }
2905 60493ae3 2019-06-20 stsp
2906 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2907 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2908 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2909 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2910 60493ae3 2019-06-20 stsp if (err)
2911 60493ae3 2019-06-20 stsp return err;
2912 ead14cbe 2019-06-21 stsp cur++;
2913 ead14cbe 2019-06-21 stsp }
2914 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2915 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2916 60493ae3 2019-06-20 stsp if (err)
2917 60493ae3 2019-06-20 stsp return err;
2918 ead14cbe 2019-06-21 stsp cur--;
2919 60493ae3 2019-06-20 stsp }
2920 60493ae3 2019-06-20 stsp }
2921 60493ae3 2019-06-20 stsp
2922 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2923 96e2b566 2019-07-08 stsp
2924 60493ae3 2019-06-20 stsp return NULL;
2925 60493ae3 2019-06-20 stsp }
2926 60493ae3 2019-06-20 stsp
2927 60493ae3 2019-06-20 stsp static const struct got_error *
2928 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2929 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2930 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2931 80ddbec8 2018-04-29 stsp {
2932 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2933 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2934 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2935 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2936 1a76625f 2018-10-22 stsp int errcode;
2937 80ddbec8 2018-04-29 stsp
2938 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2939 f135c941 2020-02-20 stsp free(s->in_repo_path);
2940 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2941 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2942 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2943 f135c941 2020-02-20 stsp }
2944 ecb28ae0 2018-07-16 stsp
2945 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2946 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2947 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2948 78756c87 2020-11-24 stsp
2949 fb2756b9 2018-08-04 stsp s->repo = repo;
2950 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2951 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2952 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2953 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2954 9cd7cbd1 2020-12-07 stsp goto done;
2955 9cd7cbd1 2020-12-07 stsp }
2956 9cd7cbd1 2020-12-07 stsp }
2957 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2958 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2959 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2960 5036bf37 2018-09-24 stsp goto done;
2961 5036bf37 2018-09-24 stsp }
2962 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2963 e5a0f69f 2018-08-18 stsp
2964 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2965 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2966 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2967 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2968 11b20872 2019-11-08 stsp if (err)
2969 11b20872 2019-11-08 stsp goto done;
2970 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2971 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2972 11b20872 2019-11-08 stsp if (err) {
2973 11b20872 2019-11-08 stsp free_colors(&s->colors);
2974 11b20872 2019-11-08 stsp goto done;
2975 11b20872 2019-11-08 stsp }
2976 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2977 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2978 11b20872 2019-11-08 stsp if (err) {
2979 11b20872 2019-11-08 stsp free_colors(&s->colors);
2980 11b20872 2019-11-08 stsp goto done;
2981 11b20872 2019-11-08 stsp }
2982 11b20872 2019-11-08 stsp }
2983 11b20872 2019-11-08 stsp
2984 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2985 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2986 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2987 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2988 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2989 1a76625f 2018-10-22 stsp
2990 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2991 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2992 1af5eddf 2022-06-23 thomas if (err)
2993 1af5eddf 2022-06-23 thomas goto done;
2994 1af5eddf 2022-06-23 thomas }
2995 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2996 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2997 7cd52833 2022-06-23 thomas if (err)
2998 7cd52833 2022-06-23 thomas goto done;
2999 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3000 b672a97a 2020-01-27 stsp !s->log_branches);
3001 1a76625f 2018-10-22 stsp if (err)
3002 1a76625f 2018-10-22 stsp goto done;
3003 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3004 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3005 c5b78334 2020-01-12 stsp if (err)
3006 c5b78334 2020-01-12 stsp goto done;
3007 1a76625f 2018-10-22 stsp
3008 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3009 1a76625f 2018-10-22 stsp if (errcode) {
3010 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3011 1a76625f 2018-10-22 stsp goto done;
3012 1a76625f 2018-10-22 stsp }
3013 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3014 7c1452c1 2020-03-26 stsp if (errcode) {
3015 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3016 7c1452c1 2020-03-26 stsp goto done;
3017 7c1452c1 2020-03-26 stsp }
3018 1a76625f 2018-10-22 stsp
3019 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3020 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3021 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3022 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3023 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3024 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3025 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3026 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3027 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3028 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3029 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3030 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3031 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3032 ba4f502b 2018-08-04 stsp done:
3033 1a76625f 2018-10-22 stsp if (err)
3034 1a76625f 2018-10-22 stsp close_log_view(view);
3035 ba4f502b 2018-08-04 stsp return err;
3036 ba4f502b 2018-08-04 stsp }
3037 ba4f502b 2018-08-04 stsp
3038 e5a0f69f 2018-08-18 stsp static const struct got_error *
3039 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3040 ba4f502b 2018-08-04 stsp {
3041 f2f6d207 2020-11-24 stsp const struct got_error *err;
3042 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3043 ba4f502b 2018-08-04 stsp
3044 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3045 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3046 2b380cc8 2018-10-24 stsp &s->thread_args);
3047 2b380cc8 2018-10-24 stsp if (errcode)
3048 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3049 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3050 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3051 f2f6d207 2020-11-24 stsp if (err)
3052 f2f6d207 2020-11-24 stsp return err;
3053 f2f6d207 2020-11-24 stsp }
3054 2b380cc8 2018-10-24 stsp }
3055 2b380cc8 2018-10-24 stsp
3056 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3057 b31f89ff 2022-07-01 thomas }
3058 b31f89ff 2022-07-01 thomas
3059 b31f89ff 2022-07-01 thomas static void
3060 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3061 b31f89ff 2022-07-01 thomas {
3062 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3063 b31f89ff 2022-07-01 thomas
3064 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3065 b31f89ff 2022-07-01 thomas view->count = 0;
3066 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3067 b31f89ff 2022-07-01 thomas return;
3068 b31f89ff 2022-07-01 thomas
3069 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3070 b31f89ff 2022-07-01 thomas || home)
3071 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3072 b31f89ff 2022-07-01 thomas
3073 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3074 b31f89ff 2022-07-01 thomas --s->selected;
3075 b31f89ff 2022-07-01 thomas else
3076 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3077 b31f89ff 2022-07-01 thomas
3078 b31f89ff 2022-07-01 thomas select_commit(s);
3079 b31f89ff 2022-07-01 thomas return;
3080 b31f89ff 2022-07-01 thomas }
3081 b31f89ff 2022-07-01 thomas
3082 b31f89ff 2022-07-01 thomas static const struct got_error *
3083 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
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 struct commit_queue_entry *first;
3087 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3088 b31f89ff 2022-07-01 thomas
3089 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3090 b31f89ff 2022-07-01 thomas if (first == NULL) {
3091 b31f89ff 2022-07-01 thomas view->count = 0;
3092 b31f89ff 2022-07-01 thomas return NULL;
3093 b31f89ff 2022-07-01 thomas }
3094 b31f89ff 2022-07-01 thomas
3095 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3096 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3097 b31f89ff 2022-07-01 thomas return NULL;
3098 b31f89ff 2022-07-01 thomas
3099 b31f89ff 2022-07-01 thomas if (!page) {
3100 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3101 b31f89ff 2022-07-01 thomas
3102 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3103 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3104 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3105 b31f89ff 2022-07-01 thomas ++s->selected;
3106 b31f89ff 2022-07-01 thomas else
3107 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3108 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3109 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3110 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3111 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3112 b31f89ff 2022-07-01 thomas else
3113 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3114 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3115 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3116 b31f89ff 2022-07-01 thomas } else {
3117 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3118 b31f89ff 2022-07-01 thomas if (err)
3119 b31f89ff 2022-07-01 thomas return err;
3120 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3121 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3122 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3123 b31f89ff 2022-07-01 thomas }
3124 b31f89ff 2022-07-01 thomas }
3125 b31f89ff 2022-07-01 thomas if (err)
3126 b31f89ff 2022-07-01 thomas return err;
3127 b31f89ff 2022-07-01 thomas
3128 a5d43cac 2022-07-01 thomas /*
3129 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3130 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3131 a5d43cac 2022-07-01 thomas */
3132 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3133 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3134 a5d43cac 2022-07-01 thomas
3135 b31f89ff 2022-07-01 thomas select_commit(s);
3136 b31f89ff 2022-07-01 thomas
3137 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3138 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3139 b31f89ff 2022-07-01 thomas view->count = 0;
3140 b31f89ff 2022-07-01 thomas
3141 b31f89ff 2022-07-01 thomas return NULL;
3142 e5a0f69f 2018-08-18 stsp }
3143 04cc582a 2018-08-01 stsp
3144 a5d43cac 2022-07-01 thomas static void
3145 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3146 a5d43cac 2022-07-01 thomas {
3147 24415785 2022-07-03 thomas *x = 0;
3148 24415785 2022-07-03 thomas *y = 0;
3149 24415785 2022-07-03 thomas
3150 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3151 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3152 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3153 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3154 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3155 53d2bdd3 2022-07-10 thomas else
3156 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3157 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3158 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3159 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3160 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3161 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3162 53d2bdd3 2022-07-10 thomas else
3163 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3164 53d2bdd3 2022-07-10 thomas }
3165 a5d43cac 2022-07-01 thomas }
3166 a5d43cac 2022-07-01 thomas
3167 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3168 e5a0f69f 2018-08-18 stsp static const struct got_error *
3169 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3170 a5d43cac 2022-07-01 thomas {
3171 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3172 a5d43cac 2022-07-01 thomas
3173 a5d43cac 2022-07-01 thomas view->nlines = y;
3174 64486692 2022-07-07 thomas view->ncols = COLS;
3175 a5d43cac 2022-07-01 thomas err = view_resize(view);
3176 a5d43cac 2022-07-01 thomas if (err)
3177 a5d43cac 2022-07-01 thomas return err;
3178 a5d43cac 2022-07-01 thomas
3179 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3180 a5d43cac 2022-07-01 thomas
3181 a5d43cac 2022-07-01 thomas return err;
3182 a5d43cac 2022-07-01 thomas }
3183 a5d43cac 2022-07-01 thomas
3184 a5d43cac 2022-07-01 thomas static const struct got_error *
3185 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3186 e5a0f69f 2018-08-18 stsp {
3187 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3188 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3189 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3190 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3191 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3192 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3193 80ddbec8 2018-04-29 stsp
3194 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3195 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3196 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3197 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3198 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3199 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3200 fb280deb 2021-08-30 stsp }
3201 b31f89ff 2022-07-01 thomas return err;
3202 528dedf3 2021-08-30 stsp }
3203 068ab281 2022-07-01 thomas
3204 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3205 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3206 068ab281 2022-07-01 thomas --eos; /* border */
3207 068ab281 2022-07-01 thomas
3208 528dedf3 2021-08-30 stsp switch (ch) {
3209 1e37a5c2 2019-05-12 jcs case 'q':
3210 1e37a5c2 2019-05-12 jcs s->quit = 1;
3211 05171be4 2022-06-23 thomas break;
3212 05171be4 2022-06-23 thomas case '0':
3213 05171be4 2022-06-23 thomas view->x = 0;
3214 05171be4 2022-06-23 thomas break;
3215 05171be4 2022-06-23 thomas case '$':
3216 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3217 07b0611c 2022-06-23 thomas view->count = 0;
3218 05171be4 2022-06-23 thomas break;
3219 05171be4 2022-06-23 thomas case KEY_RIGHT:
3220 05171be4 2022-06-23 thomas case 'l':
3221 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3222 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3223 07b0611c 2022-06-23 thomas else
3224 07b0611c 2022-06-23 thomas view->count = 0;
3225 1e37a5c2 2019-05-12 jcs break;
3226 05171be4 2022-06-23 thomas case KEY_LEFT:
3227 05171be4 2022-06-23 thomas case 'h':
3228 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3229 07b0611c 2022-06-23 thomas if (view->x <= 0)
3230 07b0611c 2022-06-23 thomas view->count = 0;
3231 05171be4 2022-06-23 thomas break;
3232 1e37a5c2 2019-05-12 jcs case 'k':
3233 1e37a5c2 2019-05-12 jcs case KEY_UP:
3234 1e37a5c2 2019-05-12 jcs case '<':
3235 1e37a5c2 2019-05-12 jcs case ',':
3236 f7140bf5 2021-10-17 thomas case CTRL('p'):
3237 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3238 912a3f79 2021-08-30 j break;
3239 912a3f79 2021-08-30 j case 'g':
3240 912a3f79 2021-08-30 j case KEY_HOME:
3241 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3242 07b0611c 2022-06-23 thomas view->count = 0;
3243 1e37a5c2 2019-05-12 jcs break;
3244 70f17a53 2022-06-13 thomas case CTRL('u'):
3245 23427b14 2022-06-23 thomas case 'u':
3246 70f17a53 2022-06-13 thomas nscroll /= 2;
3247 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3248 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3249 a4292ac5 2019-05-12 jcs case CTRL('b'):
3250 1c5e5faa 2022-06-23 thomas case 'b':
3251 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3252 1e37a5c2 2019-05-12 jcs break;
3253 1e37a5c2 2019-05-12 jcs case 'j':
3254 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3255 1e37a5c2 2019-05-12 jcs case '>':
3256 1e37a5c2 2019-05-12 jcs case '.':
3257 f7140bf5 2021-10-17 thomas case CTRL('n'):
3258 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3259 912a3f79 2021-08-30 j break;
3260 912a3f79 2021-08-30 j case 'G':
3261 912a3f79 2021-08-30 j case KEY_END: {
3262 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3263 912a3f79 2021-08-30 j * traverse them all. */
3264 07b0611c 2022-06-23 thomas view->count = 0;
3265 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3266 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3267 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3268 80ddbec8 2018-04-29 stsp }
3269 912a3f79 2021-08-30 j
3270 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3271 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3272 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3273 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3274 f3bc9f1d 2021-09-05 naddy break;
3275 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3276 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3277 f3bc9f1d 2021-09-05 naddy }
3278 f3bc9f1d 2021-09-05 naddy if (n > 0)
3279 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3280 2b779855 2020-12-05 naddy select_commit(s);
3281 1e37a5c2 2019-05-12 jcs break;
3282 912a3f79 2021-08-30 j }
3283 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3284 23427b14 2022-06-23 thomas case 'd':
3285 70f17a53 2022-06-13 thomas nscroll /= 2;
3286 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3287 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3288 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3289 4c2d69cb 2022-06-23 thomas case 'f':
3290 b31f89ff 2022-07-01 thomas case ' ':
3291 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3292 1e37a5c2 2019-05-12 jcs break;
3293 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3294 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3295 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3296 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3297 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3298 2b779855 2020-12-05 naddy select_commit(s);
3299 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3300 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3301 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3302 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3303 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3304 0bf7f153 2020-12-02 naddy }
3305 1e37a5c2 2019-05-12 jcs break;
3306 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3307 444d5325 2022-07-03 thomas case '\r':
3308 07b0611c 2022-06-23 thomas view->count = 0;
3309 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3310 e5a0f69f 2018-08-18 stsp break;
3311 a5d43cac 2022-07-01 thomas
3312 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3313 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3314 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3315 a5d43cac 2022-07-01 thomas
3316 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3317 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3318 78756c87 2020-11-24 stsp view, s->repo);
3319 1e37a5c2 2019-05-12 jcs if (err)
3320 1e37a5c2 2019-05-12 jcs break;
3321 a5d43cac 2022-07-01 thomas
3322 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3323 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3324 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3325 a5d43cac 2022-07-01 thomas if (err)
3326 a5d43cac 2022-07-01 thomas break;
3327 a5d43cac 2022-07-01 thomas }
3328 a5d43cac 2022-07-01 thomas
3329 e78dc838 2020-12-04 stsp view->focussed = 0;
3330 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3331 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3332 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3333 a5d43cac 2022-07-01 thomas
3334 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3335 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3336 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3337 f7013a22 2018-10-24 stsp if (err)
3338 1e37a5c2 2019-05-12 jcs return err;
3339 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3340 40236d76 2022-06-23 thomas if (err)
3341 40236d76 2022-06-23 thomas return err;
3342 e78dc838 2020-12-04 stsp view->focus_child = 1;
3343 1e37a5c2 2019-05-12 jcs } else
3344 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3345 1e37a5c2 2019-05-12 jcs break;
3346 1e37a5c2 2019-05-12 jcs case 't':
3347 07b0611c 2022-06-23 thomas view->count = 0;
3348 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3349 5036bf37 2018-09-24 stsp break;
3350 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3351 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3352 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3353 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3354 4e97c21c 2020-12-06 stsp s->repo);
3355 1e37a5c2 2019-05-12 jcs if (err)
3356 e5a0f69f 2018-08-18 stsp break;
3357 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3358 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3359 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3360 444d5325 2022-07-03 thomas if (err)
3361 444d5325 2022-07-03 thomas break;
3362 444d5325 2022-07-03 thomas }
3363 e78dc838 2020-12-04 stsp view->focussed = 0;
3364 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3365 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3366 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3367 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3368 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3369 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3370 1e37a5c2 2019-05-12 jcs if (err)
3371 1e37a5c2 2019-05-12 jcs return err;
3372 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3373 40236d76 2022-06-23 thomas if (err)
3374 40236d76 2022-06-23 thomas return err;
3375 e78dc838 2020-12-04 stsp view->focus_child = 1;
3376 1e37a5c2 2019-05-12 jcs } else
3377 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3378 1e37a5c2 2019-05-12 jcs break;
3379 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3380 21355643 2020-12-06 stsp case CTRL('l'):
3381 21355643 2020-12-06 stsp case 'B':
3382 07b0611c 2022-06-23 thomas view->count = 0;
3383 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3384 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3385 1e37a5c2 2019-05-12 jcs break;
3386 21355643 2020-12-06 stsp err = stop_log_thread(s);
3387 74cfe85e 2020-10-20 stsp if (err)
3388 74cfe85e 2020-10-20 stsp return err;
3389 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3390 21355643 2020-12-06 stsp char *parent_path;
3391 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3392 21355643 2020-12-06 stsp if (err)
3393 21355643 2020-12-06 stsp return err;
3394 21355643 2020-12-06 stsp free(s->in_repo_path);
3395 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3396 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3397 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3398 21355643 2020-12-06 stsp struct got_object_id *start_id;
3399 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3400 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3401 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3402 21355643 2020-12-06 stsp if (err)
3403 21355643 2020-12-06 stsp return err;
3404 21355643 2020-12-06 stsp free(s->start_id);
3405 21355643 2020-12-06 stsp s->start_id = start_id;
3406 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3407 21355643 2020-12-06 stsp } else /* 'B' */
3408 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3409 21355643 2020-12-06 stsp
3410 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3411 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3412 bf7e79b3 2022-06-23 thomas if (err)
3413 bf7e79b3 2022-06-23 thomas return err;
3414 bf7e79b3 2022-06-23 thomas }
3415 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3416 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3417 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3418 74cfe85e 2020-10-20 stsp if (err)
3419 21355643 2020-12-06 stsp return err;
3420 51a10b52 2020-12-26 stsp tog_free_refs();
3421 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3422 ca51c541 2020-12-07 stsp if (err)
3423 ca51c541 2020-12-07 stsp return err;
3424 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3425 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3426 d01904d4 2019-06-25 stsp if (err)
3427 d01904d4 2019-06-25 stsp return err;
3428 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3429 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3430 b672a97a 2020-01-27 stsp if (err)
3431 b672a97a 2020-01-27 stsp return err;
3432 21355643 2020-12-06 stsp free_commits(&s->commits);
3433 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3434 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3435 21355643 2020-12-06 stsp s->selected_entry = NULL;
3436 21355643 2020-12-06 stsp s->selected = 0;
3437 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3438 21355643 2020-12-06 stsp s->quit = 0;
3439 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3440 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3441 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3442 d01904d4 2019-06-25 stsp break;
3443 6458efa5 2020-11-24 stsp case 'r':
3444 07b0611c 2022-06-23 thomas view->count = 0;
3445 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3446 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3447 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3448 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3449 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3450 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3451 6458efa5 2020-11-24 stsp if (err) {
3452 6458efa5 2020-11-24 stsp view_close(ref_view);
3453 6458efa5 2020-11-24 stsp return err;
3454 6458efa5 2020-11-24 stsp }
3455 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3456 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3457 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3458 444d5325 2022-07-03 thomas if (err)
3459 444d5325 2022-07-03 thomas break;
3460 444d5325 2022-07-03 thomas }
3461 e78dc838 2020-12-04 stsp view->focussed = 0;
3462 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3463 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3464 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3465 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3466 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3467 6458efa5 2020-11-24 stsp err = view_close_child(view);
3468 6458efa5 2020-11-24 stsp if (err)
3469 6458efa5 2020-11-24 stsp return err;
3470 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3471 40236d76 2022-06-23 thomas if (err)
3472 40236d76 2022-06-23 thomas return err;
3473 e78dc838 2020-12-04 stsp view->focus_child = 1;
3474 6458efa5 2020-11-24 stsp } else
3475 6458efa5 2020-11-24 stsp *new_view = ref_view;
3476 6458efa5 2020-11-24 stsp break;
3477 1e37a5c2 2019-05-12 jcs default:
3478 07b0611c 2022-06-23 thomas view->count = 0;
3479 1e37a5c2 2019-05-12 jcs break;
3480 899d86c2 2018-05-10 stsp }
3481 e5a0f69f 2018-08-18 stsp
3482 80ddbec8 2018-04-29 stsp return err;
3483 80ddbec8 2018-04-29 stsp }
3484 80ddbec8 2018-04-29 stsp
3485 4ed7e80c 2018-05-20 stsp static const struct got_error *
3486 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3487 c2db6724 2019-01-04 stsp {
3488 c2db6724 2019-01-04 stsp const struct got_error *error;
3489 c2db6724 2019-01-04 stsp
3490 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3491 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3492 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3493 37c06ea4 2019-07-15 stsp #endif
3494 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3495 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3496 c2db6724 2019-01-04 stsp
3497 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3498 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3499 c2db6724 2019-01-04 stsp
3500 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3501 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3502 c2db6724 2019-01-04 stsp
3503 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3504 c2db6724 2019-01-04 stsp if (error != NULL)
3505 c2db6724 2019-01-04 stsp return error;
3506 c2db6724 2019-01-04 stsp
3507 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3508 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3509 c2db6724 2019-01-04 stsp
3510 c2db6724 2019-01-04 stsp return NULL;
3511 c2db6724 2019-01-04 stsp }
3512 c2db6724 2019-01-04 stsp
3513 a915003a 2019-02-05 stsp static void
3514 a915003a 2019-02-05 stsp init_curses(void)
3515 a915003a 2019-02-05 stsp {
3516 296152d1 2022-05-31 thomas /*
3517 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3518 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3519 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3520 296152d1 2022-05-31 thomas */
3521 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3522 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3523 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3524 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3525 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3526 296152d1 2022-05-31 thomas
3527 a915003a 2019-02-05 stsp initscr();
3528 a915003a 2019-02-05 stsp cbreak();
3529 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3530 a915003a 2019-02-05 stsp noecho();
3531 a915003a 2019-02-05 stsp nonl();
3532 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3533 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3534 a915003a 2019-02-05 stsp curs_set(0);
3535 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3536 6d17833f 2019-11-08 stsp start_color();
3537 6d17833f 2019-11-08 stsp use_default_colors();
3538 6d17833f 2019-11-08 stsp }
3539 a915003a 2019-02-05 stsp }
3540 a915003a 2019-02-05 stsp
3541 c2db6724 2019-01-04 stsp static const struct got_error *
3542 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3543 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3544 f135c941 2020-02-20 stsp {
3545 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3546 f135c941 2020-02-20 stsp
3547 f135c941 2020-02-20 stsp if (argc == 0) {
3548 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3549 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3550 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3551 f135c941 2020-02-20 stsp return NULL;
3552 f135c941 2020-02-20 stsp }
3553 f135c941 2020-02-20 stsp
3554 f135c941 2020-02-20 stsp if (worktree) {
3555 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3556 bfd61697 2020-11-14 stsp char *p;
3557 f135c941 2020-02-20 stsp
3558 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3559 f135c941 2020-02-20 stsp if (err)
3560 f135c941 2020-02-20 stsp return err;
3561 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3562 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3563 bfd61697 2020-11-14 stsp p) == -1) {
3564 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3565 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3566 f135c941 2020-02-20 stsp }
3567 f135c941 2020-02-20 stsp free(p);
3568 f135c941 2020-02-20 stsp } else
3569 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3570 f135c941 2020-02-20 stsp
3571 f135c941 2020-02-20 stsp return err;
3572 f135c941 2020-02-20 stsp }
3573 f135c941 2020-02-20 stsp
3574 f135c941 2020-02-20 stsp static const struct got_error *
3575 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3576 9f7d7167 2018-04-29 stsp {
3577 80ddbec8 2018-04-29 stsp const struct got_error *error;
3578 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3579 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3580 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3581 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3582 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3583 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3584 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3585 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3586 04cc582a 2018-08-01 stsp struct tog_view *view;
3587 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3588 80ddbec8 2018-04-29 stsp
3589 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3590 80ddbec8 2018-04-29 stsp switch (ch) {
3591 b672a97a 2020-01-27 stsp case 'b':
3592 b672a97a 2020-01-27 stsp log_branches = 1;
3593 b672a97a 2020-01-27 stsp break;
3594 80ddbec8 2018-04-29 stsp case 'c':
3595 80ddbec8 2018-04-29 stsp start_commit = optarg;
3596 80ddbec8 2018-04-29 stsp break;
3597 ecb28ae0 2018-07-16 stsp case 'r':
3598 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3599 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3600 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3601 9ba1d308 2019-10-21 stsp optarg);
3602 ecb28ae0 2018-07-16 stsp break;
3603 80ddbec8 2018-04-29 stsp default:
3604 17020d27 2019-03-07 stsp usage_log();
3605 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3606 80ddbec8 2018-04-29 stsp }
3607 80ddbec8 2018-04-29 stsp }
3608 80ddbec8 2018-04-29 stsp
3609 80ddbec8 2018-04-29 stsp argc -= optind;
3610 80ddbec8 2018-04-29 stsp argv += optind;
3611 80ddbec8 2018-04-29 stsp
3612 f135c941 2020-02-20 stsp if (argc > 1)
3613 f135c941 2020-02-20 stsp usage_log();
3614 963f97a1 2019-03-18 stsp
3615 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3616 7cd52833 2022-06-23 thomas if (error != NULL)
3617 7cd52833 2022-06-23 thomas goto done;
3618 7cd52833 2022-06-23 thomas
3619 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3620 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3621 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3622 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3623 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3624 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3625 c156c7a4 2020-12-18 stsp goto done;
3626 a1fbf39a 2019-08-11 stsp if (worktree)
3627 6962eb72 2020-02-20 stsp repo_path =
3628 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3629 a1fbf39a 2019-08-11 stsp else
3630 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3631 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3632 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3633 c156c7a4 2020-12-18 stsp goto done;
3634 c156c7a4 2020-12-18 stsp }
3635 ecb28ae0 2018-07-16 stsp }
3636 ecb28ae0 2018-07-16 stsp
3637 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3638 80ddbec8 2018-04-29 stsp if (error != NULL)
3639 ecb28ae0 2018-07-16 stsp goto done;
3640 80ddbec8 2018-04-29 stsp
3641 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3642 f135c941 2020-02-20 stsp repo, worktree);
3643 f135c941 2020-02-20 stsp if (error)
3644 f135c941 2020-02-20 stsp goto done;
3645 f135c941 2020-02-20 stsp
3646 f135c941 2020-02-20 stsp init_curses();
3647 f135c941 2020-02-20 stsp
3648 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3649 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3650 c02c541e 2019-03-29 stsp if (error)
3651 c02c541e 2019-03-29 stsp goto done;
3652 c02c541e 2019-03-29 stsp
3653 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3654 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3655 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3656 87670572 2020-12-26 naddy if (error)
3657 87670572 2020-12-26 naddy goto done;
3658 87670572 2020-12-26 naddy }
3659 51a10b52 2020-12-26 stsp
3660 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3661 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3662 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3663 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3664 d8f38dc4 2020-12-05 stsp if (error)
3665 d8f38dc4 2020-12-05 stsp goto done;
3666 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3667 d8f38dc4 2020-12-05 stsp } else {
3668 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3669 d8f38dc4 2020-12-05 stsp if (error == NULL)
3670 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3671 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3672 d8f38dc4 2020-12-05 stsp goto done;
3673 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3674 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3675 d8f38dc4 2020-12-05 stsp if (error)
3676 d8f38dc4 2020-12-05 stsp goto done;
3677 d8f38dc4 2020-12-05 stsp }
3678 8b473291 2019-02-21 stsp
3679 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3680 04cc582a 2018-08-01 stsp if (view == NULL) {
3681 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3682 04cc582a 2018-08-01 stsp goto done;
3683 04cc582a 2018-08-01 stsp }
3684 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3685 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3686 ba4f502b 2018-08-04 stsp if (error)
3687 ba4f502b 2018-08-04 stsp goto done;
3688 2fc00ff4 2019-08-31 stsp if (worktree) {
3689 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3690 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3691 2fc00ff4 2019-08-31 stsp worktree = NULL;
3692 2fc00ff4 2019-08-31 stsp }
3693 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3694 ecb28ae0 2018-07-16 stsp done:
3695 f135c941 2020-02-20 stsp free(in_repo_path);
3696 ecb28ae0 2018-07-16 stsp free(repo_path);
3697 ecb28ae0 2018-07-16 stsp free(cwd);
3698 899d86c2 2018-05-10 stsp free(start_id);
3699 d8f38dc4 2020-12-05 stsp free(label);
3700 d8f38dc4 2020-12-05 stsp if (ref)
3701 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3702 1d0f4054 2021-06-17 stsp if (repo) {
3703 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3704 1d0f4054 2021-06-17 stsp if (error == NULL)
3705 1d0f4054 2021-06-17 stsp error = close_err;
3706 1d0f4054 2021-06-17 stsp }
3707 ec142235 2019-03-07 stsp if (worktree)
3708 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3709 7cd52833 2022-06-23 thomas if (pack_fds) {
3710 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3711 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3712 7cd52833 2022-06-23 thomas if (error == NULL)
3713 7cd52833 2022-06-23 thomas error = pack_err;
3714 7cd52833 2022-06-23 thomas }
3715 51a10b52 2020-12-26 stsp tog_free_refs();
3716 80ddbec8 2018-04-29 stsp return error;
3717 9f7d7167 2018-04-29 stsp }
3718 9f7d7167 2018-04-29 stsp
3719 4ed7e80c 2018-05-20 stsp __dead static void
3720 9f7d7167 2018-04-29 stsp usage_diff(void)
3721 9f7d7167 2018-04-29 stsp {
3722 80ddbec8 2018-04-29 stsp endwin();
3723 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3724 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3725 9f7d7167 2018-04-29 stsp exit(1);
3726 b304db33 2018-05-20 stsp }
3727 b304db33 2018-05-20 stsp
3728 6d17833f 2019-11-08 stsp static int
3729 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3730 41605754 2020-11-12 stsp regmatch_t *regmatch)
3731 6d17833f 2019-11-08 stsp {
3732 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3733 6d17833f 2019-11-08 stsp }
3734 6d17833f 2019-11-08 stsp
3735 ef20f542 2022-06-26 thomas static struct tog_color *
3736 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3737 6d17833f 2019-11-08 stsp {
3738 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3739 6d17833f 2019-11-08 stsp
3740 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3741 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3742 6d17833f 2019-11-08 stsp return tc;
3743 6d17833f 2019-11-08 stsp }
3744 6d17833f 2019-11-08 stsp
3745 6d17833f 2019-11-08 stsp return NULL;
3746 6d17833f 2019-11-08 stsp }
3747 6d17833f 2019-11-08 stsp
3748 4ed7e80c 2018-05-20 stsp static const struct got_error *
3749 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3750 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3751 41605754 2020-11-12 stsp {
3752 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3753 666f7b10 2022-06-23 thomas char *exstr = NULL;
3754 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3755 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3756 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3757 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3758 41605754 2020-11-12 stsp
3759 41605754 2020-11-12 stsp *wtotal = 0;
3760 cbea3800 2022-06-23 thomas
3761 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3762 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3763 41605754 2020-11-12 stsp
3764 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3765 666f7b10 2022-06-23 thomas if (err)
3766 666f7b10 2022-06-23 thomas return err;
3767 666f7b10 2022-06-23 thomas
3768 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3769 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3770 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3771 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3772 666f7b10 2022-06-23 thomas goto done;
3773 666f7b10 2022-06-23 thomas }
3774 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3775 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3776 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3777 cbea3800 2022-06-23 thomas goto done;
3778 cbea3800 2022-06-23 thomas }
3779 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3780 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3781 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3782 cbea3800 2022-06-23 thomas goto done;
3783 cbea3800 2022-06-23 thomas }
3784 05171be4 2022-06-23 thomas
3785 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3786 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3787 cbea3800 2022-06-23 thomas col_tab_align, 1);
3788 cbea3800 2022-06-23 thomas if (err)
3789 cbea3800 2022-06-23 thomas goto done;
3790 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3791 05171be4 2022-06-23 thomas if (n) {
3792 cbea3800 2022-06-23 thomas free(wline);
3793 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3794 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3795 cbea3800 2022-06-23 thomas if (err)
3796 cbea3800 2022-06-23 thomas goto done;
3797 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3798 cbea3800 2022-06-23 thomas wlimit -= width;
3799 cbea3800 2022-06-23 thomas *wtotal += width;
3800 41605754 2020-11-12 stsp }
3801 41605754 2020-11-12 stsp
3802 41605754 2020-11-12 stsp if (wlimit > 0) {
3803 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3804 cbea3800 2022-06-23 thomas size_t wlen;
3805 cbea3800 2022-06-23 thomas
3806 cbea3800 2022-06-23 thomas free(wline);
3807 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3808 cbea3800 2022-06-23 thomas col_tab_align, 1);
3809 cbea3800 2022-06-23 thomas if (err)
3810 cbea3800 2022-06-23 thomas goto done;
3811 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3812 cbea3800 2022-06-23 thomas while (i < wlen) {
3813 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3814 cbea3800 2022-06-23 thomas if (width == -1) {
3815 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3816 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3817 cbea3800 2022-06-23 thomas goto done;
3818 cbea3800 2022-06-23 thomas }
3819 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3820 cbea3800 2022-06-23 thomas break;
3821 1c5e5faa 2022-06-23 thomas w += width;
3822 cbea3800 2022-06-23 thomas i++;
3823 41605754 2020-11-12 stsp }
3824 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3825 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3826 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3827 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3828 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3829 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3830 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3831 41605754 2020-11-12 stsp }
3832 41605754 2020-11-12 stsp }
3833 41605754 2020-11-12 stsp
3834 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3835 cbea3800 2022-06-23 thomas free(wline);
3836 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3837 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3838 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3839 cbea3800 2022-06-23 thomas col_tab_align, 1);
3840 cbea3800 2022-06-23 thomas if (err)
3841 cbea3800 2022-06-23 thomas goto done;
3842 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3843 cbea3800 2022-06-23 thomas } else {
3844 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3845 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3846 cbea3800 2022-06-23 thomas if (err)
3847 cbea3800 2022-06-23 thomas goto done;
3848 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3849 cbea3800 2022-06-23 thomas }
3850 cbea3800 2022-06-23 thomas *wtotal += width2;
3851 41605754 2020-11-12 stsp }
3852 cbea3800 2022-06-23 thomas done:
3853 05171be4 2022-06-23 thomas free(wline);
3854 666f7b10 2022-06-23 thomas free(exstr);
3855 cbea3800 2022-06-23 thomas free(seg0);
3856 cbea3800 2022-06-23 thomas free(seg1);
3857 cbea3800 2022-06-23 thomas free(seg2);
3858 cbea3800 2022-06-23 thomas return err;
3859 41605754 2020-11-12 stsp }
3860 41605754 2020-11-12 stsp
3861 41605754 2020-11-12 stsp static const struct got_error *
3862 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3863 26ed57b2 2018-05-19 stsp {
3864 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3865 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3866 61e69b96 2018-05-20 stsp const struct got_error *err;
3867 fe621944 2020-11-10 stsp int nprinted = 0;
3868 b304db33 2018-05-20 stsp char *line;
3869 826082fe 2020-12-10 stsp size_t linesize = 0;
3870 826082fe 2020-12-10 stsp ssize_t linelen;
3871 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3872 61e69b96 2018-05-20 stsp wchar_t *wline;
3873 e0b650dd 2018-05-20 stsp int width;
3874 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3875 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3876 fe621944 2020-11-10 stsp off_t line_offset;
3877 26ed57b2 2018-05-19 stsp
3878 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3879 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3880 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3881 fe621944 2020-11-10 stsp
3882 f7d12f7e 2018-08-01 stsp werase(view->window);
3883 a3404814 2018-09-02 stsp
3884 a3404814 2018-09-02 stsp if (header) {
3885 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3886 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3887 135a2da0 2020-11-11 stsp header) == -1)
3888 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3889 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3890 f91a2b48 2022-06-23 thomas 0, 0);
3891 135a2da0 2020-11-11 stsp free(line);
3892 135a2da0 2020-11-11 stsp if (err)
3893 a3404814 2018-09-02 stsp return err;
3894 a3404814 2018-09-02 stsp
3895 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3896 a3404814 2018-09-02 stsp wstandout(view->window);
3897 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3898 e54cc94a 2020-11-11 stsp free(wline);
3899 e54cc94a 2020-11-11 stsp wline = NULL;
3900 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3901 a3404814 2018-09-02 stsp wstandend(view->window);
3902 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3903 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3904 26ed57b2 2018-05-19 stsp
3905 a3404814 2018-09-02 stsp if (max_lines <= 1)
3906 a3404814 2018-09-02 stsp return NULL;
3907 a3404814 2018-09-02 stsp max_lines--;
3908 a3404814 2018-09-02 stsp }
3909 a3404814 2018-09-02 stsp
3910 89f1a395 2020-12-01 naddy s->eof = 0;
3911 05171be4 2022-06-23 thomas view->maxx = 0;
3912 826082fe 2020-12-10 stsp line = NULL;
3913 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3914 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3915 826082fe 2020-12-10 stsp if (linelen == -1) {
3916 826082fe 2020-12-10 stsp if (feof(s->f)) {
3917 826082fe 2020-12-10 stsp s->eof = 1;
3918 826082fe 2020-12-10 stsp break;
3919 826082fe 2020-12-10 stsp }
3920 826082fe 2020-12-10 stsp free(line);
3921 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3922 61e69b96 2018-05-20 stsp }
3923 05171be4 2022-06-23 thomas
3924 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3925 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3926 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3927 cbea3800 2022-06-23 thomas if (err) {
3928 cbea3800 2022-06-23 thomas free(line);
3929 cbea3800 2022-06-23 thomas return err;
3930 cbea3800 2022-06-23 thomas }
3931 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3932 cbea3800 2022-06-23 thomas free(wline);
3933 cbea3800 2022-06-23 thomas wline = NULL;
3934 cbea3800 2022-06-23 thomas
3935 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3936 f26dddb7 2019-11-08 stsp if (tc)
3937 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3938 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3939 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3940 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3941 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3942 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3943 41605754 2020-11-12 stsp if (err) {
3944 41605754 2020-11-12 stsp free(line);
3945 41605754 2020-11-12 stsp return err;
3946 41605754 2020-11-12 stsp }
3947 41605754 2020-11-12 stsp } else {
3948 f1c0ec19 2022-06-23 thomas int skip;
3949 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3950 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3951 f1c0ec19 2022-06-23 thomas if (err) {
3952 f1c0ec19 2022-06-23 thomas free(line);
3953 f1c0ec19 2022-06-23 thomas return err;
3954 f1c0ec19 2022-06-23 thomas }
3955 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3956 f1c0ec19 2022-06-23 thomas free(wline);
3957 41605754 2020-11-12 stsp wline = NULL;
3958 41605754 2020-11-12 stsp }
3959 f26dddb7 2019-11-08 stsp if (tc)
3960 6d17833f 2019-11-08 stsp wattr_off(view->window,
3961 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3962 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3963 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3964 fe621944 2020-11-10 stsp nprinted++;
3965 826082fe 2020-12-10 stsp }
3966 826082fe 2020-12-10 stsp free(line);
3967 fe621944 2020-11-10 stsp if (nprinted >= 1)
3968 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3969 89f1a395 2020-12-01 naddy (nprinted - 1);
3970 fe621944 2020-11-10 stsp else
3971 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3972 26ed57b2 2018-05-19 stsp
3973 a5d43cac 2022-07-01 thomas view_border(view);
3974 c3e9aa98 2019-05-13 jcs
3975 89f1a395 2020-12-01 naddy if (s->eof) {
3976 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3977 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3978 c3e9aa98 2019-05-13 jcs nprinted++;
3979 c3e9aa98 2019-05-13 jcs }
3980 c3e9aa98 2019-05-13 jcs
3981 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3982 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3983 c3e9aa98 2019-05-13 jcs if (err) {
3984 c3e9aa98 2019-05-13 jcs return err;
3985 c3e9aa98 2019-05-13 jcs }
3986 26ed57b2 2018-05-19 stsp
3987 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3988 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3989 e54cc94a 2020-11-11 stsp free(wline);
3990 e54cc94a 2020-11-11 stsp wline = NULL;
3991 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3992 c3e9aa98 2019-05-13 jcs }
3993 c3e9aa98 2019-05-13 jcs
3994 26ed57b2 2018-05-19 stsp return NULL;
3995 abd2672a 2018-12-23 stsp }
3996 abd2672a 2018-12-23 stsp
3997 abd2672a 2018-12-23 stsp static char *
3998 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3999 abd2672a 2018-12-23 stsp {
4000 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4001 09867e48 2019-08-13 stsp char *p, *s;
4002 09867e48 2019-08-13 stsp
4003 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4004 09867e48 2019-08-13 stsp if (tm == NULL)
4005 09867e48 2019-08-13 stsp return NULL;
4006 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4007 09867e48 2019-08-13 stsp if (s == NULL)
4008 09867e48 2019-08-13 stsp return NULL;
4009 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4010 abd2672a 2018-12-23 stsp if (p)
4011 abd2672a 2018-12-23 stsp *p = '\0';
4012 abd2672a 2018-12-23 stsp return s;
4013 9f7d7167 2018-04-29 stsp }
4014 9f7d7167 2018-04-29 stsp
4015 4ed7e80c 2018-05-20 stsp static const struct got_error *
4016 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4017 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4018 0208f208 2020-05-05 stsp {
4019 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4020 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4021 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4022 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4023 0208f208 2020-05-05 stsp
4024 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4025 0208f208 2020-05-05 stsp if (qid != NULL) {
4026 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4027 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4028 ec242592 2022-04-22 thomas &qid->id);
4029 0208f208 2020-05-05 stsp if (err)
4030 0208f208 2020-05-05 stsp return err;
4031 0208f208 2020-05-05 stsp
4032 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4033 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4034 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4035 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4036 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4037 aa8b5dd0 2021-08-01 stsp }
4038 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4039 0208f208 2020-05-05 stsp
4040 0208f208 2020-05-05 stsp }
4041 0208f208 2020-05-05 stsp
4042 0208f208 2020-05-05 stsp if (tree_id1) {
4043 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4044 0208f208 2020-05-05 stsp if (err)
4045 0208f208 2020-05-05 stsp goto done;
4046 0208f208 2020-05-05 stsp }
4047 0208f208 2020-05-05 stsp
4048 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4049 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4050 0208f208 2020-05-05 stsp if (err)
4051 0208f208 2020-05-05 stsp goto done;
4052 0208f208 2020-05-05 stsp
4053 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4054 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4055 0208f208 2020-05-05 stsp done:
4056 0208f208 2020-05-05 stsp if (tree1)
4057 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4058 0208f208 2020-05-05 stsp if (tree2)
4059 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4060 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4061 0208f208 2020-05-05 stsp return err;
4062 0208f208 2020-05-05 stsp }
4063 0208f208 2020-05-05 stsp
4064 0208f208 2020-05-05 stsp static const struct got_error *
4065 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4066 abd2672a 2018-12-23 stsp {
4067 fe621944 2020-11-10 stsp off_t *p;
4068 fe621944 2020-11-10 stsp
4069 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4070 fe621944 2020-11-10 stsp if (p == NULL)
4071 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4072 fe621944 2020-11-10 stsp *line_offsets = p;
4073 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4074 fe621944 2020-11-10 stsp (*nlines)++;
4075 fe621944 2020-11-10 stsp return NULL;
4076 fe621944 2020-11-10 stsp }
4077 fe621944 2020-11-10 stsp
4078 fe621944 2020-11-10 stsp static const struct got_error *
4079 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4080 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4081 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4082 fe621944 2020-11-10 stsp {
4083 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4084 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4085 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4086 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4087 45d799e2 2018-12-23 stsp time_t committer_time;
4088 45d799e2 2018-12-23 stsp const char *author, *committer;
4089 8b473291 2019-02-21 stsp char *refs_str = NULL;
4090 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4091 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4092 fe621944 2020-11-10 stsp off_t outoff = 0;
4093 fe621944 2020-11-10 stsp int n;
4094 abd2672a 2018-12-23 stsp
4095 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4096 0208f208 2020-05-05 stsp
4097 8b473291 2019-02-21 stsp if (refs) {
4098 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4099 8b473291 2019-02-21 stsp if (err)
4100 8b473291 2019-02-21 stsp return err;
4101 8b473291 2019-02-21 stsp }
4102 8b473291 2019-02-21 stsp
4103 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4104 abd2672a 2018-12-23 stsp if (err)
4105 abd2672a 2018-12-23 stsp return err;
4106 abd2672a 2018-12-23 stsp
4107 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4108 15a94983 2018-12-23 stsp if (err) {
4109 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4110 15a94983 2018-12-23 stsp goto done;
4111 15a94983 2018-12-23 stsp }
4112 abd2672a 2018-12-23 stsp
4113 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4114 fe621944 2020-11-10 stsp if (err)
4115 fe621944 2020-11-10 stsp goto done;
4116 fe621944 2020-11-10 stsp
4117 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4118 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4119 fe621944 2020-11-10 stsp if (n < 0) {
4120 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4121 abd2672a 2018-12-23 stsp goto done;
4122 abd2672a 2018-12-23 stsp }
4123 fe621944 2020-11-10 stsp outoff += n;
4124 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4125 fe621944 2020-11-10 stsp if (err)
4126 fe621944 2020-11-10 stsp goto done;
4127 fe621944 2020-11-10 stsp
4128 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4129 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4130 fe621944 2020-11-10 stsp if (n < 0) {
4131 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4132 abd2672a 2018-12-23 stsp goto done;
4133 abd2672a 2018-12-23 stsp }
4134 fe621944 2020-11-10 stsp outoff += n;
4135 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4136 fe621944 2020-11-10 stsp if (err)
4137 fe621944 2020-11-10 stsp goto done;
4138 fe621944 2020-11-10 stsp
4139 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4140 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4141 fe621944 2020-11-10 stsp if (datestr) {
4142 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4143 fe621944 2020-11-10 stsp if (n < 0) {
4144 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4145 fe621944 2020-11-10 stsp goto done;
4146 fe621944 2020-11-10 stsp }
4147 fe621944 2020-11-10 stsp outoff += n;
4148 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4149 fe621944 2020-11-10 stsp if (err)
4150 fe621944 2020-11-10 stsp goto done;
4151 abd2672a 2018-12-23 stsp }
4152 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4153 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4154 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4155 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4156 fe621944 2020-11-10 stsp if (n < 0) {
4157 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4158 fe621944 2020-11-10 stsp goto done;
4159 fe621944 2020-11-10 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 abd2672a 2018-12-23 stsp }
4165 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4166 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4167 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4168 ac4dc263 2021-09-24 thomas int pn = 1;
4169 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4170 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4171 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4172 ac4dc263 2021-09-24 thomas if (err)
4173 ac4dc263 2021-09-24 thomas goto done;
4174 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4175 ac4dc263 2021-09-24 thomas if (n < 0) {
4176 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4177 ac4dc263 2021-09-24 thomas goto done;
4178 ac4dc263 2021-09-24 thomas }
4179 ac4dc263 2021-09-24 thomas outoff += n;
4180 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4181 ac4dc263 2021-09-24 thomas if (err)
4182 ac4dc263 2021-09-24 thomas goto done;
4183 ac4dc263 2021-09-24 thomas free(id_str);
4184 ac4dc263 2021-09-24 thomas id_str = NULL;
4185 ac4dc263 2021-09-24 thomas }
4186 ac4dc263 2021-09-24 thomas }
4187 ac4dc263 2021-09-24 thomas
4188 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4189 5943eee2 2019-08-13 stsp if (err)
4190 5943eee2 2019-08-13 stsp goto done;
4191 fe621944 2020-11-10 stsp s = logmsg;
4192 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4193 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4194 fe621944 2020-11-10 stsp if (n < 0) {
4195 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4196 fe621944 2020-11-10 stsp goto done;
4197 fe621944 2020-11-10 stsp }
4198 fe621944 2020-11-10 stsp outoff += n;
4199 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4200 fe621944 2020-11-10 stsp if (err)
4201 fe621944 2020-11-10 stsp goto done;
4202 abd2672a 2018-12-23 stsp }
4203 fe621944 2020-11-10 stsp
4204 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4205 0208f208 2020-05-05 stsp if (err)
4206 0208f208 2020-05-05 stsp goto done;
4207 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4208 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4209 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4210 fe621944 2020-11-10 stsp if (n < 0) {
4211 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4212 fe621944 2020-11-10 stsp goto done;
4213 fe621944 2020-11-10 stsp }
4214 fe621944 2020-11-10 stsp outoff += n;
4215 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4216 fe621944 2020-11-10 stsp if (err)
4217 fe621944 2020-11-10 stsp goto done;
4218 0208f208 2020-05-05 stsp free((char *)pe->path);
4219 0208f208 2020-05-05 stsp free(pe->data);
4220 0208f208 2020-05-05 stsp }
4221 fe621944 2020-11-10 stsp
4222 0208f208 2020-05-05 stsp fputc('\n', outfile);
4223 fe621944 2020-11-10 stsp outoff++;
4224 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4225 abd2672a 2018-12-23 stsp done:
4226 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4227 abd2672a 2018-12-23 stsp free(id_str);
4228 5943eee2 2019-08-13 stsp free(logmsg);
4229 8b473291 2019-02-21 stsp free(refs_str);
4230 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4231 7510f233 2020-08-09 stsp if (err) {
4232 ae6a6978 2020-08-09 stsp free(*line_offsets);
4233 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4234 ae6a6978 2020-08-09 stsp *nlines = 0;
4235 7510f233 2020-08-09 stsp }
4236 fe621944 2020-11-10 stsp return err;
4237 abd2672a 2018-12-23 stsp }
4238 abd2672a 2018-12-23 stsp
4239 abd2672a 2018-12-23 stsp static const struct got_error *
4240 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4241 26ed57b2 2018-05-19 stsp {
4242 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4243 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4244 15a94983 2018-12-23 stsp int obj_type;
4245 fe621944 2020-11-10 stsp
4246 fe621944 2020-11-10 stsp free(s->line_offsets);
4247 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4248 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4249 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4250 fe621944 2020-11-10 stsp s->nlines = 0;
4251 26ed57b2 2018-05-19 stsp
4252 511a516b 2018-05-19 stsp f = got_opentemp();
4253 48ae06ee 2018-10-18 stsp if (f == NULL) {
4254 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4255 48ae06ee 2018-10-18 stsp goto done;
4256 48ae06ee 2018-10-18 stsp }
4257 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4258 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4259 fb43ecf1 2019-02-11 stsp goto done;
4260 fb43ecf1 2019-02-11 stsp }
4261 48ae06ee 2018-10-18 stsp s->f = f;
4262 26ed57b2 2018-05-19 stsp
4263 15a94983 2018-12-23 stsp if (s->id1)
4264 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4265 15a94983 2018-12-23 stsp else
4266 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4267 15a94983 2018-12-23 stsp if (err)
4268 15a94983 2018-12-23 stsp goto done;
4269 15a94983 2018-12-23 stsp
4270 15a94983 2018-12-23 stsp switch (obj_type) {
4271 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4272 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4273 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4274 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4275 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4276 26ed57b2 2018-05-19 stsp break;
4277 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4278 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4279 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4280 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4281 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4282 26ed57b2 2018-05-19 stsp break;
4283 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4284 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4285 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4286 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4287 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4288 abd2672a 2018-12-23 stsp
4289 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4290 abd2672a 2018-12-23 stsp if (err)
4291 3ffacbe1 2020-02-02 tracey goto done;
4292 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4293 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4294 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4295 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4296 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4297 f44b1f58 2020-02-02 tracey if (err)
4298 f44b1f58 2020-02-02 tracey goto done;
4299 f44b1f58 2020-02-02 tracey } else {
4300 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4301 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4302 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4303 fe621944 2020-11-10 stsp err = write_commit_info(
4304 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4305 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4306 f44b1f58 2020-02-02 tracey if (err)
4307 f44b1f58 2020-02-02 tracey goto done;
4308 f5404e4e 2020-02-02 tracey break;
4309 15a087fe 2019-02-21 stsp }
4310 abd2672a 2018-12-23 stsp }
4311 abd2672a 2018-12-23 stsp }
4312 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4313 abd2672a 2018-12-23 stsp
4314 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4315 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4316 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4317 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4318 26ed57b2 2018-05-19 stsp break;
4319 abd2672a 2018-12-23 stsp }
4320 26ed57b2 2018-05-19 stsp default:
4321 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4322 48ae06ee 2018-10-18 stsp break;
4323 26ed57b2 2018-05-19 stsp }
4324 f44b1f58 2020-02-02 tracey if (err)
4325 f44b1f58 2020-02-02 tracey goto done;
4326 48ae06ee 2018-10-18 stsp done:
4327 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4328 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4329 48ae06ee 2018-10-18 stsp return err;
4330 48ae06ee 2018-10-18 stsp }
4331 26ed57b2 2018-05-19 stsp
4332 f5215bb9 2019-02-22 stsp static void
4333 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4334 f5215bb9 2019-02-22 stsp {
4335 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4336 f5215bb9 2019-02-22 stsp update_panels();
4337 f5215bb9 2019-02-22 stsp doupdate();
4338 f44b1f58 2020-02-02 tracey }
4339 f44b1f58 2020-02-02 tracey
4340 f44b1f58 2020-02-02 tracey static const struct got_error *
4341 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4342 f44b1f58 2020-02-02 tracey {
4343 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4344 f44b1f58 2020-02-02 tracey
4345 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4346 f44b1f58 2020-02-02 tracey return NULL;
4347 f44b1f58 2020-02-02 tracey }
4348 f44b1f58 2020-02-02 tracey
4349 f44b1f58 2020-02-02 tracey static const struct got_error *
4350 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4351 f44b1f58 2020-02-02 tracey {
4352 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4353 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4354 f44b1f58 2020-02-02 tracey int lineno;
4355 78eecffc 2022-06-23 thomas char *line = NULL;
4356 826082fe 2020-12-10 stsp size_t linesize = 0;
4357 826082fe 2020-12-10 stsp ssize_t linelen;
4358 f44b1f58 2020-02-02 tracey
4359 f44b1f58 2020-02-02 tracey if (!view->searching) {
4360 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4361 f44b1f58 2020-02-02 tracey return NULL;
4362 f44b1f58 2020-02-02 tracey }
4363 f44b1f58 2020-02-02 tracey
4364 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4365 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4366 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4367 f44b1f58 2020-02-02 tracey else
4368 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4369 4b3f9dac 2021-12-17 thomas } else
4370 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4371 f44b1f58 2020-02-02 tracey
4372 f44b1f58 2020-02-02 tracey while (1) {
4373 f44b1f58 2020-02-02 tracey off_t offset;
4374 f44b1f58 2020-02-02 tracey
4375 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4376 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4377 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4378 f44b1f58 2020-02-02 tracey break;
4379 f44b1f58 2020-02-02 tracey }
4380 f44b1f58 2020-02-02 tracey
4381 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4382 f44b1f58 2020-02-02 tracey lineno = 1;
4383 f44b1f58 2020-02-02 tracey else
4384 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4385 f44b1f58 2020-02-02 tracey }
4386 f44b1f58 2020-02-02 tracey
4387 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4388 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4389 f44b1f58 2020-02-02 tracey free(line);
4390 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4391 f44b1f58 2020-02-02 tracey }
4392 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4393 78eecffc 2022-06-23 thomas if (linelen != -1) {
4394 78eecffc 2022-06-23 thomas char *exstr;
4395 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4396 78eecffc 2022-06-23 thomas if (err)
4397 78eecffc 2022-06-23 thomas break;
4398 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4399 78eecffc 2022-06-23 thomas &view->regmatch)) {
4400 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4402 78eecffc 2022-06-23 thomas free(exstr);
4403 78eecffc 2022-06-23 thomas break;
4404 78eecffc 2022-06-23 thomas }
4405 78eecffc 2022-06-23 thomas free(exstr);
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++;
4409 f44b1f58 2020-02-02 tracey else
4410 f44b1f58 2020-02-02 tracey lineno--;
4411 f44b1f58 2020-02-02 tracey }
4412 826082fe 2020-12-10 stsp free(line);
4413 f44b1f58 2020-02-02 tracey
4414 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4415 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4416 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4417 f44b1f58 2020-02-02 tracey }
4418 f44b1f58 2020-02-02 tracey
4419 05171be4 2022-06-23 thomas return err;
4420 f5215bb9 2019-02-22 stsp }
4421 f5215bb9 2019-02-22 stsp
4422 48ae06ee 2018-10-18 stsp static const struct got_error *
4423 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4424 a0f32f33 2022-06-13 thomas {
4425 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4426 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4427 a0f32f33 2022-06-13 thomas
4428 a0f32f33 2022-06-13 thomas free(s->id1);
4429 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4430 a0f32f33 2022-06-13 thomas free(s->id2);
4431 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4432 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4433 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4434 a0f32f33 2022-06-13 thomas s->f = NULL;
4435 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4436 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4437 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4438 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4439 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4440 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4441 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4442 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4443 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4444 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4445 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4446 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4447 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4448 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4449 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4450 a0f32f33 2022-06-13 thomas s->nlines = 0;
4451 a0f32f33 2022-06-13 thomas return err;
4452 a0f32f33 2022-06-13 thomas }
4453 a0f32f33 2022-06-13 thomas
4454 a0f32f33 2022-06-13 thomas static const struct got_error *
4455 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4456 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4457 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4458 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4459 48ae06ee 2018-10-18 stsp {
4460 48ae06ee 2018-10-18 stsp const struct got_error *err;
4461 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4462 5dc9f4bc 2018-08-04 stsp
4463 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4464 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4465 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4466 a0f32f33 2022-06-13 thomas
4467 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4468 15a94983 2018-12-23 stsp int type1, type2;
4469 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4470 15a94983 2018-12-23 stsp if (err)
4471 15a94983 2018-12-23 stsp return err;
4472 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4473 15a94983 2018-12-23 stsp if (err)
4474 15a94983 2018-12-23 stsp return err;
4475 15a94983 2018-12-23 stsp
4476 15a94983 2018-12-23 stsp if (type1 != type2)
4477 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4478 15a94983 2018-12-23 stsp }
4479 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4480 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4481 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4482 f44b1f58 2020-02-02 tracey s->repo = repo;
4483 f44b1f58 2020-02-02 tracey s->id1 = id1;
4484 f44b1f58 2020-02-02 tracey s->id2 = id2;
4485 3dbaef42 2020-11-24 stsp s->label1 = label1;
4486 3dbaef42 2020-11-24 stsp s->label2 = label2;
4487 48ae06ee 2018-10-18 stsp
4488 15a94983 2018-12-23 stsp if (id1) {
4489 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4490 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4491 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4492 48ae06ee 2018-10-18 stsp } else
4493 5465d566 2020-02-01 tracey s->id1 = NULL;
4494 48ae06ee 2018-10-18 stsp
4495 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4496 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4497 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4498 a0f32f33 2022-06-13 thomas goto done;
4499 48ae06ee 2018-10-18 stsp }
4500 a0f32f33 2022-06-13 thomas
4501 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4502 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4503 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4504 9a1d7435 2022-06-23 thomas goto done;
4505 9a1d7435 2022-06-23 thomas }
4506 9a1d7435 2022-06-23 thomas
4507 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4508 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4509 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4510 a0f32f33 2022-06-13 thomas goto done;
4511 a0f32f33 2022-06-13 thomas }
4512 a0f32f33 2022-06-13 thomas
4513 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4514 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4515 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4516 19a6a6b5 2022-07-01 thomas goto done;
4517 19a6a6b5 2022-07-01 thomas }
4518 19a6a6b5 2022-07-01 thomas
4519 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4520 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4521 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4522 19a6a6b5 2022-07-01 thomas goto done;
4523 19a6a6b5 2022-07-01 thomas }
4524 19a6a6b5 2022-07-01 thomas
4525 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4526 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4527 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4528 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4529 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4530 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4531 5465d566 2020-02-01 tracey s->repo = repo;
4532 6d17833f 2019-11-08 stsp
4533 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4534 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4535 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4536 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4537 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4538 6d17833f 2019-11-08 stsp if (err)
4539 a0f32f33 2022-06-13 thomas goto done;
4540 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4541 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4543 a0f32f33 2022-06-13 thomas if (err)
4544 a0f32f33 2022-06-13 thomas goto done;
4545 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4546 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4547 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4548 a0f32f33 2022-06-13 thomas if (err)
4549 a0f32f33 2022-06-13 thomas goto done;
4550 6d17833f 2019-11-08 stsp
4551 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4552 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4553 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4554 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4555 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4556 a0f32f33 2022-06-13 thomas if (err)
4557 a0f32f33 2022-06-13 thomas goto done;
4558 11b20872 2019-11-08 stsp
4559 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4560 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4561 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4562 a0f32f33 2022-06-13 thomas if (err)
4563 a0f32f33 2022-06-13 thomas goto done;
4564 11b20872 2019-11-08 stsp
4565 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4566 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4567 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4568 a0f32f33 2022-06-13 thomas if (err)
4569 a0f32f33 2022-06-13 thomas goto done;
4570 6d17833f 2019-11-08 stsp }
4571 5dc9f4bc 2018-08-04 stsp
4572 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4573 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4574 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4575 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4576 f5215bb9 2019-02-22 stsp
4577 5465d566 2020-02-01 tracey err = create_diff(s);
4578 48ae06ee 2018-10-18 stsp
4579 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4580 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4581 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4582 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4583 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4584 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4585 a0f32f33 2022-06-13 thomas done:
4586 a0f32f33 2022-06-13 thomas if (err)
4587 a0f32f33 2022-06-13 thomas close_diff_view(view);
4588 e5a0f69f 2018-08-18 stsp return err;
4589 5dc9f4bc 2018-08-04 stsp }
4590 5dc9f4bc 2018-08-04 stsp
4591 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4592 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4593 5dc9f4bc 2018-08-04 stsp {
4594 a3404814 2018-09-02 stsp const struct got_error *err;
4595 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4596 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4597 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4598 a3404814 2018-09-02 stsp
4599 a3404814 2018-09-02 stsp if (s->id1) {
4600 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4601 a3404814 2018-09-02 stsp if (err)
4602 a3404814 2018-09-02 stsp return err;
4603 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4604 3dbaef42 2020-11-24 stsp } else
4605 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4606 3dbaef42 2020-11-24 stsp
4607 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4608 a3404814 2018-09-02 stsp if (err)
4609 a3404814 2018-09-02 stsp return err;
4610 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4611 26ed57b2 2018-05-19 stsp
4612 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4613 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4614 a3404814 2018-09-02 stsp free(id_str1);
4615 a3404814 2018-09-02 stsp free(id_str2);
4616 a3404814 2018-09-02 stsp return err;
4617 a3404814 2018-09-02 stsp }
4618 a3404814 2018-09-02 stsp free(id_str1);
4619 a3404814 2018-09-02 stsp free(id_str2);
4620 a3404814 2018-09-02 stsp
4621 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4622 267bb3b8 2021-08-01 stsp free(header);
4623 267bb3b8 2021-08-01 stsp return err;
4624 15a087fe 2019-02-21 stsp }
4625 15a087fe 2019-02-21 stsp
4626 15a087fe 2019-02-21 stsp static const struct got_error *
4627 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4628 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4629 15a087fe 2019-02-21 stsp {
4630 d7a04538 2019-02-21 stsp const struct got_error *err;
4631 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4632 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4633 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4634 15a087fe 2019-02-21 stsp
4635 15a087fe 2019-02-21 stsp free(s->id2);
4636 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4637 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4638 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4639 15a087fe 2019-02-21 stsp
4640 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4641 d7a04538 2019-02-21 stsp if (err)
4642 d7a04538 2019-02-21 stsp return err;
4643 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4644 15a087fe 2019-02-21 stsp free(s->id1);
4645 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4646 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4647 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4648 15a087fe 2019-02-21 stsp return NULL;
4649 0cf4efb1 2018-09-29 stsp }
4650 0cf4efb1 2018-09-29 stsp
4651 0cf4efb1 2018-09-29 stsp static const struct got_error *
4652 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4653 adf4c9e0 2022-07-03 thomas {
4654 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4655 adf4c9e0 2022-07-03 thomas
4656 adf4c9e0 2022-07-03 thomas view->count = 0;
4657 adf4c9e0 2022-07-03 thomas wclear(view->window);
4658 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4659 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4660 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4661 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4662 adf4c9e0 2022-07-03 thomas return create_diff(s);
4663 adf4c9e0 2022-07-03 thomas }
4664 adf4c9e0 2022-07-03 thomas
4665 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4666 4fc71f3b 2022-07-12 thomas int, int, int);
4667 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4668 4fc71f3b 2022-07-12 thomas int, int);
4669 4fc71f3b 2022-07-12 thomas
4670 adf4c9e0 2022-07-03 thomas static const struct got_error *
4671 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4672 e5a0f69f 2018-08-18 stsp {
4673 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4674 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4675 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4676 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4677 826082fe 2020-12-10 stsp char *line = NULL;
4678 826082fe 2020-12-10 stsp size_t linesize = 0;
4679 826082fe 2020-12-10 stsp ssize_t linelen;
4680 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4681 e5a0f69f 2018-08-18 stsp
4682 e5a0f69f 2018-08-18 stsp switch (ch) {
4683 05171be4 2022-06-23 thomas case '0':
4684 05171be4 2022-06-23 thomas view->x = 0;
4685 05171be4 2022-06-23 thomas break;
4686 05171be4 2022-06-23 thomas case '$':
4687 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4688 07b0611c 2022-06-23 thomas view->count = 0;
4689 05171be4 2022-06-23 thomas break;
4690 05171be4 2022-06-23 thomas case KEY_RIGHT:
4691 05171be4 2022-06-23 thomas case 'l':
4692 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4693 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4694 07b0611c 2022-06-23 thomas else
4695 07b0611c 2022-06-23 thomas view->count = 0;
4696 05171be4 2022-06-23 thomas break;
4697 05171be4 2022-06-23 thomas case KEY_LEFT:
4698 05171be4 2022-06-23 thomas case 'h':
4699 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4700 07b0611c 2022-06-23 thomas if (view->x <= 0)
4701 07b0611c 2022-06-23 thomas view->count = 0;
4702 05171be4 2022-06-23 thomas break;
4703 64453f7e 2020-11-21 stsp case 'a':
4704 3dbaef42 2020-11-24 stsp case 'w':
4705 3dbaef42 2020-11-24 stsp if (ch == 'a')
4706 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4707 3dbaef42 2020-11-24 stsp if (ch == 'w')
4708 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4709 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4710 912a3f79 2021-08-30 j break;
4711 912a3f79 2021-08-30 j case 'g':
4712 912a3f79 2021-08-30 j case KEY_HOME:
4713 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4714 07b0611c 2022-06-23 thomas view->count = 0;
4715 64453f7e 2020-11-21 stsp break;
4716 912a3f79 2021-08-30 j case 'G':
4717 912a3f79 2021-08-30 j case KEY_END:
4718 07b0611c 2022-06-23 thomas view->count = 0;
4719 912a3f79 2021-08-30 j if (s->eof)
4720 912a3f79 2021-08-30 j break;
4721 912a3f79 2021-08-30 j
4722 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4723 912a3f79 2021-08-30 j s->eof = 1;
4724 912a3f79 2021-08-30 j break;
4725 1e37a5c2 2019-05-12 jcs case 'k':
4726 1e37a5c2 2019-05-12 jcs case KEY_UP:
4727 f7140bf5 2021-10-17 thomas case CTRL('p'):
4728 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4729 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4730 07b0611c 2022-06-23 thomas else
4731 07b0611c 2022-06-23 thomas view->count = 0;
4732 1e37a5c2 2019-05-12 jcs break;
4733 70f17a53 2022-06-13 thomas case CTRL('u'):
4734 23427b14 2022-06-23 thomas case 'u':
4735 70f17a53 2022-06-13 thomas nscroll /= 2;
4736 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4737 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4738 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4739 1c5e5faa 2022-06-23 thomas case 'b':
4740 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4741 07b0611c 2022-06-23 thomas view->count = 0;
4742 26ed57b2 2018-05-19 stsp break;
4743 07b0611c 2022-06-23 thomas }
4744 1e37a5c2 2019-05-12 jcs i = 0;
4745 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4746 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4747 1e37a5c2 2019-05-12 jcs break;
4748 1e37a5c2 2019-05-12 jcs case 'j':
4749 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4750 f7140bf5 2021-10-17 thomas case CTRL('n'):
4751 1e37a5c2 2019-05-12 jcs if (!s->eof)
4752 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4753 07b0611c 2022-06-23 thomas else
4754 07b0611c 2022-06-23 thomas view->count = 0;
4755 1e37a5c2 2019-05-12 jcs break;
4756 70f17a53 2022-06-13 thomas case CTRL('d'):
4757 23427b14 2022-06-23 thomas case 'd':
4758 70f17a53 2022-06-13 thomas nscroll /= 2;
4759 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4760 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4761 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4762 1c5e5faa 2022-06-23 thomas case 'f':
4763 1e37a5c2 2019-05-12 jcs case ' ':
4764 07b0611c 2022-06-23 thomas if (s->eof) {
4765 07b0611c 2022-06-23 thomas view->count = 0;
4766 1e37a5c2 2019-05-12 jcs break;
4767 07b0611c 2022-06-23 thomas }
4768 1e37a5c2 2019-05-12 jcs i = 0;
4769 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4770 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4771 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4772 826082fe 2020-12-10 stsp if (linelen == -1) {
4773 826082fe 2020-12-10 stsp if (feof(s->f)) {
4774 826082fe 2020-12-10 stsp s->eof = 1;
4775 826082fe 2020-12-10 stsp } else
4776 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4777 34bc9ec9 2019-02-22 stsp break;
4778 826082fe 2020-12-10 stsp }
4779 1e37a5c2 2019-05-12 jcs }
4780 826082fe 2020-12-10 stsp free(line);
4781 1e37a5c2 2019-05-12 jcs break;
4782 1e37a5c2 2019-05-12 jcs case '[':
4783 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4784 1e37a5c2 2019-05-12 jcs s->diff_context--;
4785 aa61903a 2021-12-31 thomas s->matched_line = 0;
4786 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4787 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4788 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4789 27829c9e 2020-11-21 stsp s->nlines) {
4790 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4791 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4792 27829c9e 2020-11-21 stsp }
4793 07b0611c 2022-06-23 thomas } else
4794 07b0611c 2022-06-23 thomas view->count = 0;
4795 1e37a5c2 2019-05-12 jcs break;
4796 1e37a5c2 2019-05-12 jcs case ']':
4797 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4798 1e37a5c2 2019-05-12 jcs s->diff_context++;
4799 aa61903a 2021-12-31 thomas s->matched_line = 0;
4800 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4801 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4802 07b0611c 2022-06-23 thomas } else
4803 07b0611c 2022-06-23 thomas view->count = 0;
4804 1e37a5c2 2019-05-12 jcs break;
4805 1e37a5c2 2019-05-12 jcs case '<':
4806 1e37a5c2 2019-05-12 jcs case ',':
4807 4fc71f3b 2022-07-12 thomas up = 1;
4808 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4809 4fc71f3b 2022-07-12 thomas case '>':
4810 4fc71f3b 2022-07-12 thomas case '.':
4811 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4812 07b0611c 2022-06-23 thomas view->count = 0;
4813 48ae06ee 2018-10-18 stsp break;
4814 07b0611c 2022-06-23 thomas }
4815 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4816 6524637e 2019-02-21 stsp
4817 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4818 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4819 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4820 15a087fe 2019-02-21 stsp
4821 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4822 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4823 4fc71f3b 2022-07-12 thomas if (err)
4824 4fc71f3b 2022-07-12 thomas break;
4825 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4826 15a087fe 2019-02-21 stsp
4827 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4828 4fc71f3b 2022-07-12 thomas break;
4829 15a087fe 2019-02-21 stsp
4830 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4831 4fc71f3b 2022-07-12 thomas if (err)
4832 4fc71f3b 2022-07-12 thomas break;
4833 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4834 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4835 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4836 5e224a3e 2019-02-22 stsp
4837 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4838 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4839 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4840 4fc71f3b 2022-07-12 thomas
4841 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4842 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4843 4fc71f3b 2022-07-12 thomas if (err)
4844 4fc71f3b 2022-07-12 thomas break;
4845 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4846 5a8b5076 2020-12-05 stsp
4847 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4848 4fc71f3b 2022-07-12 thomas break;
4849 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4850 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4851 4fc71f3b 2022-07-12 thomas bs->selected_line);
4852 4fc71f3b 2022-07-12 thomas if (id == NULL)
4853 4fc71f3b 2022-07-12 thomas break;
4854 15a087fe 2019-02-21 stsp
4855 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4856 4fc71f3b 2022-07-12 thomas break;
4857 15a087fe 2019-02-21 stsp
4858 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4859 4fc71f3b 2022-07-12 thomas if (err)
4860 4fc71f3b 2022-07-12 thomas break;
4861 4fc71f3b 2022-07-12 thomas }
4862 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4863 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4864 aa61903a 2021-12-31 thomas s->matched_line = 0;
4865 05171be4 2022-06-23 thomas view->x = 0;
4866 1e37a5c2 2019-05-12 jcs
4867 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4868 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4869 1e37a5c2 2019-05-12 jcs break;
4870 1e37a5c2 2019-05-12 jcs default:
4871 07b0611c 2022-06-23 thomas view->count = 0;
4872 1e37a5c2 2019-05-12 jcs break;
4873 26ed57b2 2018-05-19 stsp }
4874 e5a0f69f 2018-08-18 stsp
4875 bcbd79e2 2018-08-19 stsp return err;
4876 26ed57b2 2018-05-19 stsp }
4877 26ed57b2 2018-05-19 stsp
4878 4ed7e80c 2018-05-20 stsp static const struct got_error *
4879 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4880 9f7d7167 2018-04-29 stsp {
4881 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4882 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4883 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4884 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4885 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4886 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4887 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4888 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4889 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4890 3dbaef42 2020-11-24 stsp const char *errstr;
4891 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4892 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4893 70ac5f84 2019-03-28 stsp
4894 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4895 26ed57b2 2018-05-19 stsp switch (ch) {
4896 64453f7e 2020-11-21 stsp case 'a':
4897 64453f7e 2020-11-21 stsp force_text_diff = 1;
4898 3dbaef42 2020-11-24 stsp break;
4899 3dbaef42 2020-11-24 stsp case 'C':
4900 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4901 3dbaef42 2020-11-24 stsp &errstr);
4902 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4903 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4904 8f666e67 2022-02-12 thomas errstr, errstr);
4905 64453f7e 2020-11-21 stsp break;
4906 09b5bff8 2020-02-23 naddy case 'r':
4907 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4908 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4909 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4910 09b5bff8 2020-02-23 naddy optarg);
4911 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4912 09b5bff8 2020-02-23 naddy break;
4913 3dbaef42 2020-11-24 stsp case 'w':
4914 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4915 3dbaef42 2020-11-24 stsp break;
4916 26ed57b2 2018-05-19 stsp default:
4917 17020d27 2019-03-07 stsp usage_diff();
4918 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4919 26ed57b2 2018-05-19 stsp }
4920 26ed57b2 2018-05-19 stsp }
4921 26ed57b2 2018-05-19 stsp
4922 26ed57b2 2018-05-19 stsp argc -= optind;
4923 26ed57b2 2018-05-19 stsp argv += optind;
4924 26ed57b2 2018-05-19 stsp
4925 26ed57b2 2018-05-19 stsp if (argc == 0) {
4926 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4927 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4928 15a94983 2018-12-23 stsp id_str1 = argv[0];
4929 15a94983 2018-12-23 stsp id_str2 = argv[1];
4930 26ed57b2 2018-05-19 stsp } else
4931 26ed57b2 2018-05-19 stsp usage_diff();
4932 eb6600df 2019-01-04 stsp
4933 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4934 7cd52833 2022-06-23 thomas if (error)
4935 7cd52833 2022-06-23 thomas goto done;
4936 7cd52833 2022-06-23 thomas
4937 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4938 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4939 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4940 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4941 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4942 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4943 c156c7a4 2020-12-18 stsp goto done;
4944 a273ac94 2020-02-23 naddy if (worktree)
4945 a273ac94 2020-02-23 naddy repo_path =
4946 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4947 a273ac94 2020-02-23 naddy else
4948 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4949 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4950 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4951 c156c7a4 2020-12-18 stsp goto done;
4952 c156c7a4 2020-12-18 stsp }
4953 a273ac94 2020-02-23 naddy }
4954 a273ac94 2020-02-23 naddy
4955 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4956 eb6600df 2019-01-04 stsp if (error)
4957 eb6600df 2019-01-04 stsp goto done;
4958 26ed57b2 2018-05-19 stsp
4959 a273ac94 2020-02-23 naddy init_curses();
4960 a273ac94 2020-02-23 naddy
4961 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4962 51a10b52 2020-12-26 stsp if (error)
4963 51a10b52 2020-12-26 stsp goto done;
4964 51a10b52 2020-12-26 stsp
4965 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4966 26ed57b2 2018-05-19 stsp if (error)
4967 26ed57b2 2018-05-19 stsp goto done;
4968 26ed57b2 2018-05-19 stsp
4969 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4970 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4971 26ed57b2 2018-05-19 stsp if (error)
4972 26ed57b2 2018-05-19 stsp goto done;
4973 26ed57b2 2018-05-19 stsp
4974 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4975 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4976 26ed57b2 2018-05-19 stsp if (error)
4977 26ed57b2 2018-05-19 stsp goto done;
4978 26ed57b2 2018-05-19 stsp
4979 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4980 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4981 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4982 ea5e7bb5 2018-08-01 stsp goto done;
4983 ea5e7bb5 2018-08-01 stsp }
4984 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4985 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4986 5dc9f4bc 2018-08-04 stsp if (error)
4987 5dc9f4bc 2018-08-04 stsp goto done;
4988 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4989 26ed57b2 2018-05-19 stsp done:
4990 3dbaef42 2020-11-24 stsp free(label1);
4991 3dbaef42 2020-11-24 stsp free(label2);
4992 c02c541e 2019-03-29 stsp free(repo_path);
4993 a273ac94 2020-02-23 naddy free(cwd);
4994 1d0f4054 2021-06-17 stsp if (repo) {
4995 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4996 1d0f4054 2021-06-17 stsp if (error == NULL)
4997 1d0f4054 2021-06-17 stsp error = close_err;
4998 1d0f4054 2021-06-17 stsp }
4999 a273ac94 2020-02-23 naddy if (worktree)
5000 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5001 7cd52833 2022-06-23 thomas if (pack_fds) {
5002 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5003 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5004 7cd52833 2022-06-23 thomas if (error == NULL)
5005 7cd52833 2022-06-23 thomas error = pack_err;
5006 7cd52833 2022-06-23 thomas }
5007 51a10b52 2020-12-26 stsp tog_free_refs();
5008 26ed57b2 2018-05-19 stsp return error;
5009 9f7d7167 2018-04-29 stsp }
5010 9f7d7167 2018-04-29 stsp
5011 4ed7e80c 2018-05-20 stsp __dead static void
5012 9f7d7167 2018-04-29 stsp usage_blame(void)
5013 9f7d7167 2018-04-29 stsp {
5014 80ddbec8 2018-04-29 stsp endwin();
5015 91198554 2022-06-23 thomas fprintf(stderr,
5016 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5017 9f7d7167 2018-04-29 stsp getprogname());
5018 9f7d7167 2018-04-29 stsp exit(1);
5019 9f7d7167 2018-04-29 stsp }
5020 84451b3e 2018-07-10 stsp
5021 84451b3e 2018-07-10 stsp struct tog_blame_line {
5022 84451b3e 2018-07-10 stsp int annotated;
5023 84451b3e 2018-07-10 stsp struct got_object_id *id;
5024 84451b3e 2018-07-10 stsp };
5025 9f7d7167 2018-04-29 stsp
5026 4ed7e80c 2018-05-20 stsp static const struct got_error *
5027 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5028 84451b3e 2018-07-10 stsp {
5029 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5030 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5031 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5032 84451b3e 2018-07-10 stsp const struct got_error *err;
5033 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5034 826082fe 2020-12-10 stsp char *line = NULL;
5035 826082fe 2020-12-10 stsp size_t linesize = 0;
5036 826082fe 2020-12-10 stsp ssize_t linelen;
5037 84451b3e 2018-07-10 stsp wchar_t *wline;
5038 27a741e5 2019-09-11 stsp int width;
5039 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5040 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5041 ab089a2a 2018-07-12 stsp char *id_str;
5042 11b20872 2019-11-08 stsp struct tog_color *tc;
5043 ab089a2a 2018-07-12 stsp
5044 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5045 ab089a2a 2018-07-12 stsp if (err)
5046 ab089a2a 2018-07-12 stsp return err;
5047 84451b3e 2018-07-10 stsp
5048 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5049 f7d12f7e 2018-08-01 stsp werase(view->window);
5050 84451b3e 2018-07-10 stsp
5051 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5052 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5053 ab089a2a 2018-07-12 stsp free(id_str);
5054 ab089a2a 2018-07-12 stsp return err;
5055 ab089a2a 2018-07-12 stsp }
5056 ab089a2a 2018-07-12 stsp
5057 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5058 ab089a2a 2018-07-12 stsp free(line);
5059 2550e4c3 2018-07-13 stsp line = NULL;
5060 1cae65b4 2019-09-22 stsp if (err)
5061 1cae65b4 2019-09-22 stsp return err;
5062 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5063 a3404814 2018-09-02 stsp wstandout(view->window);
5064 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5065 11b20872 2019-11-08 stsp if (tc)
5066 11b20872 2019-11-08 stsp wattr_on(view->window,
5067 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5068 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5069 11b20872 2019-11-08 stsp if (tc)
5070 11b20872 2019-11-08 stsp wattr_off(view->window,
5071 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5072 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5073 a3404814 2018-09-02 stsp wstandend(view->window);
5074 2550e4c3 2018-07-13 stsp free(wline);
5075 2550e4c3 2018-07-13 stsp wline = NULL;
5076 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5077 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5078 ab089a2a 2018-07-12 stsp
5079 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5080 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5081 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5082 ab089a2a 2018-07-12 stsp free(id_str);
5083 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5084 ab089a2a 2018-07-12 stsp }
5085 ab089a2a 2018-07-12 stsp free(id_str);
5086 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 3f60a8ef 2018-07-10 stsp free(line);
5088 2550e4c3 2018-07-13 stsp line = NULL;
5089 3f60a8ef 2018-07-10 stsp if (err)
5090 3f60a8ef 2018-07-10 stsp return err;
5091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5092 2550e4c3 2018-07-13 stsp free(wline);
5093 2550e4c3 2018-07-13 stsp wline = NULL;
5094 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5095 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5096 3f60a8ef 2018-07-10 stsp
5097 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5098 05171be4 2022-06-23 thomas view->maxx = 0;
5099 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5100 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5101 826082fe 2020-12-10 stsp if (linelen == -1) {
5102 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5103 826082fe 2020-12-10 stsp s->eof = 1;
5104 826082fe 2020-12-10 stsp break;
5105 826082fe 2020-12-10 stsp }
5106 84451b3e 2018-07-10 stsp free(line);
5107 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5108 84451b3e 2018-07-10 stsp }
5109 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5110 826082fe 2020-12-10 stsp continue;
5111 cbea3800 2022-06-23 thomas
5112 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5113 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5114 cbea3800 2022-06-23 thomas if (err) {
5115 cbea3800 2022-06-23 thomas free(line);
5116 cbea3800 2022-06-23 thomas return err;
5117 cbea3800 2022-06-23 thomas }
5118 cbea3800 2022-06-23 thomas free(wline);
5119 cbea3800 2022-06-23 thomas wline = NULL;
5120 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5121 84451b3e 2018-07-10 stsp
5122 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5123 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5124 b700b5d6 2018-07-10 stsp
5125 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5126 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5127 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5128 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5129 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5130 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5131 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5132 8d0fe45a 2019-08-12 stsp char *id_str;
5133 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5134 91198554 2022-06-23 thomas blame_line->id);
5135 8d0fe45a 2019-08-12 stsp if (err) {
5136 8d0fe45a 2019-08-12 stsp free(line);
5137 8d0fe45a 2019-08-12 stsp return err;
5138 8d0fe45a 2019-08-12 stsp }
5139 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5140 11b20872 2019-11-08 stsp if (tc)
5141 11b20872 2019-11-08 stsp wattr_on(view->window,
5142 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5143 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5144 11b20872 2019-11-08 stsp if (tc)
5145 11b20872 2019-11-08 stsp wattr_off(view->window,
5146 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5147 8d0fe45a 2019-08-12 stsp free(id_str);
5148 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5149 8d0fe45a 2019-08-12 stsp } else {
5150 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5151 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5152 84451b3e 2018-07-10 stsp }
5153 ee41ec32 2018-07-10 stsp } else {
5154 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5155 ee41ec32 2018-07-10 stsp prev_id = NULL;
5156 ee41ec32 2018-07-10 stsp }
5157 84451b3e 2018-07-10 stsp
5158 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5159 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5160 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5161 27a741e5 2019-09-11 stsp
5162 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5163 41605754 2020-11-12 stsp width = 9;
5164 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5165 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5166 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5167 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5168 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5169 41605754 2020-11-12 stsp if (err) {
5170 41605754 2020-11-12 stsp free(line);
5171 41605754 2020-11-12 stsp return err;
5172 41605754 2020-11-12 stsp }
5173 41605754 2020-11-12 stsp width += 9;
5174 41605754 2020-11-12 stsp } else {
5175 923086fd 2022-06-23 thomas int skip;
5176 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5177 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5178 923086fd 2022-06-23 thomas if (err) {
5179 923086fd 2022-06-23 thomas free(line);
5180 923086fd 2022-06-23 thomas return err;
5181 05171be4 2022-06-23 thomas }
5182 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5183 02bce7e2 2022-06-23 thomas width += 9;
5184 41605754 2020-11-12 stsp free(wline);
5185 41605754 2020-11-12 stsp wline = NULL;
5186 41605754 2020-11-12 stsp }
5187 41605754 2020-11-12 stsp
5188 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5189 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5190 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5191 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5192 84451b3e 2018-07-10 stsp }
5193 826082fe 2020-12-10 stsp free(line);
5194 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5195 84451b3e 2018-07-10 stsp
5196 a5d43cac 2022-07-01 thomas view_border(view);
5197 84451b3e 2018-07-10 stsp
5198 84451b3e 2018-07-10 stsp return NULL;
5199 84451b3e 2018-07-10 stsp }
5200 84451b3e 2018-07-10 stsp
5201 84451b3e 2018-07-10 stsp static const struct got_error *
5202 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5203 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5204 84451b3e 2018-07-10 stsp {
5205 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5206 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5207 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5208 1a76625f 2018-10-22 stsp int errcode;
5209 84451b3e 2018-07-10 stsp
5210 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5211 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5212 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5213 84451b3e 2018-07-10 stsp
5214 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5215 1a76625f 2018-10-22 stsp if (errcode)
5216 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5217 84451b3e 2018-07-10 stsp
5218 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5219 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5220 d68a0a7d 2018-07-10 stsp goto done;
5221 d68a0a7d 2018-07-10 stsp }
5222 d68a0a7d 2018-07-10 stsp
5223 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5224 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5225 d68a0a7d 2018-07-10 stsp
5226 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5227 d68a0a7d 2018-07-10 stsp if (line->annotated)
5228 d68a0a7d 2018-07-10 stsp goto done;
5229 d68a0a7d 2018-07-10 stsp
5230 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5231 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5232 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5233 84451b3e 2018-07-10 stsp goto done;
5234 84451b3e 2018-07-10 stsp }
5235 84451b3e 2018-07-10 stsp line->annotated = 1;
5236 84451b3e 2018-07-10 stsp done:
5237 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5238 1a76625f 2018-10-22 stsp if (errcode)
5239 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5240 84451b3e 2018-07-10 stsp return err;
5241 84451b3e 2018-07-10 stsp }
5242 84451b3e 2018-07-10 stsp
5243 84451b3e 2018-07-10 stsp static void *
5244 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5245 84451b3e 2018-07-10 stsp {
5246 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5247 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5248 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5249 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5250 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5251 00a8878e 2022-07-01 thomas
5252 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5253 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5254 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5255 9117a7b7 2022-07-01 thomas
5256 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5257 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5258 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5259 9117a7b7 2022-07-01 thomas goto done;
5260 9117a7b7 2022-07-01 thomas }
5261 18430de3 2018-07-10 stsp
5262 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5263 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5264 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5265 9117a7b7 2022-07-01 thomas goto done;
5266 9117a7b7 2022-07-01 thomas }
5267 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5268 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5269 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5270 9117a7b7 2022-07-01 thomas goto done;
5271 9117a7b7 2022-07-01 thomas }
5272 9117a7b7 2022-07-01 thomas
5273 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5274 61266923 2020-01-14 stsp if (err)
5275 9117a7b7 2022-07-01 thomas goto done;
5276 61266923 2020-01-14 stsp
5277 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5278 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5279 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5280 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5281 fc06ba56 2019-08-22 stsp err = NULL;
5282 18430de3 2018-07-10 stsp
5283 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5284 9117a7b7 2022-07-01 thomas if (errcode) {
5285 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5286 9117a7b7 2022-07-01 thomas goto done;
5287 9117a7b7 2022-07-01 thomas }
5288 18430de3 2018-07-10 stsp
5289 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5290 1d0f4054 2021-06-17 stsp if (err == NULL)
5291 1d0f4054 2021-06-17 stsp err = close_err;
5292 c9beca56 2018-07-22 stsp ta->repo = NULL;
5293 c9beca56 2018-07-22 stsp *ta->complete = 1;
5294 18430de3 2018-07-10 stsp
5295 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5296 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5297 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5298 18430de3 2018-07-10 stsp
5299 9117a7b7 2022-07-01 thomas done:
5300 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5301 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5302 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5303 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5304 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5305 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5306 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5307 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5308 00a8878e 2022-07-01 thomas
5309 18430de3 2018-07-10 stsp return (void *)err;
5310 84451b3e 2018-07-10 stsp }
5311 84451b3e 2018-07-10 stsp
5312 245d91c1 2018-07-12 stsp static struct got_object_id *
5313 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5314 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5315 245d91c1 2018-07-12 stsp {
5316 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5317 8d0fe45a 2019-08-12 stsp
5318 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5319 8d0fe45a 2019-08-12 stsp return NULL;
5320 b880a918 2018-07-10 stsp
5321 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5322 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5323 4fc71f3b 2022-07-12 thomas return NULL;
5324 4fc71f3b 2022-07-12 thomas
5325 4fc71f3b 2022-07-12 thomas return line->id;
5326 4fc71f3b 2022-07-12 thomas }
5327 4fc71f3b 2022-07-12 thomas
5328 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5329 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5330 4fc71f3b 2022-07-12 thomas int lineno)
5331 4fc71f3b 2022-07-12 thomas {
5332 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5333 4fc71f3b 2022-07-12 thomas
5334 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5335 4fc71f3b 2022-07-12 thomas return NULL;
5336 4fc71f3b 2022-07-12 thomas
5337 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5338 245d91c1 2018-07-12 stsp if (!line->annotated)
5339 245d91c1 2018-07-12 stsp return NULL;
5340 245d91c1 2018-07-12 stsp
5341 245d91c1 2018-07-12 stsp return line->id;
5342 b880a918 2018-07-10 stsp }
5343 245d91c1 2018-07-12 stsp
5344 b880a918 2018-07-10 stsp static const struct got_error *
5345 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5346 a70480e0 2018-06-23 stsp {
5347 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5348 245d91c1 2018-07-12 stsp int i;
5349 245d91c1 2018-07-12 stsp
5350 245d91c1 2018-07-12 stsp if (blame->thread) {
5351 1a76625f 2018-10-22 stsp int errcode;
5352 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5353 1a76625f 2018-10-22 stsp if (errcode)
5354 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5355 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5356 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5357 1a76625f 2018-10-22 stsp if (errcode)
5358 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5359 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5360 1a76625f 2018-10-22 stsp if (errcode)
5361 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5362 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5363 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5364 245d91c1 2018-07-12 stsp err = NULL;
5365 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5366 245d91c1 2018-07-12 stsp }
5367 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5368 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5369 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5370 1d0f4054 2021-06-17 stsp if (err == NULL)
5371 1d0f4054 2021-06-17 stsp err = close_err;
5372 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5373 245d91c1 2018-07-12 stsp }
5374 245d91c1 2018-07-12 stsp if (blame->f) {
5375 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5376 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5377 245d91c1 2018-07-12 stsp blame->f = NULL;
5378 245d91c1 2018-07-12 stsp }
5379 57670559 2018-12-23 stsp if (blame->lines) {
5380 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5381 57670559 2018-12-23 stsp free(blame->lines[i].id);
5382 57670559 2018-12-23 stsp free(blame->lines);
5383 57670559 2018-12-23 stsp blame->lines = NULL;
5384 57670559 2018-12-23 stsp }
5385 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5386 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5387 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5388 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5389 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5390 7cd52833 2022-06-23 thomas if (err == NULL)
5391 7cd52833 2022-06-23 thomas err = pack_err;
5392 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5393 7cd52833 2022-06-23 thomas }
5394 245d91c1 2018-07-12 stsp return err;
5395 245d91c1 2018-07-12 stsp }
5396 245d91c1 2018-07-12 stsp
5397 245d91c1 2018-07-12 stsp static const struct got_error *
5398 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5399 fc06ba56 2019-08-22 stsp {
5400 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5401 fc06ba56 2019-08-22 stsp int *done = arg;
5402 fc06ba56 2019-08-22 stsp int errcode;
5403 fc06ba56 2019-08-22 stsp
5404 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5405 fc06ba56 2019-08-22 stsp if (errcode)
5406 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5407 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5408 fc06ba56 2019-08-22 stsp
5409 fc06ba56 2019-08-22 stsp if (*done)
5410 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5411 fc06ba56 2019-08-22 stsp
5412 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5413 fc06ba56 2019-08-22 stsp if (errcode)
5414 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5415 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5416 fc06ba56 2019-08-22 stsp
5417 fc06ba56 2019-08-22 stsp return err;
5418 fc06ba56 2019-08-22 stsp }
5419 fc06ba56 2019-08-22 stsp
5420 fc06ba56 2019-08-22 stsp static const struct got_error *
5421 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5422 245d91c1 2018-07-12 stsp {
5423 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5424 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5425 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5426 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5427 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5428 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5429 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5430 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5431 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5432 a70480e0 2018-06-23 stsp
5433 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5434 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5435 27d434c2 2018-09-15 stsp if (err)
5436 15a94983 2018-12-23 stsp return err;
5437 f4ae6ddb 2022-07-01 thomas
5438 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5439 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5440 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5441 f4ae6ddb 2022-07-01 thomas goto done;
5442 f4ae6ddb 2022-07-01 thomas }
5443 945f9229 2022-04-16 thomas
5444 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5445 945f9229 2022-04-16 thomas if (err)
5446 945f9229 2022-04-16 thomas goto done;
5447 27d434c2 2018-09-15 stsp
5448 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5449 84451b3e 2018-07-10 stsp if (err)
5450 84451b3e 2018-07-10 stsp goto done;
5451 27d434c2 2018-09-15 stsp
5452 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5453 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5454 84451b3e 2018-07-10 stsp goto done;
5455 84451b3e 2018-07-10 stsp }
5456 a70480e0 2018-06-23 stsp
5457 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5458 a70480e0 2018-06-23 stsp if (err)
5459 a70480e0 2018-06-23 stsp goto done;
5460 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5461 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5462 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5463 84451b3e 2018-07-10 stsp goto done;
5464 84451b3e 2018-07-10 stsp }
5465 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5466 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5467 1fddf795 2021-01-20 stsp if (err)
5468 1fddf795 2021-01-20 stsp goto done;
5469 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5470 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5471 84451b3e 2018-07-10 stsp goto done;
5472 1fddf795 2021-01-20 stsp }
5473 b02560ec 2019-08-19 stsp
5474 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5475 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5476 b02560ec 2019-08-19 stsp blame->nlines--;
5477 a70480e0 2018-06-23 stsp
5478 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5479 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5480 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5481 84451b3e 2018-07-10 stsp goto done;
5482 84451b3e 2018-07-10 stsp }
5483 a70480e0 2018-06-23 stsp
5484 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5485 bd24772e 2018-07-11 stsp if (err)
5486 bd24772e 2018-07-11 stsp goto done;
5487 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5488 7cd52833 2022-06-23 thomas pack_fds);
5489 7cd52833 2022-06-23 thomas if (err)
5490 7cd52833 2022-06-23 thomas goto done;
5491 bd24772e 2018-07-11 stsp
5492 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5493 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5494 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5495 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5496 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5497 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5498 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5499 245d91c1 2018-07-12 stsp goto done;
5500 245d91c1 2018-07-12 stsp }
5501 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5502 245d91c1 2018-07-12 stsp
5503 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5504 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5505 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5506 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5507 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5508 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5509 a5388363 2020-12-01 naddy s->blame_complete = 0;
5510 f5a09613 2020-12-13 naddy
5511 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5512 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5513 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5514 f5a09613 2020-12-13 naddy s->selected_line = 1;
5515 f5a09613 2020-12-13 naddy }
5516 aa61903a 2021-12-31 thomas s->matched_line = 0;
5517 245d91c1 2018-07-12 stsp
5518 245d91c1 2018-07-12 stsp done:
5519 945f9229 2022-04-16 thomas if (commit)
5520 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5521 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5522 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5523 245d91c1 2018-07-12 stsp if (blob)
5524 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5525 27d434c2 2018-09-15 stsp free(obj_id);
5526 245d91c1 2018-07-12 stsp if (err)
5527 245d91c1 2018-07-12 stsp stop_blame(blame);
5528 245d91c1 2018-07-12 stsp return err;
5529 245d91c1 2018-07-12 stsp }
5530 245d91c1 2018-07-12 stsp
5531 245d91c1 2018-07-12 stsp static const struct got_error *
5532 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5533 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5534 245d91c1 2018-07-12 stsp {
5535 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5536 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5537 dbc6a6b6 2018-07-12 stsp
5538 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5539 245d91c1 2018-07-12 stsp
5540 c4843652 2019-08-12 stsp s->path = strdup(path);
5541 c4843652 2019-08-12 stsp if (s->path == NULL)
5542 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5543 c4843652 2019-08-12 stsp
5544 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5545 c4843652 2019-08-12 stsp if (err) {
5546 c4843652 2019-08-12 stsp free(s->path);
5547 7cbe629d 2018-08-04 stsp return err;
5548 c4843652 2019-08-12 stsp }
5549 245d91c1 2018-07-12 stsp
5550 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5551 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5552 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5553 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5554 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5555 fb2756b9 2018-08-04 stsp s->repo = repo;
5556 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5557 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5558 7cbe629d 2018-08-04 stsp
5559 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5560 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5561 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5562 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5563 11b20872 2019-11-08 stsp if (err)
5564 11b20872 2019-11-08 stsp return err;
5565 11b20872 2019-11-08 stsp }
5566 11b20872 2019-11-08 stsp
5567 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5568 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5569 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5570 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5571 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5572 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5573 e5a0f69f 2018-08-18 stsp
5574 a5388363 2020-12-01 naddy return run_blame(view);
5575 7cbe629d 2018-08-04 stsp }
5576 7cbe629d 2018-08-04 stsp
5577 e5a0f69f 2018-08-18 stsp static const struct got_error *
5578 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5579 7cbe629d 2018-08-04 stsp {
5580 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5581 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5582 7cbe629d 2018-08-04 stsp
5583 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5584 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5585 e5a0f69f 2018-08-18 stsp
5586 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5587 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5588 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5589 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5590 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5591 7cbe629d 2018-08-04 stsp }
5592 e5a0f69f 2018-08-18 stsp
5593 e5a0f69f 2018-08-18 stsp free(s->path);
5594 11b20872 2019-11-08 stsp free_colors(&s->colors);
5595 e5a0f69f 2018-08-18 stsp return err;
5596 7cbe629d 2018-08-04 stsp }
5597 7cbe629d 2018-08-04 stsp
5598 7cbe629d 2018-08-04 stsp static const struct got_error *
5599 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5600 6c4c42e0 2019-06-24 stsp {
5601 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5602 6c4c42e0 2019-06-24 stsp
5603 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5604 6c4c42e0 2019-06-24 stsp return NULL;
5605 6c4c42e0 2019-06-24 stsp }
5606 6c4c42e0 2019-06-24 stsp
5607 6c4c42e0 2019-06-24 stsp static const struct got_error *
5608 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5609 6c4c42e0 2019-06-24 stsp {
5610 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5611 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5612 6c4c42e0 2019-06-24 stsp int lineno;
5613 78eecffc 2022-06-23 thomas char *line = NULL;
5614 826082fe 2020-12-10 stsp size_t linesize = 0;
5615 826082fe 2020-12-10 stsp ssize_t linelen;
5616 6c4c42e0 2019-06-24 stsp
5617 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5618 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5619 6c4c42e0 2019-06-24 stsp return NULL;
5620 6c4c42e0 2019-06-24 stsp }
5621 6c4c42e0 2019-06-24 stsp
5622 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5623 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5624 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5625 6c4c42e0 2019-06-24 stsp else
5626 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5627 4b3f9dac 2021-12-17 thomas } else
5628 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5629 6c4c42e0 2019-06-24 stsp
5630 6c4c42e0 2019-06-24 stsp while (1) {
5631 6c4c42e0 2019-06-24 stsp off_t offset;
5632 6c4c42e0 2019-06-24 stsp
5633 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5634 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5635 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5636 6c4c42e0 2019-06-24 stsp break;
5637 6c4c42e0 2019-06-24 stsp }
5638 2246482e 2019-06-25 stsp
5639 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5640 6c4c42e0 2019-06-24 stsp lineno = 1;
5641 6c4c42e0 2019-06-24 stsp else
5642 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5643 6c4c42e0 2019-06-24 stsp }
5644 6c4c42e0 2019-06-24 stsp
5645 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5646 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5647 6c4c42e0 2019-06-24 stsp free(line);
5648 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5649 6c4c42e0 2019-06-24 stsp }
5650 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5651 78eecffc 2022-06-23 thomas if (linelen != -1) {
5652 78eecffc 2022-06-23 thomas char *exstr;
5653 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5654 78eecffc 2022-06-23 thomas if (err)
5655 78eecffc 2022-06-23 thomas break;
5656 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5657 78eecffc 2022-06-23 thomas &view->regmatch)) {
5658 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5659 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5660 78eecffc 2022-06-23 thomas free(exstr);
5661 78eecffc 2022-06-23 thomas break;
5662 78eecffc 2022-06-23 thomas }
5663 78eecffc 2022-06-23 thomas free(exstr);
5664 6c4c42e0 2019-06-24 stsp }
5665 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5666 6c4c42e0 2019-06-24 stsp lineno++;
5667 6c4c42e0 2019-06-24 stsp else
5668 6c4c42e0 2019-06-24 stsp lineno--;
5669 6c4c42e0 2019-06-24 stsp }
5670 826082fe 2020-12-10 stsp free(line);
5671 6c4c42e0 2019-06-24 stsp
5672 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5673 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5674 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5675 6c4c42e0 2019-06-24 stsp }
5676 6c4c42e0 2019-06-24 stsp
5677 05171be4 2022-06-23 thomas return err;
5678 6c4c42e0 2019-06-24 stsp }
5679 6c4c42e0 2019-06-24 stsp
5680 6c4c42e0 2019-06-24 stsp static const struct got_error *
5681 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5682 7cbe629d 2018-08-04 stsp {
5683 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5684 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5685 2b380cc8 2018-10-24 stsp int errcode;
5686 2b380cc8 2018-10-24 stsp
5687 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5688 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5689 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5690 2b380cc8 2018-10-24 stsp if (errcode)
5691 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5692 51fe7530 2019-08-19 stsp
5693 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5694 2b380cc8 2018-10-24 stsp }
5695 e5a0f69f 2018-08-18 stsp
5696 51fe7530 2019-08-19 stsp if (s->blame_complete)
5697 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5698 51fe7530 2019-08-19 stsp
5699 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5700 e5a0f69f 2018-08-18 stsp
5701 a5d43cac 2022-07-01 thomas view_border(view);
5702 e5a0f69f 2018-08-18 stsp return err;
5703 e5a0f69f 2018-08-18 stsp }
5704 e5a0f69f 2018-08-18 stsp
5705 e5a0f69f 2018-08-18 stsp static const struct got_error *
5706 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5707 e5a0f69f 2018-08-18 stsp {
5708 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5709 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5710 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5711 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5712 a5d43cac 2022-07-01 thomas
5713 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5714 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5715 a5d43cac 2022-07-01 thomas --eos; /* border */
5716 7cbe629d 2018-08-04 stsp
5717 e5a0f69f 2018-08-18 stsp switch (ch) {
5718 05171be4 2022-06-23 thomas case '0':
5719 05171be4 2022-06-23 thomas view->x = 0;
5720 05171be4 2022-06-23 thomas break;
5721 05171be4 2022-06-23 thomas case '$':
5722 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5723 07b0611c 2022-06-23 thomas view->count = 0;
5724 05171be4 2022-06-23 thomas break;
5725 05171be4 2022-06-23 thomas case KEY_RIGHT:
5726 05171be4 2022-06-23 thomas case 'l':
5727 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5728 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5729 07b0611c 2022-06-23 thomas else
5730 07b0611c 2022-06-23 thomas view->count = 0;
5731 05171be4 2022-06-23 thomas break;
5732 05171be4 2022-06-23 thomas case KEY_LEFT:
5733 05171be4 2022-06-23 thomas case 'h':
5734 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5735 07b0611c 2022-06-23 thomas if (view->x <= 0)
5736 07b0611c 2022-06-23 thomas view->count = 0;
5737 05171be4 2022-06-23 thomas break;
5738 1e37a5c2 2019-05-12 jcs case 'q':
5739 1e37a5c2 2019-05-12 jcs s->done = 1;
5740 4deef56f 2021-09-02 naddy break;
5741 4deef56f 2021-09-02 naddy case 'g':
5742 4deef56f 2021-09-02 naddy case KEY_HOME:
5743 4deef56f 2021-09-02 naddy s->selected_line = 1;
5744 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5745 07b0611c 2022-06-23 thomas view->count = 0;
5746 4deef56f 2021-09-02 naddy break;
5747 4deef56f 2021-09-02 naddy case 'G':
5748 4deef56f 2021-09-02 naddy case KEY_END:
5749 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5750 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5751 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5752 4deef56f 2021-09-02 naddy } else {
5753 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5754 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5755 4deef56f 2021-09-02 naddy }
5756 07b0611c 2022-06-23 thomas view->count = 0;
5757 1e37a5c2 2019-05-12 jcs break;
5758 1e37a5c2 2019-05-12 jcs case 'k':
5759 1e37a5c2 2019-05-12 jcs case KEY_UP:
5760 f7140bf5 2021-10-17 thomas case CTRL('p'):
5761 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5762 1e37a5c2 2019-05-12 jcs s->selected_line--;
5763 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5764 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5765 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5766 07b0611c 2022-06-23 thomas else
5767 07b0611c 2022-06-23 thomas view->count = 0;
5768 1e37a5c2 2019-05-12 jcs break;
5769 70f17a53 2022-06-13 thomas case CTRL('u'):
5770 23427b14 2022-06-23 thomas case 'u':
5771 70f17a53 2022-06-13 thomas nscroll /= 2;
5772 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5773 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5774 ea025d1d 2020-02-22 naddy case CTRL('b'):
5775 1c5e5faa 2022-06-23 thomas case 'b':
5776 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5777 07b0611c 2022-06-23 thomas if (view->count > 1)
5778 07b0611c 2022-06-23 thomas nscroll += nscroll;
5779 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5780 07b0611c 2022-06-23 thomas view->count = 0;
5781 e5a0f69f 2018-08-18 stsp break;
5782 1e37a5c2 2019-05-12 jcs }
5783 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5784 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5785 1e37a5c2 2019-05-12 jcs else
5786 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5787 1e37a5c2 2019-05-12 jcs break;
5788 1e37a5c2 2019-05-12 jcs case 'j':
5789 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5790 f7140bf5 2021-10-17 thomas case CTRL('n'):
5791 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5792 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5793 1e37a5c2 2019-05-12 jcs s->selected_line++;
5794 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5795 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5796 07b0611c 2022-06-23 thomas else
5797 07b0611c 2022-06-23 thomas view->count = 0;
5798 1e37a5c2 2019-05-12 jcs break;
5799 1c5e5faa 2022-06-23 thomas case 'c':
5800 1e37a5c2 2019-05-12 jcs case 'p': {
5801 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5802 07b0611c 2022-06-23 thomas
5803 07b0611c 2022-06-23 thomas view->count = 0;
5804 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5805 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5806 1e37a5c2 2019-05-12 jcs if (id == NULL)
5807 e5a0f69f 2018-08-18 stsp break;
5808 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5809 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5810 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5811 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5812 1e37a5c2 2019-05-12 jcs int obj_type;
5813 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5814 1e37a5c2 2019-05-12 jcs s->repo, id);
5815 e5a0f69f 2018-08-18 stsp if (err)
5816 e5a0f69f 2018-08-18 stsp break;
5817 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5818 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5819 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5820 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5821 e5a0f69f 2018-08-18 stsp break;
5822 e5a0f69f 2018-08-18 stsp }
5823 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5824 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5825 ec242592 2022-04-22 thomas s->repo, &pid->id);
5826 945f9229 2022-04-16 thomas if (err)
5827 945f9229 2022-04-16 thomas break;
5828 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5829 945f9229 2022-04-16 thomas pcommit, s->path);
5830 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5831 e5a0f69f 2018-08-18 stsp if (err) {
5832 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5833 1e37a5c2 2019-05-12 jcs err = NULL;
5834 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5835 e5a0f69f 2018-08-18 stsp break;
5836 e5a0f69f 2018-08-18 stsp }
5837 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5838 1e37a5c2 2019-05-12 jcs blob_id);
5839 1e37a5c2 2019-05-12 jcs free(blob_id);
5840 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5841 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5842 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5843 e5a0f69f 2018-08-18 stsp break;
5844 1e37a5c2 2019-05-12 jcs }
5845 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5846 ec242592 2022-04-22 thomas &pid->id);
5847 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5848 1e37a5c2 2019-05-12 jcs } else {
5849 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5850 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5851 1e37a5c2 2019-05-12 jcs break;
5852 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5853 1e37a5c2 2019-05-12 jcs id);
5854 1e37a5c2 2019-05-12 jcs }
5855 1e37a5c2 2019-05-12 jcs if (err)
5856 e5a0f69f 2018-08-18 stsp break;
5857 1e37a5c2 2019-05-12 jcs s->done = 1;
5858 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5859 1e37a5c2 2019-05-12 jcs s->done = 0;
5860 1e37a5c2 2019-05-12 jcs if (thread_err)
5861 1e37a5c2 2019-05-12 jcs break;
5862 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5863 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5864 a5388363 2020-12-01 naddy err = run_blame(view);
5865 1e37a5c2 2019-05-12 jcs if (err)
5866 1e37a5c2 2019-05-12 jcs break;
5867 1e37a5c2 2019-05-12 jcs break;
5868 1e37a5c2 2019-05-12 jcs }
5869 1c5e5faa 2022-06-23 thomas case 'C': {
5870 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5871 07b0611c 2022-06-23 thomas
5872 07b0611c 2022-06-23 thomas view->count = 0;
5873 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5874 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5875 1e37a5c2 2019-05-12 jcs break;
5876 1e37a5c2 2019-05-12 jcs s->done = 1;
5877 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5878 1e37a5c2 2019-05-12 jcs s->done = 0;
5879 1e37a5c2 2019-05-12 jcs if (thread_err)
5880 1e37a5c2 2019-05-12 jcs break;
5881 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5882 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5883 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5884 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5885 a5388363 2020-12-01 naddy err = run_blame(view);
5886 1e37a5c2 2019-05-12 jcs if (err)
5887 1e37a5c2 2019-05-12 jcs break;
5888 1e37a5c2 2019-05-12 jcs break;
5889 1e37a5c2 2019-05-12 jcs }
5890 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5891 1e37a5c2 2019-05-12 jcs case '\r': {
5892 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5893 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5894 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5895 07b0611c 2022-06-23 thomas
5896 07b0611c 2022-06-23 thomas view->count = 0;
5897 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5898 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5899 1e37a5c2 2019-05-12 jcs if (id == NULL)
5900 1e37a5c2 2019-05-12 jcs break;
5901 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5902 1e37a5c2 2019-05-12 jcs if (err)
5903 1e37a5c2 2019-05-12 jcs break;
5904 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5905 4fc71f3b 2022-07-12 thomas if (*new_view) {
5906 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5907 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5908 4fc71f3b 2022-07-12 thomas if (err)
5909 4fc71f3b 2022-07-12 thomas break;
5910 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
5911 4fc71f3b 2022-07-12 thomas } else {
5912 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
5913 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
5914 a5d43cac 2022-07-01 thomas
5915 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
5916 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
5917 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
5918 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
5919 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
5920 4fc71f3b 2022-07-12 thomas break;
5921 4fc71f3b 2022-07-12 thomas }
5922 15a94983 2018-12-23 stsp }
5923 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5924 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
5925 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5926 1e37a5c2 2019-05-12 jcs if (err) {
5927 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5928 1e37a5c2 2019-05-12 jcs break;
5929 1e37a5c2 2019-05-12 jcs }
5930 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
5931 4fc71f3b 2022-07-12 thomas s->selected_line;
5932 4fc71f3b 2022-07-12 thomas if (*new_view)
5933 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
5934 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5935 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5936 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5937 a5d43cac 2022-07-01 thomas if (err)
5938 a5d43cac 2022-07-01 thomas break;
5939 a5d43cac 2022-07-01 thomas }
5940 a5d43cac 2022-07-01 thomas
5941 e78dc838 2020-12-04 stsp view->focussed = 0;
5942 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5943 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5944 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5945 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5946 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
5947 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5948 1e37a5c2 2019-05-12 jcs if (err)
5949 34bc9ec9 2019-02-22 stsp break;
5950 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5951 40236d76 2022-06-23 thomas if (err)
5952 40236d76 2022-06-23 thomas break;
5953 e78dc838 2020-12-04 stsp view->focus_child = 1;
5954 1e37a5c2 2019-05-12 jcs } else
5955 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5956 1e37a5c2 2019-05-12 jcs if (err)
5957 e5a0f69f 2018-08-18 stsp break;
5958 1e37a5c2 2019-05-12 jcs break;
5959 1e37a5c2 2019-05-12 jcs }
5960 70f17a53 2022-06-13 thomas case CTRL('d'):
5961 23427b14 2022-06-23 thomas case 'd':
5962 70f17a53 2022-06-13 thomas nscroll /= 2;
5963 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5964 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5965 ea025d1d 2020-02-22 naddy case CTRL('f'):
5966 1c5e5faa 2022-06-23 thomas case 'f':
5967 1e37a5c2 2019-05-12 jcs case ' ':
5968 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5969 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5970 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5971 07b0611c 2022-06-23 thomas view->count = 0;
5972 e5a0f69f 2018-08-18 stsp break;
5973 1e37a5c2 2019-05-12 jcs }
5974 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5975 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5976 70f17a53 2022-06-13 thomas s->selected_line +=
5977 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5978 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5979 1e37a5c2 2019-05-12 jcs }
5980 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5981 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5982 1e37a5c2 2019-05-12 jcs else
5983 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5984 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5985 1e37a5c2 2019-05-12 jcs break;
5986 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5987 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5988 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5989 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5990 1e37a5c2 2019-05-12 jcs }
5991 1e37a5c2 2019-05-12 jcs break;
5992 1e37a5c2 2019-05-12 jcs default:
5993 07b0611c 2022-06-23 thomas view->count = 0;
5994 1e37a5c2 2019-05-12 jcs break;
5995 a70480e0 2018-06-23 stsp }
5996 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5997 adf4c9e0 2022-07-03 thomas }
5998 adf4c9e0 2022-07-03 thomas
5999 adf4c9e0 2022-07-03 thomas static const struct got_error *
6000 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6001 adf4c9e0 2022-07-03 thomas {
6002 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6003 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6004 adf4c9e0 2022-07-03 thomas
6005 adf4c9e0 2022-07-03 thomas view->count = 0;
6006 adf4c9e0 2022-07-03 thomas s->done = 1;
6007 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6008 adf4c9e0 2022-07-03 thomas s->done = 0;
6009 adf4c9e0 2022-07-03 thomas if (err)
6010 adf4c9e0 2022-07-03 thomas return err;
6011 adf4c9e0 2022-07-03 thomas return run_blame(view);
6012 a70480e0 2018-06-23 stsp }
6013 a70480e0 2018-06-23 stsp
6014 a70480e0 2018-06-23 stsp static const struct got_error *
6015 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6016 9f7d7167 2018-04-29 stsp {
6017 a70480e0 2018-06-23 stsp const struct got_error *error;
6018 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6019 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6020 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6021 0587e10c 2020-07-23 stsp char *link_target = NULL;
6022 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6023 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6024 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6025 a70480e0 2018-06-23 stsp int ch;
6026 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6027 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6028 a70480e0 2018-06-23 stsp
6029 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6030 a70480e0 2018-06-23 stsp switch (ch) {
6031 a70480e0 2018-06-23 stsp case 'c':
6032 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6033 a70480e0 2018-06-23 stsp break;
6034 69069811 2018-08-02 stsp case 'r':
6035 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6036 69069811 2018-08-02 stsp if (repo_path == NULL)
6037 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6038 9ba1d308 2019-10-21 stsp optarg);
6039 69069811 2018-08-02 stsp break;
6040 a70480e0 2018-06-23 stsp default:
6041 17020d27 2019-03-07 stsp usage_blame();
6042 a70480e0 2018-06-23 stsp /* NOTREACHED */
6043 a70480e0 2018-06-23 stsp }
6044 a70480e0 2018-06-23 stsp }
6045 a70480e0 2018-06-23 stsp
6046 a70480e0 2018-06-23 stsp argc -= optind;
6047 a70480e0 2018-06-23 stsp argv += optind;
6048 a70480e0 2018-06-23 stsp
6049 f135c941 2020-02-20 stsp if (argc != 1)
6050 a70480e0 2018-06-23 stsp usage_blame();
6051 6962eb72 2020-02-20 stsp
6052 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6053 7cd52833 2022-06-23 thomas if (error != NULL)
6054 7cd52833 2022-06-23 thomas goto done;
6055 7cd52833 2022-06-23 thomas
6056 69069811 2018-08-02 stsp if (repo_path == NULL) {
6057 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6058 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6059 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6060 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6061 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6062 c156c7a4 2020-12-18 stsp goto done;
6063 f135c941 2020-02-20 stsp if (worktree)
6064 eb41ed75 2019-02-05 stsp repo_path =
6065 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6066 f135c941 2020-02-20 stsp else
6067 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6068 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6069 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6070 c156c7a4 2020-12-18 stsp goto done;
6071 c156c7a4 2020-12-18 stsp }
6072 f135c941 2020-02-20 stsp }
6073 a915003a 2019-02-05 stsp
6074 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6075 c02c541e 2019-03-29 stsp if (error != NULL)
6076 8e94dd5b 2019-01-04 stsp goto done;
6077 69069811 2018-08-02 stsp
6078 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6079 f135c941 2020-02-20 stsp worktree);
6080 c02c541e 2019-03-29 stsp if (error)
6081 92205607 2019-01-04 stsp goto done;
6082 69069811 2018-08-02 stsp
6083 f135c941 2020-02-20 stsp init_curses();
6084 f135c941 2020-02-20 stsp
6085 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6086 51a10b52 2020-12-26 stsp if (error)
6087 51a10b52 2020-12-26 stsp goto done;
6088 51a10b52 2020-12-26 stsp
6089 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6090 eb41ed75 2019-02-05 stsp if (error)
6091 69069811 2018-08-02 stsp goto done;
6092 a70480e0 2018-06-23 stsp
6093 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6094 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6095 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6096 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6097 a70480e0 2018-06-23 stsp if (error != NULL)
6098 66b4983c 2018-06-23 stsp goto done;
6099 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6100 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6101 a70480e0 2018-06-23 stsp } else {
6102 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6103 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6104 a70480e0 2018-06-23 stsp }
6105 a19e88aa 2018-06-23 stsp if (error != NULL)
6106 8b473291 2019-02-21 stsp goto done;
6107 8b473291 2019-02-21 stsp
6108 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6109 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6110 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6111 e1cd8fed 2018-08-01 stsp goto done;
6112 e1cd8fed 2018-08-01 stsp }
6113 0587e10c 2020-07-23 stsp
6114 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6115 945f9229 2022-04-16 thomas if (error)
6116 945f9229 2022-04-16 thomas goto done;
6117 945f9229 2022-04-16 thomas
6118 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6119 945f9229 2022-04-16 thomas commit, repo);
6120 7cbe629d 2018-08-04 stsp if (error)
6121 7cbe629d 2018-08-04 stsp goto done;
6122 0587e10c 2020-07-23 stsp
6123 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6124 78756c87 2020-11-24 stsp commit_id, repo);
6125 0587e10c 2020-07-23 stsp if (error)
6126 0587e10c 2020-07-23 stsp goto done;
6127 12314ad4 2019-08-31 stsp if (worktree) {
6128 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6129 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6130 12314ad4 2019-08-31 stsp worktree = NULL;
6131 12314ad4 2019-08-31 stsp }
6132 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6133 a70480e0 2018-06-23 stsp done:
6134 69069811 2018-08-02 stsp free(repo_path);
6135 f135c941 2020-02-20 stsp free(in_repo_path);
6136 0587e10c 2020-07-23 stsp free(link_target);
6137 69069811 2018-08-02 stsp free(cwd);
6138 a70480e0 2018-06-23 stsp free(commit_id);
6139 945f9229 2022-04-16 thomas if (commit)
6140 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6141 eb41ed75 2019-02-05 stsp if (worktree)
6142 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6143 1d0f4054 2021-06-17 stsp if (repo) {
6144 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6145 1d0f4054 2021-06-17 stsp if (error == NULL)
6146 1d0f4054 2021-06-17 stsp error = close_err;
6147 1d0f4054 2021-06-17 stsp }
6148 7cd52833 2022-06-23 thomas if (pack_fds) {
6149 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6150 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6151 7cd52833 2022-06-23 thomas if (error == NULL)
6152 7cd52833 2022-06-23 thomas error = pack_err;
6153 7cd52833 2022-06-23 thomas }
6154 51a10b52 2020-12-26 stsp tog_free_refs();
6155 a70480e0 2018-06-23 stsp return error;
6156 ffd1d5e5 2018-06-23 stsp }
6157 ffd1d5e5 2018-06-23 stsp
6158 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6159 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6160 ffd1d5e5 2018-06-23 stsp {
6161 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6162 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6163 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6164 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6165 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6166 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6167 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6168 ffd1d5e5 2018-06-23 stsp
6169 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6170 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6171 a5d43cac 2022-07-01 thomas --limit; /* border */
6172 ffd1d5e5 2018-06-23 stsp
6173 f7d12f7e 2018-08-01 stsp werase(view->window);
6174 ffd1d5e5 2018-06-23 stsp
6175 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6176 ffd1d5e5 2018-06-23 stsp return NULL;
6177 ffd1d5e5 2018-06-23 stsp
6178 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6179 f91a2b48 2022-06-23 thomas 0, 0);
6180 ffd1d5e5 2018-06-23 stsp if (err)
6181 ffd1d5e5 2018-06-23 stsp return err;
6182 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6183 a3404814 2018-09-02 stsp wstandout(view->window);
6184 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6185 11b20872 2019-11-08 stsp if (tc)
6186 11b20872 2019-11-08 stsp wattr_on(view->window,
6187 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6188 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6189 11b20872 2019-11-08 stsp if (tc)
6190 11b20872 2019-11-08 stsp wattr_off(view->window,
6191 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6192 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6193 a3404814 2018-09-02 stsp wstandend(view->window);
6194 2550e4c3 2018-07-13 stsp free(wline);
6195 2550e4c3 2018-07-13 stsp wline = NULL;
6196 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6197 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6198 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6199 ffd1d5e5 2018-06-23 stsp return NULL;
6200 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6201 f91a2b48 2022-06-23 thomas 0, 0);
6202 ce52c690 2018-06-23 stsp if (err)
6203 ce52c690 2018-06-23 stsp return err;
6204 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6205 2550e4c3 2018-07-13 stsp free(wline);
6206 2550e4c3 2018-07-13 stsp wline = NULL;
6207 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6208 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6209 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6210 ffd1d5e5 2018-06-23 stsp return NULL;
6211 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6212 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6213 a1eca9bb 2018-06-23 stsp return NULL;
6214 ffd1d5e5 2018-06-23 stsp
6215 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6216 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6217 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6218 0cf4efb1 2018-09-29 stsp if (view->focussed)
6219 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6220 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6221 ffd1d5e5 2018-06-23 stsp }
6222 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6223 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6224 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6225 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6226 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6227 ffd1d5e5 2018-06-23 stsp return NULL;
6228 ffd1d5e5 2018-06-23 stsp n = 1;
6229 ffd1d5e5 2018-06-23 stsp } else {
6230 ffd1d5e5 2018-06-23 stsp n = 0;
6231 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6232 ffd1d5e5 2018-06-23 stsp }
6233 ffd1d5e5 2018-06-23 stsp
6234 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6235 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6236 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6237 848d6979 2019-08-12 stsp const char *modestr = "";
6238 56e0773d 2019-11-28 stsp mode_t mode;
6239 1d13200f 2018-07-12 stsp
6240 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6241 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6242 56e0773d 2019-11-28 stsp
6243 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6244 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6245 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6246 1d13200f 2018-07-12 stsp if (err)
6247 638f9024 2019-05-13 stsp return got_error_from_errno(
6248 230a42bd 2019-05-11 jcs "got_object_id_str");
6249 1d13200f 2018-07-12 stsp }
6250 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6251 63c5ca5d 2019-08-24 stsp modestr = "$";
6252 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6253 0d6c6ee3 2020-05-20 stsp int i;
6254 0d6c6ee3 2020-05-20 stsp
6255 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6256 d86d3b18 2020-12-01 naddy te, s->repo);
6257 0d6c6ee3 2020-05-20 stsp if (err) {
6258 0d6c6ee3 2020-05-20 stsp free(id_str);
6259 0d6c6ee3 2020-05-20 stsp return err;
6260 0d6c6ee3 2020-05-20 stsp }
6261 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6262 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6263 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6264 0d6c6ee3 2020-05-20 stsp }
6265 848d6979 2019-08-12 stsp modestr = "@";
6266 0d6c6ee3 2020-05-20 stsp }
6267 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6268 848d6979 2019-08-12 stsp modestr = "/";
6269 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6270 848d6979 2019-08-12 stsp modestr = "*";
6271 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6272 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6273 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6274 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6275 1d13200f 2018-07-12 stsp free(id_str);
6276 0d6c6ee3 2020-05-20 stsp free(link_target);
6277 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6278 1d13200f 2018-07-12 stsp }
6279 1d13200f 2018-07-12 stsp free(id_str);
6280 0d6c6ee3 2020-05-20 stsp free(link_target);
6281 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6282 f91a2b48 2022-06-23 thomas 0, 0);
6283 ffd1d5e5 2018-06-23 stsp if (err) {
6284 ffd1d5e5 2018-06-23 stsp free(line);
6285 ffd1d5e5 2018-06-23 stsp break;
6286 ffd1d5e5 2018-06-23 stsp }
6287 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6288 0cf4efb1 2018-09-29 stsp if (view->focussed)
6289 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6290 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6291 ffd1d5e5 2018-06-23 stsp }
6292 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6293 f26dddb7 2019-11-08 stsp if (tc)
6294 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6295 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6296 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6297 f26dddb7 2019-11-08 stsp if (tc)
6298 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6299 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6300 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6302 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6303 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6304 ffd1d5e5 2018-06-23 stsp free(line);
6305 2550e4c3 2018-07-13 stsp free(wline);
6306 2550e4c3 2018-07-13 stsp wline = NULL;
6307 ffd1d5e5 2018-06-23 stsp n++;
6308 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6309 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6310 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6311 ffd1d5e5 2018-06-23 stsp break;
6312 ffd1d5e5 2018-06-23 stsp }
6313 ffd1d5e5 2018-06-23 stsp
6314 ffd1d5e5 2018-06-23 stsp return err;
6315 ffd1d5e5 2018-06-23 stsp }
6316 ffd1d5e5 2018-06-23 stsp
6317 ffd1d5e5 2018-06-23 stsp static void
6318 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6319 ffd1d5e5 2018-06-23 stsp {
6320 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6321 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6322 fa86c4bf 2020-11-29 stsp int i = 0;
6323 ffd1d5e5 2018-06-23 stsp
6324 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6325 ffd1d5e5 2018-06-23 stsp return;
6326 ffd1d5e5 2018-06-23 stsp
6327 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6328 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6329 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6330 fa86c4bf 2020-11-29 stsp if (!isroot)
6331 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6332 fa86c4bf 2020-11-29 stsp break;
6333 fa86c4bf 2020-11-29 stsp }
6334 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6335 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6336 ffd1d5e5 2018-06-23 stsp }
6337 ffd1d5e5 2018-06-23 stsp }
6338 ffd1d5e5 2018-06-23 stsp
6339 a5d43cac 2022-07-01 thomas static const struct got_error *
6340 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6341 ffd1d5e5 2018-06-23 stsp {
6342 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6343 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6344 ffd1d5e5 2018-06-23 stsp int n = 0;
6345 ffd1d5e5 2018-06-23 stsp
6346 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6347 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6348 694d3271 2020-12-01 naddy s->first_displayed_entry);
6349 694d3271 2020-12-01 naddy else
6350 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6351 56e0773d 2019-11-28 stsp
6352 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6353 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6354 a5d43cac 2022-07-01 thomas if (last)
6355 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6356 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6357 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6358 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6359 768394f3 2019-01-24 stsp }
6360 ffd1d5e5 2018-06-23 stsp }
6361 a5d43cac 2022-07-01 thomas
6362 a5d43cac 2022-07-01 thomas return NULL;
6363 ffd1d5e5 2018-06-23 stsp }
6364 ffd1d5e5 2018-06-23 stsp
6365 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6366 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6367 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6368 ffd1d5e5 2018-06-23 stsp {
6369 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6370 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6371 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6372 ffd1d5e5 2018-06-23 stsp
6373 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6374 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6375 56e0773d 2019-11-28 stsp + 1 /* slash */;
6376 ce52c690 2018-06-23 stsp if (te)
6377 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6378 ce52c690 2018-06-23 stsp
6379 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6380 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6381 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6382 ffd1d5e5 2018-06-23 stsp
6383 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6384 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6385 d9765a41 2018-06-23 stsp while (pt) {
6386 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6387 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6388 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6389 cb2ebc8a 2018-06-23 stsp goto done;
6390 cb2ebc8a 2018-06-23 stsp }
6391 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6392 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6393 cb2ebc8a 2018-06-23 stsp goto done;
6394 cb2ebc8a 2018-06-23 stsp }
6395 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6396 ffd1d5e5 2018-06-23 stsp }
6397 ce52c690 2018-06-23 stsp if (te) {
6398 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6399 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6400 ce52c690 2018-06-23 stsp goto done;
6401 ce52c690 2018-06-23 stsp }
6402 cb2ebc8a 2018-06-23 stsp }
6403 ce52c690 2018-06-23 stsp done:
6404 ce52c690 2018-06-23 stsp if (err) {
6405 ce52c690 2018-06-23 stsp free(*path);
6406 ce52c690 2018-06-23 stsp *path = NULL;
6407 ce52c690 2018-06-23 stsp }
6408 ce52c690 2018-06-23 stsp return err;
6409 ce52c690 2018-06-23 stsp }
6410 ce52c690 2018-06-23 stsp
6411 ce52c690 2018-06-23 stsp static const struct got_error *
6412 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6413 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6414 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6415 ce52c690 2018-06-23 stsp {
6416 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6417 ce52c690 2018-06-23 stsp char *path;
6418 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6419 a0de39f3 2019-08-09 stsp
6420 a0de39f3 2019-08-09 stsp *new_view = NULL;
6421 69efd4c4 2018-07-18 stsp
6422 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6423 ce52c690 2018-06-23 stsp if (err)
6424 ce52c690 2018-06-23 stsp return err;
6425 ffd1d5e5 2018-06-23 stsp
6426 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6427 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6428 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6429 83ce39e3 2019-08-12 stsp goto done;
6430 83ce39e3 2019-08-12 stsp }
6431 cdf1ee82 2018-08-01 stsp
6432 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6433 e5a0f69f 2018-08-18 stsp if (err) {
6434 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6435 fc06ba56 2019-08-22 stsp err = NULL;
6436 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6437 e5a0f69f 2018-08-18 stsp } else
6438 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6439 83ce39e3 2019-08-12 stsp done:
6440 83ce39e3 2019-08-12 stsp free(path);
6441 69efd4c4 2018-07-18 stsp return err;
6442 69efd4c4 2018-07-18 stsp }
6443 69efd4c4 2018-07-18 stsp
6444 69efd4c4 2018-07-18 stsp static const struct got_error *
6445 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6446 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6447 69efd4c4 2018-07-18 stsp {
6448 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6449 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6450 69efd4c4 2018-07-18 stsp char *path;
6451 a0de39f3 2019-08-09 stsp
6452 a0de39f3 2019-08-09 stsp *new_view = NULL;
6453 69efd4c4 2018-07-18 stsp
6454 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6455 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6456 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6457 e5a0f69f 2018-08-18 stsp
6458 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6459 69efd4c4 2018-07-18 stsp if (err)
6460 69efd4c4 2018-07-18 stsp return err;
6461 69efd4c4 2018-07-18 stsp
6462 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6463 4e97c21c 2020-12-06 stsp path, 0);
6464 ba4f502b 2018-08-04 stsp if (err)
6465 e5a0f69f 2018-08-18 stsp view_close(log_view);
6466 e5a0f69f 2018-08-18 stsp else
6467 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6468 cb2ebc8a 2018-06-23 stsp free(path);
6469 cb2ebc8a 2018-06-23 stsp return err;
6470 ffd1d5e5 2018-06-23 stsp }
6471 ffd1d5e5 2018-06-23 stsp
6472 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6473 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6474 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6475 ffd1d5e5 2018-06-23 stsp {
6476 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6477 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6478 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6479 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6480 ffd1d5e5 2018-06-23 stsp
6481 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6482 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6483 bc573f3b 2021-07-10 stsp
6484 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6485 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6486 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6487 ffd1d5e5 2018-06-23 stsp
6488 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6489 bc573f3b 2021-07-10 stsp if (err)
6490 bc573f3b 2021-07-10 stsp goto done;
6491 bc573f3b 2021-07-10 stsp
6492 bc573f3b 2021-07-10 stsp /*
6493 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6494 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6495 bc573f3b 2021-07-10 stsp * closed on demand.
6496 bc573f3b 2021-07-10 stsp */
6497 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6498 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6499 bc573f3b 2021-07-10 stsp if (err)
6500 bc573f3b 2021-07-10 stsp goto done;
6501 bc573f3b 2021-07-10 stsp s->tree = s->root;
6502 bc573f3b 2021-07-10 stsp
6503 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6504 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6505 ffd1d5e5 2018-06-23 stsp goto done;
6506 ffd1d5e5 2018-06-23 stsp
6507 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6509 ffd1d5e5 2018-06-23 stsp goto done;
6510 ffd1d5e5 2018-06-23 stsp }
6511 ffd1d5e5 2018-06-23 stsp
6512 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6513 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6514 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6515 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6516 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6517 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6518 9cd7cbd1 2020-12-07 stsp goto done;
6519 9cd7cbd1 2020-12-07 stsp }
6520 9cd7cbd1 2020-12-07 stsp }
6521 fb2756b9 2018-08-04 stsp s->repo = repo;
6522 c0b01bdb 2019-11-08 stsp
6523 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6524 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6525 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6526 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6527 c0b01bdb 2019-11-08 stsp if (err)
6528 c0b01bdb 2019-11-08 stsp goto done;
6529 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6530 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6531 bc573f3b 2021-07-10 stsp if (err)
6532 c0b01bdb 2019-11-08 stsp goto done;
6533 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6534 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6535 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6536 bc573f3b 2021-07-10 stsp if (err)
6537 c0b01bdb 2019-11-08 stsp goto done;
6538 e5a0f69f 2018-08-18 stsp
6539 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6540 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6541 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6542 bc573f3b 2021-07-10 stsp if (err)
6543 11b20872 2019-11-08 stsp goto done;
6544 11b20872 2019-11-08 stsp
6545 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6546 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6547 bc573f3b 2021-07-10 stsp if (err)
6548 c0b01bdb 2019-11-08 stsp goto done;
6549 c0b01bdb 2019-11-08 stsp }
6550 c0b01bdb 2019-11-08 stsp
6551 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6552 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6553 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6554 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6555 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6556 ad80ab7b 2018-08-04 stsp done:
6557 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6558 bc573f3b 2021-07-10 stsp if (commit)
6559 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6560 bc573f3b 2021-07-10 stsp if (err)
6561 bc573f3b 2021-07-10 stsp close_tree_view(view);
6562 ad80ab7b 2018-08-04 stsp return err;
6563 ad80ab7b 2018-08-04 stsp }
6564 ad80ab7b 2018-08-04 stsp
6565 e5a0f69f 2018-08-18 stsp static const struct got_error *
6566 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6567 ad80ab7b 2018-08-04 stsp {
6568 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6569 ad80ab7b 2018-08-04 stsp
6570 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6571 fb2756b9 2018-08-04 stsp free(s->tree_label);
6572 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6573 6484ec90 2018-09-29 stsp free(s->commit_id);
6574 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6575 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6576 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6577 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6578 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6579 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6580 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6581 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6582 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6583 ad80ab7b 2018-08-04 stsp free(parent);
6584 ad80ab7b 2018-08-04 stsp
6585 ad80ab7b 2018-08-04 stsp }
6586 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6587 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6588 bc573f3b 2021-07-10 stsp if (s->root)
6589 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6590 7c32bd05 2019-06-22 stsp return NULL;
6591 7c32bd05 2019-06-22 stsp }
6592 7c32bd05 2019-06-22 stsp
6593 7c32bd05 2019-06-22 stsp static const struct got_error *
6594 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6595 7c32bd05 2019-06-22 stsp {
6596 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6597 7c32bd05 2019-06-22 stsp
6598 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6599 7c32bd05 2019-06-22 stsp return NULL;
6600 7c32bd05 2019-06-22 stsp }
6601 7c32bd05 2019-06-22 stsp
6602 7c32bd05 2019-06-22 stsp static int
6603 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6604 7c32bd05 2019-06-22 stsp {
6605 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6606 7c32bd05 2019-06-22 stsp
6607 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6608 56e0773d 2019-11-28 stsp 0) == 0;
6609 7c32bd05 2019-06-22 stsp }
6610 7c32bd05 2019-06-22 stsp
6611 7c32bd05 2019-06-22 stsp static const struct got_error *
6612 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6613 7c32bd05 2019-06-22 stsp {
6614 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6615 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6616 7c32bd05 2019-06-22 stsp
6617 7c32bd05 2019-06-22 stsp if (!view->searching) {
6618 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6619 7c32bd05 2019-06-22 stsp return NULL;
6620 7c32bd05 2019-06-22 stsp }
6621 7c32bd05 2019-06-22 stsp
6622 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6623 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6624 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6625 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6626 56e0773d 2019-11-28 stsp s->selected_entry);
6627 7c32bd05 2019-06-22 stsp else
6628 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6629 56e0773d 2019-11-28 stsp } else {
6630 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6631 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6632 56e0773d 2019-11-28 stsp else
6633 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6634 56e0773d 2019-11-28 stsp s->selected_entry);
6635 7c32bd05 2019-06-22 stsp }
6636 7c32bd05 2019-06-22 stsp } else {
6637 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6638 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6639 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6640 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6641 56e0773d 2019-11-28 stsp else
6642 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6643 7c32bd05 2019-06-22 stsp }
6644 7c32bd05 2019-06-22 stsp
6645 7c32bd05 2019-06-22 stsp while (1) {
6646 56e0773d 2019-11-28 stsp if (te == NULL) {
6647 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6648 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6649 ac66afb8 2019-06-24 stsp return NULL;
6650 ac66afb8 2019-06-24 stsp }
6651 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6652 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6653 56e0773d 2019-11-28 stsp else
6654 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6655 7c32bd05 2019-06-22 stsp }
6656 7c32bd05 2019-06-22 stsp
6657 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6658 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6659 56e0773d 2019-11-28 stsp s->matched_entry = te;
6660 7c32bd05 2019-06-22 stsp break;
6661 7c32bd05 2019-06-22 stsp }
6662 7c32bd05 2019-06-22 stsp
6663 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6664 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6665 56e0773d 2019-11-28 stsp else
6666 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6667 7c32bd05 2019-06-22 stsp }
6668 e5a0f69f 2018-08-18 stsp
6669 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6670 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6671 7c32bd05 2019-06-22 stsp s->selected = 0;
6672 7c32bd05 2019-06-22 stsp }
6673 7c32bd05 2019-06-22 stsp
6674 e5a0f69f 2018-08-18 stsp return NULL;
6675 ad80ab7b 2018-08-04 stsp }
6676 ad80ab7b 2018-08-04 stsp
6677 ad80ab7b 2018-08-04 stsp static const struct got_error *
6678 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6679 ad80ab7b 2018-08-04 stsp {
6680 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6681 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6682 e5a0f69f 2018-08-18 stsp char *parent_path;
6683 ad80ab7b 2018-08-04 stsp
6684 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6685 e5a0f69f 2018-08-18 stsp if (err)
6686 e5a0f69f 2018-08-18 stsp return err;
6687 ffd1d5e5 2018-06-23 stsp
6688 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6689 e5a0f69f 2018-08-18 stsp free(parent_path);
6690 669b5ffa 2018-10-07 stsp
6691 a5d43cac 2022-07-01 thomas view_border(view);
6692 e5a0f69f 2018-08-18 stsp return err;
6693 e5a0f69f 2018-08-18 stsp }
6694 ce52c690 2018-06-23 stsp
6695 e5a0f69f 2018-08-18 stsp static const struct got_error *
6696 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6697 e5a0f69f 2018-08-18 stsp {
6698 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6699 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6700 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6701 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6702 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6703 ffd1d5e5 2018-06-23 stsp
6704 e5a0f69f 2018-08-18 stsp switch (ch) {
6705 1e37a5c2 2019-05-12 jcs case 'i':
6706 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6707 07b0611c 2022-06-23 thomas view->count = 0;
6708 1e37a5c2 2019-05-12 jcs break;
6709 1e37a5c2 2019-05-12 jcs case 'l':
6710 07b0611c 2022-06-23 thomas view->count = 0;
6711 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6712 ffd1d5e5 2018-06-23 stsp break;
6713 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6714 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6715 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6716 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6717 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6718 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6719 444d5325 2022-07-03 thomas if (err)
6720 444d5325 2022-07-03 thomas break;
6721 444d5325 2022-07-03 thomas }
6722 e78dc838 2020-12-04 stsp view->focussed = 0;
6723 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6724 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6725 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6726 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6727 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6728 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6729 1e37a5c2 2019-05-12 jcs if (err)
6730 1e37a5c2 2019-05-12 jcs return err;
6731 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6732 40236d76 2022-06-23 thomas if (err)
6733 40236d76 2022-06-23 thomas return err;
6734 e78dc838 2020-12-04 stsp view->focus_child = 1;
6735 1e37a5c2 2019-05-12 jcs } else
6736 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6737 152c1c93 2020-11-29 stsp break;
6738 152c1c93 2020-11-29 stsp case 'r':
6739 07b0611c 2022-06-23 thomas view->count = 0;
6740 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6741 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6742 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6743 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6744 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6745 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6746 152c1c93 2020-11-29 stsp if (err) {
6747 152c1c93 2020-11-29 stsp view_close(ref_view);
6748 152c1c93 2020-11-29 stsp return err;
6749 152c1c93 2020-11-29 stsp }
6750 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6751 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6752 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6753 444d5325 2022-07-03 thomas if (err)
6754 444d5325 2022-07-03 thomas break;
6755 444d5325 2022-07-03 thomas }
6756 e78dc838 2020-12-04 stsp view->focussed = 0;
6757 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6758 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6759 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6760 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6761 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6762 152c1c93 2020-11-29 stsp err = view_close_child(view);
6763 152c1c93 2020-11-29 stsp if (err)
6764 152c1c93 2020-11-29 stsp return err;
6765 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6766 40236d76 2022-06-23 thomas if (err)
6767 40236d76 2022-06-23 thomas return err;
6768 e78dc838 2020-12-04 stsp view->focus_child = 1;
6769 152c1c93 2020-11-29 stsp } else
6770 152c1c93 2020-11-29 stsp *new_view = ref_view;
6771 e4526bf5 2021-09-03 naddy break;
6772 e4526bf5 2021-09-03 naddy case 'g':
6773 e4526bf5 2021-09-03 naddy case KEY_HOME:
6774 e4526bf5 2021-09-03 naddy s->selected = 0;
6775 07b0611c 2022-06-23 thomas view->count = 0;
6776 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6777 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6778 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6779 e4526bf5 2021-09-03 naddy else
6780 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6781 1e37a5c2 2019-05-12 jcs break;
6782 e4526bf5 2021-09-03 naddy case 'G':
6783 a5d43cac 2022-07-01 thomas case KEY_END: {
6784 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6785 a5d43cac 2022-07-01 thomas
6786 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6787 a5d43cac 2022-07-01 thomas --eos; /* border */
6788 e4526bf5 2021-09-03 naddy s->selected = 0;
6789 07b0611c 2022-06-23 thomas view->count = 0;
6790 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6791 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6792 e4526bf5 2021-09-03 naddy if (te == NULL) {
6793 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6794 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6795 e4526bf5 2021-09-03 naddy n++;
6796 e4526bf5 2021-09-03 naddy }
6797 e4526bf5 2021-09-03 naddy break;
6798 e4526bf5 2021-09-03 naddy }
6799 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6800 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6801 e4526bf5 2021-09-03 naddy }
6802 e4526bf5 2021-09-03 naddy if (n > 0)
6803 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6804 e4526bf5 2021-09-03 naddy break;
6805 a5d43cac 2022-07-01 thomas }
6806 1e37a5c2 2019-05-12 jcs case 'k':
6807 1e37a5c2 2019-05-12 jcs case KEY_UP:
6808 f7140bf5 2021-10-17 thomas case CTRL('p'):
6809 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6810 1e37a5c2 2019-05-12 jcs s->selected--;
6811 fa86c4bf 2020-11-29 stsp break;
6812 1e37a5c2 2019-05-12 jcs }
6813 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6814 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6815 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6816 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6817 07b0611c 2022-06-23 thomas view->count = 0;
6818 1e37a5c2 2019-05-12 jcs break;
6819 70f17a53 2022-06-13 thomas case CTRL('u'):
6820 23427b14 2022-06-23 thomas case 'u':
6821 70f17a53 2022-06-13 thomas nscroll /= 2;
6822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6823 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6824 ea025d1d 2020-02-22 naddy case CTRL('b'):
6825 1c5e5faa 2022-06-23 thomas case 'b':
6826 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6827 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6828 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6829 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6830 fa86c4bf 2020-11-29 stsp } else {
6831 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6832 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6833 fa86c4bf 2020-11-29 stsp }
6834 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6835 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6836 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6837 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6838 07b0611c 2022-06-23 thomas view->count = 0;
6839 1e37a5c2 2019-05-12 jcs break;
6840 1e37a5c2 2019-05-12 jcs case 'j':
6841 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6842 f7140bf5 2021-10-17 thomas case CTRL('n'):
6843 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6844 1e37a5c2 2019-05-12 jcs s->selected++;
6845 1e37a5c2 2019-05-12 jcs break;
6846 1e37a5c2 2019-05-12 jcs }
6847 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6848 07b0611c 2022-06-23 thomas == NULL) {
6849 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6850 07b0611c 2022-06-23 thomas view->count = 0;
6851 1e37a5c2 2019-05-12 jcs break;
6852 07b0611c 2022-06-23 thomas }
6853 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6854 1e37a5c2 2019-05-12 jcs break;
6855 70f17a53 2022-06-13 thomas case CTRL('d'):
6856 23427b14 2022-06-23 thomas case 'd':
6857 70f17a53 2022-06-13 thomas nscroll /= 2;
6858 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6859 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6860 ea025d1d 2020-02-22 naddy case CTRL('f'):
6861 1c5e5faa 2022-06-23 thomas case 'f':
6862 4c2d69cb 2022-06-23 thomas case ' ':
6863 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6864 1e37a5c2 2019-05-12 jcs == NULL) {
6865 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6866 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6867 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6868 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6869 07b0611c 2022-06-23 thomas else
6870 07b0611c 2022-06-23 thomas view->count = 0;
6871 1e37a5c2 2019-05-12 jcs break;
6872 1e37a5c2 2019-05-12 jcs }
6873 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6874 1e37a5c2 2019-05-12 jcs break;
6875 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6876 1e37a5c2 2019-05-12 jcs case '\r':
6877 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6878 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6879 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6880 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6881 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6882 07b0611c 2022-06-23 thomas view->count = 0;
6883 1e37a5c2 2019-05-12 jcs break;
6884 07b0611c 2022-06-23 thomas }
6885 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6886 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6887 1e37a5c2 2019-05-12 jcs entry);
6888 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6889 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6890 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6891 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6892 1e37a5c2 2019-05-12 jcs s->selected_entry =
6893 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6894 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6895 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6897 a5d43cac 2022-07-01 thomas if (err)
6898 a5d43cac 2022-07-01 thomas break;
6899 a5d43cac 2022-07-01 thomas }
6900 1e37a5c2 2019-05-12 jcs free(parent);
6901 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6902 56e0773d 2019-11-28 stsp s->selected_entry))) {
6903 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6904 07b0611c 2022-06-23 thomas view->count = 0;
6905 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6906 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6907 1e37a5c2 2019-05-12 jcs if (err)
6908 1e37a5c2 2019-05-12 jcs break;
6909 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6910 941e9f74 2019-05-21 stsp if (err) {
6911 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6912 1e37a5c2 2019-05-12 jcs break;
6913 1e37a5c2 2019-05-12 jcs }
6914 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6915 56e0773d 2019-11-28 stsp s->selected_entry))) {
6916 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6917 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
6918 1e37a5c2 2019-05-12 jcs
6919 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
6920 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
6921 a5d43cac 2022-07-01 thomas
6922 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
6923 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6924 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6925 1e37a5c2 2019-05-12 jcs if (err)
6926 1e37a5c2 2019-05-12 jcs break;
6927 a5d43cac 2022-07-01 thomas
6928 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6929 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6930 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6931 a5d43cac 2022-07-01 thomas if (err)
6932 a5d43cac 2022-07-01 thomas break;
6933 a5d43cac 2022-07-01 thomas }
6934 a5d43cac 2022-07-01 thomas
6935 07b0611c 2022-06-23 thomas view->count = 0;
6936 e78dc838 2020-12-04 stsp view->focussed = 0;
6937 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6938 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
6939 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
6940 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6941 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
6942 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6943 669b5ffa 2018-10-07 stsp if (err)
6944 669b5ffa 2018-10-07 stsp return err;
6945 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
6946 40236d76 2022-06-23 thomas if (err)
6947 40236d76 2022-06-23 thomas return err;
6948 e78dc838 2020-12-04 stsp view->focus_child = 1;
6949 669b5ffa 2018-10-07 stsp } else
6950 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6951 1e37a5c2 2019-05-12 jcs }
6952 1e37a5c2 2019-05-12 jcs break;
6953 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6954 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6955 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6956 07b0611c 2022-06-23 thomas view->count = 0;
6957 1e37a5c2 2019-05-12 jcs break;
6958 1e37a5c2 2019-05-12 jcs default:
6959 07b0611c 2022-06-23 thomas view->count = 0;
6960 1e37a5c2 2019-05-12 jcs break;
6961 ffd1d5e5 2018-06-23 stsp }
6962 e5a0f69f 2018-08-18 stsp
6963 ffd1d5e5 2018-06-23 stsp return err;
6964 9f7d7167 2018-04-29 stsp }
6965 9f7d7167 2018-04-29 stsp
6966 ffd1d5e5 2018-06-23 stsp __dead static void
6967 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6968 ffd1d5e5 2018-06-23 stsp {
6969 ffd1d5e5 2018-06-23 stsp endwin();
6970 91198554 2022-06-23 thomas fprintf(stderr,
6971 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6972 ffd1d5e5 2018-06-23 stsp getprogname());
6973 ffd1d5e5 2018-06-23 stsp exit(1);
6974 ffd1d5e5 2018-06-23 stsp }
6975 ffd1d5e5 2018-06-23 stsp
6976 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6977 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6978 ffd1d5e5 2018-06-23 stsp {
6979 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6980 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6981 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6982 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6983 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6984 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6985 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6986 4e97c21c 2020-12-06 stsp char *label = NULL;
6987 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6988 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6989 ffd1d5e5 2018-06-23 stsp int ch;
6990 5221c383 2018-08-01 stsp struct tog_view *view;
6991 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6992 70ac5f84 2019-03-28 stsp
6993 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6994 ffd1d5e5 2018-06-23 stsp switch (ch) {
6995 ffd1d5e5 2018-06-23 stsp case 'c':
6996 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6997 74283ab8 2020-02-07 stsp break;
6998 74283ab8 2020-02-07 stsp case 'r':
6999 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7000 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7001 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7002 74283ab8 2020-02-07 stsp optarg);
7003 ffd1d5e5 2018-06-23 stsp break;
7004 ffd1d5e5 2018-06-23 stsp default:
7005 e99e2d15 2020-11-24 naddy usage_tree();
7006 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7007 ffd1d5e5 2018-06-23 stsp }
7008 ffd1d5e5 2018-06-23 stsp }
7009 ffd1d5e5 2018-06-23 stsp
7010 ffd1d5e5 2018-06-23 stsp argc -= optind;
7011 ffd1d5e5 2018-06-23 stsp argv += optind;
7012 ffd1d5e5 2018-06-23 stsp
7013 55cccc34 2020-02-20 stsp if (argc > 1)
7014 e99e2d15 2020-11-24 naddy usage_tree();
7015 7cd52833 2022-06-23 thomas
7016 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7017 7cd52833 2022-06-23 thomas if (error != NULL)
7018 7cd52833 2022-06-23 thomas goto done;
7019 74283ab8 2020-02-07 stsp
7020 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7021 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7022 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7023 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7024 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7025 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7026 c156c7a4 2020-12-18 stsp goto done;
7027 55cccc34 2020-02-20 stsp if (worktree)
7028 52185f70 2019-02-05 stsp repo_path =
7029 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7030 55cccc34 2020-02-20 stsp else
7031 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7032 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7033 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7034 c156c7a4 2020-12-18 stsp goto done;
7035 c156c7a4 2020-12-18 stsp }
7036 55cccc34 2020-02-20 stsp }
7037 a915003a 2019-02-05 stsp
7038 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7039 c02c541e 2019-03-29 stsp if (error != NULL)
7040 52185f70 2019-02-05 stsp goto done;
7041 d188b9a6 2019-01-04 stsp
7042 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7043 55cccc34 2020-02-20 stsp repo, worktree);
7044 55cccc34 2020-02-20 stsp if (error)
7045 55cccc34 2020-02-20 stsp goto done;
7046 55cccc34 2020-02-20 stsp
7047 55cccc34 2020-02-20 stsp init_curses();
7048 55cccc34 2020-02-20 stsp
7049 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7050 c02c541e 2019-03-29 stsp if (error)
7051 52185f70 2019-02-05 stsp goto done;
7052 ffd1d5e5 2018-06-23 stsp
7053 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7054 51a10b52 2020-12-26 stsp if (error)
7055 51a10b52 2020-12-26 stsp goto done;
7056 51a10b52 2020-12-26 stsp
7057 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7058 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7059 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7060 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7061 4e97c21c 2020-12-06 stsp if (error)
7062 4e97c21c 2020-12-06 stsp goto done;
7063 4e97c21c 2020-12-06 stsp head_ref_name = label;
7064 4e97c21c 2020-12-06 stsp } else {
7065 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7066 4e97c21c 2020-12-06 stsp if (error == NULL)
7067 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7068 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7069 4e97c21c 2020-12-06 stsp goto done;
7070 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7071 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7072 4e97c21c 2020-12-06 stsp if (error)
7073 4e97c21c 2020-12-06 stsp goto done;
7074 4e97c21c 2020-12-06 stsp }
7075 ffd1d5e5 2018-06-23 stsp
7076 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7077 945f9229 2022-04-16 thomas if (error)
7078 945f9229 2022-04-16 thomas goto done;
7079 945f9229 2022-04-16 thomas
7080 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7081 5221c383 2018-08-01 stsp if (view == NULL) {
7082 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7083 5221c383 2018-08-01 stsp goto done;
7084 5221c383 2018-08-01 stsp }
7085 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7086 ad80ab7b 2018-08-04 stsp if (error)
7087 ad80ab7b 2018-08-04 stsp goto done;
7088 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7089 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7090 d91faf3b 2020-12-01 naddy in_repo_path);
7091 55cccc34 2020-02-20 stsp if (error)
7092 55cccc34 2020-02-20 stsp goto done;
7093 55cccc34 2020-02-20 stsp }
7094 55cccc34 2020-02-20 stsp
7095 55cccc34 2020-02-20 stsp if (worktree) {
7096 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7097 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7098 55cccc34 2020-02-20 stsp worktree = NULL;
7099 55cccc34 2020-02-20 stsp }
7100 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7101 ffd1d5e5 2018-06-23 stsp done:
7102 52185f70 2019-02-05 stsp free(repo_path);
7103 e4a0e26d 2020-02-20 stsp free(cwd);
7104 ffd1d5e5 2018-06-23 stsp free(commit_id);
7105 4e97c21c 2020-12-06 stsp free(label);
7106 486cd271 2020-12-06 stsp if (ref)
7107 486cd271 2020-12-06 stsp got_ref_close(ref);
7108 1d0f4054 2021-06-17 stsp if (repo) {
7109 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7110 1d0f4054 2021-06-17 stsp if (error == NULL)
7111 1d0f4054 2021-06-17 stsp error = close_err;
7112 1d0f4054 2021-06-17 stsp }
7113 7cd52833 2022-06-23 thomas if (pack_fds) {
7114 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7115 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7116 7cd52833 2022-06-23 thomas if (error == NULL)
7117 7cd52833 2022-06-23 thomas error = pack_err;
7118 7cd52833 2022-06-23 thomas }
7119 51a10b52 2020-12-26 stsp tog_free_refs();
7120 ffd1d5e5 2018-06-23 stsp return error;
7121 6458efa5 2020-11-24 stsp }
7122 6458efa5 2020-11-24 stsp
7123 6458efa5 2020-11-24 stsp static const struct got_error *
7124 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7125 6458efa5 2020-11-24 stsp {
7126 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7127 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7128 6458efa5 2020-11-24 stsp
7129 6458efa5 2020-11-24 stsp s->nrefs = 0;
7130 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7131 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7132 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7133 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7134 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7135 6458efa5 2020-11-24 stsp continue;
7136 6458efa5 2020-11-24 stsp
7137 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7138 6458efa5 2020-11-24 stsp if (re == NULL)
7139 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7140 6458efa5 2020-11-24 stsp
7141 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7142 8924d611 2020-12-26 stsp if (re->ref == NULL)
7143 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7144 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7145 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7146 6458efa5 2020-11-24 stsp }
7147 6458efa5 2020-11-24 stsp
7148 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7149 6458efa5 2020-11-24 stsp return NULL;
7150 6458efa5 2020-11-24 stsp }
7151 6458efa5 2020-11-24 stsp
7152 ef20f542 2022-06-26 thomas static void
7153 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7154 6458efa5 2020-11-24 stsp {
7155 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7156 6458efa5 2020-11-24 stsp
7157 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7158 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7159 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7160 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7161 6458efa5 2020-11-24 stsp free(re);
7162 6458efa5 2020-11-24 stsp }
7163 6458efa5 2020-11-24 stsp }
7164 6458efa5 2020-11-24 stsp
7165 6458efa5 2020-11-24 stsp static const struct got_error *
7166 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7167 6458efa5 2020-11-24 stsp {
7168 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7169 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7170 6458efa5 2020-11-24 stsp
7171 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7172 6458efa5 2020-11-24 stsp s->repo = repo;
7173 6458efa5 2020-11-24 stsp
7174 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7175 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7176 6458efa5 2020-11-24 stsp
7177 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7178 6458efa5 2020-11-24 stsp if (err)
7179 6458efa5 2020-11-24 stsp return err;
7180 34ba6917 2020-11-30 stsp
7181 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7182 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7183 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7184 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7185 6458efa5 2020-11-24 stsp if (err)
7186 6458efa5 2020-11-24 stsp goto done;
7187 6458efa5 2020-11-24 stsp
7188 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7189 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7190 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7191 6458efa5 2020-11-24 stsp if (err)
7192 6458efa5 2020-11-24 stsp goto done;
7193 6458efa5 2020-11-24 stsp
7194 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7195 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7196 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7197 2183bbf6 2022-01-23 thomas if (err)
7198 2183bbf6 2022-01-23 thomas goto done;
7199 2183bbf6 2022-01-23 thomas
7200 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7201 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7202 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7203 6458efa5 2020-11-24 stsp if (err)
7204 6458efa5 2020-11-24 stsp goto done;
7205 6458efa5 2020-11-24 stsp }
7206 6458efa5 2020-11-24 stsp
7207 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7208 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7209 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7210 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7211 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7212 6458efa5 2020-11-24 stsp done:
7213 6458efa5 2020-11-24 stsp if (err)
7214 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7215 6458efa5 2020-11-24 stsp return err;
7216 6458efa5 2020-11-24 stsp }
7217 6458efa5 2020-11-24 stsp
7218 6458efa5 2020-11-24 stsp static const struct got_error *
7219 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7220 6458efa5 2020-11-24 stsp {
7221 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7222 6458efa5 2020-11-24 stsp
7223 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7224 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7225 6458efa5 2020-11-24 stsp
7226 6458efa5 2020-11-24 stsp return NULL;
7227 9f7d7167 2018-04-29 stsp }
7228 ce5b7c56 2019-07-09 stsp
7229 6458efa5 2020-11-24 stsp static const struct got_error *
7230 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7231 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7232 6458efa5 2020-11-24 stsp {
7233 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7234 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7235 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7236 6458efa5 2020-11-24 stsp int obj_type;
7237 6458efa5 2020-11-24 stsp
7238 c42c9805 2020-11-24 stsp *commit_id = NULL;
7239 6458efa5 2020-11-24 stsp
7240 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7241 6458efa5 2020-11-24 stsp if (err)
7242 6458efa5 2020-11-24 stsp return err;
7243 6458efa5 2020-11-24 stsp
7244 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7245 6458efa5 2020-11-24 stsp if (err)
7246 6458efa5 2020-11-24 stsp goto done;
7247 6458efa5 2020-11-24 stsp
7248 6458efa5 2020-11-24 stsp switch (obj_type) {
7249 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7250 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7251 6458efa5 2020-11-24 stsp break;
7252 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7253 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7254 6458efa5 2020-11-24 stsp if (err)
7255 6458efa5 2020-11-24 stsp goto done;
7256 c42c9805 2020-11-24 stsp free(obj_id);
7257 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7258 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7259 c42c9805 2020-11-24 stsp if (err)
7260 6458efa5 2020-11-24 stsp goto done;
7261 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7262 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7263 c42c9805 2020-11-24 stsp goto done;
7264 c42c9805 2020-11-24 stsp }
7265 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7266 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7267 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7268 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7269 c42c9805 2020-11-24 stsp goto done;
7270 c42c9805 2020-11-24 stsp }
7271 6458efa5 2020-11-24 stsp break;
7272 6458efa5 2020-11-24 stsp default:
7273 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7274 c42c9805 2020-11-24 stsp break;
7275 6458efa5 2020-11-24 stsp }
7276 6458efa5 2020-11-24 stsp
7277 c42c9805 2020-11-24 stsp done:
7278 c42c9805 2020-11-24 stsp if (tag)
7279 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7280 c42c9805 2020-11-24 stsp if (err) {
7281 c42c9805 2020-11-24 stsp free(*commit_id);
7282 c42c9805 2020-11-24 stsp *commit_id = NULL;
7283 c42c9805 2020-11-24 stsp }
7284 c42c9805 2020-11-24 stsp return err;
7285 c42c9805 2020-11-24 stsp }
7286 c42c9805 2020-11-24 stsp
7287 c42c9805 2020-11-24 stsp static const struct got_error *
7288 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7289 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7290 c42c9805 2020-11-24 stsp {
7291 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7292 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7293 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7294 c42c9805 2020-11-24 stsp
7295 c42c9805 2020-11-24 stsp *new_view = NULL;
7296 c42c9805 2020-11-24 stsp
7297 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7298 c42c9805 2020-11-24 stsp if (err) {
7299 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7300 c42c9805 2020-11-24 stsp return err;
7301 c42c9805 2020-11-24 stsp else
7302 c42c9805 2020-11-24 stsp return NULL;
7303 c42c9805 2020-11-24 stsp }
7304 c42c9805 2020-11-24 stsp
7305 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7306 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7307 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7308 6458efa5 2020-11-24 stsp goto done;
7309 6458efa5 2020-11-24 stsp }
7310 6458efa5 2020-11-24 stsp
7311 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7312 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7313 6458efa5 2020-11-24 stsp done:
7314 6458efa5 2020-11-24 stsp if (err)
7315 6458efa5 2020-11-24 stsp view_close(log_view);
7316 6458efa5 2020-11-24 stsp else
7317 6458efa5 2020-11-24 stsp *new_view = log_view;
7318 c42c9805 2020-11-24 stsp free(commit_id);
7319 6458efa5 2020-11-24 stsp return err;
7320 6458efa5 2020-11-24 stsp }
7321 6458efa5 2020-11-24 stsp
7322 ce5b7c56 2019-07-09 stsp static void
7323 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7324 6458efa5 2020-11-24 stsp {
7325 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7326 34ba6917 2020-11-30 stsp int i = 0;
7327 6458efa5 2020-11-24 stsp
7328 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7329 6458efa5 2020-11-24 stsp return;
7330 6458efa5 2020-11-24 stsp
7331 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7332 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7333 34ba6917 2020-11-30 stsp if (re == NULL)
7334 34ba6917 2020-11-30 stsp break;
7335 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7336 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7337 6458efa5 2020-11-24 stsp }
7338 6458efa5 2020-11-24 stsp }
7339 6458efa5 2020-11-24 stsp
7340 a5d43cac 2022-07-01 thomas static const struct got_error *
7341 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7342 6458efa5 2020-11-24 stsp {
7343 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7344 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7345 6458efa5 2020-11-24 stsp int n = 0;
7346 6458efa5 2020-11-24 stsp
7347 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7348 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7349 6458efa5 2020-11-24 stsp else
7350 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7351 6458efa5 2020-11-24 stsp
7352 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7353 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7354 a5d43cac 2022-07-01 thomas if (last)
7355 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7356 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7357 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7358 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7359 6458efa5 2020-11-24 stsp }
7360 6458efa5 2020-11-24 stsp }
7361 a5d43cac 2022-07-01 thomas
7362 a5d43cac 2022-07-01 thomas return NULL;
7363 6458efa5 2020-11-24 stsp }
7364 6458efa5 2020-11-24 stsp
7365 6458efa5 2020-11-24 stsp static const struct got_error *
7366 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7367 6458efa5 2020-11-24 stsp {
7368 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7369 6458efa5 2020-11-24 stsp
7370 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7371 6458efa5 2020-11-24 stsp return NULL;
7372 6458efa5 2020-11-24 stsp }
7373 6458efa5 2020-11-24 stsp
7374 6458efa5 2020-11-24 stsp static int
7375 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7376 6458efa5 2020-11-24 stsp {
7377 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7378 6458efa5 2020-11-24 stsp
7379 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7380 6458efa5 2020-11-24 stsp 0) == 0;
7381 6458efa5 2020-11-24 stsp }
7382 6458efa5 2020-11-24 stsp
7383 6458efa5 2020-11-24 stsp static const struct got_error *
7384 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7385 6458efa5 2020-11-24 stsp {
7386 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7387 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7388 6458efa5 2020-11-24 stsp
7389 6458efa5 2020-11-24 stsp if (!view->searching) {
7390 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7391 6458efa5 2020-11-24 stsp return NULL;
7392 6458efa5 2020-11-24 stsp }
7393 6458efa5 2020-11-24 stsp
7394 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7395 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7396 6458efa5 2020-11-24 stsp if (s->selected_entry)
7397 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7398 6458efa5 2020-11-24 stsp else
7399 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7400 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7401 6458efa5 2020-11-24 stsp } else {
7402 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7403 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7404 6458efa5 2020-11-24 stsp else
7405 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7406 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7407 6458efa5 2020-11-24 stsp }
7408 6458efa5 2020-11-24 stsp } else {
7409 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7410 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7411 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7412 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7413 6458efa5 2020-11-24 stsp else
7414 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7415 6458efa5 2020-11-24 stsp }
7416 6458efa5 2020-11-24 stsp
7417 6458efa5 2020-11-24 stsp while (1) {
7418 6458efa5 2020-11-24 stsp if (re == NULL) {
7419 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7420 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7421 6458efa5 2020-11-24 stsp return NULL;
7422 6458efa5 2020-11-24 stsp }
7423 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7424 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7425 6458efa5 2020-11-24 stsp else
7426 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7427 6458efa5 2020-11-24 stsp }
7428 6458efa5 2020-11-24 stsp
7429 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7430 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7431 6458efa5 2020-11-24 stsp s->matched_entry = re;
7432 6458efa5 2020-11-24 stsp break;
7433 6458efa5 2020-11-24 stsp }
7434 6458efa5 2020-11-24 stsp
7435 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7436 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7437 6458efa5 2020-11-24 stsp else
7438 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7439 6458efa5 2020-11-24 stsp }
7440 6458efa5 2020-11-24 stsp
7441 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7442 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7443 6458efa5 2020-11-24 stsp s->selected = 0;
7444 6458efa5 2020-11-24 stsp }
7445 6458efa5 2020-11-24 stsp
7446 6458efa5 2020-11-24 stsp return NULL;
7447 6458efa5 2020-11-24 stsp }
7448 6458efa5 2020-11-24 stsp
7449 6458efa5 2020-11-24 stsp static const struct got_error *
7450 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7451 6458efa5 2020-11-24 stsp {
7452 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7453 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7454 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7455 6458efa5 2020-11-24 stsp char *line = NULL;
7456 6458efa5 2020-11-24 stsp wchar_t *wline;
7457 6458efa5 2020-11-24 stsp struct tog_color *tc;
7458 6458efa5 2020-11-24 stsp int width, n;
7459 6458efa5 2020-11-24 stsp int limit = view->nlines;
7460 6458efa5 2020-11-24 stsp
7461 6458efa5 2020-11-24 stsp werase(view->window);
7462 6458efa5 2020-11-24 stsp
7463 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7464 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7465 a5d43cac 2022-07-01 thomas --limit; /* border */
7466 6458efa5 2020-11-24 stsp
7467 6458efa5 2020-11-24 stsp if (limit == 0)
7468 6458efa5 2020-11-24 stsp return NULL;
7469 6458efa5 2020-11-24 stsp
7470 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7471 6458efa5 2020-11-24 stsp
7472 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7473 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7474 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7475 6458efa5 2020-11-24 stsp
7476 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7477 6458efa5 2020-11-24 stsp if (err) {
7478 6458efa5 2020-11-24 stsp free(line);
7479 6458efa5 2020-11-24 stsp return err;
7480 6458efa5 2020-11-24 stsp }
7481 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7482 6458efa5 2020-11-24 stsp wstandout(view->window);
7483 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7484 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7485 6458efa5 2020-11-24 stsp wstandend(view->window);
7486 6458efa5 2020-11-24 stsp free(wline);
7487 6458efa5 2020-11-24 stsp wline = NULL;
7488 6458efa5 2020-11-24 stsp free(line);
7489 6458efa5 2020-11-24 stsp line = NULL;
7490 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7491 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7492 6458efa5 2020-11-24 stsp if (--limit <= 0)
7493 6458efa5 2020-11-24 stsp return NULL;
7494 6458efa5 2020-11-24 stsp
7495 6458efa5 2020-11-24 stsp n = 0;
7496 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7497 6458efa5 2020-11-24 stsp char *line = NULL;
7498 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7499 6458efa5 2020-11-24 stsp
7500 84227eb1 2022-06-23 thomas if (s->show_date) {
7501 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7502 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7503 84227eb1 2022-06-23 thomas struct got_object_id *id;
7504 84227eb1 2022-06-23 thomas struct tm tm;
7505 84227eb1 2022-06-23 thomas time_t t;
7506 84227eb1 2022-06-23 thomas
7507 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7508 84227eb1 2022-06-23 thomas if (err)
7509 84227eb1 2022-06-23 thomas return err;
7510 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7511 84227eb1 2022-06-23 thomas if (err) {
7512 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7513 84227eb1 2022-06-23 thomas free(id);
7514 84227eb1 2022-06-23 thomas return err;
7515 84227eb1 2022-06-23 thomas }
7516 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7517 84227eb1 2022-06-23 thomas id);
7518 84227eb1 2022-06-23 thomas if (err) {
7519 84227eb1 2022-06-23 thomas free(id);
7520 84227eb1 2022-06-23 thomas return err;
7521 84227eb1 2022-06-23 thomas }
7522 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7523 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7524 84227eb1 2022-06-23 thomas } else {
7525 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7526 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7527 84227eb1 2022-06-23 thomas }
7528 84227eb1 2022-06-23 thomas free(id);
7529 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7530 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7531 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7532 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7533 84227eb1 2022-06-23 thomas }
7534 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7535 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7536 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7537 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7538 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7539 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7540 6458efa5 2020-11-24 stsp struct got_object_id *id;
7541 6458efa5 2020-11-24 stsp char *id_str;
7542 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7543 6458efa5 2020-11-24 stsp if (err)
7544 6458efa5 2020-11-24 stsp return err;
7545 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7546 6458efa5 2020-11-24 stsp if (err) {
7547 6458efa5 2020-11-24 stsp free(id);
7548 6458efa5 2020-11-24 stsp return err;
7549 6458efa5 2020-11-24 stsp }
7550 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7551 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7552 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7553 6458efa5 2020-11-24 stsp free(id);
7554 6458efa5 2020-11-24 stsp free(id_str);
7555 6458efa5 2020-11-24 stsp return err;
7556 6458efa5 2020-11-24 stsp }
7557 6458efa5 2020-11-24 stsp free(id);
7558 6458efa5 2020-11-24 stsp free(id_str);
7559 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7560 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7561 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7562 6458efa5 2020-11-24 stsp
7563 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7564 f91a2b48 2022-06-23 thomas 0, 0);
7565 6458efa5 2020-11-24 stsp if (err) {
7566 6458efa5 2020-11-24 stsp free(line);
7567 6458efa5 2020-11-24 stsp return err;
7568 6458efa5 2020-11-24 stsp }
7569 6458efa5 2020-11-24 stsp if (n == s->selected) {
7570 6458efa5 2020-11-24 stsp if (view->focussed)
7571 6458efa5 2020-11-24 stsp wstandout(view->window);
7572 6458efa5 2020-11-24 stsp s->selected_entry = re;
7573 6458efa5 2020-11-24 stsp }
7574 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7575 6458efa5 2020-11-24 stsp if (tc)
7576 6458efa5 2020-11-24 stsp wattr_on(view->window,
7577 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7578 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7579 6458efa5 2020-11-24 stsp if (tc)
7580 6458efa5 2020-11-24 stsp wattr_off(view->window,
7581 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7582 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7583 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7584 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7585 6458efa5 2020-11-24 stsp wstandend(view->window);
7586 6458efa5 2020-11-24 stsp free(line);
7587 6458efa5 2020-11-24 stsp free(wline);
7588 6458efa5 2020-11-24 stsp wline = NULL;
7589 6458efa5 2020-11-24 stsp n++;
7590 6458efa5 2020-11-24 stsp s->ndisplayed++;
7591 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7592 6458efa5 2020-11-24 stsp
7593 6458efa5 2020-11-24 stsp limit--;
7594 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7595 6458efa5 2020-11-24 stsp }
7596 6458efa5 2020-11-24 stsp
7597 a5d43cac 2022-07-01 thomas view_border(view);
7598 6458efa5 2020-11-24 stsp return err;
7599 6458efa5 2020-11-24 stsp }
7600 6458efa5 2020-11-24 stsp
7601 6458efa5 2020-11-24 stsp static const struct got_error *
7602 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7603 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7604 c42c9805 2020-11-24 stsp {
7605 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7606 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7607 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7608 c42c9805 2020-11-24 stsp
7609 c42c9805 2020-11-24 stsp *new_view = NULL;
7610 c42c9805 2020-11-24 stsp
7611 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7612 c42c9805 2020-11-24 stsp if (err) {
7613 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7614 c42c9805 2020-11-24 stsp return err;
7615 c42c9805 2020-11-24 stsp else
7616 c42c9805 2020-11-24 stsp return NULL;
7617 c42c9805 2020-11-24 stsp }
7618 c42c9805 2020-11-24 stsp
7619 c42c9805 2020-11-24 stsp
7620 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7621 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7622 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7623 c42c9805 2020-11-24 stsp goto done;
7624 c42c9805 2020-11-24 stsp }
7625 c42c9805 2020-11-24 stsp
7626 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7627 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7628 c42c9805 2020-11-24 stsp if (err)
7629 c42c9805 2020-11-24 stsp goto done;
7630 c42c9805 2020-11-24 stsp
7631 c42c9805 2020-11-24 stsp *new_view = tree_view;
7632 c42c9805 2020-11-24 stsp done:
7633 c42c9805 2020-11-24 stsp free(commit_id);
7634 c42c9805 2020-11-24 stsp return err;
7635 c42c9805 2020-11-24 stsp }
7636 c42c9805 2020-11-24 stsp static const struct got_error *
7637 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7638 6458efa5 2020-11-24 stsp {
7639 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7640 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7641 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7642 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7643 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7644 6458efa5 2020-11-24 stsp
7645 6458efa5 2020-11-24 stsp switch (ch) {
7646 6458efa5 2020-11-24 stsp case 'i':
7647 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7648 07b0611c 2022-06-23 thomas view->count = 0;
7649 3bfadbd4 2021-11-20 thomas break;
7650 84227eb1 2022-06-23 thomas case 'm':
7651 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7652 07b0611c 2022-06-23 thomas view->count = 0;
7653 84227eb1 2022-06-23 thomas break;
7654 98182bd0 2021-11-20 thomas case 'o':
7655 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7656 07b0611c 2022-06-23 thomas view->count = 0;
7657 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7658 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7659 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7660 c591440f 2021-11-20 thomas if (err)
7661 c591440f 2021-11-20 thomas break;
7662 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7663 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7664 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7665 3bfadbd4 2021-11-20 thomas if (err)
7666 3bfadbd4 2021-11-20 thomas break;
7667 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7668 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7669 6458efa5 2020-11-24 stsp break;
7670 6458efa5 2020-11-24 stsp case KEY_ENTER:
7671 6458efa5 2020-11-24 stsp case '\r':
7672 07b0611c 2022-06-23 thomas view->count = 0;
7673 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7674 6458efa5 2020-11-24 stsp break;
7675 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7676 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7677 a5d43cac 2022-07-01 thomas
7678 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7679 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7680 a5d43cac 2022-07-01 thomas if (err)
7681 a5d43cac 2022-07-01 thomas break;
7682 a5d43cac 2022-07-01 thomas
7683 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7684 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7685 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7686 a5d43cac 2022-07-01 thomas if (err)
7687 a5d43cac 2022-07-01 thomas break;
7688 a5d43cac 2022-07-01 thomas }
7689 a5d43cac 2022-07-01 thomas
7690 e78dc838 2020-12-04 stsp view->focussed = 0;
7691 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7692 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7693 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7694 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7695 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7696 6458efa5 2020-11-24 stsp err = view_close_child(view);
7697 6458efa5 2020-11-24 stsp if (err)
7698 6458efa5 2020-11-24 stsp return err;
7699 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7700 40236d76 2022-06-23 thomas if (err)
7701 40236d76 2022-06-23 thomas return err;
7702 e78dc838 2020-12-04 stsp view->focus_child = 1;
7703 6458efa5 2020-11-24 stsp } else
7704 6458efa5 2020-11-24 stsp *new_view = log_view;
7705 6458efa5 2020-11-24 stsp break;
7706 c42c9805 2020-11-24 stsp case 't':
7707 07b0611c 2022-06-23 thomas view->count = 0;
7708 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7709 c42c9805 2020-11-24 stsp break;
7710 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7711 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7712 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7713 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7714 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7715 c42c9805 2020-11-24 stsp break;
7716 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7717 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7718 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7719 444d5325 2022-07-03 thomas if (err)
7720 444d5325 2022-07-03 thomas break;
7721 444d5325 2022-07-03 thomas }
7722 e78dc838 2020-12-04 stsp view->focussed = 0;
7723 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7724 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7725 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7726 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7727 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7728 c42c9805 2020-11-24 stsp err = view_close_child(view);
7729 c42c9805 2020-11-24 stsp if (err)
7730 c42c9805 2020-11-24 stsp return err;
7731 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7732 40236d76 2022-06-23 thomas if (err)
7733 40236d76 2022-06-23 thomas return err;
7734 e78dc838 2020-12-04 stsp view->focus_child = 1;
7735 c42c9805 2020-11-24 stsp } else
7736 c42c9805 2020-11-24 stsp *new_view = tree_view;
7737 c42c9805 2020-11-24 stsp break;
7738 e4526bf5 2021-09-03 naddy case 'g':
7739 e4526bf5 2021-09-03 naddy case KEY_HOME:
7740 e4526bf5 2021-09-03 naddy s->selected = 0;
7741 07b0611c 2022-06-23 thomas view->count = 0;
7742 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7743 e4526bf5 2021-09-03 naddy break;
7744 e4526bf5 2021-09-03 naddy case 'G':
7745 a5d43cac 2022-07-01 thomas case KEY_END: {
7746 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7747 a5d43cac 2022-07-01 thomas
7748 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7749 a5d43cac 2022-07-01 thomas --eos; /* border */
7750 e4526bf5 2021-09-03 naddy s->selected = 0;
7751 07b0611c 2022-06-23 thomas view->count = 0;
7752 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7753 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7754 e4526bf5 2021-09-03 naddy if (re == NULL)
7755 e4526bf5 2021-09-03 naddy break;
7756 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7757 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7758 e4526bf5 2021-09-03 naddy }
7759 e4526bf5 2021-09-03 naddy if (n > 0)
7760 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7761 e4526bf5 2021-09-03 naddy break;
7762 a5d43cac 2022-07-01 thomas }
7763 6458efa5 2020-11-24 stsp case 'k':
7764 6458efa5 2020-11-24 stsp case KEY_UP:
7765 f7140bf5 2021-10-17 thomas case CTRL('p'):
7766 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7767 6458efa5 2020-11-24 stsp s->selected--;
7768 6458efa5 2020-11-24 stsp break;
7769 34ba6917 2020-11-30 stsp }
7770 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7771 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7772 07b0611c 2022-06-23 thomas view->count = 0;
7773 6458efa5 2020-11-24 stsp break;
7774 70f17a53 2022-06-13 thomas case CTRL('u'):
7775 23427b14 2022-06-23 thomas case 'u':
7776 70f17a53 2022-06-13 thomas nscroll /= 2;
7777 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7778 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7779 6458efa5 2020-11-24 stsp case CTRL('b'):
7780 1c5e5faa 2022-06-23 thomas case 'b':
7781 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7782 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7783 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7784 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7785 07b0611c 2022-06-23 thomas view->count = 0;
7786 6458efa5 2020-11-24 stsp break;
7787 6458efa5 2020-11-24 stsp case 'j':
7788 6458efa5 2020-11-24 stsp case KEY_DOWN:
7789 f7140bf5 2021-10-17 thomas case CTRL('n'):
7790 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7791 6458efa5 2020-11-24 stsp s->selected++;
7792 6458efa5 2020-11-24 stsp break;
7793 6458efa5 2020-11-24 stsp }
7794 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7795 6458efa5 2020-11-24 stsp /* can't scroll any further */
7796 07b0611c 2022-06-23 thomas view->count = 0;
7797 6458efa5 2020-11-24 stsp break;
7798 07b0611c 2022-06-23 thomas }
7799 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7800 6458efa5 2020-11-24 stsp break;
7801 70f17a53 2022-06-13 thomas case CTRL('d'):
7802 23427b14 2022-06-23 thomas case 'd':
7803 70f17a53 2022-06-13 thomas nscroll /= 2;
7804 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7805 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7806 6458efa5 2020-11-24 stsp case CTRL('f'):
7807 1c5e5faa 2022-06-23 thomas case 'f':
7808 4c2d69cb 2022-06-23 thomas case ' ':
7809 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7810 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7811 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7812 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7813 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7814 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7815 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7816 07b0611c 2022-06-23 thomas view->count = 0;
7817 6458efa5 2020-11-24 stsp break;
7818 6458efa5 2020-11-24 stsp }
7819 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7820 6458efa5 2020-11-24 stsp break;
7821 6458efa5 2020-11-24 stsp case CTRL('l'):
7822 07b0611c 2022-06-23 thomas view->count = 0;
7823 8924d611 2020-12-26 stsp tog_free_refs();
7824 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7825 8924d611 2020-12-26 stsp if (err)
7826 8924d611 2020-12-26 stsp break;
7827 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7828 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7829 6458efa5 2020-11-24 stsp break;
7830 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7831 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7832 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7833 6458efa5 2020-11-24 stsp break;
7834 6458efa5 2020-11-24 stsp default:
7835 07b0611c 2022-06-23 thomas view->count = 0;
7836 6458efa5 2020-11-24 stsp break;
7837 6458efa5 2020-11-24 stsp }
7838 6458efa5 2020-11-24 stsp
7839 6458efa5 2020-11-24 stsp return err;
7840 6458efa5 2020-11-24 stsp }
7841 6458efa5 2020-11-24 stsp
7842 6458efa5 2020-11-24 stsp __dead static void
7843 6458efa5 2020-11-24 stsp usage_ref(void)
7844 6458efa5 2020-11-24 stsp {
7845 6458efa5 2020-11-24 stsp endwin();
7846 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7847 6458efa5 2020-11-24 stsp getprogname());
7848 6458efa5 2020-11-24 stsp exit(1);
7849 6458efa5 2020-11-24 stsp }
7850 6458efa5 2020-11-24 stsp
7851 6458efa5 2020-11-24 stsp static const struct got_error *
7852 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7853 6458efa5 2020-11-24 stsp {
7854 6458efa5 2020-11-24 stsp const struct got_error *error;
7855 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7856 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7857 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7858 6458efa5 2020-11-24 stsp int ch;
7859 6458efa5 2020-11-24 stsp struct tog_view *view;
7860 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7861 6458efa5 2020-11-24 stsp
7862 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7863 6458efa5 2020-11-24 stsp switch (ch) {
7864 6458efa5 2020-11-24 stsp case 'r':
7865 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7866 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7867 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7868 6458efa5 2020-11-24 stsp optarg);
7869 6458efa5 2020-11-24 stsp break;
7870 6458efa5 2020-11-24 stsp default:
7871 e99e2d15 2020-11-24 naddy usage_ref();
7872 6458efa5 2020-11-24 stsp /* NOTREACHED */
7873 6458efa5 2020-11-24 stsp }
7874 6458efa5 2020-11-24 stsp }
7875 6458efa5 2020-11-24 stsp
7876 6458efa5 2020-11-24 stsp argc -= optind;
7877 6458efa5 2020-11-24 stsp argv += optind;
7878 6458efa5 2020-11-24 stsp
7879 6458efa5 2020-11-24 stsp if (argc > 1)
7880 e99e2d15 2020-11-24 naddy usage_ref();
7881 7cd52833 2022-06-23 thomas
7882 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7883 7cd52833 2022-06-23 thomas if (error != NULL)
7884 7cd52833 2022-06-23 thomas goto done;
7885 6458efa5 2020-11-24 stsp
7886 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7887 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7888 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7889 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7890 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7891 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7892 c156c7a4 2020-12-18 stsp goto done;
7893 6458efa5 2020-11-24 stsp if (worktree)
7894 6458efa5 2020-11-24 stsp repo_path =
7895 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7896 6458efa5 2020-11-24 stsp else
7897 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7898 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7899 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7900 c156c7a4 2020-12-18 stsp goto done;
7901 c156c7a4 2020-12-18 stsp }
7902 6458efa5 2020-11-24 stsp }
7903 6458efa5 2020-11-24 stsp
7904 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7905 6458efa5 2020-11-24 stsp if (error != NULL)
7906 6458efa5 2020-11-24 stsp goto done;
7907 6458efa5 2020-11-24 stsp
7908 6458efa5 2020-11-24 stsp init_curses();
7909 6458efa5 2020-11-24 stsp
7910 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7911 51a10b52 2020-12-26 stsp if (error)
7912 51a10b52 2020-12-26 stsp goto done;
7913 51a10b52 2020-12-26 stsp
7914 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7915 6458efa5 2020-11-24 stsp if (error)
7916 6458efa5 2020-11-24 stsp goto done;
7917 6458efa5 2020-11-24 stsp
7918 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7919 6458efa5 2020-11-24 stsp if (view == NULL) {
7920 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7921 6458efa5 2020-11-24 stsp goto done;
7922 6458efa5 2020-11-24 stsp }
7923 6458efa5 2020-11-24 stsp
7924 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7925 6458efa5 2020-11-24 stsp if (error)
7926 6458efa5 2020-11-24 stsp goto done;
7927 6458efa5 2020-11-24 stsp
7928 6458efa5 2020-11-24 stsp if (worktree) {
7929 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7930 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7931 6458efa5 2020-11-24 stsp worktree = NULL;
7932 6458efa5 2020-11-24 stsp }
7933 6458efa5 2020-11-24 stsp error = view_loop(view);
7934 6458efa5 2020-11-24 stsp done:
7935 6458efa5 2020-11-24 stsp free(repo_path);
7936 6458efa5 2020-11-24 stsp free(cwd);
7937 1d0f4054 2021-06-17 stsp if (repo) {
7938 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7939 1d0f4054 2021-06-17 stsp if (close_err)
7940 1d0f4054 2021-06-17 stsp error = close_err;
7941 1d0f4054 2021-06-17 stsp }
7942 7cd52833 2022-06-23 thomas if (pack_fds) {
7943 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7944 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7945 7cd52833 2022-06-23 thomas if (error == NULL)
7946 7cd52833 2022-06-23 thomas error = pack_err;
7947 7cd52833 2022-06-23 thomas }
7948 51a10b52 2020-12-26 stsp tog_free_refs();
7949 6458efa5 2020-11-24 stsp return error;
7950 6458efa5 2020-11-24 stsp }
7951 6458efa5 2020-11-24 stsp
7952 a5d43cac 2022-07-01 thomas /*
7953 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7954 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7955 a5d43cac 2022-07-01 thomas */
7956 6458efa5 2020-11-24 stsp static void
7957 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7958 a5d43cac 2022-07-01 thomas {
7959 a5d43cac 2022-07-01 thomas switch (view->type) {
7960 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7961 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7962 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7963 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7964 a5d43cac 2022-07-01 thomas 1);
7965 a5d43cac 2022-07-01 thomas break;
7966 a5d43cac 2022-07-01 thomas }
7967 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7968 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7969 a5d43cac 2022-07-01 thomas else
7970 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7971 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7972 a5d43cac 2022-07-01 thomas break;
7973 a5d43cac 2022-07-01 thomas }
7974 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7975 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7976 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7977 a5d43cac 2022-07-01 thomas break;
7978 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7979 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7980 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7981 a5d43cac 2022-07-01 thomas break;
7982 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7983 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7984 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7985 a5d43cac 2022-07-01 thomas break;
7986 a5d43cac 2022-07-01 thomas default:
7987 a5d43cac 2022-07-01 thomas break;
7988 a5d43cac 2022-07-01 thomas }
7989 a5d43cac 2022-07-01 thomas
7990 a5d43cac 2022-07-01 thomas view->offset = 0;
7991 a5d43cac 2022-07-01 thomas }
7992 a5d43cac 2022-07-01 thomas
7993 a5d43cac 2022-07-01 thomas /*
7994 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
7995 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
7996 a5d43cac 2022-07-01 thomas */
7997 a5d43cac 2022-07-01 thomas static const struct got_error *
7998 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
7999 a5d43cac 2022-07-01 thomas {
8000 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8001 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8002 a5d43cac 2022-07-01 thomas int *selected = NULL;
8003 a5d43cac 2022-07-01 thomas int header, offset;
8004 a5d43cac 2022-07-01 thomas
8005 a5d43cac 2022-07-01 thomas switch (view->type) {
8006 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8007 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8008 a5d43cac 2022-07-01 thomas header = 3;
8009 a5d43cac 2022-07-01 thomas scrolld = NULL;
8010 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8011 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8012 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8013 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8014 a5d43cac 2022-07-01 thomas view->offset = offset;
8015 a5d43cac 2022-07-01 thomas }
8016 a5d43cac 2022-07-01 thomas break;
8017 a5d43cac 2022-07-01 thomas }
8018 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8019 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8020 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8021 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8022 a5d43cac 2022-07-01 thomas selected = &s->selected;
8023 a5d43cac 2022-07-01 thomas break;
8024 a5d43cac 2022-07-01 thomas }
8025 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8026 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8027 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8028 a5d43cac 2022-07-01 thomas header = 3;
8029 a5d43cac 2022-07-01 thomas selected = &s->selected;
8030 a5d43cac 2022-07-01 thomas break;
8031 a5d43cac 2022-07-01 thomas }
8032 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8033 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8034 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8035 a5d43cac 2022-07-01 thomas header = 5;
8036 a5d43cac 2022-07-01 thomas selected = &s->selected;
8037 a5d43cac 2022-07-01 thomas break;
8038 a5d43cac 2022-07-01 thomas }
8039 a5d43cac 2022-07-01 thomas default:
8040 a5d43cac 2022-07-01 thomas selected = NULL;
8041 a5d43cac 2022-07-01 thomas scrolld = NULL;
8042 a5d43cac 2022-07-01 thomas header = 0;
8043 a5d43cac 2022-07-01 thomas break;
8044 a5d43cac 2022-07-01 thomas }
8045 a5d43cac 2022-07-01 thomas
8046 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8047 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8048 a5d43cac 2022-07-01 thomas view->offset = offset;
8049 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8050 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8051 a5d43cac 2022-07-01 thomas *selected -= offset;
8052 a5d43cac 2022-07-01 thomas }
8053 a5d43cac 2022-07-01 thomas }
8054 a5d43cac 2022-07-01 thomas
8055 a5d43cac 2022-07-01 thomas return err;
8056 a5d43cac 2022-07-01 thomas }
8057 a5d43cac 2022-07-01 thomas
8058 a5d43cac 2022-07-01 thomas static void
8059 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8060 ce5b7c56 2019-07-09 stsp {
8061 6059809a 2020-12-17 stsp size_t i;
8062 9f7d7167 2018-04-29 stsp
8063 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8064 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8065 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8066 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8067 ce5b7c56 2019-07-09 stsp }
8068 6879ba42 2020-10-01 naddy fputc('\n', fp);
8069 ce5b7c56 2019-07-09 stsp }
8070 ce5b7c56 2019-07-09 stsp
8071 4ed7e80c 2018-05-20 stsp __dead static void
8072 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8073 9f7d7167 2018-04-29 stsp {
8074 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8075 6879ba42 2020-10-01 naddy
8076 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8077 6879ba42 2020-10-01 naddy getprogname());
8078 6879ba42 2020-10-01 naddy if (hflag) {
8079 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8080 6879ba42 2020-10-01 naddy list_commands(fp);
8081 ee85c5e8 2020-02-29 stsp }
8082 6879ba42 2020-10-01 naddy exit(status);
8083 9f7d7167 2018-04-29 stsp }
8084 9f7d7167 2018-04-29 stsp
8085 c2301be8 2018-04-30 stsp static char **
8086 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8087 c2301be8 2018-04-30 stsp {
8088 ee85c5e8 2020-02-29 stsp va_list ap;
8089 c2301be8 2018-04-30 stsp char **argv;
8090 ee85c5e8 2020-02-29 stsp int i;
8091 c2301be8 2018-04-30 stsp
8092 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8093 ee85c5e8 2020-02-29 stsp
8094 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8095 c2301be8 2018-04-30 stsp if (argv == NULL)
8096 c2301be8 2018-04-30 stsp err(1, "calloc");
8097 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8098 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8099 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8100 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8101 c2301be8 2018-04-30 stsp }
8102 c2301be8 2018-04-30 stsp
8103 ee85c5e8 2020-02-29 stsp va_end(ap);
8104 c2301be8 2018-04-30 stsp return argv;
8105 ee85c5e8 2020-02-29 stsp }
8106 ee85c5e8 2020-02-29 stsp
8107 ee85c5e8 2020-02-29 stsp /*
8108 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8109 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8110 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8111 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8112 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8113 ee85c5e8 2020-02-29 stsp */
8114 ee85c5e8 2020-02-29 stsp static const struct got_error *
8115 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8116 ee85c5e8 2020-02-29 stsp {
8117 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8118 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8119 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8120 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8121 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8122 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8123 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8124 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8125 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8126 84de9106 2020-12-26 stsp
8127 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8128 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8129 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8130 ee85c5e8 2020-02-29 stsp
8131 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8132 7cd52833 2022-06-23 thomas if (error != NULL)
8133 7cd52833 2022-06-23 thomas goto done;
8134 7cd52833 2022-06-23 thomas
8135 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8136 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8137 ee85c5e8 2020-02-29 stsp goto done;
8138 ee85c5e8 2020-02-29 stsp
8139 ee85c5e8 2020-02-29 stsp if (worktree)
8140 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8141 ee85c5e8 2020-02-29 stsp else
8142 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8143 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8144 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8145 ee85c5e8 2020-02-29 stsp goto done;
8146 ee85c5e8 2020-02-29 stsp }
8147 ee85c5e8 2020-02-29 stsp
8148 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8149 ee85c5e8 2020-02-29 stsp if (error != NULL)
8150 ee85c5e8 2020-02-29 stsp goto done;
8151 ee85c5e8 2020-02-29 stsp
8152 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8153 ee85c5e8 2020-02-29 stsp repo, worktree);
8154 ee85c5e8 2020-02-29 stsp if (error)
8155 ee85c5e8 2020-02-29 stsp goto done;
8156 ee85c5e8 2020-02-29 stsp
8157 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8158 84de9106 2020-12-26 stsp if (error)
8159 84de9106 2020-12-26 stsp goto done;
8160 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8161 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8162 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8163 ee85c5e8 2020-02-29 stsp if (error)
8164 ee85c5e8 2020-02-29 stsp goto done;
8165 ee85c5e8 2020-02-29 stsp
8166 ee85c5e8 2020-02-29 stsp if (worktree) {
8167 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8168 ee85c5e8 2020-02-29 stsp worktree = NULL;
8169 ee85c5e8 2020-02-29 stsp }
8170 ee85c5e8 2020-02-29 stsp
8171 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8172 945f9229 2022-04-16 thomas if (error)
8173 945f9229 2022-04-16 thomas goto done;
8174 945f9229 2022-04-16 thomas
8175 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8176 ee85c5e8 2020-02-29 stsp if (error) {
8177 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8178 ee85c5e8 2020-02-29 stsp goto done;
8179 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8180 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8181 6879ba42 2020-10-01 naddy usage(1, 1);
8182 ee85c5e8 2020-02-29 stsp /* not reached */
8183 ee85c5e8 2020-02-29 stsp }
8184 ee85c5e8 2020-02-29 stsp
8185 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8186 1d0f4054 2021-06-17 stsp if (error == NULL)
8187 1d0f4054 2021-06-17 stsp error = close_err;
8188 ee85c5e8 2020-02-29 stsp repo = NULL;
8189 ee85c5e8 2020-02-29 stsp
8190 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8191 ee85c5e8 2020-02-29 stsp if (error)
8192 ee85c5e8 2020-02-29 stsp goto done;
8193 ee85c5e8 2020-02-29 stsp
8194 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8195 ee85c5e8 2020-02-29 stsp argc = 4;
8196 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8197 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8198 ee85c5e8 2020-02-29 stsp done:
8199 1d0f4054 2021-06-17 stsp if (repo) {
8200 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8201 1d0f4054 2021-06-17 stsp if (error == NULL)
8202 1d0f4054 2021-06-17 stsp error = close_err;
8203 1d0f4054 2021-06-17 stsp }
8204 945f9229 2022-04-16 thomas if (commit)
8205 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8206 ee85c5e8 2020-02-29 stsp if (worktree)
8207 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8208 7cd52833 2022-06-23 thomas if (pack_fds) {
8209 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8210 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8211 7cd52833 2022-06-23 thomas if (error == NULL)
8212 7cd52833 2022-06-23 thomas error = pack_err;
8213 7cd52833 2022-06-23 thomas }
8214 ee85c5e8 2020-02-29 stsp free(id);
8215 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8216 ee85c5e8 2020-02-29 stsp free(commit_id);
8217 ee85c5e8 2020-02-29 stsp free(cwd);
8218 ee85c5e8 2020-02-29 stsp free(repo_path);
8219 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8220 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8221 ee85c5e8 2020-02-29 stsp int i;
8222 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8223 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8224 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8225 ee85c5e8 2020-02-29 stsp }
8226 87670572 2020-12-26 naddy tog_free_refs();
8227 ee85c5e8 2020-02-29 stsp return error;
8228 c2301be8 2018-04-30 stsp }
8229 c2301be8 2018-04-30 stsp
8230 9f7d7167 2018-04-29 stsp int
8231 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8232 9f7d7167 2018-04-29 stsp {
8233 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8234 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8235 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8236 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8237 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8238 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8239 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8240 83cd27f8 2020-01-13 stsp };
8241 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8242 9f7d7167 2018-04-29 stsp
8243 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8244 9f7d7167 2018-04-29 stsp
8245 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8246 9f7d7167 2018-04-29 stsp switch (ch) {
8247 9f7d7167 2018-04-29 stsp case 'h':
8248 9f7d7167 2018-04-29 stsp hflag = 1;
8249 9f7d7167 2018-04-29 stsp break;
8250 53ccebc2 2019-07-30 stsp case 'V':
8251 53ccebc2 2019-07-30 stsp Vflag = 1;
8252 53ccebc2 2019-07-30 stsp break;
8253 9f7d7167 2018-04-29 stsp default:
8254 6879ba42 2020-10-01 naddy usage(hflag, 1);
8255 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8256 9f7d7167 2018-04-29 stsp }
8257 9f7d7167 2018-04-29 stsp }
8258 9f7d7167 2018-04-29 stsp
8259 9f7d7167 2018-04-29 stsp argc -= optind;
8260 9f7d7167 2018-04-29 stsp argv += optind;
8261 9814e6a3 2020-09-27 naddy optind = 1;
8262 c2301be8 2018-04-30 stsp optreset = 1;
8263 9f7d7167 2018-04-29 stsp
8264 53ccebc2 2019-07-30 stsp if (Vflag) {
8265 53ccebc2 2019-07-30 stsp got_version_print_str();
8266 6879ba42 2020-10-01 naddy return 0;
8267 53ccebc2 2019-07-30 stsp }
8268 4010e238 2020-12-04 stsp
8269 4010e238 2020-12-04 stsp #ifndef PROFILE
8270 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8271 4010e238 2020-12-04 stsp NULL) == -1)
8272 4010e238 2020-12-04 stsp err(1, "pledge");
8273 4010e238 2020-12-04 stsp #endif
8274 53ccebc2 2019-07-30 stsp
8275 c2301be8 2018-04-30 stsp if (argc == 0) {
8276 f29d3e89 2018-06-23 stsp if (hflag)
8277 6879ba42 2020-10-01 naddy usage(hflag, 0);
8278 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8279 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8280 c2301be8 2018-04-30 stsp argc = 1;
8281 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8282 c2301be8 2018-04-30 stsp } else {
8283 6059809a 2020-12-17 stsp size_t i;
8284 9f7d7167 2018-04-29 stsp
8285 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8286 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8287 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8288 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8289 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8290 9f7d7167 2018-04-29 stsp break;
8291 9f7d7167 2018-04-29 stsp }
8292 9f7d7167 2018-04-29 stsp }
8293 ee85c5e8 2020-02-29 stsp }
8294 3642c4c6 2019-07-09 stsp
8295 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8296 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8297 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8298 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8299 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8300 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8301 adf4c9e0 2022-07-03 thomas }
8302 adf4c9e0 2022-07-03 thomas
8303 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8304 ee85c5e8 2020-02-29 stsp if (argc != 1)
8305 6879ba42 2020-10-01 naddy usage(0, 1);
8306 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8307 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8308 ee85c5e8 2020-02-29 stsp } else {
8309 ee85c5e8 2020-02-29 stsp if (hflag)
8310 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8311 ee85c5e8 2020-02-29 stsp else
8312 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8313 9f7d7167 2018-04-29 stsp }
8314 9f7d7167 2018-04-29 stsp
8315 9f7d7167 2018-04-29 stsp endwin();
8316 b46c1e04 2020-09-20 naddy putchar('\n');
8317 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8318 a2f4a359 2020-02-28 stsp int i;
8319 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8320 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8321 a2f4a359 2020-02-28 stsp free(cmd_argv);
8322 a2f4a359 2020-02-28 stsp }
8323 a2f4a359 2020-02-28 stsp
8324 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8325 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8326 9f7d7167 2018-04-29 stsp return 0;
8327 9f7d7167 2018-04-29 stsp }