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 fe731b51 2022-07-14 thomas if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
995 ae98518f 2022-07-12 thomas v->child->nscrolled = y - v->child->begin_y;
996 fe731b51 2022-07-14 thomas else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
997 fe731b51 2022-07-14 thomas v->nscrolled = v->child->begin_y - y;
998 53d2bdd3 2022-07-10 thomas } else {
999 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1000 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1001 53d2bdd3 2022-07-10 thomas if (view->parent)
1002 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1003 53d2bdd3 2022-07-10 thomas else
1004 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1005 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1006 53d2bdd3 2022-07-10 thomas view->count = 0;
1007 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1008 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1009 53d2bdd3 2022-07-10 thomas view->count = 0;
1010 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1011 53d2bdd3 2022-07-10 thomas }
1012 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1013 53d2bdd3 2022-07-10 thomas }
1014 53d2bdd3 2022-07-10 thomas
1015 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1016 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1017 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1018 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1019 53d2bdd3 2022-07-10 thomas
1020 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1021 53d2bdd3 2022-07-10 thomas if (err)
1022 53d2bdd3 2022-07-10 thomas return err;
1023 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1024 53d2bdd3 2022-07-10 thomas if (err)
1025 53d2bdd3 2022-07-10 thomas return err;
1026 53d2bdd3 2022-07-10 thomas
1027 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1028 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1029 53d2bdd3 2022-07-10 thomas if (err)
1030 53d2bdd3 2022-07-10 thomas return err;
1031 53d2bdd3 2022-07-10 thomas }
1032 53d2bdd3 2022-07-10 thomas
1033 fe731b51 2022-07-14 thomas if (v->nscrolled)
1034 53d2bdd3 2022-07-10 thomas err = request_log_commits(v);
1035 fe731b51 2022-07-14 thomas else if (v->child->nscrolled)
1036 53d2bdd3 2022-07-10 thomas err = request_log_commits(v->child);
1037 53d2bdd3 2022-07-10 thomas
1038 53d2bdd3 2022-07-10 thomas v->resize = v->child->resize = 0;
1039 53d2bdd3 2022-07-10 thomas
1040 53d2bdd3 2022-07-10 thomas return err;
1041 53d2bdd3 2022-07-10 thomas }
1042 53d2bdd3 2022-07-10 thomas
1043 53d2bdd3 2022-07-10 thomas static void
1044 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1045 53d2bdd3 2022-07-10 thomas {
1046 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1047 53d2bdd3 2022-07-10 thomas
1048 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1049 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1050 669b5ffa 2018-10-07 stsp }
1051 669b5ffa 2018-10-07 stsp
1052 669b5ffa 2018-10-07 stsp static const struct got_error *
1053 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1054 669b5ffa 2018-10-07 stsp {
1055 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1056 669b5ffa 2018-10-07 stsp
1057 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1058 669b5ffa 2018-10-07 stsp return NULL;
1059 669b5ffa 2018-10-07 stsp
1060 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1061 669b5ffa 2018-10-07 stsp view->child = NULL;
1062 669b5ffa 2018-10-07 stsp return err;
1063 669b5ffa 2018-10-07 stsp }
1064 669b5ffa 2018-10-07 stsp
1065 40236d76 2022-06-23 thomas static const struct got_error *
1066 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1067 669b5ffa 2018-10-07 stsp {
1068 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1069 53d2bdd3 2022-07-10 thomas
1070 669b5ffa 2018-10-07 stsp view->child = child;
1071 669b5ffa 2018-10-07 stsp child->parent = view;
1072 40236d76 2022-06-23 thomas
1073 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1074 53d2bdd3 2022-07-10 thomas if (err)
1075 53d2bdd3 2022-07-10 thomas return err;
1076 53d2bdd3 2022-07-10 thomas
1077 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1078 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1079 53d2bdd3 2022-07-10 thomas
1080 53d2bdd3 2022-07-10 thomas return err;
1081 bfddd0d9 2018-09-29 stsp }
1082 bfddd0d9 2018-09-29 stsp
1083 34bc9ec9 2019-02-22 stsp static void
1084 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1085 25791caa 2018-10-24 stsp {
1086 25791caa 2018-10-24 stsp int cols, lines;
1087 25791caa 2018-10-24 stsp struct winsize size;
1088 25791caa 2018-10-24 stsp
1089 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1090 25791caa 2018-10-24 stsp cols = 80; /* Default */
1091 25791caa 2018-10-24 stsp lines = 24;
1092 25791caa 2018-10-24 stsp } else {
1093 25791caa 2018-10-24 stsp cols = size.ws_col;
1094 25791caa 2018-10-24 stsp lines = size.ws_row;
1095 25791caa 2018-10-24 stsp }
1096 25791caa 2018-10-24 stsp resize_term(lines, cols);
1097 2b49a8ae 2019-06-22 stsp }
1098 2b49a8ae 2019-06-22 stsp
1099 2b49a8ae 2019-06-22 stsp static const struct got_error *
1100 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1101 2b49a8ae 2019-06-22 stsp {
1102 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1103 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1104 2b49a8ae 2019-06-22 stsp char pattern[1024];
1105 2b49a8ae 2019-06-22 stsp int ret;
1106 c0c4acc8 2021-01-24 stsp
1107 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1108 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1109 c0c4acc8 2021-01-24 stsp view->searching = 0;
1110 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1111 c0c4acc8 2021-01-24 stsp }
1112 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1113 2b49a8ae 2019-06-22 stsp
1114 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1115 2b49a8ae 2019-06-22 stsp return NULL;
1116 2b49a8ae 2019-06-22 stsp
1117 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1118 a5d43cac 2022-07-01 thomas v = view->child;
1119 2b49a8ae 2019-06-22 stsp
1120 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1121 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1122 a5d43cac 2022-07-01 thomas
1123 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1124 2b49a8ae 2019-06-22 stsp nocbreak();
1125 2b49a8ae 2019-06-22 stsp echo();
1126 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1127 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1128 2b49a8ae 2019-06-22 stsp cbreak();
1129 2b49a8ae 2019-06-22 stsp noecho();
1130 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1131 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1132 2b49a8ae 2019-06-22 stsp return NULL;
1133 2b49a8ae 2019-06-22 stsp
1134 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1135 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1136 7c32bd05 2019-06-22 stsp if (err) {
1137 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1138 7c32bd05 2019-06-22 stsp return err;
1139 7c32bd05 2019-06-22 stsp }
1140 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1141 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1142 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1143 2b49a8ae 2019-06-22 stsp view->search_next(view);
1144 2b49a8ae 2019-06-22 stsp }
1145 2b49a8ae 2019-06-22 stsp
1146 2b49a8ae 2019-06-22 stsp return NULL;
1147 64486692 2022-07-07 thomas }
1148 64486692 2022-07-07 thomas
1149 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1150 64486692 2022-07-07 thomas static const struct got_error *
1151 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1152 64486692 2022-07-07 thomas {
1153 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1154 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1155 64486692 2022-07-07 thomas
1156 64486692 2022-07-07 thomas if (view->parent)
1157 64486692 2022-07-07 thomas v = view->parent;
1158 64486692 2022-07-07 thomas else
1159 64486692 2022-07-07 thomas v = view;
1160 64486692 2022-07-07 thomas
1161 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1162 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1163 ddbc4d37 2022-07-12 thomas else
1164 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1165 64486692 2022-07-07 thomas
1166 ddbc4d37 2022-07-12 thomas if (!v->child)
1167 ddbc4d37 2022-07-12 thomas return NULL;
1168 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1169 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1170 ddbc4d37 2022-07-12 thomas
1171 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1172 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1173 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1174 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1175 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1176 64486692 2022-07-07 thomas
1177 ddbc4d37 2022-07-12 thomas
1178 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1179 64486692 2022-07-07 thomas v->ncols = COLS;
1180 64486692 2022-07-07 thomas v->child->ncols = COLS;
1181 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1182 64486692 2022-07-07 thomas
1183 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1184 64486692 2022-07-07 thomas if (err)
1185 64486692 2022-07-07 thomas return err;
1186 64486692 2022-07-07 thomas }
1187 64486692 2022-07-07 thomas v->child->mode = v->mode;
1188 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1189 64486692 2022-07-07 thomas v->focus_child = 1;
1190 64486692 2022-07-07 thomas
1191 64486692 2022-07-07 thomas err = view_fullscreen(v);
1192 64486692 2022-07-07 thomas if (err)
1193 64486692 2022-07-07 thomas return err;
1194 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1195 64486692 2022-07-07 thomas if (err)
1196 64486692 2022-07-07 thomas return err;
1197 64486692 2022-07-07 thomas
1198 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1199 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1200 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1201 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1202 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1203 ddbc4d37 2022-07-12 thomas } else {
1204 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1205 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1206 64486692 2022-07-07 thomas }
1207 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1208 ddbc4d37 2022-07-12 thomas err = request_log_commits(v);
1209 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1210 ddbc4d37 2022-07-12 thomas err = request_log_commits(v->child);
1211 64486692 2022-07-07 thomas
1212 64486692 2022-07-07 thomas return err;
1213 0cf4efb1 2018-09-29 stsp }
1214 6d0fee91 2018-08-01 stsp
1215 07b0611c 2022-06-23 thomas /*
1216 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1217 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1218 07b0611c 2022-06-23 thomas */
1219 07b0611c 2022-06-23 thomas static int
1220 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1221 07b0611c 2022-06-23 thomas {
1222 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1223 a5d43cac 2022-07-01 thomas int x, n = 0;
1224 07b0611c 2022-06-23 thomas
1225 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1226 a5d43cac 2022-07-01 thomas v = view->child;
1227 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1228 a5d43cac 2022-07-01 thomas v = view->parent;
1229 a5d43cac 2022-07-01 thomas
1230 07b0611c 2022-06-23 thomas view->count = 0;
1231 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1232 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1233 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1234 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1235 07b0611c 2022-06-23 thomas
1236 07b0611c 2022-06-23 thomas do {
1237 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1238 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1239 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1240 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1241 a5d43cac 2022-07-01 thomas }
1242 a5d43cac 2022-07-01 thomas
1243 07b0611c 2022-06-23 thomas /*
1244 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1245 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1246 07b0611c 2022-06-23 thomas */
1247 07b0611c 2022-06-23 thomas if (n >= 9999999)
1248 07b0611c 2022-06-23 thomas n = 9999999;
1249 07b0611c 2022-06-23 thomas else
1250 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1251 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1252 07b0611c 2022-06-23 thomas
1253 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1254 07b0611c 2022-06-23 thomas view->count = n;
1255 07b0611c 2022-06-23 thomas
1256 07b0611c 2022-06-23 thomas return c;
1257 07b0611c 2022-06-23 thomas }
1258 07b0611c 2022-06-23 thomas
1259 0cf4efb1 2018-09-29 stsp static const struct got_error *
1260 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1261 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1262 e5a0f69f 2018-08-18 stsp {
1263 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1264 669b5ffa 2018-10-07 stsp struct tog_view *v;
1265 1a76625f 2018-10-22 stsp int ch, errcode;
1266 e5a0f69f 2018-08-18 stsp
1267 e5a0f69f 2018-08-18 stsp *new = NULL;
1268 8f4ed634 2020-03-26 stsp
1269 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1270 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1271 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1272 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1273 07b0611c 2022-06-23 thomas view->count = 0;
1274 07b0611c 2022-06-23 thomas }
1275 e5a0f69f 2018-08-18 stsp
1276 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1277 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1278 82954512 2020-02-03 stsp if (errcode)
1279 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1280 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1281 3da8ef85 2021-09-21 stsp sched_yield();
1282 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1283 82954512 2020-02-03 stsp if (errcode)
1284 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1285 82954512 2020-02-03 stsp "pthread_mutex_lock");
1286 60493ae3 2019-06-20 stsp view->search_next(view);
1287 60493ae3 2019-06-20 stsp return NULL;
1288 60493ae3 2019-06-20 stsp }
1289 60493ae3 2019-06-20 stsp
1290 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1291 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1292 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1293 1a76625f 2018-10-22 stsp if (errcode)
1294 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1295 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1296 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1297 634cb454 2022-07-03 thomas cbreak();
1298 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1299 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1300 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1301 634cb454 2022-07-03 thomas view->count = 0;
1302 634cb454 2022-07-03 thomas else
1303 634cb454 2022-07-03 thomas ch = view->ch;
1304 634cb454 2022-07-03 thomas } else {
1305 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1306 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1307 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1308 07b0611c 2022-06-23 thomas }
1309 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1310 1a76625f 2018-10-22 stsp if (errcode)
1311 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1312 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1313 25791caa 2018-10-24 stsp
1314 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1315 25791caa 2018-10-24 stsp tog_resizeterm();
1316 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1317 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1318 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1319 25791caa 2018-10-24 stsp err = view_resize(v);
1320 25791caa 2018-10-24 stsp if (err)
1321 25791caa 2018-10-24 stsp return err;
1322 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1323 25791caa 2018-10-24 stsp if (err)
1324 25791caa 2018-10-24 stsp return err;
1325 cdfcfb03 2020-12-06 stsp if (v->child) {
1326 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1327 cdfcfb03 2020-12-06 stsp if (err)
1328 cdfcfb03 2020-12-06 stsp return err;
1329 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1330 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1331 cdfcfb03 2020-12-06 stsp if (err)
1332 cdfcfb03 2020-12-06 stsp return err;
1333 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1334 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1335 53d2bdd3 2022-07-10 thomas if (err)
1336 53d2bdd3 2022-07-10 thomas return err;
1337 53d2bdd3 2022-07-10 thomas }
1338 cdfcfb03 2020-12-06 stsp }
1339 25791caa 2018-10-24 stsp }
1340 25791caa 2018-10-24 stsp }
1341 25791caa 2018-10-24 stsp
1342 e5a0f69f 2018-08-18 stsp switch (ch) {
1343 1e37a5c2 2019-05-12 jcs case '\t':
1344 07b0611c 2022-06-23 thomas view->count = 0;
1345 1e37a5c2 2019-05-12 jcs if (view->child) {
1346 e78dc838 2020-12-04 stsp view->focussed = 0;
1347 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1348 e78dc838 2020-12-04 stsp view->focus_child = 1;
1349 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1350 e78dc838 2020-12-04 stsp view->focussed = 0;
1351 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1352 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1353 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1354 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1355 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1356 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1357 a5d43cac 2022-07-01 thomas if (err)
1358 a5d43cac 2022-07-01 thomas return err;
1359 a5d43cac 2022-07-01 thomas }
1360 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1361 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1362 a5d43cac 2022-07-01 thomas if (err)
1363 a5d43cac 2022-07-01 thomas return err;
1364 a5d43cac 2022-07-01 thomas }
1365 1e37a5c2 2019-05-12 jcs }
1366 1e37a5c2 2019-05-12 jcs break;
1367 1e37a5c2 2019-05-12 jcs case 'q':
1368 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1369 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1370 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1371 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1372 a5d43cac 2022-07-01 thomas if (err)
1373 a5d43cac 2022-07-01 thomas break;
1374 a5d43cac 2022-07-01 thomas }
1375 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1376 a5d43cac 2022-07-01 thomas }
1377 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1378 9970f7fc 2020-12-03 stsp view->dying = 1;
1379 1e37a5c2 2019-05-12 jcs break;
1380 1e37a5c2 2019-05-12 jcs case 'Q':
1381 1e37a5c2 2019-05-12 jcs *done = 1;
1382 1e37a5c2 2019-05-12 jcs break;
1383 1c5e5faa 2022-06-23 thomas case 'F':
1384 07b0611c 2022-06-23 thomas view->count = 0;
1385 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1386 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1387 1e37a5c2 2019-05-12 jcs break;
1388 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1389 e78dc838 2020-12-04 stsp view->focussed = 0;
1390 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1391 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1392 53d2bdd3 2022-07-10 thomas } else {
1393 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1394 53d2bdd3 2022-07-10 thomas if (!err)
1395 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1396 53d2bdd3 2022-07-10 thomas }
1397 1e37a5c2 2019-05-12 jcs if (err)
1398 1e37a5c2 2019-05-12 jcs break;
1399 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1400 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1401 1e37a5c2 2019-05-12 jcs } else {
1402 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1403 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1404 e78dc838 2020-12-04 stsp view->focussed = 1;
1405 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1406 1e37a5c2 2019-05-12 jcs } else {
1407 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1408 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1409 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1410 53d2bdd3 2022-07-10 thomas if (!err)
1411 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1412 669b5ffa 2018-10-07 stsp }
1413 1e37a5c2 2019-05-12 jcs if (err)
1414 1e37a5c2 2019-05-12 jcs break;
1415 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1416 a5d43cac 2022-07-01 thomas }
1417 a5d43cac 2022-07-01 thomas if (err)
1418 a5d43cac 2022-07-01 thomas break;
1419 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1420 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1421 a5d43cac 2022-07-01 thomas if (err)
1422 a5d43cac 2022-07-01 thomas break;
1423 1e37a5c2 2019-05-12 jcs }
1424 a5d43cac 2022-07-01 thomas if (view->parent)
1425 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1426 a5d43cac 2022-07-01 thomas if (!err)
1427 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1428 1e37a5c2 2019-05-12 jcs break;
1429 64486692 2022-07-07 thomas case 'S':
1430 53d2bdd3 2022-07-10 thomas view->count = 0;
1431 64486692 2022-07-07 thomas err = switch_split(view);
1432 53d2bdd3 2022-07-10 thomas break;
1433 53d2bdd3 2022-07-10 thomas case '-':
1434 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1435 64486692 2022-07-07 thomas break;
1436 53d2bdd3 2022-07-10 thomas case '+':
1437 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1438 53d2bdd3 2022-07-10 thomas break;
1439 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1440 60493ae3 2019-06-20 stsp break;
1441 60493ae3 2019-06-20 stsp case '/':
1442 07b0611c 2022-06-23 thomas view->count = 0;
1443 60493ae3 2019-06-20 stsp if (view->search_start)
1444 2b49a8ae 2019-06-22 stsp view_search_start(view);
1445 60493ae3 2019-06-20 stsp else
1446 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1447 1e37a5c2 2019-05-12 jcs break;
1448 b1bf1435 2019-06-21 stsp case 'N':
1449 60493ae3 2019-06-20 stsp case 'n':
1450 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1451 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1452 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1453 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1454 60493ae3 2019-06-20 stsp view->search_next(view);
1455 60493ae3 2019-06-20 stsp } else
1456 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1457 adf4c9e0 2022-07-03 thomas break;
1458 adf4c9e0 2022-07-03 thomas case 'A':
1459 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1460 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1461 adf4c9e0 2022-07-03 thomas else
1462 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1463 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1464 adf4c9e0 2022-07-03 thomas if (v->reset) {
1465 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1466 adf4c9e0 2022-07-03 thomas if (err)
1467 adf4c9e0 2022-07-03 thomas return err;
1468 adf4c9e0 2022-07-03 thomas }
1469 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1470 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1471 adf4c9e0 2022-07-03 thomas if (err)
1472 adf4c9e0 2022-07-03 thomas return err;
1473 adf4c9e0 2022-07-03 thomas }
1474 adf4c9e0 2022-07-03 thomas }
1475 60493ae3 2019-06-20 stsp break;
1476 1e37a5c2 2019-05-12 jcs default:
1477 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1478 1e37a5c2 2019-05-12 jcs break;
1479 e5a0f69f 2018-08-18 stsp }
1480 e5a0f69f 2018-08-18 stsp
1481 e5a0f69f 2018-08-18 stsp return err;
1482 bcbd79e2 2018-08-19 stsp }
1483 bcbd79e2 2018-08-19 stsp
1484 ef20f542 2022-06-26 thomas static int
1485 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1486 a3404814 2018-09-02 stsp {
1487 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1488 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1489 669b5ffa 2018-10-07 stsp return 0;
1490 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1491 669b5ffa 2018-10-07 stsp return 0;
1492 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1493 a3404814 2018-09-02 stsp return 0;
1494 a3404814 2018-09-02 stsp
1495 669b5ffa 2018-10-07 stsp return view->focussed;
1496 a3404814 2018-09-02 stsp }
1497 a3404814 2018-09-02 stsp
1498 bcbd79e2 2018-08-19 stsp static const struct got_error *
1499 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1500 e5a0f69f 2018-08-18 stsp {
1501 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1502 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1503 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1504 64486692 2022-07-07 thomas char *mode;
1505 fd823528 2018-10-22 stsp int fast_refresh = 10;
1506 1a76625f 2018-10-22 stsp int done = 0, errcode;
1507 e5a0f69f 2018-08-18 stsp
1508 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1509 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1510 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1511 64486692 2022-07-07 thomas else
1512 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1513 64486692 2022-07-07 thomas
1514 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1515 1a76625f 2018-10-22 stsp if (errcode)
1516 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1517 1a76625f 2018-10-22 stsp
1518 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1519 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1520 e5a0f69f 2018-08-18 stsp
1521 1004088d 2018-09-29 stsp view->focussed = 1;
1522 878940b7 2018-09-29 stsp err = view->show(view);
1523 0cf4efb1 2018-09-29 stsp if (err)
1524 0cf4efb1 2018-09-29 stsp return err;
1525 0cf4efb1 2018-09-29 stsp update_panels();
1526 0cf4efb1 2018-09-29 stsp doupdate();
1527 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1528 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1529 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1530 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1531 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1532 fd823528 2018-10-22 stsp
1533 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1534 e5a0f69f 2018-08-18 stsp if (err)
1535 e5a0f69f 2018-08-18 stsp break;
1536 9970f7fc 2020-12-03 stsp if (view->dying) {
1537 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1538 669b5ffa 2018-10-07 stsp
1539 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1540 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1541 9970f7fc 2020-12-03 stsp entry);
1542 e78dc838 2020-12-04 stsp else if (view->parent)
1543 669b5ffa 2018-10-07 stsp prev = view->parent;
1544 669b5ffa 2018-10-07 stsp
1545 e78dc838 2020-12-04 stsp if (view->parent) {
1546 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1547 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1548 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1549 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1550 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1551 40236d76 2022-06-23 thomas if (err)
1552 40236d76 2022-06-23 thomas break;
1553 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1554 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1555 e78dc838 2020-12-04 stsp } else
1556 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1557 669b5ffa 2018-10-07 stsp
1558 9970f7fc 2020-12-03 stsp err = view_close(view);
1559 fb59748f 2020-12-05 stsp if (err)
1560 e5a0f69f 2018-08-18 stsp goto done;
1561 669b5ffa 2018-10-07 stsp
1562 e78dc838 2020-12-04 stsp view = NULL;
1563 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1564 e78dc838 2020-12-04 stsp if (v->focussed)
1565 e78dc838 2020-12-04 stsp break;
1566 0cf4efb1 2018-09-29 stsp }
1567 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1568 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1569 e78dc838 2020-12-04 stsp if (prev)
1570 e78dc838 2020-12-04 stsp view = prev;
1571 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1572 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1573 e78dc838 2020-12-04 stsp tog_view_list_head);
1574 e78dc838 2020-12-04 stsp }
1575 e78dc838 2020-12-04 stsp if (view) {
1576 e78dc838 2020-12-04 stsp if (view->focus_child) {
1577 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1578 e78dc838 2020-12-04 stsp view = view->child;
1579 e78dc838 2020-12-04 stsp } else
1580 e78dc838 2020-12-04 stsp view->focussed = 1;
1581 e78dc838 2020-12-04 stsp }
1582 e78dc838 2020-12-04 stsp }
1583 e5a0f69f 2018-08-18 stsp }
1584 bcbd79e2 2018-08-19 stsp if (new_view) {
1585 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1586 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1587 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1588 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1589 86c66b02 2018-10-18 stsp continue;
1590 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1591 86c66b02 2018-10-18 stsp err = view_close(v);
1592 86c66b02 2018-10-18 stsp if (err)
1593 86c66b02 2018-10-18 stsp goto done;
1594 86c66b02 2018-10-18 stsp break;
1595 86c66b02 2018-10-18 stsp }
1596 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1597 fed7eaa8 2018-10-24 stsp view = new_view;
1598 7cd52833 2022-06-23 thomas }
1599 669b5ffa 2018-10-07 stsp if (view) {
1600 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1601 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1602 e78dc838 2020-12-04 stsp view = view->child;
1603 e78dc838 2020-12-04 stsp } else {
1604 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1605 e78dc838 2020-12-04 stsp view = view->parent;
1606 1a76625f 2018-10-22 stsp }
1607 e78dc838 2020-12-04 stsp show_panel(view->panel);
1608 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1609 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1610 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1611 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1612 669b5ffa 2018-10-07 stsp if (err)
1613 1a76625f 2018-10-22 stsp goto done;
1614 669b5ffa 2018-10-07 stsp }
1615 669b5ffa 2018-10-07 stsp err = view->show(view);
1616 0cf4efb1 2018-09-29 stsp if (err)
1617 1a76625f 2018-10-22 stsp goto done;
1618 669b5ffa 2018-10-07 stsp if (view->child) {
1619 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1620 669b5ffa 2018-10-07 stsp if (err)
1621 1a76625f 2018-10-22 stsp goto done;
1622 669b5ffa 2018-10-07 stsp }
1623 1a76625f 2018-10-22 stsp update_panels();
1624 1a76625f 2018-10-22 stsp doupdate();
1625 0cf4efb1 2018-09-29 stsp }
1626 e5a0f69f 2018-08-18 stsp }
1627 e5a0f69f 2018-08-18 stsp done:
1628 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1629 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1630 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1631 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1632 f2d749db 2022-07-12 thomas close_err = view_close(view);
1633 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1634 f2d749db 2022-07-12 thomas err = close_err;
1635 e5a0f69f 2018-08-18 stsp }
1636 1a76625f 2018-10-22 stsp
1637 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1638 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1639 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1640 1a76625f 2018-10-22 stsp
1641 e5a0f69f 2018-08-18 stsp return err;
1642 ea5e7bb5 2018-08-01 stsp }
1643 ea5e7bb5 2018-08-01 stsp
1644 4ed7e80c 2018-05-20 stsp __dead static void
1645 9f7d7167 2018-04-29 stsp usage_log(void)
1646 9f7d7167 2018-04-29 stsp {
1647 80ddbec8 2018-04-29 stsp endwin();
1648 c70c5802 2018-08-01 stsp fprintf(stderr,
1649 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1650 9f7d7167 2018-04-29 stsp getprogname());
1651 9f7d7167 2018-04-29 stsp exit(1);
1652 80ddbec8 2018-04-29 stsp }
1653 80ddbec8 2018-04-29 stsp
1654 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1655 80ddbec8 2018-04-29 stsp static const struct got_error *
1656 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1657 963b370f 2018-05-20 stsp {
1658 00dfcb92 2018-06-11 stsp char *vis = NULL;
1659 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1660 963b370f 2018-05-20 stsp
1661 963b370f 2018-05-20 stsp *ws = NULL;
1662 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1663 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1664 00dfcb92 2018-06-11 stsp int vislen;
1665 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1666 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1667 00dfcb92 2018-06-11 stsp
1668 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1669 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1670 00dfcb92 2018-06-11 stsp if (err)
1671 00dfcb92 2018-06-11 stsp return err;
1672 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1673 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1674 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1675 a7f50699 2018-06-11 stsp goto done;
1676 a7f50699 2018-06-11 stsp }
1677 00dfcb92 2018-06-11 stsp }
1678 963b370f 2018-05-20 stsp
1679 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1680 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1681 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1682 a7f50699 2018-06-11 stsp goto done;
1683 a7f50699 2018-06-11 stsp }
1684 963b370f 2018-05-20 stsp
1685 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1686 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1687 a7f50699 2018-06-11 stsp done:
1688 00dfcb92 2018-06-11 stsp free(vis);
1689 963b370f 2018-05-20 stsp if (err) {
1690 963b370f 2018-05-20 stsp free(*ws);
1691 963b370f 2018-05-20 stsp *ws = NULL;
1692 963b370f 2018-05-20 stsp *wlen = 0;
1693 963b370f 2018-05-20 stsp }
1694 963b370f 2018-05-20 stsp return err;
1695 05171be4 2022-06-23 thomas }
1696 05171be4 2022-06-23 thomas
1697 05171be4 2022-06-23 thomas static const struct got_error *
1698 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1699 05171be4 2022-06-23 thomas {
1700 05171be4 2022-06-23 thomas char *dst;
1701 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1702 05171be4 2022-06-23 thomas
1703 05171be4 2022-06-23 thomas *ptr = NULL;
1704 05171be4 2022-06-23 thomas n = len = strlen(src);
1705 83316834 2022-06-23 thomas dst = malloc(n + 1);
1706 05171be4 2022-06-23 thomas if (dst == NULL)
1707 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1708 05171be4 2022-06-23 thomas
1709 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1710 05171be4 2022-06-23 thomas const char c = src[idx];
1711 05171be4 2022-06-23 thomas
1712 05171be4 2022-06-23 thomas if (c == '\t') {
1713 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1714 b235ad5d 2022-06-23 thomas char *p;
1715 b235ad5d 2022-06-23 thomas
1716 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1717 83316834 2022-06-23 thomas if (p == NULL) {
1718 83316834 2022-06-23 thomas free(dst);
1719 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1720 83316834 2022-06-23 thomas
1721 83316834 2022-06-23 thomas }
1722 83316834 2022-06-23 thomas dst = p;
1723 05171be4 2022-06-23 thomas n += nb;
1724 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1725 05171be4 2022-06-23 thomas sz += nb;
1726 05171be4 2022-06-23 thomas } else
1727 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1728 05171be4 2022-06-23 thomas ++idx;
1729 05171be4 2022-06-23 thomas }
1730 05171be4 2022-06-23 thomas
1731 05171be4 2022-06-23 thomas dst[sz] = '\0';
1732 05171be4 2022-06-23 thomas *ptr = dst;
1733 05171be4 2022-06-23 thomas return NULL;
1734 963b370f 2018-05-20 stsp }
1735 963b370f 2018-05-20 stsp
1736 8d208d34 2022-06-23 thomas /*
1737 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1738 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1739 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1740 8d208d34 2022-06-23 thomas * *rcol.
1741 f91a2b48 2022-06-23 thomas */
1742 8d208d34 2022-06-23 thomas static int
1743 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1744 8d208d34 2022-06-23 thomas {
1745 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1746 f91a2b48 2022-06-23 thomas
1747 8d208d34 2022-06-23 thomas if (n == 0) {
1748 8d208d34 2022-06-23 thomas *rcol = cols;
1749 8d208d34 2022-06-23 thomas return off;
1750 8d208d34 2022-06-23 thomas }
1751 f91a2b48 2022-06-23 thomas
1752 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1753 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1754 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1755 8d208d34 2022-06-23 thomas else
1756 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1757 f91a2b48 2022-06-23 thomas
1758 8d208d34 2022-06-23 thomas if (width == -1) {
1759 8d208d34 2022-06-23 thomas width = 1;
1760 8d208d34 2022-06-23 thomas wline[i] = L'.';
1761 f91a2b48 2022-06-23 thomas }
1762 f91a2b48 2022-06-23 thomas
1763 8d208d34 2022-06-23 thomas if (cols + width > n)
1764 8d208d34 2022-06-23 thomas break;
1765 8d208d34 2022-06-23 thomas cols += width;
1766 f91a2b48 2022-06-23 thomas }
1767 f91a2b48 2022-06-23 thomas
1768 8d208d34 2022-06-23 thomas *rcol = cols;
1769 8d208d34 2022-06-23 thomas return i;
1770 f91a2b48 2022-06-23 thomas }
1771 f91a2b48 2022-06-23 thomas
1772 f91a2b48 2022-06-23 thomas /*
1773 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1774 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1775 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1776 f91a2b48 2022-06-23 thomas */
1777 f91a2b48 2022-06-23 thomas static const struct got_error *
1778 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1779 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1780 f91a2b48 2022-06-23 thomas {
1781 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1782 8d208d34 2022-06-23 thomas int cols;
1783 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1784 05171be4 2022-06-23 thomas char *exstr = NULL;
1785 963b370f 2018-05-20 stsp size_t wlen;
1786 8d208d34 2022-06-23 thomas int i, scrollx;
1787 963b370f 2018-05-20 stsp
1788 963b370f 2018-05-20 stsp *wlinep = NULL;
1789 b700b5d6 2018-07-10 stsp *widthp = 0;
1790 963b370f 2018-05-20 stsp
1791 05171be4 2022-06-23 thomas if (expand) {
1792 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1793 05171be4 2022-06-23 thomas if (err)
1794 05171be4 2022-06-23 thomas return err;
1795 05171be4 2022-06-23 thomas }
1796 05171be4 2022-06-23 thomas
1797 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1798 05171be4 2022-06-23 thomas free(exstr);
1799 963b370f 2018-05-20 stsp if (err)
1800 963b370f 2018-05-20 stsp return err;
1801 963b370f 2018-05-20 stsp
1802 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1803 f91a2b48 2022-06-23 thomas
1804 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1805 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1806 3f670bfb 2020-12-10 stsp wlen--;
1807 3f670bfb 2020-12-10 stsp }
1808 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1809 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1810 3f670bfb 2020-12-10 stsp wlen--;
1811 3f670bfb 2020-12-10 stsp }
1812 3f670bfb 2020-12-10 stsp
1813 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1814 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1815 27a741e5 2019-09-11 stsp
1816 b700b5d6 2018-07-10 stsp if (widthp)
1817 b700b5d6 2018-07-10 stsp *widthp = cols;
1818 f91a2b48 2022-06-23 thomas if (scrollxp)
1819 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1820 963b370f 2018-05-20 stsp if (err)
1821 963b370f 2018-05-20 stsp free(wline);
1822 963b370f 2018-05-20 stsp else
1823 963b370f 2018-05-20 stsp *wlinep = wline;
1824 963b370f 2018-05-20 stsp return err;
1825 963b370f 2018-05-20 stsp }
1826 963b370f 2018-05-20 stsp
1827 8b473291 2019-02-21 stsp static const struct got_error*
1828 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1829 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1830 8b473291 2019-02-21 stsp {
1831 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1832 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1833 8b473291 2019-02-21 stsp char *s;
1834 8b473291 2019-02-21 stsp const char *name;
1835 8b473291 2019-02-21 stsp
1836 8b473291 2019-02-21 stsp *refs_str = NULL;
1837 8b473291 2019-02-21 stsp
1838 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1839 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1840 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1841 52b5abe1 2019-08-13 stsp int cmp;
1842 52b5abe1 2019-08-13 stsp
1843 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1844 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1845 8b473291 2019-02-21 stsp continue;
1846 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1847 8b473291 2019-02-21 stsp name += 5;
1848 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1849 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1850 7143d404 2019-03-12 stsp continue;
1851 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1852 8b473291 2019-02-21 stsp name += 6;
1853 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1854 8b473291 2019-02-21 stsp name += 8;
1855 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1856 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1857 79cc719f 2020-04-24 stsp continue;
1858 79cc719f 2020-04-24 stsp }
1859 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1860 48cae60d 2020-09-22 stsp if (err)
1861 48cae60d 2020-09-22 stsp break;
1862 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1863 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1864 5d844a1e 2019-08-13 stsp if (err) {
1865 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1866 48cae60d 2020-09-22 stsp free(ref_id);
1867 5d844a1e 2019-08-13 stsp break;
1868 48cae60d 2020-09-22 stsp }
1869 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1870 5d844a1e 2019-08-13 stsp err = NULL;
1871 5d844a1e 2019-08-13 stsp tag = NULL;
1872 5d844a1e 2019-08-13 stsp }
1873 52b5abe1 2019-08-13 stsp }
1874 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1875 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1876 48cae60d 2020-09-22 stsp free(ref_id);
1877 52b5abe1 2019-08-13 stsp if (tag)
1878 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1879 52b5abe1 2019-08-13 stsp if (cmp != 0)
1880 52b5abe1 2019-08-13 stsp continue;
1881 8b473291 2019-02-21 stsp s = *refs_str;
1882 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1883 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1884 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1885 8b473291 2019-02-21 stsp free(s);
1886 8b473291 2019-02-21 stsp *refs_str = NULL;
1887 8b473291 2019-02-21 stsp break;
1888 8b473291 2019-02-21 stsp }
1889 8b473291 2019-02-21 stsp free(s);
1890 8b473291 2019-02-21 stsp }
1891 8b473291 2019-02-21 stsp
1892 8b473291 2019-02-21 stsp return err;
1893 8b473291 2019-02-21 stsp }
1894 8b473291 2019-02-21 stsp
1895 963b370f 2018-05-20 stsp static const struct got_error *
1896 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1897 27a741e5 2019-09-11 stsp int col_tab_align)
1898 5813d178 2019-03-09 stsp {
1899 e6b8b890 2020-12-29 naddy char *smallerthan;
1900 5813d178 2019-03-09 stsp
1901 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1902 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1903 5813d178 2019-03-09 stsp author = smallerthan + 1;
1904 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1905 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1906 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1907 5813d178 2019-03-09 stsp }
1908 5813d178 2019-03-09 stsp
1909 5813d178 2019-03-09 stsp static const struct got_error *
1910 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1911 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1912 8fdc79fe 2020-12-01 naddy int author_display_cols)
1913 80ddbec8 2018-04-29 stsp {
1914 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1915 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1916 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1917 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1918 5813d178 2019-03-09 stsp char *author = NULL;
1919 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1920 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1921 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1922 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1923 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1924 ccb26ccd 2018-11-05 stsp struct tm tm;
1925 45d799e2 2018-12-23 stsp time_t committer_time;
1926 11b20872 2019-11-08 stsp struct tog_color *tc;
1927 80ddbec8 2018-04-29 stsp
1928 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1929 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1930 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1931 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1932 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1933 b39d25c7 2018-07-10 stsp
1934 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1935 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1936 b39d25c7 2018-07-10 stsp else
1937 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1938 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1939 11b20872 2019-11-08 stsp if (tc)
1940 11b20872 2019-11-08 stsp wattr_on(view->window,
1941 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1942 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1943 11b20872 2019-11-08 stsp if (tc)
1944 11b20872 2019-11-08 stsp wattr_off(view->window,
1945 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1946 27a741e5 2019-09-11 stsp col = limit;
1947 b39d25c7 2018-07-10 stsp if (col > avail)
1948 b39d25c7 2018-07-10 stsp goto done;
1949 6570a66d 2019-11-08 stsp
1950 6570a66d 2019-11-08 stsp if (avail >= 120) {
1951 6570a66d 2019-11-08 stsp char *id_str;
1952 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1953 6570a66d 2019-11-08 stsp if (err)
1954 6570a66d 2019-11-08 stsp goto done;
1955 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1956 11b20872 2019-11-08 stsp if (tc)
1957 11b20872 2019-11-08 stsp wattr_on(view->window,
1958 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1959 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1960 11b20872 2019-11-08 stsp if (tc)
1961 11b20872 2019-11-08 stsp wattr_off(view->window,
1962 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1963 6570a66d 2019-11-08 stsp free(id_str);
1964 6570a66d 2019-11-08 stsp col += 9;
1965 6570a66d 2019-11-08 stsp if (col > avail)
1966 6570a66d 2019-11-08 stsp goto done;
1967 6570a66d 2019-11-08 stsp }
1968 b39d25c7 2018-07-10 stsp
1969 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1970 5813d178 2019-03-09 stsp if (author == NULL) {
1971 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1972 80ddbec8 2018-04-29 stsp goto done;
1973 80ddbec8 2018-04-29 stsp }
1974 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1975 bb737323 2018-05-20 stsp if (err)
1976 bb737323 2018-05-20 stsp goto done;
1977 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1978 11b20872 2019-11-08 stsp if (tc)
1979 11b20872 2019-11-08 stsp wattr_on(view->window,
1980 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1981 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1982 11b20872 2019-11-08 stsp if (tc)
1983 11b20872 2019-11-08 stsp wattr_off(view->window,
1984 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1985 bb737323 2018-05-20 stsp col += author_width;
1986 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1987 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1988 bb737323 2018-05-20 stsp col++;
1989 bb737323 2018-05-20 stsp author_width++;
1990 bb737323 2018-05-20 stsp }
1991 9c2eaf34 2018-05-20 stsp if (col > avail)
1992 9c2eaf34 2018-05-20 stsp goto done;
1993 80ddbec8 2018-04-29 stsp
1994 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1995 5943eee2 2019-08-13 stsp if (err)
1996 6d9fbc00 2018-04-29 stsp goto done;
1997 bb737323 2018-05-20 stsp logmsg = logmsg0;
1998 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1999 bb737323 2018-05-20 stsp logmsg++;
2000 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2001 bb737323 2018-05-20 stsp if (newline)
2002 bb737323 2018-05-20 stsp *newline = '\0';
2003 f91a2b48 2022-06-23 thomas limit = avail - col;
2004 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2005 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2006 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2007 f91a2b48 2022-06-23 thomas limit, col, 1);
2008 331b1a16 2022-06-23 thomas if (err)
2009 331b1a16 2022-06-23 thomas goto done;
2010 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2011 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2012 27a741e5 2019-09-11 stsp while (col < avail) {
2013 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2014 bb737323 2018-05-20 stsp col++;
2015 881b2d3e 2018-04-30 stsp }
2016 80ddbec8 2018-04-29 stsp done:
2017 80ddbec8 2018-04-29 stsp free(logmsg0);
2018 bb737323 2018-05-20 stsp free(wlogmsg);
2019 5813d178 2019-03-09 stsp free(author);
2020 bb737323 2018-05-20 stsp free(wauthor);
2021 80ddbec8 2018-04-29 stsp free(line);
2022 80ddbec8 2018-04-29 stsp return err;
2023 80ddbec8 2018-04-29 stsp }
2024 26ed57b2 2018-05-19 stsp
2025 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2026 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2027 899d86c2 2018-05-10 stsp struct got_object_id *id)
2028 80ddbec8 2018-04-29 stsp {
2029 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2030 80ddbec8 2018-04-29 stsp
2031 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2032 80ddbec8 2018-04-29 stsp if (entry == NULL)
2033 899d86c2 2018-05-10 stsp return NULL;
2034 99db9666 2018-05-07 stsp
2035 899d86c2 2018-05-10 stsp entry->id = id;
2036 99db9666 2018-05-07 stsp entry->commit = commit;
2037 899d86c2 2018-05-10 stsp return entry;
2038 99db9666 2018-05-07 stsp }
2039 80ddbec8 2018-04-29 stsp
2040 99db9666 2018-05-07 stsp static void
2041 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2042 99db9666 2018-05-07 stsp {
2043 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2044 99db9666 2018-05-07 stsp
2045 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2046 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2047 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2048 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2049 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2050 99db9666 2018-05-07 stsp free(entry);
2051 99db9666 2018-05-07 stsp }
2052 99db9666 2018-05-07 stsp
2053 99db9666 2018-05-07 stsp static void
2054 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2055 99db9666 2018-05-07 stsp {
2056 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2057 99db9666 2018-05-07 stsp pop_commit(commits);
2058 c4972b91 2018-05-07 stsp }
2059 c4972b91 2018-05-07 stsp
2060 c4972b91 2018-05-07 stsp static const struct got_error *
2061 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2062 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2063 13add988 2019-10-15 stsp {
2064 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2065 13add988 2019-10-15 stsp regmatch_t regmatch;
2066 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2067 13add988 2019-10-15 stsp
2068 13add988 2019-10-15 stsp *have_match = 0;
2069 13add988 2019-10-15 stsp
2070 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2071 13add988 2019-10-15 stsp if (err)
2072 13add988 2019-10-15 stsp return err;
2073 13add988 2019-10-15 stsp
2074 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2075 13add988 2019-10-15 stsp if (err)
2076 13add988 2019-10-15 stsp goto done;
2077 13add988 2019-10-15 stsp
2078 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2079 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2080 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2081 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2082 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2083 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2084 13add988 2019-10-15 stsp *have_match = 1;
2085 13add988 2019-10-15 stsp done:
2086 13add988 2019-10-15 stsp free(id_str);
2087 13add988 2019-10-15 stsp free(logmsg);
2088 13add988 2019-10-15 stsp return err;
2089 13add988 2019-10-15 stsp }
2090 13add988 2019-10-15 stsp
2091 13add988 2019-10-15 stsp static const struct got_error *
2092 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2093 c4972b91 2018-05-07 stsp {
2094 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2095 9ba79e04 2018-06-11 stsp
2096 1a76625f 2018-10-22 stsp /*
2097 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2098 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2099 1a76625f 2018-10-22 stsp * while updating the display.
2100 1a76625f 2018-10-22 stsp */
2101 4e0d2870 2020-12-07 naddy do {
2102 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2103 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2104 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2105 1a76625f 2018-10-22 stsp int errcode;
2106 899d86c2 2018-05-10 stsp
2107 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2108 4e0d2870 2020-12-07 naddy NULL, NULL);
2109 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2110 ecb28ae0 2018-07-16 stsp break;
2111 899d86c2 2018-05-10 stsp
2112 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2113 9ba79e04 2018-06-11 stsp if (err)
2114 9ba79e04 2018-06-11 stsp break;
2115 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2116 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2117 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2118 9ba79e04 2018-06-11 stsp break;
2119 9ba79e04 2018-06-11 stsp }
2120 93e45b7c 2018-09-24 stsp
2121 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2122 1a76625f 2018-10-22 stsp if (errcode) {
2123 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2124 13add988 2019-10-15 stsp "pthread_mutex_lock");
2125 1a76625f 2018-10-22 stsp break;
2126 1a76625f 2018-10-22 stsp }
2127 1a76625f 2018-10-22 stsp
2128 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2129 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2130 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2131 1a76625f 2018-10-22 stsp
2132 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2133 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2134 7c1452c1 2020-03-26 stsp int have_match;
2135 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2136 7c1452c1 2020-03-26 stsp if (err)
2137 7c1452c1 2020-03-26 stsp break;
2138 7c1452c1 2020-03-26 stsp if (have_match)
2139 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 13add988 2019-10-15 stsp }
2141 13add988 2019-10-15 stsp
2142 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2143 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2144 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2145 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2146 7c1452c1 2020-03-26 stsp if (err)
2147 13add988 2019-10-15 stsp break;
2148 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2149 899d86c2 2018-05-10 stsp
2150 9ba79e04 2018-06-11 stsp return err;
2151 0553a4e3 2018-04-30 stsp }
2152 0553a4e3 2018-04-30 stsp
2153 2b779855 2020-12-05 naddy static void
2154 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2155 2b779855 2020-12-05 naddy {
2156 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2157 2b779855 2020-12-05 naddy int ncommits = 0;
2158 2b779855 2020-12-05 naddy
2159 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2160 2b779855 2020-12-05 naddy while (entry) {
2161 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2162 2b779855 2020-12-05 naddy s->selected_entry = entry;
2163 2b779855 2020-12-05 naddy break;
2164 2b779855 2020-12-05 naddy }
2165 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2166 2b779855 2020-12-05 naddy ncommits++;
2167 2b779855 2020-12-05 naddy }
2168 2b779855 2020-12-05 naddy }
2169 2b779855 2020-12-05 naddy
2170 0553a4e3 2018-04-30 stsp static const struct got_error *
2171 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2172 0553a4e3 2018-04-30 stsp {
2173 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2174 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2175 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2176 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2177 60493ae3 2019-06-20 stsp int width;
2178 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2179 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2180 8b473291 2019-02-21 stsp char *refs_str = NULL;
2181 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2182 11b20872 2019-11-08 stsp struct tog_color *tc;
2183 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2184 0553a4e3 2018-04-30 stsp
2185 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2186 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2187 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2188 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2189 1a76625f 2018-10-22 stsp if (err)
2190 ecb28ae0 2018-07-16 stsp return err;
2191 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2192 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2193 d2075bf3 2020-12-25 stsp if (refs) {
2194 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2195 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2196 d2075bf3 2020-12-25 stsp if (err)
2197 d2075bf3 2020-12-25 stsp goto done;
2198 d2075bf3 2020-12-25 stsp }
2199 867c6645 2018-07-10 stsp }
2200 359bfafd 2019-02-22 stsp
2201 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2202 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2203 1a76625f 2018-10-22 stsp
2204 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2205 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2206 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2207 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2208 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2209 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2210 8f4ed634 2020-03-26 stsp goto done;
2211 8f4ed634 2020-03-26 stsp }
2212 8f4ed634 2020-03-26 stsp } else {
2213 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2214 f9686aa5 2020-03-27 stsp
2215 f9686aa5 2020-03-27 stsp if (view->searching) {
2216 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2217 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2218 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2219 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2220 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2221 f9686aa5 2020-03-27 stsp search_str = "searching...";
2222 f9686aa5 2020-03-27 stsp }
2223 f9686aa5 2020-03-27 stsp
2224 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2225 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2226 f9686aa5 2020-03-27 stsp search_str ? search_str :
2227 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2228 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2229 8f4ed634 2020-03-26 stsp goto done;
2230 8f4ed634 2020-03-26 stsp }
2231 8b473291 2019-02-21 stsp }
2232 1a76625f 2018-10-22 stsp
2233 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2234 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2235 91198554 2022-06-23 thomas "........................................",
2236 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2237 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2238 1a76625f 2018-10-22 stsp header = NULL;
2239 1a76625f 2018-10-22 stsp goto done;
2240 1a76625f 2018-10-22 stsp }
2241 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2242 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2243 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2244 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2245 1a76625f 2018-10-22 stsp header = NULL;
2246 1a76625f 2018-10-22 stsp goto done;
2247 ecb28ae0 2018-07-16 stsp }
2248 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2249 1a76625f 2018-10-22 stsp if (err)
2250 1a76625f 2018-10-22 stsp goto done;
2251 867c6645 2018-07-10 stsp
2252 2814baeb 2018-08-01 stsp werase(view->window);
2253 867c6645 2018-07-10 stsp
2254 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2255 a3404814 2018-09-02 stsp wstandout(view->window);
2256 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2257 11b20872 2019-11-08 stsp if (tc)
2258 11b20872 2019-11-08 stsp wattr_on(view->window,
2259 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2260 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2261 11b20872 2019-11-08 stsp if (tc)
2262 11b20872 2019-11-08 stsp wattr_off(view->window,
2263 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2264 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2265 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2266 1a76625f 2018-10-22 stsp width++;
2267 1a76625f 2018-10-22 stsp }
2268 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2269 a3404814 2018-09-02 stsp wstandend(view->window);
2270 ecb28ae0 2018-07-16 stsp free(wline);
2271 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2272 1a76625f 2018-10-22 stsp goto done;
2273 0553a4e3 2018-04-30 stsp
2274 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2275 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2276 5813d178 2019-03-09 stsp ncommits = 0;
2277 05171be4 2022-06-23 thomas view->maxx = 0;
2278 5813d178 2019-03-09 stsp while (entry) {
2279 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2280 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2281 5813d178 2019-03-09 stsp int width;
2282 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2283 5813d178 2019-03-09 stsp break;
2284 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2285 5813d178 2019-03-09 stsp if (author == NULL) {
2286 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2287 5813d178 2019-03-09 stsp goto done;
2288 5813d178 2019-03-09 stsp }
2289 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2290 27a741e5 2019-09-11 stsp date_display_cols);
2291 5813d178 2019-03-09 stsp if (author_cols < width)
2292 5813d178 2019-03-09 stsp author_cols = width;
2293 5813d178 2019-03-09 stsp free(wauthor);
2294 5813d178 2019-03-09 stsp free(author);
2295 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
2296 05171be4 2022-06-23 thomas if (err)
2297 05171be4 2022-06-23 thomas goto done;
2298 05171be4 2022-06-23 thomas msg = msg0;
2299 05171be4 2022-06-23 thomas while (*msg == '\n')
2300 05171be4 2022-06-23 thomas ++msg;
2301 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2302 331b1a16 2022-06-23 thomas *eol = '\0';
2303 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2304 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2305 331b1a16 2022-06-23 thomas if (err)
2306 331b1a16 2022-06-23 thomas goto done;
2307 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2308 05171be4 2022-06-23 thomas free(msg0);
2309 331b1a16 2022-06-23 thomas free(wmsg);
2310 7ca04879 2019-10-19 stsp ncommits++;
2311 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2312 5813d178 2019-03-09 stsp }
2313 5813d178 2019-03-09 stsp
2314 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2315 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2316 867c6645 2018-07-10 stsp ncommits = 0;
2317 899d86c2 2018-05-10 stsp while (entry) {
2318 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2319 899d86c2 2018-05-10 stsp break;
2320 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2321 2814baeb 2018-08-01 stsp wstandout(view->window);
2322 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2323 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2324 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2325 2814baeb 2018-08-01 stsp wstandend(view->window);
2326 0553a4e3 2018-04-30 stsp if (err)
2327 60493ae3 2019-06-20 stsp goto done;
2328 0553a4e3 2018-04-30 stsp ncommits++;
2329 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2330 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2331 80ddbec8 2018-04-29 stsp }
2332 80ddbec8 2018-04-29 stsp
2333 a5d43cac 2022-07-01 thomas view_border(view);
2334 1a76625f 2018-10-22 stsp done:
2335 1a76625f 2018-10-22 stsp free(id_str);
2336 8b473291 2019-02-21 stsp free(refs_str);
2337 1a76625f 2018-10-22 stsp free(ncommits_str);
2338 1a76625f 2018-10-22 stsp free(header);
2339 80ddbec8 2018-04-29 stsp return err;
2340 9f7d7167 2018-04-29 stsp }
2341 07b55e75 2018-05-10 stsp
2342 07b55e75 2018-05-10 stsp static void
2343 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2344 07b55e75 2018-05-10 stsp {
2345 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2346 07b55e75 2018-05-10 stsp int nscrolled = 0;
2347 07b55e75 2018-05-10 stsp
2348 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2349 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2350 07b55e75 2018-05-10 stsp return;
2351 9f7d7167 2018-04-29 stsp
2352 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2353 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2354 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2355 07b55e75 2018-05-10 stsp if (entry) {
2356 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2357 07b55e75 2018-05-10 stsp nscrolled++;
2358 07b55e75 2018-05-10 stsp }
2359 07b55e75 2018-05-10 stsp }
2360 aa075928 2018-05-10 stsp }
2361 aa075928 2018-05-10 stsp
2362 aa075928 2018-05-10 stsp static const struct got_error *
2363 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2364 aa075928 2018-05-10 stsp {
2365 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2366 5e224a3e 2019-02-22 stsp int errcode;
2367 8a42fca8 2019-02-22 stsp
2368 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2369 aa075928 2018-05-10 stsp
2370 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2371 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2372 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2373 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2374 7aafa0d1 2019-02-22 stsp if (errcode)
2375 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2376 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2377 7c1452c1 2020-03-26 stsp
2378 7c1452c1 2020-03-26 stsp /*
2379 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2380 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2381 7c1452c1 2020-03-26 stsp */
2382 7c1452c1 2020-03-26 stsp if (!wait)
2383 7c1452c1 2020-03-26 stsp break;
2384 7c1452c1 2020-03-26 stsp
2385 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2386 ffe38506 2020-12-01 naddy show_log_view(view);
2387 7c1452c1 2020-03-26 stsp update_panels();
2388 7c1452c1 2020-03-26 stsp doupdate();
2389 7c1452c1 2020-03-26 stsp
2390 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2391 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2392 82954512 2020-02-03 stsp if (errcode)
2393 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2394 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2395 82954512 2020-02-03 stsp
2396 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2397 ffe38506 2020-12-01 naddy show_log_view(view);
2398 7c1452c1 2020-03-26 stsp update_panels();
2399 7c1452c1 2020-03-26 stsp doupdate();
2400 5e224a3e 2019-02-22 stsp }
2401 5e224a3e 2019-02-22 stsp
2402 5e224a3e 2019-02-22 stsp return NULL;
2403 a5d43cac 2022-07-01 thomas }
2404 a5d43cac 2022-07-01 thomas
2405 a5d43cac 2022-07-01 thomas static const struct got_error *
2406 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2407 a5d43cac 2022-07-01 thomas {
2408 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2409 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2410 fe731b51 2022-07-14 thomas
2411 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2412 fe731b51 2022-07-14 thomas return NULL;
2413 a5d43cac 2022-07-01 thomas
2414 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2415 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2416 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2417 a5d43cac 2022-07-01 thomas
2418 a5d43cac 2022-07-01 thomas return err;
2419 5e224a3e 2019-02-22 stsp }
2420 5e224a3e 2019-02-22 stsp
2421 5e224a3e 2019-02-22 stsp static const struct got_error *
2422 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2423 5e224a3e 2019-02-22 stsp {
2424 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2425 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2426 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2427 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2428 5e224a3e 2019-02-22 stsp
2429 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2430 5e224a3e 2019-02-22 stsp return NULL;
2431 5e224a3e 2019-02-22 stsp
2432 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2433 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2434 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2435 08ebd0a9 2019-02-22 stsp /*
2436 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2437 08ebd0a9 2019-02-22 stsp */
2438 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2439 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2440 5e224a3e 2019-02-22 stsp if (err)
2441 5e224a3e 2019-02-22 stsp return err;
2442 7aafa0d1 2019-02-22 stsp }
2443 b295e71b 2019-02-22 stsp
2444 7aafa0d1 2019-02-22 stsp do {
2445 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2446 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2447 88048b54 2019-02-21 stsp break;
2448 88048b54 2019-02-21 stsp
2449 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2450 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2451 aa075928 2018-05-10 stsp
2452 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2453 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2454 dd0a52c1 2018-05-20 stsp break;
2455 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2456 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2457 aa075928 2018-05-10 stsp
2458 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2459 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2460 a5d43cac 2022-07-01 thomas else
2461 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2462 a5d43cac 2022-07-01 thomas
2463 dd0a52c1 2018-05-20 stsp return err;
2464 07b55e75 2018-05-10 stsp }
2465 4a7f7875 2018-05-10 stsp
2466 cd0acaa7 2018-05-20 stsp static const struct got_error *
2467 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2468 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2469 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2470 cd0acaa7 2018-05-20 stsp {
2471 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2472 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2473 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2474 cd0acaa7 2018-05-20 stsp
2475 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2476 15a94983 2018-12-23 stsp if (diff_view == NULL)
2477 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2478 ea5e7bb5 2018-08-01 stsp
2479 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2480 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2481 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2482 e5a0f69f 2018-08-18 stsp if (err == NULL)
2483 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2484 cd0acaa7 2018-05-20 stsp return err;
2485 4a7f7875 2018-05-10 stsp }
2486 4a7f7875 2018-05-10 stsp
2487 80ddbec8 2018-04-29 stsp static const struct got_error *
2488 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2489 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2490 9343a5fb 2018-06-23 stsp {
2491 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2492 941e9f74 2019-05-21 stsp
2493 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2494 941e9f74 2019-05-21 stsp if (parent == NULL)
2495 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2496 941e9f74 2019-05-21 stsp
2497 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2498 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2499 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2500 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2501 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2502 941e9f74 2019-05-21 stsp s->tree = subtree;
2503 941e9f74 2019-05-21 stsp s->selected = 0;
2504 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2505 941e9f74 2019-05-21 stsp return NULL;
2506 941e9f74 2019-05-21 stsp }
2507 941e9f74 2019-05-21 stsp
2508 941e9f74 2019-05-21 stsp static const struct got_error *
2509 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2510 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2511 941e9f74 2019-05-21 stsp {
2512 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2513 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2514 941e9f74 2019-05-21 stsp const char *p;
2515 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2516 9343a5fb 2018-06-23 stsp
2517 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2518 941e9f74 2019-05-21 stsp p = path;
2519 941e9f74 2019-05-21 stsp while (*p) {
2520 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2521 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2522 56e0773d 2019-11-28 stsp char *te_name;
2523 33cbf02b 2020-01-12 stsp
2524 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2525 33cbf02b 2020-01-12 stsp p++;
2526 941e9f74 2019-05-21 stsp
2527 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2528 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2529 941e9f74 2019-05-21 stsp if (slash == NULL)
2530 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2531 33cbf02b 2020-01-12 stsp else
2532 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2533 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2534 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2535 56e0773d 2019-11-28 stsp break;
2536 941e9f74 2019-05-21 stsp }
2537 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2538 56e0773d 2019-11-28 stsp if (te == NULL) {
2539 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2540 56e0773d 2019-11-28 stsp free(te_name);
2541 941e9f74 2019-05-21 stsp break;
2542 941e9f74 2019-05-21 stsp }
2543 56e0773d 2019-11-28 stsp free(te_name);
2544 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2545 941e9f74 2019-05-21 stsp
2546 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2547 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2548 b03c880f 2019-05-21 stsp
2549 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2550 941e9f74 2019-05-21 stsp if (slash)
2551 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2552 941e9f74 2019-05-21 stsp else
2553 941e9f74 2019-05-21 stsp subpath = strdup(path);
2554 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2555 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2556 941e9f74 2019-05-21 stsp break;
2557 941e9f74 2019-05-21 stsp }
2558 941e9f74 2019-05-21 stsp
2559 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2560 941e9f74 2019-05-21 stsp subpath);
2561 941e9f74 2019-05-21 stsp if (err)
2562 941e9f74 2019-05-21 stsp break;
2563 941e9f74 2019-05-21 stsp
2564 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2565 941e9f74 2019-05-21 stsp free(tree_id);
2566 941e9f74 2019-05-21 stsp if (err)
2567 941e9f74 2019-05-21 stsp break;
2568 941e9f74 2019-05-21 stsp
2569 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2570 941e9f74 2019-05-21 stsp if (err) {
2571 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2572 941e9f74 2019-05-21 stsp break;
2573 941e9f74 2019-05-21 stsp }
2574 941e9f74 2019-05-21 stsp if (slash == NULL)
2575 941e9f74 2019-05-21 stsp break;
2576 941e9f74 2019-05-21 stsp free(subpath);
2577 941e9f74 2019-05-21 stsp subpath = NULL;
2578 941e9f74 2019-05-21 stsp p = slash;
2579 941e9f74 2019-05-21 stsp }
2580 941e9f74 2019-05-21 stsp
2581 941e9f74 2019-05-21 stsp free(subpath);
2582 1a76625f 2018-10-22 stsp return err;
2583 61266923 2020-01-14 stsp }
2584 61266923 2020-01-14 stsp
2585 61266923 2020-01-14 stsp static const struct got_error *
2586 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2587 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2588 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2589 55cccc34 2020-02-20 stsp {
2590 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2591 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2592 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2593 55cccc34 2020-02-20 stsp
2594 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2595 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2596 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2597 55cccc34 2020-02-20 stsp
2598 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2599 bc573f3b 2021-07-10 stsp if (err)
2600 55cccc34 2020-02-20 stsp return err;
2601 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2602 55cccc34 2020-02-20 stsp
2603 55cccc34 2020-02-20 stsp *new_view = tree_view;
2604 55cccc34 2020-02-20 stsp
2605 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2606 55cccc34 2020-02-20 stsp return NULL;
2607 55cccc34 2020-02-20 stsp
2608 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2609 55cccc34 2020-02-20 stsp }
2610 55cccc34 2020-02-20 stsp
2611 55cccc34 2020-02-20 stsp static const struct got_error *
2612 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2613 61266923 2020-01-14 stsp {
2614 61266923 2020-01-14 stsp sigset_t sigset;
2615 61266923 2020-01-14 stsp int errcode;
2616 61266923 2020-01-14 stsp
2617 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2618 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2619 61266923 2020-01-14 stsp
2620 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2621 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2622 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2623 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2624 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2625 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2626 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2627 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2628 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2629 61266923 2020-01-14 stsp
2630 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2631 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2632 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2633 61266923 2020-01-14 stsp
2634 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2635 61266923 2020-01-14 stsp if (errcode)
2636 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2637 61266923 2020-01-14 stsp
2638 61266923 2020-01-14 stsp return NULL;
2639 1a76625f 2018-10-22 stsp }
2640 1a76625f 2018-10-22 stsp
2641 1a76625f 2018-10-22 stsp static void *
2642 1a76625f 2018-10-22 stsp log_thread(void *arg)
2643 1a76625f 2018-10-22 stsp {
2644 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2645 1a76625f 2018-10-22 stsp int errcode = 0;
2646 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2647 1a76625f 2018-10-22 stsp int done = 0;
2648 61266923 2020-01-14 stsp
2649 f2d749db 2022-07-12 thomas /*
2650 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2651 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2652 f2d749db 2022-07-12 thomas */
2653 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2654 f2d749db 2022-07-12 thomas if (errcode) {
2655 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2656 61266923 2020-01-14 stsp return (void *)err;
2657 f2d749db 2022-07-12 thomas }
2658 1a76625f 2018-10-22 stsp
2659 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2660 f2d749db 2022-07-12 thomas if (err) {
2661 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2662 f2d749db 2022-07-12 thomas goto done;
2663 f2d749db 2022-07-12 thomas }
2664 f2d749db 2022-07-12 thomas
2665 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2666 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2667 f2d749db 2022-07-12 thomas if (errcode) {
2668 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2669 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2670 f2d749db 2022-07-12 thomas goto done;
2671 f2d749db 2022-07-12 thomas }
2672 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2673 1a76625f 2018-10-22 stsp if (err) {
2674 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2675 f2d749db 2022-07-12 thomas goto done;
2676 1a76625f 2018-10-22 stsp err = NULL;
2677 1a76625f 2018-10-22 stsp done = 1;
2678 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2679 1a76625f 2018-10-22 stsp a->commits_needed--;
2680 1a76625f 2018-10-22 stsp
2681 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2682 3abe8080 2019-04-10 stsp if (errcode) {
2683 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2684 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2685 f2d749db 2022-07-12 thomas goto done;
2686 3abe8080 2019-04-10 stsp } else if (*a->quit)
2687 1a76625f 2018-10-22 stsp done = 1;
2688 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2689 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2690 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2691 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2692 1a76625f 2018-10-22 stsp }
2693 1a76625f 2018-10-22 stsp
2694 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2695 7c1452c1 2020-03-26 stsp if (errcode) {
2696 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2697 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2698 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2699 f2d749db 2022-07-12 thomas goto done;
2700 7c1452c1 2020-03-26 stsp }
2701 7c1452c1 2020-03-26 stsp
2702 1a76625f 2018-10-22 stsp if (done)
2703 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2704 7c1452c1 2020-03-26 stsp else {
2705 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2706 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2707 7c1452c1 2020-03-26 stsp &tog_mutex);
2708 f2d749db 2022-07-12 thomas if (errcode) {
2709 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2710 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2711 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2712 f2d749db 2022-07-12 thomas goto done;
2713 f2d749db 2022-07-12 thomas }
2714 21355643 2020-12-06 stsp if (*a->quit)
2715 21355643 2020-12-06 stsp done = 1;
2716 7c1452c1 2020-03-26 stsp }
2717 1a76625f 2018-10-22 stsp }
2718 1a76625f 2018-10-22 stsp }
2719 3abe8080 2019-04-10 stsp a->log_complete = 1;
2720 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2721 f2d749db 2022-07-12 thomas if (errcode)
2722 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2723 f2d749db 2022-07-12 thomas done:
2724 f2d749db 2022-07-12 thomas if (err) {
2725 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2726 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2727 f2d749db 2022-07-12 thomas }
2728 1a76625f 2018-10-22 stsp return (void *)err;
2729 1a76625f 2018-10-22 stsp }
2730 1a76625f 2018-10-22 stsp
2731 1a76625f 2018-10-22 stsp static const struct got_error *
2732 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2733 1a76625f 2018-10-22 stsp {
2734 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2735 1a76625f 2018-10-22 stsp int errcode;
2736 1a76625f 2018-10-22 stsp
2737 1a76625f 2018-10-22 stsp if (s->thread) {
2738 1a76625f 2018-10-22 stsp s->quit = 1;
2739 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2740 1a76625f 2018-10-22 stsp if (errcode)
2741 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2742 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2743 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2744 1a76625f 2018-10-22 stsp if (errcode)
2745 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2746 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2747 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2748 1a76625f 2018-10-22 stsp if (errcode)
2749 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2750 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2751 1a76625f 2018-10-22 stsp if (errcode)
2752 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2753 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2754 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2755 1a76625f 2018-10-22 stsp }
2756 1a76625f 2018-10-22 stsp
2757 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2758 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2759 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2760 1af5eddf 2022-06-23 thomas }
2761 1af5eddf 2022-06-23 thomas
2762 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2763 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2764 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2765 1af5eddf 2022-06-23 thomas if (err == NULL)
2766 1af5eddf 2022-06-23 thomas err = pack_err;
2767 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2768 1a76625f 2018-10-22 stsp }
2769 1a76625f 2018-10-22 stsp
2770 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2771 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2772 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2773 1a76625f 2018-10-22 stsp }
2774 1a76625f 2018-10-22 stsp
2775 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2776 9343a5fb 2018-06-23 stsp }
2777 9343a5fb 2018-06-23 stsp
2778 9343a5fb 2018-06-23 stsp static const struct got_error *
2779 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2780 1a76625f 2018-10-22 stsp {
2781 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2782 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2783 276b94a1 2020-11-13 naddy int errcode;
2784 1a76625f 2018-10-22 stsp
2785 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2786 276b94a1 2020-11-13 naddy
2787 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2788 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2789 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2790 276b94a1 2020-11-13 naddy
2791 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2792 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2793 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2794 276b94a1 2020-11-13 naddy
2795 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2796 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2797 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2798 1a76625f 2018-10-22 stsp free(s->start_id);
2799 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2800 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2801 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2802 1a76625f 2018-10-22 stsp return err;
2803 1a76625f 2018-10-22 stsp }
2804 1a76625f 2018-10-22 stsp
2805 1a76625f 2018-10-22 stsp static const struct got_error *
2806 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2807 60493ae3 2019-06-20 stsp {
2808 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2809 60493ae3 2019-06-20 stsp
2810 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2811 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2812 60493ae3 2019-06-20 stsp return NULL;
2813 60493ae3 2019-06-20 stsp }
2814 60493ae3 2019-06-20 stsp
2815 60493ae3 2019-06-20 stsp static const struct got_error *
2816 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2817 60493ae3 2019-06-20 stsp {
2818 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2819 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2820 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2821 60493ae3 2019-06-20 stsp
2822 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2823 f9686aa5 2020-03-27 stsp show_log_view(view);
2824 f9686aa5 2020-03-27 stsp update_panels();
2825 f9686aa5 2020-03-27 stsp doupdate();
2826 f9686aa5 2020-03-27 stsp
2827 96e2b566 2019-07-08 stsp if (s->search_entry) {
2828 13add988 2019-10-15 stsp int errcode, ch;
2829 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&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_unlock");
2833 13add988 2019-10-15 stsp ch = wgetch(view->window);
2834 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2835 13add988 2019-10-15 stsp if (errcode)
2836 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2837 13add988 2019-10-15 stsp "pthread_mutex_lock");
2838 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2839 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2840 678cbce5 2019-07-28 stsp return NULL;
2841 678cbce5 2019-07-28 stsp }
2842 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2843 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2844 96e2b566 2019-07-08 stsp else
2845 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2846 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2847 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2848 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2849 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2850 df5e841d 2022-06-23 thomas
2851 df5e841d 2022-06-23 thomas /*
2852 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2853 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2854 1f4d5162 2022-06-23 thomas * might have changed.
2855 df5e841d 2022-06-23 thomas */
2856 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2857 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2858 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2859 df5e841d 2022-06-23 thomas else
2860 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2861 b74feda6 2022-06-23 thomas } else {
2862 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2863 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2864 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2865 df5e841d 2022-06-23 thomas else
2866 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2867 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2868 b74feda6 2022-06-23 thomas }
2869 20be8d96 2019-06-21 stsp } else {
2870 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2871 20be8d96 2019-06-21 stsp }
2872 60493ae3 2019-06-20 stsp
2873 60493ae3 2019-06-20 stsp while (1) {
2874 13add988 2019-10-15 stsp int have_match = 0;
2875 13add988 2019-10-15 stsp
2876 60493ae3 2019-06-20 stsp if (entry == NULL) {
2877 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2878 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2879 f9967bca 2020-03-27 stsp view->search_next_done =
2880 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2881 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2882 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2883 f801134a 2019-06-25 stsp return NULL;
2884 60493ae3 2019-06-20 stsp }
2885 96e2b566 2019-07-08 stsp /*
2886 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2887 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2888 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2889 96e2b566 2019-07-08 stsp */
2890 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2891 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2892 60493ae3 2019-06-20 stsp }
2893 60493ae3 2019-06-20 stsp
2894 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2895 13add988 2019-10-15 stsp &view->regex);
2896 5943eee2 2019-08-13 stsp if (err)
2897 13add988 2019-10-15 stsp break;
2898 13add988 2019-10-15 stsp if (have_match) {
2899 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2900 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2901 60493ae3 2019-06-20 stsp break;
2902 60493ae3 2019-06-20 stsp }
2903 13add988 2019-10-15 stsp
2904 96e2b566 2019-07-08 stsp s->search_entry = entry;
2905 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2906 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2907 b1bf1435 2019-06-21 stsp else
2908 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2909 60493ae3 2019-06-20 stsp }
2910 60493ae3 2019-06-20 stsp
2911 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2912 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2913 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2914 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2915 60493ae3 2019-06-20 stsp if (err)
2916 60493ae3 2019-06-20 stsp return err;
2917 ead14cbe 2019-06-21 stsp cur++;
2918 ead14cbe 2019-06-21 stsp }
2919 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2920 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2921 60493ae3 2019-06-20 stsp if (err)
2922 60493ae3 2019-06-20 stsp return err;
2923 ead14cbe 2019-06-21 stsp cur--;
2924 60493ae3 2019-06-20 stsp }
2925 60493ae3 2019-06-20 stsp }
2926 60493ae3 2019-06-20 stsp
2927 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2928 96e2b566 2019-07-08 stsp
2929 60493ae3 2019-06-20 stsp return NULL;
2930 60493ae3 2019-06-20 stsp }
2931 60493ae3 2019-06-20 stsp
2932 60493ae3 2019-06-20 stsp static const struct got_error *
2933 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2934 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2935 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2936 80ddbec8 2018-04-29 stsp {
2937 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2938 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2939 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2940 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2941 1a76625f 2018-10-22 stsp int errcode;
2942 80ddbec8 2018-04-29 stsp
2943 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2944 f135c941 2020-02-20 stsp free(s->in_repo_path);
2945 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2946 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2947 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2948 f135c941 2020-02-20 stsp }
2949 ecb28ae0 2018-07-16 stsp
2950 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2951 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2952 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2953 78756c87 2020-11-24 stsp
2954 fb2756b9 2018-08-04 stsp s->repo = repo;
2955 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2956 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2957 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2958 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2959 9cd7cbd1 2020-12-07 stsp goto done;
2960 9cd7cbd1 2020-12-07 stsp }
2961 9cd7cbd1 2020-12-07 stsp }
2962 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2963 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2964 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2965 5036bf37 2018-09-24 stsp goto done;
2966 5036bf37 2018-09-24 stsp }
2967 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2968 e5a0f69f 2018-08-18 stsp
2969 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2970 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2971 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2972 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2973 11b20872 2019-11-08 stsp if (err)
2974 11b20872 2019-11-08 stsp goto done;
2975 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2976 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2977 11b20872 2019-11-08 stsp if (err) {
2978 11b20872 2019-11-08 stsp free_colors(&s->colors);
2979 11b20872 2019-11-08 stsp goto done;
2980 11b20872 2019-11-08 stsp }
2981 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2982 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2983 11b20872 2019-11-08 stsp if (err) {
2984 11b20872 2019-11-08 stsp free_colors(&s->colors);
2985 11b20872 2019-11-08 stsp goto done;
2986 11b20872 2019-11-08 stsp }
2987 11b20872 2019-11-08 stsp }
2988 11b20872 2019-11-08 stsp
2989 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2990 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2991 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2992 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2993 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2994 1a76625f 2018-10-22 stsp
2995 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2996 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2997 1af5eddf 2022-06-23 thomas if (err)
2998 1af5eddf 2022-06-23 thomas goto done;
2999 1af5eddf 2022-06-23 thomas }
3000 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3001 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3002 7cd52833 2022-06-23 thomas if (err)
3003 7cd52833 2022-06-23 thomas goto done;
3004 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3005 b672a97a 2020-01-27 stsp !s->log_branches);
3006 1a76625f 2018-10-22 stsp if (err)
3007 1a76625f 2018-10-22 stsp goto done;
3008 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3009 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3010 c5b78334 2020-01-12 stsp if (err)
3011 c5b78334 2020-01-12 stsp goto done;
3012 1a76625f 2018-10-22 stsp
3013 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3014 1a76625f 2018-10-22 stsp if (errcode) {
3015 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3016 1a76625f 2018-10-22 stsp goto done;
3017 1a76625f 2018-10-22 stsp }
3018 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3019 7c1452c1 2020-03-26 stsp if (errcode) {
3020 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3021 7c1452c1 2020-03-26 stsp goto done;
3022 7c1452c1 2020-03-26 stsp }
3023 1a76625f 2018-10-22 stsp
3024 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3025 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3026 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3027 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3028 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3029 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3030 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3031 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3032 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3033 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3034 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3035 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3036 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3037 ba4f502b 2018-08-04 stsp done:
3038 1a76625f 2018-10-22 stsp if (err)
3039 1a76625f 2018-10-22 stsp close_log_view(view);
3040 ba4f502b 2018-08-04 stsp return err;
3041 ba4f502b 2018-08-04 stsp }
3042 ba4f502b 2018-08-04 stsp
3043 e5a0f69f 2018-08-18 stsp static const struct got_error *
3044 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3045 ba4f502b 2018-08-04 stsp {
3046 f2f6d207 2020-11-24 stsp const struct got_error *err;
3047 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3048 ba4f502b 2018-08-04 stsp
3049 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3050 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3051 2b380cc8 2018-10-24 stsp &s->thread_args);
3052 2b380cc8 2018-10-24 stsp if (errcode)
3053 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3054 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3055 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3056 f2f6d207 2020-11-24 stsp if (err)
3057 f2f6d207 2020-11-24 stsp return err;
3058 f2f6d207 2020-11-24 stsp }
3059 2b380cc8 2018-10-24 stsp }
3060 2b380cc8 2018-10-24 stsp
3061 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3062 b31f89ff 2022-07-01 thomas }
3063 b31f89ff 2022-07-01 thomas
3064 b31f89ff 2022-07-01 thomas static void
3065 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3066 b31f89ff 2022-07-01 thomas {
3067 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3068 b31f89ff 2022-07-01 thomas
3069 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3070 b31f89ff 2022-07-01 thomas view->count = 0;
3071 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3072 b31f89ff 2022-07-01 thomas return;
3073 b31f89ff 2022-07-01 thomas
3074 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3075 b31f89ff 2022-07-01 thomas || home)
3076 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3077 b31f89ff 2022-07-01 thomas
3078 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3079 b31f89ff 2022-07-01 thomas --s->selected;
3080 b31f89ff 2022-07-01 thomas else
3081 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3082 b31f89ff 2022-07-01 thomas
3083 b31f89ff 2022-07-01 thomas select_commit(s);
3084 b31f89ff 2022-07-01 thomas return;
3085 b31f89ff 2022-07-01 thomas }
3086 b31f89ff 2022-07-01 thomas
3087 b31f89ff 2022-07-01 thomas static const struct got_error *
3088 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3089 b31f89ff 2022-07-01 thomas {
3090 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3091 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3092 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3093 b31f89ff 2022-07-01 thomas
3094 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3095 b31f89ff 2022-07-01 thomas if (first == NULL) {
3096 b31f89ff 2022-07-01 thomas view->count = 0;
3097 b31f89ff 2022-07-01 thomas return NULL;
3098 b31f89ff 2022-07-01 thomas }
3099 b31f89ff 2022-07-01 thomas
3100 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3101 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3102 b31f89ff 2022-07-01 thomas return NULL;
3103 b31f89ff 2022-07-01 thomas
3104 b31f89ff 2022-07-01 thomas if (!page) {
3105 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3106 b31f89ff 2022-07-01 thomas
3107 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3108 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3109 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3110 b31f89ff 2022-07-01 thomas ++s->selected;
3111 b31f89ff 2022-07-01 thomas else
3112 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3113 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3114 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3115 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3116 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3117 b31f89ff 2022-07-01 thomas else
3118 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3119 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3120 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3121 b31f89ff 2022-07-01 thomas } else {
3122 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3123 b31f89ff 2022-07-01 thomas if (err)
3124 b31f89ff 2022-07-01 thomas return err;
3125 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3126 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3127 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3128 b31f89ff 2022-07-01 thomas }
3129 b31f89ff 2022-07-01 thomas }
3130 b31f89ff 2022-07-01 thomas if (err)
3131 b31f89ff 2022-07-01 thomas return err;
3132 b31f89ff 2022-07-01 thomas
3133 a5d43cac 2022-07-01 thomas /*
3134 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3135 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3136 a5d43cac 2022-07-01 thomas */
3137 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3138 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3139 a5d43cac 2022-07-01 thomas
3140 b31f89ff 2022-07-01 thomas select_commit(s);
3141 b31f89ff 2022-07-01 thomas
3142 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3143 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3144 b31f89ff 2022-07-01 thomas view->count = 0;
3145 b31f89ff 2022-07-01 thomas
3146 b31f89ff 2022-07-01 thomas return NULL;
3147 e5a0f69f 2018-08-18 stsp }
3148 04cc582a 2018-08-01 stsp
3149 a5d43cac 2022-07-01 thomas static void
3150 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3151 a5d43cac 2022-07-01 thomas {
3152 24415785 2022-07-03 thomas *x = 0;
3153 24415785 2022-07-03 thomas *y = 0;
3154 24415785 2022-07-03 thomas
3155 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3156 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3157 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3158 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3159 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3160 53d2bdd3 2022-07-10 thomas else
3161 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3162 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3163 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3164 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3165 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3166 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3167 53d2bdd3 2022-07-10 thomas else
3168 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3169 53d2bdd3 2022-07-10 thomas }
3170 a5d43cac 2022-07-01 thomas }
3171 a5d43cac 2022-07-01 thomas
3172 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3173 e5a0f69f 2018-08-18 stsp static const struct got_error *
3174 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3175 a5d43cac 2022-07-01 thomas {
3176 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3177 a5d43cac 2022-07-01 thomas
3178 a5d43cac 2022-07-01 thomas view->nlines = y;
3179 64486692 2022-07-07 thomas view->ncols = COLS;
3180 a5d43cac 2022-07-01 thomas err = view_resize(view);
3181 a5d43cac 2022-07-01 thomas if (err)
3182 a5d43cac 2022-07-01 thomas return err;
3183 a5d43cac 2022-07-01 thomas
3184 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3185 a5d43cac 2022-07-01 thomas
3186 a5d43cac 2022-07-01 thomas return err;
3187 a5d43cac 2022-07-01 thomas }
3188 a5d43cac 2022-07-01 thomas
3189 a5d43cac 2022-07-01 thomas static const struct got_error *
3190 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3191 e5a0f69f 2018-08-18 stsp {
3192 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3193 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3194 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3195 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3196 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3197 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3198 80ddbec8 2018-04-29 stsp
3199 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3200 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3201 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3202 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3203 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3204 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3205 fb280deb 2021-08-30 stsp }
3206 b31f89ff 2022-07-01 thomas return err;
3207 528dedf3 2021-08-30 stsp }
3208 068ab281 2022-07-01 thomas
3209 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3210 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3211 068ab281 2022-07-01 thomas --eos; /* border */
3212 068ab281 2022-07-01 thomas
3213 528dedf3 2021-08-30 stsp switch (ch) {
3214 1e37a5c2 2019-05-12 jcs case 'q':
3215 1e37a5c2 2019-05-12 jcs s->quit = 1;
3216 05171be4 2022-06-23 thomas break;
3217 05171be4 2022-06-23 thomas case '0':
3218 05171be4 2022-06-23 thomas view->x = 0;
3219 05171be4 2022-06-23 thomas break;
3220 05171be4 2022-06-23 thomas case '$':
3221 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3222 07b0611c 2022-06-23 thomas view->count = 0;
3223 05171be4 2022-06-23 thomas break;
3224 05171be4 2022-06-23 thomas case KEY_RIGHT:
3225 05171be4 2022-06-23 thomas case 'l':
3226 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3227 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3228 07b0611c 2022-06-23 thomas else
3229 07b0611c 2022-06-23 thomas view->count = 0;
3230 1e37a5c2 2019-05-12 jcs break;
3231 05171be4 2022-06-23 thomas case KEY_LEFT:
3232 05171be4 2022-06-23 thomas case 'h':
3233 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3234 07b0611c 2022-06-23 thomas if (view->x <= 0)
3235 07b0611c 2022-06-23 thomas view->count = 0;
3236 05171be4 2022-06-23 thomas break;
3237 1e37a5c2 2019-05-12 jcs case 'k':
3238 1e37a5c2 2019-05-12 jcs case KEY_UP:
3239 1e37a5c2 2019-05-12 jcs case '<':
3240 1e37a5c2 2019-05-12 jcs case ',':
3241 f7140bf5 2021-10-17 thomas case CTRL('p'):
3242 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3243 912a3f79 2021-08-30 j break;
3244 912a3f79 2021-08-30 j case 'g':
3245 912a3f79 2021-08-30 j case KEY_HOME:
3246 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3247 07b0611c 2022-06-23 thomas view->count = 0;
3248 1e37a5c2 2019-05-12 jcs break;
3249 70f17a53 2022-06-13 thomas case CTRL('u'):
3250 23427b14 2022-06-23 thomas case 'u':
3251 70f17a53 2022-06-13 thomas nscroll /= 2;
3252 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3253 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3254 a4292ac5 2019-05-12 jcs case CTRL('b'):
3255 1c5e5faa 2022-06-23 thomas case 'b':
3256 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3257 1e37a5c2 2019-05-12 jcs break;
3258 1e37a5c2 2019-05-12 jcs case 'j':
3259 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3260 1e37a5c2 2019-05-12 jcs case '>':
3261 1e37a5c2 2019-05-12 jcs case '.':
3262 f7140bf5 2021-10-17 thomas case CTRL('n'):
3263 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3264 912a3f79 2021-08-30 j break;
3265 912a3f79 2021-08-30 j case 'G':
3266 912a3f79 2021-08-30 j case KEY_END: {
3267 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3268 912a3f79 2021-08-30 j * traverse them all. */
3269 07b0611c 2022-06-23 thomas view->count = 0;
3270 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3271 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3272 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3273 80ddbec8 2018-04-29 stsp }
3274 912a3f79 2021-08-30 j
3275 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3276 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3277 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3278 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3279 f3bc9f1d 2021-09-05 naddy break;
3280 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3281 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3282 f3bc9f1d 2021-09-05 naddy }
3283 f3bc9f1d 2021-09-05 naddy if (n > 0)
3284 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3285 2b779855 2020-12-05 naddy select_commit(s);
3286 1e37a5c2 2019-05-12 jcs break;
3287 912a3f79 2021-08-30 j }
3288 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3289 23427b14 2022-06-23 thomas case 'd':
3290 70f17a53 2022-06-13 thomas nscroll /= 2;
3291 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3292 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3293 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3294 4c2d69cb 2022-06-23 thomas case 'f':
3295 b31f89ff 2022-07-01 thomas case ' ':
3296 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3297 1e37a5c2 2019-05-12 jcs break;
3298 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3299 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3300 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3301 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3302 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3303 2b779855 2020-12-05 naddy select_commit(s);
3304 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3305 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3306 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3307 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3308 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3309 0bf7f153 2020-12-02 naddy }
3310 1e37a5c2 2019-05-12 jcs break;
3311 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3312 444d5325 2022-07-03 thomas case '\r':
3313 07b0611c 2022-06-23 thomas view->count = 0;
3314 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3315 e5a0f69f 2018-08-18 stsp break;
3316 a5d43cac 2022-07-01 thomas
3317 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3318 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3319 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3320 a5d43cac 2022-07-01 thomas
3321 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3322 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3323 78756c87 2020-11-24 stsp view, s->repo);
3324 1e37a5c2 2019-05-12 jcs if (err)
3325 1e37a5c2 2019-05-12 jcs break;
3326 a5d43cac 2022-07-01 thomas
3327 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3328 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3329 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3330 a5d43cac 2022-07-01 thomas if (err)
3331 a5d43cac 2022-07-01 thomas break;
3332 a5d43cac 2022-07-01 thomas }
3333 a5d43cac 2022-07-01 thomas
3334 e78dc838 2020-12-04 stsp view->focussed = 0;
3335 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3336 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3337 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3338 a5d43cac 2022-07-01 thomas
3339 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3340 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3341 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3342 f7013a22 2018-10-24 stsp if (err)
3343 1e37a5c2 2019-05-12 jcs return err;
3344 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3345 40236d76 2022-06-23 thomas if (err)
3346 40236d76 2022-06-23 thomas return err;
3347 e78dc838 2020-12-04 stsp view->focus_child = 1;
3348 1e37a5c2 2019-05-12 jcs } else
3349 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3350 1e37a5c2 2019-05-12 jcs break;
3351 1e37a5c2 2019-05-12 jcs case 't':
3352 07b0611c 2022-06-23 thomas view->count = 0;
3353 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3354 5036bf37 2018-09-24 stsp break;
3355 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3356 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3357 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3358 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3359 4e97c21c 2020-12-06 stsp s->repo);
3360 1e37a5c2 2019-05-12 jcs if (err)
3361 e5a0f69f 2018-08-18 stsp break;
3362 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3363 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3364 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3365 444d5325 2022-07-03 thomas if (err)
3366 444d5325 2022-07-03 thomas break;
3367 444d5325 2022-07-03 thomas }
3368 e78dc838 2020-12-04 stsp view->focussed = 0;
3369 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3370 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3371 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3372 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3373 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3374 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3375 1e37a5c2 2019-05-12 jcs if (err)
3376 1e37a5c2 2019-05-12 jcs return err;
3377 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3378 40236d76 2022-06-23 thomas if (err)
3379 40236d76 2022-06-23 thomas return err;
3380 e78dc838 2020-12-04 stsp view->focus_child = 1;
3381 1e37a5c2 2019-05-12 jcs } else
3382 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3383 1e37a5c2 2019-05-12 jcs break;
3384 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3385 21355643 2020-12-06 stsp case CTRL('l'):
3386 21355643 2020-12-06 stsp case 'B':
3387 07b0611c 2022-06-23 thomas view->count = 0;
3388 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3389 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3390 1e37a5c2 2019-05-12 jcs break;
3391 21355643 2020-12-06 stsp err = stop_log_thread(s);
3392 74cfe85e 2020-10-20 stsp if (err)
3393 74cfe85e 2020-10-20 stsp return err;
3394 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3395 21355643 2020-12-06 stsp char *parent_path;
3396 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3397 21355643 2020-12-06 stsp if (err)
3398 21355643 2020-12-06 stsp return err;
3399 21355643 2020-12-06 stsp free(s->in_repo_path);
3400 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3401 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3402 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3403 21355643 2020-12-06 stsp struct got_object_id *start_id;
3404 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3405 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3406 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3407 21355643 2020-12-06 stsp if (err)
3408 21355643 2020-12-06 stsp return err;
3409 21355643 2020-12-06 stsp free(s->start_id);
3410 21355643 2020-12-06 stsp s->start_id = start_id;
3411 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3412 21355643 2020-12-06 stsp } else /* 'B' */
3413 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3414 21355643 2020-12-06 stsp
3415 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3416 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3417 bf7e79b3 2022-06-23 thomas if (err)
3418 bf7e79b3 2022-06-23 thomas return err;
3419 bf7e79b3 2022-06-23 thomas }
3420 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3421 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3422 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3423 74cfe85e 2020-10-20 stsp if (err)
3424 21355643 2020-12-06 stsp return err;
3425 51a10b52 2020-12-26 stsp tog_free_refs();
3426 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3427 ca51c541 2020-12-07 stsp if (err)
3428 ca51c541 2020-12-07 stsp return err;
3429 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3430 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3431 d01904d4 2019-06-25 stsp if (err)
3432 d01904d4 2019-06-25 stsp return err;
3433 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3434 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3435 b672a97a 2020-01-27 stsp if (err)
3436 b672a97a 2020-01-27 stsp return err;
3437 21355643 2020-12-06 stsp free_commits(&s->commits);
3438 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3439 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3440 21355643 2020-12-06 stsp s->selected_entry = NULL;
3441 21355643 2020-12-06 stsp s->selected = 0;
3442 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3443 21355643 2020-12-06 stsp s->quit = 0;
3444 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3445 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3446 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3447 d01904d4 2019-06-25 stsp break;
3448 6458efa5 2020-11-24 stsp case 'r':
3449 07b0611c 2022-06-23 thomas view->count = 0;
3450 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3451 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3452 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3453 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3454 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3455 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3456 6458efa5 2020-11-24 stsp if (err) {
3457 6458efa5 2020-11-24 stsp view_close(ref_view);
3458 6458efa5 2020-11-24 stsp return err;
3459 6458efa5 2020-11-24 stsp }
3460 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3461 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3462 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3463 444d5325 2022-07-03 thomas if (err)
3464 444d5325 2022-07-03 thomas break;
3465 444d5325 2022-07-03 thomas }
3466 e78dc838 2020-12-04 stsp view->focussed = 0;
3467 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3468 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3469 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3470 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3471 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3472 6458efa5 2020-11-24 stsp err = view_close_child(view);
3473 6458efa5 2020-11-24 stsp if (err)
3474 6458efa5 2020-11-24 stsp return err;
3475 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3476 40236d76 2022-06-23 thomas if (err)
3477 40236d76 2022-06-23 thomas return err;
3478 e78dc838 2020-12-04 stsp view->focus_child = 1;
3479 6458efa5 2020-11-24 stsp } else
3480 6458efa5 2020-11-24 stsp *new_view = ref_view;
3481 6458efa5 2020-11-24 stsp break;
3482 1e37a5c2 2019-05-12 jcs default:
3483 07b0611c 2022-06-23 thomas view->count = 0;
3484 1e37a5c2 2019-05-12 jcs break;
3485 899d86c2 2018-05-10 stsp }
3486 e5a0f69f 2018-08-18 stsp
3487 80ddbec8 2018-04-29 stsp return err;
3488 80ddbec8 2018-04-29 stsp }
3489 80ddbec8 2018-04-29 stsp
3490 4ed7e80c 2018-05-20 stsp static const struct got_error *
3491 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3492 c2db6724 2019-01-04 stsp {
3493 c2db6724 2019-01-04 stsp const struct got_error *error;
3494 c2db6724 2019-01-04 stsp
3495 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3496 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3497 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3498 37c06ea4 2019-07-15 stsp #endif
3499 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3500 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3501 c2db6724 2019-01-04 stsp
3502 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3503 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3504 c2db6724 2019-01-04 stsp
3505 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3506 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3507 c2db6724 2019-01-04 stsp
3508 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3509 c2db6724 2019-01-04 stsp if (error != NULL)
3510 c2db6724 2019-01-04 stsp return error;
3511 c2db6724 2019-01-04 stsp
3512 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3513 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3514 c2db6724 2019-01-04 stsp
3515 c2db6724 2019-01-04 stsp return NULL;
3516 c2db6724 2019-01-04 stsp }
3517 c2db6724 2019-01-04 stsp
3518 a915003a 2019-02-05 stsp static void
3519 a915003a 2019-02-05 stsp init_curses(void)
3520 a915003a 2019-02-05 stsp {
3521 296152d1 2022-05-31 thomas /*
3522 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3523 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3524 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3525 296152d1 2022-05-31 thomas */
3526 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3527 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3528 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3529 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3530 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3531 296152d1 2022-05-31 thomas
3532 a915003a 2019-02-05 stsp initscr();
3533 a915003a 2019-02-05 stsp cbreak();
3534 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3535 a915003a 2019-02-05 stsp noecho();
3536 a915003a 2019-02-05 stsp nonl();
3537 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3538 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3539 a915003a 2019-02-05 stsp curs_set(0);
3540 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3541 6d17833f 2019-11-08 stsp start_color();
3542 6d17833f 2019-11-08 stsp use_default_colors();
3543 6d17833f 2019-11-08 stsp }
3544 a915003a 2019-02-05 stsp }
3545 a915003a 2019-02-05 stsp
3546 c2db6724 2019-01-04 stsp static const struct got_error *
3547 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3548 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3549 f135c941 2020-02-20 stsp {
3550 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3551 f135c941 2020-02-20 stsp
3552 f135c941 2020-02-20 stsp if (argc == 0) {
3553 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3554 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3555 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3556 f135c941 2020-02-20 stsp return NULL;
3557 f135c941 2020-02-20 stsp }
3558 f135c941 2020-02-20 stsp
3559 f135c941 2020-02-20 stsp if (worktree) {
3560 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3561 bfd61697 2020-11-14 stsp char *p;
3562 f135c941 2020-02-20 stsp
3563 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3564 f135c941 2020-02-20 stsp if (err)
3565 f135c941 2020-02-20 stsp return err;
3566 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3567 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3568 bfd61697 2020-11-14 stsp p) == -1) {
3569 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3570 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3571 f135c941 2020-02-20 stsp }
3572 f135c941 2020-02-20 stsp free(p);
3573 f135c941 2020-02-20 stsp } else
3574 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3575 f135c941 2020-02-20 stsp
3576 f135c941 2020-02-20 stsp return err;
3577 f135c941 2020-02-20 stsp }
3578 f135c941 2020-02-20 stsp
3579 f135c941 2020-02-20 stsp static const struct got_error *
3580 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3581 9f7d7167 2018-04-29 stsp {
3582 80ddbec8 2018-04-29 stsp const struct got_error *error;
3583 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3584 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3585 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3586 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3587 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3588 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3589 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3590 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3591 04cc582a 2018-08-01 stsp struct tog_view *view;
3592 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3593 80ddbec8 2018-04-29 stsp
3594 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3595 80ddbec8 2018-04-29 stsp switch (ch) {
3596 b672a97a 2020-01-27 stsp case 'b':
3597 b672a97a 2020-01-27 stsp log_branches = 1;
3598 b672a97a 2020-01-27 stsp break;
3599 80ddbec8 2018-04-29 stsp case 'c':
3600 80ddbec8 2018-04-29 stsp start_commit = optarg;
3601 80ddbec8 2018-04-29 stsp break;
3602 ecb28ae0 2018-07-16 stsp case 'r':
3603 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3604 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3605 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3606 9ba1d308 2019-10-21 stsp optarg);
3607 ecb28ae0 2018-07-16 stsp break;
3608 80ddbec8 2018-04-29 stsp default:
3609 17020d27 2019-03-07 stsp usage_log();
3610 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3611 80ddbec8 2018-04-29 stsp }
3612 80ddbec8 2018-04-29 stsp }
3613 80ddbec8 2018-04-29 stsp
3614 80ddbec8 2018-04-29 stsp argc -= optind;
3615 80ddbec8 2018-04-29 stsp argv += optind;
3616 80ddbec8 2018-04-29 stsp
3617 f135c941 2020-02-20 stsp if (argc > 1)
3618 f135c941 2020-02-20 stsp usage_log();
3619 963f97a1 2019-03-18 stsp
3620 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3621 7cd52833 2022-06-23 thomas if (error != NULL)
3622 7cd52833 2022-06-23 thomas goto done;
3623 7cd52833 2022-06-23 thomas
3624 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3625 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3626 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3627 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3628 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3629 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3630 c156c7a4 2020-12-18 stsp goto done;
3631 a1fbf39a 2019-08-11 stsp if (worktree)
3632 6962eb72 2020-02-20 stsp repo_path =
3633 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3634 a1fbf39a 2019-08-11 stsp else
3635 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3636 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3637 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3638 c156c7a4 2020-12-18 stsp goto done;
3639 c156c7a4 2020-12-18 stsp }
3640 ecb28ae0 2018-07-16 stsp }
3641 ecb28ae0 2018-07-16 stsp
3642 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3643 80ddbec8 2018-04-29 stsp if (error != NULL)
3644 ecb28ae0 2018-07-16 stsp goto done;
3645 80ddbec8 2018-04-29 stsp
3646 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3647 f135c941 2020-02-20 stsp repo, worktree);
3648 f135c941 2020-02-20 stsp if (error)
3649 f135c941 2020-02-20 stsp goto done;
3650 f135c941 2020-02-20 stsp
3651 f135c941 2020-02-20 stsp init_curses();
3652 f135c941 2020-02-20 stsp
3653 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3654 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3655 c02c541e 2019-03-29 stsp if (error)
3656 c02c541e 2019-03-29 stsp goto done;
3657 c02c541e 2019-03-29 stsp
3658 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3659 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3660 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3661 87670572 2020-12-26 naddy if (error)
3662 87670572 2020-12-26 naddy goto done;
3663 87670572 2020-12-26 naddy }
3664 51a10b52 2020-12-26 stsp
3665 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3666 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3667 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3668 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3669 d8f38dc4 2020-12-05 stsp if (error)
3670 d8f38dc4 2020-12-05 stsp goto done;
3671 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3672 d8f38dc4 2020-12-05 stsp } else {
3673 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3674 d8f38dc4 2020-12-05 stsp if (error == NULL)
3675 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3676 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3677 d8f38dc4 2020-12-05 stsp goto done;
3678 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3679 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3680 d8f38dc4 2020-12-05 stsp if (error)
3681 d8f38dc4 2020-12-05 stsp goto done;
3682 d8f38dc4 2020-12-05 stsp }
3683 8b473291 2019-02-21 stsp
3684 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3685 04cc582a 2018-08-01 stsp if (view == NULL) {
3686 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3687 04cc582a 2018-08-01 stsp goto done;
3688 04cc582a 2018-08-01 stsp }
3689 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3690 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3691 ba4f502b 2018-08-04 stsp if (error)
3692 ba4f502b 2018-08-04 stsp goto done;
3693 2fc00ff4 2019-08-31 stsp if (worktree) {
3694 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3695 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3696 2fc00ff4 2019-08-31 stsp worktree = NULL;
3697 2fc00ff4 2019-08-31 stsp }
3698 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3699 ecb28ae0 2018-07-16 stsp done:
3700 f135c941 2020-02-20 stsp free(in_repo_path);
3701 ecb28ae0 2018-07-16 stsp free(repo_path);
3702 ecb28ae0 2018-07-16 stsp free(cwd);
3703 899d86c2 2018-05-10 stsp free(start_id);
3704 d8f38dc4 2020-12-05 stsp free(label);
3705 d8f38dc4 2020-12-05 stsp if (ref)
3706 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3707 1d0f4054 2021-06-17 stsp if (repo) {
3708 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3709 1d0f4054 2021-06-17 stsp if (error == NULL)
3710 1d0f4054 2021-06-17 stsp error = close_err;
3711 1d0f4054 2021-06-17 stsp }
3712 ec142235 2019-03-07 stsp if (worktree)
3713 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3714 7cd52833 2022-06-23 thomas if (pack_fds) {
3715 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3716 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3717 7cd52833 2022-06-23 thomas if (error == NULL)
3718 7cd52833 2022-06-23 thomas error = pack_err;
3719 7cd52833 2022-06-23 thomas }
3720 51a10b52 2020-12-26 stsp tog_free_refs();
3721 80ddbec8 2018-04-29 stsp return error;
3722 9f7d7167 2018-04-29 stsp }
3723 9f7d7167 2018-04-29 stsp
3724 4ed7e80c 2018-05-20 stsp __dead static void
3725 9f7d7167 2018-04-29 stsp usage_diff(void)
3726 9f7d7167 2018-04-29 stsp {
3727 80ddbec8 2018-04-29 stsp endwin();
3728 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3729 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3730 9f7d7167 2018-04-29 stsp exit(1);
3731 b304db33 2018-05-20 stsp }
3732 b304db33 2018-05-20 stsp
3733 6d17833f 2019-11-08 stsp static int
3734 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3735 41605754 2020-11-12 stsp regmatch_t *regmatch)
3736 6d17833f 2019-11-08 stsp {
3737 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3738 6d17833f 2019-11-08 stsp }
3739 6d17833f 2019-11-08 stsp
3740 ef20f542 2022-06-26 thomas static struct tog_color *
3741 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3742 6d17833f 2019-11-08 stsp {
3743 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3744 6d17833f 2019-11-08 stsp
3745 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3746 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3747 6d17833f 2019-11-08 stsp return tc;
3748 6d17833f 2019-11-08 stsp }
3749 6d17833f 2019-11-08 stsp
3750 6d17833f 2019-11-08 stsp return NULL;
3751 6d17833f 2019-11-08 stsp }
3752 6d17833f 2019-11-08 stsp
3753 4ed7e80c 2018-05-20 stsp static const struct got_error *
3754 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3755 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3756 41605754 2020-11-12 stsp {
3757 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3758 666f7b10 2022-06-23 thomas char *exstr = NULL;
3759 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3760 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3761 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3762 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3763 41605754 2020-11-12 stsp
3764 41605754 2020-11-12 stsp *wtotal = 0;
3765 cbea3800 2022-06-23 thomas
3766 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3767 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3768 41605754 2020-11-12 stsp
3769 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3770 666f7b10 2022-06-23 thomas if (err)
3771 666f7b10 2022-06-23 thomas return err;
3772 666f7b10 2022-06-23 thomas
3773 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3774 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3775 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3776 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3777 666f7b10 2022-06-23 thomas goto done;
3778 666f7b10 2022-06-23 thomas }
3779 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3780 cbea3800 2022-06-23 thomas if (seg1 == 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 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3785 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3786 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3787 cbea3800 2022-06-23 thomas goto done;
3788 cbea3800 2022-06-23 thomas }
3789 05171be4 2022-06-23 thomas
3790 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3791 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3792 cbea3800 2022-06-23 thomas col_tab_align, 1);
3793 cbea3800 2022-06-23 thomas if (err)
3794 cbea3800 2022-06-23 thomas goto done;
3795 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3796 05171be4 2022-06-23 thomas if (n) {
3797 cbea3800 2022-06-23 thomas free(wline);
3798 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3799 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3800 cbea3800 2022-06-23 thomas if (err)
3801 cbea3800 2022-06-23 thomas goto done;
3802 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3803 cbea3800 2022-06-23 thomas wlimit -= width;
3804 cbea3800 2022-06-23 thomas *wtotal += width;
3805 41605754 2020-11-12 stsp }
3806 41605754 2020-11-12 stsp
3807 41605754 2020-11-12 stsp if (wlimit > 0) {
3808 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3809 cbea3800 2022-06-23 thomas size_t wlen;
3810 cbea3800 2022-06-23 thomas
3811 cbea3800 2022-06-23 thomas free(wline);
3812 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3813 cbea3800 2022-06-23 thomas col_tab_align, 1);
3814 cbea3800 2022-06-23 thomas if (err)
3815 cbea3800 2022-06-23 thomas goto done;
3816 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3817 cbea3800 2022-06-23 thomas while (i < wlen) {
3818 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3819 cbea3800 2022-06-23 thomas if (width == -1) {
3820 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3821 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3822 cbea3800 2022-06-23 thomas goto done;
3823 cbea3800 2022-06-23 thomas }
3824 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3825 cbea3800 2022-06-23 thomas break;
3826 1c5e5faa 2022-06-23 thomas w += width;
3827 cbea3800 2022-06-23 thomas i++;
3828 41605754 2020-11-12 stsp }
3829 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3830 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3831 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3832 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3833 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3834 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3835 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3836 41605754 2020-11-12 stsp }
3837 41605754 2020-11-12 stsp }
3838 41605754 2020-11-12 stsp
3839 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3840 cbea3800 2022-06-23 thomas free(wline);
3841 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3842 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3843 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3844 cbea3800 2022-06-23 thomas col_tab_align, 1);
3845 cbea3800 2022-06-23 thomas if (err)
3846 cbea3800 2022-06-23 thomas goto done;
3847 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3848 cbea3800 2022-06-23 thomas } else {
3849 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3850 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3851 cbea3800 2022-06-23 thomas if (err)
3852 cbea3800 2022-06-23 thomas goto done;
3853 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3854 cbea3800 2022-06-23 thomas }
3855 cbea3800 2022-06-23 thomas *wtotal += width2;
3856 41605754 2020-11-12 stsp }
3857 cbea3800 2022-06-23 thomas done:
3858 05171be4 2022-06-23 thomas free(wline);
3859 666f7b10 2022-06-23 thomas free(exstr);
3860 cbea3800 2022-06-23 thomas free(seg0);
3861 cbea3800 2022-06-23 thomas free(seg1);
3862 cbea3800 2022-06-23 thomas free(seg2);
3863 cbea3800 2022-06-23 thomas return err;
3864 41605754 2020-11-12 stsp }
3865 41605754 2020-11-12 stsp
3866 41605754 2020-11-12 stsp static const struct got_error *
3867 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3868 26ed57b2 2018-05-19 stsp {
3869 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3870 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3871 61e69b96 2018-05-20 stsp const struct got_error *err;
3872 fe621944 2020-11-10 stsp int nprinted = 0;
3873 b304db33 2018-05-20 stsp char *line;
3874 826082fe 2020-12-10 stsp size_t linesize = 0;
3875 826082fe 2020-12-10 stsp ssize_t linelen;
3876 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3877 61e69b96 2018-05-20 stsp wchar_t *wline;
3878 e0b650dd 2018-05-20 stsp int width;
3879 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3880 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3881 fe621944 2020-11-10 stsp off_t line_offset;
3882 26ed57b2 2018-05-19 stsp
3883 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3884 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3885 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3886 fe621944 2020-11-10 stsp
3887 f7d12f7e 2018-08-01 stsp werase(view->window);
3888 a3404814 2018-09-02 stsp
3889 a3404814 2018-09-02 stsp if (header) {
3890 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3891 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3892 135a2da0 2020-11-11 stsp header) == -1)
3893 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3894 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3895 f91a2b48 2022-06-23 thomas 0, 0);
3896 135a2da0 2020-11-11 stsp free(line);
3897 135a2da0 2020-11-11 stsp if (err)
3898 a3404814 2018-09-02 stsp return err;
3899 a3404814 2018-09-02 stsp
3900 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3901 a3404814 2018-09-02 stsp wstandout(view->window);
3902 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3903 e54cc94a 2020-11-11 stsp free(wline);
3904 e54cc94a 2020-11-11 stsp wline = NULL;
3905 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3906 a3404814 2018-09-02 stsp wstandend(view->window);
3907 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3908 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3909 26ed57b2 2018-05-19 stsp
3910 a3404814 2018-09-02 stsp if (max_lines <= 1)
3911 a3404814 2018-09-02 stsp return NULL;
3912 a3404814 2018-09-02 stsp max_lines--;
3913 a3404814 2018-09-02 stsp }
3914 a3404814 2018-09-02 stsp
3915 89f1a395 2020-12-01 naddy s->eof = 0;
3916 05171be4 2022-06-23 thomas view->maxx = 0;
3917 826082fe 2020-12-10 stsp line = NULL;
3918 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3919 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3920 826082fe 2020-12-10 stsp if (linelen == -1) {
3921 826082fe 2020-12-10 stsp if (feof(s->f)) {
3922 826082fe 2020-12-10 stsp s->eof = 1;
3923 826082fe 2020-12-10 stsp break;
3924 826082fe 2020-12-10 stsp }
3925 826082fe 2020-12-10 stsp free(line);
3926 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3927 61e69b96 2018-05-20 stsp }
3928 05171be4 2022-06-23 thomas
3929 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3930 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3931 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3932 cbea3800 2022-06-23 thomas if (err) {
3933 cbea3800 2022-06-23 thomas free(line);
3934 cbea3800 2022-06-23 thomas return err;
3935 cbea3800 2022-06-23 thomas }
3936 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3937 cbea3800 2022-06-23 thomas free(wline);
3938 cbea3800 2022-06-23 thomas wline = NULL;
3939 cbea3800 2022-06-23 thomas
3940 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3941 f26dddb7 2019-11-08 stsp if (tc)
3942 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3943 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3944 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3945 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3946 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3947 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3948 41605754 2020-11-12 stsp if (err) {
3949 41605754 2020-11-12 stsp free(line);
3950 41605754 2020-11-12 stsp return err;
3951 41605754 2020-11-12 stsp }
3952 41605754 2020-11-12 stsp } else {
3953 f1c0ec19 2022-06-23 thomas int skip;
3954 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3955 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3956 f1c0ec19 2022-06-23 thomas if (err) {
3957 f1c0ec19 2022-06-23 thomas free(line);
3958 f1c0ec19 2022-06-23 thomas return err;
3959 f1c0ec19 2022-06-23 thomas }
3960 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3961 f1c0ec19 2022-06-23 thomas free(wline);
3962 41605754 2020-11-12 stsp wline = NULL;
3963 41605754 2020-11-12 stsp }
3964 f26dddb7 2019-11-08 stsp if (tc)
3965 6d17833f 2019-11-08 stsp wattr_off(view->window,
3966 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3967 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3968 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3969 fe621944 2020-11-10 stsp nprinted++;
3970 826082fe 2020-12-10 stsp }
3971 826082fe 2020-12-10 stsp free(line);
3972 fe621944 2020-11-10 stsp if (nprinted >= 1)
3973 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3974 89f1a395 2020-12-01 naddy (nprinted - 1);
3975 fe621944 2020-11-10 stsp else
3976 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3977 26ed57b2 2018-05-19 stsp
3978 a5d43cac 2022-07-01 thomas view_border(view);
3979 c3e9aa98 2019-05-13 jcs
3980 89f1a395 2020-12-01 naddy if (s->eof) {
3981 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3982 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3983 c3e9aa98 2019-05-13 jcs nprinted++;
3984 c3e9aa98 2019-05-13 jcs }
3985 c3e9aa98 2019-05-13 jcs
3986 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3987 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3988 c3e9aa98 2019-05-13 jcs if (err) {
3989 c3e9aa98 2019-05-13 jcs return err;
3990 c3e9aa98 2019-05-13 jcs }
3991 26ed57b2 2018-05-19 stsp
3992 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3993 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3994 e54cc94a 2020-11-11 stsp free(wline);
3995 e54cc94a 2020-11-11 stsp wline = NULL;
3996 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3997 c3e9aa98 2019-05-13 jcs }
3998 c3e9aa98 2019-05-13 jcs
3999 26ed57b2 2018-05-19 stsp return NULL;
4000 abd2672a 2018-12-23 stsp }
4001 abd2672a 2018-12-23 stsp
4002 abd2672a 2018-12-23 stsp static char *
4003 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4004 abd2672a 2018-12-23 stsp {
4005 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4006 09867e48 2019-08-13 stsp char *p, *s;
4007 09867e48 2019-08-13 stsp
4008 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4009 09867e48 2019-08-13 stsp if (tm == NULL)
4010 09867e48 2019-08-13 stsp return NULL;
4011 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4012 09867e48 2019-08-13 stsp if (s == NULL)
4013 09867e48 2019-08-13 stsp return NULL;
4014 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4015 abd2672a 2018-12-23 stsp if (p)
4016 abd2672a 2018-12-23 stsp *p = '\0';
4017 abd2672a 2018-12-23 stsp return s;
4018 9f7d7167 2018-04-29 stsp }
4019 9f7d7167 2018-04-29 stsp
4020 4ed7e80c 2018-05-20 stsp static const struct got_error *
4021 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4022 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4023 0208f208 2020-05-05 stsp {
4024 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4025 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4026 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4027 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4028 0208f208 2020-05-05 stsp
4029 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4030 0208f208 2020-05-05 stsp if (qid != NULL) {
4031 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4032 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4033 ec242592 2022-04-22 thomas &qid->id);
4034 0208f208 2020-05-05 stsp if (err)
4035 0208f208 2020-05-05 stsp return err;
4036 0208f208 2020-05-05 stsp
4037 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4038 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4039 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4040 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4041 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4042 aa8b5dd0 2021-08-01 stsp }
4043 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4044 0208f208 2020-05-05 stsp
4045 0208f208 2020-05-05 stsp }
4046 0208f208 2020-05-05 stsp
4047 0208f208 2020-05-05 stsp if (tree_id1) {
4048 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4049 0208f208 2020-05-05 stsp if (err)
4050 0208f208 2020-05-05 stsp goto done;
4051 0208f208 2020-05-05 stsp }
4052 0208f208 2020-05-05 stsp
4053 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4054 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4055 0208f208 2020-05-05 stsp if (err)
4056 0208f208 2020-05-05 stsp goto done;
4057 0208f208 2020-05-05 stsp
4058 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4059 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4060 0208f208 2020-05-05 stsp done:
4061 0208f208 2020-05-05 stsp if (tree1)
4062 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4063 0208f208 2020-05-05 stsp if (tree2)
4064 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4065 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4066 0208f208 2020-05-05 stsp return err;
4067 0208f208 2020-05-05 stsp }
4068 0208f208 2020-05-05 stsp
4069 0208f208 2020-05-05 stsp static const struct got_error *
4070 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4071 abd2672a 2018-12-23 stsp {
4072 fe621944 2020-11-10 stsp off_t *p;
4073 fe621944 2020-11-10 stsp
4074 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4075 fe621944 2020-11-10 stsp if (p == NULL)
4076 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4077 fe621944 2020-11-10 stsp *line_offsets = p;
4078 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4079 fe621944 2020-11-10 stsp (*nlines)++;
4080 fe621944 2020-11-10 stsp return NULL;
4081 fe621944 2020-11-10 stsp }
4082 fe621944 2020-11-10 stsp
4083 fe621944 2020-11-10 stsp static const struct got_error *
4084 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4085 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4086 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4087 fe621944 2020-11-10 stsp {
4088 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4089 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4090 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4091 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4092 45d799e2 2018-12-23 stsp time_t committer_time;
4093 45d799e2 2018-12-23 stsp const char *author, *committer;
4094 8b473291 2019-02-21 stsp char *refs_str = NULL;
4095 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4096 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4097 fe621944 2020-11-10 stsp off_t outoff = 0;
4098 fe621944 2020-11-10 stsp int n;
4099 abd2672a 2018-12-23 stsp
4100 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4101 0208f208 2020-05-05 stsp
4102 8b473291 2019-02-21 stsp if (refs) {
4103 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4104 8b473291 2019-02-21 stsp if (err)
4105 8b473291 2019-02-21 stsp return err;
4106 8b473291 2019-02-21 stsp }
4107 8b473291 2019-02-21 stsp
4108 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4109 abd2672a 2018-12-23 stsp if (err)
4110 abd2672a 2018-12-23 stsp return err;
4111 abd2672a 2018-12-23 stsp
4112 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4113 15a94983 2018-12-23 stsp if (err) {
4114 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4115 15a94983 2018-12-23 stsp goto done;
4116 15a94983 2018-12-23 stsp }
4117 abd2672a 2018-12-23 stsp
4118 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4119 fe621944 2020-11-10 stsp if (err)
4120 fe621944 2020-11-10 stsp goto done;
4121 fe621944 2020-11-10 stsp
4122 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4123 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4124 fe621944 2020-11-10 stsp if (n < 0) {
4125 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4126 abd2672a 2018-12-23 stsp goto done;
4127 abd2672a 2018-12-23 stsp }
4128 fe621944 2020-11-10 stsp outoff += n;
4129 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4130 fe621944 2020-11-10 stsp if (err)
4131 fe621944 2020-11-10 stsp goto done;
4132 fe621944 2020-11-10 stsp
4133 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4134 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4135 fe621944 2020-11-10 stsp if (n < 0) {
4136 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4137 abd2672a 2018-12-23 stsp goto done;
4138 abd2672a 2018-12-23 stsp }
4139 fe621944 2020-11-10 stsp outoff += n;
4140 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4141 fe621944 2020-11-10 stsp if (err)
4142 fe621944 2020-11-10 stsp goto done;
4143 fe621944 2020-11-10 stsp
4144 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4145 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4146 fe621944 2020-11-10 stsp if (datestr) {
4147 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4148 fe621944 2020-11-10 stsp if (n < 0) {
4149 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4150 fe621944 2020-11-10 stsp goto done;
4151 fe621944 2020-11-10 stsp }
4152 fe621944 2020-11-10 stsp outoff += n;
4153 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4154 fe621944 2020-11-10 stsp if (err)
4155 fe621944 2020-11-10 stsp goto done;
4156 abd2672a 2018-12-23 stsp }
4157 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4158 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4159 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4160 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4161 fe621944 2020-11-10 stsp if (n < 0) {
4162 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4163 fe621944 2020-11-10 stsp goto done;
4164 fe621944 2020-11-10 stsp }
4165 fe621944 2020-11-10 stsp outoff += n;
4166 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4167 fe621944 2020-11-10 stsp if (err)
4168 fe621944 2020-11-10 stsp goto done;
4169 abd2672a 2018-12-23 stsp }
4170 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4171 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4172 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4173 ac4dc263 2021-09-24 thomas int pn = 1;
4174 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4175 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4176 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4177 ac4dc263 2021-09-24 thomas if (err)
4178 ac4dc263 2021-09-24 thomas goto done;
4179 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4180 ac4dc263 2021-09-24 thomas if (n < 0) {
4181 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4182 ac4dc263 2021-09-24 thomas goto done;
4183 ac4dc263 2021-09-24 thomas }
4184 ac4dc263 2021-09-24 thomas outoff += n;
4185 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4186 ac4dc263 2021-09-24 thomas if (err)
4187 ac4dc263 2021-09-24 thomas goto done;
4188 ac4dc263 2021-09-24 thomas free(id_str);
4189 ac4dc263 2021-09-24 thomas id_str = NULL;
4190 ac4dc263 2021-09-24 thomas }
4191 ac4dc263 2021-09-24 thomas }
4192 ac4dc263 2021-09-24 thomas
4193 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4194 5943eee2 2019-08-13 stsp if (err)
4195 5943eee2 2019-08-13 stsp goto done;
4196 fe621944 2020-11-10 stsp s = logmsg;
4197 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4198 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4199 fe621944 2020-11-10 stsp if (n < 0) {
4200 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4201 fe621944 2020-11-10 stsp goto done;
4202 fe621944 2020-11-10 stsp }
4203 fe621944 2020-11-10 stsp outoff += n;
4204 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4205 fe621944 2020-11-10 stsp if (err)
4206 fe621944 2020-11-10 stsp goto done;
4207 abd2672a 2018-12-23 stsp }
4208 fe621944 2020-11-10 stsp
4209 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4210 0208f208 2020-05-05 stsp if (err)
4211 0208f208 2020-05-05 stsp goto done;
4212 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4213 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4214 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4215 fe621944 2020-11-10 stsp if (n < 0) {
4216 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4217 fe621944 2020-11-10 stsp goto done;
4218 fe621944 2020-11-10 stsp }
4219 fe621944 2020-11-10 stsp outoff += n;
4220 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4221 fe621944 2020-11-10 stsp if (err)
4222 fe621944 2020-11-10 stsp goto done;
4223 0208f208 2020-05-05 stsp free((char *)pe->path);
4224 0208f208 2020-05-05 stsp free(pe->data);
4225 0208f208 2020-05-05 stsp }
4226 fe621944 2020-11-10 stsp
4227 0208f208 2020-05-05 stsp fputc('\n', outfile);
4228 fe621944 2020-11-10 stsp outoff++;
4229 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4230 abd2672a 2018-12-23 stsp done:
4231 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4232 abd2672a 2018-12-23 stsp free(id_str);
4233 5943eee2 2019-08-13 stsp free(logmsg);
4234 8b473291 2019-02-21 stsp free(refs_str);
4235 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4236 7510f233 2020-08-09 stsp if (err) {
4237 ae6a6978 2020-08-09 stsp free(*line_offsets);
4238 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4239 ae6a6978 2020-08-09 stsp *nlines = 0;
4240 7510f233 2020-08-09 stsp }
4241 fe621944 2020-11-10 stsp return err;
4242 abd2672a 2018-12-23 stsp }
4243 abd2672a 2018-12-23 stsp
4244 abd2672a 2018-12-23 stsp static const struct got_error *
4245 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4246 26ed57b2 2018-05-19 stsp {
4247 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4248 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4249 15a94983 2018-12-23 stsp int obj_type;
4250 fe621944 2020-11-10 stsp
4251 fe621944 2020-11-10 stsp free(s->line_offsets);
4252 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4253 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4254 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4255 fe621944 2020-11-10 stsp s->nlines = 0;
4256 26ed57b2 2018-05-19 stsp
4257 511a516b 2018-05-19 stsp f = got_opentemp();
4258 48ae06ee 2018-10-18 stsp if (f == NULL) {
4259 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4260 48ae06ee 2018-10-18 stsp goto done;
4261 48ae06ee 2018-10-18 stsp }
4262 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4263 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4264 fb43ecf1 2019-02-11 stsp goto done;
4265 fb43ecf1 2019-02-11 stsp }
4266 48ae06ee 2018-10-18 stsp s->f = f;
4267 26ed57b2 2018-05-19 stsp
4268 15a94983 2018-12-23 stsp if (s->id1)
4269 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4270 15a94983 2018-12-23 stsp else
4271 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4272 15a94983 2018-12-23 stsp if (err)
4273 15a94983 2018-12-23 stsp goto done;
4274 15a94983 2018-12-23 stsp
4275 15a94983 2018-12-23 stsp switch (obj_type) {
4276 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4277 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4278 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4279 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4280 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4281 26ed57b2 2018-05-19 stsp break;
4282 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4283 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4284 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4285 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4286 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4287 26ed57b2 2018-05-19 stsp break;
4288 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4289 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4290 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4291 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4292 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4293 abd2672a 2018-12-23 stsp
4294 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4295 abd2672a 2018-12-23 stsp if (err)
4296 3ffacbe1 2020-02-02 tracey goto done;
4297 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4298 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4299 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4300 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4301 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4302 f44b1f58 2020-02-02 tracey if (err)
4303 f44b1f58 2020-02-02 tracey goto done;
4304 f44b1f58 2020-02-02 tracey } else {
4305 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4306 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4307 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4308 fe621944 2020-11-10 stsp err = write_commit_info(
4309 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4310 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4311 f44b1f58 2020-02-02 tracey if (err)
4312 f44b1f58 2020-02-02 tracey goto done;
4313 f5404e4e 2020-02-02 tracey break;
4314 15a087fe 2019-02-21 stsp }
4315 abd2672a 2018-12-23 stsp }
4316 abd2672a 2018-12-23 stsp }
4317 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4318 abd2672a 2018-12-23 stsp
4319 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4320 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4321 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4322 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4323 26ed57b2 2018-05-19 stsp break;
4324 abd2672a 2018-12-23 stsp }
4325 26ed57b2 2018-05-19 stsp default:
4326 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4327 48ae06ee 2018-10-18 stsp break;
4328 26ed57b2 2018-05-19 stsp }
4329 f44b1f58 2020-02-02 tracey if (err)
4330 f44b1f58 2020-02-02 tracey goto done;
4331 48ae06ee 2018-10-18 stsp done:
4332 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4333 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4334 48ae06ee 2018-10-18 stsp return err;
4335 48ae06ee 2018-10-18 stsp }
4336 26ed57b2 2018-05-19 stsp
4337 f5215bb9 2019-02-22 stsp static void
4338 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4339 f5215bb9 2019-02-22 stsp {
4340 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4341 f5215bb9 2019-02-22 stsp update_panels();
4342 f5215bb9 2019-02-22 stsp doupdate();
4343 f44b1f58 2020-02-02 tracey }
4344 f44b1f58 2020-02-02 tracey
4345 f44b1f58 2020-02-02 tracey static const struct got_error *
4346 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4347 f44b1f58 2020-02-02 tracey {
4348 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4349 f44b1f58 2020-02-02 tracey
4350 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4351 f44b1f58 2020-02-02 tracey return NULL;
4352 f44b1f58 2020-02-02 tracey }
4353 f44b1f58 2020-02-02 tracey
4354 f44b1f58 2020-02-02 tracey static const struct got_error *
4355 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4356 f44b1f58 2020-02-02 tracey {
4357 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4358 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4359 f44b1f58 2020-02-02 tracey int lineno;
4360 78eecffc 2022-06-23 thomas char *line = NULL;
4361 826082fe 2020-12-10 stsp size_t linesize = 0;
4362 826082fe 2020-12-10 stsp ssize_t linelen;
4363 f44b1f58 2020-02-02 tracey
4364 f44b1f58 2020-02-02 tracey if (!view->searching) {
4365 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4366 f44b1f58 2020-02-02 tracey return NULL;
4367 f44b1f58 2020-02-02 tracey }
4368 f44b1f58 2020-02-02 tracey
4369 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4370 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4371 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4372 f44b1f58 2020-02-02 tracey else
4373 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4374 4b3f9dac 2021-12-17 thomas } else
4375 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4376 f44b1f58 2020-02-02 tracey
4377 f44b1f58 2020-02-02 tracey while (1) {
4378 f44b1f58 2020-02-02 tracey off_t offset;
4379 f44b1f58 2020-02-02 tracey
4380 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4381 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4382 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 f44b1f58 2020-02-02 tracey break;
4384 f44b1f58 2020-02-02 tracey }
4385 f44b1f58 2020-02-02 tracey
4386 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4387 f44b1f58 2020-02-02 tracey lineno = 1;
4388 f44b1f58 2020-02-02 tracey else
4389 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4390 f44b1f58 2020-02-02 tracey }
4391 f44b1f58 2020-02-02 tracey
4392 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4393 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4394 f44b1f58 2020-02-02 tracey free(line);
4395 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4396 f44b1f58 2020-02-02 tracey }
4397 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4398 78eecffc 2022-06-23 thomas if (linelen != -1) {
4399 78eecffc 2022-06-23 thomas char *exstr;
4400 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4401 78eecffc 2022-06-23 thomas if (err)
4402 78eecffc 2022-06-23 thomas break;
4403 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4404 78eecffc 2022-06-23 thomas &view->regmatch)) {
4405 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4406 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4407 78eecffc 2022-06-23 thomas free(exstr);
4408 78eecffc 2022-06-23 thomas break;
4409 78eecffc 2022-06-23 thomas }
4410 78eecffc 2022-06-23 thomas free(exstr);
4411 f44b1f58 2020-02-02 tracey }
4412 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4413 f44b1f58 2020-02-02 tracey lineno++;
4414 f44b1f58 2020-02-02 tracey else
4415 f44b1f58 2020-02-02 tracey lineno--;
4416 f44b1f58 2020-02-02 tracey }
4417 826082fe 2020-12-10 stsp free(line);
4418 f44b1f58 2020-02-02 tracey
4419 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4420 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4421 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4422 f44b1f58 2020-02-02 tracey }
4423 f44b1f58 2020-02-02 tracey
4424 05171be4 2022-06-23 thomas return err;
4425 f5215bb9 2019-02-22 stsp }
4426 f5215bb9 2019-02-22 stsp
4427 48ae06ee 2018-10-18 stsp static const struct got_error *
4428 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4429 a0f32f33 2022-06-13 thomas {
4430 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4431 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4432 a0f32f33 2022-06-13 thomas
4433 a0f32f33 2022-06-13 thomas free(s->id1);
4434 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4435 a0f32f33 2022-06-13 thomas free(s->id2);
4436 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4437 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4438 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4439 a0f32f33 2022-06-13 thomas s->f = NULL;
4440 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4441 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4442 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4443 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4444 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4445 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4446 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4447 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4448 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4449 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4450 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4451 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4452 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4453 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4454 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4455 a0f32f33 2022-06-13 thomas s->nlines = 0;
4456 a0f32f33 2022-06-13 thomas return err;
4457 a0f32f33 2022-06-13 thomas }
4458 a0f32f33 2022-06-13 thomas
4459 a0f32f33 2022-06-13 thomas static const struct got_error *
4460 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4461 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4462 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4463 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4464 48ae06ee 2018-10-18 stsp {
4465 48ae06ee 2018-10-18 stsp const struct got_error *err;
4466 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4467 5dc9f4bc 2018-08-04 stsp
4468 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4469 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4470 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4471 a0f32f33 2022-06-13 thomas
4472 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4473 15a94983 2018-12-23 stsp int type1, type2;
4474 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4475 15a94983 2018-12-23 stsp if (err)
4476 15a94983 2018-12-23 stsp return err;
4477 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4478 15a94983 2018-12-23 stsp if (err)
4479 15a94983 2018-12-23 stsp return err;
4480 15a94983 2018-12-23 stsp
4481 15a94983 2018-12-23 stsp if (type1 != type2)
4482 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4483 15a94983 2018-12-23 stsp }
4484 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4485 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4486 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4487 f44b1f58 2020-02-02 tracey s->repo = repo;
4488 f44b1f58 2020-02-02 tracey s->id1 = id1;
4489 f44b1f58 2020-02-02 tracey s->id2 = id2;
4490 3dbaef42 2020-11-24 stsp s->label1 = label1;
4491 3dbaef42 2020-11-24 stsp s->label2 = label2;
4492 48ae06ee 2018-10-18 stsp
4493 15a94983 2018-12-23 stsp if (id1) {
4494 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4495 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4496 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4497 48ae06ee 2018-10-18 stsp } else
4498 5465d566 2020-02-01 tracey s->id1 = NULL;
4499 48ae06ee 2018-10-18 stsp
4500 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4501 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4502 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4503 a0f32f33 2022-06-13 thomas goto done;
4504 48ae06ee 2018-10-18 stsp }
4505 a0f32f33 2022-06-13 thomas
4506 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4507 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4508 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4509 9a1d7435 2022-06-23 thomas goto done;
4510 9a1d7435 2022-06-23 thomas }
4511 9a1d7435 2022-06-23 thomas
4512 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4513 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4514 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4515 a0f32f33 2022-06-13 thomas goto done;
4516 a0f32f33 2022-06-13 thomas }
4517 a0f32f33 2022-06-13 thomas
4518 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4519 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4520 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4521 19a6a6b5 2022-07-01 thomas goto done;
4522 19a6a6b5 2022-07-01 thomas }
4523 19a6a6b5 2022-07-01 thomas
4524 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4525 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4526 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4527 19a6a6b5 2022-07-01 thomas goto done;
4528 19a6a6b5 2022-07-01 thomas }
4529 19a6a6b5 2022-07-01 thomas
4530 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4531 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4532 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4533 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4534 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4535 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4536 5465d566 2020-02-01 tracey s->repo = repo;
4537 6d17833f 2019-11-08 stsp
4538 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4539 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4540 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4541 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4543 6d17833f 2019-11-08 stsp 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_PLUS,
4547 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4548 a0f32f33 2022-06-13 thomas if (err)
4549 a0f32f33 2022-06-13 thomas goto done;
4550 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4551 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4552 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4553 a0f32f33 2022-06-13 thomas if (err)
4554 a0f32f33 2022-06-13 thomas goto done;
4555 6d17833f 2019-11-08 stsp
4556 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4557 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4558 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4559 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4560 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4561 a0f32f33 2022-06-13 thomas if (err)
4562 a0f32f33 2022-06-13 thomas goto done;
4563 11b20872 2019-11-08 stsp
4564 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4565 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4566 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4567 a0f32f33 2022-06-13 thomas if (err)
4568 a0f32f33 2022-06-13 thomas goto done;
4569 11b20872 2019-11-08 stsp
4570 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4571 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4572 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4573 a0f32f33 2022-06-13 thomas if (err)
4574 a0f32f33 2022-06-13 thomas goto done;
4575 6d17833f 2019-11-08 stsp }
4576 5dc9f4bc 2018-08-04 stsp
4577 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4578 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4579 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4580 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4581 f5215bb9 2019-02-22 stsp
4582 5465d566 2020-02-01 tracey err = create_diff(s);
4583 48ae06ee 2018-10-18 stsp
4584 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4585 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4586 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4587 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4588 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4589 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4590 a0f32f33 2022-06-13 thomas done:
4591 a0f32f33 2022-06-13 thomas if (err)
4592 a0f32f33 2022-06-13 thomas close_diff_view(view);
4593 e5a0f69f 2018-08-18 stsp return err;
4594 5dc9f4bc 2018-08-04 stsp }
4595 5dc9f4bc 2018-08-04 stsp
4596 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4597 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4598 5dc9f4bc 2018-08-04 stsp {
4599 a3404814 2018-09-02 stsp const struct got_error *err;
4600 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4601 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4602 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4603 a3404814 2018-09-02 stsp
4604 a3404814 2018-09-02 stsp if (s->id1) {
4605 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4606 a3404814 2018-09-02 stsp if (err)
4607 a3404814 2018-09-02 stsp return err;
4608 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4609 3dbaef42 2020-11-24 stsp } else
4610 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4611 3dbaef42 2020-11-24 stsp
4612 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4613 a3404814 2018-09-02 stsp if (err)
4614 a3404814 2018-09-02 stsp return err;
4615 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4616 26ed57b2 2018-05-19 stsp
4617 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4618 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4619 a3404814 2018-09-02 stsp free(id_str1);
4620 a3404814 2018-09-02 stsp free(id_str2);
4621 a3404814 2018-09-02 stsp return err;
4622 a3404814 2018-09-02 stsp }
4623 a3404814 2018-09-02 stsp free(id_str1);
4624 a3404814 2018-09-02 stsp free(id_str2);
4625 a3404814 2018-09-02 stsp
4626 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4627 267bb3b8 2021-08-01 stsp free(header);
4628 267bb3b8 2021-08-01 stsp return err;
4629 15a087fe 2019-02-21 stsp }
4630 15a087fe 2019-02-21 stsp
4631 15a087fe 2019-02-21 stsp static const struct got_error *
4632 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4633 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4634 15a087fe 2019-02-21 stsp {
4635 d7a04538 2019-02-21 stsp const struct got_error *err;
4636 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4637 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4638 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4639 15a087fe 2019-02-21 stsp
4640 15a087fe 2019-02-21 stsp free(s->id2);
4641 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4642 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4643 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4644 15a087fe 2019-02-21 stsp
4645 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4646 d7a04538 2019-02-21 stsp if (err)
4647 d7a04538 2019-02-21 stsp return err;
4648 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4649 15a087fe 2019-02-21 stsp free(s->id1);
4650 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4651 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4652 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4653 15a087fe 2019-02-21 stsp return NULL;
4654 0cf4efb1 2018-09-29 stsp }
4655 0cf4efb1 2018-09-29 stsp
4656 0cf4efb1 2018-09-29 stsp static const struct got_error *
4657 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4658 adf4c9e0 2022-07-03 thomas {
4659 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4660 adf4c9e0 2022-07-03 thomas
4661 adf4c9e0 2022-07-03 thomas view->count = 0;
4662 adf4c9e0 2022-07-03 thomas wclear(view->window);
4663 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4664 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4665 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4666 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4667 adf4c9e0 2022-07-03 thomas return create_diff(s);
4668 adf4c9e0 2022-07-03 thomas }
4669 adf4c9e0 2022-07-03 thomas
4670 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4671 4fc71f3b 2022-07-12 thomas int, int, int);
4672 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4673 4fc71f3b 2022-07-12 thomas int, int);
4674 4fc71f3b 2022-07-12 thomas
4675 adf4c9e0 2022-07-03 thomas static const struct got_error *
4676 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4677 e5a0f69f 2018-08-18 stsp {
4678 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4679 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4680 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4681 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4682 826082fe 2020-12-10 stsp char *line = NULL;
4683 826082fe 2020-12-10 stsp size_t linesize = 0;
4684 826082fe 2020-12-10 stsp ssize_t linelen;
4685 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4686 e5a0f69f 2018-08-18 stsp
4687 e5a0f69f 2018-08-18 stsp switch (ch) {
4688 05171be4 2022-06-23 thomas case '0':
4689 05171be4 2022-06-23 thomas view->x = 0;
4690 05171be4 2022-06-23 thomas break;
4691 05171be4 2022-06-23 thomas case '$':
4692 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4693 07b0611c 2022-06-23 thomas view->count = 0;
4694 05171be4 2022-06-23 thomas break;
4695 05171be4 2022-06-23 thomas case KEY_RIGHT:
4696 05171be4 2022-06-23 thomas case 'l':
4697 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4698 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4699 07b0611c 2022-06-23 thomas else
4700 07b0611c 2022-06-23 thomas view->count = 0;
4701 05171be4 2022-06-23 thomas break;
4702 05171be4 2022-06-23 thomas case KEY_LEFT:
4703 05171be4 2022-06-23 thomas case 'h':
4704 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4705 07b0611c 2022-06-23 thomas if (view->x <= 0)
4706 07b0611c 2022-06-23 thomas view->count = 0;
4707 05171be4 2022-06-23 thomas break;
4708 64453f7e 2020-11-21 stsp case 'a':
4709 3dbaef42 2020-11-24 stsp case 'w':
4710 3dbaef42 2020-11-24 stsp if (ch == 'a')
4711 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4712 3dbaef42 2020-11-24 stsp if (ch == 'w')
4713 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4714 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4715 912a3f79 2021-08-30 j break;
4716 912a3f79 2021-08-30 j case 'g':
4717 912a3f79 2021-08-30 j case KEY_HOME:
4718 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4719 07b0611c 2022-06-23 thomas view->count = 0;
4720 64453f7e 2020-11-21 stsp break;
4721 912a3f79 2021-08-30 j case 'G':
4722 912a3f79 2021-08-30 j case KEY_END:
4723 07b0611c 2022-06-23 thomas view->count = 0;
4724 912a3f79 2021-08-30 j if (s->eof)
4725 912a3f79 2021-08-30 j break;
4726 912a3f79 2021-08-30 j
4727 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4728 912a3f79 2021-08-30 j s->eof = 1;
4729 912a3f79 2021-08-30 j break;
4730 1e37a5c2 2019-05-12 jcs case 'k':
4731 1e37a5c2 2019-05-12 jcs case KEY_UP:
4732 f7140bf5 2021-10-17 thomas case CTRL('p'):
4733 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4734 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4735 07b0611c 2022-06-23 thomas else
4736 07b0611c 2022-06-23 thomas view->count = 0;
4737 1e37a5c2 2019-05-12 jcs break;
4738 70f17a53 2022-06-13 thomas case CTRL('u'):
4739 23427b14 2022-06-23 thomas case 'u':
4740 70f17a53 2022-06-13 thomas nscroll /= 2;
4741 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4742 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4743 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4744 1c5e5faa 2022-06-23 thomas case 'b':
4745 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4746 07b0611c 2022-06-23 thomas view->count = 0;
4747 26ed57b2 2018-05-19 stsp break;
4748 07b0611c 2022-06-23 thomas }
4749 1e37a5c2 2019-05-12 jcs i = 0;
4750 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4751 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4752 1e37a5c2 2019-05-12 jcs break;
4753 1e37a5c2 2019-05-12 jcs case 'j':
4754 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4755 f7140bf5 2021-10-17 thomas case CTRL('n'):
4756 1e37a5c2 2019-05-12 jcs if (!s->eof)
4757 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4758 07b0611c 2022-06-23 thomas else
4759 07b0611c 2022-06-23 thomas view->count = 0;
4760 1e37a5c2 2019-05-12 jcs break;
4761 70f17a53 2022-06-13 thomas case CTRL('d'):
4762 23427b14 2022-06-23 thomas case 'd':
4763 70f17a53 2022-06-13 thomas nscroll /= 2;
4764 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4765 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4766 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4767 1c5e5faa 2022-06-23 thomas case 'f':
4768 1e37a5c2 2019-05-12 jcs case ' ':
4769 07b0611c 2022-06-23 thomas if (s->eof) {
4770 07b0611c 2022-06-23 thomas view->count = 0;
4771 1e37a5c2 2019-05-12 jcs break;
4772 07b0611c 2022-06-23 thomas }
4773 1e37a5c2 2019-05-12 jcs i = 0;
4774 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4775 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4776 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4777 826082fe 2020-12-10 stsp if (linelen == -1) {
4778 826082fe 2020-12-10 stsp if (feof(s->f)) {
4779 826082fe 2020-12-10 stsp s->eof = 1;
4780 826082fe 2020-12-10 stsp } else
4781 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4782 34bc9ec9 2019-02-22 stsp break;
4783 826082fe 2020-12-10 stsp }
4784 1e37a5c2 2019-05-12 jcs }
4785 826082fe 2020-12-10 stsp free(line);
4786 1e37a5c2 2019-05-12 jcs break;
4787 1e37a5c2 2019-05-12 jcs case '[':
4788 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4789 1e37a5c2 2019-05-12 jcs s->diff_context--;
4790 aa61903a 2021-12-31 thomas s->matched_line = 0;
4791 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4792 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4793 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4794 27829c9e 2020-11-21 stsp s->nlines) {
4795 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4796 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4797 27829c9e 2020-11-21 stsp }
4798 07b0611c 2022-06-23 thomas } else
4799 07b0611c 2022-06-23 thomas view->count = 0;
4800 1e37a5c2 2019-05-12 jcs break;
4801 1e37a5c2 2019-05-12 jcs case ']':
4802 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4803 1e37a5c2 2019-05-12 jcs s->diff_context++;
4804 aa61903a 2021-12-31 thomas s->matched_line = 0;
4805 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4806 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4807 07b0611c 2022-06-23 thomas } else
4808 07b0611c 2022-06-23 thomas view->count = 0;
4809 1e37a5c2 2019-05-12 jcs break;
4810 1e37a5c2 2019-05-12 jcs case '<':
4811 1e37a5c2 2019-05-12 jcs case ',':
4812 4fc71f3b 2022-07-12 thomas up = 1;
4813 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4814 4fc71f3b 2022-07-12 thomas case '>':
4815 4fc71f3b 2022-07-12 thomas case '.':
4816 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4817 07b0611c 2022-06-23 thomas view->count = 0;
4818 48ae06ee 2018-10-18 stsp break;
4819 07b0611c 2022-06-23 thomas }
4820 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4821 6524637e 2019-02-21 stsp
4822 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4823 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4824 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4825 15a087fe 2019-02-21 stsp
4826 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4827 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4828 4fc71f3b 2022-07-12 thomas if (err)
4829 4fc71f3b 2022-07-12 thomas break;
4830 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4831 15a087fe 2019-02-21 stsp
4832 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4833 4fc71f3b 2022-07-12 thomas break;
4834 15a087fe 2019-02-21 stsp
4835 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4836 4fc71f3b 2022-07-12 thomas if (err)
4837 4fc71f3b 2022-07-12 thomas break;
4838 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4839 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4840 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4841 5e224a3e 2019-02-22 stsp
4842 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4843 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4844 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4845 4fc71f3b 2022-07-12 thomas
4846 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4847 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4848 4fc71f3b 2022-07-12 thomas if (err)
4849 4fc71f3b 2022-07-12 thomas break;
4850 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4851 5a8b5076 2020-12-05 stsp
4852 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4853 4fc71f3b 2022-07-12 thomas break;
4854 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4855 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4856 4fc71f3b 2022-07-12 thomas bs->selected_line);
4857 4fc71f3b 2022-07-12 thomas if (id == NULL)
4858 4fc71f3b 2022-07-12 thomas break;
4859 15a087fe 2019-02-21 stsp
4860 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4861 4fc71f3b 2022-07-12 thomas break;
4862 15a087fe 2019-02-21 stsp
4863 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4864 4fc71f3b 2022-07-12 thomas if (err)
4865 4fc71f3b 2022-07-12 thomas break;
4866 4fc71f3b 2022-07-12 thomas }
4867 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4868 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4869 aa61903a 2021-12-31 thomas s->matched_line = 0;
4870 05171be4 2022-06-23 thomas view->x = 0;
4871 1e37a5c2 2019-05-12 jcs
4872 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4873 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4874 1e37a5c2 2019-05-12 jcs break;
4875 1e37a5c2 2019-05-12 jcs default:
4876 07b0611c 2022-06-23 thomas view->count = 0;
4877 1e37a5c2 2019-05-12 jcs break;
4878 26ed57b2 2018-05-19 stsp }
4879 e5a0f69f 2018-08-18 stsp
4880 bcbd79e2 2018-08-19 stsp return err;
4881 26ed57b2 2018-05-19 stsp }
4882 26ed57b2 2018-05-19 stsp
4883 4ed7e80c 2018-05-20 stsp static const struct got_error *
4884 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4885 9f7d7167 2018-04-29 stsp {
4886 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4887 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4888 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4889 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4890 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4891 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4892 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4893 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4894 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4895 3dbaef42 2020-11-24 stsp const char *errstr;
4896 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4897 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4898 70ac5f84 2019-03-28 stsp
4899 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4900 26ed57b2 2018-05-19 stsp switch (ch) {
4901 64453f7e 2020-11-21 stsp case 'a':
4902 64453f7e 2020-11-21 stsp force_text_diff = 1;
4903 3dbaef42 2020-11-24 stsp break;
4904 3dbaef42 2020-11-24 stsp case 'C':
4905 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4906 3dbaef42 2020-11-24 stsp &errstr);
4907 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4908 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4909 8f666e67 2022-02-12 thomas errstr, errstr);
4910 64453f7e 2020-11-21 stsp break;
4911 09b5bff8 2020-02-23 naddy case 'r':
4912 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4913 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4914 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4915 09b5bff8 2020-02-23 naddy optarg);
4916 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4917 09b5bff8 2020-02-23 naddy break;
4918 3dbaef42 2020-11-24 stsp case 'w':
4919 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4920 3dbaef42 2020-11-24 stsp break;
4921 26ed57b2 2018-05-19 stsp default:
4922 17020d27 2019-03-07 stsp usage_diff();
4923 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4924 26ed57b2 2018-05-19 stsp }
4925 26ed57b2 2018-05-19 stsp }
4926 26ed57b2 2018-05-19 stsp
4927 26ed57b2 2018-05-19 stsp argc -= optind;
4928 26ed57b2 2018-05-19 stsp argv += optind;
4929 26ed57b2 2018-05-19 stsp
4930 26ed57b2 2018-05-19 stsp if (argc == 0) {
4931 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4932 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4933 15a94983 2018-12-23 stsp id_str1 = argv[0];
4934 15a94983 2018-12-23 stsp id_str2 = argv[1];
4935 26ed57b2 2018-05-19 stsp } else
4936 26ed57b2 2018-05-19 stsp usage_diff();
4937 eb6600df 2019-01-04 stsp
4938 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4939 7cd52833 2022-06-23 thomas if (error)
4940 7cd52833 2022-06-23 thomas goto done;
4941 7cd52833 2022-06-23 thomas
4942 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4943 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4944 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4945 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4946 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4947 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4948 c156c7a4 2020-12-18 stsp goto done;
4949 a273ac94 2020-02-23 naddy if (worktree)
4950 a273ac94 2020-02-23 naddy repo_path =
4951 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4952 a273ac94 2020-02-23 naddy else
4953 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4954 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4955 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4956 c156c7a4 2020-12-18 stsp goto done;
4957 c156c7a4 2020-12-18 stsp }
4958 a273ac94 2020-02-23 naddy }
4959 a273ac94 2020-02-23 naddy
4960 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4961 eb6600df 2019-01-04 stsp if (error)
4962 eb6600df 2019-01-04 stsp goto done;
4963 26ed57b2 2018-05-19 stsp
4964 a273ac94 2020-02-23 naddy init_curses();
4965 a273ac94 2020-02-23 naddy
4966 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4967 51a10b52 2020-12-26 stsp if (error)
4968 51a10b52 2020-12-26 stsp goto done;
4969 51a10b52 2020-12-26 stsp
4970 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
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(&id1, &label1, id_str1,
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 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4980 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4981 26ed57b2 2018-05-19 stsp if (error)
4982 26ed57b2 2018-05-19 stsp goto done;
4983 26ed57b2 2018-05-19 stsp
4984 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4985 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4986 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4987 ea5e7bb5 2018-08-01 stsp goto done;
4988 ea5e7bb5 2018-08-01 stsp }
4989 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4990 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4991 5dc9f4bc 2018-08-04 stsp if (error)
4992 5dc9f4bc 2018-08-04 stsp goto done;
4993 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4994 26ed57b2 2018-05-19 stsp done:
4995 3dbaef42 2020-11-24 stsp free(label1);
4996 3dbaef42 2020-11-24 stsp free(label2);
4997 c02c541e 2019-03-29 stsp free(repo_path);
4998 a273ac94 2020-02-23 naddy free(cwd);
4999 1d0f4054 2021-06-17 stsp if (repo) {
5000 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5001 1d0f4054 2021-06-17 stsp if (error == NULL)
5002 1d0f4054 2021-06-17 stsp error = close_err;
5003 1d0f4054 2021-06-17 stsp }
5004 a273ac94 2020-02-23 naddy if (worktree)
5005 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5006 7cd52833 2022-06-23 thomas if (pack_fds) {
5007 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5008 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5009 7cd52833 2022-06-23 thomas if (error == NULL)
5010 7cd52833 2022-06-23 thomas error = pack_err;
5011 7cd52833 2022-06-23 thomas }
5012 51a10b52 2020-12-26 stsp tog_free_refs();
5013 26ed57b2 2018-05-19 stsp return error;
5014 9f7d7167 2018-04-29 stsp }
5015 9f7d7167 2018-04-29 stsp
5016 4ed7e80c 2018-05-20 stsp __dead static void
5017 9f7d7167 2018-04-29 stsp usage_blame(void)
5018 9f7d7167 2018-04-29 stsp {
5019 80ddbec8 2018-04-29 stsp endwin();
5020 91198554 2022-06-23 thomas fprintf(stderr,
5021 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5022 9f7d7167 2018-04-29 stsp getprogname());
5023 9f7d7167 2018-04-29 stsp exit(1);
5024 9f7d7167 2018-04-29 stsp }
5025 84451b3e 2018-07-10 stsp
5026 84451b3e 2018-07-10 stsp struct tog_blame_line {
5027 84451b3e 2018-07-10 stsp int annotated;
5028 84451b3e 2018-07-10 stsp struct got_object_id *id;
5029 84451b3e 2018-07-10 stsp };
5030 9f7d7167 2018-04-29 stsp
5031 4ed7e80c 2018-05-20 stsp static const struct got_error *
5032 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5033 84451b3e 2018-07-10 stsp {
5034 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5035 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5036 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5037 84451b3e 2018-07-10 stsp const struct got_error *err;
5038 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5039 826082fe 2020-12-10 stsp char *line = NULL;
5040 826082fe 2020-12-10 stsp size_t linesize = 0;
5041 826082fe 2020-12-10 stsp ssize_t linelen;
5042 84451b3e 2018-07-10 stsp wchar_t *wline;
5043 27a741e5 2019-09-11 stsp int width;
5044 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5045 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5046 ab089a2a 2018-07-12 stsp char *id_str;
5047 11b20872 2019-11-08 stsp struct tog_color *tc;
5048 ab089a2a 2018-07-12 stsp
5049 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5050 ab089a2a 2018-07-12 stsp if (err)
5051 ab089a2a 2018-07-12 stsp return err;
5052 84451b3e 2018-07-10 stsp
5053 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5054 f7d12f7e 2018-08-01 stsp werase(view->window);
5055 84451b3e 2018-07-10 stsp
5056 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5057 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5058 ab089a2a 2018-07-12 stsp free(id_str);
5059 ab089a2a 2018-07-12 stsp return err;
5060 ab089a2a 2018-07-12 stsp }
5061 ab089a2a 2018-07-12 stsp
5062 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5063 ab089a2a 2018-07-12 stsp free(line);
5064 2550e4c3 2018-07-13 stsp line = NULL;
5065 1cae65b4 2019-09-22 stsp if (err)
5066 1cae65b4 2019-09-22 stsp return err;
5067 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5068 a3404814 2018-09-02 stsp wstandout(view->window);
5069 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5070 11b20872 2019-11-08 stsp if (tc)
5071 11b20872 2019-11-08 stsp wattr_on(view->window,
5072 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5073 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5074 11b20872 2019-11-08 stsp if (tc)
5075 11b20872 2019-11-08 stsp wattr_off(view->window,
5076 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5077 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5078 a3404814 2018-09-02 stsp wstandend(view->window);
5079 2550e4c3 2018-07-13 stsp free(wline);
5080 2550e4c3 2018-07-13 stsp wline = NULL;
5081 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5082 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5083 ab089a2a 2018-07-12 stsp
5084 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5085 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5086 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5087 ab089a2a 2018-07-12 stsp free(id_str);
5088 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5089 ab089a2a 2018-07-12 stsp }
5090 ab089a2a 2018-07-12 stsp free(id_str);
5091 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5092 3f60a8ef 2018-07-10 stsp free(line);
5093 2550e4c3 2018-07-13 stsp line = NULL;
5094 3f60a8ef 2018-07-10 stsp if (err)
5095 3f60a8ef 2018-07-10 stsp return err;
5096 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5097 2550e4c3 2018-07-13 stsp free(wline);
5098 2550e4c3 2018-07-13 stsp wline = NULL;
5099 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5100 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5101 3f60a8ef 2018-07-10 stsp
5102 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5103 05171be4 2022-06-23 thomas view->maxx = 0;
5104 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5105 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5106 826082fe 2020-12-10 stsp if (linelen == -1) {
5107 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5108 826082fe 2020-12-10 stsp s->eof = 1;
5109 826082fe 2020-12-10 stsp break;
5110 826082fe 2020-12-10 stsp }
5111 84451b3e 2018-07-10 stsp free(line);
5112 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5113 84451b3e 2018-07-10 stsp }
5114 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5115 826082fe 2020-12-10 stsp continue;
5116 cbea3800 2022-06-23 thomas
5117 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5118 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5119 cbea3800 2022-06-23 thomas if (err) {
5120 cbea3800 2022-06-23 thomas free(line);
5121 cbea3800 2022-06-23 thomas return err;
5122 cbea3800 2022-06-23 thomas }
5123 cbea3800 2022-06-23 thomas free(wline);
5124 cbea3800 2022-06-23 thomas wline = NULL;
5125 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5126 84451b3e 2018-07-10 stsp
5127 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5128 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5129 b700b5d6 2018-07-10 stsp
5130 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5131 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5132 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5133 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5134 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5135 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5136 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5137 8d0fe45a 2019-08-12 stsp char *id_str;
5138 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5139 91198554 2022-06-23 thomas blame_line->id);
5140 8d0fe45a 2019-08-12 stsp if (err) {
5141 8d0fe45a 2019-08-12 stsp free(line);
5142 8d0fe45a 2019-08-12 stsp return err;
5143 8d0fe45a 2019-08-12 stsp }
5144 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5145 11b20872 2019-11-08 stsp if (tc)
5146 11b20872 2019-11-08 stsp wattr_on(view->window,
5147 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5148 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5149 11b20872 2019-11-08 stsp if (tc)
5150 11b20872 2019-11-08 stsp wattr_off(view->window,
5151 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5152 8d0fe45a 2019-08-12 stsp free(id_str);
5153 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5154 8d0fe45a 2019-08-12 stsp } else {
5155 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5156 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5157 84451b3e 2018-07-10 stsp }
5158 ee41ec32 2018-07-10 stsp } else {
5159 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5160 ee41ec32 2018-07-10 stsp prev_id = NULL;
5161 ee41ec32 2018-07-10 stsp }
5162 84451b3e 2018-07-10 stsp
5163 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5164 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5165 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5166 27a741e5 2019-09-11 stsp
5167 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5168 41605754 2020-11-12 stsp width = 9;
5169 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5170 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5171 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5172 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5173 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5174 41605754 2020-11-12 stsp if (err) {
5175 41605754 2020-11-12 stsp free(line);
5176 41605754 2020-11-12 stsp return err;
5177 41605754 2020-11-12 stsp }
5178 41605754 2020-11-12 stsp width += 9;
5179 41605754 2020-11-12 stsp } else {
5180 923086fd 2022-06-23 thomas int skip;
5181 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5182 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5183 923086fd 2022-06-23 thomas if (err) {
5184 923086fd 2022-06-23 thomas free(line);
5185 923086fd 2022-06-23 thomas return err;
5186 05171be4 2022-06-23 thomas }
5187 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5188 02bce7e2 2022-06-23 thomas width += 9;
5189 41605754 2020-11-12 stsp free(wline);
5190 41605754 2020-11-12 stsp wline = NULL;
5191 41605754 2020-11-12 stsp }
5192 41605754 2020-11-12 stsp
5193 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5194 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5195 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5196 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5197 84451b3e 2018-07-10 stsp }
5198 826082fe 2020-12-10 stsp free(line);
5199 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5200 84451b3e 2018-07-10 stsp
5201 a5d43cac 2022-07-01 thomas view_border(view);
5202 84451b3e 2018-07-10 stsp
5203 84451b3e 2018-07-10 stsp return NULL;
5204 84451b3e 2018-07-10 stsp }
5205 84451b3e 2018-07-10 stsp
5206 84451b3e 2018-07-10 stsp static const struct got_error *
5207 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5208 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5209 84451b3e 2018-07-10 stsp {
5210 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5211 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5212 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5213 1a76625f 2018-10-22 stsp int errcode;
5214 84451b3e 2018-07-10 stsp
5215 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5216 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5217 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5218 84451b3e 2018-07-10 stsp
5219 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5220 1a76625f 2018-10-22 stsp if (errcode)
5221 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5222 84451b3e 2018-07-10 stsp
5223 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5224 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5225 d68a0a7d 2018-07-10 stsp goto done;
5226 d68a0a7d 2018-07-10 stsp }
5227 d68a0a7d 2018-07-10 stsp
5228 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5229 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5230 d68a0a7d 2018-07-10 stsp
5231 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5232 d68a0a7d 2018-07-10 stsp if (line->annotated)
5233 d68a0a7d 2018-07-10 stsp goto done;
5234 d68a0a7d 2018-07-10 stsp
5235 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5236 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5237 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5238 84451b3e 2018-07-10 stsp goto done;
5239 84451b3e 2018-07-10 stsp }
5240 84451b3e 2018-07-10 stsp line->annotated = 1;
5241 84451b3e 2018-07-10 stsp done:
5242 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5243 1a76625f 2018-10-22 stsp if (errcode)
5244 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5245 84451b3e 2018-07-10 stsp return err;
5246 84451b3e 2018-07-10 stsp }
5247 84451b3e 2018-07-10 stsp
5248 84451b3e 2018-07-10 stsp static void *
5249 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5250 84451b3e 2018-07-10 stsp {
5251 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5252 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5253 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5254 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5255 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5256 00a8878e 2022-07-01 thomas
5257 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5258 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5259 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5260 9117a7b7 2022-07-01 thomas
5261 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5262 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5263 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5264 9117a7b7 2022-07-01 thomas goto done;
5265 9117a7b7 2022-07-01 thomas }
5266 18430de3 2018-07-10 stsp
5267 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5268 9117a7b7 2022-07-01 thomas if (f1 == 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 f2 = got_opentemp();
5273 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5274 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5275 9117a7b7 2022-07-01 thomas goto done;
5276 9117a7b7 2022-07-01 thomas }
5277 9117a7b7 2022-07-01 thomas
5278 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5279 61266923 2020-01-14 stsp if (err)
5280 9117a7b7 2022-07-01 thomas goto done;
5281 61266923 2020-01-14 stsp
5282 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5283 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5284 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5285 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5286 fc06ba56 2019-08-22 stsp err = NULL;
5287 18430de3 2018-07-10 stsp
5288 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5289 9117a7b7 2022-07-01 thomas if (errcode) {
5290 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5291 9117a7b7 2022-07-01 thomas goto done;
5292 9117a7b7 2022-07-01 thomas }
5293 18430de3 2018-07-10 stsp
5294 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5295 1d0f4054 2021-06-17 stsp if (err == NULL)
5296 1d0f4054 2021-06-17 stsp err = close_err;
5297 c9beca56 2018-07-22 stsp ta->repo = NULL;
5298 c9beca56 2018-07-22 stsp *ta->complete = 1;
5299 18430de3 2018-07-10 stsp
5300 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5301 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5302 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5303 18430de3 2018-07-10 stsp
5304 9117a7b7 2022-07-01 thomas done:
5305 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5306 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5307 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5308 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5309 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5310 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5311 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5312 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5313 00a8878e 2022-07-01 thomas
5314 18430de3 2018-07-10 stsp return (void *)err;
5315 84451b3e 2018-07-10 stsp }
5316 84451b3e 2018-07-10 stsp
5317 245d91c1 2018-07-12 stsp static struct got_object_id *
5318 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5319 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5320 245d91c1 2018-07-12 stsp {
5321 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5322 8d0fe45a 2019-08-12 stsp
5323 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5324 8d0fe45a 2019-08-12 stsp return NULL;
5325 b880a918 2018-07-10 stsp
5326 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5327 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5328 4fc71f3b 2022-07-12 thomas return NULL;
5329 4fc71f3b 2022-07-12 thomas
5330 4fc71f3b 2022-07-12 thomas return line->id;
5331 4fc71f3b 2022-07-12 thomas }
5332 4fc71f3b 2022-07-12 thomas
5333 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5334 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5335 4fc71f3b 2022-07-12 thomas int lineno)
5336 4fc71f3b 2022-07-12 thomas {
5337 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5338 4fc71f3b 2022-07-12 thomas
5339 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5340 4fc71f3b 2022-07-12 thomas return NULL;
5341 4fc71f3b 2022-07-12 thomas
5342 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5343 245d91c1 2018-07-12 stsp if (!line->annotated)
5344 245d91c1 2018-07-12 stsp return NULL;
5345 245d91c1 2018-07-12 stsp
5346 245d91c1 2018-07-12 stsp return line->id;
5347 b880a918 2018-07-10 stsp }
5348 245d91c1 2018-07-12 stsp
5349 b880a918 2018-07-10 stsp static const struct got_error *
5350 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5351 a70480e0 2018-06-23 stsp {
5352 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5353 245d91c1 2018-07-12 stsp int i;
5354 245d91c1 2018-07-12 stsp
5355 245d91c1 2018-07-12 stsp if (blame->thread) {
5356 1a76625f 2018-10-22 stsp int errcode;
5357 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5358 1a76625f 2018-10-22 stsp if (errcode)
5359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5360 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5361 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5362 1a76625f 2018-10-22 stsp if (errcode)
5363 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5364 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5365 1a76625f 2018-10-22 stsp if (errcode)
5366 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5367 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5368 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5369 245d91c1 2018-07-12 stsp err = NULL;
5370 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5371 245d91c1 2018-07-12 stsp }
5372 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5373 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5374 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5375 1d0f4054 2021-06-17 stsp if (err == NULL)
5376 1d0f4054 2021-06-17 stsp err = close_err;
5377 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5378 245d91c1 2018-07-12 stsp }
5379 245d91c1 2018-07-12 stsp if (blame->f) {
5380 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5381 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5382 245d91c1 2018-07-12 stsp blame->f = NULL;
5383 245d91c1 2018-07-12 stsp }
5384 57670559 2018-12-23 stsp if (blame->lines) {
5385 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5386 57670559 2018-12-23 stsp free(blame->lines[i].id);
5387 57670559 2018-12-23 stsp free(blame->lines);
5388 57670559 2018-12-23 stsp blame->lines = NULL;
5389 57670559 2018-12-23 stsp }
5390 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5391 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5392 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5393 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5394 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5395 7cd52833 2022-06-23 thomas if (err == NULL)
5396 7cd52833 2022-06-23 thomas err = pack_err;
5397 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5398 7cd52833 2022-06-23 thomas }
5399 245d91c1 2018-07-12 stsp return err;
5400 245d91c1 2018-07-12 stsp }
5401 245d91c1 2018-07-12 stsp
5402 245d91c1 2018-07-12 stsp static const struct got_error *
5403 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5404 fc06ba56 2019-08-22 stsp {
5405 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5406 fc06ba56 2019-08-22 stsp int *done = arg;
5407 fc06ba56 2019-08-22 stsp int errcode;
5408 fc06ba56 2019-08-22 stsp
5409 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5410 fc06ba56 2019-08-22 stsp if (errcode)
5411 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5412 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5413 fc06ba56 2019-08-22 stsp
5414 fc06ba56 2019-08-22 stsp if (*done)
5415 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5416 fc06ba56 2019-08-22 stsp
5417 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5418 fc06ba56 2019-08-22 stsp if (errcode)
5419 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5420 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5421 fc06ba56 2019-08-22 stsp
5422 fc06ba56 2019-08-22 stsp return err;
5423 fc06ba56 2019-08-22 stsp }
5424 fc06ba56 2019-08-22 stsp
5425 fc06ba56 2019-08-22 stsp static const struct got_error *
5426 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5427 245d91c1 2018-07-12 stsp {
5428 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5429 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5430 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5431 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5432 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5433 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5434 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5435 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5436 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5437 a70480e0 2018-06-23 stsp
5438 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5439 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5440 27d434c2 2018-09-15 stsp if (err)
5441 15a94983 2018-12-23 stsp return err;
5442 f4ae6ddb 2022-07-01 thomas
5443 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5444 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5445 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5446 f4ae6ddb 2022-07-01 thomas goto done;
5447 f4ae6ddb 2022-07-01 thomas }
5448 945f9229 2022-04-16 thomas
5449 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5450 945f9229 2022-04-16 thomas if (err)
5451 945f9229 2022-04-16 thomas goto done;
5452 27d434c2 2018-09-15 stsp
5453 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5454 84451b3e 2018-07-10 stsp if (err)
5455 84451b3e 2018-07-10 stsp goto done;
5456 27d434c2 2018-09-15 stsp
5457 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5458 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5459 84451b3e 2018-07-10 stsp goto done;
5460 84451b3e 2018-07-10 stsp }
5461 a70480e0 2018-06-23 stsp
5462 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5463 a70480e0 2018-06-23 stsp if (err)
5464 a70480e0 2018-06-23 stsp goto done;
5465 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5466 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5467 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5468 84451b3e 2018-07-10 stsp goto done;
5469 84451b3e 2018-07-10 stsp }
5470 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5471 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5472 1fddf795 2021-01-20 stsp if (err)
5473 1fddf795 2021-01-20 stsp goto done;
5474 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5475 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5476 84451b3e 2018-07-10 stsp goto done;
5477 1fddf795 2021-01-20 stsp }
5478 b02560ec 2019-08-19 stsp
5479 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5480 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5481 b02560ec 2019-08-19 stsp blame->nlines--;
5482 a70480e0 2018-06-23 stsp
5483 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5484 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5485 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5486 84451b3e 2018-07-10 stsp goto done;
5487 84451b3e 2018-07-10 stsp }
5488 a70480e0 2018-06-23 stsp
5489 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5490 bd24772e 2018-07-11 stsp if (err)
5491 bd24772e 2018-07-11 stsp goto done;
5492 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5493 7cd52833 2022-06-23 thomas pack_fds);
5494 7cd52833 2022-06-23 thomas if (err)
5495 7cd52833 2022-06-23 thomas goto done;
5496 bd24772e 2018-07-11 stsp
5497 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5498 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5499 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5500 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5501 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5502 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5503 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5504 245d91c1 2018-07-12 stsp goto done;
5505 245d91c1 2018-07-12 stsp }
5506 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5507 245d91c1 2018-07-12 stsp
5508 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5509 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5510 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5511 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5512 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5513 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5514 a5388363 2020-12-01 naddy s->blame_complete = 0;
5515 f5a09613 2020-12-13 naddy
5516 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5517 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5518 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5519 f5a09613 2020-12-13 naddy s->selected_line = 1;
5520 f5a09613 2020-12-13 naddy }
5521 aa61903a 2021-12-31 thomas s->matched_line = 0;
5522 245d91c1 2018-07-12 stsp
5523 245d91c1 2018-07-12 stsp done:
5524 945f9229 2022-04-16 thomas if (commit)
5525 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5526 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5527 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5528 245d91c1 2018-07-12 stsp if (blob)
5529 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5530 27d434c2 2018-09-15 stsp free(obj_id);
5531 245d91c1 2018-07-12 stsp if (err)
5532 245d91c1 2018-07-12 stsp stop_blame(blame);
5533 245d91c1 2018-07-12 stsp return err;
5534 245d91c1 2018-07-12 stsp }
5535 245d91c1 2018-07-12 stsp
5536 245d91c1 2018-07-12 stsp static const struct got_error *
5537 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5538 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5539 245d91c1 2018-07-12 stsp {
5540 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5541 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5542 dbc6a6b6 2018-07-12 stsp
5543 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5544 245d91c1 2018-07-12 stsp
5545 c4843652 2019-08-12 stsp s->path = strdup(path);
5546 c4843652 2019-08-12 stsp if (s->path == NULL)
5547 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5548 c4843652 2019-08-12 stsp
5549 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5550 c4843652 2019-08-12 stsp if (err) {
5551 c4843652 2019-08-12 stsp free(s->path);
5552 7cbe629d 2018-08-04 stsp return err;
5553 c4843652 2019-08-12 stsp }
5554 245d91c1 2018-07-12 stsp
5555 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5556 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5557 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5558 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5559 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5560 fb2756b9 2018-08-04 stsp s->repo = repo;
5561 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5562 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5563 7cbe629d 2018-08-04 stsp
5564 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5565 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5566 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5567 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5568 11b20872 2019-11-08 stsp if (err)
5569 11b20872 2019-11-08 stsp return err;
5570 11b20872 2019-11-08 stsp }
5571 11b20872 2019-11-08 stsp
5572 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5573 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5574 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5575 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5576 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5577 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5578 e5a0f69f 2018-08-18 stsp
5579 a5388363 2020-12-01 naddy return run_blame(view);
5580 7cbe629d 2018-08-04 stsp }
5581 7cbe629d 2018-08-04 stsp
5582 e5a0f69f 2018-08-18 stsp static const struct got_error *
5583 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5584 7cbe629d 2018-08-04 stsp {
5585 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5586 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5587 7cbe629d 2018-08-04 stsp
5588 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5589 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5590 e5a0f69f 2018-08-18 stsp
5591 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5592 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5593 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5594 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5595 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5596 7cbe629d 2018-08-04 stsp }
5597 e5a0f69f 2018-08-18 stsp
5598 e5a0f69f 2018-08-18 stsp free(s->path);
5599 11b20872 2019-11-08 stsp free_colors(&s->colors);
5600 e5a0f69f 2018-08-18 stsp return err;
5601 7cbe629d 2018-08-04 stsp }
5602 7cbe629d 2018-08-04 stsp
5603 7cbe629d 2018-08-04 stsp static const struct got_error *
5604 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5605 6c4c42e0 2019-06-24 stsp {
5606 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5607 6c4c42e0 2019-06-24 stsp
5608 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5609 6c4c42e0 2019-06-24 stsp return NULL;
5610 6c4c42e0 2019-06-24 stsp }
5611 6c4c42e0 2019-06-24 stsp
5612 6c4c42e0 2019-06-24 stsp static const struct got_error *
5613 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5614 6c4c42e0 2019-06-24 stsp {
5615 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5616 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5617 6c4c42e0 2019-06-24 stsp int lineno;
5618 78eecffc 2022-06-23 thomas char *line = NULL;
5619 826082fe 2020-12-10 stsp size_t linesize = 0;
5620 826082fe 2020-12-10 stsp ssize_t linelen;
5621 6c4c42e0 2019-06-24 stsp
5622 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5623 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5624 6c4c42e0 2019-06-24 stsp return NULL;
5625 6c4c42e0 2019-06-24 stsp }
5626 6c4c42e0 2019-06-24 stsp
5627 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5628 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5629 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5630 6c4c42e0 2019-06-24 stsp else
5631 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5632 4b3f9dac 2021-12-17 thomas } else
5633 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5634 6c4c42e0 2019-06-24 stsp
5635 6c4c42e0 2019-06-24 stsp while (1) {
5636 6c4c42e0 2019-06-24 stsp off_t offset;
5637 6c4c42e0 2019-06-24 stsp
5638 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5639 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5640 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5641 6c4c42e0 2019-06-24 stsp break;
5642 6c4c42e0 2019-06-24 stsp }
5643 2246482e 2019-06-25 stsp
5644 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5645 6c4c42e0 2019-06-24 stsp lineno = 1;
5646 6c4c42e0 2019-06-24 stsp else
5647 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5648 6c4c42e0 2019-06-24 stsp }
5649 6c4c42e0 2019-06-24 stsp
5650 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5651 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5652 6c4c42e0 2019-06-24 stsp free(line);
5653 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5654 6c4c42e0 2019-06-24 stsp }
5655 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5656 78eecffc 2022-06-23 thomas if (linelen != -1) {
5657 78eecffc 2022-06-23 thomas char *exstr;
5658 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5659 78eecffc 2022-06-23 thomas if (err)
5660 78eecffc 2022-06-23 thomas break;
5661 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5662 78eecffc 2022-06-23 thomas &view->regmatch)) {
5663 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5664 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5665 78eecffc 2022-06-23 thomas free(exstr);
5666 78eecffc 2022-06-23 thomas break;
5667 78eecffc 2022-06-23 thomas }
5668 78eecffc 2022-06-23 thomas free(exstr);
5669 6c4c42e0 2019-06-24 stsp }
5670 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5671 6c4c42e0 2019-06-24 stsp lineno++;
5672 6c4c42e0 2019-06-24 stsp else
5673 6c4c42e0 2019-06-24 stsp lineno--;
5674 6c4c42e0 2019-06-24 stsp }
5675 826082fe 2020-12-10 stsp free(line);
5676 6c4c42e0 2019-06-24 stsp
5677 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5678 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5679 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5680 6c4c42e0 2019-06-24 stsp }
5681 6c4c42e0 2019-06-24 stsp
5682 05171be4 2022-06-23 thomas return err;
5683 6c4c42e0 2019-06-24 stsp }
5684 6c4c42e0 2019-06-24 stsp
5685 6c4c42e0 2019-06-24 stsp static const struct got_error *
5686 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5687 7cbe629d 2018-08-04 stsp {
5688 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5689 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5690 2b380cc8 2018-10-24 stsp int errcode;
5691 2b380cc8 2018-10-24 stsp
5692 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5693 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5694 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5695 2b380cc8 2018-10-24 stsp if (errcode)
5696 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5697 51fe7530 2019-08-19 stsp
5698 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5699 2b380cc8 2018-10-24 stsp }
5700 e5a0f69f 2018-08-18 stsp
5701 51fe7530 2019-08-19 stsp if (s->blame_complete)
5702 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5703 51fe7530 2019-08-19 stsp
5704 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5705 e5a0f69f 2018-08-18 stsp
5706 a5d43cac 2022-07-01 thomas view_border(view);
5707 e5a0f69f 2018-08-18 stsp return err;
5708 e5a0f69f 2018-08-18 stsp }
5709 e5a0f69f 2018-08-18 stsp
5710 e5a0f69f 2018-08-18 stsp static const struct got_error *
5711 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5712 e5a0f69f 2018-08-18 stsp {
5713 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5714 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5715 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5716 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5717 a5d43cac 2022-07-01 thomas
5718 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5719 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5720 a5d43cac 2022-07-01 thomas --eos; /* border */
5721 7cbe629d 2018-08-04 stsp
5722 e5a0f69f 2018-08-18 stsp switch (ch) {
5723 05171be4 2022-06-23 thomas case '0':
5724 05171be4 2022-06-23 thomas view->x = 0;
5725 05171be4 2022-06-23 thomas break;
5726 05171be4 2022-06-23 thomas case '$':
5727 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5728 07b0611c 2022-06-23 thomas view->count = 0;
5729 05171be4 2022-06-23 thomas break;
5730 05171be4 2022-06-23 thomas case KEY_RIGHT:
5731 05171be4 2022-06-23 thomas case 'l':
5732 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5733 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5734 07b0611c 2022-06-23 thomas else
5735 07b0611c 2022-06-23 thomas view->count = 0;
5736 05171be4 2022-06-23 thomas break;
5737 05171be4 2022-06-23 thomas case KEY_LEFT:
5738 05171be4 2022-06-23 thomas case 'h':
5739 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5740 07b0611c 2022-06-23 thomas if (view->x <= 0)
5741 07b0611c 2022-06-23 thomas view->count = 0;
5742 05171be4 2022-06-23 thomas break;
5743 1e37a5c2 2019-05-12 jcs case 'q':
5744 1e37a5c2 2019-05-12 jcs s->done = 1;
5745 4deef56f 2021-09-02 naddy break;
5746 4deef56f 2021-09-02 naddy case 'g':
5747 4deef56f 2021-09-02 naddy case KEY_HOME:
5748 4deef56f 2021-09-02 naddy s->selected_line = 1;
5749 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5750 07b0611c 2022-06-23 thomas view->count = 0;
5751 4deef56f 2021-09-02 naddy break;
5752 4deef56f 2021-09-02 naddy case 'G':
5753 4deef56f 2021-09-02 naddy case KEY_END:
5754 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5755 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5756 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5757 4deef56f 2021-09-02 naddy } else {
5758 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5759 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5760 4deef56f 2021-09-02 naddy }
5761 07b0611c 2022-06-23 thomas view->count = 0;
5762 1e37a5c2 2019-05-12 jcs break;
5763 1e37a5c2 2019-05-12 jcs case 'k':
5764 1e37a5c2 2019-05-12 jcs case KEY_UP:
5765 f7140bf5 2021-10-17 thomas case CTRL('p'):
5766 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5767 1e37a5c2 2019-05-12 jcs s->selected_line--;
5768 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5769 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5771 07b0611c 2022-06-23 thomas else
5772 07b0611c 2022-06-23 thomas view->count = 0;
5773 1e37a5c2 2019-05-12 jcs break;
5774 70f17a53 2022-06-13 thomas case CTRL('u'):
5775 23427b14 2022-06-23 thomas case 'u':
5776 70f17a53 2022-06-13 thomas nscroll /= 2;
5777 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5778 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5779 ea025d1d 2020-02-22 naddy case CTRL('b'):
5780 1c5e5faa 2022-06-23 thomas case 'b':
5781 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5782 07b0611c 2022-06-23 thomas if (view->count > 1)
5783 07b0611c 2022-06-23 thomas nscroll += nscroll;
5784 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5785 07b0611c 2022-06-23 thomas view->count = 0;
5786 e5a0f69f 2018-08-18 stsp break;
5787 1e37a5c2 2019-05-12 jcs }
5788 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5789 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5790 1e37a5c2 2019-05-12 jcs else
5791 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5792 1e37a5c2 2019-05-12 jcs break;
5793 1e37a5c2 2019-05-12 jcs case 'j':
5794 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5795 f7140bf5 2021-10-17 thomas case CTRL('n'):
5796 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5797 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5798 1e37a5c2 2019-05-12 jcs s->selected_line++;
5799 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5800 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5801 07b0611c 2022-06-23 thomas else
5802 07b0611c 2022-06-23 thomas view->count = 0;
5803 1e37a5c2 2019-05-12 jcs break;
5804 1c5e5faa 2022-06-23 thomas case 'c':
5805 1e37a5c2 2019-05-12 jcs case 'p': {
5806 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5807 07b0611c 2022-06-23 thomas
5808 07b0611c 2022-06-23 thomas view->count = 0;
5809 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5810 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5811 1e37a5c2 2019-05-12 jcs if (id == NULL)
5812 e5a0f69f 2018-08-18 stsp break;
5813 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5814 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5815 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5816 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5817 1e37a5c2 2019-05-12 jcs int obj_type;
5818 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5819 1e37a5c2 2019-05-12 jcs s->repo, id);
5820 e5a0f69f 2018-08-18 stsp if (err)
5821 e5a0f69f 2018-08-18 stsp break;
5822 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5823 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5824 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5825 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5826 e5a0f69f 2018-08-18 stsp break;
5827 e5a0f69f 2018-08-18 stsp }
5828 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5829 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5830 ec242592 2022-04-22 thomas s->repo, &pid->id);
5831 945f9229 2022-04-16 thomas if (err)
5832 945f9229 2022-04-16 thomas break;
5833 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5834 945f9229 2022-04-16 thomas pcommit, s->path);
5835 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5836 e5a0f69f 2018-08-18 stsp if (err) {
5837 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5838 1e37a5c2 2019-05-12 jcs err = NULL;
5839 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5840 e5a0f69f 2018-08-18 stsp break;
5841 e5a0f69f 2018-08-18 stsp }
5842 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5843 1e37a5c2 2019-05-12 jcs blob_id);
5844 1e37a5c2 2019-05-12 jcs free(blob_id);
5845 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5846 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5847 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5848 e5a0f69f 2018-08-18 stsp break;
5849 1e37a5c2 2019-05-12 jcs }
5850 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5851 ec242592 2022-04-22 thomas &pid->id);
5852 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5853 1e37a5c2 2019-05-12 jcs } else {
5854 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5855 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5856 1e37a5c2 2019-05-12 jcs break;
5857 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5858 1e37a5c2 2019-05-12 jcs id);
5859 1e37a5c2 2019-05-12 jcs }
5860 1e37a5c2 2019-05-12 jcs if (err)
5861 e5a0f69f 2018-08-18 stsp break;
5862 1e37a5c2 2019-05-12 jcs s->done = 1;
5863 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5864 1e37a5c2 2019-05-12 jcs s->done = 0;
5865 1e37a5c2 2019-05-12 jcs if (thread_err)
5866 1e37a5c2 2019-05-12 jcs break;
5867 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5868 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5869 a5388363 2020-12-01 naddy err = run_blame(view);
5870 1e37a5c2 2019-05-12 jcs if (err)
5871 1e37a5c2 2019-05-12 jcs break;
5872 1e37a5c2 2019-05-12 jcs break;
5873 1e37a5c2 2019-05-12 jcs }
5874 1c5e5faa 2022-06-23 thomas case 'C': {
5875 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5876 07b0611c 2022-06-23 thomas
5877 07b0611c 2022-06-23 thomas view->count = 0;
5878 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5879 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5880 1e37a5c2 2019-05-12 jcs break;
5881 1e37a5c2 2019-05-12 jcs s->done = 1;
5882 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5883 1e37a5c2 2019-05-12 jcs s->done = 0;
5884 1e37a5c2 2019-05-12 jcs if (thread_err)
5885 1e37a5c2 2019-05-12 jcs break;
5886 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5887 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5888 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5889 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5890 a5388363 2020-12-01 naddy err = run_blame(view);
5891 1e37a5c2 2019-05-12 jcs if (err)
5892 1e37a5c2 2019-05-12 jcs break;
5893 1e37a5c2 2019-05-12 jcs break;
5894 1e37a5c2 2019-05-12 jcs }
5895 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5896 1e37a5c2 2019-05-12 jcs case '\r': {
5897 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5898 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5899 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5900 07b0611c 2022-06-23 thomas
5901 07b0611c 2022-06-23 thomas view->count = 0;
5902 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5903 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5904 1e37a5c2 2019-05-12 jcs if (id == NULL)
5905 1e37a5c2 2019-05-12 jcs break;
5906 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5907 1e37a5c2 2019-05-12 jcs if (err)
5908 1e37a5c2 2019-05-12 jcs break;
5909 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5910 4fc71f3b 2022-07-12 thomas if (*new_view) {
5911 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5912 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5913 4fc71f3b 2022-07-12 thomas if (err)
5914 4fc71f3b 2022-07-12 thomas break;
5915 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
5916 4fc71f3b 2022-07-12 thomas } else {
5917 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
5918 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
5919 a5d43cac 2022-07-01 thomas
5920 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
5921 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
5922 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
5923 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
5924 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
5925 4fc71f3b 2022-07-12 thomas break;
5926 4fc71f3b 2022-07-12 thomas }
5927 15a94983 2018-12-23 stsp }
5928 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5929 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
5930 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5931 1e37a5c2 2019-05-12 jcs if (err) {
5932 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5933 1e37a5c2 2019-05-12 jcs break;
5934 1e37a5c2 2019-05-12 jcs }
5935 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
5936 4fc71f3b 2022-07-12 thomas s->selected_line;
5937 4fc71f3b 2022-07-12 thomas if (*new_view)
5938 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
5939 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5940 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5941 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5942 a5d43cac 2022-07-01 thomas if (err)
5943 a5d43cac 2022-07-01 thomas break;
5944 a5d43cac 2022-07-01 thomas }
5945 a5d43cac 2022-07-01 thomas
5946 e78dc838 2020-12-04 stsp view->focussed = 0;
5947 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5948 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5949 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5950 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5951 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
5952 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5953 1e37a5c2 2019-05-12 jcs if (err)
5954 34bc9ec9 2019-02-22 stsp break;
5955 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5956 40236d76 2022-06-23 thomas if (err)
5957 40236d76 2022-06-23 thomas break;
5958 e78dc838 2020-12-04 stsp view->focus_child = 1;
5959 1e37a5c2 2019-05-12 jcs } else
5960 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5961 1e37a5c2 2019-05-12 jcs if (err)
5962 e5a0f69f 2018-08-18 stsp break;
5963 1e37a5c2 2019-05-12 jcs break;
5964 1e37a5c2 2019-05-12 jcs }
5965 70f17a53 2022-06-13 thomas case CTRL('d'):
5966 23427b14 2022-06-23 thomas case 'd':
5967 70f17a53 2022-06-13 thomas nscroll /= 2;
5968 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5969 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5970 ea025d1d 2020-02-22 naddy case CTRL('f'):
5971 1c5e5faa 2022-06-23 thomas case 'f':
5972 1e37a5c2 2019-05-12 jcs case ' ':
5973 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5974 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5975 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5976 07b0611c 2022-06-23 thomas view->count = 0;
5977 e5a0f69f 2018-08-18 stsp break;
5978 1e37a5c2 2019-05-12 jcs }
5979 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5980 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5981 70f17a53 2022-06-13 thomas s->selected_line +=
5982 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5983 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5984 1e37a5c2 2019-05-12 jcs }
5985 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5986 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5987 1e37a5c2 2019-05-12 jcs else
5988 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5989 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5990 1e37a5c2 2019-05-12 jcs break;
5991 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5992 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5993 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5994 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5995 1e37a5c2 2019-05-12 jcs }
5996 1e37a5c2 2019-05-12 jcs break;
5997 1e37a5c2 2019-05-12 jcs default:
5998 07b0611c 2022-06-23 thomas view->count = 0;
5999 1e37a5c2 2019-05-12 jcs break;
6000 a70480e0 2018-06-23 stsp }
6001 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6002 adf4c9e0 2022-07-03 thomas }
6003 adf4c9e0 2022-07-03 thomas
6004 adf4c9e0 2022-07-03 thomas static const struct got_error *
6005 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6006 adf4c9e0 2022-07-03 thomas {
6007 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6008 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6009 adf4c9e0 2022-07-03 thomas
6010 adf4c9e0 2022-07-03 thomas view->count = 0;
6011 adf4c9e0 2022-07-03 thomas s->done = 1;
6012 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6013 adf4c9e0 2022-07-03 thomas s->done = 0;
6014 adf4c9e0 2022-07-03 thomas if (err)
6015 adf4c9e0 2022-07-03 thomas return err;
6016 adf4c9e0 2022-07-03 thomas return run_blame(view);
6017 a70480e0 2018-06-23 stsp }
6018 a70480e0 2018-06-23 stsp
6019 a70480e0 2018-06-23 stsp static const struct got_error *
6020 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6021 9f7d7167 2018-04-29 stsp {
6022 a70480e0 2018-06-23 stsp const struct got_error *error;
6023 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6024 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6025 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6026 0587e10c 2020-07-23 stsp char *link_target = NULL;
6027 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6028 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6029 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6030 a70480e0 2018-06-23 stsp int ch;
6031 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6032 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6033 a70480e0 2018-06-23 stsp
6034 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6035 a70480e0 2018-06-23 stsp switch (ch) {
6036 a70480e0 2018-06-23 stsp case 'c':
6037 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6038 a70480e0 2018-06-23 stsp break;
6039 69069811 2018-08-02 stsp case 'r':
6040 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6041 69069811 2018-08-02 stsp if (repo_path == NULL)
6042 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6043 9ba1d308 2019-10-21 stsp optarg);
6044 69069811 2018-08-02 stsp break;
6045 a70480e0 2018-06-23 stsp default:
6046 17020d27 2019-03-07 stsp usage_blame();
6047 a70480e0 2018-06-23 stsp /* NOTREACHED */
6048 a70480e0 2018-06-23 stsp }
6049 a70480e0 2018-06-23 stsp }
6050 a70480e0 2018-06-23 stsp
6051 a70480e0 2018-06-23 stsp argc -= optind;
6052 a70480e0 2018-06-23 stsp argv += optind;
6053 a70480e0 2018-06-23 stsp
6054 f135c941 2020-02-20 stsp if (argc != 1)
6055 a70480e0 2018-06-23 stsp usage_blame();
6056 6962eb72 2020-02-20 stsp
6057 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6058 7cd52833 2022-06-23 thomas if (error != NULL)
6059 7cd52833 2022-06-23 thomas goto done;
6060 7cd52833 2022-06-23 thomas
6061 69069811 2018-08-02 stsp if (repo_path == NULL) {
6062 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6063 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6064 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6065 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6066 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6067 c156c7a4 2020-12-18 stsp goto done;
6068 f135c941 2020-02-20 stsp if (worktree)
6069 eb41ed75 2019-02-05 stsp repo_path =
6070 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6071 f135c941 2020-02-20 stsp else
6072 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6073 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6074 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6075 c156c7a4 2020-12-18 stsp goto done;
6076 c156c7a4 2020-12-18 stsp }
6077 f135c941 2020-02-20 stsp }
6078 a915003a 2019-02-05 stsp
6079 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6080 c02c541e 2019-03-29 stsp if (error != NULL)
6081 8e94dd5b 2019-01-04 stsp goto done;
6082 69069811 2018-08-02 stsp
6083 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6084 f135c941 2020-02-20 stsp worktree);
6085 c02c541e 2019-03-29 stsp if (error)
6086 92205607 2019-01-04 stsp goto done;
6087 69069811 2018-08-02 stsp
6088 f135c941 2020-02-20 stsp init_curses();
6089 f135c941 2020-02-20 stsp
6090 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6091 51a10b52 2020-12-26 stsp if (error)
6092 51a10b52 2020-12-26 stsp goto done;
6093 51a10b52 2020-12-26 stsp
6094 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6095 eb41ed75 2019-02-05 stsp if (error)
6096 69069811 2018-08-02 stsp goto done;
6097 a70480e0 2018-06-23 stsp
6098 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6099 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6100 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6101 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6102 a70480e0 2018-06-23 stsp if (error != NULL)
6103 66b4983c 2018-06-23 stsp goto done;
6104 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6105 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6106 a70480e0 2018-06-23 stsp } else {
6107 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6108 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6109 a70480e0 2018-06-23 stsp }
6110 a19e88aa 2018-06-23 stsp if (error != NULL)
6111 8b473291 2019-02-21 stsp goto done;
6112 8b473291 2019-02-21 stsp
6113 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6114 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6115 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6116 e1cd8fed 2018-08-01 stsp goto done;
6117 e1cd8fed 2018-08-01 stsp }
6118 0587e10c 2020-07-23 stsp
6119 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6120 945f9229 2022-04-16 thomas if (error)
6121 945f9229 2022-04-16 thomas goto done;
6122 945f9229 2022-04-16 thomas
6123 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6124 945f9229 2022-04-16 thomas commit, repo);
6125 7cbe629d 2018-08-04 stsp if (error)
6126 7cbe629d 2018-08-04 stsp goto done;
6127 0587e10c 2020-07-23 stsp
6128 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6129 78756c87 2020-11-24 stsp commit_id, repo);
6130 0587e10c 2020-07-23 stsp if (error)
6131 0587e10c 2020-07-23 stsp goto done;
6132 12314ad4 2019-08-31 stsp if (worktree) {
6133 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6134 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6135 12314ad4 2019-08-31 stsp worktree = NULL;
6136 12314ad4 2019-08-31 stsp }
6137 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6138 a70480e0 2018-06-23 stsp done:
6139 69069811 2018-08-02 stsp free(repo_path);
6140 f135c941 2020-02-20 stsp free(in_repo_path);
6141 0587e10c 2020-07-23 stsp free(link_target);
6142 69069811 2018-08-02 stsp free(cwd);
6143 a70480e0 2018-06-23 stsp free(commit_id);
6144 945f9229 2022-04-16 thomas if (commit)
6145 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6146 eb41ed75 2019-02-05 stsp if (worktree)
6147 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6148 1d0f4054 2021-06-17 stsp if (repo) {
6149 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6150 1d0f4054 2021-06-17 stsp if (error == NULL)
6151 1d0f4054 2021-06-17 stsp error = close_err;
6152 1d0f4054 2021-06-17 stsp }
6153 7cd52833 2022-06-23 thomas if (pack_fds) {
6154 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6155 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6156 7cd52833 2022-06-23 thomas if (error == NULL)
6157 7cd52833 2022-06-23 thomas error = pack_err;
6158 7cd52833 2022-06-23 thomas }
6159 51a10b52 2020-12-26 stsp tog_free_refs();
6160 a70480e0 2018-06-23 stsp return error;
6161 ffd1d5e5 2018-06-23 stsp }
6162 ffd1d5e5 2018-06-23 stsp
6163 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6164 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6165 ffd1d5e5 2018-06-23 stsp {
6166 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6167 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6168 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6169 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6170 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6171 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6172 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6173 ffd1d5e5 2018-06-23 stsp
6174 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6175 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6176 a5d43cac 2022-07-01 thomas --limit; /* border */
6177 ffd1d5e5 2018-06-23 stsp
6178 f7d12f7e 2018-08-01 stsp werase(view->window);
6179 ffd1d5e5 2018-06-23 stsp
6180 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6181 ffd1d5e5 2018-06-23 stsp return NULL;
6182 ffd1d5e5 2018-06-23 stsp
6183 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6184 f91a2b48 2022-06-23 thomas 0, 0);
6185 ffd1d5e5 2018-06-23 stsp if (err)
6186 ffd1d5e5 2018-06-23 stsp return err;
6187 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6188 a3404814 2018-09-02 stsp wstandout(view->window);
6189 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6190 11b20872 2019-11-08 stsp if (tc)
6191 11b20872 2019-11-08 stsp wattr_on(view->window,
6192 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6193 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6194 11b20872 2019-11-08 stsp if (tc)
6195 11b20872 2019-11-08 stsp wattr_off(view->window,
6196 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6197 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6198 a3404814 2018-09-02 stsp wstandend(view->window);
6199 2550e4c3 2018-07-13 stsp free(wline);
6200 2550e4c3 2018-07-13 stsp wline = NULL;
6201 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6202 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6203 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6204 ffd1d5e5 2018-06-23 stsp return NULL;
6205 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6206 f91a2b48 2022-06-23 thomas 0, 0);
6207 ce52c690 2018-06-23 stsp if (err)
6208 ce52c690 2018-06-23 stsp return err;
6209 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6210 2550e4c3 2018-07-13 stsp free(wline);
6211 2550e4c3 2018-07-13 stsp wline = NULL;
6212 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6213 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6214 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6215 ffd1d5e5 2018-06-23 stsp return NULL;
6216 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6217 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6218 a1eca9bb 2018-06-23 stsp return NULL;
6219 ffd1d5e5 2018-06-23 stsp
6220 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6221 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6222 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6223 0cf4efb1 2018-09-29 stsp if (view->focussed)
6224 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6225 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6226 ffd1d5e5 2018-06-23 stsp }
6227 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6228 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6229 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6230 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6231 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6232 ffd1d5e5 2018-06-23 stsp return NULL;
6233 ffd1d5e5 2018-06-23 stsp n = 1;
6234 ffd1d5e5 2018-06-23 stsp } else {
6235 ffd1d5e5 2018-06-23 stsp n = 0;
6236 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6237 ffd1d5e5 2018-06-23 stsp }
6238 ffd1d5e5 2018-06-23 stsp
6239 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6240 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6241 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6242 848d6979 2019-08-12 stsp const char *modestr = "";
6243 56e0773d 2019-11-28 stsp mode_t mode;
6244 1d13200f 2018-07-12 stsp
6245 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6246 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6247 56e0773d 2019-11-28 stsp
6248 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6249 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6250 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6251 1d13200f 2018-07-12 stsp if (err)
6252 638f9024 2019-05-13 stsp return got_error_from_errno(
6253 230a42bd 2019-05-11 jcs "got_object_id_str");
6254 1d13200f 2018-07-12 stsp }
6255 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6256 63c5ca5d 2019-08-24 stsp modestr = "$";
6257 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6258 0d6c6ee3 2020-05-20 stsp int i;
6259 0d6c6ee3 2020-05-20 stsp
6260 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6261 d86d3b18 2020-12-01 naddy te, s->repo);
6262 0d6c6ee3 2020-05-20 stsp if (err) {
6263 0d6c6ee3 2020-05-20 stsp free(id_str);
6264 0d6c6ee3 2020-05-20 stsp return err;
6265 0d6c6ee3 2020-05-20 stsp }
6266 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6267 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6268 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6269 0d6c6ee3 2020-05-20 stsp }
6270 848d6979 2019-08-12 stsp modestr = "@";
6271 0d6c6ee3 2020-05-20 stsp }
6272 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6273 848d6979 2019-08-12 stsp modestr = "/";
6274 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6275 848d6979 2019-08-12 stsp modestr = "*";
6276 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6277 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6278 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6279 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6280 1d13200f 2018-07-12 stsp free(id_str);
6281 0d6c6ee3 2020-05-20 stsp free(link_target);
6282 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6283 1d13200f 2018-07-12 stsp }
6284 1d13200f 2018-07-12 stsp free(id_str);
6285 0d6c6ee3 2020-05-20 stsp free(link_target);
6286 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6287 f91a2b48 2022-06-23 thomas 0, 0);
6288 ffd1d5e5 2018-06-23 stsp if (err) {
6289 ffd1d5e5 2018-06-23 stsp free(line);
6290 ffd1d5e5 2018-06-23 stsp break;
6291 ffd1d5e5 2018-06-23 stsp }
6292 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6293 0cf4efb1 2018-09-29 stsp if (view->focussed)
6294 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6295 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6296 ffd1d5e5 2018-06-23 stsp }
6297 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6298 f26dddb7 2019-11-08 stsp if (tc)
6299 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6300 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6301 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6302 f26dddb7 2019-11-08 stsp if (tc)
6303 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6304 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6305 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6306 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6307 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6308 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6309 ffd1d5e5 2018-06-23 stsp free(line);
6310 2550e4c3 2018-07-13 stsp free(wline);
6311 2550e4c3 2018-07-13 stsp wline = NULL;
6312 ffd1d5e5 2018-06-23 stsp n++;
6313 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6314 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6315 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6316 ffd1d5e5 2018-06-23 stsp break;
6317 ffd1d5e5 2018-06-23 stsp }
6318 ffd1d5e5 2018-06-23 stsp
6319 ffd1d5e5 2018-06-23 stsp return err;
6320 ffd1d5e5 2018-06-23 stsp }
6321 ffd1d5e5 2018-06-23 stsp
6322 ffd1d5e5 2018-06-23 stsp static void
6323 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6324 ffd1d5e5 2018-06-23 stsp {
6325 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6326 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6327 fa86c4bf 2020-11-29 stsp int i = 0;
6328 ffd1d5e5 2018-06-23 stsp
6329 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6330 ffd1d5e5 2018-06-23 stsp return;
6331 ffd1d5e5 2018-06-23 stsp
6332 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6333 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6334 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6335 fa86c4bf 2020-11-29 stsp if (!isroot)
6336 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6337 fa86c4bf 2020-11-29 stsp break;
6338 fa86c4bf 2020-11-29 stsp }
6339 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6340 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6341 ffd1d5e5 2018-06-23 stsp }
6342 ffd1d5e5 2018-06-23 stsp }
6343 ffd1d5e5 2018-06-23 stsp
6344 a5d43cac 2022-07-01 thomas static const struct got_error *
6345 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6346 ffd1d5e5 2018-06-23 stsp {
6347 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6348 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6349 ffd1d5e5 2018-06-23 stsp int n = 0;
6350 ffd1d5e5 2018-06-23 stsp
6351 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6352 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6353 694d3271 2020-12-01 naddy s->first_displayed_entry);
6354 694d3271 2020-12-01 naddy else
6355 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6356 56e0773d 2019-11-28 stsp
6357 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6358 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6359 a5d43cac 2022-07-01 thomas if (last)
6360 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6361 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6362 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6363 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6364 768394f3 2019-01-24 stsp }
6365 ffd1d5e5 2018-06-23 stsp }
6366 a5d43cac 2022-07-01 thomas
6367 a5d43cac 2022-07-01 thomas return NULL;
6368 ffd1d5e5 2018-06-23 stsp }
6369 ffd1d5e5 2018-06-23 stsp
6370 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6371 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6372 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6373 ffd1d5e5 2018-06-23 stsp {
6374 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6375 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6376 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6377 ffd1d5e5 2018-06-23 stsp
6378 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6379 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6380 56e0773d 2019-11-28 stsp + 1 /* slash */;
6381 ce52c690 2018-06-23 stsp if (te)
6382 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6383 ce52c690 2018-06-23 stsp
6384 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6385 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6386 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6387 ffd1d5e5 2018-06-23 stsp
6388 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6389 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6390 d9765a41 2018-06-23 stsp while (pt) {
6391 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6392 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6393 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6394 cb2ebc8a 2018-06-23 stsp goto done;
6395 cb2ebc8a 2018-06-23 stsp }
6396 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6397 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6398 cb2ebc8a 2018-06-23 stsp goto done;
6399 cb2ebc8a 2018-06-23 stsp }
6400 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6401 ffd1d5e5 2018-06-23 stsp }
6402 ce52c690 2018-06-23 stsp if (te) {
6403 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6404 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6405 ce52c690 2018-06-23 stsp goto done;
6406 ce52c690 2018-06-23 stsp }
6407 cb2ebc8a 2018-06-23 stsp }
6408 ce52c690 2018-06-23 stsp done:
6409 ce52c690 2018-06-23 stsp if (err) {
6410 ce52c690 2018-06-23 stsp free(*path);
6411 ce52c690 2018-06-23 stsp *path = NULL;
6412 ce52c690 2018-06-23 stsp }
6413 ce52c690 2018-06-23 stsp return err;
6414 ce52c690 2018-06-23 stsp }
6415 ce52c690 2018-06-23 stsp
6416 ce52c690 2018-06-23 stsp static const struct got_error *
6417 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6418 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6419 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6420 ce52c690 2018-06-23 stsp {
6421 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6422 ce52c690 2018-06-23 stsp char *path;
6423 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6424 a0de39f3 2019-08-09 stsp
6425 a0de39f3 2019-08-09 stsp *new_view = NULL;
6426 69efd4c4 2018-07-18 stsp
6427 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6428 ce52c690 2018-06-23 stsp if (err)
6429 ce52c690 2018-06-23 stsp return err;
6430 ffd1d5e5 2018-06-23 stsp
6431 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6432 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6433 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6434 83ce39e3 2019-08-12 stsp goto done;
6435 83ce39e3 2019-08-12 stsp }
6436 cdf1ee82 2018-08-01 stsp
6437 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6438 e5a0f69f 2018-08-18 stsp if (err) {
6439 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6440 fc06ba56 2019-08-22 stsp err = NULL;
6441 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6442 e5a0f69f 2018-08-18 stsp } else
6443 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6444 83ce39e3 2019-08-12 stsp done:
6445 83ce39e3 2019-08-12 stsp free(path);
6446 69efd4c4 2018-07-18 stsp return err;
6447 69efd4c4 2018-07-18 stsp }
6448 69efd4c4 2018-07-18 stsp
6449 69efd4c4 2018-07-18 stsp static const struct got_error *
6450 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6451 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6452 69efd4c4 2018-07-18 stsp {
6453 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6454 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6455 69efd4c4 2018-07-18 stsp char *path;
6456 a0de39f3 2019-08-09 stsp
6457 a0de39f3 2019-08-09 stsp *new_view = NULL;
6458 69efd4c4 2018-07-18 stsp
6459 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6460 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6461 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6462 e5a0f69f 2018-08-18 stsp
6463 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6464 69efd4c4 2018-07-18 stsp if (err)
6465 69efd4c4 2018-07-18 stsp return err;
6466 69efd4c4 2018-07-18 stsp
6467 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6468 4e97c21c 2020-12-06 stsp path, 0);
6469 ba4f502b 2018-08-04 stsp if (err)
6470 e5a0f69f 2018-08-18 stsp view_close(log_view);
6471 e5a0f69f 2018-08-18 stsp else
6472 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6473 cb2ebc8a 2018-06-23 stsp free(path);
6474 cb2ebc8a 2018-06-23 stsp return err;
6475 ffd1d5e5 2018-06-23 stsp }
6476 ffd1d5e5 2018-06-23 stsp
6477 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6478 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6479 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6480 ffd1d5e5 2018-06-23 stsp {
6481 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6482 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6483 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6484 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6485 ffd1d5e5 2018-06-23 stsp
6486 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6487 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6488 bc573f3b 2021-07-10 stsp
6489 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6490 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6491 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6492 ffd1d5e5 2018-06-23 stsp
6493 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6494 bc573f3b 2021-07-10 stsp if (err)
6495 bc573f3b 2021-07-10 stsp goto done;
6496 bc573f3b 2021-07-10 stsp
6497 bc573f3b 2021-07-10 stsp /*
6498 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6499 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6500 bc573f3b 2021-07-10 stsp * closed on demand.
6501 bc573f3b 2021-07-10 stsp */
6502 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6503 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6504 bc573f3b 2021-07-10 stsp if (err)
6505 bc573f3b 2021-07-10 stsp goto done;
6506 bc573f3b 2021-07-10 stsp s->tree = s->root;
6507 bc573f3b 2021-07-10 stsp
6508 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6509 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6510 ffd1d5e5 2018-06-23 stsp goto done;
6511 ffd1d5e5 2018-06-23 stsp
6512 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6513 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6514 ffd1d5e5 2018-06-23 stsp goto done;
6515 ffd1d5e5 2018-06-23 stsp }
6516 ffd1d5e5 2018-06-23 stsp
6517 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6518 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6519 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6520 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6521 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6522 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6523 9cd7cbd1 2020-12-07 stsp goto done;
6524 9cd7cbd1 2020-12-07 stsp }
6525 9cd7cbd1 2020-12-07 stsp }
6526 fb2756b9 2018-08-04 stsp s->repo = repo;
6527 c0b01bdb 2019-11-08 stsp
6528 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6529 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6530 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6531 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6532 c0b01bdb 2019-11-08 stsp if (err)
6533 c0b01bdb 2019-11-08 stsp goto done;
6534 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6535 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6536 bc573f3b 2021-07-10 stsp if (err)
6537 c0b01bdb 2019-11-08 stsp goto done;
6538 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6539 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6540 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6541 bc573f3b 2021-07-10 stsp if (err)
6542 c0b01bdb 2019-11-08 stsp goto done;
6543 e5a0f69f 2018-08-18 stsp
6544 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6545 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6546 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6547 bc573f3b 2021-07-10 stsp if (err)
6548 11b20872 2019-11-08 stsp goto done;
6549 11b20872 2019-11-08 stsp
6550 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6551 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6552 bc573f3b 2021-07-10 stsp if (err)
6553 c0b01bdb 2019-11-08 stsp goto done;
6554 c0b01bdb 2019-11-08 stsp }
6555 c0b01bdb 2019-11-08 stsp
6556 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6557 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6558 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6559 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6560 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6561 ad80ab7b 2018-08-04 stsp done:
6562 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6563 bc573f3b 2021-07-10 stsp if (commit)
6564 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6565 bc573f3b 2021-07-10 stsp if (err)
6566 bc573f3b 2021-07-10 stsp close_tree_view(view);
6567 ad80ab7b 2018-08-04 stsp return err;
6568 ad80ab7b 2018-08-04 stsp }
6569 ad80ab7b 2018-08-04 stsp
6570 e5a0f69f 2018-08-18 stsp static const struct got_error *
6571 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6572 ad80ab7b 2018-08-04 stsp {
6573 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6574 ad80ab7b 2018-08-04 stsp
6575 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6576 fb2756b9 2018-08-04 stsp free(s->tree_label);
6577 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6578 6484ec90 2018-09-29 stsp free(s->commit_id);
6579 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6580 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6581 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6582 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6583 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6584 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6585 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6586 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6587 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6588 ad80ab7b 2018-08-04 stsp free(parent);
6589 ad80ab7b 2018-08-04 stsp
6590 ad80ab7b 2018-08-04 stsp }
6591 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6592 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6593 bc573f3b 2021-07-10 stsp if (s->root)
6594 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6595 7c32bd05 2019-06-22 stsp return NULL;
6596 7c32bd05 2019-06-22 stsp }
6597 7c32bd05 2019-06-22 stsp
6598 7c32bd05 2019-06-22 stsp static const struct got_error *
6599 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6600 7c32bd05 2019-06-22 stsp {
6601 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6602 7c32bd05 2019-06-22 stsp
6603 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6604 7c32bd05 2019-06-22 stsp return NULL;
6605 7c32bd05 2019-06-22 stsp }
6606 7c32bd05 2019-06-22 stsp
6607 7c32bd05 2019-06-22 stsp static int
6608 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6609 7c32bd05 2019-06-22 stsp {
6610 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6611 7c32bd05 2019-06-22 stsp
6612 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6613 56e0773d 2019-11-28 stsp 0) == 0;
6614 7c32bd05 2019-06-22 stsp }
6615 7c32bd05 2019-06-22 stsp
6616 7c32bd05 2019-06-22 stsp static const struct got_error *
6617 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6618 7c32bd05 2019-06-22 stsp {
6619 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6620 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6621 7c32bd05 2019-06-22 stsp
6622 7c32bd05 2019-06-22 stsp if (!view->searching) {
6623 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6624 7c32bd05 2019-06-22 stsp return NULL;
6625 7c32bd05 2019-06-22 stsp }
6626 7c32bd05 2019-06-22 stsp
6627 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6628 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6629 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6630 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6631 56e0773d 2019-11-28 stsp s->selected_entry);
6632 7c32bd05 2019-06-22 stsp else
6633 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6634 56e0773d 2019-11-28 stsp } else {
6635 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6636 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6637 56e0773d 2019-11-28 stsp else
6638 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6639 56e0773d 2019-11-28 stsp s->selected_entry);
6640 7c32bd05 2019-06-22 stsp }
6641 7c32bd05 2019-06-22 stsp } else {
6642 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6643 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6644 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6645 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6646 56e0773d 2019-11-28 stsp else
6647 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6648 7c32bd05 2019-06-22 stsp }
6649 7c32bd05 2019-06-22 stsp
6650 7c32bd05 2019-06-22 stsp while (1) {
6651 56e0773d 2019-11-28 stsp if (te == NULL) {
6652 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6653 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6654 ac66afb8 2019-06-24 stsp return NULL;
6655 ac66afb8 2019-06-24 stsp }
6656 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6657 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6658 56e0773d 2019-11-28 stsp else
6659 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6660 7c32bd05 2019-06-22 stsp }
6661 7c32bd05 2019-06-22 stsp
6662 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6663 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6664 56e0773d 2019-11-28 stsp s->matched_entry = te;
6665 7c32bd05 2019-06-22 stsp break;
6666 7c32bd05 2019-06-22 stsp }
6667 7c32bd05 2019-06-22 stsp
6668 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6669 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6670 56e0773d 2019-11-28 stsp else
6671 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6672 7c32bd05 2019-06-22 stsp }
6673 e5a0f69f 2018-08-18 stsp
6674 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6675 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6676 7c32bd05 2019-06-22 stsp s->selected = 0;
6677 7c32bd05 2019-06-22 stsp }
6678 7c32bd05 2019-06-22 stsp
6679 e5a0f69f 2018-08-18 stsp return NULL;
6680 ad80ab7b 2018-08-04 stsp }
6681 ad80ab7b 2018-08-04 stsp
6682 ad80ab7b 2018-08-04 stsp static const struct got_error *
6683 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6684 ad80ab7b 2018-08-04 stsp {
6685 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6686 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6687 e5a0f69f 2018-08-18 stsp char *parent_path;
6688 ad80ab7b 2018-08-04 stsp
6689 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6690 e5a0f69f 2018-08-18 stsp if (err)
6691 e5a0f69f 2018-08-18 stsp return err;
6692 ffd1d5e5 2018-06-23 stsp
6693 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6694 e5a0f69f 2018-08-18 stsp free(parent_path);
6695 669b5ffa 2018-10-07 stsp
6696 a5d43cac 2022-07-01 thomas view_border(view);
6697 e5a0f69f 2018-08-18 stsp return err;
6698 e5a0f69f 2018-08-18 stsp }
6699 ce52c690 2018-06-23 stsp
6700 e5a0f69f 2018-08-18 stsp static const struct got_error *
6701 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6702 e5a0f69f 2018-08-18 stsp {
6703 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6704 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6705 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6706 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6707 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6708 ffd1d5e5 2018-06-23 stsp
6709 e5a0f69f 2018-08-18 stsp switch (ch) {
6710 1e37a5c2 2019-05-12 jcs case 'i':
6711 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6712 07b0611c 2022-06-23 thomas view->count = 0;
6713 1e37a5c2 2019-05-12 jcs break;
6714 1e37a5c2 2019-05-12 jcs case 'l':
6715 07b0611c 2022-06-23 thomas view->count = 0;
6716 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6717 ffd1d5e5 2018-06-23 stsp break;
6718 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6719 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6720 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6721 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6722 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6723 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6724 444d5325 2022-07-03 thomas if (err)
6725 444d5325 2022-07-03 thomas break;
6726 444d5325 2022-07-03 thomas }
6727 e78dc838 2020-12-04 stsp view->focussed = 0;
6728 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6729 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6730 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6731 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6732 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6733 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6734 1e37a5c2 2019-05-12 jcs if (err)
6735 1e37a5c2 2019-05-12 jcs return err;
6736 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6737 40236d76 2022-06-23 thomas if (err)
6738 40236d76 2022-06-23 thomas return err;
6739 e78dc838 2020-12-04 stsp view->focus_child = 1;
6740 1e37a5c2 2019-05-12 jcs } else
6741 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6742 152c1c93 2020-11-29 stsp break;
6743 152c1c93 2020-11-29 stsp case 'r':
6744 07b0611c 2022-06-23 thomas view->count = 0;
6745 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6746 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6747 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6748 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6749 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6750 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6751 152c1c93 2020-11-29 stsp if (err) {
6752 152c1c93 2020-11-29 stsp view_close(ref_view);
6753 152c1c93 2020-11-29 stsp return err;
6754 152c1c93 2020-11-29 stsp }
6755 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6756 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6757 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6758 444d5325 2022-07-03 thomas if (err)
6759 444d5325 2022-07-03 thomas break;
6760 444d5325 2022-07-03 thomas }
6761 e78dc838 2020-12-04 stsp view->focussed = 0;
6762 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6763 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6764 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6765 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6766 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6767 152c1c93 2020-11-29 stsp err = view_close_child(view);
6768 152c1c93 2020-11-29 stsp if (err)
6769 152c1c93 2020-11-29 stsp return err;
6770 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6771 40236d76 2022-06-23 thomas if (err)
6772 40236d76 2022-06-23 thomas return err;
6773 e78dc838 2020-12-04 stsp view->focus_child = 1;
6774 152c1c93 2020-11-29 stsp } else
6775 152c1c93 2020-11-29 stsp *new_view = ref_view;
6776 e4526bf5 2021-09-03 naddy break;
6777 e4526bf5 2021-09-03 naddy case 'g':
6778 e4526bf5 2021-09-03 naddy case KEY_HOME:
6779 e4526bf5 2021-09-03 naddy s->selected = 0;
6780 07b0611c 2022-06-23 thomas view->count = 0;
6781 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6782 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6783 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6784 e4526bf5 2021-09-03 naddy else
6785 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6786 1e37a5c2 2019-05-12 jcs break;
6787 e4526bf5 2021-09-03 naddy case 'G':
6788 a5d43cac 2022-07-01 thomas case KEY_END: {
6789 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6790 a5d43cac 2022-07-01 thomas
6791 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6792 a5d43cac 2022-07-01 thomas --eos; /* border */
6793 e4526bf5 2021-09-03 naddy s->selected = 0;
6794 07b0611c 2022-06-23 thomas view->count = 0;
6795 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6796 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6797 e4526bf5 2021-09-03 naddy if (te == NULL) {
6798 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6799 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6800 e4526bf5 2021-09-03 naddy n++;
6801 e4526bf5 2021-09-03 naddy }
6802 e4526bf5 2021-09-03 naddy break;
6803 e4526bf5 2021-09-03 naddy }
6804 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6805 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6806 e4526bf5 2021-09-03 naddy }
6807 e4526bf5 2021-09-03 naddy if (n > 0)
6808 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6809 e4526bf5 2021-09-03 naddy break;
6810 a5d43cac 2022-07-01 thomas }
6811 1e37a5c2 2019-05-12 jcs case 'k':
6812 1e37a5c2 2019-05-12 jcs case KEY_UP:
6813 f7140bf5 2021-10-17 thomas case CTRL('p'):
6814 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6815 1e37a5c2 2019-05-12 jcs s->selected--;
6816 fa86c4bf 2020-11-29 stsp break;
6817 1e37a5c2 2019-05-12 jcs }
6818 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6819 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6820 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6821 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6822 07b0611c 2022-06-23 thomas view->count = 0;
6823 1e37a5c2 2019-05-12 jcs break;
6824 70f17a53 2022-06-13 thomas case CTRL('u'):
6825 23427b14 2022-06-23 thomas case 'u':
6826 70f17a53 2022-06-13 thomas nscroll /= 2;
6827 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6828 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6829 ea025d1d 2020-02-22 naddy case CTRL('b'):
6830 1c5e5faa 2022-06-23 thomas case 'b':
6831 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6832 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6833 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6834 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6835 fa86c4bf 2020-11-29 stsp } else {
6836 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6837 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6838 fa86c4bf 2020-11-29 stsp }
6839 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6840 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6841 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6842 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6843 07b0611c 2022-06-23 thomas view->count = 0;
6844 1e37a5c2 2019-05-12 jcs break;
6845 1e37a5c2 2019-05-12 jcs case 'j':
6846 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6847 f7140bf5 2021-10-17 thomas case CTRL('n'):
6848 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6849 1e37a5c2 2019-05-12 jcs s->selected++;
6850 1e37a5c2 2019-05-12 jcs break;
6851 1e37a5c2 2019-05-12 jcs }
6852 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6853 07b0611c 2022-06-23 thomas == NULL) {
6854 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6855 07b0611c 2022-06-23 thomas view->count = 0;
6856 1e37a5c2 2019-05-12 jcs break;
6857 07b0611c 2022-06-23 thomas }
6858 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6859 1e37a5c2 2019-05-12 jcs break;
6860 70f17a53 2022-06-13 thomas case CTRL('d'):
6861 23427b14 2022-06-23 thomas case 'd':
6862 70f17a53 2022-06-13 thomas nscroll /= 2;
6863 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6864 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6865 ea025d1d 2020-02-22 naddy case CTRL('f'):
6866 1c5e5faa 2022-06-23 thomas case 'f':
6867 4c2d69cb 2022-06-23 thomas case ' ':
6868 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6869 1e37a5c2 2019-05-12 jcs == NULL) {
6870 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6871 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6872 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6873 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6874 07b0611c 2022-06-23 thomas else
6875 07b0611c 2022-06-23 thomas view->count = 0;
6876 1e37a5c2 2019-05-12 jcs break;
6877 1e37a5c2 2019-05-12 jcs }
6878 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6879 1e37a5c2 2019-05-12 jcs break;
6880 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6881 1e37a5c2 2019-05-12 jcs case '\r':
6882 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6883 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6884 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6885 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6886 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6887 07b0611c 2022-06-23 thomas view->count = 0;
6888 1e37a5c2 2019-05-12 jcs break;
6889 07b0611c 2022-06-23 thomas }
6890 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6891 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6892 1e37a5c2 2019-05-12 jcs entry);
6893 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6894 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6895 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6896 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6897 1e37a5c2 2019-05-12 jcs s->selected_entry =
6898 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6899 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6900 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6901 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6902 a5d43cac 2022-07-01 thomas if (err)
6903 a5d43cac 2022-07-01 thomas break;
6904 a5d43cac 2022-07-01 thomas }
6905 1e37a5c2 2019-05-12 jcs free(parent);
6906 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6907 56e0773d 2019-11-28 stsp s->selected_entry))) {
6908 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6909 07b0611c 2022-06-23 thomas view->count = 0;
6910 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6911 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6912 1e37a5c2 2019-05-12 jcs if (err)
6913 1e37a5c2 2019-05-12 jcs break;
6914 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6915 941e9f74 2019-05-21 stsp if (err) {
6916 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6917 1e37a5c2 2019-05-12 jcs break;
6918 1e37a5c2 2019-05-12 jcs }
6919 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6920 56e0773d 2019-11-28 stsp s->selected_entry))) {
6921 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6922 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
6923 1e37a5c2 2019-05-12 jcs
6924 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
6925 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
6926 a5d43cac 2022-07-01 thomas
6927 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
6928 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6929 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6930 1e37a5c2 2019-05-12 jcs if (err)
6931 1e37a5c2 2019-05-12 jcs break;
6932 a5d43cac 2022-07-01 thomas
6933 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6934 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6935 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6936 a5d43cac 2022-07-01 thomas if (err)
6937 a5d43cac 2022-07-01 thomas break;
6938 a5d43cac 2022-07-01 thomas }
6939 a5d43cac 2022-07-01 thomas
6940 07b0611c 2022-06-23 thomas view->count = 0;
6941 e78dc838 2020-12-04 stsp view->focussed = 0;
6942 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6943 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
6944 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
6945 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6946 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
6947 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6948 669b5ffa 2018-10-07 stsp if (err)
6949 669b5ffa 2018-10-07 stsp return err;
6950 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
6951 40236d76 2022-06-23 thomas if (err)
6952 40236d76 2022-06-23 thomas return err;
6953 e78dc838 2020-12-04 stsp view->focus_child = 1;
6954 669b5ffa 2018-10-07 stsp } else
6955 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6956 1e37a5c2 2019-05-12 jcs }
6957 1e37a5c2 2019-05-12 jcs break;
6958 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6959 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6960 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6961 07b0611c 2022-06-23 thomas view->count = 0;
6962 1e37a5c2 2019-05-12 jcs break;
6963 1e37a5c2 2019-05-12 jcs default:
6964 07b0611c 2022-06-23 thomas view->count = 0;
6965 1e37a5c2 2019-05-12 jcs break;
6966 ffd1d5e5 2018-06-23 stsp }
6967 e5a0f69f 2018-08-18 stsp
6968 ffd1d5e5 2018-06-23 stsp return err;
6969 9f7d7167 2018-04-29 stsp }
6970 9f7d7167 2018-04-29 stsp
6971 ffd1d5e5 2018-06-23 stsp __dead static void
6972 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6973 ffd1d5e5 2018-06-23 stsp {
6974 ffd1d5e5 2018-06-23 stsp endwin();
6975 91198554 2022-06-23 thomas fprintf(stderr,
6976 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6977 ffd1d5e5 2018-06-23 stsp getprogname());
6978 ffd1d5e5 2018-06-23 stsp exit(1);
6979 ffd1d5e5 2018-06-23 stsp }
6980 ffd1d5e5 2018-06-23 stsp
6981 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6982 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6983 ffd1d5e5 2018-06-23 stsp {
6984 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6985 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6986 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6987 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6988 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6989 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6990 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6991 4e97c21c 2020-12-06 stsp char *label = NULL;
6992 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6993 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6994 ffd1d5e5 2018-06-23 stsp int ch;
6995 5221c383 2018-08-01 stsp struct tog_view *view;
6996 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6997 70ac5f84 2019-03-28 stsp
6998 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6999 ffd1d5e5 2018-06-23 stsp switch (ch) {
7000 ffd1d5e5 2018-06-23 stsp case 'c':
7001 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7002 74283ab8 2020-02-07 stsp break;
7003 74283ab8 2020-02-07 stsp case 'r':
7004 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7005 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7006 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7007 74283ab8 2020-02-07 stsp optarg);
7008 ffd1d5e5 2018-06-23 stsp break;
7009 ffd1d5e5 2018-06-23 stsp default:
7010 e99e2d15 2020-11-24 naddy usage_tree();
7011 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7012 ffd1d5e5 2018-06-23 stsp }
7013 ffd1d5e5 2018-06-23 stsp }
7014 ffd1d5e5 2018-06-23 stsp
7015 ffd1d5e5 2018-06-23 stsp argc -= optind;
7016 ffd1d5e5 2018-06-23 stsp argv += optind;
7017 ffd1d5e5 2018-06-23 stsp
7018 55cccc34 2020-02-20 stsp if (argc > 1)
7019 e99e2d15 2020-11-24 naddy usage_tree();
7020 7cd52833 2022-06-23 thomas
7021 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7022 7cd52833 2022-06-23 thomas if (error != NULL)
7023 7cd52833 2022-06-23 thomas goto done;
7024 74283ab8 2020-02-07 stsp
7025 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7026 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7027 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7028 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7029 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7030 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7031 c156c7a4 2020-12-18 stsp goto done;
7032 55cccc34 2020-02-20 stsp if (worktree)
7033 52185f70 2019-02-05 stsp repo_path =
7034 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7035 55cccc34 2020-02-20 stsp else
7036 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7037 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7038 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7039 c156c7a4 2020-12-18 stsp goto done;
7040 c156c7a4 2020-12-18 stsp }
7041 55cccc34 2020-02-20 stsp }
7042 a915003a 2019-02-05 stsp
7043 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7044 c02c541e 2019-03-29 stsp if (error != NULL)
7045 52185f70 2019-02-05 stsp goto done;
7046 d188b9a6 2019-01-04 stsp
7047 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7048 55cccc34 2020-02-20 stsp repo, worktree);
7049 55cccc34 2020-02-20 stsp if (error)
7050 55cccc34 2020-02-20 stsp goto done;
7051 55cccc34 2020-02-20 stsp
7052 55cccc34 2020-02-20 stsp init_curses();
7053 55cccc34 2020-02-20 stsp
7054 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7055 c02c541e 2019-03-29 stsp if (error)
7056 52185f70 2019-02-05 stsp goto done;
7057 ffd1d5e5 2018-06-23 stsp
7058 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7059 51a10b52 2020-12-26 stsp if (error)
7060 51a10b52 2020-12-26 stsp goto done;
7061 51a10b52 2020-12-26 stsp
7062 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7063 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7064 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7065 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7066 4e97c21c 2020-12-06 stsp if (error)
7067 4e97c21c 2020-12-06 stsp goto done;
7068 4e97c21c 2020-12-06 stsp head_ref_name = label;
7069 4e97c21c 2020-12-06 stsp } else {
7070 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7071 4e97c21c 2020-12-06 stsp if (error == NULL)
7072 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7073 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7074 4e97c21c 2020-12-06 stsp goto done;
7075 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7076 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7077 4e97c21c 2020-12-06 stsp if (error)
7078 4e97c21c 2020-12-06 stsp goto done;
7079 4e97c21c 2020-12-06 stsp }
7080 ffd1d5e5 2018-06-23 stsp
7081 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7082 945f9229 2022-04-16 thomas if (error)
7083 945f9229 2022-04-16 thomas goto done;
7084 945f9229 2022-04-16 thomas
7085 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7086 5221c383 2018-08-01 stsp if (view == NULL) {
7087 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7088 5221c383 2018-08-01 stsp goto done;
7089 5221c383 2018-08-01 stsp }
7090 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7091 ad80ab7b 2018-08-04 stsp if (error)
7092 ad80ab7b 2018-08-04 stsp goto done;
7093 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7094 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7095 d91faf3b 2020-12-01 naddy in_repo_path);
7096 55cccc34 2020-02-20 stsp if (error)
7097 55cccc34 2020-02-20 stsp goto done;
7098 55cccc34 2020-02-20 stsp }
7099 55cccc34 2020-02-20 stsp
7100 55cccc34 2020-02-20 stsp if (worktree) {
7101 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7102 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7103 55cccc34 2020-02-20 stsp worktree = NULL;
7104 55cccc34 2020-02-20 stsp }
7105 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7106 ffd1d5e5 2018-06-23 stsp done:
7107 52185f70 2019-02-05 stsp free(repo_path);
7108 e4a0e26d 2020-02-20 stsp free(cwd);
7109 ffd1d5e5 2018-06-23 stsp free(commit_id);
7110 4e97c21c 2020-12-06 stsp free(label);
7111 486cd271 2020-12-06 stsp if (ref)
7112 486cd271 2020-12-06 stsp got_ref_close(ref);
7113 1d0f4054 2021-06-17 stsp if (repo) {
7114 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7115 1d0f4054 2021-06-17 stsp if (error == NULL)
7116 1d0f4054 2021-06-17 stsp error = close_err;
7117 1d0f4054 2021-06-17 stsp }
7118 7cd52833 2022-06-23 thomas if (pack_fds) {
7119 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7120 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7121 7cd52833 2022-06-23 thomas if (error == NULL)
7122 7cd52833 2022-06-23 thomas error = pack_err;
7123 7cd52833 2022-06-23 thomas }
7124 51a10b52 2020-12-26 stsp tog_free_refs();
7125 ffd1d5e5 2018-06-23 stsp return error;
7126 6458efa5 2020-11-24 stsp }
7127 6458efa5 2020-11-24 stsp
7128 6458efa5 2020-11-24 stsp static const struct got_error *
7129 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7130 6458efa5 2020-11-24 stsp {
7131 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7132 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7133 6458efa5 2020-11-24 stsp
7134 6458efa5 2020-11-24 stsp s->nrefs = 0;
7135 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7136 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7137 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7138 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7139 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7140 6458efa5 2020-11-24 stsp continue;
7141 6458efa5 2020-11-24 stsp
7142 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7143 6458efa5 2020-11-24 stsp if (re == NULL)
7144 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7145 6458efa5 2020-11-24 stsp
7146 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7147 8924d611 2020-12-26 stsp if (re->ref == NULL)
7148 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7149 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7150 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7151 6458efa5 2020-11-24 stsp }
7152 6458efa5 2020-11-24 stsp
7153 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7154 6458efa5 2020-11-24 stsp return NULL;
7155 6458efa5 2020-11-24 stsp }
7156 6458efa5 2020-11-24 stsp
7157 ef20f542 2022-06-26 thomas static void
7158 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7159 6458efa5 2020-11-24 stsp {
7160 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7161 6458efa5 2020-11-24 stsp
7162 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7163 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7164 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7165 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7166 6458efa5 2020-11-24 stsp free(re);
7167 6458efa5 2020-11-24 stsp }
7168 6458efa5 2020-11-24 stsp }
7169 6458efa5 2020-11-24 stsp
7170 6458efa5 2020-11-24 stsp static const struct got_error *
7171 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7172 6458efa5 2020-11-24 stsp {
7173 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7174 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7175 6458efa5 2020-11-24 stsp
7176 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7177 6458efa5 2020-11-24 stsp s->repo = repo;
7178 6458efa5 2020-11-24 stsp
7179 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7180 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7181 6458efa5 2020-11-24 stsp
7182 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7183 6458efa5 2020-11-24 stsp if (err)
7184 6458efa5 2020-11-24 stsp return err;
7185 34ba6917 2020-11-30 stsp
7186 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7187 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7188 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7189 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7190 6458efa5 2020-11-24 stsp if (err)
7191 6458efa5 2020-11-24 stsp goto done;
7192 6458efa5 2020-11-24 stsp
7193 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7194 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7195 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7196 6458efa5 2020-11-24 stsp if (err)
7197 6458efa5 2020-11-24 stsp goto done;
7198 6458efa5 2020-11-24 stsp
7199 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7200 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7201 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7202 2183bbf6 2022-01-23 thomas if (err)
7203 2183bbf6 2022-01-23 thomas goto done;
7204 2183bbf6 2022-01-23 thomas
7205 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7206 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7207 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7208 6458efa5 2020-11-24 stsp if (err)
7209 6458efa5 2020-11-24 stsp goto done;
7210 6458efa5 2020-11-24 stsp }
7211 6458efa5 2020-11-24 stsp
7212 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7213 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7214 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7215 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7216 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7217 6458efa5 2020-11-24 stsp done:
7218 6458efa5 2020-11-24 stsp if (err)
7219 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7220 6458efa5 2020-11-24 stsp return err;
7221 6458efa5 2020-11-24 stsp }
7222 6458efa5 2020-11-24 stsp
7223 6458efa5 2020-11-24 stsp static const struct got_error *
7224 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7225 6458efa5 2020-11-24 stsp {
7226 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7227 6458efa5 2020-11-24 stsp
7228 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7229 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7230 6458efa5 2020-11-24 stsp
7231 6458efa5 2020-11-24 stsp return NULL;
7232 9f7d7167 2018-04-29 stsp }
7233 ce5b7c56 2019-07-09 stsp
7234 6458efa5 2020-11-24 stsp static const struct got_error *
7235 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7236 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7237 6458efa5 2020-11-24 stsp {
7238 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7239 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7240 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7241 6458efa5 2020-11-24 stsp int obj_type;
7242 6458efa5 2020-11-24 stsp
7243 c42c9805 2020-11-24 stsp *commit_id = NULL;
7244 6458efa5 2020-11-24 stsp
7245 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7246 6458efa5 2020-11-24 stsp if (err)
7247 6458efa5 2020-11-24 stsp return err;
7248 6458efa5 2020-11-24 stsp
7249 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7250 6458efa5 2020-11-24 stsp if (err)
7251 6458efa5 2020-11-24 stsp goto done;
7252 6458efa5 2020-11-24 stsp
7253 6458efa5 2020-11-24 stsp switch (obj_type) {
7254 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7255 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7256 6458efa5 2020-11-24 stsp break;
7257 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7258 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7259 6458efa5 2020-11-24 stsp if (err)
7260 6458efa5 2020-11-24 stsp goto done;
7261 c42c9805 2020-11-24 stsp free(obj_id);
7262 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7263 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7264 c42c9805 2020-11-24 stsp if (err)
7265 6458efa5 2020-11-24 stsp goto done;
7266 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7267 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7268 c42c9805 2020-11-24 stsp goto done;
7269 c42c9805 2020-11-24 stsp }
7270 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7271 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7272 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7273 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7274 c42c9805 2020-11-24 stsp goto done;
7275 c42c9805 2020-11-24 stsp }
7276 6458efa5 2020-11-24 stsp break;
7277 6458efa5 2020-11-24 stsp default:
7278 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7279 c42c9805 2020-11-24 stsp break;
7280 6458efa5 2020-11-24 stsp }
7281 6458efa5 2020-11-24 stsp
7282 c42c9805 2020-11-24 stsp done:
7283 c42c9805 2020-11-24 stsp if (tag)
7284 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7285 c42c9805 2020-11-24 stsp if (err) {
7286 c42c9805 2020-11-24 stsp free(*commit_id);
7287 c42c9805 2020-11-24 stsp *commit_id = NULL;
7288 c42c9805 2020-11-24 stsp }
7289 c42c9805 2020-11-24 stsp return err;
7290 c42c9805 2020-11-24 stsp }
7291 c42c9805 2020-11-24 stsp
7292 c42c9805 2020-11-24 stsp static const struct got_error *
7293 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7294 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7295 c42c9805 2020-11-24 stsp {
7296 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7297 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7298 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7299 c42c9805 2020-11-24 stsp
7300 c42c9805 2020-11-24 stsp *new_view = NULL;
7301 c42c9805 2020-11-24 stsp
7302 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7303 c42c9805 2020-11-24 stsp if (err) {
7304 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7305 c42c9805 2020-11-24 stsp return err;
7306 c42c9805 2020-11-24 stsp else
7307 c42c9805 2020-11-24 stsp return NULL;
7308 c42c9805 2020-11-24 stsp }
7309 c42c9805 2020-11-24 stsp
7310 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7311 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7312 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7313 6458efa5 2020-11-24 stsp goto done;
7314 6458efa5 2020-11-24 stsp }
7315 6458efa5 2020-11-24 stsp
7316 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7317 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7318 6458efa5 2020-11-24 stsp done:
7319 6458efa5 2020-11-24 stsp if (err)
7320 6458efa5 2020-11-24 stsp view_close(log_view);
7321 6458efa5 2020-11-24 stsp else
7322 6458efa5 2020-11-24 stsp *new_view = log_view;
7323 c42c9805 2020-11-24 stsp free(commit_id);
7324 6458efa5 2020-11-24 stsp return err;
7325 6458efa5 2020-11-24 stsp }
7326 6458efa5 2020-11-24 stsp
7327 ce5b7c56 2019-07-09 stsp static void
7328 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7329 6458efa5 2020-11-24 stsp {
7330 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7331 34ba6917 2020-11-30 stsp int i = 0;
7332 6458efa5 2020-11-24 stsp
7333 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7334 6458efa5 2020-11-24 stsp return;
7335 6458efa5 2020-11-24 stsp
7336 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7337 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7338 34ba6917 2020-11-30 stsp if (re == NULL)
7339 34ba6917 2020-11-30 stsp break;
7340 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7341 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7342 6458efa5 2020-11-24 stsp }
7343 6458efa5 2020-11-24 stsp }
7344 6458efa5 2020-11-24 stsp
7345 a5d43cac 2022-07-01 thomas static const struct got_error *
7346 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7347 6458efa5 2020-11-24 stsp {
7348 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7349 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7350 6458efa5 2020-11-24 stsp int n = 0;
7351 6458efa5 2020-11-24 stsp
7352 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7353 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7354 6458efa5 2020-11-24 stsp else
7355 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7356 6458efa5 2020-11-24 stsp
7357 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7358 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7359 a5d43cac 2022-07-01 thomas if (last)
7360 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7361 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7362 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7363 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7364 6458efa5 2020-11-24 stsp }
7365 6458efa5 2020-11-24 stsp }
7366 a5d43cac 2022-07-01 thomas
7367 a5d43cac 2022-07-01 thomas return NULL;
7368 6458efa5 2020-11-24 stsp }
7369 6458efa5 2020-11-24 stsp
7370 6458efa5 2020-11-24 stsp static const struct got_error *
7371 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7372 6458efa5 2020-11-24 stsp {
7373 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7374 6458efa5 2020-11-24 stsp
7375 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7376 6458efa5 2020-11-24 stsp return NULL;
7377 6458efa5 2020-11-24 stsp }
7378 6458efa5 2020-11-24 stsp
7379 6458efa5 2020-11-24 stsp static int
7380 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7381 6458efa5 2020-11-24 stsp {
7382 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7383 6458efa5 2020-11-24 stsp
7384 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7385 6458efa5 2020-11-24 stsp 0) == 0;
7386 6458efa5 2020-11-24 stsp }
7387 6458efa5 2020-11-24 stsp
7388 6458efa5 2020-11-24 stsp static const struct got_error *
7389 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7390 6458efa5 2020-11-24 stsp {
7391 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7392 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7393 6458efa5 2020-11-24 stsp
7394 6458efa5 2020-11-24 stsp if (!view->searching) {
7395 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7396 6458efa5 2020-11-24 stsp return NULL;
7397 6458efa5 2020-11-24 stsp }
7398 6458efa5 2020-11-24 stsp
7399 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7400 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7401 6458efa5 2020-11-24 stsp if (s->selected_entry)
7402 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7403 6458efa5 2020-11-24 stsp else
7404 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7405 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7406 6458efa5 2020-11-24 stsp } else {
7407 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7408 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7409 6458efa5 2020-11-24 stsp else
7410 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7411 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7412 6458efa5 2020-11-24 stsp }
7413 6458efa5 2020-11-24 stsp } else {
7414 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7415 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7416 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7417 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7418 6458efa5 2020-11-24 stsp else
7419 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7420 6458efa5 2020-11-24 stsp }
7421 6458efa5 2020-11-24 stsp
7422 6458efa5 2020-11-24 stsp while (1) {
7423 6458efa5 2020-11-24 stsp if (re == NULL) {
7424 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7425 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7426 6458efa5 2020-11-24 stsp return NULL;
7427 6458efa5 2020-11-24 stsp }
7428 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7429 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7430 6458efa5 2020-11-24 stsp else
7431 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7432 6458efa5 2020-11-24 stsp }
7433 6458efa5 2020-11-24 stsp
7434 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7435 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7436 6458efa5 2020-11-24 stsp s->matched_entry = re;
7437 6458efa5 2020-11-24 stsp break;
7438 6458efa5 2020-11-24 stsp }
7439 6458efa5 2020-11-24 stsp
7440 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7441 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7442 6458efa5 2020-11-24 stsp else
7443 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7444 6458efa5 2020-11-24 stsp }
7445 6458efa5 2020-11-24 stsp
7446 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7447 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7448 6458efa5 2020-11-24 stsp s->selected = 0;
7449 6458efa5 2020-11-24 stsp }
7450 6458efa5 2020-11-24 stsp
7451 6458efa5 2020-11-24 stsp return NULL;
7452 6458efa5 2020-11-24 stsp }
7453 6458efa5 2020-11-24 stsp
7454 6458efa5 2020-11-24 stsp static const struct got_error *
7455 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7456 6458efa5 2020-11-24 stsp {
7457 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7458 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7459 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7460 6458efa5 2020-11-24 stsp char *line = NULL;
7461 6458efa5 2020-11-24 stsp wchar_t *wline;
7462 6458efa5 2020-11-24 stsp struct tog_color *tc;
7463 6458efa5 2020-11-24 stsp int width, n;
7464 6458efa5 2020-11-24 stsp int limit = view->nlines;
7465 6458efa5 2020-11-24 stsp
7466 6458efa5 2020-11-24 stsp werase(view->window);
7467 6458efa5 2020-11-24 stsp
7468 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7469 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7470 a5d43cac 2022-07-01 thomas --limit; /* border */
7471 6458efa5 2020-11-24 stsp
7472 6458efa5 2020-11-24 stsp if (limit == 0)
7473 6458efa5 2020-11-24 stsp return NULL;
7474 6458efa5 2020-11-24 stsp
7475 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7476 6458efa5 2020-11-24 stsp
7477 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7478 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7479 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7480 6458efa5 2020-11-24 stsp
7481 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7482 6458efa5 2020-11-24 stsp if (err) {
7483 6458efa5 2020-11-24 stsp free(line);
7484 6458efa5 2020-11-24 stsp return err;
7485 6458efa5 2020-11-24 stsp }
7486 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7487 6458efa5 2020-11-24 stsp wstandout(view->window);
7488 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7489 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7490 6458efa5 2020-11-24 stsp wstandend(view->window);
7491 6458efa5 2020-11-24 stsp free(wline);
7492 6458efa5 2020-11-24 stsp wline = NULL;
7493 6458efa5 2020-11-24 stsp free(line);
7494 6458efa5 2020-11-24 stsp line = NULL;
7495 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7496 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7497 6458efa5 2020-11-24 stsp if (--limit <= 0)
7498 6458efa5 2020-11-24 stsp return NULL;
7499 6458efa5 2020-11-24 stsp
7500 6458efa5 2020-11-24 stsp n = 0;
7501 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7502 6458efa5 2020-11-24 stsp char *line = NULL;
7503 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7504 6458efa5 2020-11-24 stsp
7505 84227eb1 2022-06-23 thomas if (s->show_date) {
7506 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7507 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7508 84227eb1 2022-06-23 thomas struct got_object_id *id;
7509 84227eb1 2022-06-23 thomas struct tm tm;
7510 84227eb1 2022-06-23 thomas time_t t;
7511 84227eb1 2022-06-23 thomas
7512 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7513 84227eb1 2022-06-23 thomas if (err)
7514 84227eb1 2022-06-23 thomas return err;
7515 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7516 84227eb1 2022-06-23 thomas if (err) {
7517 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7518 84227eb1 2022-06-23 thomas free(id);
7519 84227eb1 2022-06-23 thomas return err;
7520 84227eb1 2022-06-23 thomas }
7521 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7522 84227eb1 2022-06-23 thomas id);
7523 84227eb1 2022-06-23 thomas if (err) {
7524 84227eb1 2022-06-23 thomas free(id);
7525 84227eb1 2022-06-23 thomas return err;
7526 84227eb1 2022-06-23 thomas }
7527 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7528 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7529 84227eb1 2022-06-23 thomas } else {
7530 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7531 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7532 84227eb1 2022-06-23 thomas }
7533 84227eb1 2022-06-23 thomas free(id);
7534 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7535 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7536 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7537 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7538 84227eb1 2022-06-23 thomas }
7539 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7540 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7541 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7542 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7543 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7544 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7545 6458efa5 2020-11-24 stsp struct got_object_id *id;
7546 6458efa5 2020-11-24 stsp char *id_str;
7547 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7548 6458efa5 2020-11-24 stsp if (err)
7549 6458efa5 2020-11-24 stsp return err;
7550 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7551 6458efa5 2020-11-24 stsp if (err) {
7552 6458efa5 2020-11-24 stsp free(id);
7553 6458efa5 2020-11-24 stsp return err;
7554 6458efa5 2020-11-24 stsp }
7555 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7556 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7557 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7558 6458efa5 2020-11-24 stsp free(id);
7559 6458efa5 2020-11-24 stsp free(id_str);
7560 6458efa5 2020-11-24 stsp return err;
7561 6458efa5 2020-11-24 stsp }
7562 6458efa5 2020-11-24 stsp free(id);
7563 6458efa5 2020-11-24 stsp free(id_str);
7564 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7565 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7566 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7567 6458efa5 2020-11-24 stsp
7568 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7569 f91a2b48 2022-06-23 thomas 0, 0);
7570 6458efa5 2020-11-24 stsp if (err) {
7571 6458efa5 2020-11-24 stsp free(line);
7572 6458efa5 2020-11-24 stsp return err;
7573 6458efa5 2020-11-24 stsp }
7574 6458efa5 2020-11-24 stsp if (n == s->selected) {
7575 6458efa5 2020-11-24 stsp if (view->focussed)
7576 6458efa5 2020-11-24 stsp wstandout(view->window);
7577 6458efa5 2020-11-24 stsp s->selected_entry = re;
7578 6458efa5 2020-11-24 stsp }
7579 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7580 6458efa5 2020-11-24 stsp if (tc)
7581 6458efa5 2020-11-24 stsp wattr_on(view->window,
7582 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7583 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7584 6458efa5 2020-11-24 stsp if (tc)
7585 6458efa5 2020-11-24 stsp wattr_off(view->window,
7586 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7587 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7588 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7589 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7590 6458efa5 2020-11-24 stsp wstandend(view->window);
7591 6458efa5 2020-11-24 stsp free(line);
7592 6458efa5 2020-11-24 stsp free(wline);
7593 6458efa5 2020-11-24 stsp wline = NULL;
7594 6458efa5 2020-11-24 stsp n++;
7595 6458efa5 2020-11-24 stsp s->ndisplayed++;
7596 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7597 6458efa5 2020-11-24 stsp
7598 6458efa5 2020-11-24 stsp limit--;
7599 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7600 6458efa5 2020-11-24 stsp }
7601 6458efa5 2020-11-24 stsp
7602 a5d43cac 2022-07-01 thomas view_border(view);
7603 6458efa5 2020-11-24 stsp return err;
7604 6458efa5 2020-11-24 stsp }
7605 6458efa5 2020-11-24 stsp
7606 6458efa5 2020-11-24 stsp static const struct got_error *
7607 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7608 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7609 c42c9805 2020-11-24 stsp {
7610 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7611 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7612 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7613 c42c9805 2020-11-24 stsp
7614 c42c9805 2020-11-24 stsp *new_view = NULL;
7615 c42c9805 2020-11-24 stsp
7616 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7617 c42c9805 2020-11-24 stsp if (err) {
7618 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7619 c42c9805 2020-11-24 stsp return err;
7620 c42c9805 2020-11-24 stsp else
7621 c42c9805 2020-11-24 stsp return NULL;
7622 c42c9805 2020-11-24 stsp }
7623 c42c9805 2020-11-24 stsp
7624 c42c9805 2020-11-24 stsp
7625 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7626 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7627 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7628 c42c9805 2020-11-24 stsp goto done;
7629 c42c9805 2020-11-24 stsp }
7630 c42c9805 2020-11-24 stsp
7631 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7632 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7633 c42c9805 2020-11-24 stsp if (err)
7634 c42c9805 2020-11-24 stsp goto done;
7635 c42c9805 2020-11-24 stsp
7636 c42c9805 2020-11-24 stsp *new_view = tree_view;
7637 c42c9805 2020-11-24 stsp done:
7638 c42c9805 2020-11-24 stsp free(commit_id);
7639 c42c9805 2020-11-24 stsp return err;
7640 c42c9805 2020-11-24 stsp }
7641 c42c9805 2020-11-24 stsp static const struct got_error *
7642 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7643 6458efa5 2020-11-24 stsp {
7644 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7645 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7646 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7647 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7648 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7649 6458efa5 2020-11-24 stsp
7650 6458efa5 2020-11-24 stsp switch (ch) {
7651 6458efa5 2020-11-24 stsp case 'i':
7652 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7653 07b0611c 2022-06-23 thomas view->count = 0;
7654 3bfadbd4 2021-11-20 thomas break;
7655 84227eb1 2022-06-23 thomas case 'm':
7656 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7657 07b0611c 2022-06-23 thomas view->count = 0;
7658 84227eb1 2022-06-23 thomas break;
7659 98182bd0 2021-11-20 thomas case 'o':
7660 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7661 07b0611c 2022-06-23 thomas view->count = 0;
7662 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7663 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7664 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7665 c591440f 2021-11-20 thomas if (err)
7666 c591440f 2021-11-20 thomas break;
7667 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7668 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7669 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7670 3bfadbd4 2021-11-20 thomas if (err)
7671 3bfadbd4 2021-11-20 thomas break;
7672 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7673 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7674 6458efa5 2020-11-24 stsp break;
7675 6458efa5 2020-11-24 stsp case KEY_ENTER:
7676 6458efa5 2020-11-24 stsp case '\r':
7677 07b0611c 2022-06-23 thomas view->count = 0;
7678 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7679 6458efa5 2020-11-24 stsp break;
7680 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7681 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7682 a5d43cac 2022-07-01 thomas
7683 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7684 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7685 a5d43cac 2022-07-01 thomas if (err)
7686 a5d43cac 2022-07-01 thomas break;
7687 a5d43cac 2022-07-01 thomas
7688 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7689 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7690 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7691 a5d43cac 2022-07-01 thomas if (err)
7692 a5d43cac 2022-07-01 thomas break;
7693 a5d43cac 2022-07-01 thomas }
7694 a5d43cac 2022-07-01 thomas
7695 e78dc838 2020-12-04 stsp view->focussed = 0;
7696 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7697 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7698 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7699 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7700 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7701 6458efa5 2020-11-24 stsp err = view_close_child(view);
7702 6458efa5 2020-11-24 stsp if (err)
7703 6458efa5 2020-11-24 stsp return err;
7704 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7705 40236d76 2022-06-23 thomas if (err)
7706 40236d76 2022-06-23 thomas return err;
7707 e78dc838 2020-12-04 stsp view->focus_child = 1;
7708 6458efa5 2020-11-24 stsp } else
7709 6458efa5 2020-11-24 stsp *new_view = log_view;
7710 6458efa5 2020-11-24 stsp break;
7711 c42c9805 2020-11-24 stsp case 't':
7712 07b0611c 2022-06-23 thomas view->count = 0;
7713 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7714 c42c9805 2020-11-24 stsp break;
7715 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7716 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7717 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7718 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7719 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7720 c42c9805 2020-11-24 stsp break;
7721 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7722 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7723 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7724 444d5325 2022-07-03 thomas if (err)
7725 444d5325 2022-07-03 thomas break;
7726 444d5325 2022-07-03 thomas }
7727 e78dc838 2020-12-04 stsp view->focussed = 0;
7728 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7729 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7730 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7731 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7732 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7733 c42c9805 2020-11-24 stsp err = view_close_child(view);
7734 c42c9805 2020-11-24 stsp if (err)
7735 c42c9805 2020-11-24 stsp return err;
7736 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7737 40236d76 2022-06-23 thomas if (err)
7738 40236d76 2022-06-23 thomas return err;
7739 e78dc838 2020-12-04 stsp view->focus_child = 1;
7740 c42c9805 2020-11-24 stsp } else
7741 c42c9805 2020-11-24 stsp *new_view = tree_view;
7742 c42c9805 2020-11-24 stsp break;
7743 e4526bf5 2021-09-03 naddy case 'g':
7744 e4526bf5 2021-09-03 naddy case KEY_HOME:
7745 e4526bf5 2021-09-03 naddy s->selected = 0;
7746 07b0611c 2022-06-23 thomas view->count = 0;
7747 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7748 e4526bf5 2021-09-03 naddy break;
7749 e4526bf5 2021-09-03 naddy case 'G':
7750 a5d43cac 2022-07-01 thomas case KEY_END: {
7751 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7752 a5d43cac 2022-07-01 thomas
7753 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7754 a5d43cac 2022-07-01 thomas --eos; /* border */
7755 e4526bf5 2021-09-03 naddy s->selected = 0;
7756 07b0611c 2022-06-23 thomas view->count = 0;
7757 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7758 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7759 e4526bf5 2021-09-03 naddy if (re == NULL)
7760 e4526bf5 2021-09-03 naddy break;
7761 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7762 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7763 e4526bf5 2021-09-03 naddy }
7764 e4526bf5 2021-09-03 naddy if (n > 0)
7765 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7766 e4526bf5 2021-09-03 naddy break;
7767 a5d43cac 2022-07-01 thomas }
7768 6458efa5 2020-11-24 stsp case 'k':
7769 6458efa5 2020-11-24 stsp case KEY_UP:
7770 f7140bf5 2021-10-17 thomas case CTRL('p'):
7771 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7772 6458efa5 2020-11-24 stsp s->selected--;
7773 6458efa5 2020-11-24 stsp break;
7774 34ba6917 2020-11-30 stsp }
7775 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7776 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7777 07b0611c 2022-06-23 thomas view->count = 0;
7778 6458efa5 2020-11-24 stsp break;
7779 70f17a53 2022-06-13 thomas case CTRL('u'):
7780 23427b14 2022-06-23 thomas case 'u':
7781 70f17a53 2022-06-13 thomas nscroll /= 2;
7782 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7783 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7784 6458efa5 2020-11-24 stsp case CTRL('b'):
7785 1c5e5faa 2022-06-23 thomas case 'b':
7786 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7787 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7788 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7789 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7790 07b0611c 2022-06-23 thomas view->count = 0;
7791 6458efa5 2020-11-24 stsp break;
7792 6458efa5 2020-11-24 stsp case 'j':
7793 6458efa5 2020-11-24 stsp case KEY_DOWN:
7794 f7140bf5 2021-10-17 thomas case CTRL('n'):
7795 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7796 6458efa5 2020-11-24 stsp s->selected++;
7797 6458efa5 2020-11-24 stsp break;
7798 6458efa5 2020-11-24 stsp }
7799 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7800 6458efa5 2020-11-24 stsp /* can't scroll any further */
7801 07b0611c 2022-06-23 thomas view->count = 0;
7802 6458efa5 2020-11-24 stsp break;
7803 07b0611c 2022-06-23 thomas }
7804 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7805 6458efa5 2020-11-24 stsp break;
7806 70f17a53 2022-06-13 thomas case CTRL('d'):
7807 23427b14 2022-06-23 thomas case 'd':
7808 70f17a53 2022-06-13 thomas nscroll /= 2;
7809 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7810 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7811 6458efa5 2020-11-24 stsp case CTRL('f'):
7812 1c5e5faa 2022-06-23 thomas case 'f':
7813 4c2d69cb 2022-06-23 thomas case ' ':
7814 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7815 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7816 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7817 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7818 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7819 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7820 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7821 07b0611c 2022-06-23 thomas view->count = 0;
7822 6458efa5 2020-11-24 stsp break;
7823 6458efa5 2020-11-24 stsp }
7824 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7825 6458efa5 2020-11-24 stsp break;
7826 6458efa5 2020-11-24 stsp case CTRL('l'):
7827 07b0611c 2022-06-23 thomas view->count = 0;
7828 8924d611 2020-12-26 stsp tog_free_refs();
7829 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7830 8924d611 2020-12-26 stsp if (err)
7831 8924d611 2020-12-26 stsp break;
7832 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7833 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7834 6458efa5 2020-11-24 stsp break;
7835 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7836 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7837 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7838 6458efa5 2020-11-24 stsp break;
7839 6458efa5 2020-11-24 stsp default:
7840 07b0611c 2022-06-23 thomas view->count = 0;
7841 6458efa5 2020-11-24 stsp break;
7842 6458efa5 2020-11-24 stsp }
7843 6458efa5 2020-11-24 stsp
7844 6458efa5 2020-11-24 stsp return err;
7845 6458efa5 2020-11-24 stsp }
7846 6458efa5 2020-11-24 stsp
7847 6458efa5 2020-11-24 stsp __dead static void
7848 6458efa5 2020-11-24 stsp usage_ref(void)
7849 6458efa5 2020-11-24 stsp {
7850 6458efa5 2020-11-24 stsp endwin();
7851 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7852 6458efa5 2020-11-24 stsp getprogname());
7853 6458efa5 2020-11-24 stsp exit(1);
7854 6458efa5 2020-11-24 stsp }
7855 6458efa5 2020-11-24 stsp
7856 6458efa5 2020-11-24 stsp static const struct got_error *
7857 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7858 6458efa5 2020-11-24 stsp {
7859 6458efa5 2020-11-24 stsp const struct got_error *error;
7860 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7861 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7862 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7863 6458efa5 2020-11-24 stsp int ch;
7864 6458efa5 2020-11-24 stsp struct tog_view *view;
7865 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7866 6458efa5 2020-11-24 stsp
7867 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7868 6458efa5 2020-11-24 stsp switch (ch) {
7869 6458efa5 2020-11-24 stsp case 'r':
7870 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7871 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7872 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7873 6458efa5 2020-11-24 stsp optarg);
7874 6458efa5 2020-11-24 stsp break;
7875 6458efa5 2020-11-24 stsp default:
7876 e99e2d15 2020-11-24 naddy usage_ref();
7877 6458efa5 2020-11-24 stsp /* NOTREACHED */
7878 6458efa5 2020-11-24 stsp }
7879 6458efa5 2020-11-24 stsp }
7880 6458efa5 2020-11-24 stsp
7881 6458efa5 2020-11-24 stsp argc -= optind;
7882 6458efa5 2020-11-24 stsp argv += optind;
7883 6458efa5 2020-11-24 stsp
7884 6458efa5 2020-11-24 stsp if (argc > 1)
7885 e99e2d15 2020-11-24 naddy usage_ref();
7886 7cd52833 2022-06-23 thomas
7887 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7888 7cd52833 2022-06-23 thomas if (error != NULL)
7889 7cd52833 2022-06-23 thomas goto done;
7890 6458efa5 2020-11-24 stsp
7891 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7892 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7893 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7894 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7895 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7896 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7897 c156c7a4 2020-12-18 stsp goto done;
7898 6458efa5 2020-11-24 stsp if (worktree)
7899 6458efa5 2020-11-24 stsp repo_path =
7900 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7901 6458efa5 2020-11-24 stsp else
7902 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7903 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7904 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7905 c156c7a4 2020-12-18 stsp goto done;
7906 c156c7a4 2020-12-18 stsp }
7907 6458efa5 2020-11-24 stsp }
7908 6458efa5 2020-11-24 stsp
7909 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7910 6458efa5 2020-11-24 stsp if (error != NULL)
7911 6458efa5 2020-11-24 stsp goto done;
7912 6458efa5 2020-11-24 stsp
7913 6458efa5 2020-11-24 stsp init_curses();
7914 6458efa5 2020-11-24 stsp
7915 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7916 51a10b52 2020-12-26 stsp if (error)
7917 51a10b52 2020-12-26 stsp goto done;
7918 51a10b52 2020-12-26 stsp
7919 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7920 6458efa5 2020-11-24 stsp if (error)
7921 6458efa5 2020-11-24 stsp goto done;
7922 6458efa5 2020-11-24 stsp
7923 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7924 6458efa5 2020-11-24 stsp if (view == NULL) {
7925 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7926 6458efa5 2020-11-24 stsp goto done;
7927 6458efa5 2020-11-24 stsp }
7928 6458efa5 2020-11-24 stsp
7929 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7930 6458efa5 2020-11-24 stsp if (error)
7931 6458efa5 2020-11-24 stsp goto done;
7932 6458efa5 2020-11-24 stsp
7933 6458efa5 2020-11-24 stsp if (worktree) {
7934 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7935 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7936 6458efa5 2020-11-24 stsp worktree = NULL;
7937 6458efa5 2020-11-24 stsp }
7938 6458efa5 2020-11-24 stsp error = view_loop(view);
7939 6458efa5 2020-11-24 stsp done:
7940 6458efa5 2020-11-24 stsp free(repo_path);
7941 6458efa5 2020-11-24 stsp free(cwd);
7942 1d0f4054 2021-06-17 stsp if (repo) {
7943 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7944 1d0f4054 2021-06-17 stsp if (close_err)
7945 1d0f4054 2021-06-17 stsp error = close_err;
7946 1d0f4054 2021-06-17 stsp }
7947 7cd52833 2022-06-23 thomas if (pack_fds) {
7948 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7949 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7950 7cd52833 2022-06-23 thomas if (error == NULL)
7951 7cd52833 2022-06-23 thomas error = pack_err;
7952 7cd52833 2022-06-23 thomas }
7953 51a10b52 2020-12-26 stsp tog_free_refs();
7954 6458efa5 2020-11-24 stsp return error;
7955 6458efa5 2020-11-24 stsp }
7956 6458efa5 2020-11-24 stsp
7957 a5d43cac 2022-07-01 thomas /*
7958 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7959 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7960 a5d43cac 2022-07-01 thomas */
7961 6458efa5 2020-11-24 stsp static void
7962 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7963 a5d43cac 2022-07-01 thomas {
7964 a5d43cac 2022-07-01 thomas switch (view->type) {
7965 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7966 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7967 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7968 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7969 a5d43cac 2022-07-01 thomas 1);
7970 a5d43cac 2022-07-01 thomas break;
7971 a5d43cac 2022-07-01 thomas }
7972 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7973 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7974 a5d43cac 2022-07-01 thomas else
7975 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7976 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7977 a5d43cac 2022-07-01 thomas break;
7978 a5d43cac 2022-07-01 thomas }
7979 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7980 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7981 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7982 a5d43cac 2022-07-01 thomas break;
7983 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7984 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7985 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7986 a5d43cac 2022-07-01 thomas break;
7987 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7988 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7989 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7990 a5d43cac 2022-07-01 thomas break;
7991 a5d43cac 2022-07-01 thomas default:
7992 a5d43cac 2022-07-01 thomas break;
7993 a5d43cac 2022-07-01 thomas }
7994 a5d43cac 2022-07-01 thomas
7995 a5d43cac 2022-07-01 thomas view->offset = 0;
7996 a5d43cac 2022-07-01 thomas }
7997 a5d43cac 2022-07-01 thomas
7998 a5d43cac 2022-07-01 thomas /*
7999 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8000 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8001 a5d43cac 2022-07-01 thomas */
8002 a5d43cac 2022-07-01 thomas static const struct got_error *
8003 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8004 a5d43cac 2022-07-01 thomas {
8005 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8006 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8007 a5d43cac 2022-07-01 thomas int *selected = NULL;
8008 a5d43cac 2022-07-01 thomas int header, offset;
8009 a5d43cac 2022-07-01 thomas
8010 a5d43cac 2022-07-01 thomas switch (view->type) {
8011 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8012 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8013 a5d43cac 2022-07-01 thomas header = 3;
8014 a5d43cac 2022-07-01 thomas scrolld = NULL;
8015 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8016 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8017 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8018 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8019 a5d43cac 2022-07-01 thomas view->offset = offset;
8020 a5d43cac 2022-07-01 thomas }
8021 a5d43cac 2022-07-01 thomas break;
8022 a5d43cac 2022-07-01 thomas }
8023 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8024 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8025 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8026 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8027 a5d43cac 2022-07-01 thomas selected = &s->selected;
8028 a5d43cac 2022-07-01 thomas break;
8029 a5d43cac 2022-07-01 thomas }
8030 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8031 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8032 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8033 a5d43cac 2022-07-01 thomas header = 3;
8034 a5d43cac 2022-07-01 thomas selected = &s->selected;
8035 a5d43cac 2022-07-01 thomas break;
8036 a5d43cac 2022-07-01 thomas }
8037 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8038 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8039 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8040 a5d43cac 2022-07-01 thomas header = 5;
8041 a5d43cac 2022-07-01 thomas selected = &s->selected;
8042 a5d43cac 2022-07-01 thomas break;
8043 a5d43cac 2022-07-01 thomas }
8044 a5d43cac 2022-07-01 thomas default:
8045 a5d43cac 2022-07-01 thomas selected = NULL;
8046 a5d43cac 2022-07-01 thomas scrolld = NULL;
8047 a5d43cac 2022-07-01 thomas header = 0;
8048 a5d43cac 2022-07-01 thomas break;
8049 a5d43cac 2022-07-01 thomas }
8050 a5d43cac 2022-07-01 thomas
8051 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8052 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8053 a5d43cac 2022-07-01 thomas view->offset = offset;
8054 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8055 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8056 a5d43cac 2022-07-01 thomas *selected -= offset;
8057 a5d43cac 2022-07-01 thomas }
8058 a5d43cac 2022-07-01 thomas }
8059 a5d43cac 2022-07-01 thomas
8060 a5d43cac 2022-07-01 thomas return err;
8061 a5d43cac 2022-07-01 thomas }
8062 a5d43cac 2022-07-01 thomas
8063 a5d43cac 2022-07-01 thomas static void
8064 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8065 ce5b7c56 2019-07-09 stsp {
8066 6059809a 2020-12-17 stsp size_t i;
8067 9f7d7167 2018-04-29 stsp
8068 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8069 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8070 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8071 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8072 ce5b7c56 2019-07-09 stsp }
8073 6879ba42 2020-10-01 naddy fputc('\n', fp);
8074 ce5b7c56 2019-07-09 stsp }
8075 ce5b7c56 2019-07-09 stsp
8076 4ed7e80c 2018-05-20 stsp __dead static void
8077 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8078 9f7d7167 2018-04-29 stsp {
8079 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8080 6879ba42 2020-10-01 naddy
8081 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8082 6879ba42 2020-10-01 naddy getprogname());
8083 6879ba42 2020-10-01 naddy if (hflag) {
8084 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8085 6879ba42 2020-10-01 naddy list_commands(fp);
8086 ee85c5e8 2020-02-29 stsp }
8087 6879ba42 2020-10-01 naddy exit(status);
8088 9f7d7167 2018-04-29 stsp }
8089 9f7d7167 2018-04-29 stsp
8090 c2301be8 2018-04-30 stsp static char **
8091 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8092 c2301be8 2018-04-30 stsp {
8093 ee85c5e8 2020-02-29 stsp va_list ap;
8094 c2301be8 2018-04-30 stsp char **argv;
8095 ee85c5e8 2020-02-29 stsp int i;
8096 c2301be8 2018-04-30 stsp
8097 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8098 ee85c5e8 2020-02-29 stsp
8099 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8100 c2301be8 2018-04-30 stsp if (argv == NULL)
8101 c2301be8 2018-04-30 stsp err(1, "calloc");
8102 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8103 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8104 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8105 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8106 c2301be8 2018-04-30 stsp }
8107 c2301be8 2018-04-30 stsp
8108 ee85c5e8 2020-02-29 stsp va_end(ap);
8109 c2301be8 2018-04-30 stsp return argv;
8110 ee85c5e8 2020-02-29 stsp }
8111 ee85c5e8 2020-02-29 stsp
8112 ee85c5e8 2020-02-29 stsp /*
8113 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8114 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8115 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8116 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8117 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8118 ee85c5e8 2020-02-29 stsp */
8119 ee85c5e8 2020-02-29 stsp static const struct got_error *
8120 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8121 ee85c5e8 2020-02-29 stsp {
8122 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8123 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8124 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8125 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8126 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8127 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8128 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8129 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8130 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8131 84de9106 2020-12-26 stsp
8132 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8133 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8134 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8135 ee85c5e8 2020-02-29 stsp
8136 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8137 7cd52833 2022-06-23 thomas if (error != NULL)
8138 7cd52833 2022-06-23 thomas goto done;
8139 7cd52833 2022-06-23 thomas
8140 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8141 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8142 ee85c5e8 2020-02-29 stsp goto done;
8143 ee85c5e8 2020-02-29 stsp
8144 ee85c5e8 2020-02-29 stsp if (worktree)
8145 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8146 ee85c5e8 2020-02-29 stsp else
8147 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8148 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8149 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8150 ee85c5e8 2020-02-29 stsp goto done;
8151 ee85c5e8 2020-02-29 stsp }
8152 ee85c5e8 2020-02-29 stsp
8153 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8154 ee85c5e8 2020-02-29 stsp if (error != NULL)
8155 ee85c5e8 2020-02-29 stsp goto done;
8156 ee85c5e8 2020-02-29 stsp
8157 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8158 ee85c5e8 2020-02-29 stsp repo, worktree);
8159 ee85c5e8 2020-02-29 stsp if (error)
8160 ee85c5e8 2020-02-29 stsp goto done;
8161 ee85c5e8 2020-02-29 stsp
8162 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8163 84de9106 2020-12-26 stsp if (error)
8164 84de9106 2020-12-26 stsp goto done;
8165 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8166 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8167 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8168 ee85c5e8 2020-02-29 stsp if (error)
8169 ee85c5e8 2020-02-29 stsp goto done;
8170 ee85c5e8 2020-02-29 stsp
8171 ee85c5e8 2020-02-29 stsp if (worktree) {
8172 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8173 ee85c5e8 2020-02-29 stsp worktree = NULL;
8174 ee85c5e8 2020-02-29 stsp }
8175 ee85c5e8 2020-02-29 stsp
8176 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8177 945f9229 2022-04-16 thomas if (error)
8178 945f9229 2022-04-16 thomas goto done;
8179 945f9229 2022-04-16 thomas
8180 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8181 ee85c5e8 2020-02-29 stsp if (error) {
8182 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8183 ee85c5e8 2020-02-29 stsp goto done;
8184 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8185 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8186 6879ba42 2020-10-01 naddy usage(1, 1);
8187 ee85c5e8 2020-02-29 stsp /* not reached */
8188 ee85c5e8 2020-02-29 stsp }
8189 ee85c5e8 2020-02-29 stsp
8190 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8191 1d0f4054 2021-06-17 stsp if (error == NULL)
8192 1d0f4054 2021-06-17 stsp error = close_err;
8193 ee85c5e8 2020-02-29 stsp repo = NULL;
8194 ee85c5e8 2020-02-29 stsp
8195 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8196 ee85c5e8 2020-02-29 stsp if (error)
8197 ee85c5e8 2020-02-29 stsp goto done;
8198 ee85c5e8 2020-02-29 stsp
8199 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8200 ee85c5e8 2020-02-29 stsp argc = 4;
8201 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8202 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8203 ee85c5e8 2020-02-29 stsp done:
8204 1d0f4054 2021-06-17 stsp if (repo) {
8205 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8206 1d0f4054 2021-06-17 stsp if (error == NULL)
8207 1d0f4054 2021-06-17 stsp error = close_err;
8208 1d0f4054 2021-06-17 stsp }
8209 945f9229 2022-04-16 thomas if (commit)
8210 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8211 ee85c5e8 2020-02-29 stsp if (worktree)
8212 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8213 7cd52833 2022-06-23 thomas if (pack_fds) {
8214 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8215 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8216 7cd52833 2022-06-23 thomas if (error == NULL)
8217 7cd52833 2022-06-23 thomas error = pack_err;
8218 7cd52833 2022-06-23 thomas }
8219 ee85c5e8 2020-02-29 stsp free(id);
8220 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8221 ee85c5e8 2020-02-29 stsp free(commit_id);
8222 ee85c5e8 2020-02-29 stsp free(cwd);
8223 ee85c5e8 2020-02-29 stsp free(repo_path);
8224 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8225 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8226 ee85c5e8 2020-02-29 stsp int i;
8227 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8228 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8229 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8230 ee85c5e8 2020-02-29 stsp }
8231 87670572 2020-12-26 naddy tog_free_refs();
8232 ee85c5e8 2020-02-29 stsp return error;
8233 c2301be8 2018-04-30 stsp }
8234 c2301be8 2018-04-30 stsp
8235 9f7d7167 2018-04-29 stsp int
8236 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8237 9f7d7167 2018-04-29 stsp {
8238 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8239 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8240 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8241 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8242 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8243 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8244 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8245 83cd27f8 2020-01-13 stsp };
8246 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8247 9f7d7167 2018-04-29 stsp
8248 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8249 9f7d7167 2018-04-29 stsp
8250 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8251 9f7d7167 2018-04-29 stsp switch (ch) {
8252 9f7d7167 2018-04-29 stsp case 'h':
8253 9f7d7167 2018-04-29 stsp hflag = 1;
8254 9f7d7167 2018-04-29 stsp break;
8255 53ccebc2 2019-07-30 stsp case 'V':
8256 53ccebc2 2019-07-30 stsp Vflag = 1;
8257 53ccebc2 2019-07-30 stsp break;
8258 9f7d7167 2018-04-29 stsp default:
8259 6879ba42 2020-10-01 naddy usage(hflag, 1);
8260 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8261 9f7d7167 2018-04-29 stsp }
8262 9f7d7167 2018-04-29 stsp }
8263 9f7d7167 2018-04-29 stsp
8264 9f7d7167 2018-04-29 stsp argc -= optind;
8265 9f7d7167 2018-04-29 stsp argv += optind;
8266 9814e6a3 2020-09-27 naddy optind = 1;
8267 c2301be8 2018-04-30 stsp optreset = 1;
8268 9f7d7167 2018-04-29 stsp
8269 53ccebc2 2019-07-30 stsp if (Vflag) {
8270 53ccebc2 2019-07-30 stsp got_version_print_str();
8271 6879ba42 2020-10-01 naddy return 0;
8272 53ccebc2 2019-07-30 stsp }
8273 4010e238 2020-12-04 stsp
8274 4010e238 2020-12-04 stsp #ifndef PROFILE
8275 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8276 4010e238 2020-12-04 stsp NULL) == -1)
8277 4010e238 2020-12-04 stsp err(1, "pledge");
8278 4010e238 2020-12-04 stsp #endif
8279 53ccebc2 2019-07-30 stsp
8280 c2301be8 2018-04-30 stsp if (argc == 0) {
8281 f29d3e89 2018-06-23 stsp if (hflag)
8282 6879ba42 2020-10-01 naddy usage(hflag, 0);
8283 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8284 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8285 c2301be8 2018-04-30 stsp argc = 1;
8286 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8287 c2301be8 2018-04-30 stsp } else {
8288 6059809a 2020-12-17 stsp size_t i;
8289 9f7d7167 2018-04-29 stsp
8290 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8291 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8292 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8293 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8294 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8295 9f7d7167 2018-04-29 stsp break;
8296 9f7d7167 2018-04-29 stsp }
8297 9f7d7167 2018-04-29 stsp }
8298 ee85c5e8 2020-02-29 stsp }
8299 3642c4c6 2019-07-09 stsp
8300 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8301 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8302 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8303 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8304 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8305 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8306 adf4c9e0 2022-07-03 thomas }
8307 adf4c9e0 2022-07-03 thomas
8308 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8309 ee85c5e8 2020-02-29 stsp if (argc != 1)
8310 6879ba42 2020-10-01 naddy usage(0, 1);
8311 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8312 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8313 ee85c5e8 2020-02-29 stsp } else {
8314 ee85c5e8 2020-02-29 stsp if (hflag)
8315 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8316 ee85c5e8 2020-02-29 stsp else
8317 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8318 9f7d7167 2018-04-29 stsp }
8319 9f7d7167 2018-04-29 stsp
8320 9f7d7167 2018-04-29 stsp endwin();
8321 b46c1e04 2020-09-20 naddy putchar('\n');
8322 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8323 a2f4a359 2020-02-28 stsp int i;
8324 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8325 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8326 a2f4a359 2020-02-28 stsp free(cmd_argv);
8327 a2f4a359 2020-02-28 stsp }
8328 a2f4a359 2020-02-28 stsp
8329 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8330 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8331 9f7d7167 2018-04-29 stsp return 0;
8332 9f7d7167 2018-04-29 stsp }