Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 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 82c78e96 2022-08-06 thomas int lineno;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
335 fe621944 2020-11-10 stsp size_t nlines;
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 7e8004ba 2022-09-11 thomas struct commit_queue *real_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 7e8004ba 2022-09-11 thomas int *limiting;
365 7e8004ba 2022-09-11 thomas int limit_match;
366 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
367 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
368 1a76625f 2018-10-22 stsp };
369 1a76625f 2018-10-22 stsp
370 1a76625f 2018-10-22 stsp struct tog_log_view_state {
371 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
372 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
373 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
374 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
375 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
376 b01e7d3b 2018-08-04 stsp int selected;
377 b01e7d3b 2018-08-04 stsp char *in_repo_path;
378 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
379 b672a97a 2020-01-27 stsp int log_branches;
380 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
381 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
382 1a76625f 2018-10-22 stsp sig_atomic_t quit;
383 1a76625f 2018-10-22 stsp pthread_t thread;
384 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
385 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
386 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
387 11b20872 2019-11-08 stsp struct tog_colors colors;
388 f69c5a46 2022-07-19 thomas int use_committer;
389 7e8004ba 2022-09-11 thomas int limit_view;
390 7e8004ba 2022-09-11 thomas regex_t limit_regex;
391 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
392 ba4f502b 2018-08-04 stsp };
393 11b20872 2019-11-08 stsp
394 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
395 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
396 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
397 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
398 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
399 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
400 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
401 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
402 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
403 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
404 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
405 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
406 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
407 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
408 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
409 ba4f502b 2018-08-04 stsp
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
411 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
412 e9424729 2018-08-04 stsp int nlines;
413 e9424729 2018-08-04 stsp
414 e9424729 2018-08-04 stsp struct tog_view *view;
415 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
416 e9424729 2018-08-04 stsp int *quit;
417 e9424729 2018-08-04 stsp };
418 e9424729 2018-08-04 stsp
419 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
420 e9424729 2018-08-04 stsp const char *path;
421 e9424729 2018-08-04 stsp struct got_repository *repo;
422 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
423 e9424729 2018-08-04 stsp int *complete;
424 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
425 fc06ba56 2019-08-22 stsp void *cancel_arg;
426 e9424729 2018-08-04 stsp };
427 e9424729 2018-08-04 stsp
428 e9424729 2018-08-04 stsp struct tog_blame {
429 e9424729 2018-08-04 stsp FILE *f;
430 be659d10 2020-11-18 stsp off_t filesize;
431 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
432 6fcac457 2018-11-19 stsp int nlines;
433 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
434 e9424729 2018-08-04 stsp pthread_t thread;
435 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
436 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
437 e9424729 2018-08-04 stsp const char *path;
438 7cd52833 2022-06-23 thomas int *pack_fds;
439 e9424729 2018-08-04 stsp };
440 e9424729 2018-08-04 stsp
441 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
442 7cbe629d 2018-08-04 stsp int first_displayed_line;
443 7cbe629d 2018-08-04 stsp int last_displayed_line;
444 7cbe629d 2018-08-04 stsp int selected_line;
445 4fc71f3b 2022-07-12 thomas int last_diffed_line;
446 7cbe629d 2018-08-04 stsp int blame_complete;
447 e5a0f69f 2018-08-18 stsp int eof;
448 e5a0f69f 2018-08-18 stsp int done;
449 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
450 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
451 e5a0f69f 2018-08-18 stsp char *path;
452 7cbe629d 2018-08-04 stsp struct got_repository *repo;
453 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
454 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
455 e9424729 2018-08-04 stsp struct tog_blame blame;
456 6c4c42e0 2019-06-24 stsp int matched_line;
457 11b20872 2019-11-08 stsp struct tog_colors colors;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
461 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
462 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 ad80ab7b 2018-08-04 stsp int selected;
466 ad80ab7b 2018-08-04 stsp };
467 ad80ab7b 2018-08-04 stsp
468 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
469 ad80ab7b 2018-08-04 stsp
470 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
471 ad80ab7b 2018-08-04 stsp char *tree_label;
472 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
473 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
474 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
475 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
479 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
480 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
481 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
482 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
483 6458efa5 2020-11-24 stsp struct tog_colors colors;
484 6458efa5 2020-11-24 stsp };
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
487 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
488 6458efa5 2020-11-24 stsp struct got_reference *ref;
489 6458efa5 2020-11-24 stsp int idx;
490 6458efa5 2020-11-24 stsp };
491 6458efa5 2020-11-24 stsp
492 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
493 6458efa5 2020-11-24 stsp
494 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
495 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
496 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
497 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
498 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
499 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
500 6458efa5 2020-11-24 stsp struct got_repository *repo;
501 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
502 bddb1296 2019-11-08 stsp struct tog_colors colors;
503 7cbe629d 2018-08-04 stsp };
504 7cbe629d 2018-08-04 stsp
505 669b5ffa 2018-10-07 stsp /*
506 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
507 669b5ffa 2018-10-07 stsp *
508 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
509 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
510 669b5ffa 2018-10-07 stsp * there is enough screen estate.
511 669b5ffa 2018-10-07 stsp *
512 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
513 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
514 669b5ffa 2018-10-07 stsp *
515 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
516 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
517 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
518 669b5ffa 2018-10-07 stsp *
519 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
520 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
521 669b5ffa 2018-10-07 stsp */
522 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
523 669b5ffa 2018-10-07 stsp
524 cc3c9aac 2018-08-01 stsp struct tog_view {
525 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
526 26ed57b2 2018-05-19 stsp WINDOW *window;
527 26ed57b2 2018-05-19 stsp PANEL *panel;
528 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
529 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
530 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
531 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
532 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
533 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
534 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
535 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
536 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
537 9970f7fc 2020-12-03 stsp int dying;
538 669b5ffa 2018-10-07 stsp struct tog_view *parent;
539 669b5ffa 2018-10-07 stsp struct tog_view *child;
540 5dc9f4bc 2018-08-04 stsp
541 e78dc838 2020-12-04 stsp /*
542 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
543 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
544 e78dc838 2020-12-04 stsp * between parent and child.
545 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
546 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
547 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
548 e78dc838 2020-12-04 stsp * situations.
549 e78dc838 2020-12-04 stsp */
550 e78dc838 2020-12-04 stsp int focus_child;
551 e78dc838 2020-12-04 stsp
552 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
553 5dc9f4bc 2018-08-04 stsp /* type-specific state */
554 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
555 5dc9f4bc 2018-08-04 stsp union {
556 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
557 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
558 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
559 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
560 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
561 5dc9f4bc 2018-08-04 stsp } state;
562 e5a0f69f 2018-08-18 stsp
563 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
565 e78dc838 2020-12-04 stsp struct tog_view *, int);
566 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
567 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
568 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
569 60493ae3 2019-06-20 stsp
570 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
571 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
572 c0c4acc8 2021-01-24 stsp int search_started;
573 60493ae3 2019-06-20 stsp int searching;
574 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
575 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
576 60493ae3 2019-06-20 stsp int search_next_done;
577 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
578 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
579 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
580 1803e47f 2019-06-22 stsp regex_t regex;
581 41605754 2020-11-12 stsp regmatch_t regmatch;
582 cc3c9aac 2018-08-01 stsp };
583 cd0acaa7 2018-05-20 stsp
584 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
585 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
586 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
587 78756c87 2020-11-24 stsp struct got_repository *);
588 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
593 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
594 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp
596 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
597 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
598 78756c87 2020-11-24 stsp const char *, const char *, int);
599 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
601 e78dc838 2020-12-04 stsp struct tog_view *, int);
602 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
603 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
604 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
605 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp
607 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
608 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
609 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
610 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
611 e78dc838 2020-12-04 stsp struct tog_view *, int);
612 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
614 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
615 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
616 e5a0f69f 2018-08-18 stsp
617 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
618 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
619 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
620 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
623 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
624 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp
626 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
627 6458efa5 2020-11-24 stsp struct got_repository *);
628 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
629 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
630 e78dc838 2020-12-04 stsp struct tog_view *, int);
631 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
632 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
633 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
634 25791caa 2018-10-24 stsp
635 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
636 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
637 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
638 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
639 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
640 25791caa 2018-10-24 stsp
641 25791caa 2018-10-24 stsp static void
642 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
643 25791caa 2018-10-24 stsp {
644 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
645 83baff54 2019-08-12 stsp }
646 83baff54 2019-08-12 stsp
647 83baff54 2019-08-12 stsp static void
648 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
649 83baff54 2019-08-12 stsp {
650 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
651 25791caa 2018-10-24 stsp }
652 26ed57b2 2018-05-19 stsp
653 61266923 2020-01-14 stsp static void
654 61266923 2020-01-14 stsp tog_sigcont(int signo)
655 61266923 2020-01-14 stsp {
656 61266923 2020-01-14 stsp tog_sigcont_received = 1;
657 61266923 2020-01-14 stsp }
658 61266923 2020-01-14 stsp
659 296152d1 2022-05-31 thomas static void
660 296152d1 2022-05-31 thomas tog_sigint(int signo)
661 296152d1 2022-05-31 thomas {
662 296152d1 2022-05-31 thomas tog_sigint_received = 1;
663 296152d1 2022-05-31 thomas }
664 296152d1 2022-05-31 thomas
665 296152d1 2022-05-31 thomas static void
666 296152d1 2022-05-31 thomas tog_sigterm(int signo)
667 296152d1 2022-05-31 thomas {
668 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
669 296152d1 2022-05-31 thomas }
670 296152d1 2022-05-31 thomas
671 296152d1 2022-05-31 thomas static int
672 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
673 296152d1 2022-05-31 thomas {
674 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
675 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
676 296152d1 2022-05-31 thomas }
677 296152d1 2022-05-31 thomas
678 e5a0f69f 2018-08-18 stsp static const struct got_error *
679 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
680 ea5e7bb5 2018-08-01 stsp {
681 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
682 e5a0f69f 2018-08-18 stsp
683 669b5ffa 2018-10-07 stsp if (view->child) {
684 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
685 669b5ffa 2018-10-07 stsp view->child = NULL;
686 669b5ffa 2018-10-07 stsp }
687 e5a0f69f 2018-08-18 stsp if (view->close)
688 e5a0f69f 2018-08-18 stsp err = view->close(view);
689 ea5e7bb5 2018-08-01 stsp if (view->panel)
690 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
691 ea5e7bb5 2018-08-01 stsp if (view->window)
692 ea5e7bb5 2018-08-01 stsp delwin(view->window);
693 ea5e7bb5 2018-08-01 stsp free(view);
694 f2d749db 2022-07-12 thomas return err ? err : child_err;
695 ea5e7bb5 2018-08-01 stsp }
696 ea5e7bb5 2018-08-01 stsp
697 ea5e7bb5 2018-08-01 stsp static struct tog_view *
698 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
699 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
700 ea5e7bb5 2018-08-01 stsp {
701 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
702 ea5e7bb5 2018-08-01 stsp
703 ea5e7bb5 2018-08-01 stsp if (view == NULL)
704 ea5e7bb5 2018-08-01 stsp return NULL;
705 ea5e7bb5 2018-08-01 stsp
706 d6b05b5b 2018-08-04 stsp view->type = type;
707 f7d12f7e 2018-08-01 stsp view->lines = LINES;
708 f7d12f7e 2018-08-01 stsp view->cols = COLS;
709 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
710 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
711 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
712 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
713 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
714 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
715 96a765a8 2018-08-04 stsp view_close(view);
716 ea5e7bb5 2018-08-01 stsp return NULL;
717 ea5e7bb5 2018-08-01 stsp }
718 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
719 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
720 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
721 96a765a8 2018-08-04 stsp view_close(view);
722 ea5e7bb5 2018-08-01 stsp return NULL;
723 ea5e7bb5 2018-08-01 stsp }
724 ea5e7bb5 2018-08-01 stsp
725 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
726 ea5e7bb5 2018-08-01 stsp return view;
727 cdf1ee82 2018-08-01 stsp }
728 cdf1ee82 2018-08-01 stsp
729 0cf4efb1 2018-09-29 stsp static int
730 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
731 0cf4efb1 2018-09-29 stsp {
732 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
733 0cf4efb1 2018-09-29 stsp return 0;
734 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
735 5c60c32a 2018-10-18 stsp }
736 5c60c32a 2018-10-18 stsp
737 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
738 a5d43cac 2022-07-01 thomas static int
739 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
740 a5d43cac 2022-07-01 thomas {
741 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
742 a5d43cac 2022-07-01 thomas }
743 a5d43cac 2022-07-01 thomas
744 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
745 5c60c32a 2018-10-18 stsp
746 5c60c32a 2018-10-18 stsp static const struct got_error *
747 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
748 5c60c32a 2018-10-18 stsp {
749 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
750 5c60c32a 2018-10-18 stsp
751 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
752 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
753 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
754 53d2bdd3 2022-07-10 thomas else
755 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
756 a5d43cac 2022-07-01 thomas view->begin_x = 0;
757 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
758 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
759 53d2bdd3 2022-07-10 thomas view->cols > 119)
760 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
761 53d2bdd3 2022-07-10 thomas else
762 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
763 a5d43cac 2022-07-01 thomas view->begin_y = 0;
764 a5d43cac 2022-07-01 thomas }
765 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
766 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
767 5c60c32a 2018-10-18 stsp view->lines = LINES;
768 5c60c32a 2018-10-18 stsp view->cols = COLS;
769 5c60c32a 2018-10-18 stsp err = view_resize(view);
770 5c60c32a 2018-10-18 stsp if (err)
771 5c60c32a 2018-10-18 stsp return err;
772 5c60c32a 2018-10-18 stsp
773 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
774 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
775 a5d43cac 2022-07-01 thomas
776 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp return NULL;
780 5c60c32a 2018-10-18 stsp }
781 5c60c32a 2018-10-18 stsp
782 5c60c32a 2018-10-18 stsp static const struct got_error *
783 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
784 5c60c32a 2018-10-18 stsp {
785 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
786 5c60c32a 2018-10-18 stsp
787 5c60c32a 2018-10-18 stsp view->begin_x = 0;
788 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
789 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
790 5c60c32a 2018-10-18 stsp view->ncols = COLS;
791 5c60c32a 2018-10-18 stsp view->lines = LINES;
792 5c60c32a 2018-10-18 stsp view->cols = COLS;
793 5c60c32a 2018-10-18 stsp err = view_resize(view);
794 5c60c32a 2018-10-18 stsp if (err)
795 5c60c32a 2018-10-18 stsp return err;
796 5c60c32a 2018-10-18 stsp
797 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
798 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
799 5c60c32a 2018-10-18 stsp
800 5c60c32a 2018-10-18 stsp return NULL;
801 0cf4efb1 2018-09-29 stsp }
802 0cf4efb1 2018-09-29 stsp
803 5c60c32a 2018-10-18 stsp static int
804 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
805 5c60c32a 2018-10-18 stsp {
806 5c60c32a 2018-10-18 stsp return view->parent == NULL;
807 5c60c32a 2018-10-18 stsp }
808 5c60c32a 2018-10-18 stsp
809 b65b3ea0 2022-06-23 thomas static int
810 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
811 b65b3ea0 2022-06-23 thomas {
812 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
813 e44940c3 2022-07-01 thomas }
814 e44940c3 2022-07-01 thomas
815 e44940c3 2022-07-01 thomas static int
816 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
817 e44940c3 2022-07-01 thomas {
818 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
819 b65b3ea0 2022-06-23 thomas }
820 b65b3ea0 2022-06-23 thomas
821 444d5325 2022-07-03 thomas static int
822 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
823 444d5325 2022-07-03 thomas {
824 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
825 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
826 444d5325 2022-07-03 thomas }
827 444d5325 2022-07-03 thomas
828 a5d43cac 2022-07-01 thomas static void
829 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
830 a5d43cac 2022-07-01 thomas {
831 a5d43cac 2022-07-01 thomas PANEL *panel;
832 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
833 b65b3ea0 2022-06-23 thomas
834 a5d43cac 2022-07-01 thomas if (view->parent)
835 a5d43cac 2022-07-01 thomas return view_border(view->parent);
836 a5d43cac 2022-07-01 thomas
837 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
838 a5d43cac 2022-07-01 thomas if (panel == NULL)
839 a5d43cac 2022-07-01 thomas return;
840 a5d43cac 2022-07-01 thomas
841 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
842 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
843 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
844 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
845 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
846 a5d43cac 2022-07-01 thomas else
847 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
848 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
849 a5d43cac 2022-07-01 thomas }
850 a5d43cac 2022-07-01 thomas
851 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
852 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
853 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
854 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
855 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
856 a5d43cac 2022-07-01 thomas
857 4d8c2215 2018-08-19 stsp static const struct got_error *
858 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
859 f7d12f7e 2018-08-01 stsp {
860 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
861 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
862 f7d12f7e 2018-08-01 stsp
863 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
864 a5d43cac 2022-07-01 thomas
865 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
866 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
867 0cf4efb1 2018-09-29 stsp else
868 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
869 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
870 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
871 0cf4efb1 2018-09-29 stsp else
872 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
873 6d0fee91 2018-08-01 stsp
874 4918811f 2022-07-01 thomas if (view->child) {
875 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
876 a5d43cac 2022-07-01 thomas
877 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
878 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
879 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
880 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
881 40236d76 2022-06-23 thomas ncols = COLS;
882 40236d76 2022-06-23 thomas
883 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
884 5c60c32a 2018-10-18 stsp if (view->child->focussed)
885 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
886 5c60c32a 2018-10-18 stsp else
887 5c60c32a 2018-10-18 stsp show_panel(view->panel);
888 5c60c32a 2018-10-18 stsp } else {
889 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
890 40236d76 2022-06-23 thomas
891 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
892 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
893 a5d43cac 2022-07-01 thomas }
894 a5d43cac 2022-07-01 thomas /*
895 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
896 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
897 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
898 a5d43cac 2022-07-01 thomas */
899 a5d43cac 2022-07-01 thomas if (hs) {
900 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
901 a5d43cac 2022-07-01 thomas if (err)
902 a5d43cac 2022-07-01 thomas return err;
903 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
904 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
905 a5d43cac 2022-07-01 thomas if (err)
906 a5d43cac 2022-07-01 thomas return err;
907 a5d43cac 2022-07-01 thomas }
908 a5d43cac 2022-07-01 thomas view_border(view);
909 a5d43cac 2022-07-01 thomas update_panels();
910 a5d43cac 2022-07-01 thomas doupdate();
911 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
912 a5d43cac 2022-07-01 thomas nlines = view->nlines;
913 a5d43cac 2022-07-01 thomas }
914 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
915 40236d76 2022-06-23 thomas ncols = COLS;
916 669b5ffa 2018-10-07 stsp
917 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
918 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
919 ea0bff04 2022-07-19 thomas if (err)
920 ea0bff04 2022-07-19 thomas return err;
921 ea0bff04 2022-07-19 thomas }
922 ea0bff04 2022-07-19 thomas
923 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
924 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
925 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
926 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
927 40236d76 2022-06-23 thomas wclear(view->window);
928 40236d76 2022-06-23 thomas
929 40236d76 2022-06-23 thomas view->nlines = nlines;
930 40236d76 2022-06-23 thomas view->ncols = ncols;
931 40236d76 2022-06-23 thomas view->lines = LINES;
932 40236d76 2022-06-23 thomas view->cols = COLS;
933 40236d76 2022-06-23 thomas
934 5c60c32a 2018-10-18 stsp return NULL;
935 ea0bff04 2022-07-19 thomas }
936 ea0bff04 2022-07-19 thomas
937 ea0bff04 2022-07-19 thomas static const struct got_error *
938 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
939 ea0bff04 2022-07-19 thomas {
940 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
941 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
942 3a0139e8 2022-07-22 thomas int n = 0;
943 ea0bff04 2022-07-19 thomas
944 3a0139e8 2022-07-22 thomas if (s->selected_entry)
945 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
946 3a0139e8 2022-07-22 thomas
947 ea0bff04 2022-07-19 thomas /*
948 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
949 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
950 ea0bff04 2022-07-19 thomas */
951 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
952 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
953 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
954 ea0bff04 2022-07-19 thomas }
955 ea0bff04 2022-07-19 thomas
956 ea0bff04 2022-07-19 thomas return err;
957 97cb21cd 2022-07-12 thomas }
958 97cb21cd 2022-07-12 thomas
959 97cb21cd 2022-07-12 thomas static void
960 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
961 97cb21cd 2022-07-12 thomas {
962 97cb21cd 2022-07-12 thomas if (n == 0)
963 97cb21cd 2022-07-12 thomas return;
964 97cb21cd 2022-07-12 thomas
965 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
966 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
967 97cb21cd 2022-07-12 thomas view->parent->offset += n;
968 97cb21cd 2022-07-12 thomas else
969 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
970 97cb21cd 2022-07-12 thomas } else if (view->offset) {
971 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
972 97cb21cd 2022-07-12 thomas view->offset -= n;
973 97cb21cd 2022-07-12 thomas else
974 97cb21cd 2022-07-12 thomas view->offset = 0;
975 97cb21cd 2022-07-12 thomas }
976 53d2bdd3 2022-07-10 thomas }
977 53d2bdd3 2022-07-10 thomas
978 53d2bdd3 2022-07-10 thomas static const struct got_error *
979 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
980 53d2bdd3 2022-07-10 thomas {
981 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
982 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
983 53d2bdd3 2022-07-10 thomas
984 53d2bdd3 2022-07-10 thomas if (view->parent)
985 53d2bdd3 2022-07-10 thomas v = view->parent;
986 53d2bdd3 2022-07-10 thomas else
987 53d2bdd3 2022-07-10 thomas v = view;
988 53d2bdd3 2022-07-10 thomas
989 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
990 53d2bdd3 2022-07-10 thomas return NULL;
991 53d2bdd3 2022-07-10 thomas
992 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
993 53d2bdd3 2022-07-10 thomas
994 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
995 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
996 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
997 53d2bdd3 2022-07-10 thomas if (view->parent)
998 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
999 53d2bdd3 2022-07-10 thomas else
1000 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1001 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1002 53d2bdd3 2022-07-10 thomas view->count = 0;
1003 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1004 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1005 53d2bdd3 2022-07-10 thomas view->count = 0;
1006 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1007 53d2bdd3 2022-07-10 thomas }
1008 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1009 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1010 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1011 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1012 53d2bdd3 2022-07-10 thomas if (err)
1013 53d2bdd3 2022-07-10 thomas return err;
1014 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1015 53d2bdd3 2022-07-10 thomas } else {
1016 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1017 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1018 53d2bdd3 2022-07-10 thomas if (view->parent)
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1020 53d2bdd3 2022-07-10 thomas else
1021 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1022 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1023 53d2bdd3 2022-07-10 thomas view->count = 0;
1024 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1025 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1026 53d2bdd3 2022-07-10 thomas view->count = 0;
1027 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1028 53d2bdd3 2022-07-10 thomas }
1029 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1030 53d2bdd3 2022-07-10 thomas }
1031 53d2bdd3 2022-07-10 thomas
1032 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1033 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1034 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1035 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1036 53d2bdd3 2022-07-10 thomas
1037 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1041 53d2bdd3 2022-07-10 thomas if (err)
1042 53d2bdd3 2022-07-10 thomas return err;
1043 53d2bdd3 2022-07-10 thomas
1044 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1045 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1046 53d2bdd3 2022-07-10 thomas if (err)
1047 53d2bdd3 2022-07-10 thomas return err;
1048 53d2bdd3 2022-07-10 thomas }
1049 53d2bdd3 2022-07-10 thomas
1050 3a0139e8 2022-07-22 thomas if (v->resize)
1051 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1052 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1053 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1054 53d2bdd3 2022-07-10 thomas
1055 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1056 53d2bdd3 2022-07-10 thomas
1057 53d2bdd3 2022-07-10 thomas return err;
1058 53d2bdd3 2022-07-10 thomas }
1059 53d2bdd3 2022-07-10 thomas
1060 53d2bdd3 2022-07-10 thomas static void
1061 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1062 53d2bdd3 2022-07-10 thomas {
1063 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1064 53d2bdd3 2022-07-10 thomas
1065 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1066 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1067 669b5ffa 2018-10-07 stsp }
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp static const struct got_error *
1070 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1071 669b5ffa 2018-10-07 stsp {
1072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1073 669b5ffa 2018-10-07 stsp
1074 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1075 669b5ffa 2018-10-07 stsp return NULL;
1076 669b5ffa 2018-10-07 stsp
1077 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1078 669b5ffa 2018-10-07 stsp view->child = NULL;
1079 669b5ffa 2018-10-07 stsp return err;
1080 669b5ffa 2018-10-07 stsp }
1081 669b5ffa 2018-10-07 stsp
1082 40236d76 2022-06-23 thomas static const struct got_error *
1083 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1084 669b5ffa 2018-10-07 stsp {
1085 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1086 53d2bdd3 2022-07-10 thomas
1087 669b5ffa 2018-10-07 stsp view->child = child;
1088 669b5ffa 2018-10-07 stsp child->parent = view;
1089 40236d76 2022-06-23 thomas
1090 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1091 53d2bdd3 2022-07-10 thomas if (err)
1092 53d2bdd3 2022-07-10 thomas return err;
1093 53d2bdd3 2022-07-10 thomas
1094 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1095 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1096 53d2bdd3 2022-07-10 thomas
1097 53d2bdd3 2022-07-10 thomas return err;
1098 bfddd0d9 2018-09-29 stsp }
1099 2a31b33b 2022-07-23 thomas
1100 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1101 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1102 2a31b33b 2022-07-23 thomas
1103 2a31b33b 2022-07-23 thomas static const struct got_error *
1104 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1105 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1106 2a31b33b 2022-07-23 thomas {
1107 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1108 2a31b33b 2022-07-23 thomas const struct got_error *err;
1109 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1110 2a31b33b 2022-07-23 thomas
1111 2a31b33b 2022-07-23 thomas *requested = NULL;
1112 2a31b33b 2022-07-23 thomas
1113 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1114 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1115 bfddd0d9 2018-09-29 stsp
1116 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1117 2a31b33b 2022-07-23 thomas if (err)
1118 2a31b33b 2022-07-23 thomas return err;
1119 2a31b33b 2022-07-23 thomas
1120 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1121 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1122 2a31b33b 2022-07-23 thomas if (err)
1123 2a31b33b 2022-07-23 thomas return err;
1124 2a31b33b 2022-07-23 thomas }
1125 2a31b33b 2022-07-23 thomas
1126 2a31b33b 2022-07-23 thomas view->focussed = 0;
1127 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1128 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1129 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1130 2a31b33b 2022-07-23 thomas
1131 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1132 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1133 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1134 2a31b33b 2022-07-23 thomas if (err)
1135 2a31b33b 2022-07-23 thomas return err;
1136 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1137 2a31b33b 2022-07-23 thomas if (err)
1138 2a31b33b 2022-07-23 thomas return err;
1139 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1140 2a31b33b 2022-07-23 thomas } else
1141 2a31b33b 2022-07-23 thomas *requested = new_view;
1142 2a31b33b 2022-07-23 thomas
1143 2a31b33b 2022-07-23 thomas return NULL;
1144 2a31b33b 2022-07-23 thomas }
1145 2a31b33b 2022-07-23 thomas
1146 34bc9ec9 2019-02-22 stsp static void
1147 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1148 25791caa 2018-10-24 stsp {
1149 25791caa 2018-10-24 stsp int cols, lines;
1150 25791caa 2018-10-24 stsp struct winsize size;
1151 25791caa 2018-10-24 stsp
1152 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1153 25791caa 2018-10-24 stsp cols = 80; /* Default */
1154 25791caa 2018-10-24 stsp lines = 24;
1155 25791caa 2018-10-24 stsp } else {
1156 25791caa 2018-10-24 stsp cols = size.ws_col;
1157 25791caa 2018-10-24 stsp lines = size.ws_row;
1158 25791caa 2018-10-24 stsp }
1159 25791caa 2018-10-24 stsp resize_term(lines, cols);
1160 2b49a8ae 2019-06-22 stsp }
1161 2b49a8ae 2019-06-22 stsp
1162 2b49a8ae 2019-06-22 stsp static const struct got_error *
1163 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1164 2b49a8ae 2019-06-22 stsp {
1165 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1166 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1167 2b49a8ae 2019-06-22 stsp char pattern[1024];
1168 2b49a8ae 2019-06-22 stsp int ret;
1169 c0c4acc8 2021-01-24 stsp
1170 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1171 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1172 c0c4acc8 2021-01-24 stsp view->searching = 0;
1173 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1174 c0c4acc8 2021-01-24 stsp }
1175 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1176 2b49a8ae 2019-06-22 stsp
1177 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1178 2b49a8ae 2019-06-22 stsp return NULL;
1179 2b49a8ae 2019-06-22 stsp
1180 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1181 a5d43cac 2022-07-01 thomas v = view->child;
1182 2b49a8ae 2019-06-22 stsp
1183 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1184 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1185 a5d43cac 2022-07-01 thomas
1186 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1187 2b49a8ae 2019-06-22 stsp nocbreak();
1188 2b49a8ae 2019-06-22 stsp echo();
1189 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1190 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1191 2b49a8ae 2019-06-22 stsp cbreak();
1192 2b49a8ae 2019-06-22 stsp noecho();
1193 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1194 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1195 2b49a8ae 2019-06-22 stsp return NULL;
1196 2b49a8ae 2019-06-22 stsp
1197 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1198 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1199 7c32bd05 2019-06-22 stsp if (err) {
1200 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1201 7c32bd05 2019-06-22 stsp return err;
1202 7c32bd05 2019-06-22 stsp }
1203 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1204 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1205 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1206 2b49a8ae 2019-06-22 stsp view->search_next(view);
1207 2b49a8ae 2019-06-22 stsp }
1208 2b49a8ae 2019-06-22 stsp
1209 2b49a8ae 2019-06-22 stsp return NULL;
1210 64486692 2022-07-07 thomas }
1211 64486692 2022-07-07 thomas
1212 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1213 64486692 2022-07-07 thomas static const struct got_error *
1214 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1215 64486692 2022-07-07 thomas {
1216 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1217 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1218 64486692 2022-07-07 thomas
1219 64486692 2022-07-07 thomas if (view->parent)
1220 64486692 2022-07-07 thomas v = view->parent;
1221 64486692 2022-07-07 thomas else
1222 64486692 2022-07-07 thomas v = view;
1223 64486692 2022-07-07 thomas
1224 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1225 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1226 ddbc4d37 2022-07-12 thomas else
1227 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1228 64486692 2022-07-07 thomas
1229 ddbc4d37 2022-07-12 thomas if (!v->child)
1230 ddbc4d37 2022-07-12 thomas return NULL;
1231 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1232 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1233 ddbc4d37 2022-07-12 thomas
1234 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1235 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1236 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1237 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1238 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1239 64486692 2022-07-07 thomas
1240 ddbc4d37 2022-07-12 thomas
1241 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1242 64486692 2022-07-07 thomas v->ncols = COLS;
1243 64486692 2022-07-07 thomas v->child->ncols = COLS;
1244 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1245 64486692 2022-07-07 thomas
1246 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas }
1250 64486692 2022-07-07 thomas v->child->mode = v->mode;
1251 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1252 64486692 2022-07-07 thomas v->focus_child = 1;
1253 64486692 2022-07-07 thomas
1254 64486692 2022-07-07 thomas err = view_fullscreen(v);
1255 64486692 2022-07-07 thomas if (err)
1256 64486692 2022-07-07 thomas return err;
1257 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1258 64486692 2022-07-07 thomas if (err)
1259 64486692 2022-07-07 thomas return err;
1260 64486692 2022-07-07 thomas
1261 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1262 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1263 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1264 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1265 cf1fe301 2022-07-29 thomas if (err)
1266 cf1fe301 2022-07-29 thomas return err;
1267 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1268 cf1fe301 2022-07-29 thomas if (err)
1269 cf1fe301 2022-07-29 thomas return err;
1270 ddbc4d37 2022-07-12 thomas } else {
1271 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1272 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1273 64486692 2022-07-07 thomas }
1274 f4e6231a 2022-07-22 thomas if (v->resize)
1275 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1276 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1277 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1278 64486692 2022-07-07 thomas
1279 64486692 2022-07-07 thomas return err;
1280 0cf4efb1 2018-09-29 stsp }
1281 6d0fee91 2018-08-01 stsp
1282 07b0611c 2022-06-23 thomas /*
1283 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1284 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1285 07b0611c 2022-06-23 thomas */
1286 07b0611c 2022-06-23 thomas static int
1287 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1288 07b0611c 2022-06-23 thomas {
1289 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1290 a5d43cac 2022-07-01 thomas int x, n = 0;
1291 07b0611c 2022-06-23 thomas
1292 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1293 a5d43cac 2022-07-01 thomas v = view->child;
1294 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1295 a5d43cac 2022-07-01 thomas v = view->parent;
1296 a5d43cac 2022-07-01 thomas
1297 07b0611c 2022-06-23 thomas view->count = 0;
1298 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1299 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1300 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1301 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1302 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1303 07b0611c 2022-06-23 thomas
1304 07b0611c 2022-06-23 thomas do {
1305 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1306 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1307 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1308 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1309 a5d43cac 2022-07-01 thomas }
1310 a5d43cac 2022-07-01 thomas
1311 07b0611c 2022-06-23 thomas /*
1312 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1313 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1314 07b0611c 2022-06-23 thomas */
1315 07b0611c 2022-06-23 thomas if (n >= 9999999)
1316 07b0611c 2022-06-23 thomas n = 9999999;
1317 07b0611c 2022-06-23 thomas else
1318 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1319 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1320 07b0611c 2022-06-23 thomas
1321 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1322 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1323 07dd3ed3 2022-08-06 thomas n = 0;
1324 07dd3ed3 2022-08-06 thomas c = 0;
1325 07dd3ed3 2022-08-06 thomas }
1326 07dd3ed3 2022-08-06 thomas
1327 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1328 07b0611c 2022-06-23 thomas view->count = n;
1329 07b0611c 2022-06-23 thomas
1330 07b0611c 2022-06-23 thomas return c;
1331 07b0611c 2022-06-23 thomas }
1332 07b0611c 2022-06-23 thomas
1333 0cf4efb1 2018-09-29 stsp static const struct got_error *
1334 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1335 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1336 e5a0f69f 2018-08-18 stsp {
1337 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1338 669b5ffa 2018-10-07 stsp struct tog_view *v;
1339 1a76625f 2018-10-22 stsp int ch, errcode;
1340 e5a0f69f 2018-08-18 stsp
1341 e5a0f69f 2018-08-18 stsp *new = NULL;
1342 8f4ed634 2020-03-26 stsp
1343 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1344 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1345 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1347 07b0611c 2022-06-23 thomas view->count = 0;
1348 07b0611c 2022-06-23 thomas }
1349 e5a0f69f 2018-08-18 stsp
1350 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1351 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1352 82954512 2020-02-03 stsp if (errcode)
1353 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1354 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1355 3da8ef85 2021-09-21 stsp sched_yield();
1356 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1357 82954512 2020-02-03 stsp if (errcode)
1358 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1359 82954512 2020-02-03 stsp "pthread_mutex_lock");
1360 60493ae3 2019-06-20 stsp view->search_next(view);
1361 60493ae3 2019-06-20 stsp return NULL;
1362 60493ae3 2019-06-20 stsp }
1363 60493ae3 2019-06-20 stsp
1364 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1365 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1366 1a76625f 2018-10-22 stsp if (errcode)
1367 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1368 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1369 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1370 634cb454 2022-07-03 thomas cbreak();
1371 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1372 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1373 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1374 634cb454 2022-07-03 thomas view->count = 0;
1375 634cb454 2022-07-03 thomas else
1376 634cb454 2022-07-03 thomas ch = view->ch;
1377 634cb454 2022-07-03 thomas } else {
1378 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1379 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1380 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1381 07b0611c 2022-06-23 thomas }
1382 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1383 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1384 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1385 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1386 1a76625f 2018-10-22 stsp if (errcode)
1387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1388 25791caa 2018-10-24 stsp
1389 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1390 25791caa 2018-10-24 stsp tog_resizeterm();
1391 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1392 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1393 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1394 25791caa 2018-10-24 stsp err = view_resize(v);
1395 25791caa 2018-10-24 stsp if (err)
1396 25791caa 2018-10-24 stsp return err;
1397 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1398 25791caa 2018-10-24 stsp if (err)
1399 25791caa 2018-10-24 stsp return err;
1400 cdfcfb03 2020-12-06 stsp if (v->child) {
1401 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1402 cdfcfb03 2020-12-06 stsp if (err)
1403 cdfcfb03 2020-12-06 stsp return err;
1404 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1405 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1406 cdfcfb03 2020-12-06 stsp if (err)
1407 cdfcfb03 2020-12-06 stsp return err;
1408 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1409 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1410 53d2bdd3 2022-07-10 thomas if (err)
1411 53d2bdd3 2022-07-10 thomas return err;
1412 53d2bdd3 2022-07-10 thomas }
1413 cdfcfb03 2020-12-06 stsp }
1414 25791caa 2018-10-24 stsp }
1415 25791caa 2018-10-24 stsp }
1416 25791caa 2018-10-24 stsp
1417 e5a0f69f 2018-08-18 stsp switch (ch) {
1418 1e37a5c2 2019-05-12 jcs case '\t':
1419 07b0611c 2022-06-23 thomas view->count = 0;
1420 1e37a5c2 2019-05-12 jcs if (view->child) {
1421 e78dc838 2020-12-04 stsp view->focussed = 0;
1422 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1423 e78dc838 2020-12-04 stsp view->focus_child = 1;
1424 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1425 e78dc838 2020-12-04 stsp view->focussed = 0;
1426 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1427 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1428 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1429 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1430 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1431 3a0139e8 2022-07-22 thomas 0);
1432 a5d43cac 2022-07-01 thomas if (err)
1433 a5d43cac 2022-07-01 thomas return err;
1434 a5d43cac 2022-07-01 thomas }
1435 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1436 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1437 a5d43cac 2022-07-01 thomas if (err)
1438 a5d43cac 2022-07-01 thomas return err;
1439 a5d43cac 2022-07-01 thomas }
1440 1e37a5c2 2019-05-12 jcs }
1441 1e37a5c2 2019-05-12 jcs break;
1442 1e37a5c2 2019-05-12 jcs case 'q':
1443 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1444 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1445 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1446 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1447 a5d43cac 2022-07-01 thomas if (err)
1448 a5d43cac 2022-07-01 thomas break;
1449 a5d43cac 2022-07-01 thomas }
1450 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1451 a5d43cac 2022-07-01 thomas }
1452 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1453 9970f7fc 2020-12-03 stsp view->dying = 1;
1454 1e37a5c2 2019-05-12 jcs break;
1455 1e37a5c2 2019-05-12 jcs case 'Q':
1456 1e37a5c2 2019-05-12 jcs *done = 1;
1457 1e37a5c2 2019-05-12 jcs break;
1458 1c5e5faa 2022-06-23 thomas case 'F':
1459 07b0611c 2022-06-23 thomas view->count = 0;
1460 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1461 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1462 1e37a5c2 2019-05-12 jcs break;
1463 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1464 e78dc838 2020-12-04 stsp view->focussed = 0;
1465 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1466 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1467 53d2bdd3 2022-07-10 thomas } else {
1468 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1469 53d2bdd3 2022-07-10 thomas if (!err)
1470 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1471 53d2bdd3 2022-07-10 thomas }
1472 1e37a5c2 2019-05-12 jcs if (err)
1473 1e37a5c2 2019-05-12 jcs break;
1474 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1475 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1476 1e37a5c2 2019-05-12 jcs } else {
1477 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1478 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1479 e78dc838 2020-12-04 stsp view->focussed = 1;
1480 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1481 1e37a5c2 2019-05-12 jcs } else {
1482 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1483 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1484 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1485 53d2bdd3 2022-07-10 thomas if (!err)
1486 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1487 669b5ffa 2018-10-07 stsp }
1488 1e37a5c2 2019-05-12 jcs if (err)
1489 1e37a5c2 2019-05-12 jcs break;
1490 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1491 a5d43cac 2022-07-01 thomas }
1492 a5d43cac 2022-07-01 thomas if (err)
1493 a5d43cac 2022-07-01 thomas break;
1494 3a0139e8 2022-07-22 thomas if (view->resize) {
1495 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1496 a5d43cac 2022-07-01 thomas if (err)
1497 a5d43cac 2022-07-01 thomas break;
1498 1e37a5c2 2019-05-12 jcs }
1499 a5d43cac 2022-07-01 thomas if (view->parent)
1500 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1501 a5d43cac 2022-07-01 thomas if (!err)
1502 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1503 1e37a5c2 2019-05-12 jcs break;
1504 64486692 2022-07-07 thomas case 'S':
1505 53d2bdd3 2022-07-10 thomas view->count = 0;
1506 64486692 2022-07-07 thomas err = switch_split(view);
1507 53d2bdd3 2022-07-10 thomas break;
1508 53d2bdd3 2022-07-10 thomas case '-':
1509 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1510 64486692 2022-07-07 thomas break;
1511 53d2bdd3 2022-07-10 thomas case '+':
1512 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1513 53d2bdd3 2022-07-10 thomas break;
1514 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1515 60493ae3 2019-06-20 stsp break;
1516 60493ae3 2019-06-20 stsp case '/':
1517 07b0611c 2022-06-23 thomas view->count = 0;
1518 60493ae3 2019-06-20 stsp if (view->search_start)
1519 2b49a8ae 2019-06-22 stsp view_search_start(view);
1520 60493ae3 2019-06-20 stsp else
1521 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1522 1e37a5c2 2019-05-12 jcs break;
1523 b1bf1435 2019-06-21 stsp case 'N':
1524 60493ae3 2019-06-20 stsp case 'n':
1525 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1526 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1527 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1528 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1529 60493ae3 2019-06-20 stsp view->search_next(view);
1530 60493ae3 2019-06-20 stsp } else
1531 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1532 adf4c9e0 2022-07-03 thomas break;
1533 adf4c9e0 2022-07-03 thomas case 'A':
1534 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1535 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1536 adf4c9e0 2022-07-03 thomas else
1537 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1538 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1539 adf4c9e0 2022-07-03 thomas if (v->reset) {
1540 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1541 adf4c9e0 2022-07-03 thomas if (err)
1542 adf4c9e0 2022-07-03 thomas return err;
1543 adf4c9e0 2022-07-03 thomas }
1544 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1545 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1546 adf4c9e0 2022-07-03 thomas if (err)
1547 adf4c9e0 2022-07-03 thomas return err;
1548 adf4c9e0 2022-07-03 thomas }
1549 adf4c9e0 2022-07-03 thomas }
1550 60493ae3 2019-06-20 stsp break;
1551 1e37a5c2 2019-05-12 jcs default:
1552 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1553 1e37a5c2 2019-05-12 jcs break;
1554 e5a0f69f 2018-08-18 stsp }
1555 e5a0f69f 2018-08-18 stsp
1556 e5a0f69f 2018-08-18 stsp return err;
1557 bcbd79e2 2018-08-19 stsp }
1558 bcbd79e2 2018-08-19 stsp
1559 ef20f542 2022-06-26 thomas static int
1560 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1561 a3404814 2018-09-02 stsp {
1562 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1563 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1564 669b5ffa 2018-10-07 stsp return 0;
1565 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1566 669b5ffa 2018-10-07 stsp return 0;
1567 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1568 a3404814 2018-09-02 stsp return 0;
1569 a3404814 2018-09-02 stsp
1570 669b5ffa 2018-10-07 stsp return view->focussed;
1571 a3404814 2018-09-02 stsp }
1572 a3404814 2018-09-02 stsp
1573 bcbd79e2 2018-08-19 stsp static const struct got_error *
1574 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1575 e5a0f69f 2018-08-18 stsp {
1576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1577 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1578 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1579 64486692 2022-07-07 thomas char *mode;
1580 fd823528 2018-10-22 stsp int fast_refresh = 10;
1581 1a76625f 2018-10-22 stsp int done = 0, errcode;
1582 e5a0f69f 2018-08-18 stsp
1583 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1584 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1585 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1586 64486692 2022-07-07 thomas else
1587 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1588 64486692 2022-07-07 thomas
1589 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1590 1a76625f 2018-10-22 stsp if (errcode)
1591 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1592 1a76625f 2018-10-22 stsp
1593 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1594 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1595 e5a0f69f 2018-08-18 stsp
1596 1004088d 2018-09-29 stsp view->focussed = 1;
1597 878940b7 2018-09-29 stsp err = view->show(view);
1598 0cf4efb1 2018-09-29 stsp if (err)
1599 0cf4efb1 2018-09-29 stsp return err;
1600 0cf4efb1 2018-09-29 stsp update_panels();
1601 0cf4efb1 2018-09-29 stsp doupdate();
1602 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1603 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1604 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1605 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1606 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1607 fd823528 2018-10-22 stsp
1608 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1609 e5a0f69f 2018-08-18 stsp if (err)
1610 e5a0f69f 2018-08-18 stsp break;
1611 9970f7fc 2020-12-03 stsp if (view->dying) {
1612 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1613 669b5ffa 2018-10-07 stsp
1614 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1615 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1616 9970f7fc 2020-12-03 stsp entry);
1617 e78dc838 2020-12-04 stsp else if (view->parent)
1618 669b5ffa 2018-10-07 stsp prev = view->parent;
1619 669b5ffa 2018-10-07 stsp
1620 e78dc838 2020-12-04 stsp if (view->parent) {
1621 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1622 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1623 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1624 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1625 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1626 40236d76 2022-06-23 thomas if (err)
1627 40236d76 2022-06-23 thomas break;
1628 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1629 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1630 e78dc838 2020-12-04 stsp } else
1631 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1632 669b5ffa 2018-10-07 stsp
1633 9970f7fc 2020-12-03 stsp err = view_close(view);
1634 fb59748f 2020-12-05 stsp if (err)
1635 e5a0f69f 2018-08-18 stsp goto done;
1636 669b5ffa 2018-10-07 stsp
1637 e78dc838 2020-12-04 stsp view = NULL;
1638 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1639 e78dc838 2020-12-04 stsp if (v->focussed)
1640 e78dc838 2020-12-04 stsp break;
1641 0cf4efb1 2018-09-29 stsp }
1642 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1643 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1644 e78dc838 2020-12-04 stsp if (prev)
1645 e78dc838 2020-12-04 stsp view = prev;
1646 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1647 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1648 e78dc838 2020-12-04 stsp tog_view_list_head);
1649 e78dc838 2020-12-04 stsp }
1650 e78dc838 2020-12-04 stsp if (view) {
1651 e78dc838 2020-12-04 stsp if (view->focus_child) {
1652 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1653 e78dc838 2020-12-04 stsp view = view->child;
1654 e78dc838 2020-12-04 stsp } else
1655 e78dc838 2020-12-04 stsp view->focussed = 1;
1656 e78dc838 2020-12-04 stsp }
1657 e78dc838 2020-12-04 stsp }
1658 e5a0f69f 2018-08-18 stsp }
1659 bcbd79e2 2018-08-19 stsp if (new_view) {
1660 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1661 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1662 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1663 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1664 86c66b02 2018-10-18 stsp continue;
1665 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1666 86c66b02 2018-10-18 stsp err = view_close(v);
1667 86c66b02 2018-10-18 stsp if (err)
1668 86c66b02 2018-10-18 stsp goto done;
1669 86c66b02 2018-10-18 stsp break;
1670 86c66b02 2018-10-18 stsp }
1671 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1672 fed7eaa8 2018-10-24 stsp view = new_view;
1673 7cd52833 2022-06-23 thomas }
1674 669b5ffa 2018-10-07 stsp if (view) {
1675 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1676 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1677 e78dc838 2020-12-04 stsp view = view->child;
1678 e78dc838 2020-12-04 stsp } else {
1679 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1680 e78dc838 2020-12-04 stsp view = view->parent;
1681 1a76625f 2018-10-22 stsp }
1682 e78dc838 2020-12-04 stsp show_panel(view->panel);
1683 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1684 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1685 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1686 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1687 669b5ffa 2018-10-07 stsp if (err)
1688 1a76625f 2018-10-22 stsp goto done;
1689 669b5ffa 2018-10-07 stsp }
1690 669b5ffa 2018-10-07 stsp err = view->show(view);
1691 0cf4efb1 2018-09-29 stsp if (err)
1692 1a76625f 2018-10-22 stsp goto done;
1693 669b5ffa 2018-10-07 stsp if (view->child) {
1694 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1695 669b5ffa 2018-10-07 stsp if (err)
1696 1a76625f 2018-10-22 stsp goto done;
1697 669b5ffa 2018-10-07 stsp }
1698 1a76625f 2018-10-22 stsp update_panels();
1699 1a76625f 2018-10-22 stsp doupdate();
1700 0cf4efb1 2018-09-29 stsp }
1701 e5a0f69f 2018-08-18 stsp }
1702 e5a0f69f 2018-08-18 stsp done:
1703 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1704 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1705 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1706 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1707 f2d749db 2022-07-12 thomas close_err = view_close(view);
1708 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1709 f2d749db 2022-07-12 thomas err = close_err;
1710 e5a0f69f 2018-08-18 stsp }
1711 1a76625f 2018-10-22 stsp
1712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1713 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1714 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1715 1a76625f 2018-10-22 stsp
1716 e5a0f69f 2018-08-18 stsp return err;
1717 ea5e7bb5 2018-08-01 stsp }
1718 ea5e7bb5 2018-08-01 stsp
1719 4ed7e80c 2018-05-20 stsp __dead static void
1720 9f7d7167 2018-04-29 stsp usage_log(void)
1721 9f7d7167 2018-04-29 stsp {
1722 80ddbec8 2018-04-29 stsp endwin();
1723 c70c5802 2018-08-01 stsp fprintf(stderr,
1724 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1725 9f7d7167 2018-04-29 stsp getprogname());
1726 9f7d7167 2018-04-29 stsp exit(1);
1727 80ddbec8 2018-04-29 stsp }
1728 80ddbec8 2018-04-29 stsp
1729 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1730 80ddbec8 2018-04-29 stsp static const struct got_error *
1731 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1732 963b370f 2018-05-20 stsp {
1733 00dfcb92 2018-06-11 stsp char *vis = NULL;
1734 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1735 963b370f 2018-05-20 stsp
1736 963b370f 2018-05-20 stsp *ws = NULL;
1737 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1738 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1739 00dfcb92 2018-06-11 stsp int vislen;
1740 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1741 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1742 00dfcb92 2018-06-11 stsp
1743 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1744 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1745 00dfcb92 2018-06-11 stsp if (err)
1746 00dfcb92 2018-06-11 stsp return err;
1747 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1748 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1750 a7f50699 2018-06-11 stsp goto done;
1751 a7f50699 2018-06-11 stsp }
1752 00dfcb92 2018-06-11 stsp }
1753 963b370f 2018-05-20 stsp
1754 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1755 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1757 a7f50699 2018-06-11 stsp goto done;
1758 a7f50699 2018-06-11 stsp }
1759 963b370f 2018-05-20 stsp
1760 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1761 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1762 a7f50699 2018-06-11 stsp done:
1763 00dfcb92 2018-06-11 stsp free(vis);
1764 963b370f 2018-05-20 stsp if (err) {
1765 963b370f 2018-05-20 stsp free(*ws);
1766 963b370f 2018-05-20 stsp *ws = NULL;
1767 963b370f 2018-05-20 stsp *wlen = 0;
1768 963b370f 2018-05-20 stsp }
1769 963b370f 2018-05-20 stsp return err;
1770 05171be4 2022-06-23 thomas }
1771 05171be4 2022-06-23 thomas
1772 05171be4 2022-06-23 thomas static const struct got_error *
1773 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1774 05171be4 2022-06-23 thomas {
1775 05171be4 2022-06-23 thomas char *dst;
1776 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1777 05171be4 2022-06-23 thomas
1778 05171be4 2022-06-23 thomas *ptr = NULL;
1779 05171be4 2022-06-23 thomas n = len = strlen(src);
1780 83316834 2022-06-23 thomas dst = malloc(n + 1);
1781 05171be4 2022-06-23 thomas if (dst == NULL)
1782 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1783 05171be4 2022-06-23 thomas
1784 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1785 05171be4 2022-06-23 thomas const char c = src[idx];
1786 05171be4 2022-06-23 thomas
1787 05171be4 2022-06-23 thomas if (c == '\t') {
1788 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1789 b235ad5d 2022-06-23 thomas char *p;
1790 b235ad5d 2022-06-23 thomas
1791 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1792 83316834 2022-06-23 thomas if (p == NULL) {
1793 83316834 2022-06-23 thomas free(dst);
1794 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1795 83316834 2022-06-23 thomas
1796 83316834 2022-06-23 thomas }
1797 83316834 2022-06-23 thomas dst = p;
1798 05171be4 2022-06-23 thomas n += nb;
1799 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1800 05171be4 2022-06-23 thomas sz += nb;
1801 05171be4 2022-06-23 thomas } else
1802 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1803 05171be4 2022-06-23 thomas ++idx;
1804 05171be4 2022-06-23 thomas }
1805 05171be4 2022-06-23 thomas
1806 05171be4 2022-06-23 thomas dst[sz] = '\0';
1807 05171be4 2022-06-23 thomas *ptr = dst;
1808 05171be4 2022-06-23 thomas return NULL;
1809 963b370f 2018-05-20 stsp }
1810 963b370f 2018-05-20 stsp
1811 8d208d34 2022-06-23 thomas /*
1812 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1813 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1814 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1815 8d208d34 2022-06-23 thomas * *rcol.
1816 f91a2b48 2022-06-23 thomas */
1817 8d208d34 2022-06-23 thomas static int
1818 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1819 8d208d34 2022-06-23 thomas {
1820 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1821 f91a2b48 2022-06-23 thomas
1822 8d208d34 2022-06-23 thomas if (n == 0) {
1823 8d208d34 2022-06-23 thomas *rcol = cols;
1824 8d208d34 2022-06-23 thomas return off;
1825 8d208d34 2022-06-23 thomas }
1826 f91a2b48 2022-06-23 thomas
1827 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1828 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1829 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1830 8d208d34 2022-06-23 thomas else
1831 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1832 f91a2b48 2022-06-23 thomas
1833 8d208d34 2022-06-23 thomas if (width == -1) {
1834 8d208d34 2022-06-23 thomas width = 1;
1835 8d208d34 2022-06-23 thomas wline[i] = L'.';
1836 f91a2b48 2022-06-23 thomas }
1837 f91a2b48 2022-06-23 thomas
1838 8d208d34 2022-06-23 thomas if (cols + width > n)
1839 8d208d34 2022-06-23 thomas break;
1840 8d208d34 2022-06-23 thomas cols += width;
1841 f91a2b48 2022-06-23 thomas }
1842 f91a2b48 2022-06-23 thomas
1843 8d208d34 2022-06-23 thomas *rcol = cols;
1844 8d208d34 2022-06-23 thomas return i;
1845 f91a2b48 2022-06-23 thomas }
1846 f91a2b48 2022-06-23 thomas
1847 f91a2b48 2022-06-23 thomas /*
1848 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1849 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1850 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1851 f91a2b48 2022-06-23 thomas */
1852 f91a2b48 2022-06-23 thomas static const struct got_error *
1853 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1854 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1855 f91a2b48 2022-06-23 thomas {
1856 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1857 8d208d34 2022-06-23 thomas int cols;
1858 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1859 05171be4 2022-06-23 thomas char *exstr = NULL;
1860 963b370f 2018-05-20 stsp size_t wlen;
1861 8d208d34 2022-06-23 thomas int i, scrollx;
1862 963b370f 2018-05-20 stsp
1863 963b370f 2018-05-20 stsp *wlinep = NULL;
1864 b700b5d6 2018-07-10 stsp *widthp = 0;
1865 963b370f 2018-05-20 stsp
1866 05171be4 2022-06-23 thomas if (expand) {
1867 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1868 05171be4 2022-06-23 thomas if (err)
1869 05171be4 2022-06-23 thomas return err;
1870 05171be4 2022-06-23 thomas }
1871 05171be4 2022-06-23 thomas
1872 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1873 05171be4 2022-06-23 thomas free(exstr);
1874 963b370f 2018-05-20 stsp if (err)
1875 963b370f 2018-05-20 stsp return err;
1876 963b370f 2018-05-20 stsp
1877 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1878 f91a2b48 2022-06-23 thomas
1879 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1880 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1881 3f670bfb 2020-12-10 stsp wlen--;
1882 3f670bfb 2020-12-10 stsp }
1883 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1884 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1885 3f670bfb 2020-12-10 stsp wlen--;
1886 3f670bfb 2020-12-10 stsp }
1887 3f670bfb 2020-12-10 stsp
1888 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1889 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1890 27a741e5 2019-09-11 stsp
1891 b700b5d6 2018-07-10 stsp if (widthp)
1892 b700b5d6 2018-07-10 stsp *widthp = cols;
1893 f91a2b48 2022-06-23 thomas if (scrollxp)
1894 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1895 963b370f 2018-05-20 stsp if (err)
1896 963b370f 2018-05-20 stsp free(wline);
1897 963b370f 2018-05-20 stsp else
1898 963b370f 2018-05-20 stsp *wlinep = wline;
1899 963b370f 2018-05-20 stsp return err;
1900 963b370f 2018-05-20 stsp }
1901 963b370f 2018-05-20 stsp
1902 8b473291 2019-02-21 stsp static const struct got_error*
1903 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1904 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1905 8b473291 2019-02-21 stsp {
1906 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1907 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1908 8b473291 2019-02-21 stsp char *s;
1909 8b473291 2019-02-21 stsp const char *name;
1910 8b473291 2019-02-21 stsp
1911 8b473291 2019-02-21 stsp *refs_str = NULL;
1912 8b473291 2019-02-21 stsp
1913 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1914 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1915 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1916 52b5abe1 2019-08-13 stsp int cmp;
1917 52b5abe1 2019-08-13 stsp
1918 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1919 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1920 8b473291 2019-02-21 stsp continue;
1921 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1922 8b473291 2019-02-21 stsp name += 5;
1923 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1924 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1925 7143d404 2019-03-12 stsp continue;
1926 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1927 8b473291 2019-02-21 stsp name += 6;
1928 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1929 8b473291 2019-02-21 stsp name += 8;
1930 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1931 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1932 79cc719f 2020-04-24 stsp continue;
1933 79cc719f 2020-04-24 stsp }
1934 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1935 48cae60d 2020-09-22 stsp if (err)
1936 48cae60d 2020-09-22 stsp break;
1937 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1938 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1939 5d844a1e 2019-08-13 stsp if (err) {
1940 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1941 48cae60d 2020-09-22 stsp free(ref_id);
1942 5d844a1e 2019-08-13 stsp break;
1943 48cae60d 2020-09-22 stsp }
1944 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1945 5d844a1e 2019-08-13 stsp err = NULL;
1946 5d844a1e 2019-08-13 stsp tag = NULL;
1947 5d844a1e 2019-08-13 stsp }
1948 52b5abe1 2019-08-13 stsp }
1949 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1950 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1951 48cae60d 2020-09-22 stsp free(ref_id);
1952 52b5abe1 2019-08-13 stsp if (tag)
1953 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1954 52b5abe1 2019-08-13 stsp if (cmp != 0)
1955 52b5abe1 2019-08-13 stsp continue;
1956 8b473291 2019-02-21 stsp s = *refs_str;
1957 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1958 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1959 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1960 8b473291 2019-02-21 stsp free(s);
1961 8b473291 2019-02-21 stsp *refs_str = NULL;
1962 8b473291 2019-02-21 stsp break;
1963 8b473291 2019-02-21 stsp }
1964 8b473291 2019-02-21 stsp free(s);
1965 8b473291 2019-02-21 stsp }
1966 8b473291 2019-02-21 stsp
1967 8b473291 2019-02-21 stsp return err;
1968 8b473291 2019-02-21 stsp }
1969 8b473291 2019-02-21 stsp
1970 963b370f 2018-05-20 stsp static const struct got_error *
1971 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1972 27a741e5 2019-09-11 stsp int col_tab_align)
1973 5813d178 2019-03-09 stsp {
1974 e6b8b890 2020-12-29 naddy char *smallerthan;
1975 5813d178 2019-03-09 stsp
1976 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1977 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1978 5813d178 2019-03-09 stsp author = smallerthan + 1;
1979 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1980 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1981 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1982 5813d178 2019-03-09 stsp }
1983 5813d178 2019-03-09 stsp
1984 5813d178 2019-03-09 stsp static const struct got_error *
1985 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1986 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1987 8fdc79fe 2020-12-01 naddy int author_display_cols)
1988 80ddbec8 2018-04-29 stsp {
1989 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1990 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1991 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1992 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1993 5813d178 2019-03-09 stsp char *author = NULL;
1994 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1995 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1996 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1997 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1998 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1999 ccb26ccd 2018-11-05 stsp struct tm tm;
2000 45d799e2 2018-12-23 stsp time_t committer_time;
2001 11b20872 2019-11-08 stsp struct tog_color *tc;
2002 80ddbec8 2018-04-29 stsp
2003 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2004 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2005 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2006 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2007 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2008 b39d25c7 2018-07-10 stsp
2009 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2010 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2011 b39d25c7 2018-07-10 stsp else
2012 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2013 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2014 11b20872 2019-11-08 stsp if (tc)
2015 11b20872 2019-11-08 stsp wattr_on(view->window,
2016 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2017 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2018 11b20872 2019-11-08 stsp if (tc)
2019 11b20872 2019-11-08 stsp wattr_off(view->window,
2020 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2021 27a741e5 2019-09-11 stsp col = limit;
2022 b39d25c7 2018-07-10 stsp if (col > avail)
2023 b39d25c7 2018-07-10 stsp goto done;
2024 6570a66d 2019-11-08 stsp
2025 6570a66d 2019-11-08 stsp if (avail >= 120) {
2026 6570a66d 2019-11-08 stsp char *id_str;
2027 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2028 6570a66d 2019-11-08 stsp if (err)
2029 6570a66d 2019-11-08 stsp goto done;
2030 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2031 11b20872 2019-11-08 stsp if (tc)
2032 11b20872 2019-11-08 stsp wattr_on(view->window,
2033 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2034 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2035 11b20872 2019-11-08 stsp if (tc)
2036 11b20872 2019-11-08 stsp wattr_off(view->window,
2037 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2038 6570a66d 2019-11-08 stsp free(id_str);
2039 6570a66d 2019-11-08 stsp col += 9;
2040 6570a66d 2019-11-08 stsp if (col > avail)
2041 6570a66d 2019-11-08 stsp goto done;
2042 6570a66d 2019-11-08 stsp }
2043 b39d25c7 2018-07-10 stsp
2044 f69c5a46 2022-07-19 thomas if (s->use_committer)
2045 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2046 f69c5a46 2022-07-19 thomas else
2047 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2048 5813d178 2019-03-09 stsp if (author == NULL) {
2049 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2050 80ddbec8 2018-04-29 stsp goto done;
2051 80ddbec8 2018-04-29 stsp }
2052 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2053 bb737323 2018-05-20 stsp if (err)
2054 bb737323 2018-05-20 stsp goto done;
2055 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2056 11b20872 2019-11-08 stsp if (tc)
2057 11b20872 2019-11-08 stsp wattr_on(view->window,
2058 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2059 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2060 bb737323 2018-05-20 stsp col += author_width;
2061 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2062 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2063 bb737323 2018-05-20 stsp col++;
2064 bb737323 2018-05-20 stsp author_width++;
2065 bb737323 2018-05-20 stsp }
2066 f31d6c3b 2022-09-09 thomas if (tc)
2067 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2068 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2069 9c2eaf34 2018-05-20 stsp if (col > avail)
2070 9c2eaf34 2018-05-20 stsp goto done;
2071 80ddbec8 2018-04-29 stsp
2072 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2073 5943eee2 2019-08-13 stsp if (err)
2074 6d9fbc00 2018-04-29 stsp goto done;
2075 bb737323 2018-05-20 stsp logmsg = logmsg0;
2076 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2077 bb737323 2018-05-20 stsp logmsg++;
2078 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2079 bb737323 2018-05-20 stsp if (newline)
2080 bb737323 2018-05-20 stsp *newline = '\0';
2081 f91a2b48 2022-06-23 thomas limit = avail - col;
2082 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2083 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2084 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2085 f91a2b48 2022-06-23 thomas limit, col, 1);
2086 331b1a16 2022-06-23 thomas if (err)
2087 331b1a16 2022-06-23 thomas goto done;
2088 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2089 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2090 27a741e5 2019-09-11 stsp while (col < avail) {
2091 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2092 bb737323 2018-05-20 stsp col++;
2093 881b2d3e 2018-04-30 stsp }
2094 80ddbec8 2018-04-29 stsp done:
2095 80ddbec8 2018-04-29 stsp free(logmsg0);
2096 bb737323 2018-05-20 stsp free(wlogmsg);
2097 5813d178 2019-03-09 stsp free(author);
2098 bb737323 2018-05-20 stsp free(wauthor);
2099 80ddbec8 2018-04-29 stsp free(line);
2100 80ddbec8 2018-04-29 stsp return err;
2101 80ddbec8 2018-04-29 stsp }
2102 26ed57b2 2018-05-19 stsp
2103 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2104 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2105 899d86c2 2018-05-10 stsp struct got_object_id *id)
2106 80ddbec8 2018-04-29 stsp {
2107 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2108 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2109 80ddbec8 2018-04-29 stsp
2110 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2111 80ddbec8 2018-04-29 stsp if (entry == NULL)
2112 899d86c2 2018-05-10 stsp return NULL;
2113 99db9666 2018-05-07 stsp
2114 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2115 d68b9737 2022-09-05 thomas if (dup == NULL) {
2116 d68b9737 2022-09-05 thomas free(entry);
2117 d68b9737 2022-09-05 thomas return NULL;
2118 d68b9737 2022-09-05 thomas }
2119 d68b9737 2022-09-05 thomas
2120 d68b9737 2022-09-05 thomas entry->id = dup;
2121 99db9666 2018-05-07 stsp entry->commit = commit;
2122 899d86c2 2018-05-10 stsp return entry;
2123 99db9666 2018-05-07 stsp }
2124 80ddbec8 2018-04-29 stsp
2125 99db9666 2018-05-07 stsp static void
2126 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2127 99db9666 2018-05-07 stsp {
2128 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2129 99db9666 2018-05-07 stsp
2130 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2131 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2132 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2133 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2134 d68b9737 2022-09-05 thomas free(entry->id);
2135 99db9666 2018-05-07 stsp free(entry);
2136 99db9666 2018-05-07 stsp }
2137 99db9666 2018-05-07 stsp
2138 99db9666 2018-05-07 stsp static void
2139 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2140 99db9666 2018-05-07 stsp {
2141 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2142 99db9666 2018-05-07 stsp pop_commit(commits);
2143 c4972b91 2018-05-07 stsp }
2144 c4972b91 2018-05-07 stsp
2145 c4972b91 2018-05-07 stsp static const struct got_error *
2146 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2147 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2148 13add988 2019-10-15 stsp {
2149 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2150 13add988 2019-10-15 stsp regmatch_t regmatch;
2151 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2152 13add988 2019-10-15 stsp
2153 13add988 2019-10-15 stsp *have_match = 0;
2154 13add988 2019-10-15 stsp
2155 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2156 13add988 2019-10-15 stsp if (err)
2157 13add988 2019-10-15 stsp return err;
2158 13add988 2019-10-15 stsp
2159 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2160 13add988 2019-10-15 stsp if (err)
2161 13add988 2019-10-15 stsp goto done;
2162 13add988 2019-10-15 stsp
2163 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2164 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2165 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2166 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2167 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2168 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2169 13add988 2019-10-15 stsp *have_match = 1;
2170 13add988 2019-10-15 stsp done:
2171 13add988 2019-10-15 stsp free(id_str);
2172 13add988 2019-10-15 stsp free(logmsg);
2173 13add988 2019-10-15 stsp return err;
2174 13add988 2019-10-15 stsp }
2175 13add988 2019-10-15 stsp
2176 13add988 2019-10-15 stsp static const struct got_error *
2177 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2178 c4972b91 2018-05-07 stsp {
2179 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2180 9ba79e04 2018-06-11 stsp
2181 1a76625f 2018-10-22 stsp /*
2182 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2183 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2184 1a76625f 2018-10-22 stsp * while updating the display.
2185 1a76625f 2018-10-22 stsp */
2186 4e0d2870 2020-12-07 naddy do {
2187 7210b715 2022-09-11 thomas struct got_object_id id;
2188 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2189 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2190 7e8004ba 2022-09-11 thomas int limit_match = 0;
2191 1a76625f 2018-10-22 stsp int errcode;
2192 899d86c2 2018-05-10 stsp
2193 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2194 4e0d2870 2020-12-07 naddy NULL, NULL);
2195 7210b715 2022-09-11 thomas if (err)
2196 ecb28ae0 2018-07-16 stsp break;
2197 899d86c2 2018-05-10 stsp
2198 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2199 9ba79e04 2018-06-11 stsp if (err)
2200 9ba79e04 2018-06-11 stsp break;
2201 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2202 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2203 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2204 9ba79e04 2018-06-11 stsp break;
2205 9ba79e04 2018-06-11 stsp }
2206 93e45b7c 2018-09-24 stsp
2207 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2208 1a76625f 2018-10-22 stsp if (errcode) {
2209 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2210 13add988 2019-10-15 stsp "pthread_mutex_lock");
2211 1a76625f 2018-10-22 stsp break;
2212 1a76625f 2018-10-22 stsp }
2213 1a76625f 2018-10-22 stsp
2214 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2215 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2216 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2217 1a76625f 2018-10-22 stsp
2218 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2219 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2220 7e8004ba 2022-09-11 thomas a->limit_regex);
2221 7e8004ba 2022-09-11 thomas if (err)
2222 7e8004ba 2022-09-11 thomas break;
2223 7e8004ba 2022-09-11 thomas
2224 7e8004ba 2022-09-11 thomas if (limit_match) {
2225 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2226 7e8004ba 2022-09-11 thomas
2227 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2228 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2229 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2230 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2231 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2232 7e8004ba 2022-09-11 thomas break;
2233 7e8004ba 2022-09-11 thomas }
2234 7e8004ba 2022-09-11 thomas
2235 7e8004ba 2022-09-11 thomas err = got_object_commit_dup(&matched->commit,
2236 7e8004ba 2022-09-11 thomas entry->commit);
2237 7e8004ba 2022-09-11 thomas if (err)
2238 7e8004ba 2022-09-11 thomas break;
2239 7e8004ba 2022-09-11 thomas
2240 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2241 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2242 7e8004ba 2022-09-11 thomas matched, entry);
2243 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2244 7e8004ba 2022-09-11 thomas }
2245 7e8004ba 2022-09-11 thomas
2246 7e8004ba 2022-09-11 thomas /*
2247 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2248 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2249 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2250 7e8004ba 2022-09-11 thomas */
2251 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2252 7e8004ba 2022-09-11 thomas }
2253 7e8004ba 2022-09-11 thomas
2254 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2255 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2256 7c1452c1 2020-03-26 stsp int have_match;
2257 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2258 7c1452c1 2020-03-26 stsp if (err)
2259 7c1452c1 2020-03-26 stsp break;
2260 7e8004ba 2022-09-11 thomas
2261 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2262 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2263 7e8004ba 2022-09-11 thomas *a->search_next_done =
2264 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2265 7e8004ba 2022-09-11 thomas } else if (have_match)
2266 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2267 13add988 2019-10-15 stsp }
2268 13add988 2019-10-15 stsp
2269 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2270 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2271 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2272 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2273 7c1452c1 2020-03-26 stsp if (err)
2274 13add988 2019-10-15 stsp break;
2275 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2276 899d86c2 2018-05-10 stsp
2277 9ba79e04 2018-06-11 stsp return err;
2278 0553a4e3 2018-04-30 stsp }
2279 0553a4e3 2018-04-30 stsp
2280 2b779855 2020-12-05 naddy static void
2281 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2282 2b779855 2020-12-05 naddy {
2283 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2284 2b779855 2020-12-05 naddy int ncommits = 0;
2285 2b779855 2020-12-05 naddy
2286 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2287 2b779855 2020-12-05 naddy while (entry) {
2288 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2289 2b779855 2020-12-05 naddy s->selected_entry = entry;
2290 2b779855 2020-12-05 naddy break;
2291 2b779855 2020-12-05 naddy }
2292 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2293 2b779855 2020-12-05 naddy ncommits++;
2294 2b779855 2020-12-05 naddy }
2295 2b779855 2020-12-05 naddy }
2296 2b779855 2020-12-05 naddy
2297 0553a4e3 2018-04-30 stsp static const struct got_error *
2298 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2299 0553a4e3 2018-04-30 stsp {
2300 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2301 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2302 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2303 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2304 60493ae3 2019-06-20 stsp int width;
2305 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2306 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2307 8b473291 2019-02-21 stsp char *refs_str = NULL;
2308 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2309 11b20872 2019-11-08 stsp struct tog_color *tc;
2310 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2311 bd3f8225 2022-08-12 thomas
2312 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2313 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2314 0553a4e3 2018-04-30 stsp
2315 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2316 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2317 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2318 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2319 1a76625f 2018-10-22 stsp if (err)
2320 ecb28ae0 2018-07-16 stsp return err;
2321 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2322 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2323 d2075bf3 2020-12-25 stsp if (refs) {
2324 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2325 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2326 d2075bf3 2020-12-25 stsp if (err)
2327 d2075bf3 2020-12-25 stsp goto done;
2328 d2075bf3 2020-12-25 stsp }
2329 867c6645 2018-07-10 stsp }
2330 359bfafd 2019-02-22 stsp
2331 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2332 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2333 1a76625f 2018-10-22 stsp
2334 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2335 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2336 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2337 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2338 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2339 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2340 8f4ed634 2020-03-26 stsp goto done;
2341 8f4ed634 2020-03-26 stsp }
2342 8f4ed634 2020-03-26 stsp } else {
2343 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2344 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2345 f9686aa5 2020-03-27 stsp
2346 f9686aa5 2020-03-27 stsp if (view->searching) {
2347 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2348 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2349 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2350 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2351 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2352 f9686aa5 2020-03-27 stsp search_str = "searching...";
2353 f9686aa5 2020-03-27 stsp }
2354 f9686aa5 2020-03-27 stsp
2355 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2356 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2357 7e8004ba 2022-09-11 thomas
2358 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2359 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2360 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2361 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2362 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2363 8f4ed634 2020-03-26 stsp goto done;
2364 8f4ed634 2020-03-26 stsp }
2365 8b473291 2019-02-21 stsp }
2366 1a76625f 2018-10-22 stsp
2367 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2368 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2369 91198554 2022-06-23 thomas "........................................",
2370 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2372 1a76625f 2018-10-22 stsp header = NULL;
2373 1a76625f 2018-10-22 stsp goto done;
2374 1a76625f 2018-10-22 stsp }
2375 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2376 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2377 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2379 1a76625f 2018-10-22 stsp header = NULL;
2380 1a76625f 2018-10-22 stsp goto done;
2381 ecb28ae0 2018-07-16 stsp }
2382 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2383 1a76625f 2018-10-22 stsp if (err)
2384 1a76625f 2018-10-22 stsp goto done;
2385 867c6645 2018-07-10 stsp
2386 2814baeb 2018-08-01 stsp werase(view->window);
2387 867c6645 2018-07-10 stsp
2388 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2389 a3404814 2018-09-02 stsp wstandout(view->window);
2390 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2391 11b20872 2019-11-08 stsp if (tc)
2392 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2393 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2394 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2395 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2396 1a76625f 2018-10-22 stsp width++;
2397 1a76625f 2018-10-22 stsp }
2398 86f4aab9 2022-09-09 thomas if (tc)
2399 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2400 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2401 a3404814 2018-09-02 stsp wstandend(view->window);
2402 ecb28ae0 2018-07-16 stsp free(wline);
2403 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2404 1a76625f 2018-10-22 stsp goto done;
2405 0553a4e3 2018-04-30 stsp
2406 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2407 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2408 5813d178 2019-03-09 stsp ncommits = 0;
2409 05171be4 2022-06-23 thomas view->maxx = 0;
2410 5813d178 2019-03-09 stsp while (entry) {
2411 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2412 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2413 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2414 5813d178 2019-03-09 stsp int width;
2415 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2416 5813d178 2019-03-09 stsp break;
2417 f69c5a46 2022-07-19 thomas if (s->use_committer)
2418 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2419 f69c5a46 2022-07-19 thomas else
2420 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2421 5813d178 2019-03-09 stsp if (author == NULL) {
2422 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2423 5813d178 2019-03-09 stsp goto done;
2424 5813d178 2019-03-09 stsp }
2425 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2426 27a741e5 2019-09-11 stsp date_display_cols);
2427 5813d178 2019-03-09 stsp if (author_cols < width)
2428 5813d178 2019-03-09 stsp author_cols = width;
2429 5813d178 2019-03-09 stsp free(wauthor);
2430 5813d178 2019-03-09 stsp free(author);
2431 ef944b8b 2022-07-21 thomas if (err)
2432 ef944b8b 2022-07-21 thomas goto done;
2433 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2434 05171be4 2022-06-23 thomas if (err)
2435 05171be4 2022-06-23 thomas goto done;
2436 05171be4 2022-06-23 thomas msg = msg0;
2437 05171be4 2022-06-23 thomas while (*msg == '\n')
2438 05171be4 2022-06-23 thomas ++msg;
2439 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2440 331b1a16 2022-06-23 thomas *eol = '\0';
2441 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2442 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2443 331b1a16 2022-06-23 thomas if (err)
2444 331b1a16 2022-06-23 thomas goto done;
2445 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2446 05171be4 2022-06-23 thomas free(msg0);
2447 331b1a16 2022-06-23 thomas free(wmsg);
2448 7ca04879 2019-10-19 stsp ncommits++;
2449 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2450 5813d178 2019-03-09 stsp }
2451 5813d178 2019-03-09 stsp
2452 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2453 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2454 867c6645 2018-07-10 stsp ncommits = 0;
2455 899d86c2 2018-05-10 stsp while (entry) {
2456 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2457 899d86c2 2018-05-10 stsp break;
2458 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2459 2814baeb 2018-08-01 stsp wstandout(view->window);
2460 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2461 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2462 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2463 2814baeb 2018-08-01 stsp wstandend(view->window);
2464 0553a4e3 2018-04-30 stsp if (err)
2465 60493ae3 2019-06-20 stsp goto done;
2466 0553a4e3 2018-04-30 stsp ncommits++;
2467 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2468 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2469 80ddbec8 2018-04-29 stsp }
2470 80ddbec8 2018-04-29 stsp
2471 a5d43cac 2022-07-01 thomas view_border(view);
2472 1a76625f 2018-10-22 stsp done:
2473 1a76625f 2018-10-22 stsp free(id_str);
2474 8b473291 2019-02-21 stsp free(refs_str);
2475 1a76625f 2018-10-22 stsp free(ncommits_str);
2476 1a76625f 2018-10-22 stsp free(header);
2477 80ddbec8 2018-04-29 stsp return err;
2478 9f7d7167 2018-04-29 stsp }
2479 07b55e75 2018-05-10 stsp
2480 07b55e75 2018-05-10 stsp static void
2481 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2482 07b55e75 2018-05-10 stsp {
2483 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2484 07b55e75 2018-05-10 stsp int nscrolled = 0;
2485 07b55e75 2018-05-10 stsp
2486 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2487 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2488 07b55e75 2018-05-10 stsp return;
2489 9f7d7167 2018-04-29 stsp
2490 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2491 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2492 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2493 07b55e75 2018-05-10 stsp if (entry) {
2494 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2495 07b55e75 2018-05-10 stsp nscrolled++;
2496 07b55e75 2018-05-10 stsp }
2497 07b55e75 2018-05-10 stsp }
2498 aa075928 2018-05-10 stsp }
2499 aa075928 2018-05-10 stsp
2500 aa075928 2018-05-10 stsp static const struct got_error *
2501 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2502 aa075928 2018-05-10 stsp {
2503 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2504 5e224a3e 2019-02-22 stsp int errcode;
2505 8a42fca8 2019-02-22 stsp
2506 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2507 aa075928 2018-05-10 stsp
2508 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2509 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2510 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2511 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2512 7aafa0d1 2019-02-22 stsp if (errcode)
2513 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2514 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2515 7c1452c1 2020-03-26 stsp
2516 7c1452c1 2020-03-26 stsp /*
2517 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2518 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2519 7c1452c1 2020-03-26 stsp */
2520 7c1452c1 2020-03-26 stsp if (!wait)
2521 7c1452c1 2020-03-26 stsp break;
2522 7c1452c1 2020-03-26 stsp
2523 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2524 ffe38506 2020-12-01 naddy show_log_view(view);
2525 7c1452c1 2020-03-26 stsp update_panels();
2526 7c1452c1 2020-03-26 stsp doupdate();
2527 7c1452c1 2020-03-26 stsp
2528 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2529 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2530 82954512 2020-02-03 stsp if (errcode)
2531 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2532 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2533 82954512 2020-02-03 stsp
2534 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2535 ffe38506 2020-12-01 naddy show_log_view(view);
2536 7c1452c1 2020-03-26 stsp update_panels();
2537 7c1452c1 2020-03-26 stsp doupdate();
2538 5e224a3e 2019-02-22 stsp }
2539 5e224a3e 2019-02-22 stsp
2540 5e224a3e 2019-02-22 stsp return NULL;
2541 a5d43cac 2022-07-01 thomas }
2542 a5d43cac 2022-07-01 thomas
2543 a5d43cac 2022-07-01 thomas static const struct got_error *
2544 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2545 a5d43cac 2022-07-01 thomas {
2546 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2547 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2548 fe731b51 2022-07-14 thomas
2549 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2550 fe731b51 2022-07-14 thomas return NULL;
2551 a5d43cac 2022-07-01 thomas
2552 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2553 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2554 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2555 a5d43cac 2022-07-01 thomas
2556 a5d43cac 2022-07-01 thomas return err;
2557 5e224a3e 2019-02-22 stsp }
2558 5e224a3e 2019-02-22 stsp
2559 5e224a3e 2019-02-22 stsp static const struct got_error *
2560 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2561 5e224a3e 2019-02-22 stsp {
2562 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2563 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2564 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2565 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2566 5e224a3e 2019-02-22 stsp
2567 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2568 5e224a3e 2019-02-22 stsp return NULL;
2569 5e224a3e 2019-02-22 stsp
2570 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2571 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2572 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2573 08ebd0a9 2019-02-22 stsp /*
2574 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2575 08ebd0a9 2019-02-22 stsp */
2576 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2577 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2578 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2579 5e224a3e 2019-02-22 stsp if (err)
2580 5e224a3e 2019-02-22 stsp return err;
2581 7aafa0d1 2019-02-22 stsp }
2582 b295e71b 2019-02-22 stsp
2583 7aafa0d1 2019-02-22 stsp do {
2584 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2585 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2586 88048b54 2019-02-21 stsp break;
2587 88048b54 2019-02-21 stsp
2588 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2589 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2590 aa075928 2018-05-10 stsp
2591 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2592 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2593 dd0a52c1 2018-05-20 stsp break;
2594 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2595 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2596 aa075928 2018-05-10 stsp
2597 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2598 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2599 a5d43cac 2022-07-01 thomas else
2600 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2601 a5d43cac 2022-07-01 thomas
2602 dd0a52c1 2018-05-20 stsp return err;
2603 07b55e75 2018-05-10 stsp }
2604 4a7f7875 2018-05-10 stsp
2605 cd0acaa7 2018-05-20 stsp static const struct got_error *
2606 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2607 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2608 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2609 cd0acaa7 2018-05-20 stsp {
2610 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2611 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2612 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2613 cd0acaa7 2018-05-20 stsp
2614 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2615 15a94983 2018-12-23 stsp if (diff_view == NULL)
2616 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2617 ea5e7bb5 2018-08-01 stsp
2618 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2619 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2620 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2621 e5a0f69f 2018-08-18 stsp if (err == NULL)
2622 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2623 cd0acaa7 2018-05-20 stsp return err;
2624 4a7f7875 2018-05-10 stsp }
2625 4a7f7875 2018-05-10 stsp
2626 80ddbec8 2018-04-29 stsp static const struct got_error *
2627 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2628 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2629 9343a5fb 2018-06-23 stsp {
2630 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2631 941e9f74 2019-05-21 stsp
2632 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2633 941e9f74 2019-05-21 stsp if (parent == NULL)
2634 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2635 941e9f74 2019-05-21 stsp
2636 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2637 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2638 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2639 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2640 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2641 941e9f74 2019-05-21 stsp s->tree = subtree;
2642 941e9f74 2019-05-21 stsp s->selected = 0;
2643 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2644 941e9f74 2019-05-21 stsp return NULL;
2645 941e9f74 2019-05-21 stsp }
2646 941e9f74 2019-05-21 stsp
2647 941e9f74 2019-05-21 stsp static const struct got_error *
2648 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2649 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2650 941e9f74 2019-05-21 stsp {
2651 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2652 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2653 941e9f74 2019-05-21 stsp const char *p;
2654 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2655 9343a5fb 2018-06-23 stsp
2656 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2657 941e9f74 2019-05-21 stsp p = path;
2658 941e9f74 2019-05-21 stsp while (*p) {
2659 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2660 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2661 56e0773d 2019-11-28 stsp char *te_name;
2662 33cbf02b 2020-01-12 stsp
2663 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2664 33cbf02b 2020-01-12 stsp p++;
2665 941e9f74 2019-05-21 stsp
2666 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2667 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2668 941e9f74 2019-05-21 stsp if (slash == NULL)
2669 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2670 33cbf02b 2020-01-12 stsp else
2671 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2672 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2673 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2674 56e0773d 2019-11-28 stsp break;
2675 941e9f74 2019-05-21 stsp }
2676 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2677 56e0773d 2019-11-28 stsp if (te == NULL) {
2678 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2679 56e0773d 2019-11-28 stsp free(te_name);
2680 941e9f74 2019-05-21 stsp break;
2681 941e9f74 2019-05-21 stsp }
2682 56e0773d 2019-11-28 stsp free(te_name);
2683 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2684 941e9f74 2019-05-21 stsp
2685 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2686 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2687 b03c880f 2019-05-21 stsp
2688 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2689 941e9f74 2019-05-21 stsp if (slash)
2690 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2691 941e9f74 2019-05-21 stsp else
2692 941e9f74 2019-05-21 stsp subpath = strdup(path);
2693 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2694 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2695 941e9f74 2019-05-21 stsp break;
2696 941e9f74 2019-05-21 stsp }
2697 941e9f74 2019-05-21 stsp
2698 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2699 941e9f74 2019-05-21 stsp subpath);
2700 941e9f74 2019-05-21 stsp if (err)
2701 941e9f74 2019-05-21 stsp break;
2702 941e9f74 2019-05-21 stsp
2703 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2704 941e9f74 2019-05-21 stsp free(tree_id);
2705 941e9f74 2019-05-21 stsp if (err)
2706 941e9f74 2019-05-21 stsp break;
2707 941e9f74 2019-05-21 stsp
2708 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2709 941e9f74 2019-05-21 stsp if (err) {
2710 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2711 941e9f74 2019-05-21 stsp break;
2712 941e9f74 2019-05-21 stsp }
2713 941e9f74 2019-05-21 stsp if (slash == NULL)
2714 941e9f74 2019-05-21 stsp break;
2715 941e9f74 2019-05-21 stsp free(subpath);
2716 941e9f74 2019-05-21 stsp subpath = NULL;
2717 941e9f74 2019-05-21 stsp p = slash;
2718 941e9f74 2019-05-21 stsp }
2719 941e9f74 2019-05-21 stsp
2720 941e9f74 2019-05-21 stsp free(subpath);
2721 1a76625f 2018-10-22 stsp return err;
2722 61266923 2020-01-14 stsp }
2723 61266923 2020-01-14 stsp
2724 61266923 2020-01-14 stsp static const struct got_error *
2725 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2726 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2727 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2728 55cccc34 2020-02-20 stsp {
2729 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2730 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2731 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2732 55cccc34 2020-02-20 stsp
2733 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2734 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2735 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2736 55cccc34 2020-02-20 stsp
2737 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2738 bc573f3b 2021-07-10 stsp if (err)
2739 55cccc34 2020-02-20 stsp return err;
2740 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2741 55cccc34 2020-02-20 stsp
2742 55cccc34 2020-02-20 stsp *new_view = tree_view;
2743 55cccc34 2020-02-20 stsp
2744 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2745 55cccc34 2020-02-20 stsp return NULL;
2746 55cccc34 2020-02-20 stsp
2747 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2748 55cccc34 2020-02-20 stsp }
2749 55cccc34 2020-02-20 stsp
2750 55cccc34 2020-02-20 stsp static const struct got_error *
2751 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2752 61266923 2020-01-14 stsp {
2753 61266923 2020-01-14 stsp sigset_t sigset;
2754 61266923 2020-01-14 stsp int errcode;
2755 61266923 2020-01-14 stsp
2756 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2757 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2758 61266923 2020-01-14 stsp
2759 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2760 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2761 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2762 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2763 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2764 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2765 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2766 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2767 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2768 61266923 2020-01-14 stsp
2769 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2770 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2771 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2772 61266923 2020-01-14 stsp
2773 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2774 61266923 2020-01-14 stsp if (errcode)
2775 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2776 61266923 2020-01-14 stsp
2777 61266923 2020-01-14 stsp return NULL;
2778 1a76625f 2018-10-22 stsp }
2779 1a76625f 2018-10-22 stsp
2780 1a76625f 2018-10-22 stsp static void *
2781 1a76625f 2018-10-22 stsp log_thread(void *arg)
2782 1a76625f 2018-10-22 stsp {
2783 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2784 1a76625f 2018-10-22 stsp int errcode = 0;
2785 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2786 1a76625f 2018-10-22 stsp int done = 0;
2787 61266923 2020-01-14 stsp
2788 f2d749db 2022-07-12 thomas /*
2789 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2790 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2791 f2d749db 2022-07-12 thomas */
2792 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2793 f2d749db 2022-07-12 thomas if (errcode) {
2794 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2795 61266923 2020-01-14 stsp return (void *)err;
2796 f2d749db 2022-07-12 thomas }
2797 1a76625f 2018-10-22 stsp
2798 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2799 f2d749db 2022-07-12 thomas if (err) {
2800 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2801 f2d749db 2022-07-12 thomas goto done;
2802 f2d749db 2022-07-12 thomas }
2803 f2d749db 2022-07-12 thomas
2804 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2805 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2806 f2d749db 2022-07-12 thomas if (errcode) {
2807 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2808 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2809 f2d749db 2022-07-12 thomas goto done;
2810 f2d749db 2022-07-12 thomas }
2811 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2812 1a76625f 2018-10-22 stsp if (err) {
2813 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2814 f2d749db 2022-07-12 thomas goto done;
2815 1a76625f 2018-10-22 stsp err = NULL;
2816 1a76625f 2018-10-22 stsp done = 1;
2817 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2818 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2819 7e8004ba 2022-09-11 thomas if (a->limit_match)
2820 7e8004ba 2022-09-11 thomas a->commits_needed--;
2821 7e8004ba 2022-09-11 thomas } else
2822 7e8004ba 2022-09-11 thomas a->commits_needed--;
2823 7e8004ba 2022-09-11 thomas }
2824 1a76625f 2018-10-22 stsp
2825 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2826 3abe8080 2019-04-10 stsp if (errcode) {
2827 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2828 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2829 f2d749db 2022-07-12 thomas goto done;
2830 3abe8080 2019-04-10 stsp } else if (*a->quit)
2831 1a76625f 2018-10-22 stsp done = 1;
2832 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2833 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2834 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2835 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2836 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2837 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2838 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2839 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2840 1a76625f 2018-10-22 stsp }
2841 1a76625f 2018-10-22 stsp
2842 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2843 7c1452c1 2020-03-26 stsp if (errcode) {
2844 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2845 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2846 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2847 f2d749db 2022-07-12 thomas goto done;
2848 7c1452c1 2020-03-26 stsp }
2849 7c1452c1 2020-03-26 stsp
2850 1a76625f 2018-10-22 stsp if (done)
2851 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2852 7c1452c1 2020-03-26 stsp else {
2853 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2854 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2855 7c1452c1 2020-03-26 stsp &tog_mutex);
2856 f2d749db 2022-07-12 thomas if (errcode) {
2857 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2858 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2859 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2860 f2d749db 2022-07-12 thomas goto done;
2861 f2d749db 2022-07-12 thomas }
2862 21355643 2020-12-06 stsp if (*a->quit)
2863 21355643 2020-12-06 stsp done = 1;
2864 7c1452c1 2020-03-26 stsp }
2865 1a76625f 2018-10-22 stsp }
2866 1a76625f 2018-10-22 stsp }
2867 3abe8080 2019-04-10 stsp a->log_complete = 1;
2868 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2869 f2d749db 2022-07-12 thomas if (errcode)
2870 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2871 f2d749db 2022-07-12 thomas done:
2872 f2d749db 2022-07-12 thomas if (err) {
2873 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2874 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2875 f2d749db 2022-07-12 thomas }
2876 1a76625f 2018-10-22 stsp return (void *)err;
2877 1a76625f 2018-10-22 stsp }
2878 1a76625f 2018-10-22 stsp
2879 1a76625f 2018-10-22 stsp static const struct got_error *
2880 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2881 1a76625f 2018-10-22 stsp {
2882 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2883 1a76625f 2018-10-22 stsp int errcode;
2884 1a76625f 2018-10-22 stsp
2885 1a76625f 2018-10-22 stsp if (s->thread) {
2886 1a76625f 2018-10-22 stsp s->quit = 1;
2887 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2888 1a76625f 2018-10-22 stsp if (errcode)
2889 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2890 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2891 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2892 1a76625f 2018-10-22 stsp if (errcode)
2893 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2894 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2895 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2896 1a76625f 2018-10-22 stsp if (errcode)
2897 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2898 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2899 1a76625f 2018-10-22 stsp if (errcode)
2900 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2901 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2902 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2903 1a76625f 2018-10-22 stsp }
2904 1a76625f 2018-10-22 stsp
2905 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2906 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2907 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2908 1af5eddf 2022-06-23 thomas }
2909 1af5eddf 2022-06-23 thomas
2910 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2911 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2912 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2913 1af5eddf 2022-06-23 thomas if (err == NULL)
2914 1af5eddf 2022-06-23 thomas err = pack_err;
2915 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2916 1a76625f 2018-10-22 stsp }
2917 1a76625f 2018-10-22 stsp
2918 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2919 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2920 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2921 1a76625f 2018-10-22 stsp }
2922 1a76625f 2018-10-22 stsp
2923 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2924 9343a5fb 2018-06-23 stsp }
2925 9343a5fb 2018-06-23 stsp
2926 9343a5fb 2018-06-23 stsp static const struct got_error *
2927 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2928 1a76625f 2018-10-22 stsp {
2929 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2930 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2931 276b94a1 2020-11-13 naddy int errcode;
2932 1a76625f 2018-10-22 stsp
2933 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2934 276b94a1 2020-11-13 naddy
2935 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2936 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2937 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2938 276b94a1 2020-11-13 naddy
2939 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2940 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2941 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2942 276b94a1 2020-11-13 naddy
2943 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
2944 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
2945 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2946 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2947 1a76625f 2018-10-22 stsp free(s->start_id);
2948 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2949 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2950 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2951 1a76625f 2018-10-22 stsp return err;
2952 1a76625f 2018-10-22 stsp }
2953 1a76625f 2018-10-22 stsp
2954 7e8004ba 2022-09-11 thomas /*
2955 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
2956 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
2957 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
2958 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
2959 7e8004ba 2022-09-11 thomas * works with very slight change.
2960 7e8004ba 2022-09-11 thomas */
2961 1a76625f 2018-10-22 stsp static const struct got_error *
2962 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
2963 7e8004ba 2022-09-11 thomas {
2964 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
2965 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
2966 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
2967 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
2968 7e8004ba 2022-09-11 thomas char pattern[1024];
2969 7e8004ba 2022-09-11 thomas int ret;
2970 7e8004ba 2022-09-11 thomas
2971 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
2972 7e8004ba 2022-09-11 thomas v = view->child;
2973 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2974 7e8004ba 2022-09-11 thomas v = view->parent;
2975 7e8004ba 2022-09-11 thomas
2976 7e8004ba 2022-09-11 thomas /* Get the pattern */
2977 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
2978 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
2979 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2980 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
2981 7e8004ba 2022-09-11 thomas nocbreak();
2982 7e8004ba 2022-09-11 thomas echo();
2983 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
2984 7e8004ba 2022-09-11 thomas cbreak();
2985 7e8004ba 2022-09-11 thomas noecho();
2986 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
2987 7e8004ba 2022-09-11 thomas if (ret == ERR)
2988 7e8004ba 2022-09-11 thomas return NULL;
2989 7e8004ba 2022-09-11 thomas
2990 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
2991 7e8004ba 2022-09-11 thomas /*
2992 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
2993 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
2994 7e8004ba 2022-09-11 thomas */
2995 7e8004ba 2022-09-11 thomas if (!s->limit_view)
2996 7e8004ba 2022-09-11 thomas return NULL;
2997 7e8004ba 2022-09-11 thomas
2998 7e8004ba 2022-09-11 thomas /*
2999 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3000 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3001 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3002 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3003 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3004 7e8004ba 2022-09-11 thomas * the queue.
3005 7e8004ba 2022-09-11 thomas */
3006 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3007 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3008 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3009 7e8004ba 2022-09-11 thomas s->selected = 0;
3010 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3011 7e8004ba 2022-09-11 thomas
3012 7e8004ba 2022-09-11 thomas return NULL;
3013 7e8004ba 2022-09-11 thomas }
3014 7e8004ba 2022-09-11 thomas
3015 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3016 7e8004ba 2022-09-11 thomas return NULL;
3017 7e8004ba 2022-09-11 thomas
3018 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3019 7e8004ba 2022-09-11 thomas
3020 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3021 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3022 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3023 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3024 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3025 7e8004ba 2022-09-11 thomas
3026 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3027 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3028 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3029 7e8004ba 2022-09-11 thomas
3030 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3031 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3032 7e8004ba 2022-09-11 thomas int have_match = 0;
3033 7e8004ba 2022-09-11 thomas
3034 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3035 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3036 7e8004ba 2022-09-11 thomas if (err)
3037 7e8004ba 2022-09-11 thomas return err;
3038 7e8004ba 2022-09-11 thomas
3039 7e8004ba 2022-09-11 thomas if (have_match) {
3040 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3041 7e8004ba 2022-09-11 thomas
3042 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3043 7e8004ba 2022-09-11 thomas entry->id);
3044 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3045 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3046 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3047 7e8004ba 2022-09-11 thomas break;
3048 7e8004ba 2022-09-11 thomas }
3049 7e8004ba 2022-09-11 thomas
3050 7e8004ba 2022-09-11 thomas err = got_object_commit_dup(&matched->commit,
3051 7e8004ba 2022-09-11 thomas entry->commit);
3052 7e8004ba 2022-09-11 thomas if (err) {
3053 7e8004ba 2022-09-11 thomas free(matched);
3054 7e8004ba 2022-09-11 thomas return err;
3055 7e8004ba 2022-09-11 thomas }
3056 7e8004ba 2022-09-11 thomas
3057 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3058 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3059 7e8004ba 2022-09-11 thomas matched, entry);
3060 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3061 7e8004ba 2022-09-11 thomas }
3062 7e8004ba 2022-09-11 thomas }
3063 7e8004ba 2022-09-11 thomas
3064 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3065 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3066 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3067 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3068 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3069 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3070 7e8004ba 2022-09-11 thomas if (err)
3071 7e8004ba 2022-09-11 thomas return err;
3072 7e8004ba 2022-09-11 thomas }
3073 7e8004ba 2022-09-11 thomas
3074 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3075 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3076 7e8004ba 2022-09-11 thomas s->selected = 0;
3077 7e8004ba 2022-09-11 thomas
3078 7e8004ba 2022-09-11 thomas return NULL;
3079 7e8004ba 2022-09-11 thomas }
3080 7e8004ba 2022-09-11 thomas
3081 7e8004ba 2022-09-11 thomas static const struct got_error *
3082 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3083 60493ae3 2019-06-20 stsp {
3084 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3085 60493ae3 2019-06-20 stsp
3086 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3087 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3088 60493ae3 2019-06-20 stsp return NULL;
3089 60493ae3 2019-06-20 stsp }
3090 60493ae3 2019-06-20 stsp
3091 60493ae3 2019-06-20 stsp static const struct got_error *
3092 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3093 60493ae3 2019-06-20 stsp {
3094 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3095 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3096 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3097 60493ae3 2019-06-20 stsp
3098 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3099 f9686aa5 2020-03-27 stsp show_log_view(view);
3100 f9686aa5 2020-03-27 stsp update_panels();
3101 f9686aa5 2020-03-27 stsp doupdate();
3102 f9686aa5 2020-03-27 stsp
3103 96e2b566 2019-07-08 stsp if (s->search_entry) {
3104 13add988 2019-10-15 stsp int errcode, ch;
3105 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3106 13add988 2019-10-15 stsp if (errcode)
3107 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3108 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3109 13add988 2019-10-15 stsp ch = wgetch(view->window);
3110 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3111 13add988 2019-10-15 stsp if (errcode)
3112 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3113 13add988 2019-10-15 stsp "pthread_mutex_lock");
3114 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3115 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3116 678cbce5 2019-07-28 stsp return NULL;
3117 678cbce5 2019-07-28 stsp }
3118 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3119 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3120 96e2b566 2019-07-08 stsp else
3121 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3122 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3123 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3124 df5e841d 2022-06-23 thomas /*
3125 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3126 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3127 1f4d5162 2022-06-23 thomas * might have changed.
3128 df5e841d 2022-06-23 thomas */
3129 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3130 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3131 e7baaec8 2022-09-13 thomas else
3132 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3133 e7baaec8 2022-09-13 thomas entry);
3134 20be8d96 2019-06-21 stsp } else {
3135 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3136 20be8d96 2019-06-21 stsp }
3137 60493ae3 2019-06-20 stsp
3138 60493ae3 2019-06-20 stsp while (1) {
3139 13add988 2019-10-15 stsp int have_match = 0;
3140 13add988 2019-10-15 stsp
3141 60493ae3 2019-06-20 stsp if (entry == NULL) {
3142 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3143 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3144 f9967bca 2020-03-27 stsp view->search_next_done =
3145 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3146 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3147 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3148 f801134a 2019-06-25 stsp return NULL;
3149 60493ae3 2019-06-20 stsp }
3150 96e2b566 2019-07-08 stsp /*
3151 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3152 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3153 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3154 96e2b566 2019-07-08 stsp */
3155 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3156 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3157 60493ae3 2019-06-20 stsp }
3158 60493ae3 2019-06-20 stsp
3159 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3160 13add988 2019-10-15 stsp &view->regex);
3161 5943eee2 2019-08-13 stsp if (err)
3162 13add988 2019-10-15 stsp break;
3163 13add988 2019-10-15 stsp if (have_match) {
3164 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3165 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3166 60493ae3 2019-06-20 stsp break;
3167 60493ae3 2019-06-20 stsp }
3168 13add988 2019-10-15 stsp
3169 96e2b566 2019-07-08 stsp s->search_entry = entry;
3170 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3171 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3172 b1bf1435 2019-06-21 stsp else
3173 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3174 60493ae3 2019-06-20 stsp }
3175 60493ae3 2019-06-20 stsp
3176 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3177 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3178 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3179 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3180 60493ae3 2019-06-20 stsp if (err)
3181 60493ae3 2019-06-20 stsp return err;
3182 ead14cbe 2019-06-21 stsp cur++;
3183 ead14cbe 2019-06-21 stsp }
3184 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3185 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3186 60493ae3 2019-06-20 stsp if (err)
3187 60493ae3 2019-06-20 stsp return err;
3188 ead14cbe 2019-06-21 stsp cur--;
3189 60493ae3 2019-06-20 stsp }
3190 60493ae3 2019-06-20 stsp }
3191 60493ae3 2019-06-20 stsp
3192 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3193 96e2b566 2019-07-08 stsp
3194 60493ae3 2019-06-20 stsp return NULL;
3195 60493ae3 2019-06-20 stsp }
3196 60493ae3 2019-06-20 stsp
3197 60493ae3 2019-06-20 stsp static const struct got_error *
3198 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3199 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3200 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3201 80ddbec8 2018-04-29 stsp {
3202 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3203 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3204 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3205 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3206 1a76625f 2018-10-22 stsp int errcode;
3207 80ddbec8 2018-04-29 stsp
3208 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3209 f135c941 2020-02-20 stsp free(s->in_repo_path);
3210 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3211 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3212 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3213 f135c941 2020-02-20 stsp }
3214 ecb28ae0 2018-07-16 stsp
3215 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3216 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3217 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3218 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3219 78756c87 2020-11-24 stsp
3220 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3221 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3222 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3223 7e8004ba 2022-09-11 thomas
3224 fb2756b9 2018-08-04 stsp s->repo = repo;
3225 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3226 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3227 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3228 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3229 9cd7cbd1 2020-12-07 stsp goto done;
3230 9cd7cbd1 2020-12-07 stsp }
3231 9cd7cbd1 2020-12-07 stsp }
3232 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3233 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3234 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3235 5036bf37 2018-09-24 stsp goto done;
3236 5036bf37 2018-09-24 stsp }
3237 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3238 e5a0f69f 2018-08-18 stsp
3239 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3240 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3241 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3242 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3243 11b20872 2019-11-08 stsp if (err)
3244 11b20872 2019-11-08 stsp goto done;
3245 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3246 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3247 11b20872 2019-11-08 stsp if (err) {
3248 11b20872 2019-11-08 stsp free_colors(&s->colors);
3249 11b20872 2019-11-08 stsp goto done;
3250 11b20872 2019-11-08 stsp }
3251 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3252 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3253 11b20872 2019-11-08 stsp if (err) {
3254 11b20872 2019-11-08 stsp free_colors(&s->colors);
3255 11b20872 2019-11-08 stsp goto done;
3256 11b20872 2019-11-08 stsp }
3257 11b20872 2019-11-08 stsp }
3258 11b20872 2019-11-08 stsp
3259 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3260 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3261 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3262 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3263 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3264 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3265 1a76625f 2018-10-22 stsp
3266 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3267 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3268 1af5eddf 2022-06-23 thomas if (err)
3269 1af5eddf 2022-06-23 thomas goto done;
3270 1af5eddf 2022-06-23 thomas }
3271 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3272 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3273 7cd52833 2022-06-23 thomas if (err)
3274 7cd52833 2022-06-23 thomas goto done;
3275 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3276 b672a97a 2020-01-27 stsp !s->log_branches);
3277 1a76625f 2018-10-22 stsp if (err)
3278 1a76625f 2018-10-22 stsp goto done;
3279 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3280 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3281 c5b78334 2020-01-12 stsp if (err)
3282 c5b78334 2020-01-12 stsp goto done;
3283 1a76625f 2018-10-22 stsp
3284 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3285 1a76625f 2018-10-22 stsp if (errcode) {
3286 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3287 1a76625f 2018-10-22 stsp goto done;
3288 1a76625f 2018-10-22 stsp }
3289 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3290 7c1452c1 2020-03-26 stsp if (errcode) {
3291 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3292 7c1452c1 2020-03-26 stsp goto done;
3293 7c1452c1 2020-03-26 stsp }
3294 1a76625f 2018-10-22 stsp
3295 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3296 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3297 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3298 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3299 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3300 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3301 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3302 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3303 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3304 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3305 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3306 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3307 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3308 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3309 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3310 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3311 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3312 ba4f502b 2018-08-04 stsp done:
3313 1a76625f 2018-10-22 stsp if (err)
3314 1a76625f 2018-10-22 stsp close_log_view(view);
3315 ba4f502b 2018-08-04 stsp return err;
3316 ba4f502b 2018-08-04 stsp }
3317 ba4f502b 2018-08-04 stsp
3318 e5a0f69f 2018-08-18 stsp static const struct got_error *
3319 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3320 ba4f502b 2018-08-04 stsp {
3321 f2f6d207 2020-11-24 stsp const struct got_error *err;
3322 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3323 ba4f502b 2018-08-04 stsp
3324 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3325 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3326 2b380cc8 2018-10-24 stsp &s->thread_args);
3327 2b380cc8 2018-10-24 stsp if (errcode)
3328 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3329 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3330 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3331 f2f6d207 2020-11-24 stsp if (err)
3332 f2f6d207 2020-11-24 stsp return err;
3333 f2f6d207 2020-11-24 stsp }
3334 2b380cc8 2018-10-24 stsp }
3335 2b380cc8 2018-10-24 stsp
3336 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3337 b31f89ff 2022-07-01 thomas }
3338 b31f89ff 2022-07-01 thomas
3339 b31f89ff 2022-07-01 thomas static void
3340 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3341 b31f89ff 2022-07-01 thomas {
3342 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3343 b31f89ff 2022-07-01 thomas
3344 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3345 b31f89ff 2022-07-01 thomas return;
3346 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3347 7e8004ba 2022-09-11 thomas view->count = 0;
3348 b31f89ff 2022-07-01 thomas
3349 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3350 b31f89ff 2022-07-01 thomas || home)
3351 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3352 b31f89ff 2022-07-01 thomas
3353 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3354 b31f89ff 2022-07-01 thomas --s->selected;
3355 b31f89ff 2022-07-01 thomas else
3356 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3357 b31f89ff 2022-07-01 thomas
3358 b31f89ff 2022-07-01 thomas select_commit(s);
3359 b31f89ff 2022-07-01 thomas return;
3360 b31f89ff 2022-07-01 thomas }
3361 b31f89ff 2022-07-01 thomas
3362 b31f89ff 2022-07-01 thomas static const struct got_error *
3363 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3364 b31f89ff 2022-07-01 thomas {
3365 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3366 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3367 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3368 b31f89ff 2022-07-01 thomas
3369 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3370 7e8004ba 2022-09-11 thomas return NULL;
3371 7e8004ba 2022-09-11 thomas
3372 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3373 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3374 b31f89ff 2022-07-01 thomas return NULL;
3375 b31f89ff 2022-07-01 thomas
3376 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3377 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3378 b31f89ff 2022-07-01 thomas
3379 7ed048bd 2022-08-12 thomas if (!page) {
3380 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3381 b31f89ff 2022-07-01 thomas ++s->selected;
3382 b31f89ff 2022-07-01 thomas else
3383 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3384 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3385 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3386 7ed048bd 2022-08-12 thomas int n;
3387 7ed048bd 2022-08-12 thomas
3388 7ed048bd 2022-08-12 thomas s->selected = 0;
3389 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3390 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3391 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3392 7ed048bd 2022-08-12 thomas if (entry == NULL)
3393 7ed048bd 2022-08-12 thomas break;
3394 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3395 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3396 7ed048bd 2022-08-12 thomas }
3397 7ed048bd 2022-08-12 thomas if (n > 0)
3398 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3399 b31f89ff 2022-07-01 thomas } else {
3400 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3401 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3402 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3403 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3404 bd3f8225 2022-08-12 thomas else
3405 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3406 b31f89ff 2022-07-01 thomas }
3407 b31f89ff 2022-07-01 thomas if (err)
3408 b31f89ff 2022-07-01 thomas return err;
3409 b31f89ff 2022-07-01 thomas
3410 a5d43cac 2022-07-01 thomas /*
3411 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3412 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3413 a5d43cac 2022-07-01 thomas */
3414 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3415 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3416 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3417 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3418 25100026 2022-08-13 thomas }
3419 a5d43cac 2022-07-01 thomas
3420 b31f89ff 2022-07-01 thomas select_commit(s);
3421 b31f89ff 2022-07-01 thomas
3422 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3423 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3424 b31f89ff 2022-07-01 thomas view->count = 0;
3425 b31f89ff 2022-07-01 thomas
3426 b31f89ff 2022-07-01 thomas return NULL;
3427 e5a0f69f 2018-08-18 stsp }
3428 04cc582a 2018-08-01 stsp
3429 a5d43cac 2022-07-01 thomas static void
3430 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3431 a5d43cac 2022-07-01 thomas {
3432 24415785 2022-07-03 thomas *x = 0;
3433 24415785 2022-07-03 thomas *y = 0;
3434 24415785 2022-07-03 thomas
3435 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3436 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3437 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3438 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3439 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3440 53d2bdd3 2022-07-10 thomas else
3441 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3442 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3443 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3444 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3445 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3446 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3447 53d2bdd3 2022-07-10 thomas else
3448 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3449 53d2bdd3 2022-07-10 thomas }
3450 a5d43cac 2022-07-01 thomas }
3451 a5d43cac 2022-07-01 thomas
3452 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3453 e5a0f69f 2018-08-18 stsp static const struct got_error *
3454 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3455 a5d43cac 2022-07-01 thomas {
3456 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3457 a5d43cac 2022-07-01 thomas
3458 a5d43cac 2022-07-01 thomas view->nlines = y;
3459 64486692 2022-07-07 thomas view->ncols = COLS;
3460 a5d43cac 2022-07-01 thomas err = view_resize(view);
3461 a5d43cac 2022-07-01 thomas if (err)
3462 a5d43cac 2022-07-01 thomas return err;
3463 a5d43cac 2022-07-01 thomas
3464 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3465 a5d43cac 2022-07-01 thomas
3466 a5d43cac 2022-07-01 thomas return err;
3467 07dd3ed3 2022-08-06 thomas }
3468 07dd3ed3 2022-08-06 thomas
3469 07dd3ed3 2022-08-06 thomas static const struct got_error *
3470 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3471 07dd3ed3 2022-08-06 thomas {
3472 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3473 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3474 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3475 25100026 2022-08-13 thomas
3476 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3477 25100026 2022-08-13 thomas return NULL;
3478 07dd3ed3 2022-08-06 thomas
3479 07dd3ed3 2022-08-06 thomas g = view->gline;
3480 07dd3ed3 2022-08-06 thomas view->gline = 0;
3481 07dd3ed3 2022-08-06 thomas
3482 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3483 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3484 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3485 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3486 07dd3ed3 2022-08-06 thomas select_commit(s);
3487 07dd3ed3 2022-08-06 thomas return NULL;
3488 07dd3ed3 2022-08-06 thomas }
3489 07dd3ed3 2022-08-06 thomas
3490 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3491 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3492 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3493 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3494 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3495 07dd3ed3 2022-08-06 thomas if (err)
3496 07dd3ed3 2022-08-06 thomas return err;
3497 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3498 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3499 07dd3ed3 2022-08-06 thomas
3500 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3501 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3502 07dd3ed3 2022-08-06 thomas
3503 07dd3ed3 2022-08-06 thomas select_commit(s);
3504 07dd3ed3 2022-08-06 thomas return NULL;
3505 07dd3ed3 2022-08-06 thomas
3506 a5d43cac 2022-07-01 thomas }
3507 a5d43cac 2022-07-01 thomas
3508 a5d43cac 2022-07-01 thomas static const struct got_error *
3509 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3510 e5a0f69f 2018-08-18 stsp {
3511 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3512 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3513 7ed048bd 2022-08-12 thomas int eos, nscroll;
3514 80ddbec8 2018-04-29 stsp
3515 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3516 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3517 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3518 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3519 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3520 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3521 fb280deb 2021-08-30 stsp }
3522 c0be8933 2022-08-12 thomas if (err)
3523 c0be8933 2022-08-12 thomas return err;
3524 528dedf3 2021-08-30 stsp }
3525 068ab281 2022-07-01 thomas
3526 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3527 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3528 068ab281 2022-07-01 thomas --eos; /* border */
3529 07dd3ed3 2022-08-06 thomas
3530 07dd3ed3 2022-08-06 thomas if (view->gline)
3531 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3532 068ab281 2022-07-01 thomas
3533 528dedf3 2021-08-30 stsp switch (ch) {
3534 7e8004ba 2022-09-11 thomas case '&':
3535 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3536 7e8004ba 2022-09-11 thomas break;
3537 1e37a5c2 2019-05-12 jcs case 'q':
3538 1e37a5c2 2019-05-12 jcs s->quit = 1;
3539 05171be4 2022-06-23 thomas break;
3540 05171be4 2022-06-23 thomas case '0':
3541 05171be4 2022-06-23 thomas view->x = 0;
3542 05171be4 2022-06-23 thomas break;
3543 05171be4 2022-06-23 thomas case '$':
3544 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3545 07b0611c 2022-06-23 thomas view->count = 0;
3546 05171be4 2022-06-23 thomas break;
3547 05171be4 2022-06-23 thomas case KEY_RIGHT:
3548 05171be4 2022-06-23 thomas case 'l':
3549 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3550 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3551 07b0611c 2022-06-23 thomas else
3552 07b0611c 2022-06-23 thomas view->count = 0;
3553 1e37a5c2 2019-05-12 jcs break;
3554 05171be4 2022-06-23 thomas case KEY_LEFT:
3555 05171be4 2022-06-23 thomas case 'h':
3556 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3557 07b0611c 2022-06-23 thomas if (view->x <= 0)
3558 07b0611c 2022-06-23 thomas view->count = 0;
3559 05171be4 2022-06-23 thomas break;
3560 1e37a5c2 2019-05-12 jcs case 'k':
3561 1e37a5c2 2019-05-12 jcs case KEY_UP:
3562 1e37a5c2 2019-05-12 jcs case '<':
3563 1e37a5c2 2019-05-12 jcs case ',':
3564 f7140bf5 2021-10-17 thomas case CTRL('p'):
3565 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3566 912a3f79 2021-08-30 j break;
3567 912a3f79 2021-08-30 j case 'g':
3568 912a3f79 2021-08-30 j case KEY_HOME:
3569 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3570 07b0611c 2022-06-23 thomas view->count = 0;
3571 1e37a5c2 2019-05-12 jcs break;
3572 70f17a53 2022-06-13 thomas case CTRL('u'):
3573 23427b14 2022-06-23 thomas case 'u':
3574 70f17a53 2022-06-13 thomas nscroll /= 2;
3575 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3576 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3577 a4292ac5 2019-05-12 jcs case CTRL('b'):
3578 1c5e5faa 2022-06-23 thomas case 'b':
3579 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3580 1e37a5c2 2019-05-12 jcs break;
3581 1e37a5c2 2019-05-12 jcs case 'j':
3582 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3583 1e37a5c2 2019-05-12 jcs case '>':
3584 1e37a5c2 2019-05-12 jcs case '.':
3585 f7140bf5 2021-10-17 thomas case CTRL('n'):
3586 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3587 912a3f79 2021-08-30 j break;
3588 f69c5a46 2022-07-19 thomas case '@':
3589 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3590 f69c5a46 2022-07-19 thomas break;
3591 912a3f79 2021-08-30 j case 'G':
3592 912a3f79 2021-08-30 j case KEY_END: {
3593 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3594 912a3f79 2021-08-30 j * traverse them all. */
3595 07b0611c 2022-06-23 thomas view->count = 0;
3596 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3597 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3598 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3599 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3600 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3601 1e37a5c2 2019-05-12 jcs break;
3602 912a3f79 2021-08-30 j }
3603 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3604 23427b14 2022-06-23 thomas case 'd':
3605 70f17a53 2022-06-13 thomas nscroll /= 2;
3606 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3607 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3608 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3609 4c2d69cb 2022-06-23 thomas case 'f':
3610 b31f89ff 2022-07-01 thomas case ' ':
3611 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3612 1e37a5c2 2019-05-12 jcs break;
3613 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3614 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3615 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3616 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3617 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3618 2b779855 2020-12-05 naddy select_commit(s);
3619 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3620 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3621 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3622 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3623 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3624 0bf7f153 2020-12-02 naddy }
3625 1e37a5c2 2019-05-12 jcs break;
3626 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3627 444d5325 2022-07-03 thomas case '\r':
3628 07b0611c 2022-06-23 thomas view->count = 0;
3629 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3630 e5a0f69f 2018-08-18 stsp break;
3631 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3632 1e37a5c2 2019-05-12 jcs break;
3633 1be4947a 2022-07-22 thomas case 'T':
3634 07b0611c 2022-06-23 thomas view->count = 0;
3635 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3636 5036bf37 2018-09-24 stsp break;
3637 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3638 1e37a5c2 2019-05-12 jcs break;
3639 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3640 21355643 2020-12-06 stsp case CTRL('l'):
3641 21355643 2020-12-06 stsp case 'B':
3642 07b0611c 2022-06-23 thomas view->count = 0;
3643 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3644 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3645 1e37a5c2 2019-05-12 jcs break;
3646 21355643 2020-12-06 stsp err = stop_log_thread(s);
3647 74cfe85e 2020-10-20 stsp if (err)
3648 74cfe85e 2020-10-20 stsp return err;
3649 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3650 21355643 2020-12-06 stsp char *parent_path;
3651 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3652 21355643 2020-12-06 stsp if (err)
3653 21355643 2020-12-06 stsp return err;
3654 21355643 2020-12-06 stsp free(s->in_repo_path);
3655 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3656 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3657 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3658 21355643 2020-12-06 stsp struct got_object_id *start_id;
3659 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3660 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3661 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3662 21355643 2020-12-06 stsp if (err)
3663 21355643 2020-12-06 stsp return err;
3664 21355643 2020-12-06 stsp free(s->start_id);
3665 21355643 2020-12-06 stsp s->start_id = start_id;
3666 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3667 21355643 2020-12-06 stsp } else /* 'B' */
3668 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3669 21355643 2020-12-06 stsp
3670 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3671 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3672 bf7e79b3 2022-06-23 thomas if (err)
3673 bf7e79b3 2022-06-23 thomas return err;
3674 bf7e79b3 2022-06-23 thomas }
3675 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3676 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3677 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3678 74cfe85e 2020-10-20 stsp if (err)
3679 21355643 2020-12-06 stsp return err;
3680 51a10b52 2020-12-26 stsp tog_free_refs();
3681 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3682 ca51c541 2020-12-07 stsp if (err)
3683 ca51c541 2020-12-07 stsp return err;
3684 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3685 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3686 d01904d4 2019-06-25 stsp if (err)
3687 d01904d4 2019-06-25 stsp return err;
3688 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3689 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3690 b672a97a 2020-01-27 stsp if (err)
3691 b672a97a 2020-01-27 stsp return err;
3692 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3693 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3694 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3695 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3696 21355643 2020-12-06 stsp s->selected_entry = NULL;
3697 21355643 2020-12-06 stsp s->selected = 0;
3698 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3699 21355643 2020-12-06 stsp s->quit = 0;
3700 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3701 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3702 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3703 94ecf40d 2022-07-22 thomas view->offset = 0;
3704 d01904d4 2019-06-25 stsp break;
3705 1be4947a 2022-07-22 thomas case 'R':
3706 07b0611c 2022-06-23 thomas view->count = 0;
3707 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3708 6458efa5 2020-11-24 stsp break;
3709 1e37a5c2 2019-05-12 jcs default:
3710 07b0611c 2022-06-23 thomas view->count = 0;
3711 1e37a5c2 2019-05-12 jcs break;
3712 899d86c2 2018-05-10 stsp }
3713 e5a0f69f 2018-08-18 stsp
3714 80ddbec8 2018-04-29 stsp return err;
3715 80ddbec8 2018-04-29 stsp }
3716 80ddbec8 2018-04-29 stsp
3717 4ed7e80c 2018-05-20 stsp static const struct got_error *
3718 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3719 c2db6724 2019-01-04 stsp {
3720 c2db6724 2019-01-04 stsp const struct got_error *error;
3721 c2db6724 2019-01-04 stsp
3722 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3723 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3724 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3725 37c06ea4 2019-07-15 stsp #endif
3726 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3727 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3728 c2db6724 2019-01-04 stsp
3729 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3730 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3731 c2db6724 2019-01-04 stsp
3732 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3733 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3734 c2db6724 2019-01-04 stsp
3735 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3736 c2db6724 2019-01-04 stsp if (error != NULL)
3737 c2db6724 2019-01-04 stsp return error;
3738 c2db6724 2019-01-04 stsp
3739 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3740 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3741 c2db6724 2019-01-04 stsp
3742 c2db6724 2019-01-04 stsp return NULL;
3743 c2db6724 2019-01-04 stsp }
3744 c2db6724 2019-01-04 stsp
3745 a915003a 2019-02-05 stsp static void
3746 a915003a 2019-02-05 stsp init_curses(void)
3747 a915003a 2019-02-05 stsp {
3748 296152d1 2022-05-31 thomas /*
3749 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3750 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3751 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3752 296152d1 2022-05-31 thomas */
3753 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3754 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3755 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3756 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3757 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3758 296152d1 2022-05-31 thomas
3759 a915003a 2019-02-05 stsp initscr();
3760 a915003a 2019-02-05 stsp cbreak();
3761 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3762 a915003a 2019-02-05 stsp noecho();
3763 a915003a 2019-02-05 stsp nonl();
3764 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3765 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3766 a915003a 2019-02-05 stsp curs_set(0);
3767 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3768 6d17833f 2019-11-08 stsp start_color();
3769 6d17833f 2019-11-08 stsp use_default_colors();
3770 6d17833f 2019-11-08 stsp }
3771 a915003a 2019-02-05 stsp }
3772 a915003a 2019-02-05 stsp
3773 c2db6724 2019-01-04 stsp static const struct got_error *
3774 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3775 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3776 f135c941 2020-02-20 stsp {
3777 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3778 f135c941 2020-02-20 stsp
3779 f135c941 2020-02-20 stsp if (argc == 0) {
3780 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3781 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3782 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3783 f135c941 2020-02-20 stsp return NULL;
3784 f135c941 2020-02-20 stsp }
3785 f135c941 2020-02-20 stsp
3786 f135c941 2020-02-20 stsp if (worktree) {
3787 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3788 bfd61697 2020-11-14 stsp char *p;
3789 f135c941 2020-02-20 stsp
3790 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3791 f135c941 2020-02-20 stsp if (err)
3792 f135c941 2020-02-20 stsp return err;
3793 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3794 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3795 bfd61697 2020-11-14 stsp p) == -1) {
3796 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3797 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3798 f135c941 2020-02-20 stsp }
3799 f135c941 2020-02-20 stsp free(p);
3800 f135c941 2020-02-20 stsp } else
3801 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3802 f135c941 2020-02-20 stsp
3803 f135c941 2020-02-20 stsp return err;
3804 f135c941 2020-02-20 stsp }
3805 f135c941 2020-02-20 stsp
3806 f135c941 2020-02-20 stsp static const struct got_error *
3807 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3808 9f7d7167 2018-04-29 stsp {
3809 80ddbec8 2018-04-29 stsp const struct got_error *error;
3810 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3811 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3812 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3813 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3814 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3815 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3816 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3817 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3818 04cc582a 2018-08-01 stsp struct tog_view *view;
3819 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3820 80ddbec8 2018-04-29 stsp
3821 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3822 80ddbec8 2018-04-29 stsp switch (ch) {
3823 b672a97a 2020-01-27 stsp case 'b':
3824 b672a97a 2020-01-27 stsp log_branches = 1;
3825 b672a97a 2020-01-27 stsp break;
3826 80ddbec8 2018-04-29 stsp case 'c':
3827 80ddbec8 2018-04-29 stsp start_commit = optarg;
3828 80ddbec8 2018-04-29 stsp break;
3829 ecb28ae0 2018-07-16 stsp case 'r':
3830 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3831 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3833 9ba1d308 2019-10-21 stsp optarg);
3834 ecb28ae0 2018-07-16 stsp break;
3835 80ddbec8 2018-04-29 stsp default:
3836 17020d27 2019-03-07 stsp usage_log();
3837 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3838 80ddbec8 2018-04-29 stsp }
3839 80ddbec8 2018-04-29 stsp }
3840 80ddbec8 2018-04-29 stsp
3841 80ddbec8 2018-04-29 stsp argc -= optind;
3842 80ddbec8 2018-04-29 stsp argv += optind;
3843 80ddbec8 2018-04-29 stsp
3844 f135c941 2020-02-20 stsp if (argc > 1)
3845 f135c941 2020-02-20 stsp usage_log();
3846 963f97a1 2019-03-18 stsp
3847 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3848 7cd52833 2022-06-23 thomas if (error != NULL)
3849 7cd52833 2022-06-23 thomas goto done;
3850 7cd52833 2022-06-23 thomas
3851 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3852 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3853 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3854 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3855 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3856 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3857 c156c7a4 2020-12-18 stsp goto done;
3858 a1fbf39a 2019-08-11 stsp if (worktree)
3859 6962eb72 2020-02-20 stsp repo_path =
3860 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3861 a1fbf39a 2019-08-11 stsp else
3862 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3863 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3864 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3865 c156c7a4 2020-12-18 stsp goto done;
3866 c156c7a4 2020-12-18 stsp }
3867 ecb28ae0 2018-07-16 stsp }
3868 ecb28ae0 2018-07-16 stsp
3869 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3870 80ddbec8 2018-04-29 stsp if (error != NULL)
3871 ecb28ae0 2018-07-16 stsp goto done;
3872 80ddbec8 2018-04-29 stsp
3873 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3874 f135c941 2020-02-20 stsp repo, worktree);
3875 f135c941 2020-02-20 stsp if (error)
3876 f135c941 2020-02-20 stsp goto done;
3877 f135c941 2020-02-20 stsp
3878 f135c941 2020-02-20 stsp init_curses();
3879 f135c941 2020-02-20 stsp
3880 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3881 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3882 c02c541e 2019-03-29 stsp if (error)
3883 c02c541e 2019-03-29 stsp goto done;
3884 c02c541e 2019-03-29 stsp
3885 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3886 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3887 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3888 87670572 2020-12-26 naddy if (error)
3889 87670572 2020-12-26 naddy goto done;
3890 87670572 2020-12-26 naddy }
3891 51a10b52 2020-12-26 stsp
3892 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3893 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3894 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3895 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3896 d8f38dc4 2020-12-05 stsp if (error)
3897 d8f38dc4 2020-12-05 stsp goto done;
3898 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3899 d8f38dc4 2020-12-05 stsp } else {
3900 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3901 d8f38dc4 2020-12-05 stsp if (error == NULL)
3902 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3903 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3904 d8f38dc4 2020-12-05 stsp goto done;
3905 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3906 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3907 d8f38dc4 2020-12-05 stsp if (error)
3908 d8f38dc4 2020-12-05 stsp goto done;
3909 d8f38dc4 2020-12-05 stsp }
3910 8b473291 2019-02-21 stsp
3911 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3912 04cc582a 2018-08-01 stsp if (view == NULL) {
3913 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3914 04cc582a 2018-08-01 stsp goto done;
3915 04cc582a 2018-08-01 stsp }
3916 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3917 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3918 ba4f502b 2018-08-04 stsp if (error)
3919 ba4f502b 2018-08-04 stsp goto done;
3920 2fc00ff4 2019-08-31 stsp if (worktree) {
3921 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3922 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3923 2fc00ff4 2019-08-31 stsp worktree = NULL;
3924 2fc00ff4 2019-08-31 stsp }
3925 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3926 ecb28ae0 2018-07-16 stsp done:
3927 f135c941 2020-02-20 stsp free(in_repo_path);
3928 ecb28ae0 2018-07-16 stsp free(repo_path);
3929 ecb28ae0 2018-07-16 stsp free(cwd);
3930 899d86c2 2018-05-10 stsp free(start_id);
3931 d8f38dc4 2020-12-05 stsp free(label);
3932 d8f38dc4 2020-12-05 stsp if (ref)
3933 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3934 1d0f4054 2021-06-17 stsp if (repo) {
3935 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3936 1d0f4054 2021-06-17 stsp if (error == NULL)
3937 1d0f4054 2021-06-17 stsp error = close_err;
3938 1d0f4054 2021-06-17 stsp }
3939 ec142235 2019-03-07 stsp if (worktree)
3940 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3941 7cd52833 2022-06-23 thomas if (pack_fds) {
3942 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3943 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3944 7cd52833 2022-06-23 thomas if (error == NULL)
3945 7cd52833 2022-06-23 thomas error = pack_err;
3946 7cd52833 2022-06-23 thomas }
3947 51a10b52 2020-12-26 stsp tog_free_refs();
3948 80ddbec8 2018-04-29 stsp return error;
3949 9f7d7167 2018-04-29 stsp }
3950 9f7d7167 2018-04-29 stsp
3951 4ed7e80c 2018-05-20 stsp __dead static void
3952 9f7d7167 2018-04-29 stsp usage_diff(void)
3953 9f7d7167 2018-04-29 stsp {
3954 80ddbec8 2018-04-29 stsp endwin();
3955 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3956 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3957 9f7d7167 2018-04-29 stsp exit(1);
3958 b304db33 2018-05-20 stsp }
3959 b304db33 2018-05-20 stsp
3960 6d17833f 2019-11-08 stsp static int
3961 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3962 41605754 2020-11-12 stsp regmatch_t *regmatch)
3963 6d17833f 2019-11-08 stsp {
3964 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3965 6d17833f 2019-11-08 stsp }
3966 6d17833f 2019-11-08 stsp
3967 ef20f542 2022-06-26 thomas static struct tog_color *
3968 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3969 6d17833f 2019-11-08 stsp {
3970 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3971 6d17833f 2019-11-08 stsp
3972 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3973 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3974 6d17833f 2019-11-08 stsp return tc;
3975 6d17833f 2019-11-08 stsp }
3976 6d17833f 2019-11-08 stsp
3977 6d17833f 2019-11-08 stsp return NULL;
3978 6d17833f 2019-11-08 stsp }
3979 6d17833f 2019-11-08 stsp
3980 4ed7e80c 2018-05-20 stsp static const struct got_error *
3981 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3982 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3983 41605754 2020-11-12 stsp {
3984 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3985 666f7b10 2022-06-23 thomas char *exstr = NULL;
3986 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3987 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3988 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3989 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3990 41605754 2020-11-12 stsp
3991 41605754 2020-11-12 stsp *wtotal = 0;
3992 cbea3800 2022-06-23 thomas
3993 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3994 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3995 41605754 2020-11-12 stsp
3996 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3997 666f7b10 2022-06-23 thomas if (err)
3998 666f7b10 2022-06-23 thomas return err;
3999 666f7b10 2022-06-23 thomas
4000 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4001 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4002 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4003 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4004 666f7b10 2022-06-23 thomas goto done;
4005 666f7b10 2022-06-23 thomas }
4006 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4007 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4008 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4009 cbea3800 2022-06-23 thomas goto done;
4010 cbea3800 2022-06-23 thomas }
4011 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4012 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4013 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4014 cbea3800 2022-06-23 thomas goto done;
4015 cbea3800 2022-06-23 thomas }
4016 05171be4 2022-06-23 thomas
4017 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4018 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4019 cbea3800 2022-06-23 thomas col_tab_align, 1);
4020 cbea3800 2022-06-23 thomas if (err)
4021 cbea3800 2022-06-23 thomas goto done;
4022 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4023 05171be4 2022-06-23 thomas if (n) {
4024 cbea3800 2022-06-23 thomas free(wline);
4025 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4026 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4027 cbea3800 2022-06-23 thomas if (err)
4028 cbea3800 2022-06-23 thomas goto done;
4029 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4030 cbea3800 2022-06-23 thomas wlimit -= width;
4031 cbea3800 2022-06-23 thomas *wtotal += width;
4032 41605754 2020-11-12 stsp }
4033 41605754 2020-11-12 stsp
4034 41605754 2020-11-12 stsp if (wlimit > 0) {
4035 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4036 cbea3800 2022-06-23 thomas size_t wlen;
4037 cbea3800 2022-06-23 thomas
4038 cbea3800 2022-06-23 thomas free(wline);
4039 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4040 cbea3800 2022-06-23 thomas col_tab_align, 1);
4041 cbea3800 2022-06-23 thomas if (err)
4042 cbea3800 2022-06-23 thomas goto done;
4043 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4044 cbea3800 2022-06-23 thomas while (i < wlen) {
4045 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4046 cbea3800 2022-06-23 thomas if (width == -1) {
4047 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4048 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4049 cbea3800 2022-06-23 thomas goto done;
4050 cbea3800 2022-06-23 thomas }
4051 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4052 cbea3800 2022-06-23 thomas break;
4053 1c5e5faa 2022-06-23 thomas w += width;
4054 cbea3800 2022-06-23 thomas i++;
4055 41605754 2020-11-12 stsp }
4056 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4057 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4058 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4059 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4060 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4061 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4062 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4063 41605754 2020-11-12 stsp }
4064 41605754 2020-11-12 stsp }
4065 41605754 2020-11-12 stsp
4066 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4067 cbea3800 2022-06-23 thomas free(wline);
4068 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4069 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4070 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4071 cbea3800 2022-06-23 thomas col_tab_align, 1);
4072 cbea3800 2022-06-23 thomas if (err)
4073 cbea3800 2022-06-23 thomas goto done;
4074 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4075 cbea3800 2022-06-23 thomas } else {
4076 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4077 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4078 cbea3800 2022-06-23 thomas if (err)
4079 cbea3800 2022-06-23 thomas goto done;
4080 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4081 cbea3800 2022-06-23 thomas }
4082 cbea3800 2022-06-23 thomas *wtotal += width2;
4083 41605754 2020-11-12 stsp }
4084 cbea3800 2022-06-23 thomas done:
4085 05171be4 2022-06-23 thomas free(wline);
4086 666f7b10 2022-06-23 thomas free(exstr);
4087 cbea3800 2022-06-23 thomas free(seg0);
4088 cbea3800 2022-06-23 thomas free(seg1);
4089 cbea3800 2022-06-23 thomas free(seg2);
4090 cbea3800 2022-06-23 thomas return err;
4091 41605754 2020-11-12 stsp }
4092 07dd3ed3 2022-08-06 thomas
4093 07dd3ed3 2022-08-06 thomas static int
4094 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4095 07dd3ed3 2022-08-06 thomas {
4096 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4097 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4098 07dd3ed3 2022-08-06 thomas
4099 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4100 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4101 41605754 2020-11-12 stsp
4102 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4103 07dd3ed3 2022-08-06 thomas selected = first;
4104 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4105 07dd3ed3 2022-08-06 thomas f = s->f;
4106 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4107 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4108 07dd3ed3 2022-08-06 thomas
4109 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4110 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4111 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4112 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4113 07dd3ed3 2022-08-06 thomas } else
4114 07dd3ed3 2022-08-06 thomas return 0;
4115 07dd3ed3 2022-08-06 thomas
4116 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4117 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4118 07dd3ed3 2022-08-06 thomas return 0;
4119 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4120 07dd3ed3 2022-08-06 thomas rewind(f);
4121 07dd3ed3 2022-08-06 thomas *eof = 0;
4122 07dd3ed3 2022-08-06 thomas *first = 1;
4123 07dd3ed3 2022-08-06 thomas *lineno = 0;
4124 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4125 07dd3ed3 2022-08-06 thomas return 0;
4126 07dd3ed3 2022-08-06 thomas }
4127 07dd3ed3 2022-08-06 thomas
4128 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4129 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4130 07dd3ed3 2022-08-06 thomas view->gline = 0;
4131 07dd3ed3 2022-08-06 thomas
4132 07dd3ed3 2022-08-06 thomas return 1;
4133 07dd3ed3 2022-08-06 thomas }
4134 07dd3ed3 2022-08-06 thomas
4135 41605754 2020-11-12 stsp static const struct got_error *
4136 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4137 26ed57b2 2018-05-19 stsp {
4138 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4139 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4140 61e69b96 2018-05-20 stsp const struct got_error *err;
4141 82c78e96 2022-08-06 thomas int nprinted = 0;
4142 b304db33 2018-05-20 stsp char *line;
4143 826082fe 2020-12-10 stsp size_t linesize = 0;
4144 826082fe 2020-12-10 stsp ssize_t linelen;
4145 61e69b96 2018-05-20 stsp wchar_t *wline;
4146 e0b650dd 2018-05-20 stsp int width;
4147 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4148 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4149 fe621944 2020-11-10 stsp off_t line_offset;
4150 26ed57b2 2018-05-19 stsp
4151 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4152 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4153 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4154 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4155 fe621944 2020-11-10 stsp
4156 f7d12f7e 2018-08-01 stsp werase(view->window);
4157 a3404814 2018-09-02 stsp
4158 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4159 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4160 07dd3ed3 2022-08-06 thomas
4161 a3404814 2018-09-02 stsp if (header) {
4162 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4163 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4164 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4165 07dd3ed3 2022-08-06 thomas
4166 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4167 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4168 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4169 f91a2b48 2022-06-23 thomas 0, 0);
4170 135a2da0 2020-11-11 stsp free(line);
4171 135a2da0 2020-11-11 stsp if (err)
4172 a3404814 2018-09-02 stsp return err;
4173 a3404814 2018-09-02 stsp
4174 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4175 a3404814 2018-09-02 stsp wstandout(view->window);
4176 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4177 e54cc94a 2020-11-11 stsp free(wline);
4178 e54cc94a 2020-11-11 stsp wline = NULL;
4179 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4180 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4181 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4182 a3404814 2018-09-02 stsp wstandend(view->window);
4183 26ed57b2 2018-05-19 stsp
4184 a3404814 2018-09-02 stsp if (max_lines <= 1)
4185 a3404814 2018-09-02 stsp return NULL;
4186 a3404814 2018-09-02 stsp max_lines--;
4187 a3404814 2018-09-02 stsp }
4188 a3404814 2018-09-02 stsp
4189 89f1a395 2020-12-01 naddy s->eof = 0;
4190 05171be4 2022-06-23 thomas view->maxx = 0;
4191 826082fe 2020-12-10 stsp line = NULL;
4192 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4193 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4194 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4195 6568f0aa 2022-08-06 thomas
4196 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4197 826082fe 2020-12-10 stsp if (linelen == -1) {
4198 826082fe 2020-12-10 stsp if (feof(s->f)) {
4199 826082fe 2020-12-10 stsp s->eof = 1;
4200 826082fe 2020-12-10 stsp break;
4201 826082fe 2020-12-10 stsp }
4202 826082fe 2020-12-10 stsp free(line);
4203 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4204 61e69b96 2018-05-20 stsp }
4205 05171be4 2022-06-23 thomas
4206 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4207 07dd3ed3 2022-08-06 thomas continue;
4208 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4209 07dd3ed3 2022-08-06 thomas continue;
4210 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4211 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4212 07dd3ed3 2022-08-06 thomas
4213 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4214 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4215 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4216 cbea3800 2022-06-23 thomas if (err) {
4217 cbea3800 2022-06-23 thomas free(line);
4218 cbea3800 2022-06-23 thomas return err;
4219 cbea3800 2022-06-23 thomas }
4220 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4221 cbea3800 2022-06-23 thomas free(wline);
4222 cbea3800 2022-06-23 thomas wline = NULL;
4223 cbea3800 2022-06-23 thomas
4224 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4225 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4226 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4227 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4228 07dd3ed3 2022-08-06 thomas if (attr)
4229 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4230 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4231 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4232 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4233 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4234 41605754 2020-11-12 stsp if (err) {
4235 41605754 2020-11-12 stsp free(line);
4236 41605754 2020-11-12 stsp return err;
4237 41605754 2020-11-12 stsp }
4238 41605754 2020-11-12 stsp } else {
4239 f1c0ec19 2022-06-23 thomas int skip;
4240 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4241 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4242 f1c0ec19 2022-06-23 thomas if (err) {
4243 f1c0ec19 2022-06-23 thomas free(line);
4244 f1c0ec19 2022-06-23 thomas return err;
4245 f1c0ec19 2022-06-23 thomas }
4246 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4247 f1c0ec19 2022-06-23 thomas free(wline);
4248 41605754 2020-11-12 stsp wline = NULL;
4249 41605754 2020-11-12 stsp }
4250 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4251 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4252 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4253 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4254 07dd3ed3 2022-08-06 thomas } else {
4255 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4256 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4257 07dd3ed3 2022-08-06 thomas }
4258 07dd3ed3 2022-08-06 thomas if (attr)
4259 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4260 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4261 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4262 826082fe 2020-12-10 stsp }
4263 826082fe 2020-12-10 stsp free(line);
4264 fe621944 2020-11-10 stsp if (nprinted >= 1)
4265 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4266 89f1a395 2020-12-01 naddy (nprinted - 1);
4267 fe621944 2020-11-10 stsp else
4268 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4269 26ed57b2 2018-05-19 stsp
4270 a5d43cac 2022-07-01 thomas view_border(view);
4271 c3e9aa98 2019-05-13 jcs
4272 89f1a395 2020-12-01 naddy if (s->eof) {
4273 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4274 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4275 c3e9aa98 2019-05-13 jcs nprinted++;
4276 c3e9aa98 2019-05-13 jcs }
4277 c3e9aa98 2019-05-13 jcs
4278 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4279 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4280 c3e9aa98 2019-05-13 jcs if (err) {
4281 c3e9aa98 2019-05-13 jcs return err;
4282 c3e9aa98 2019-05-13 jcs }
4283 26ed57b2 2018-05-19 stsp
4284 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4285 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4286 e54cc94a 2020-11-11 stsp free(wline);
4287 e54cc94a 2020-11-11 stsp wline = NULL;
4288 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4289 c3e9aa98 2019-05-13 jcs }
4290 c3e9aa98 2019-05-13 jcs
4291 26ed57b2 2018-05-19 stsp return NULL;
4292 abd2672a 2018-12-23 stsp }
4293 abd2672a 2018-12-23 stsp
4294 abd2672a 2018-12-23 stsp static char *
4295 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4296 abd2672a 2018-12-23 stsp {
4297 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4298 09867e48 2019-08-13 stsp char *p, *s;
4299 09867e48 2019-08-13 stsp
4300 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4301 09867e48 2019-08-13 stsp if (tm == NULL)
4302 09867e48 2019-08-13 stsp return NULL;
4303 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4304 09867e48 2019-08-13 stsp if (s == NULL)
4305 09867e48 2019-08-13 stsp return NULL;
4306 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4307 abd2672a 2018-12-23 stsp if (p)
4308 abd2672a 2018-12-23 stsp *p = '\0';
4309 abd2672a 2018-12-23 stsp return s;
4310 9f7d7167 2018-04-29 stsp }
4311 9f7d7167 2018-04-29 stsp
4312 4ed7e80c 2018-05-20 stsp static const struct got_error *
4313 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4314 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4315 0208f208 2020-05-05 stsp {
4316 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4317 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4318 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4319 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4320 0208f208 2020-05-05 stsp
4321 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4322 0208f208 2020-05-05 stsp if (qid != NULL) {
4323 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4324 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4325 ec242592 2022-04-22 thomas &qid->id);
4326 0208f208 2020-05-05 stsp if (err)
4327 0208f208 2020-05-05 stsp return err;
4328 0208f208 2020-05-05 stsp
4329 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4330 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4331 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4332 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4333 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4334 aa8b5dd0 2021-08-01 stsp }
4335 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4336 0208f208 2020-05-05 stsp
4337 0208f208 2020-05-05 stsp }
4338 0208f208 2020-05-05 stsp
4339 0208f208 2020-05-05 stsp if (tree_id1) {
4340 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4341 0208f208 2020-05-05 stsp if (err)
4342 0208f208 2020-05-05 stsp goto done;
4343 0208f208 2020-05-05 stsp }
4344 0208f208 2020-05-05 stsp
4345 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4346 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4347 0208f208 2020-05-05 stsp if (err)
4348 0208f208 2020-05-05 stsp goto done;
4349 0208f208 2020-05-05 stsp
4350 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4351 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4352 0208f208 2020-05-05 stsp done:
4353 0208f208 2020-05-05 stsp if (tree1)
4354 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4355 0208f208 2020-05-05 stsp if (tree2)
4356 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4357 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4358 0208f208 2020-05-05 stsp return err;
4359 0208f208 2020-05-05 stsp }
4360 0208f208 2020-05-05 stsp
4361 0208f208 2020-05-05 stsp static const struct got_error *
4362 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4363 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4364 abd2672a 2018-12-23 stsp {
4365 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4366 fe621944 2020-11-10 stsp
4367 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4368 fe621944 2020-11-10 stsp if (p == NULL)
4369 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4370 82c78e96 2022-08-06 thomas *lines = p;
4371 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4372 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4373 fe621944 2020-11-10 stsp (*nlines)++;
4374 82c78e96 2022-08-06 thomas
4375 fe621944 2020-11-10 stsp return NULL;
4376 fe621944 2020-11-10 stsp }
4377 fe621944 2020-11-10 stsp
4378 fe621944 2020-11-10 stsp static const struct got_error *
4379 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4380 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4381 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4382 fe621944 2020-11-10 stsp {
4383 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4384 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4385 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4386 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4387 45d799e2 2018-12-23 stsp time_t committer_time;
4388 45d799e2 2018-12-23 stsp const char *author, *committer;
4389 8b473291 2019-02-21 stsp char *refs_str = NULL;
4390 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4391 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4392 fe621944 2020-11-10 stsp off_t outoff = 0;
4393 fe621944 2020-11-10 stsp int n;
4394 abd2672a 2018-12-23 stsp
4395 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4396 0208f208 2020-05-05 stsp
4397 8b473291 2019-02-21 stsp if (refs) {
4398 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4399 8b473291 2019-02-21 stsp if (err)
4400 8b473291 2019-02-21 stsp return err;
4401 8b473291 2019-02-21 stsp }
4402 8b473291 2019-02-21 stsp
4403 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4404 abd2672a 2018-12-23 stsp if (err)
4405 abd2672a 2018-12-23 stsp return err;
4406 abd2672a 2018-12-23 stsp
4407 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4408 15a94983 2018-12-23 stsp if (err) {
4409 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4410 15a94983 2018-12-23 stsp goto done;
4411 15a94983 2018-12-23 stsp }
4412 abd2672a 2018-12-23 stsp
4413 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4414 fe621944 2020-11-10 stsp if (err)
4415 fe621944 2020-11-10 stsp goto done;
4416 fe621944 2020-11-10 stsp
4417 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4418 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4419 fe621944 2020-11-10 stsp if (n < 0) {
4420 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4421 abd2672a 2018-12-23 stsp goto done;
4422 abd2672a 2018-12-23 stsp }
4423 fe621944 2020-11-10 stsp outoff += n;
4424 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4425 fe621944 2020-11-10 stsp if (err)
4426 fe621944 2020-11-10 stsp goto done;
4427 fe621944 2020-11-10 stsp
4428 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4429 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4430 fe621944 2020-11-10 stsp if (n < 0) {
4431 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4432 abd2672a 2018-12-23 stsp goto done;
4433 abd2672a 2018-12-23 stsp }
4434 fe621944 2020-11-10 stsp outoff += n;
4435 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4436 fe621944 2020-11-10 stsp if (err)
4437 fe621944 2020-11-10 stsp goto done;
4438 fe621944 2020-11-10 stsp
4439 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4440 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4441 fe621944 2020-11-10 stsp if (datestr) {
4442 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4443 fe621944 2020-11-10 stsp if (n < 0) {
4444 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4445 fe621944 2020-11-10 stsp goto done;
4446 fe621944 2020-11-10 stsp }
4447 fe621944 2020-11-10 stsp outoff += n;
4448 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4449 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4450 fe621944 2020-11-10 stsp if (err)
4451 fe621944 2020-11-10 stsp goto done;
4452 abd2672a 2018-12-23 stsp }
4453 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4454 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4455 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4456 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4457 fe621944 2020-11-10 stsp if (n < 0) {
4458 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4459 fe621944 2020-11-10 stsp goto done;
4460 fe621944 2020-11-10 stsp }
4461 fe621944 2020-11-10 stsp outoff += n;
4462 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4463 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4464 fe621944 2020-11-10 stsp if (err)
4465 fe621944 2020-11-10 stsp goto done;
4466 abd2672a 2018-12-23 stsp }
4467 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4468 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4469 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4470 ac4dc263 2021-09-24 thomas int pn = 1;
4471 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4472 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4473 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4474 ac4dc263 2021-09-24 thomas if (err)
4475 ac4dc263 2021-09-24 thomas goto done;
4476 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4477 ac4dc263 2021-09-24 thomas if (n < 0) {
4478 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4479 ac4dc263 2021-09-24 thomas goto done;
4480 ac4dc263 2021-09-24 thomas }
4481 ac4dc263 2021-09-24 thomas outoff += n;
4482 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4483 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4484 ac4dc263 2021-09-24 thomas if (err)
4485 ac4dc263 2021-09-24 thomas goto done;
4486 ac4dc263 2021-09-24 thomas free(id_str);
4487 ac4dc263 2021-09-24 thomas id_str = NULL;
4488 ac4dc263 2021-09-24 thomas }
4489 ac4dc263 2021-09-24 thomas }
4490 ac4dc263 2021-09-24 thomas
4491 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4492 5943eee2 2019-08-13 stsp if (err)
4493 5943eee2 2019-08-13 stsp goto done;
4494 fe621944 2020-11-10 stsp s = logmsg;
4495 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4496 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4497 fe621944 2020-11-10 stsp if (n < 0) {
4498 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4499 fe621944 2020-11-10 stsp goto done;
4500 fe621944 2020-11-10 stsp }
4501 fe621944 2020-11-10 stsp outoff += n;
4502 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4503 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4504 fe621944 2020-11-10 stsp if (err)
4505 fe621944 2020-11-10 stsp goto done;
4506 abd2672a 2018-12-23 stsp }
4507 fe621944 2020-11-10 stsp
4508 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4509 0208f208 2020-05-05 stsp if (err)
4510 0208f208 2020-05-05 stsp goto done;
4511 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4512 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4513 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4514 fe621944 2020-11-10 stsp if (n < 0) {
4515 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4516 fe621944 2020-11-10 stsp goto done;
4517 fe621944 2020-11-10 stsp }
4518 fe621944 2020-11-10 stsp outoff += n;
4519 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4520 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4521 fe621944 2020-11-10 stsp if (err)
4522 fe621944 2020-11-10 stsp goto done;
4523 0208f208 2020-05-05 stsp free((char *)pe->path);
4524 0208f208 2020-05-05 stsp free(pe->data);
4525 0208f208 2020-05-05 stsp }
4526 fe621944 2020-11-10 stsp
4527 0208f208 2020-05-05 stsp fputc('\n', outfile);
4528 fe621944 2020-11-10 stsp outoff++;
4529 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4530 abd2672a 2018-12-23 stsp done:
4531 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4532 abd2672a 2018-12-23 stsp free(id_str);
4533 5943eee2 2019-08-13 stsp free(logmsg);
4534 8b473291 2019-02-21 stsp free(refs_str);
4535 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4536 7510f233 2020-08-09 stsp if (err) {
4537 82c78e96 2022-08-06 thomas free(*lines);
4538 82c78e96 2022-08-06 thomas *lines = NULL;
4539 ae6a6978 2020-08-09 stsp *nlines = 0;
4540 7510f233 2020-08-09 stsp }
4541 fe621944 2020-11-10 stsp return err;
4542 abd2672a 2018-12-23 stsp }
4543 abd2672a 2018-12-23 stsp
4544 abd2672a 2018-12-23 stsp static const struct got_error *
4545 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4546 26ed57b2 2018-05-19 stsp {
4547 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4548 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4549 15a94983 2018-12-23 stsp int obj_type;
4550 fe621944 2020-11-10 stsp
4551 82c78e96 2022-08-06 thomas free(s->lines);
4552 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4553 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4554 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4555 fe621944 2020-11-10 stsp s->nlines = 0;
4556 26ed57b2 2018-05-19 stsp
4557 511a516b 2018-05-19 stsp f = got_opentemp();
4558 48ae06ee 2018-10-18 stsp if (f == NULL) {
4559 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4560 48ae06ee 2018-10-18 stsp goto done;
4561 48ae06ee 2018-10-18 stsp }
4562 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4563 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4564 fb43ecf1 2019-02-11 stsp goto done;
4565 fb43ecf1 2019-02-11 stsp }
4566 48ae06ee 2018-10-18 stsp s->f = f;
4567 26ed57b2 2018-05-19 stsp
4568 15a94983 2018-12-23 stsp if (s->id1)
4569 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4570 15a94983 2018-12-23 stsp else
4571 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4572 15a94983 2018-12-23 stsp if (err)
4573 15a94983 2018-12-23 stsp goto done;
4574 15a94983 2018-12-23 stsp
4575 15a94983 2018-12-23 stsp switch (obj_type) {
4576 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4577 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4578 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4579 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4580 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4581 26ed57b2 2018-05-19 stsp break;
4582 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4583 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4584 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4585 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4586 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4587 26ed57b2 2018-05-19 stsp break;
4588 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4589 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4590 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4591 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4592 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4593 abd2672a 2018-12-23 stsp
4594 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4595 abd2672a 2018-12-23 stsp if (err)
4596 3ffacbe1 2020-02-02 tracey goto done;
4597 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4598 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4599 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4600 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4601 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4602 f44b1f58 2020-02-02 tracey if (err)
4603 f44b1f58 2020-02-02 tracey goto done;
4604 f44b1f58 2020-02-02 tracey } else {
4605 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4606 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4607 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4608 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4609 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4610 82c78e96 2022-08-06 thomas s->f);
4611 f44b1f58 2020-02-02 tracey if (err)
4612 f44b1f58 2020-02-02 tracey goto done;
4613 f5404e4e 2020-02-02 tracey break;
4614 15a087fe 2019-02-21 stsp }
4615 abd2672a 2018-12-23 stsp }
4616 abd2672a 2018-12-23 stsp }
4617 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4618 abd2672a 2018-12-23 stsp
4619 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4620 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4621 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4622 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4623 26ed57b2 2018-05-19 stsp break;
4624 abd2672a 2018-12-23 stsp }
4625 26ed57b2 2018-05-19 stsp default:
4626 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4627 48ae06ee 2018-10-18 stsp break;
4628 26ed57b2 2018-05-19 stsp }
4629 48ae06ee 2018-10-18 stsp done:
4630 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4631 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4632 48ae06ee 2018-10-18 stsp return err;
4633 48ae06ee 2018-10-18 stsp }
4634 26ed57b2 2018-05-19 stsp
4635 f5215bb9 2019-02-22 stsp static void
4636 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4637 f5215bb9 2019-02-22 stsp {
4638 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4639 f5215bb9 2019-02-22 stsp update_panels();
4640 f5215bb9 2019-02-22 stsp doupdate();
4641 f44b1f58 2020-02-02 tracey }
4642 f44b1f58 2020-02-02 tracey
4643 f44b1f58 2020-02-02 tracey static const struct got_error *
4644 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4645 f44b1f58 2020-02-02 tracey {
4646 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4647 f44b1f58 2020-02-02 tracey
4648 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4649 f44b1f58 2020-02-02 tracey return NULL;
4650 f44b1f58 2020-02-02 tracey }
4651 f44b1f58 2020-02-02 tracey
4652 f44b1f58 2020-02-02 tracey static const struct got_error *
4653 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4654 f44b1f58 2020-02-02 tracey {
4655 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4656 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4657 f44b1f58 2020-02-02 tracey int lineno;
4658 78eecffc 2022-06-23 thomas char *line = NULL;
4659 826082fe 2020-12-10 stsp size_t linesize = 0;
4660 826082fe 2020-12-10 stsp ssize_t linelen;
4661 f44b1f58 2020-02-02 tracey
4662 f44b1f58 2020-02-02 tracey if (!view->searching) {
4663 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4664 f44b1f58 2020-02-02 tracey return NULL;
4665 f44b1f58 2020-02-02 tracey }
4666 f44b1f58 2020-02-02 tracey
4667 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4668 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4669 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4670 f44b1f58 2020-02-02 tracey else
4671 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4672 4b3f9dac 2021-12-17 thomas } else
4673 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4674 f44b1f58 2020-02-02 tracey
4675 f44b1f58 2020-02-02 tracey while (1) {
4676 f44b1f58 2020-02-02 tracey off_t offset;
4677 f44b1f58 2020-02-02 tracey
4678 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4679 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4680 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4681 f44b1f58 2020-02-02 tracey break;
4682 f44b1f58 2020-02-02 tracey }
4683 f44b1f58 2020-02-02 tracey
4684 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4685 f44b1f58 2020-02-02 tracey lineno = 1;
4686 f44b1f58 2020-02-02 tracey else
4687 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4688 f44b1f58 2020-02-02 tracey }
4689 f44b1f58 2020-02-02 tracey
4690 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4691 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4692 f44b1f58 2020-02-02 tracey free(line);
4693 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4694 f44b1f58 2020-02-02 tracey }
4695 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4696 78eecffc 2022-06-23 thomas if (linelen != -1) {
4697 78eecffc 2022-06-23 thomas char *exstr;
4698 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4699 78eecffc 2022-06-23 thomas if (err)
4700 78eecffc 2022-06-23 thomas break;
4701 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4702 78eecffc 2022-06-23 thomas &view->regmatch)) {
4703 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4704 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4705 78eecffc 2022-06-23 thomas free(exstr);
4706 78eecffc 2022-06-23 thomas break;
4707 78eecffc 2022-06-23 thomas }
4708 78eecffc 2022-06-23 thomas free(exstr);
4709 f44b1f58 2020-02-02 tracey }
4710 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4711 f44b1f58 2020-02-02 tracey lineno++;
4712 f44b1f58 2020-02-02 tracey else
4713 f44b1f58 2020-02-02 tracey lineno--;
4714 f44b1f58 2020-02-02 tracey }
4715 826082fe 2020-12-10 stsp free(line);
4716 f44b1f58 2020-02-02 tracey
4717 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4718 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4719 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4720 f44b1f58 2020-02-02 tracey }
4721 f44b1f58 2020-02-02 tracey
4722 05171be4 2022-06-23 thomas return err;
4723 f5215bb9 2019-02-22 stsp }
4724 f5215bb9 2019-02-22 stsp
4725 48ae06ee 2018-10-18 stsp static const struct got_error *
4726 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4727 a0f32f33 2022-06-13 thomas {
4728 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4729 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4730 a0f32f33 2022-06-13 thomas
4731 a0f32f33 2022-06-13 thomas free(s->id1);
4732 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4733 a0f32f33 2022-06-13 thomas free(s->id2);
4734 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4735 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4736 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4737 a0f32f33 2022-06-13 thomas s->f = NULL;
4738 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4739 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4740 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4741 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4742 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4743 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4744 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4745 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4746 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4747 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4748 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4749 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4750 82c78e96 2022-08-06 thomas free(s->lines);
4751 82c78e96 2022-08-06 thomas s->lines = NULL;
4752 a0f32f33 2022-06-13 thomas s->nlines = 0;
4753 a0f32f33 2022-06-13 thomas return err;
4754 a0f32f33 2022-06-13 thomas }
4755 a0f32f33 2022-06-13 thomas
4756 a0f32f33 2022-06-13 thomas static const struct got_error *
4757 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4758 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4759 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4760 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4761 48ae06ee 2018-10-18 stsp {
4762 48ae06ee 2018-10-18 stsp const struct got_error *err;
4763 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4764 5dc9f4bc 2018-08-04 stsp
4765 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4766 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4767 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4768 a0f32f33 2022-06-13 thomas
4769 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4770 15a94983 2018-12-23 stsp int type1, type2;
4771 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4772 15a94983 2018-12-23 stsp if (err)
4773 15a94983 2018-12-23 stsp return err;
4774 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4775 15a94983 2018-12-23 stsp if (err)
4776 15a94983 2018-12-23 stsp return err;
4777 15a94983 2018-12-23 stsp
4778 15a94983 2018-12-23 stsp if (type1 != type2)
4779 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4780 15a94983 2018-12-23 stsp }
4781 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4782 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4783 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4784 f44b1f58 2020-02-02 tracey s->repo = repo;
4785 f44b1f58 2020-02-02 tracey s->id1 = id1;
4786 f44b1f58 2020-02-02 tracey s->id2 = id2;
4787 3dbaef42 2020-11-24 stsp s->label1 = label1;
4788 3dbaef42 2020-11-24 stsp s->label2 = label2;
4789 48ae06ee 2018-10-18 stsp
4790 15a94983 2018-12-23 stsp if (id1) {
4791 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4792 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4793 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4794 48ae06ee 2018-10-18 stsp } else
4795 5465d566 2020-02-01 tracey s->id1 = NULL;
4796 48ae06ee 2018-10-18 stsp
4797 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4798 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4799 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4800 a0f32f33 2022-06-13 thomas goto done;
4801 48ae06ee 2018-10-18 stsp }
4802 a0f32f33 2022-06-13 thomas
4803 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4804 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4805 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4806 9a1d7435 2022-06-23 thomas goto done;
4807 9a1d7435 2022-06-23 thomas }
4808 9a1d7435 2022-06-23 thomas
4809 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4810 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4811 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4812 a0f32f33 2022-06-13 thomas goto done;
4813 a0f32f33 2022-06-13 thomas }
4814 a0f32f33 2022-06-13 thomas
4815 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4816 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4817 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4818 19a6a6b5 2022-07-01 thomas goto done;
4819 19a6a6b5 2022-07-01 thomas }
4820 19a6a6b5 2022-07-01 thomas
4821 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4822 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4823 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4824 19a6a6b5 2022-07-01 thomas goto done;
4825 19a6a6b5 2022-07-01 thomas }
4826 19a6a6b5 2022-07-01 thomas
4827 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4828 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4829 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4830 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4831 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4832 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4833 5465d566 2020-02-01 tracey s->repo = repo;
4834 6d17833f 2019-11-08 stsp
4835 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4836 6568f0aa 2022-08-06 thomas int rc;
4837 6d17833f 2019-11-08 stsp
4838 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4839 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4840 6568f0aa 2022-08-06 thomas if (rc != ERR)
4841 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4842 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4843 6568f0aa 2022-08-06 thomas if (rc != ERR)
4844 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4845 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4846 6568f0aa 2022-08-06 thomas if (rc != ERR)
4847 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4848 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4849 6568f0aa 2022-08-06 thomas if (rc != ERR)
4850 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4851 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4852 6568f0aa 2022-08-06 thomas if (rc != ERR)
4853 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4854 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4855 6568f0aa 2022-08-06 thomas if (rc != ERR)
4856 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4857 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4858 6568f0aa 2022-08-06 thomas if (rc != ERR)
4859 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4860 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4861 6568f0aa 2022-08-06 thomas if (rc != ERR)
4862 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4863 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4864 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4865 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4866 a0f32f33 2022-06-13 thomas goto done;
4867 6568f0aa 2022-08-06 thomas }
4868 6d17833f 2019-11-08 stsp }
4869 5dc9f4bc 2018-08-04 stsp
4870 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4871 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4872 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4873 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4874 f5215bb9 2019-02-22 stsp
4875 5465d566 2020-02-01 tracey err = create_diff(s);
4876 48ae06ee 2018-10-18 stsp
4877 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4878 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4879 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4880 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4881 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4882 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4883 a0f32f33 2022-06-13 thomas done:
4884 a0f32f33 2022-06-13 thomas if (err)
4885 a0f32f33 2022-06-13 thomas close_diff_view(view);
4886 e5a0f69f 2018-08-18 stsp return err;
4887 5dc9f4bc 2018-08-04 stsp }
4888 5dc9f4bc 2018-08-04 stsp
4889 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4890 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4891 5dc9f4bc 2018-08-04 stsp {
4892 a3404814 2018-09-02 stsp const struct got_error *err;
4893 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4894 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4895 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4896 a3404814 2018-09-02 stsp
4897 a3404814 2018-09-02 stsp if (s->id1) {
4898 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4899 a3404814 2018-09-02 stsp if (err)
4900 a3404814 2018-09-02 stsp return err;
4901 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4902 3dbaef42 2020-11-24 stsp } else
4903 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4904 3dbaef42 2020-11-24 stsp
4905 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4906 a3404814 2018-09-02 stsp if (err)
4907 a3404814 2018-09-02 stsp return err;
4908 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4909 26ed57b2 2018-05-19 stsp
4910 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4911 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4912 a3404814 2018-09-02 stsp free(id_str1);
4913 a3404814 2018-09-02 stsp free(id_str2);
4914 a3404814 2018-09-02 stsp return err;
4915 a3404814 2018-09-02 stsp }
4916 a3404814 2018-09-02 stsp free(id_str1);
4917 a3404814 2018-09-02 stsp free(id_str2);
4918 a3404814 2018-09-02 stsp
4919 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4920 267bb3b8 2021-08-01 stsp free(header);
4921 267bb3b8 2021-08-01 stsp return err;
4922 15a087fe 2019-02-21 stsp }
4923 15a087fe 2019-02-21 stsp
4924 15a087fe 2019-02-21 stsp static const struct got_error *
4925 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4926 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4927 15a087fe 2019-02-21 stsp {
4928 d7a04538 2019-02-21 stsp const struct got_error *err;
4929 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4930 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4931 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4932 15a087fe 2019-02-21 stsp
4933 15a087fe 2019-02-21 stsp free(s->id2);
4934 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4935 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4936 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4937 15a087fe 2019-02-21 stsp
4938 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4939 d7a04538 2019-02-21 stsp if (err)
4940 d7a04538 2019-02-21 stsp return err;
4941 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4942 15a087fe 2019-02-21 stsp free(s->id1);
4943 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4944 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4945 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4946 15a087fe 2019-02-21 stsp return NULL;
4947 0cf4efb1 2018-09-29 stsp }
4948 0cf4efb1 2018-09-29 stsp
4949 0cf4efb1 2018-09-29 stsp static const struct got_error *
4950 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4951 adf4c9e0 2022-07-03 thomas {
4952 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4953 adf4c9e0 2022-07-03 thomas
4954 adf4c9e0 2022-07-03 thomas view->count = 0;
4955 adf4c9e0 2022-07-03 thomas wclear(view->window);
4956 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4957 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4958 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4959 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4960 adf4c9e0 2022-07-03 thomas return create_diff(s);
4961 82c78e96 2022-08-06 thomas }
4962 82c78e96 2022-08-06 thomas
4963 82c78e96 2022-08-06 thomas static void
4964 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4965 82c78e96 2022-08-06 thomas {
4966 82c78e96 2022-08-06 thomas int start, i;
4967 82c78e96 2022-08-06 thomas
4968 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4969 82c78e96 2022-08-06 thomas
4970 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4971 82c78e96 2022-08-06 thomas if (i == 0)
4972 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4973 82c78e96 2022-08-06 thomas if (--i == start)
4974 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4975 82c78e96 2022-08-06 thomas }
4976 82c78e96 2022-08-06 thomas
4977 82c78e96 2022-08-06 thomas s->selected_line = 1;
4978 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4979 adf4c9e0 2022-07-03 thomas }
4980 adf4c9e0 2022-07-03 thomas
4981 82c78e96 2022-08-06 thomas static void
4982 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4983 82c78e96 2022-08-06 thomas {
4984 82c78e96 2022-08-06 thomas int start, i;
4985 82c78e96 2022-08-06 thomas
4986 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4987 82c78e96 2022-08-06 thomas
4988 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4989 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
4990 82c78e96 2022-08-06 thomas i = 0;
4991 82c78e96 2022-08-06 thomas if (++i == start)
4992 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4993 82c78e96 2022-08-06 thomas }
4994 82c78e96 2022-08-06 thomas
4995 82c78e96 2022-08-06 thomas s->selected_line = 1;
4996 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4997 82c78e96 2022-08-06 thomas }
4998 82c78e96 2022-08-06 thomas
4999 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5000 4fc71f3b 2022-07-12 thomas int, int, int);
5001 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5002 4fc71f3b 2022-07-12 thomas int, int);
5003 4fc71f3b 2022-07-12 thomas
5004 adf4c9e0 2022-07-03 thomas static const struct got_error *
5005 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5006 e5a0f69f 2018-08-18 stsp {
5007 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5008 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5009 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5010 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5011 826082fe 2020-12-10 stsp char *line = NULL;
5012 826082fe 2020-12-10 stsp size_t linesize = 0;
5013 826082fe 2020-12-10 stsp ssize_t linelen;
5014 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5015 e5a0f69f 2018-08-18 stsp
5016 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5017 82c78e96 2022-08-06 thomas
5018 e5a0f69f 2018-08-18 stsp switch (ch) {
5019 05171be4 2022-06-23 thomas case '0':
5020 05171be4 2022-06-23 thomas view->x = 0;
5021 05171be4 2022-06-23 thomas break;
5022 05171be4 2022-06-23 thomas case '$':
5023 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5024 07b0611c 2022-06-23 thomas view->count = 0;
5025 05171be4 2022-06-23 thomas break;
5026 05171be4 2022-06-23 thomas case KEY_RIGHT:
5027 05171be4 2022-06-23 thomas case 'l':
5028 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5029 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5030 07b0611c 2022-06-23 thomas else
5031 07b0611c 2022-06-23 thomas view->count = 0;
5032 05171be4 2022-06-23 thomas break;
5033 05171be4 2022-06-23 thomas case KEY_LEFT:
5034 05171be4 2022-06-23 thomas case 'h':
5035 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5036 07b0611c 2022-06-23 thomas if (view->x <= 0)
5037 07b0611c 2022-06-23 thomas view->count = 0;
5038 05171be4 2022-06-23 thomas break;
5039 64453f7e 2020-11-21 stsp case 'a':
5040 3dbaef42 2020-11-24 stsp case 'w':
5041 3dbaef42 2020-11-24 stsp if (ch == 'a')
5042 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5043 3dbaef42 2020-11-24 stsp if (ch == 'w')
5044 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5045 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5046 912a3f79 2021-08-30 j break;
5047 912a3f79 2021-08-30 j case 'g':
5048 912a3f79 2021-08-30 j case KEY_HOME:
5049 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5050 07b0611c 2022-06-23 thomas view->count = 0;
5051 64453f7e 2020-11-21 stsp break;
5052 912a3f79 2021-08-30 j case 'G':
5053 912a3f79 2021-08-30 j case KEY_END:
5054 07b0611c 2022-06-23 thomas view->count = 0;
5055 912a3f79 2021-08-30 j if (s->eof)
5056 912a3f79 2021-08-30 j break;
5057 912a3f79 2021-08-30 j
5058 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5059 912a3f79 2021-08-30 j s->eof = 1;
5060 912a3f79 2021-08-30 j break;
5061 1e37a5c2 2019-05-12 jcs case 'k':
5062 1e37a5c2 2019-05-12 jcs case KEY_UP:
5063 f7140bf5 2021-10-17 thomas case CTRL('p'):
5064 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5065 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5066 07b0611c 2022-06-23 thomas else
5067 07b0611c 2022-06-23 thomas view->count = 0;
5068 1e37a5c2 2019-05-12 jcs break;
5069 70f17a53 2022-06-13 thomas case CTRL('u'):
5070 23427b14 2022-06-23 thomas case 'u':
5071 70f17a53 2022-06-13 thomas nscroll /= 2;
5072 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5073 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5074 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5075 1c5e5faa 2022-06-23 thomas case 'b':
5076 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5077 07b0611c 2022-06-23 thomas view->count = 0;
5078 26ed57b2 2018-05-19 stsp break;
5079 07b0611c 2022-06-23 thomas }
5080 1e37a5c2 2019-05-12 jcs i = 0;
5081 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5082 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5083 1e37a5c2 2019-05-12 jcs break;
5084 1e37a5c2 2019-05-12 jcs case 'j':
5085 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5086 f7140bf5 2021-10-17 thomas case CTRL('n'):
5087 1e37a5c2 2019-05-12 jcs if (!s->eof)
5088 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5089 07b0611c 2022-06-23 thomas else
5090 07b0611c 2022-06-23 thomas view->count = 0;
5091 1e37a5c2 2019-05-12 jcs break;
5092 70f17a53 2022-06-13 thomas case CTRL('d'):
5093 23427b14 2022-06-23 thomas case 'd':
5094 70f17a53 2022-06-13 thomas nscroll /= 2;
5095 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5096 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5097 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5098 1c5e5faa 2022-06-23 thomas case 'f':
5099 1e37a5c2 2019-05-12 jcs case ' ':
5100 07b0611c 2022-06-23 thomas if (s->eof) {
5101 07b0611c 2022-06-23 thomas view->count = 0;
5102 1e37a5c2 2019-05-12 jcs break;
5103 07b0611c 2022-06-23 thomas }
5104 1e37a5c2 2019-05-12 jcs i = 0;
5105 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5106 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5107 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5108 826082fe 2020-12-10 stsp if (linelen == -1) {
5109 826082fe 2020-12-10 stsp if (feof(s->f)) {
5110 826082fe 2020-12-10 stsp s->eof = 1;
5111 826082fe 2020-12-10 stsp } else
5112 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5113 34bc9ec9 2019-02-22 stsp break;
5114 826082fe 2020-12-10 stsp }
5115 1e37a5c2 2019-05-12 jcs }
5116 826082fe 2020-12-10 stsp free(line);
5117 1e37a5c2 2019-05-12 jcs break;
5118 82c78e96 2022-08-06 thomas case '(':
5119 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5120 82c78e96 2022-08-06 thomas break;
5121 82c78e96 2022-08-06 thomas case ')':
5122 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5123 82c78e96 2022-08-06 thomas break;
5124 82c78e96 2022-08-06 thomas case '{':
5125 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5126 82c78e96 2022-08-06 thomas break;
5127 82c78e96 2022-08-06 thomas case '}':
5128 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5129 82c78e96 2022-08-06 thomas break;
5130 1e37a5c2 2019-05-12 jcs case '[':
5131 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5132 1e37a5c2 2019-05-12 jcs s->diff_context--;
5133 aa61903a 2021-12-31 thomas s->matched_line = 0;
5134 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5135 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5136 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5137 27829c9e 2020-11-21 stsp s->nlines) {
5138 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5139 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5140 27829c9e 2020-11-21 stsp }
5141 07b0611c 2022-06-23 thomas } else
5142 07b0611c 2022-06-23 thomas view->count = 0;
5143 1e37a5c2 2019-05-12 jcs break;
5144 1e37a5c2 2019-05-12 jcs case ']':
5145 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5146 1e37a5c2 2019-05-12 jcs s->diff_context++;
5147 aa61903a 2021-12-31 thomas s->matched_line = 0;
5148 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5149 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5150 07b0611c 2022-06-23 thomas } else
5151 07b0611c 2022-06-23 thomas view->count = 0;
5152 1e37a5c2 2019-05-12 jcs break;
5153 1e37a5c2 2019-05-12 jcs case '<':
5154 1e37a5c2 2019-05-12 jcs case ',':
5155 777aae21 2022-07-20 thomas case 'K':
5156 4fc71f3b 2022-07-12 thomas up = 1;
5157 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5158 4fc71f3b 2022-07-12 thomas case '>':
5159 4fc71f3b 2022-07-12 thomas case '.':
5160 777aae21 2022-07-20 thomas case 'J':
5161 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5162 07b0611c 2022-06-23 thomas view->count = 0;
5163 48ae06ee 2018-10-18 stsp break;
5164 07b0611c 2022-06-23 thomas }
5165 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5166 6524637e 2019-02-21 stsp
5167 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5168 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5169 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5170 15a087fe 2019-02-21 stsp
5171 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5172 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5173 4fc71f3b 2022-07-12 thomas if (err)
5174 4fc71f3b 2022-07-12 thomas break;
5175 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5176 15a087fe 2019-02-21 stsp
5177 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5178 4fc71f3b 2022-07-12 thomas break;
5179 15a087fe 2019-02-21 stsp
5180 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5181 4fc71f3b 2022-07-12 thomas if (err)
5182 4fc71f3b 2022-07-12 thomas break;
5183 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5184 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5185 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5186 5e224a3e 2019-02-22 stsp
5187 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5188 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5189 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5190 4fc71f3b 2022-07-12 thomas
5191 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5192 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5193 4fc71f3b 2022-07-12 thomas if (err)
5194 4fc71f3b 2022-07-12 thomas break;
5195 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5196 5a8b5076 2020-12-05 stsp
5197 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5198 4fc71f3b 2022-07-12 thomas break;
5199 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5200 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5201 4fc71f3b 2022-07-12 thomas bs->selected_line);
5202 4fc71f3b 2022-07-12 thomas if (id == NULL)
5203 4fc71f3b 2022-07-12 thomas break;
5204 15a087fe 2019-02-21 stsp
5205 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5206 4fc71f3b 2022-07-12 thomas break;
5207 15a087fe 2019-02-21 stsp
5208 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5209 4fc71f3b 2022-07-12 thomas if (err)
5210 4fc71f3b 2022-07-12 thomas break;
5211 4fc71f3b 2022-07-12 thomas }
5212 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5213 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5214 aa61903a 2021-12-31 thomas s->matched_line = 0;
5215 05171be4 2022-06-23 thomas view->x = 0;
5216 1e37a5c2 2019-05-12 jcs
5217 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5218 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5219 1e37a5c2 2019-05-12 jcs break;
5220 1e37a5c2 2019-05-12 jcs default:
5221 07b0611c 2022-06-23 thomas view->count = 0;
5222 1e37a5c2 2019-05-12 jcs break;
5223 26ed57b2 2018-05-19 stsp }
5224 e5a0f69f 2018-08-18 stsp
5225 bcbd79e2 2018-08-19 stsp return err;
5226 26ed57b2 2018-05-19 stsp }
5227 26ed57b2 2018-05-19 stsp
5228 4ed7e80c 2018-05-20 stsp static const struct got_error *
5229 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5230 9f7d7167 2018-04-29 stsp {
5231 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5232 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5233 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5234 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5235 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5236 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5237 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5238 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5239 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5240 3dbaef42 2020-11-24 stsp const char *errstr;
5241 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5242 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5243 70ac5f84 2019-03-28 stsp
5244 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5245 26ed57b2 2018-05-19 stsp switch (ch) {
5246 64453f7e 2020-11-21 stsp case 'a':
5247 64453f7e 2020-11-21 stsp force_text_diff = 1;
5248 3dbaef42 2020-11-24 stsp break;
5249 3dbaef42 2020-11-24 stsp case 'C':
5250 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5251 3dbaef42 2020-11-24 stsp &errstr);
5252 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5253 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5254 8f666e67 2022-02-12 thomas errstr, errstr);
5255 64453f7e 2020-11-21 stsp break;
5256 09b5bff8 2020-02-23 naddy case 'r':
5257 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5258 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5259 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5260 09b5bff8 2020-02-23 naddy optarg);
5261 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5262 09b5bff8 2020-02-23 naddy break;
5263 3dbaef42 2020-11-24 stsp case 'w':
5264 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5265 3dbaef42 2020-11-24 stsp break;
5266 26ed57b2 2018-05-19 stsp default:
5267 17020d27 2019-03-07 stsp usage_diff();
5268 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5269 26ed57b2 2018-05-19 stsp }
5270 26ed57b2 2018-05-19 stsp }
5271 26ed57b2 2018-05-19 stsp
5272 26ed57b2 2018-05-19 stsp argc -= optind;
5273 26ed57b2 2018-05-19 stsp argv += optind;
5274 26ed57b2 2018-05-19 stsp
5275 26ed57b2 2018-05-19 stsp if (argc == 0) {
5276 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5277 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5278 15a94983 2018-12-23 stsp id_str1 = argv[0];
5279 15a94983 2018-12-23 stsp id_str2 = argv[1];
5280 26ed57b2 2018-05-19 stsp } else
5281 26ed57b2 2018-05-19 stsp usage_diff();
5282 eb6600df 2019-01-04 stsp
5283 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5284 7cd52833 2022-06-23 thomas if (error)
5285 7cd52833 2022-06-23 thomas goto done;
5286 7cd52833 2022-06-23 thomas
5287 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5288 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5289 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5290 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5291 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5292 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5293 c156c7a4 2020-12-18 stsp goto done;
5294 a273ac94 2020-02-23 naddy if (worktree)
5295 a273ac94 2020-02-23 naddy repo_path =
5296 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5297 a273ac94 2020-02-23 naddy else
5298 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5299 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5300 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5301 c156c7a4 2020-12-18 stsp goto done;
5302 c156c7a4 2020-12-18 stsp }
5303 a273ac94 2020-02-23 naddy }
5304 a273ac94 2020-02-23 naddy
5305 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5306 eb6600df 2019-01-04 stsp if (error)
5307 eb6600df 2019-01-04 stsp goto done;
5308 26ed57b2 2018-05-19 stsp
5309 a273ac94 2020-02-23 naddy init_curses();
5310 a273ac94 2020-02-23 naddy
5311 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5312 51a10b52 2020-12-26 stsp if (error)
5313 51a10b52 2020-12-26 stsp goto done;
5314 51a10b52 2020-12-26 stsp
5315 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5316 26ed57b2 2018-05-19 stsp if (error)
5317 26ed57b2 2018-05-19 stsp goto done;
5318 26ed57b2 2018-05-19 stsp
5319 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5320 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5321 26ed57b2 2018-05-19 stsp if (error)
5322 26ed57b2 2018-05-19 stsp goto done;
5323 26ed57b2 2018-05-19 stsp
5324 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5325 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5326 26ed57b2 2018-05-19 stsp if (error)
5327 26ed57b2 2018-05-19 stsp goto done;
5328 26ed57b2 2018-05-19 stsp
5329 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5330 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5331 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5332 ea5e7bb5 2018-08-01 stsp goto done;
5333 ea5e7bb5 2018-08-01 stsp }
5334 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5335 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5336 5dc9f4bc 2018-08-04 stsp if (error)
5337 5dc9f4bc 2018-08-04 stsp goto done;
5338 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5339 26ed57b2 2018-05-19 stsp done:
5340 3dbaef42 2020-11-24 stsp free(label1);
5341 3dbaef42 2020-11-24 stsp free(label2);
5342 c02c541e 2019-03-29 stsp free(repo_path);
5343 a273ac94 2020-02-23 naddy free(cwd);
5344 1d0f4054 2021-06-17 stsp if (repo) {
5345 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5346 1d0f4054 2021-06-17 stsp if (error == NULL)
5347 1d0f4054 2021-06-17 stsp error = close_err;
5348 1d0f4054 2021-06-17 stsp }
5349 a273ac94 2020-02-23 naddy if (worktree)
5350 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5351 7cd52833 2022-06-23 thomas if (pack_fds) {
5352 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5353 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5354 7cd52833 2022-06-23 thomas if (error == NULL)
5355 7cd52833 2022-06-23 thomas error = pack_err;
5356 7cd52833 2022-06-23 thomas }
5357 51a10b52 2020-12-26 stsp tog_free_refs();
5358 26ed57b2 2018-05-19 stsp return error;
5359 9f7d7167 2018-04-29 stsp }
5360 9f7d7167 2018-04-29 stsp
5361 4ed7e80c 2018-05-20 stsp __dead static void
5362 9f7d7167 2018-04-29 stsp usage_blame(void)
5363 9f7d7167 2018-04-29 stsp {
5364 80ddbec8 2018-04-29 stsp endwin();
5365 91198554 2022-06-23 thomas fprintf(stderr,
5366 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5367 9f7d7167 2018-04-29 stsp getprogname());
5368 9f7d7167 2018-04-29 stsp exit(1);
5369 9f7d7167 2018-04-29 stsp }
5370 84451b3e 2018-07-10 stsp
5371 84451b3e 2018-07-10 stsp struct tog_blame_line {
5372 84451b3e 2018-07-10 stsp int annotated;
5373 84451b3e 2018-07-10 stsp struct got_object_id *id;
5374 84451b3e 2018-07-10 stsp };
5375 9f7d7167 2018-04-29 stsp
5376 4ed7e80c 2018-05-20 stsp static const struct got_error *
5377 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5378 84451b3e 2018-07-10 stsp {
5379 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5380 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5381 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5382 84451b3e 2018-07-10 stsp const struct got_error *err;
5383 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5384 826082fe 2020-12-10 stsp char *line = NULL;
5385 826082fe 2020-12-10 stsp size_t linesize = 0;
5386 826082fe 2020-12-10 stsp ssize_t linelen;
5387 84451b3e 2018-07-10 stsp wchar_t *wline;
5388 27a741e5 2019-09-11 stsp int width;
5389 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5390 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5391 ab089a2a 2018-07-12 stsp char *id_str;
5392 11b20872 2019-11-08 stsp struct tog_color *tc;
5393 ab089a2a 2018-07-12 stsp
5394 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5395 ab089a2a 2018-07-12 stsp if (err)
5396 ab089a2a 2018-07-12 stsp return err;
5397 84451b3e 2018-07-10 stsp
5398 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5399 f7d12f7e 2018-08-01 stsp werase(view->window);
5400 84451b3e 2018-07-10 stsp
5401 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5402 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5403 ab089a2a 2018-07-12 stsp free(id_str);
5404 ab089a2a 2018-07-12 stsp return err;
5405 ab089a2a 2018-07-12 stsp }
5406 ab089a2a 2018-07-12 stsp
5407 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5408 ab089a2a 2018-07-12 stsp free(line);
5409 2550e4c3 2018-07-13 stsp line = NULL;
5410 1cae65b4 2019-09-22 stsp if (err)
5411 1cae65b4 2019-09-22 stsp return err;
5412 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5413 a3404814 2018-09-02 stsp wstandout(view->window);
5414 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5415 11b20872 2019-11-08 stsp if (tc)
5416 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5417 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5418 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5419 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5420 11b20872 2019-11-08 stsp if (tc)
5421 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5422 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5423 a3404814 2018-09-02 stsp wstandend(view->window);
5424 2550e4c3 2018-07-13 stsp free(wline);
5425 2550e4c3 2018-07-13 stsp wline = NULL;
5426 ab089a2a 2018-07-12 stsp
5427 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5428 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5429 07dd3ed3 2022-08-06 thomas
5430 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5431 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5432 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5433 ab089a2a 2018-07-12 stsp free(id_str);
5434 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5435 ab089a2a 2018-07-12 stsp }
5436 ab089a2a 2018-07-12 stsp free(id_str);
5437 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5438 3f60a8ef 2018-07-10 stsp free(line);
5439 2550e4c3 2018-07-13 stsp line = NULL;
5440 3f60a8ef 2018-07-10 stsp if (err)
5441 3f60a8ef 2018-07-10 stsp return err;
5442 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5443 2550e4c3 2018-07-13 stsp free(wline);
5444 2550e4c3 2018-07-13 stsp wline = NULL;
5445 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5446 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5447 3f60a8ef 2018-07-10 stsp
5448 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5449 05171be4 2022-06-23 thomas view->maxx = 0;
5450 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5451 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5452 826082fe 2020-12-10 stsp if (linelen == -1) {
5453 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5454 826082fe 2020-12-10 stsp s->eof = 1;
5455 826082fe 2020-12-10 stsp break;
5456 826082fe 2020-12-10 stsp }
5457 84451b3e 2018-07-10 stsp free(line);
5458 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5459 84451b3e 2018-07-10 stsp }
5460 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5461 07dd3ed3 2022-08-06 thomas continue;
5462 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5463 826082fe 2020-12-10 stsp continue;
5464 cbea3800 2022-06-23 thomas
5465 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5466 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5467 cbea3800 2022-06-23 thomas if (err) {
5468 cbea3800 2022-06-23 thomas free(line);
5469 cbea3800 2022-06-23 thomas return err;
5470 cbea3800 2022-06-23 thomas }
5471 cbea3800 2022-06-23 thomas free(wline);
5472 cbea3800 2022-06-23 thomas wline = NULL;
5473 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5474 84451b3e 2018-07-10 stsp
5475 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5476 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5477 b700b5d6 2018-07-10 stsp
5478 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5479 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5480 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5481 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5482 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5483 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5484 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5485 8d0fe45a 2019-08-12 stsp char *id_str;
5486 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5487 91198554 2022-06-23 thomas blame_line->id);
5488 8d0fe45a 2019-08-12 stsp if (err) {
5489 8d0fe45a 2019-08-12 stsp free(line);
5490 8d0fe45a 2019-08-12 stsp return err;
5491 8d0fe45a 2019-08-12 stsp }
5492 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5493 11b20872 2019-11-08 stsp if (tc)
5494 11b20872 2019-11-08 stsp wattr_on(view->window,
5495 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5496 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5497 11b20872 2019-11-08 stsp if (tc)
5498 11b20872 2019-11-08 stsp wattr_off(view->window,
5499 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5500 8d0fe45a 2019-08-12 stsp free(id_str);
5501 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5502 8d0fe45a 2019-08-12 stsp } else {
5503 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5504 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5505 84451b3e 2018-07-10 stsp }
5506 ee41ec32 2018-07-10 stsp } else {
5507 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5508 ee41ec32 2018-07-10 stsp prev_id = NULL;
5509 ee41ec32 2018-07-10 stsp }
5510 84451b3e 2018-07-10 stsp
5511 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5512 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5513 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5514 27a741e5 2019-09-11 stsp
5515 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5516 41605754 2020-11-12 stsp width = 9;
5517 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5518 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5519 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5520 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5521 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5522 41605754 2020-11-12 stsp if (err) {
5523 41605754 2020-11-12 stsp free(line);
5524 41605754 2020-11-12 stsp return err;
5525 41605754 2020-11-12 stsp }
5526 41605754 2020-11-12 stsp width += 9;
5527 41605754 2020-11-12 stsp } else {
5528 923086fd 2022-06-23 thomas int skip;
5529 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5530 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5531 923086fd 2022-06-23 thomas if (err) {
5532 923086fd 2022-06-23 thomas free(line);
5533 923086fd 2022-06-23 thomas return err;
5534 05171be4 2022-06-23 thomas }
5535 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5536 02bce7e2 2022-06-23 thomas width += 9;
5537 41605754 2020-11-12 stsp free(wline);
5538 41605754 2020-11-12 stsp wline = NULL;
5539 41605754 2020-11-12 stsp }
5540 41605754 2020-11-12 stsp
5541 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5542 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5543 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5544 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5545 84451b3e 2018-07-10 stsp }
5546 826082fe 2020-12-10 stsp free(line);
5547 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5548 84451b3e 2018-07-10 stsp
5549 a5d43cac 2022-07-01 thomas view_border(view);
5550 84451b3e 2018-07-10 stsp
5551 84451b3e 2018-07-10 stsp return NULL;
5552 84451b3e 2018-07-10 stsp }
5553 84451b3e 2018-07-10 stsp
5554 84451b3e 2018-07-10 stsp static const struct got_error *
5555 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5556 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5557 84451b3e 2018-07-10 stsp {
5558 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5559 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5560 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5561 1a76625f 2018-10-22 stsp int errcode;
5562 84451b3e 2018-07-10 stsp
5563 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5564 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5565 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5566 84451b3e 2018-07-10 stsp
5567 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5568 1a76625f 2018-10-22 stsp if (errcode)
5569 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5570 84451b3e 2018-07-10 stsp
5571 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5572 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5573 d68a0a7d 2018-07-10 stsp goto done;
5574 d68a0a7d 2018-07-10 stsp }
5575 d68a0a7d 2018-07-10 stsp
5576 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5577 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5578 d68a0a7d 2018-07-10 stsp
5579 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5580 d68a0a7d 2018-07-10 stsp if (line->annotated)
5581 d68a0a7d 2018-07-10 stsp goto done;
5582 d68a0a7d 2018-07-10 stsp
5583 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5584 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5585 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5586 84451b3e 2018-07-10 stsp goto done;
5587 84451b3e 2018-07-10 stsp }
5588 84451b3e 2018-07-10 stsp line->annotated = 1;
5589 84451b3e 2018-07-10 stsp done:
5590 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5591 1a76625f 2018-10-22 stsp if (errcode)
5592 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5593 84451b3e 2018-07-10 stsp return err;
5594 84451b3e 2018-07-10 stsp }
5595 84451b3e 2018-07-10 stsp
5596 84451b3e 2018-07-10 stsp static void *
5597 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5598 84451b3e 2018-07-10 stsp {
5599 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5600 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5601 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5602 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5603 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5604 00a8878e 2022-07-01 thomas
5605 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5606 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5607 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5608 9117a7b7 2022-07-01 thomas
5609 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5610 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5611 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5612 9117a7b7 2022-07-01 thomas goto done;
5613 9117a7b7 2022-07-01 thomas }
5614 18430de3 2018-07-10 stsp
5615 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5616 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5617 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5618 9117a7b7 2022-07-01 thomas goto done;
5619 9117a7b7 2022-07-01 thomas }
5620 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5621 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5622 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5623 9117a7b7 2022-07-01 thomas goto done;
5624 9117a7b7 2022-07-01 thomas }
5625 9117a7b7 2022-07-01 thomas
5626 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5627 61266923 2020-01-14 stsp if (err)
5628 9117a7b7 2022-07-01 thomas goto done;
5629 61266923 2020-01-14 stsp
5630 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5631 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5632 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5633 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5634 fc06ba56 2019-08-22 stsp err = NULL;
5635 18430de3 2018-07-10 stsp
5636 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5637 9117a7b7 2022-07-01 thomas if (errcode) {
5638 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5639 9117a7b7 2022-07-01 thomas goto done;
5640 9117a7b7 2022-07-01 thomas }
5641 18430de3 2018-07-10 stsp
5642 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5643 1d0f4054 2021-06-17 stsp if (err == NULL)
5644 1d0f4054 2021-06-17 stsp err = close_err;
5645 c9beca56 2018-07-22 stsp ta->repo = NULL;
5646 c9beca56 2018-07-22 stsp *ta->complete = 1;
5647 18430de3 2018-07-10 stsp
5648 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5649 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5650 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5651 18430de3 2018-07-10 stsp
5652 9117a7b7 2022-07-01 thomas done:
5653 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5654 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5655 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5656 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5657 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5658 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5659 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5660 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5661 00a8878e 2022-07-01 thomas
5662 18430de3 2018-07-10 stsp return (void *)err;
5663 84451b3e 2018-07-10 stsp }
5664 84451b3e 2018-07-10 stsp
5665 245d91c1 2018-07-12 stsp static struct got_object_id *
5666 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5667 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5668 245d91c1 2018-07-12 stsp {
5669 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5670 8d0fe45a 2019-08-12 stsp
5671 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5672 8d0fe45a 2019-08-12 stsp return NULL;
5673 b880a918 2018-07-10 stsp
5674 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5675 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5676 4fc71f3b 2022-07-12 thomas return NULL;
5677 4fc71f3b 2022-07-12 thomas
5678 4fc71f3b 2022-07-12 thomas return line->id;
5679 4fc71f3b 2022-07-12 thomas }
5680 4fc71f3b 2022-07-12 thomas
5681 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5682 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5683 4fc71f3b 2022-07-12 thomas int lineno)
5684 4fc71f3b 2022-07-12 thomas {
5685 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5686 4fc71f3b 2022-07-12 thomas
5687 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5688 4fc71f3b 2022-07-12 thomas return NULL;
5689 4fc71f3b 2022-07-12 thomas
5690 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5691 245d91c1 2018-07-12 stsp if (!line->annotated)
5692 245d91c1 2018-07-12 stsp return NULL;
5693 245d91c1 2018-07-12 stsp
5694 245d91c1 2018-07-12 stsp return line->id;
5695 b880a918 2018-07-10 stsp }
5696 245d91c1 2018-07-12 stsp
5697 b880a918 2018-07-10 stsp static const struct got_error *
5698 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5699 a70480e0 2018-06-23 stsp {
5700 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5701 245d91c1 2018-07-12 stsp int i;
5702 245d91c1 2018-07-12 stsp
5703 245d91c1 2018-07-12 stsp if (blame->thread) {
5704 1a76625f 2018-10-22 stsp int errcode;
5705 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5706 1a76625f 2018-10-22 stsp if (errcode)
5707 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5708 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5709 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5710 1a76625f 2018-10-22 stsp if (errcode)
5711 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5713 1a76625f 2018-10-22 stsp if (errcode)
5714 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5715 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5716 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5717 245d91c1 2018-07-12 stsp err = NULL;
5718 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5719 245d91c1 2018-07-12 stsp }
5720 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5721 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5722 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5723 1d0f4054 2021-06-17 stsp if (err == NULL)
5724 1d0f4054 2021-06-17 stsp err = close_err;
5725 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5726 245d91c1 2018-07-12 stsp }
5727 245d91c1 2018-07-12 stsp if (blame->f) {
5728 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5729 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5730 245d91c1 2018-07-12 stsp blame->f = NULL;
5731 245d91c1 2018-07-12 stsp }
5732 57670559 2018-12-23 stsp if (blame->lines) {
5733 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5734 57670559 2018-12-23 stsp free(blame->lines[i].id);
5735 57670559 2018-12-23 stsp free(blame->lines);
5736 57670559 2018-12-23 stsp blame->lines = NULL;
5737 57670559 2018-12-23 stsp }
5738 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5739 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5740 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5741 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5742 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5743 7cd52833 2022-06-23 thomas if (err == NULL)
5744 7cd52833 2022-06-23 thomas err = pack_err;
5745 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5746 7cd52833 2022-06-23 thomas }
5747 245d91c1 2018-07-12 stsp return err;
5748 245d91c1 2018-07-12 stsp }
5749 245d91c1 2018-07-12 stsp
5750 245d91c1 2018-07-12 stsp static const struct got_error *
5751 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5752 fc06ba56 2019-08-22 stsp {
5753 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5754 fc06ba56 2019-08-22 stsp int *done = arg;
5755 fc06ba56 2019-08-22 stsp int errcode;
5756 fc06ba56 2019-08-22 stsp
5757 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5758 fc06ba56 2019-08-22 stsp if (errcode)
5759 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5760 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5761 fc06ba56 2019-08-22 stsp
5762 fc06ba56 2019-08-22 stsp if (*done)
5763 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5764 fc06ba56 2019-08-22 stsp
5765 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5766 fc06ba56 2019-08-22 stsp if (errcode)
5767 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5768 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5769 fc06ba56 2019-08-22 stsp
5770 fc06ba56 2019-08-22 stsp return err;
5771 fc06ba56 2019-08-22 stsp }
5772 fc06ba56 2019-08-22 stsp
5773 fc06ba56 2019-08-22 stsp static const struct got_error *
5774 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5775 245d91c1 2018-07-12 stsp {
5776 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5777 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5778 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5779 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5780 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5781 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5782 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5783 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5784 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5785 a70480e0 2018-06-23 stsp
5786 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5787 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5788 27d434c2 2018-09-15 stsp if (err)
5789 15a94983 2018-12-23 stsp return err;
5790 f4ae6ddb 2022-07-01 thomas
5791 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5792 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5793 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5794 f4ae6ddb 2022-07-01 thomas goto done;
5795 f4ae6ddb 2022-07-01 thomas }
5796 945f9229 2022-04-16 thomas
5797 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5798 945f9229 2022-04-16 thomas if (err)
5799 945f9229 2022-04-16 thomas goto done;
5800 27d434c2 2018-09-15 stsp
5801 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5802 84451b3e 2018-07-10 stsp if (err)
5803 84451b3e 2018-07-10 stsp goto done;
5804 27d434c2 2018-09-15 stsp
5805 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5806 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5807 84451b3e 2018-07-10 stsp goto done;
5808 84451b3e 2018-07-10 stsp }
5809 a70480e0 2018-06-23 stsp
5810 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5811 a70480e0 2018-06-23 stsp if (err)
5812 a70480e0 2018-06-23 stsp goto done;
5813 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5814 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5815 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5816 84451b3e 2018-07-10 stsp goto done;
5817 84451b3e 2018-07-10 stsp }
5818 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5819 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5820 1fddf795 2021-01-20 stsp if (err)
5821 1fddf795 2021-01-20 stsp goto done;
5822 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5823 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5824 84451b3e 2018-07-10 stsp goto done;
5825 1fddf795 2021-01-20 stsp }
5826 b02560ec 2019-08-19 stsp
5827 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5828 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5829 b02560ec 2019-08-19 stsp blame->nlines--;
5830 a70480e0 2018-06-23 stsp
5831 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5832 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5833 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5834 84451b3e 2018-07-10 stsp goto done;
5835 84451b3e 2018-07-10 stsp }
5836 a70480e0 2018-06-23 stsp
5837 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5838 bd24772e 2018-07-11 stsp if (err)
5839 bd24772e 2018-07-11 stsp goto done;
5840 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5841 7cd52833 2022-06-23 thomas pack_fds);
5842 7cd52833 2022-06-23 thomas if (err)
5843 7cd52833 2022-06-23 thomas goto done;
5844 bd24772e 2018-07-11 stsp
5845 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5846 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5847 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5848 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5849 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5850 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5851 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5852 245d91c1 2018-07-12 stsp goto done;
5853 245d91c1 2018-07-12 stsp }
5854 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5855 245d91c1 2018-07-12 stsp
5856 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5857 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5858 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5859 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5860 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5861 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5862 a5388363 2020-12-01 naddy s->blame_complete = 0;
5863 f5a09613 2020-12-13 naddy
5864 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5865 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5866 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5867 f5a09613 2020-12-13 naddy s->selected_line = 1;
5868 f5a09613 2020-12-13 naddy }
5869 aa61903a 2021-12-31 thomas s->matched_line = 0;
5870 245d91c1 2018-07-12 stsp
5871 245d91c1 2018-07-12 stsp done:
5872 945f9229 2022-04-16 thomas if (commit)
5873 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5874 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5875 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5876 245d91c1 2018-07-12 stsp if (blob)
5877 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5878 27d434c2 2018-09-15 stsp free(obj_id);
5879 245d91c1 2018-07-12 stsp if (err)
5880 245d91c1 2018-07-12 stsp stop_blame(blame);
5881 245d91c1 2018-07-12 stsp return err;
5882 245d91c1 2018-07-12 stsp }
5883 245d91c1 2018-07-12 stsp
5884 245d91c1 2018-07-12 stsp static const struct got_error *
5885 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5886 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5887 245d91c1 2018-07-12 stsp {
5888 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5889 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5890 dbc6a6b6 2018-07-12 stsp
5891 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5892 245d91c1 2018-07-12 stsp
5893 c4843652 2019-08-12 stsp s->path = strdup(path);
5894 c4843652 2019-08-12 stsp if (s->path == NULL)
5895 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5896 c4843652 2019-08-12 stsp
5897 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5898 c4843652 2019-08-12 stsp if (err) {
5899 c4843652 2019-08-12 stsp free(s->path);
5900 7cbe629d 2018-08-04 stsp return err;
5901 c4843652 2019-08-12 stsp }
5902 245d91c1 2018-07-12 stsp
5903 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5904 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5905 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5906 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5907 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5908 fb2756b9 2018-08-04 stsp s->repo = repo;
5909 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5910 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5911 7cbe629d 2018-08-04 stsp
5912 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5913 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5914 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5915 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5916 11b20872 2019-11-08 stsp if (err)
5917 11b20872 2019-11-08 stsp return err;
5918 11b20872 2019-11-08 stsp }
5919 11b20872 2019-11-08 stsp
5920 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5921 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5922 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5923 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5924 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5925 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5926 e5a0f69f 2018-08-18 stsp
5927 a5388363 2020-12-01 naddy return run_blame(view);
5928 7cbe629d 2018-08-04 stsp }
5929 7cbe629d 2018-08-04 stsp
5930 e5a0f69f 2018-08-18 stsp static const struct got_error *
5931 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5932 7cbe629d 2018-08-04 stsp {
5933 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5934 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5935 7cbe629d 2018-08-04 stsp
5936 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5937 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5938 e5a0f69f 2018-08-18 stsp
5939 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5940 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5941 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5942 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5943 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5944 7cbe629d 2018-08-04 stsp }
5945 e5a0f69f 2018-08-18 stsp
5946 e5a0f69f 2018-08-18 stsp free(s->path);
5947 11b20872 2019-11-08 stsp free_colors(&s->colors);
5948 e5a0f69f 2018-08-18 stsp return err;
5949 7cbe629d 2018-08-04 stsp }
5950 7cbe629d 2018-08-04 stsp
5951 7cbe629d 2018-08-04 stsp static const struct got_error *
5952 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5953 6c4c42e0 2019-06-24 stsp {
5954 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5955 6c4c42e0 2019-06-24 stsp
5956 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5957 6c4c42e0 2019-06-24 stsp return NULL;
5958 6c4c42e0 2019-06-24 stsp }
5959 6c4c42e0 2019-06-24 stsp
5960 6c4c42e0 2019-06-24 stsp static const struct got_error *
5961 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5962 6c4c42e0 2019-06-24 stsp {
5963 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5964 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5965 6c4c42e0 2019-06-24 stsp int lineno;
5966 78eecffc 2022-06-23 thomas char *line = NULL;
5967 826082fe 2020-12-10 stsp size_t linesize = 0;
5968 826082fe 2020-12-10 stsp ssize_t linelen;
5969 6c4c42e0 2019-06-24 stsp
5970 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5971 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5972 6c4c42e0 2019-06-24 stsp return NULL;
5973 6c4c42e0 2019-06-24 stsp }
5974 6c4c42e0 2019-06-24 stsp
5975 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5976 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5977 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5978 6c4c42e0 2019-06-24 stsp else
5979 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5980 4b3f9dac 2021-12-17 thomas } else
5981 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5982 6c4c42e0 2019-06-24 stsp
5983 6c4c42e0 2019-06-24 stsp while (1) {
5984 6c4c42e0 2019-06-24 stsp off_t offset;
5985 6c4c42e0 2019-06-24 stsp
5986 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5987 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5988 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5989 6c4c42e0 2019-06-24 stsp break;
5990 6c4c42e0 2019-06-24 stsp }
5991 2246482e 2019-06-25 stsp
5992 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5993 6c4c42e0 2019-06-24 stsp lineno = 1;
5994 6c4c42e0 2019-06-24 stsp else
5995 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5996 6c4c42e0 2019-06-24 stsp }
5997 6c4c42e0 2019-06-24 stsp
5998 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5999 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6000 6c4c42e0 2019-06-24 stsp free(line);
6001 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
6002 6c4c42e0 2019-06-24 stsp }
6003 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
6004 78eecffc 2022-06-23 thomas if (linelen != -1) {
6005 78eecffc 2022-06-23 thomas char *exstr;
6006 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
6007 78eecffc 2022-06-23 thomas if (err)
6008 78eecffc 2022-06-23 thomas break;
6009 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
6010 78eecffc 2022-06-23 thomas &view->regmatch)) {
6011 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
6012 78eecffc 2022-06-23 thomas s->matched_line = lineno;
6013 78eecffc 2022-06-23 thomas free(exstr);
6014 78eecffc 2022-06-23 thomas break;
6015 78eecffc 2022-06-23 thomas }
6016 78eecffc 2022-06-23 thomas free(exstr);
6017 6c4c42e0 2019-06-24 stsp }
6018 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6019 6c4c42e0 2019-06-24 stsp lineno++;
6020 6c4c42e0 2019-06-24 stsp else
6021 6c4c42e0 2019-06-24 stsp lineno--;
6022 6c4c42e0 2019-06-24 stsp }
6023 826082fe 2020-12-10 stsp free(line);
6024 6c4c42e0 2019-06-24 stsp
6025 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
6026 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
6027 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
6028 6c4c42e0 2019-06-24 stsp }
6029 6c4c42e0 2019-06-24 stsp
6030 05171be4 2022-06-23 thomas return err;
6031 6c4c42e0 2019-06-24 stsp }
6032 6c4c42e0 2019-06-24 stsp
6033 6c4c42e0 2019-06-24 stsp static const struct got_error *
6034 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6035 7cbe629d 2018-08-04 stsp {
6036 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6037 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6038 2b380cc8 2018-10-24 stsp int errcode;
6039 2b380cc8 2018-10-24 stsp
6040 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6041 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6042 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6043 2b380cc8 2018-10-24 stsp if (errcode)
6044 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6045 51fe7530 2019-08-19 stsp
6046 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6047 2b380cc8 2018-10-24 stsp }
6048 e5a0f69f 2018-08-18 stsp
6049 51fe7530 2019-08-19 stsp if (s->blame_complete)
6050 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6051 51fe7530 2019-08-19 stsp
6052 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6053 e5a0f69f 2018-08-18 stsp
6054 a5d43cac 2022-07-01 thomas view_border(view);
6055 e5a0f69f 2018-08-18 stsp return err;
6056 e5a0f69f 2018-08-18 stsp }
6057 e5a0f69f 2018-08-18 stsp
6058 e5a0f69f 2018-08-18 stsp static const struct got_error *
6059 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6060 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6061 eaeaa612 2022-07-20 thomas {
6062 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6063 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6064 eaeaa612 2022-07-20 thomas
6065 eaeaa612 2022-07-20 thomas *new_view = NULL;
6066 eaeaa612 2022-07-20 thomas
6067 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6068 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6069 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6070 eaeaa612 2022-07-20 thomas
6071 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6072 eaeaa612 2022-07-20 thomas if (err)
6073 eaeaa612 2022-07-20 thomas view_close(log_view);
6074 eaeaa612 2022-07-20 thomas else
6075 eaeaa612 2022-07-20 thomas *new_view = log_view;
6076 eaeaa612 2022-07-20 thomas
6077 eaeaa612 2022-07-20 thomas return err;
6078 eaeaa612 2022-07-20 thomas }
6079 eaeaa612 2022-07-20 thomas
6080 eaeaa612 2022-07-20 thomas static const struct got_error *
6081 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6082 e5a0f69f 2018-08-18 stsp {
6083 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6084 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6085 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6086 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6087 a5d43cac 2022-07-01 thomas
6088 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6089 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6090 a5d43cac 2022-07-01 thomas --eos; /* border */
6091 7cbe629d 2018-08-04 stsp
6092 e5a0f69f 2018-08-18 stsp switch (ch) {
6093 05171be4 2022-06-23 thomas case '0':
6094 05171be4 2022-06-23 thomas view->x = 0;
6095 05171be4 2022-06-23 thomas break;
6096 05171be4 2022-06-23 thomas case '$':
6097 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6098 07b0611c 2022-06-23 thomas view->count = 0;
6099 05171be4 2022-06-23 thomas break;
6100 05171be4 2022-06-23 thomas case KEY_RIGHT:
6101 05171be4 2022-06-23 thomas case 'l':
6102 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6103 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6104 07b0611c 2022-06-23 thomas else
6105 07b0611c 2022-06-23 thomas view->count = 0;
6106 05171be4 2022-06-23 thomas break;
6107 05171be4 2022-06-23 thomas case KEY_LEFT:
6108 05171be4 2022-06-23 thomas case 'h':
6109 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6110 07b0611c 2022-06-23 thomas if (view->x <= 0)
6111 07b0611c 2022-06-23 thomas view->count = 0;
6112 05171be4 2022-06-23 thomas break;
6113 1e37a5c2 2019-05-12 jcs case 'q':
6114 1e37a5c2 2019-05-12 jcs s->done = 1;
6115 4deef56f 2021-09-02 naddy break;
6116 4deef56f 2021-09-02 naddy case 'g':
6117 4deef56f 2021-09-02 naddy case KEY_HOME:
6118 4deef56f 2021-09-02 naddy s->selected_line = 1;
6119 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6120 07b0611c 2022-06-23 thomas view->count = 0;
6121 4deef56f 2021-09-02 naddy break;
6122 4deef56f 2021-09-02 naddy case 'G':
6123 4deef56f 2021-09-02 naddy case KEY_END:
6124 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6125 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6126 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6127 4deef56f 2021-09-02 naddy } else {
6128 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6129 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6130 4deef56f 2021-09-02 naddy }
6131 07b0611c 2022-06-23 thomas view->count = 0;
6132 1e37a5c2 2019-05-12 jcs break;
6133 1e37a5c2 2019-05-12 jcs case 'k':
6134 1e37a5c2 2019-05-12 jcs case KEY_UP:
6135 f7140bf5 2021-10-17 thomas case CTRL('p'):
6136 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6137 1e37a5c2 2019-05-12 jcs s->selected_line--;
6138 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6139 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6140 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6141 07b0611c 2022-06-23 thomas else
6142 07b0611c 2022-06-23 thomas view->count = 0;
6143 1e37a5c2 2019-05-12 jcs break;
6144 70f17a53 2022-06-13 thomas case CTRL('u'):
6145 23427b14 2022-06-23 thomas case 'u':
6146 70f17a53 2022-06-13 thomas nscroll /= 2;
6147 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6148 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6149 ea025d1d 2020-02-22 naddy case CTRL('b'):
6150 1c5e5faa 2022-06-23 thomas case 'b':
6151 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6152 07b0611c 2022-06-23 thomas if (view->count > 1)
6153 07b0611c 2022-06-23 thomas nscroll += nscroll;
6154 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6155 07b0611c 2022-06-23 thomas view->count = 0;
6156 e5a0f69f 2018-08-18 stsp break;
6157 1e37a5c2 2019-05-12 jcs }
6158 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6159 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6160 1e37a5c2 2019-05-12 jcs else
6161 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6162 1e37a5c2 2019-05-12 jcs break;
6163 1e37a5c2 2019-05-12 jcs case 'j':
6164 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6165 f7140bf5 2021-10-17 thomas case CTRL('n'):
6166 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6167 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6168 1e37a5c2 2019-05-12 jcs s->selected_line++;
6169 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6170 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6171 07b0611c 2022-06-23 thomas else
6172 07b0611c 2022-06-23 thomas view->count = 0;
6173 1e37a5c2 2019-05-12 jcs break;
6174 1c5e5faa 2022-06-23 thomas case 'c':
6175 1e37a5c2 2019-05-12 jcs case 'p': {
6176 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6177 07b0611c 2022-06-23 thomas
6178 07b0611c 2022-06-23 thomas view->count = 0;
6179 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6180 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6181 1e37a5c2 2019-05-12 jcs if (id == NULL)
6182 e5a0f69f 2018-08-18 stsp break;
6183 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6184 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6185 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6186 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6187 1e37a5c2 2019-05-12 jcs int obj_type;
6188 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6189 1e37a5c2 2019-05-12 jcs s->repo, id);
6190 e5a0f69f 2018-08-18 stsp if (err)
6191 e5a0f69f 2018-08-18 stsp break;
6192 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6193 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6194 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6195 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6196 e5a0f69f 2018-08-18 stsp break;
6197 e5a0f69f 2018-08-18 stsp }
6198 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6199 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6200 ec242592 2022-04-22 thomas s->repo, &pid->id);
6201 945f9229 2022-04-16 thomas if (err)
6202 945f9229 2022-04-16 thomas break;
6203 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6204 945f9229 2022-04-16 thomas pcommit, s->path);
6205 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6206 e5a0f69f 2018-08-18 stsp if (err) {
6207 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6208 1e37a5c2 2019-05-12 jcs err = NULL;
6209 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6210 e5a0f69f 2018-08-18 stsp break;
6211 e5a0f69f 2018-08-18 stsp }
6212 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6213 1e37a5c2 2019-05-12 jcs blob_id);
6214 1e37a5c2 2019-05-12 jcs free(blob_id);
6215 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6216 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6217 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6218 e5a0f69f 2018-08-18 stsp break;
6219 1e37a5c2 2019-05-12 jcs }
6220 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6221 ec242592 2022-04-22 thomas &pid->id);
6222 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6223 1e37a5c2 2019-05-12 jcs } else {
6224 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6225 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6226 1e37a5c2 2019-05-12 jcs break;
6227 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6228 1e37a5c2 2019-05-12 jcs id);
6229 1e37a5c2 2019-05-12 jcs }
6230 1e37a5c2 2019-05-12 jcs if (err)
6231 e5a0f69f 2018-08-18 stsp break;
6232 1e37a5c2 2019-05-12 jcs s->done = 1;
6233 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6234 1e37a5c2 2019-05-12 jcs s->done = 0;
6235 1e37a5c2 2019-05-12 jcs if (thread_err)
6236 1e37a5c2 2019-05-12 jcs break;
6237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6238 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6239 a5388363 2020-12-01 naddy err = run_blame(view);
6240 1e37a5c2 2019-05-12 jcs if (err)
6241 1e37a5c2 2019-05-12 jcs break;
6242 1e37a5c2 2019-05-12 jcs break;
6243 1e37a5c2 2019-05-12 jcs }
6244 1c5e5faa 2022-06-23 thomas case 'C': {
6245 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6246 07b0611c 2022-06-23 thomas
6247 07b0611c 2022-06-23 thomas view->count = 0;
6248 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6249 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6250 1e37a5c2 2019-05-12 jcs break;
6251 1e37a5c2 2019-05-12 jcs s->done = 1;
6252 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6253 1e37a5c2 2019-05-12 jcs s->done = 0;
6254 1e37a5c2 2019-05-12 jcs if (thread_err)
6255 1e37a5c2 2019-05-12 jcs break;
6256 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6257 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6258 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6259 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6260 a5388363 2020-12-01 naddy err = run_blame(view);
6261 1e37a5c2 2019-05-12 jcs if (err)
6262 1e37a5c2 2019-05-12 jcs break;
6263 1e37a5c2 2019-05-12 jcs break;
6264 1e37a5c2 2019-05-12 jcs }
6265 2a31b33b 2022-07-23 thomas case 'L':
6266 eaeaa612 2022-07-20 thomas view->count = 0;
6267 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6268 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6269 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6270 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6271 eaeaa612 2022-07-20 thomas break;
6272 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6273 1e37a5c2 2019-05-12 jcs case '\r': {
6274 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6275 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6276 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6277 07b0611c 2022-06-23 thomas
6278 07b0611c 2022-06-23 thomas view->count = 0;
6279 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6280 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6281 1e37a5c2 2019-05-12 jcs if (id == NULL)
6282 1e37a5c2 2019-05-12 jcs break;
6283 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6284 1e37a5c2 2019-05-12 jcs if (err)
6285 1e37a5c2 2019-05-12 jcs break;
6286 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6287 4fc71f3b 2022-07-12 thomas if (*new_view) {
6288 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6289 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6290 4fc71f3b 2022-07-12 thomas if (err)
6291 4fc71f3b 2022-07-12 thomas break;
6292 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6293 4fc71f3b 2022-07-12 thomas } else {
6294 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6295 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6296 a5d43cac 2022-07-01 thomas
6297 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6298 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6299 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6300 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6301 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6302 4fc71f3b 2022-07-12 thomas break;
6303 4fc71f3b 2022-07-12 thomas }
6304 15a94983 2018-12-23 stsp }
6305 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6306 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6307 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6308 1e37a5c2 2019-05-12 jcs if (err) {
6309 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6310 1e37a5c2 2019-05-12 jcs break;
6311 1e37a5c2 2019-05-12 jcs }
6312 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6313 4fc71f3b 2022-07-12 thomas s->selected_line;
6314 4fc71f3b 2022-07-12 thomas if (*new_view)
6315 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6316 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6317 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6318 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6319 a5d43cac 2022-07-01 thomas if (err)
6320 a5d43cac 2022-07-01 thomas break;
6321 a5d43cac 2022-07-01 thomas }
6322 a5d43cac 2022-07-01 thomas
6323 e78dc838 2020-12-04 stsp view->focussed = 0;
6324 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6325 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6326 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6327 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6328 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6329 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6330 1e37a5c2 2019-05-12 jcs if (err)
6331 34bc9ec9 2019-02-22 stsp break;
6332 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6333 40236d76 2022-06-23 thomas if (err)
6334 40236d76 2022-06-23 thomas break;
6335 e78dc838 2020-12-04 stsp view->focus_child = 1;
6336 1e37a5c2 2019-05-12 jcs } else
6337 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6338 1e37a5c2 2019-05-12 jcs if (err)
6339 e5a0f69f 2018-08-18 stsp break;
6340 1e37a5c2 2019-05-12 jcs break;
6341 1e37a5c2 2019-05-12 jcs }
6342 70f17a53 2022-06-13 thomas case CTRL('d'):
6343 23427b14 2022-06-23 thomas case 'd':
6344 70f17a53 2022-06-13 thomas nscroll /= 2;
6345 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6346 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6347 ea025d1d 2020-02-22 naddy case CTRL('f'):
6348 1c5e5faa 2022-06-23 thomas case 'f':
6349 1e37a5c2 2019-05-12 jcs case ' ':
6350 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6351 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6352 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6353 07b0611c 2022-06-23 thomas view->count = 0;
6354 e5a0f69f 2018-08-18 stsp break;
6355 1e37a5c2 2019-05-12 jcs }
6356 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6357 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6358 70f17a53 2022-06-13 thomas s->selected_line +=
6359 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6360 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6361 1e37a5c2 2019-05-12 jcs }
6362 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6363 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6364 1e37a5c2 2019-05-12 jcs else
6365 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6366 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6367 1e37a5c2 2019-05-12 jcs break;
6368 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6369 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6370 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6371 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6372 1e37a5c2 2019-05-12 jcs }
6373 1e37a5c2 2019-05-12 jcs break;
6374 1e37a5c2 2019-05-12 jcs default:
6375 07b0611c 2022-06-23 thomas view->count = 0;
6376 1e37a5c2 2019-05-12 jcs break;
6377 a70480e0 2018-06-23 stsp }
6378 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6379 adf4c9e0 2022-07-03 thomas }
6380 adf4c9e0 2022-07-03 thomas
6381 adf4c9e0 2022-07-03 thomas static const struct got_error *
6382 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6383 adf4c9e0 2022-07-03 thomas {
6384 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6385 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6386 adf4c9e0 2022-07-03 thomas
6387 adf4c9e0 2022-07-03 thomas view->count = 0;
6388 adf4c9e0 2022-07-03 thomas s->done = 1;
6389 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6390 adf4c9e0 2022-07-03 thomas s->done = 0;
6391 adf4c9e0 2022-07-03 thomas if (err)
6392 adf4c9e0 2022-07-03 thomas return err;
6393 adf4c9e0 2022-07-03 thomas return run_blame(view);
6394 a70480e0 2018-06-23 stsp }
6395 a70480e0 2018-06-23 stsp
6396 a70480e0 2018-06-23 stsp static const struct got_error *
6397 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6398 9f7d7167 2018-04-29 stsp {
6399 a70480e0 2018-06-23 stsp const struct got_error *error;
6400 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6401 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6402 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6403 0587e10c 2020-07-23 stsp char *link_target = NULL;
6404 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6405 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6406 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6407 a70480e0 2018-06-23 stsp int ch;
6408 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6409 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6410 a70480e0 2018-06-23 stsp
6411 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6412 a70480e0 2018-06-23 stsp switch (ch) {
6413 a70480e0 2018-06-23 stsp case 'c':
6414 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6415 a70480e0 2018-06-23 stsp break;
6416 69069811 2018-08-02 stsp case 'r':
6417 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6418 69069811 2018-08-02 stsp if (repo_path == NULL)
6419 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6420 9ba1d308 2019-10-21 stsp optarg);
6421 69069811 2018-08-02 stsp break;
6422 a70480e0 2018-06-23 stsp default:
6423 17020d27 2019-03-07 stsp usage_blame();
6424 a70480e0 2018-06-23 stsp /* NOTREACHED */
6425 a70480e0 2018-06-23 stsp }
6426 a70480e0 2018-06-23 stsp }
6427 a70480e0 2018-06-23 stsp
6428 a70480e0 2018-06-23 stsp argc -= optind;
6429 a70480e0 2018-06-23 stsp argv += optind;
6430 a70480e0 2018-06-23 stsp
6431 f135c941 2020-02-20 stsp if (argc != 1)
6432 a70480e0 2018-06-23 stsp usage_blame();
6433 6962eb72 2020-02-20 stsp
6434 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6435 7cd52833 2022-06-23 thomas if (error != NULL)
6436 7cd52833 2022-06-23 thomas goto done;
6437 7cd52833 2022-06-23 thomas
6438 69069811 2018-08-02 stsp if (repo_path == NULL) {
6439 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6440 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6441 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6442 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6443 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6444 c156c7a4 2020-12-18 stsp goto done;
6445 f135c941 2020-02-20 stsp if (worktree)
6446 eb41ed75 2019-02-05 stsp repo_path =
6447 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6448 f135c941 2020-02-20 stsp else
6449 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6450 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6451 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6452 c156c7a4 2020-12-18 stsp goto done;
6453 c156c7a4 2020-12-18 stsp }
6454 f135c941 2020-02-20 stsp }
6455 a915003a 2019-02-05 stsp
6456 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6457 c02c541e 2019-03-29 stsp if (error != NULL)
6458 8e94dd5b 2019-01-04 stsp goto done;
6459 69069811 2018-08-02 stsp
6460 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6461 f135c941 2020-02-20 stsp worktree);
6462 c02c541e 2019-03-29 stsp if (error)
6463 92205607 2019-01-04 stsp goto done;
6464 69069811 2018-08-02 stsp
6465 f135c941 2020-02-20 stsp init_curses();
6466 f135c941 2020-02-20 stsp
6467 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6468 51a10b52 2020-12-26 stsp if (error)
6469 51a10b52 2020-12-26 stsp goto done;
6470 51a10b52 2020-12-26 stsp
6471 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6472 eb41ed75 2019-02-05 stsp if (error)
6473 69069811 2018-08-02 stsp goto done;
6474 a70480e0 2018-06-23 stsp
6475 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6476 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6477 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6478 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6479 a70480e0 2018-06-23 stsp if (error != NULL)
6480 66b4983c 2018-06-23 stsp goto done;
6481 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6482 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6483 a70480e0 2018-06-23 stsp } else {
6484 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6485 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6486 a70480e0 2018-06-23 stsp }
6487 a19e88aa 2018-06-23 stsp if (error != NULL)
6488 8b473291 2019-02-21 stsp goto done;
6489 8b473291 2019-02-21 stsp
6490 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6491 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6492 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6493 e1cd8fed 2018-08-01 stsp goto done;
6494 e1cd8fed 2018-08-01 stsp }
6495 0587e10c 2020-07-23 stsp
6496 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6497 945f9229 2022-04-16 thomas if (error)
6498 945f9229 2022-04-16 thomas goto done;
6499 945f9229 2022-04-16 thomas
6500 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6501 945f9229 2022-04-16 thomas commit, repo);
6502 7cbe629d 2018-08-04 stsp if (error)
6503 7cbe629d 2018-08-04 stsp goto done;
6504 0587e10c 2020-07-23 stsp
6505 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6506 78756c87 2020-11-24 stsp commit_id, repo);
6507 0587e10c 2020-07-23 stsp if (error)
6508 0587e10c 2020-07-23 stsp goto done;
6509 12314ad4 2019-08-31 stsp if (worktree) {
6510 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6511 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6512 12314ad4 2019-08-31 stsp worktree = NULL;
6513 12314ad4 2019-08-31 stsp }
6514 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6515 a70480e0 2018-06-23 stsp done:
6516 69069811 2018-08-02 stsp free(repo_path);
6517 f135c941 2020-02-20 stsp free(in_repo_path);
6518 0587e10c 2020-07-23 stsp free(link_target);
6519 69069811 2018-08-02 stsp free(cwd);
6520 a70480e0 2018-06-23 stsp free(commit_id);
6521 945f9229 2022-04-16 thomas if (commit)
6522 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6523 eb41ed75 2019-02-05 stsp if (worktree)
6524 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6525 1d0f4054 2021-06-17 stsp if (repo) {
6526 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6527 1d0f4054 2021-06-17 stsp if (error == NULL)
6528 1d0f4054 2021-06-17 stsp error = close_err;
6529 1d0f4054 2021-06-17 stsp }
6530 7cd52833 2022-06-23 thomas if (pack_fds) {
6531 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6532 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6533 7cd52833 2022-06-23 thomas if (error == NULL)
6534 7cd52833 2022-06-23 thomas error = pack_err;
6535 7cd52833 2022-06-23 thomas }
6536 51a10b52 2020-12-26 stsp tog_free_refs();
6537 a70480e0 2018-06-23 stsp return error;
6538 ffd1d5e5 2018-06-23 stsp }
6539 ffd1d5e5 2018-06-23 stsp
6540 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6541 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6542 ffd1d5e5 2018-06-23 stsp {
6543 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6544 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6545 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6546 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6547 86f4aab9 2022-09-09 thomas char *index = NULL;
6548 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6549 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6550 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6551 ffd1d5e5 2018-06-23 stsp
6552 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6553 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6554 a5d43cac 2022-07-01 thomas --limit; /* border */
6555 ffd1d5e5 2018-06-23 stsp
6556 f7d12f7e 2018-08-01 stsp werase(view->window);
6557 ffd1d5e5 2018-06-23 stsp
6558 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6559 ffd1d5e5 2018-06-23 stsp return NULL;
6560 ffd1d5e5 2018-06-23 stsp
6561 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6562 f91a2b48 2022-06-23 thomas 0, 0);
6563 ffd1d5e5 2018-06-23 stsp if (err)
6564 ffd1d5e5 2018-06-23 stsp return err;
6565 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6566 a3404814 2018-09-02 stsp wstandout(view->window);
6567 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6568 11b20872 2019-11-08 stsp if (tc)
6569 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6570 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6571 86f4aab9 2022-09-09 thomas free(wline);
6572 86f4aab9 2022-09-09 thomas wline = NULL;
6573 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6574 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6575 11b20872 2019-11-08 stsp if (tc)
6576 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6577 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6578 a3404814 2018-09-02 stsp wstandend(view->window);
6579 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6580 86f4aab9 2022-09-09 thomas return NULL;
6581 07dd3ed3 2022-08-06 thomas
6582 6f5f393a 2022-08-12 thomas i += s->selected;
6583 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6584 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6585 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6586 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6587 07dd3ed3 2022-08-06 thomas }
6588 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6589 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6590 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6591 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6592 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6593 86f4aab9 2022-09-09 thomas free(index);
6594 ce52c690 2018-06-23 stsp if (err)
6595 ce52c690 2018-06-23 stsp return err;
6596 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6597 2550e4c3 2018-07-13 stsp free(wline);
6598 2550e4c3 2018-07-13 stsp wline = NULL;
6599 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6600 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6601 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6602 ffd1d5e5 2018-06-23 stsp return NULL;
6603 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6604 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6605 a1eca9bb 2018-06-23 stsp return NULL;
6606 ffd1d5e5 2018-06-23 stsp
6607 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6608 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6609 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6610 0cf4efb1 2018-09-29 stsp if (view->focussed)
6611 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6612 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6613 ffd1d5e5 2018-06-23 stsp }
6614 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6615 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6616 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6617 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6618 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6619 ffd1d5e5 2018-06-23 stsp return NULL;
6620 ffd1d5e5 2018-06-23 stsp n = 1;
6621 ffd1d5e5 2018-06-23 stsp } else {
6622 ffd1d5e5 2018-06-23 stsp n = 0;
6623 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6624 ffd1d5e5 2018-06-23 stsp }
6625 ffd1d5e5 2018-06-23 stsp
6626 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6627 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6628 848d6979 2019-08-12 stsp const char *modestr = "";
6629 56e0773d 2019-11-28 stsp mode_t mode;
6630 1d13200f 2018-07-12 stsp
6631 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6632 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6633 56e0773d 2019-11-28 stsp
6634 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6635 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6636 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6637 1d13200f 2018-07-12 stsp if (err)
6638 638f9024 2019-05-13 stsp return got_error_from_errno(
6639 230a42bd 2019-05-11 jcs "got_object_id_str");
6640 1d13200f 2018-07-12 stsp }
6641 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6642 63c5ca5d 2019-08-24 stsp modestr = "$";
6643 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6644 0d6c6ee3 2020-05-20 stsp int i;
6645 0d6c6ee3 2020-05-20 stsp
6646 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6647 d86d3b18 2020-12-01 naddy te, s->repo);
6648 0d6c6ee3 2020-05-20 stsp if (err) {
6649 0d6c6ee3 2020-05-20 stsp free(id_str);
6650 0d6c6ee3 2020-05-20 stsp return err;
6651 0d6c6ee3 2020-05-20 stsp }
6652 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6653 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6654 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6655 0d6c6ee3 2020-05-20 stsp }
6656 848d6979 2019-08-12 stsp modestr = "@";
6657 0d6c6ee3 2020-05-20 stsp }
6658 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6659 848d6979 2019-08-12 stsp modestr = "/";
6660 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6661 848d6979 2019-08-12 stsp modestr = "*";
6662 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6663 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6664 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6665 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6666 1d13200f 2018-07-12 stsp free(id_str);
6667 0d6c6ee3 2020-05-20 stsp free(link_target);
6668 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6669 1d13200f 2018-07-12 stsp }
6670 1d13200f 2018-07-12 stsp free(id_str);
6671 0d6c6ee3 2020-05-20 stsp free(link_target);
6672 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6673 f91a2b48 2022-06-23 thomas 0, 0);
6674 ffd1d5e5 2018-06-23 stsp if (err) {
6675 ffd1d5e5 2018-06-23 stsp free(line);
6676 ffd1d5e5 2018-06-23 stsp break;
6677 ffd1d5e5 2018-06-23 stsp }
6678 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6679 0cf4efb1 2018-09-29 stsp if (view->focussed)
6680 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6681 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6682 ffd1d5e5 2018-06-23 stsp }
6683 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6684 f26dddb7 2019-11-08 stsp if (tc)
6685 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6686 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6687 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6688 f26dddb7 2019-11-08 stsp if (tc)
6689 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6690 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6691 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6692 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6693 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6694 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6695 ffd1d5e5 2018-06-23 stsp free(line);
6696 2550e4c3 2018-07-13 stsp free(wline);
6697 2550e4c3 2018-07-13 stsp wline = NULL;
6698 ffd1d5e5 2018-06-23 stsp n++;
6699 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6700 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6701 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6702 ffd1d5e5 2018-06-23 stsp break;
6703 ffd1d5e5 2018-06-23 stsp }
6704 ffd1d5e5 2018-06-23 stsp
6705 ffd1d5e5 2018-06-23 stsp return err;
6706 ffd1d5e5 2018-06-23 stsp }
6707 ffd1d5e5 2018-06-23 stsp
6708 ffd1d5e5 2018-06-23 stsp static void
6709 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6710 ffd1d5e5 2018-06-23 stsp {
6711 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6712 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6713 fa86c4bf 2020-11-29 stsp int i = 0;
6714 ffd1d5e5 2018-06-23 stsp
6715 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6716 ffd1d5e5 2018-06-23 stsp return;
6717 ffd1d5e5 2018-06-23 stsp
6718 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6719 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6720 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6721 fa86c4bf 2020-11-29 stsp if (!isroot)
6722 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6723 fa86c4bf 2020-11-29 stsp break;
6724 fa86c4bf 2020-11-29 stsp }
6725 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6726 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6727 ffd1d5e5 2018-06-23 stsp }
6728 ffd1d5e5 2018-06-23 stsp }
6729 ffd1d5e5 2018-06-23 stsp
6730 a5d43cac 2022-07-01 thomas static const struct got_error *
6731 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6732 ffd1d5e5 2018-06-23 stsp {
6733 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6734 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6735 ffd1d5e5 2018-06-23 stsp int n = 0;
6736 ffd1d5e5 2018-06-23 stsp
6737 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6738 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6739 694d3271 2020-12-01 naddy s->first_displayed_entry);
6740 694d3271 2020-12-01 naddy else
6741 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6742 56e0773d 2019-11-28 stsp
6743 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6744 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6745 07dd3ed3 2022-08-06 thomas if (last) {
6746 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6747 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6748 07dd3ed3 2022-08-06 thomas }
6749 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6750 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6751 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6752 768394f3 2019-01-24 stsp }
6753 ffd1d5e5 2018-06-23 stsp }
6754 a5d43cac 2022-07-01 thomas
6755 a5d43cac 2022-07-01 thomas return NULL;
6756 ffd1d5e5 2018-06-23 stsp }
6757 ffd1d5e5 2018-06-23 stsp
6758 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6759 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6760 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6761 ffd1d5e5 2018-06-23 stsp {
6762 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6763 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6764 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6765 ffd1d5e5 2018-06-23 stsp
6766 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6767 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6768 56e0773d 2019-11-28 stsp + 1 /* slash */;
6769 ce52c690 2018-06-23 stsp if (te)
6770 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6771 ce52c690 2018-06-23 stsp
6772 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6773 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6774 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6775 ffd1d5e5 2018-06-23 stsp
6776 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6777 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6778 d9765a41 2018-06-23 stsp while (pt) {
6779 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6780 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6781 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6782 cb2ebc8a 2018-06-23 stsp goto done;
6783 cb2ebc8a 2018-06-23 stsp }
6784 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6785 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6786 cb2ebc8a 2018-06-23 stsp goto done;
6787 cb2ebc8a 2018-06-23 stsp }
6788 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6789 ffd1d5e5 2018-06-23 stsp }
6790 ce52c690 2018-06-23 stsp if (te) {
6791 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6792 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6793 ce52c690 2018-06-23 stsp goto done;
6794 ce52c690 2018-06-23 stsp }
6795 cb2ebc8a 2018-06-23 stsp }
6796 ce52c690 2018-06-23 stsp done:
6797 ce52c690 2018-06-23 stsp if (err) {
6798 ce52c690 2018-06-23 stsp free(*path);
6799 ce52c690 2018-06-23 stsp *path = NULL;
6800 ce52c690 2018-06-23 stsp }
6801 ce52c690 2018-06-23 stsp return err;
6802 ce52c690 2018-06-23 stsp }
6803 ce52c690 2018-06-23 stsp
6804 ce52c690 2018-06-23 stsp static const struct got_error *
6805 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6806 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6807 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6808 ce52c690 2018-06-23 stsp {
6809 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6810 ce52c690 2018-06-23 stsp char *path;
6811 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6812 a0de39f3 2019-08-09 stsp
6813 a0de39f3 2019-08-09 stsp *new_view = NULL;
6814 69efd4c4 2018-07-18 stsp
6815 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6816 ce52c690 2018-06-23 stsp if (err)
6817 ce52c690 2018-06-23 stsp return err;
6818 ffd1d5e5 2018-06-23 stsp
6819 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6820 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6821 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6822 83ce39e3 2019-08-12 stsp goto done;
6823 83ce39e3 2019-08-12 stsp }
6824 cdf1ee82 2018-08-01 stsp
6825 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6826 e5a0f69f 2018-08-18 stsp if (err) {
6827 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6828 fc06ba56 2019-08-22 stsp err = NULL;
6829 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6830 e5a0f69f 2018-08-18 stsp } else
6831 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6832 83ce39e3 2019-08-12 stsp done:
6833 83ce39e3 2019-08-12 stsp free(path);
6834 69efd4c4 2018-07-18 stsp return err;
6835 69efd4c4 2018-07-18 stsp }
6836 69efd4c4 2018-07-18 stsp
6837 69efd4c4 2018-07-18 stsp static const struct got_error *
6838 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6839 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6840 69efd4c4 2018-07-18 stsp {
6841 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6842 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6843 69efd4c4 2018-07-18 stsp char *path;
6844 a0de39f3 2019-08-09 stsp
6845 a0de39f3 2019-08-09 stsp *new_view = NULL;
6846 69efd4c4 2018-07-18 stsp
6847 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6848 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6849 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6850 e5a0f69f 2018-08-18 stsp
6851 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6852 69efd4c4 2018-07-18 stsp if (err)
6853 69efd4c4 2018-07-18 stsp return err;
6854 69efd4c4 2018-07-18 stsp
6855 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6856 4e97c21c 2020-12-06 stsp path, 0);
6857 ba4f502b 2018-08-04 stsp if (err)
6858 e5a0f69f 2018-08-18 stsp view_close(log_view);
6859 e5a0f69f 2018-08-18 stsp else
6860 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6861 cb2ebc8a 2018-06-23 stsp free(path);
6862 cb2ebc8a 2018-06-23 stsp return err;
6863 ffd1d5e5 2018-06-23 stsp }
6864 ffd1d5e5 2018-06-23 stsp
6865 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6866 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6867 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6868 ffd1d5e5 2018-06-23 stsp {
6869 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6870 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6871 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6872 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6873 ffd1d5e5 2018-06-23 stsp
6874 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6875 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6876 bc573f3b 2021-07-10 stsp
6877 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6878 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6879 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6880 ffd1d5e5 2018-06-23 stsp
6881 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6882 bc573f3b 2021-07-10 stsp if (err)
6883 bc573f3b 2021-07-10 stsp goto done;
6884 bc573f3b 2021-07-10 stsp
6885 bc573f3b 2021-07-10 stsp /*
6886 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6887 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6888 bc573f3b 2021-07-10 stsp * closed on demand.
6889 bc573f3b 2021-07-10 stsp */
6890 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6891 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6892 bc573f3b 2021-07-10 stsp if (err)
6893 bc573f3b 2021-07-10 stsp goto done;
6894 bc573f3b 2021-07-10 stsp s->tree = s->root;
6895 bc573f3b 2021-07-10 stsp
6896 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6897 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6898 ffd1d5e5 2018-06-23 stsp goto done;
6899 ffd1d5e5 2018-06-23 stsp
6900 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6901 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6902 ffd1d5e5 2018-06-23 stsp goto done;
6903 ffd1d5e5 2018-06-23 stsp }
6904 ffd1d5e5 2018-06-23 stsp
6905 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6906 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6907 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6908 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6909 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6910 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6911 9cd7cbd1 2020-12-07 stsp goto done;
6912 9cd7cbd1 2020-12-07 stsp }
6913 9cd7cbd1 2020-12-07 stsp }
6914 fb2756b9 2018-08-04 stsp s->repo = repo;
6915 c0b01bdb 2019-11-08 stsp
6916 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6917 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6918 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6919 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6920 c0b01bdb 2019-11-08 stsp if (err)
6921 c0b01bdb 2019-11-08 stsp goto done;
6922 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6923 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6924 bc573f3b 2021-07-10 stsp if (err)
6925 c0b01bdb 2019-11-08 stsp goto done;
6926 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6927 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6928 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6929 bc573f3b 2021-07-10 stsp if (err)
6930 c0b01bdb 2019-11-08 stsp goto done;
6931 e5a0f69f 2018-08-18 stsp
6932 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6933 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6934 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6935 bc573f3b 2021-07-10 stsp if (err)
6936 11b20872 2019-11-08 stsp goto done;
6937 11b20872 2019-11-08 stsp
6938 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6939 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6940 bc573f3b 2021-07-10 stsp if (err)
6941 c0b01bdb 2019-11-08 stsp goto done;
6942 c0b01bdb 2019-11-08 stsp }
6943 c0b01bdb 2019-11-08 stsp
6944 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6945 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6946 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6947 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6948 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6949 ad80ab7b 2018-08-04 stsp done:
6950 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6951 bc573f3b 2021-07-10 stsp if (commit)
6952 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6953 bc573f3b 2021-07-10 stsp if (err)
6954 bc573f3b 2021-07-10 stsp close_tree_view(view);
6955 ad80ab7b 2018-08-04 stsp return err;
6956 ad80ab7b 2018-08-04 stsp }
6957 ad80ab7b 2018-08-04 stsp
6958 e5a0f69f 2018-08-18 stsp static const struct got_error *
6959 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6960 ad80ab7b 2018-08-04 stsp {
6961 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6962 ad80ab7b 2018-08-04 stsp
6963 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6964 fb2756b9 2018-08-04 stsp free(s->tree_label);
6965 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6966 6484ec90 2018-09-29 stsp free(s->commit_id);
6967 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6968 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6969 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6970 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6971 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6972 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6973 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6974 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6975 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6976 ad80ab7b 2018-08-04 stsp free(parent);
6977 ad80ab7b 2018-08-04 stsp
6978 ad80ab7b 2018-08-04 stsp }
6979 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6980 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6981 bc573f3b 2021-07-10 stsp if (s->root)
6982 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6983 7c32bd05 2019-06-22 stsp return NULL;
6984 7c32bd05 2019-06-22 stsp }
6985 7c32bd05 2019-06-22 stsp
6986 7c32bd05 2019-06-22 stsp static const struct got_error *
6987 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6988 7c32bd05 2019-06-22 stsp {
6989 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6990 7c32bd05 2019-06-22 stsp
6991 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6992 7c32bd05 2019-06-22 stsp return NULL;
6993 7c32bd05 2019-06-22 stsp }
6994 7c32bd05 2019-06-22 stsp
6995 7c32bd05 2019-06-22 stsp static int
6996 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6997 7c32bd05 2019-06-22 stsp {
6998 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6999 7c32bd05 2019-06-22 stsp
7000 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7001 56e0773d 2019-11-28 stsp 0) == 0;
7002 7c32bd05 2019-06-22 stsp }
7003 7c32bd05 2019-06-22 stsp
7004 7c32bd05 2019-06-22 stsp static const struct got_error *
7005 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7006 7c32bd05 2019-06-22 stsp {
7007 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7008 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7009 7c32bd05 2019-06-22 stsp
7010 7c32bd05 2019-06-22 stsp if (!view->searching) {
7011 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7012 7c32bd05 2019-06-22 stsp return NULL;
7013 7c32bd05 2019-06-22 stsp }
7014 7c32bd05 2019-06-22 stsp
7015 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7016 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7017 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7018 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7019 56e0773d 2019-11-28 stsp s->selected_entry);
7020 7c32bd05 2019-06-22 stsp else
7021 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7022 56e0773d 2019-11-28 stsp } else {
7023 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7024 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7025 56e0773d 2019-11-28 stsp else
7026 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7027 56e0773d 2019-11-28 stsp s->selected_entry);
7028 7c32bd05 2019-06-22 stsp }
7029 7c32bd05 2019-06-22 stsp } else {
7030 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7031 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7032 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7033 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7034 56e0773d 2019-11-28 stsp else
7035 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7036 7c32bd05 2019-06-22 stsp }
7037 7c32bd05 2019-06-22 stsp
7038 7c32bd05 2019-06-22 stsp while (1) {
7039 56e0773d 2019-11-28 stsp if (te == NULL) {
7040 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7041 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7042 ac66afb8 2019-06-24 stsp return NULL;
7043 ac66afb8 2019-06-24 stsp }
7044 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7045 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7046 56e0773d 2019-11-28 stsp else
7047 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7048 7c32bd05 2019-06-22 stsp }
7049 7c32bd05 2019-06-22 stsp
7050 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7051 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7052 56e0773d 2019-11-28 stsp s->matched_entry = te;
7053 7c32bd05 2019-06-22 stsp break;
7054 7c32bd05 2019-06-22 stsp }
7055 7c32bd05 2019-06-22 stsp
7056 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7057 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7058 56e0773d 2019-11-28 stsp else
7059 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7060 7c32bd05 2019-06-22 stsp }
7061 e5a0f69f 2018-08-18 stsp
7062 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7063 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7064 7c32bd05 2019-06-22 stsp s->selected = 0;
7065 7c32bd05 2019-06-22 stsp }
7066 7c32bd05 2019-06-22 stsp
7067 e5a0f69f 2018-08-18 stsp return NULL;
7068 ad80ab7b 2018-08-04 stsp }
7069 ad80ab7b 2018-08-04 stsp
7070 ad80ab7b 2018-08-04 stsp static const struct got_error *
7071 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7072 ad80ab7b 2018-08-04 stsp {
7073 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7074 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7075 e5a0f69f 2018-08-18 stsp char *parent_path;
7076 ad80ab7b 2018-08-04 stsp
7077 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7078 e5a0f69f 2018-08-18 stsp if (err)
7079 e5a0f69f 2018-08-18 stsp return err;
7080 ffd1d5e5 2018-06-23 stsp
7081 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7082 e5a0f69f 2018-08-18 stsp free(parent_path);
7083 669b5ffa 2018-10-07 stsp
7084 a5d43cac 2022-07-01 thomas view_border(view);
7085 e5a0f69f 2018-08-18 stsp return err;
7086 07dd3ed3 2022-08-06 thomas }
7087 07dd3ed3 2022-08-06 thomas
7088 07dd3ed3 2022-08-06 thomas static const struct got_error *
7089 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7090 07dd3ed3 2022-08-06 thomas {
7091 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7092 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7093 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7094 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7095 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7096 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7097 07dd3ed3 2022-08-06 thomas
7098 07dd3ed3 2022-08-06 thomas g = view->gline;
7099 07dd3ed3 2022-08-06 thomas view->gline = 0;
7100 07dd3ed3 2022-08-06 thomas
7101 07dd3ed3 2022-08-06 thomas if (g == 0)
7102 07dd3ed3 2022-08-06 thomas g = 1;
7103 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7104 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7105 07dd3ed3 2022-08-06 thomas
7106 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7107 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7108 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7109 07dd3ed3 2022-08-06 thomas
7110 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7111 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7112 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7113 07dd3ed3 2022-08-06 thomas }
7114 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7115 07dd3ed3 2022-08-06 thomas last += off;
7116 07dd3ed3 2022-08-06 thomas
7117 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7118 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7119 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7120 07dd3ed3 2022-08-06 thomas }
7121 07dd3ed3 2022-08-06 thomas
7122 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7123 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7124 07dd3ed3 2022-08-06 thomas i += off;
7125 07dd3ed3 2022-08-06 thomas }
7126 07dd3ed3 2022-08-06 thomas
7127 07dd3ed3 2022-08-06 thomas if (i < g) {
7128 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7129 07dd3ed3 2022-08-06 thomas if (err)
7130 07dd3ed3 2022-08-06 thomas return err;
7131 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7132 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7133 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7134 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7135 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7136 07dd3ed3 2022-08-06 thomas first += off;
7137 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7138 07dd3ed3 2022-08-06 thomas }
7139 07dd3ed3 2022-08-06 thomas } else if (i > g)
7140 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7141 07dd3ed3 2022-08-06 thomas
7142 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7143 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7144 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7145 07dd3ed3 2022-08-06 thomas
7146 07dd3ed3 2022-08-06 thomas return NULL;
7147 e5a0f69f 2018-08-18 stsp }
7148 ce52c690 2018-06-23 stsp
7149 e5a0f69f 2018-08-18 stsp static const struct got_error *
7150 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7151 e5a0f69f 2018-08-18 stsp {
7152 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7153 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7154 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7155 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7156 ffd1d5e5 2018-06-23 stsp
7157 07dd3ed3 2022-08-06 thomas if (view->gline)
7158 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7159 07dd3ed3 2022-08-06 thomas
7160 e5a0f69f 2018-08-18 stsp switch (ch) {
7161 1e37a5c2 2019-05-12 jcs case 'i':
7162 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7163 07b0611c 2022-06-23 thomas view->count = 0;
7164 1e37a5c2 2019-05-12 jcs break;
7165 1be4947a 2022-07-22 thomas case 'L':
7166 07b0611c 2022-06-23 thomas view->count = 0;
7167 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7168 ffd1d5e5 2018-06-23 stsp break;
7169 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7170 152c1c93 2020-11-29 stsp break;
7171 1be4947a 2022-07-22 thomas case 'R':
7172 07b0611c 2022-06-23 thomas view->count = 0;
7173 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7174 e4526bf5 2021-09-03 naddy break;
7175 e4526bf5 2021-09-03 naddy case 'g':
7176 e4526bf5 2021-09-03 naddy case KEY_HOME:
7177 e4526bf5 2021-09-03 naddy s->selected = 0;
7178 07b0611c 2022-06-23 thomas view->count = 0;
7179 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7180 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7181 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7182 e4526bf5 2021-09-03 naddy else
7183 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7184 1e37a5c2 2019-05-12 jcs break;
7185 e4526bf5 2021-09-03 naddy case 'G':
7186 a5d43cac 2022-07-01 thomas case KEY_END: {
7187 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7188 a5d43cac 2022-07-01 thomas
7189 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7190 a5d43cac 2022-07-01 thomas --eos; /* border */
7191 e4526bf5 2021-09-03 naddy s->selected = 0;
7192 07b0611c 2022-06-23 thomas view->count = 0;
7193 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7194 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7195 e4526bf5 2021-09-03 naddy if (te == NULL) {
7196 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7197 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7198 e4526bf5 2021-09-03 naddy n++;
7199 e4526bf5 2021-09-03 naddy }
7200 e4526bf5 2021-09-03 naddy break;
7201 e4526bf5 2021-09-03 naddy }
7202 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7203 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7204 e4526bf5 2021-09-03 naddy }
7205 e4526bf5 2021-09-03 naddy if (n > 0)
7206 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7207 e4526bf5 2021-09-03 naddy break;
7208 a5d43cac 2022-07-01 thomas }
7209 1e37a5c2 2019-05-12 jcs case 'k':
7210 1e37a5c2 2019-05-12 jcs case KEY_UP:
7211 f7140bf5 2021-10-17 thomas case CTRL('p'):
7212 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7213 1e37a5c2 2019-05-12 jcs s->selected--;
7214 fa86c4bf 2020-11-29 stsp break;
7215 1e37a5c2 2019-05-12 jcs }
7216 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7217 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7218 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7219 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7220 07b0611c 2022-06-23 thomas view->count = 0;
7221 1e37a5c2 2019-05-12 jcs break;
7222 70f17a53 2022-06-13 thomas case CTRL('u'):
7223 23427b14 2022-06-23 thomas case 'u':
7224 70f17a53 2022-06-13 thomas nscroll /= 2;
7225 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7226 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7227 ea025d1d 2020-02-22 naddy case CTRL('b'):
7228 1c5e5faa 2022-06-23 thomas case 'b':
7229 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7230 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7231 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7232 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7233 fa86c4bf 2020-11-29 stsp } else {
7234 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7235 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7236 fa86c4bf 2020-11-29 stsp }
7237 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7238 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7239 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7240 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7241 07b0611c 2022-06-23 thomas view->count = 0;
7242 1e37a5c2 2019-05-12 jcs break;
7243 1e37a5c2 2019-05-12 jcs case 'j':
7244 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7245 f7140bf5 2021-10-17 thomas case CTRL('n'):
7246 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7247 1e37a5c2 2019-05-12 jcs s->selected++;
7248 1e37a5c2 2019-05-12 jcs break;
7249 1e37a5c2 2019-05-12 jcs }
7250 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7251 07b0611c 2022-06-23 thomas == NULL) {
7252 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7253 07b0611c 2022-06-23 thomas view->count = 0;
7254 1e37a5c2 2019-05-12 jcs break;
7255 07b0611c 2022-06-23 thomas }
7256 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7257 1e37a5c2 2019-05-12 jcs break;
7258 70f17a53 2022-06-13 thomas case CTRL('d'):
7259 23427b14 2022-06-23 thomas case 'd':
7260 70f17a53 2022-06-13 thomas nscroll /= 2;
7261 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7262 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7263 ea025d1d 2020-02-22 naddy case CTRL('f'):
7264 1c5e5faa 2022-06-23 thomas case 'f':
7265 4c2d69cb 2022-06-23 thomas case ' ':
7266 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7267 1e37a5c2 2019-05-12 jcs == NULL) {
7268 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7269 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7270 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7271 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7272 07b0611c 2022-06-23 thomas else
7273 07b0611c 2022-06-23 thomas view->count = 0;
7274 1e37a5c2 2019-05-12 jcs break;
7275 1e37a5c2 2019-05-12 jcs }
7276 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7277 1e37a5c2 2019-05-12 jcs break;
7278 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7279 1e37a5c2 2019-05-12 jcs case '\r':
7280 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7281 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7282 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7283 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7284 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7285 07b0611c 2022-06-23 thomas view->count = 0;
7286 1e37a5c2 2019-05-12 jcs break;
7287 07b0611c 2022-06-23 thomas }
7288 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7289 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7290 1e37a5c2 2019-05-12 jcs entry);
7291 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7292 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7293 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7294 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7295 1e37a5c2 2019-05-12 jcs s->selected_entry =
7296 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7297 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7298 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7299 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7300 a5d43cac 2022-07-01 thomas if (err)
7301 a5d43cac 2022-07-01 thomas break;
7302 a5d43cac 2022-07-01 thomas }
7303 1e37a5c2 2019-05-12 jcs free(parent);
7304 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7305 56e0773d 2019-11-28 stsp s->selected_entry))) {
7306 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7307 07b0611c 2022-06-23 thomas view->count = 0;
7308 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7309 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7310 1e37a5c2 2019-05-12 jcs if (err)
7311 1e37a5c2 2019-05-12 jcs break;
7312 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7313 941e9f74 2019-05-21 stsp if (err) {
7314 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7315 1e37a5c2 2019-05-12 jcs break;
7316 1e37a5c2 2019-05-12 jcs }
7317 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7318 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7319 1e37a5c2 2019-05-12 jcs break;
7320 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7321 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7322 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7323 07b0611c 2022-06-23 thomas view->count = 0;
7324 1e37a5c2 2019-05-12 jcs break;
7325 1e37a5c2 2019-05-12 jcs default:
7326 07b0611c 2022-06-23 thomas view->count = 0;
7327 1e37a5c2 2019-05-12 jcs break;
7328 ffd1d5e5 2018-06-23 stsp }
7329 e5a0f69f 2018-08-18 stsp
7330 ffd1d5e5 2018-06-23 stsp return err;
7331 9f7d7167 2018-04-29 stsp }
7332 9f7d7167 2018-04-29 stsp
7333 ffd1d5e5 2018-06-23 stsp __dead static void
7334 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7335 ffd1d5e5 2018-06-23 stsp {
7336 ffd1d5e5 2018-06-23 stsp endwin();
7337 91198554 2022-06-23 thomas fprintf(stderr,
7338 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7339 ffd1d5e5 2018-06-23 stsp getprogname());
7340 ffd1d5e5 2018-06-23 stsp exit(1);
7341 ffd1d5e5 2018-06-23 stsp }
7342 ffd1d5e5 2018-06-23 stsp
7343 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7344 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7345 ffd1d5e5 2018-06-23 stsp {
7346 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7347 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7348 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7349 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7350 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7351 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7352 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7353 4e97c21c 2020-12-06 stsp char *label = NULL;
7354 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7355 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7356 ffd1d5e5 2018-06-23 stsp int ch;
7357 5221c383 2018-08-01 stsp struct tog_view *view;
7358 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7359 70ac5f84 2019-03-28 stsp
7360 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7361 ffd1d5e5 2018-06-23 stsp switch (ch) {
7362 ffd1d5e5 2018-06-23 stsp case 'c':
7363 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7364 74283ab8 2020-02-07 stsp break;
7365 74283ab8 2020-02-07 stsp case 'r':
7366 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7367 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7368 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7369 74283ab8 2020-02-07 stsp optarg);
7370 ffd1d5e5 2018-06-23 stsp break;
7371 ffd1d5e5 2018-06-23 stsp default:
7372 e99e2d15 2020-11-24 naddy usage_tree();
7373 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7374 ffd1d5e5 2018-06-23 stsp }
7375 ffd1d5e5 2018-06-23 stsp }
7376 ffd1d5e5 2018-06-23 stsp
7377 ffd1d5e5 2018-06-23 stsp argc -= optind;
7378 ffd1d5e5 2018-06-23 stsp argv += optind;
7379 ffd1d5e5 2018-06-23 stsp
7380 55cccc34 2020-02-20 stsp if (argc > 1)
7381 e99e2d15 2020-11-24 naddy usage_tree();
7382 7cd52833 2022-06-23 thomas
7383 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7384 7cd52833 2022-06-23 thomas if (error != NULL)
7385 7cd52833 2022-06-23 thomas goto done;
7386 74283ab8 2020-02-07 stsp
7387 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7388 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7389 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7390 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7391 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7392 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7393 c156c7a4 2020-12-18 stsp goto done;
7394 55cccc34 2020-02-20 stsp if (worktree)
7395 52185f70 2019-02-05 stsp repo_path =
7396 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7397 55cccc34 2020-02-20 stsp else
7398 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7399 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7400 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7401 c156c7a4 2020-12-18 stsp goto done;
7402 c156c7a4 2020-12-18 stsp }
7403 55cccc34 2020-02-20 stsp }
7404 a915003a 2019-02-05 stsp
7405 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7406 c02c541e 2019-03-29 stsp if (error != NULL)
7407 52185f70 2019-02-05 stsp goto done;
7408 d188b9a6 2019-01-04 stsp
7409 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7410 55cccc34 2020-02-20 stsp repo, worktree);
7411 55cccc34 2020-02-20 stsp if (error)
7412 55cccc34 2020-02-20 stsp goto done;
7413 55cccc34 2020-02-20 stsp
7414 55cccc34 2020-02-20 stsp init_curses();
7415 55cccc34 2020-02-20 stsp
7416 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7417 c02c541e 2019-03-29 stsp if (error)
7418 52185f70 2019-02-05 stsp goto done;
7419 ffd1d5e5 2018-06-23 stsp
7420 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7421 51a10b52 2020-12-26 stsp if (error)
7422 51a10b52 2020-12-26 stsp goto done;
7423 51a10b52 2020-12-26 stsp
7424 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7425 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7426 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7427 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7428 4e97c21c 2020-12-06 stsp if (error)
7429 4e97c21c 2020-12-06 stsp goto done;
7430 4e97c21c 2020-12-06 stsp head_ref_name = label;
7431 4e97c21c 2020-12-06 stsp } else {
7432 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7433 4e97c21c 2020-12-06 stsp if (error == NULL)
7434 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7435 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7436 4e97c21c 2020-12-06 stsp goto done;
7437 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7438 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7439 4e97c21c 2020-12-06 stsp if (error)
7440 4e97c21c 2020-12-06 stsp goto done;
7441 4e97c21c 2020-12-06 stsp }
7442 ffd1d5e5 2018-06-23 stsp
7443 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7444 945f9229 2022-04-16 thomas if (error)
7445 945f9229 2022-04-16 thomas goto done;
7446 945f9229 2022-04-16 thomas
7447 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7448 5221c383 2018-08-01 stsp if (view == NULL) {
7449 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7450 5221c383 2018-08-01 stsp goto done;
7451 5221c383 2018-08-01 stsp }
7452 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7453 ad80ab7b 2018-08-04 stsp if (error)
7454 ad80ab7b 2018-08-04 stsp goto done;
7455 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7456 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7457 d91faf3b 2020-12-01 naddy in_repo_path);
7458 55cccc34 2020-02-20 stsp if (error)
7459 55cccc34 2020-02-20 stsp goto done;
7460 55cccc34 2020-02-20 stsp }
7461 55cccc34 2020-02-20 stsp
7462 55cccc34 2020-02-20 stsp if (worktree) {
7463 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7464 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7465 55cccc34 2020-02-20 stsp worktree = NULL;
7466 55cccc34 2020-02-20 stsp }
7467 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7468 ffd1d5e5 2018-06-23 stsp done:
7469 52185f70 2019-02-05 stsp free(repo_path);
7470 e4a0e26d 2020-02-20 stsp free(cwd);
7471 ffd1d5e5 2018-06-23 stsp free(commit_id);
7472 4e97c21c 2020-12-06 stsp free(label);
7473 486cd271 2020-12-06 stsp if (ref)
7474 486cd271 2020-12-06 stsp got_ref_close(ref);
7475 1d0f4054 2021-06-17 stsp if (repo) {
7476 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7477 1d0f4054 2021-06-17 stsp if (error == NULL)
7478 1d0f4054 2021-06-17 stsp error = close_err;
7479 1d0f4054 2021-06-17 stsp }
7480 7cd52833 2022-06-23 thomas if (pack_fds) {
7481 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7482 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7483 7cd52833 2022-06-23 thomas if (error == NULL)
7484 7cd52833 2022-06-23 thomas error = pack_err;
7485 7cd52833 2022-06-23 thomas }
7486 51a10b52 2020-12-26 stsp tog_free_refs();
7487 ffd1d5e5 2018-06-23 stsp return error;
7488 6458efa5 2020-11-24 stsp }
7489 6458efa5 2020-11-24 stsp
7490 6458efa5 2020-11-24 stsp static const struct got_error *
7491 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7492 6458efa5 2020-11-24 stsp {
7493 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7494 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7495 6458efa5 2020-11-24 stsp
7496 6458efa5 2020-11-24 stsp s->nrefs = 0;
7497 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7498 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7499 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7500 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7501 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7502 6458efa5 2020-11-24 stsp continue;
7503 6458efa5 2020-11-24 stsp
7504 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7505 6458efa5 2020-11-24 stsp if (re == NULL)
7506 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7507 6458efa5 2020-11-24 stsp
7508 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7509 8924d611 2020-12-26 stsp if (re->ref == NULL)
7510 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7511 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7512 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7513 6458efa5 2020-11-24 stsp }
7514 6458efa5 2020-11-24 stsp
7515 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7516 6458efa5 2020-11-24 stsp return NULL;
7517 6458efa5 2020-11-24 stsp }
7518 6458efa5 2020-11-24 stsp
7519 ef20f542 2022-06-26 thomas static void
7520 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7521 6458efa5 2020-11-24 stsp {
7522 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7523 6458efa5 2020-11-24 stsp
7524 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7525 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7526 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7527 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7528 6458efa5 2020-11-24 stsp free(re);
7529 6458efa5 2020-11-24 stsp }
7530 6458efa5 2020-11-24 stsp }
7531 6458efa5 2020-11-24 stsp
7532 6458efa5 2020-11-24 stsp static const struct got_error *
7533 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7534 6458efa5 2020-11-24 stsp {
7535 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7536 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7539 6458efa5 2020-11-24 stsp s->repo = repo;
7540 6458efa5 2020-11-24 stsp
7541 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7542 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7543 6458efa5 2020-11-24 stsp
7544 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7545 6458efa5 2020-11-24 stsp if (err)
7546 6458efa5 2020-11-24 stsp return err;
7547 34ba6917 2020-11-30 stsp
7548 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7549 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7550 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7551 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7552 6458efa5 2020-11-24 stsp if (err)
7553 6458efa5 2020-11-24 stsp goto done;
7554 6458efa5 2020-11-24 stsp
7555 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7556 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7557 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7558 6458efa5 2020-11-24 stsp if (err)
7559 6458efa5 2020-11-24 stsp goto done;
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7562 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7563 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7564 2183bbf6 2022-01-23 thomas if (err)
7565 2183bbf6 2022-01-23 thomas goto done;
7566 2183bbf6 2022-01-23 thomas
7567 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7568 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7569 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7570 6458efa5 2020-11-24 stsp if (err)
7571 6458efa5 2020-11-24 stsp goto done;
7572 6458efa5 2020-11-24 stsp }
7573 6458efa5 2020-11-24 stsp
7574 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7575 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7576 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7577 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7578 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7579 6458efa5 2020-11-24 stsp done:
7580 6458efa5 2020-11-24 stsp if (err)
7581 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7582 6458efa5 2020-11-24 stsp return err;
7583 6458efa5 2020-11-24 stsp }
7584 6458efa5 2020-11-24 stsp
7585 6458efa5 2020-11-24 stsp static const struct got_error *
7586 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7587 6458efa5 2020-11-24 stsp {
7588 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7589 6458efa5 2020-11-24 stsp
7590 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7591 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7592 6458efa5 2020-11-24 stsp
7593 6458efa5 2020-11-24 stsp return NULL;
7594 9f7d7167 2018-04-29 stsp }
7595 ce5b7c56 2019-07-09 stsp
7596 6458efa5 2020-11-24 stsp static const struct got_error *
7597 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7598 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7599 6458efa5 2020-11-24 stsp {
7600 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7601 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7602 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7603 6458efa5 2020-11-24 stsp int obj_type;
7604 6458efa5 2020-11-24 stsp
7605 c42c9805 2020-11-24 stsp *commit_id = NULL;
7606 6458efa5 2020-11-24 stsp
7607 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7608 6458efa5 2020-11-24 stsp if (err)
7609 6458efa5 2020-11-24 stsp return err;
7610 6458efa5 2020-11-24 stsp
7611 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7612 6458efa5 2020-11-24 stsp if (err)
7613 6458efa5 2020-11-24 stsp goto done;
7614 6458efa5 2020-11-24 stsp
7615 6458efa5 2020-11-24 stsp switch (obj_type) {
7616 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7617 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7618 6458efa5 2020-11-24 stsp break;
7619 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7620 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7621 6458efa5 2020-11-24 stsp if (err)
7622 6458efa5 2020-11-24 stsp goto done;
7623 c42c9805 2020-11-24 stsp free(obj_id);
7624 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7625 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7626 c42c9805 2020-11-24 stsp if (err)
7627 6458efa5 2020-11-24 stsp goto done;
7628 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7629 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7630 c42c9805 2020-11-24 stsp goto done;
7631 c42c9805 2020-11-24 stsp }
7632 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7633 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7634 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7635 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7636 c42c9805 2020-11-24 stsp goto done;
7637 c42c9805 2020-11-24 stsp }
7638 6458efa5 2020-11-24 stsp break;
7639 6458efa5 2020-11-24 stsp default:
7640 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7641 c42c9805 2020-11-24 stsp break;
7642 6458efa5 2020-11-24 stsp }
7643 6458efa5 2020-11-24 stsp
7644 c42c9805 2020-11-24 stsp done:
7645 c42c9805 2020-11-24 stsp if (tag)
7646 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7647 c42c9805 2020-11-24 stsp if (err) {
7648 c42c9805 2020-11-24 stsp free(*commit_id);
7649 c42c9805 2020-11-24 stsp *commit_id = NULL;
7650 c42c9805 2020-11-24 stsp }
7651 c42c9805 2020-11-24 stsp return err;
7652 c42c9805 2020-11-24 stsp }
7653 c42c9805 2020-11-24 stsp
7654 c42c9805 2020-11-24 stsp static const struct got_error *
7655 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7656 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7657 c42c9805 2020-11-24 stsp {
7658 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7659 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7660 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7661 c42c9805 2020-11-24 stsp
7662 c42c9805 2020-11-24 stsp *new_view = NULL;
7663 c42c9805 2020-11-24 stsp
7664 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7665 c42c9805 2020-11-24 stsp if (err) {
7666 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7667 c42c9805 2020-11-24 stsp return err;
7668 c42c9805 2020-11-24 stsp else
7669 c42c9805 2020-11-24 stsp return NULL;
7670 c42c9805 2020-11-24 stsp }
7671 c42c9805 2020-11-24 stsp
7672 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7673 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7674 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7675 6458efa5 2020-11-24 stsp goto done;
7676 6458efa5 2020-11-24 stsp }
7677 6458efa5 2020-11-24 stsp
7678 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7679 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7680 6458efa5 2020-11-24 stsp done:
7681 6458efa5 2020-11-24 stsp if (err)
7682 6458efa5 2020-11-24 stsp view_close(log_view);
7683 6458efa5 2020-11-24 stsp else
7684 6458efa5 2020-11-24 stsp *new_view = log_view;
7685 c42c9805 2020-11-24 stsp free(commit_id);
7686 6458efa5 2020-11-24 stsp return err;
7687 6458efa5 2020-11-24 stsp }
7688 6458efa5 2020-11-24 stsp
7689 ce5b7c56 2019-07-09 stsp static void
7690 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7691 6458efa5 2020-11-24 stsp {
7692 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7693 34ba6917 2020-11-30 stsp int i = 0;
7694 6458efa5 2020-11-24 stsp
7695 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7696 6458efa5 2020-11-24 stsp return;
7697 6458efa5 2020-11-24 stsp
7698 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7699 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7700 34ba6917 2020-11-30 stsp if (re == NULL)
7701 34ba6917 2020-11-30 stsp break;
7702 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7703 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7704 6458efa5 2020-11-24 stsp }
7705 6458efa5 2020-11-24 stsp }
7706 6458efa5 2020-11-24 stsp
7707 a5d43cac 2022-07-01 thomas static const struct got_error *
7708 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7709 6458efa5 2020-11-24 stsp {
7710 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7711 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7712 6458efa5 2020-11-24 stsp int n = 0;
7713 6458efa5 2020-11-24 stsp
7714 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7715 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7716 6458efa5 2020-11-24 stsp else
7717 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7718 6458efa5 2020-11-24 stsp
7719 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7720 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7721 07dd3ed3 2022-08-06 thomas if (last) {
7722 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7723 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7724 07dd3ed3 2022-08-06 thomas }
7725 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7726 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7727 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7728 6458efa5 2020-11-24 stsp }
7729 6458efa5 2020-11-24 stsp }
7730 a5d43cac 2022-07-01 thomas
7731 a5d43cac 2022-07-01 thomas return NULL;
7732 6458efa5 2020-11-24 stsp }
7733 6458efa5 2020-11-24 stsp
7734 6458efa5 2020-11-24 stsp static const struct got_error *
7735 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7736 6458efa5 2020-11-24 stsp {
7737 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7738 6458efa5 2020-11-24 stsp
7739 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7740 6458efa5 2020-11-24 stsp return NULL;
7741 6458efa5 2020-11-24 stsp }
7742 6458efa5 2020-11-24 stsp
7743 6458efa5 2020-11-24 stsp static int
7744 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7745 6458efa5 2020-11-24 stsp {
7746 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7747 6458efa5 2020-11-24 stsp
7748 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7749 6458efa5 2020-11-24 stsp 0) == 0;
7750 6458efa5 2020-11-24 stsp }
7751 6458efa5 2020-11-24 stsp
7752 6458efa5 2020-11-24 stsp static const struct got_error *
7753 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7754 6458efa5 2020-11-24 stsp {
7755 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7756 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7757 6458efa5 2020-11-24 stsp
7758 6458efa5 2020-11-24 stsp if (!view->searching) {
7759 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7760 6458efa5 2020-11-24 stsp return NULL;
7761 6458efa5 2020-11-24 stsp }
7762 6458efa5 2020-11-24 stsp
7763 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7764 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7765 6458efa5 2020-11-24 stsp if (s->selected_entry)
7766 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7767 6458efa5 2020-11-24 stsp else
7768 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7769 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7770 6458efa5 2020-11-24 stsp } else {
7771 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7772 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7773 6458efa5 2020-11-24 stsp else
7774 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7775 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7776 6458efa5 2020-11-24 stsp }
7777 6458efa5 2020-11-24 stsp } else {
7778 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7779 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7780 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7781 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7782 6458efa5 2020-11-24 stsp else
7783 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7784 6458efa5 2020-11-24 stsp }
7785 6458efa5 2020-11-24 stsp
7786 6458efa5 2020-11-24 stsp while (1) {
7787 6458efa5 2020-11-24 stsp if (re == NULL) {
7788 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7789 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7790 6458efa5 2020-11-24 stsp return NULL;
7791 6458efa5 2020-11-24 stsp }
7792 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7793 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7794 6458efa5 2020-11-24 stsp else
7795 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7796 6458efa5 2020-11-24 stsp }
7797 6458efa5 2020-11-24 stsp
7798 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7799 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7800 6458efa5 2020-11-24 stsp s->matched_entry = re;
7801 6458efa5 2020-11-24 stsp break;
7802 6458efa5 2020-11-24 stsp }
7803 6458efa5 2020-11-24 stsp
7804 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7805 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7806 6458efa5 2020-11-24 stsp else
7807 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7808 6458efa5 2020-11-24 stsp }
7809 6458efa5 2020-11-24 stsp
7810 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7811 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7812 6458efa5 2020-11-24 stsp s->selected = 0;
7813 6458efa5 2020-11-24 stsp }
7814 6458efa5 2020-11-24 stsp
7815 6458efa5 2020-11-24 stsp return NULL;
7816 6458efa5 2020-11-24 stsp }
7817 6458efa5 2020-11-24 stsp
7818 6458efa5 2020-11-24 stsp static const struct got_error *
7819 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7820 6458efa5 2020-11-24 stsp {
7821 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7822 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7823 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7824 6458efa5 2020-11-24 stsp char *line = NULL;
7825 6458efa5 2020-11-24 stsp wchar_t *wline;
7826 6458efa5 2020-11-24 stsp struct tog_color *tc;
7827 6458efa5 2020-11-24 stsp int width, n;
7828 6458efa5 2020-11-24 stsp int limit = view->nlines;
7829 6458efa5 2020-11-24 stsp
7830 6458efa5 2020-11-24 stsp werase(view->window);
7831 6458efa5 2020-11-24 stsp
7832 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7833 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7834 a5d43cac 2022-07-01 thomas --limit; /* border */
7835 6458efa5 2020-11-24 stsp
7836 6458efa5 2020-11-24 stsp if (limit == 0)
7837 6458efa5 2020-11-24 stsp return NULL;
7838 6458efa5 2020-11-24 stsp
7839 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7840 6458efa5 2020-11-24 stsp
7841 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7842 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7843 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7844 6458efa5 2020-11-24 stsp
7845 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7846 6458efa5 2020-11-24 stsp if (err) {
7847 6458efa5 2020-11-24 stsp free(line);
7848 6458efa5 2020-11-24 stsp return err;
7849 6458efa5 2020-11-24 stsp }
7850 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7851 6458efa5 2020-11-24 stsp wstandout(view->window);
7852 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7853 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7854 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7855 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7856 6458efa5 2020-11-24 stsp wstandend(view->window);
7857 6458efa5 2020-11-24 stsp free(wline);
7858 6458efa5 2020-11-24 stsp wline = NULL;
7859 6458efa5 2020-11-24 stsp free(line);
7860 6458efa5 2020-11-24 stsp line = NULL;
7861 6458efa5 2020-11-24 stsp if (--limit <= 0)
7862 6458efa5 2020-11-24 stsp return NULL;
7863 6458efa5 2020-11-24 stsp
7864 6458efa5 2020-11-24 stsp n = 0;
7865 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7866 6458efa5 2020-11-24 stsp char *line = NULL;
7867 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7868 6458efa5 2020-11-24 stsp
7869 84227eb1 2022-06-23 thomas if (s->show_date) {
7870 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7871 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7872 84227eb1 2022-06-23 thomas struct got_object_id *id;
7873 84227eb1 2022-06-23 thomas struct tm tm;
7874 84227eb1 2022-06-23 thomas time_t t;
7875 84227eb1 2022-06-23 thomas
7876 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7877 84227eb1 2022-06-23 thomas if (err)
7878 84227eb1 2022-06-23 thomas return err;
7879 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7880 84227eb1 2022-06-23 thomas if (err) {
7881 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7882 84227eb1 2022-06-23 thomas free(id);
7883 84227eb1 2022-06-23 thomas return err;
7884 84227eb1 2022-06-23 thomas }
7885 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7886 84227eb1 2022-06-23 thomas id);
7887 84227eb1 2022-06-23 thomas if (err) {
7888 84227eb1 2022-06-23 thomas free(id);
7889 84227eb1 2022-06-23 thomas return err;
7890 84227eb1 2022-06-23 thomas }
7891 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7892 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7893 84227eb1 2022-06-23 thomas } else {
7894 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7895 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7896 84227eb1 2022-06-23 thomas }
7897 84227eb1 2022-06-23 thomas free(id);
7898 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7899 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7900 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7901 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7902 84227eb1 2022-06-23 thomas }
7903 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7904 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7905 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7906 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7907 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7908 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7909 6458efa5 2020-11-24 stsp struct got_object_id *id;
7910 6458efa5 2020-11-24 stsp char *id_str;
7911 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7912 6458efa5 2020-11-24 stsp if (err)
7913 6458efa5 2020-11-24 stsp return err;
7914 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7915 6458efa5 2020-11-24 stsp if (err) {
7916 6458efa5 2020-11-24 stsp free(id);
7917 6458efa5 2020-11-24 stsp return err;
7918 6458efa5 2020-11-24 stsp }
7919 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7920 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7921 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7922 6458efa5 2020-11-24 stsp free(id);
7923 6458efa5 2020-11-24 stsp free(id_str);
7924 6458efa5 2020-11-24 stsp return err;
7925 6458efa5 2020-11-24 stsp }
7926 6458efa5 2020-11-24 stsp free(id);
7927 6458efa5 2020-11-24 stsp free(id_str);
7928 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7929 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7930 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7931 6458efa5 2020-11-24 stsp
7932 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7933 f91a2b48 2022-06-23 thomas 0, 0);
7934 6458efa5 2020-11-24 stsp if (err) {
7935 6458efa5 2020-11-24 stsp free(line);
7936 6458efa5 2020-11-24 stsp return err;
7937 6458efa5 2020-11-24 stsp }
7938 6458efa5 2020-11-24 stsp if (n == s->selected) {
7939 6458efa5 2020-11-24 stsp if (view->focussed)
7940 6458efa5 2020-11-24 stsp wstandout(view->window);
7941 6458efa5 2020-11-24 stsp s->selected_entry = re;
7942 6458efa5 2020-11-24 stsp }
7943 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7944 6458efa5 2020-11-24 stsp if (tc)
7945 6458efa5 2020-11-24 stsp wattr_on(view->window,
7946 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7947 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7948 6458efa5 2020-11-24 stsp if (tc)
7949 6458efa5 2020-11-24 stsp wattr_off(view->window,
7950 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7951 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7952 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7953 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7954 6458efa5 2020-11-24 stsp wstandend(view->window);
7955 6458efa5 2020-11-24 stsp free(line);
7956 6458efa5 2020-11-24 stsp free(wline);
7957 6458efa5 2020-11-24 stsp wline = NULL;
7958 6458efa5 2020-11-24 stsp n++;
7959 6458efa5 2020-11-24 stsp s->ndisplayed++;
7960 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7961 6458efa5 2020-11-24 stsp
7962 6458efa5 2020-11-24 stsp limit--;
7963 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7964 6458efa5 2020-11-24 stsp }
7965 6458efa5 2020-11-24 stsp
7966 a5d43cac 2022-07-01 thomas view_border(view);
7967 6458efa5 2020-11-24 stsp return err;
7968 6458efa5 2020-11-24 stsp }
7969 6458efa5 2020-11-24 stsp
7970 6458efa5 2020-11-24 stsp static const struct got_error *
7971 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7972 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7973 c42c9805 2020-11-24 stsp {
7974 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7975 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7976 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7977 c42c9805 2020-11-24 stsp
7978 c42c9805 2020-11-24 stsp *new_view = NULL;
7979 c42c9805 2020-11-24 stsp
7980 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7981 c42c9805 2020-11-24 stsp if (err) {
7982 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7983 c42c9805 2020-11-24 stsp return err;
7984 c42c9805 2020-11-24 stsp else
7985 c42c9805 2020-11-24 stsp return NULL;
7986 c42c9805 2020-11-24 stsp }
7987 c42c9805 2020-11-24 stsp
7988 c42c9805 2020-11-24 stsp
7989 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7990 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7991 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7992 c42c9805 2020-11-24 stsp goto done;
7993 c42c9805 2020-11-24 stsp }
7994 c42c9805 2020-11-24 stsp
7995 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7996 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7997 c42c9805 2020-11-24 stsp if (err)
7998 c42c9805 2020-11-24 stsp goto done;
7999 c42c9805 2020-11-24 stsp
8000 c42c9805 2020-11-24 stsp *new_view = tree_view;
8001 c42c9805 2020-11-24 stsp done:
8002 c42c9805 2020-11-24 stsp free(commit_id);
8003 c42c9805 2020-11-24 stsp return err;
8004 07dd3ed3 2022-08-06 thomas }
8005 07dd3ed3 2022-08-06 thomas
8006 07dd3ed3 2022-08-06 thomas static const struct got_error *
8007 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8008 07dd3ed3 2022-08-06 thomas {
8009 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8010 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8011 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8012 07dd3ed3 2022-08-06 thomas
8013 07dd3ed3 2022-08-06 thomas g = view->gline;
8014 07dd3ed3 2022-08-06 thomas view->gline = 0;
8015 07dd3ed3 2022-08-06 thomas
8016 07dd3ed3 2022-08-06 thomas if (g == 0)
8017 07dd3ed3 2022-08-06 thomas g = 1;
8018 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8019 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8020 07dd3ed3 2022-08-06 thomas
8021 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8022 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8023 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8024 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8025 07dd3ed3 2022-08-06 thomas return NULL;
8026 07dd3ed3 2022-08-06 thomas }
8027 07dd3ed3 2022-08-06 thomas
8028 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8029 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8030 07dd3ed3 2022-08-06 thomas if (err)
8031 07dd3ed3 2022-08-06 thomas return err;
8032 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8033 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8034 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8035 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8036 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8037 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8038 07dd3ed3 2022-08-06 thomas
8039 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8040 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8041 07dd3ed3 2022-08-06 thomas
8042 07dd3ed3 2022-08-06 thomas return NULL;
8043 07dd3ed3 2022-08-06 thomas
8044 c42c9805 2020-11-24 stsp }
8045 07dd3ed3 2022-08-06 thomas
8046 c42c9805 2020-11-24 stsp static const struct got_error *
8047 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8048 6458efa5 2020-11-24 stsp {
8049 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8050 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8051 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8052 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8053 6458efa5 2020-11-24 stsp
8054 07dd3ed3 2022-08-06 thomas if (view->gline)
8055 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8056 07dd3ed3 2022-08-06 thomas
8057 6458efa5 2020-11-24 stsp switch (ch) {
8058 6458efa5 2020-11-24 stsp case 'i':
8059 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8060 07b0611c 2022-06-23 thomas view->count = 0;
8061 3bfadbd4 2021-11-20 thomas break;
8062 84227eb1 2022-06-23 thomas case 'm':
8063 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8064 07b0611c 2022-06-23 thomas view->count = 0;
8065 84227eb1 2022-06-23 thomas break;
8066 98182bd0 2021-11-20 thomas case 'o':
8067 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8068 07b0611c 2022-06-23 thomas view->count = 0;
8069 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8070 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8071 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8072 c591440f 2021-11-20 thomas if (err)
8073 c591440f 2021-11-20 thomas break;
8074 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8075 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8076 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8077 3bfadbd4 2021-11-20 thomas if (err)
8078 3bfadbd4 2021-11-20 thomas break;
8079 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8080 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8081 6458efa5 2020-11-24 stsp break;
8082 6458efa5 2020-11-24 stsp case KEY_ENTER:
8083 6458efa5 2020-11-24 stsp case '\r':
8084 07b0611c 2022-06-23 thomas view->count = 0;
8085 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8086 a5d43cac 2022-07-01 thomas break;
8087 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8088 6458efa5 2020-11-24 stsp break;
8089 1be4947a 2022-07-22 thomas case 'T':
8090 07b0611c 2022-06-23 thomas view->count = 0;
8091 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8092 c42c9805 2020-11-24 stsp break;
8093 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8094 c42c9805 2020-11-24 stsp break;
8095 e4526bf5 2021-09-03 naddy case 'g':
8096 e4526bf5 2021-09-03 naddy case KEY_HOME:
8097 e4526bf5 2021-09-03 naddy s->selected = 0;
8098 07b0611c 2022-06-23 thomas view->count = 0;
8099 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8100 e4526bf5 2021-09-03 naddy break;
8101 e4526bf5 2021-09-03 naddy case 'G':
8102 a5d43cac 2022-07-01 thomas case KEY_END: {
8103 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8104 a5d43cac 2022-07-01 thomas
8105 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8106 a5d43cac 2022-07-01 thomas --eos; /* border */
8107 e4526bf5 2021-09-03 naddy s->selected = 0;
8108 07b0611c 2022-06-23 thomas view->count = 0;
8109 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8110 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8111 e4526bf5 2021-09-03 naddy if (re == NULL)
8112 e4526bf5 2021-09-03 naddy break;
8113 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8114 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8115 e4526bf5 2021-09-03 naddy }
8116 e4526bf5 2021-09-03 naddy if (n > 0)
8117 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8118 e4526bf5 2021-09-03 naddy break;
8119 a5d43cac 2022-07-01 thomas }
8120 6458efa5 2020-11-24 stsp case 'k':
8121 6458efa5 2020-11-24 stsp case KEY_UP:
8122 f7140bf5 2021-10-17 thomas case CTRL('p'):
8123 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8124 6458efa5 2020-11-24 stsp s->selected--;
8125 6458efa5 2020-11-24 stsp break;
8126 34ba6917 2020-11-30 stsp }
8127 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8128 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8129 07b0611c 2022-06-23 thomas view->count = 0;
8130 6458efa5 2020-11-24 stsp break;
8131 70f17a53 2022-06-13 thomas case CTRL('u'):
8132 23427b14 2022-06-23 thomas case 'u':
8133 70f17a53 2022-06-13 thomas nscroll /= 2;
8134 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8135 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8136 6458efa5 2020-11-24 stsp case CTRL('b'):
8137 1c5e5faa 2022-06-23 thomas case 'b':
8138 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8139 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8140 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8141 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8142 07b0611c 2022-06-23 thomas view->count = 0;
8143 6458efa5 2020-11-24 stsp break;
8144 6458efa5 2020-11-24 stsp case 'j':
8145 6458efa5 2020-11-24 stsp case KEY_DOWN:
8146 f7140bf5 2021-10-17 thomas case CTRL('n'):
8147 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8148 6458efa5 2020-11-24 stsp s->selected++;
8149 6458efa5 2020-11-24 stsp break;
8150 6458efa5 2020-11-24 stsp }
8151 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8152 6458efa5 2020-11-24 stsp /* can't scroll any further */
8153 07b0611c 2022-06-23 thomas view->count = 0;
8154 6458efa5 2020-11-24 stsp break;
8155 07b0611c 2022-06-23 thomas }
8156 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8157 6458efa5 2020-11-24 stsp break;
8158 70f17a53 2022-06-13 thomas case CTRL('d'):
8159 23427b14 2022-06-23 thomas case 'd':
8160 70f17a53 2022-06-13 thomas nscroll /= 2;
8161 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8162 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8163 6458efa5 2020-11-24 stsp case CTRL('f'):
8164 1c5e5faa 2022-06-23 thomas case 'f':
8165 4c2d69cb 2022-06-23 thomas case ' ':
8166 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8167 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8168 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8169 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8170 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8171 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8172 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8173 07b0611c 2022-06-23 thomas view->count = 0;
8174 6458efa5 2020-11-24 stsp break;
8175 6458efa5 2020-11-24 stsp }
8176 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8177 6458efa5 2020-11-24 stsp break;
8178 6458efa5 2020-11-24 stsp case CTRL('l'):
8179 07b0611c 2022-06-23 thomas view->count = 0;
8180 8924d611 2020-12-26 stsp tog_free_refs();
8181 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8182 8924d611 2020-12-26 stsp if (err)
8183 8924d611 2020-12-26 stsp break;
8184 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8185 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8186 6458efa5 2020-11-24 stsp break;
8187 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8188 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8189 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8190 6458efa5 2020-11-24 stsp break;
8191 6458efa5 2020-11-24 stsp default:
8192 07b0611c 2022-06-23 thomas view->count = 0;
8193 6458efa5 2020-11-24 stsp break;
8194 6458efa5 2020-11-24 stsp }
8195 6458efa5 2020-11-24 stsp
8196 6458efa5 2020-11-24 stsp return err;
8197 6458efa5 2020-11-24 stsp }
8198 6458efa5 2020-11-24 stsp
8199 6458efa5 2020-11-24 stsp __dead static void
8200 6458efa5 2020-11-24 stsp usage_ref(void)
8201 6458efa5 2020-11-24 stsp {
8202 6458efa5 2020-11-24 stsp endwin();
8203 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8204 6458efa5 2020-11-24 stsp getprogname());
8205 6458efa5 2020-11-24 stsp exit(1);
8206 6458efa5 2020-11-24 stsp }
8207 6458efa5 2020-11-24 stsp
8208 6458efa5 2020-11-24 stsp static const struct got_error *
8209 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8210 6458efa5 2020-11-24 stsp {
8211 6458efa5 2020-11-24 stsp const struct got_error *error;
8212 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8213 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8214 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8215 6458efa5 2020-11-24 stsp int ch;
8216 6458efa5 2020-11-24 stsp struct tog_view *view;
8217 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8218 6458efa5 2020-11-24 stsp
8219 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8220 6458efa5 2020-11-24 stsp switch (ch) {
8221 6458efa5 2020-11-24 stsp case 'r':
8222 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8223 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8224 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8225 6458efa5 2020-11-24 stsp optarg);
8226 6458efa5 2020-11-24 stsp break;
8227 6458efa5 2020-11-24 stsp default:
8228 e99e2d15 2020-11-24 naddy usage_ref();
8229 6458efa5 2020-11-24 stsp /* NOTREACHED */
8230 6458efa5 2020-11-24 stsp }
8231 6458efa5 2020-11-24 stsp }
8232 6458efa5 2020-11-24 stsp
8233 6458efa5 2020-11-24 stsp argc -= optind;
8234 6458efa5 2020-11-24 stsp argv += optind;
8235 6458efa5 2020-11-24 stsp
8236 6458efa5 2020-11-24 stsp if (argc > 1)
8237 e99e2d15 2020-11-24 naddy usage_ref();
8238 7cd52833 2022-06-23 thomas
8239 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8240 7cd52833 2022-06-23 thomas if (error != NULL)
8241 7cd52833 2022-06-23 thomas goto done;
8242 6458efa5 2020-11-24 stsp
8243 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8244 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8245 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8246 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8247 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8248 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8249 c156c7a4 2020-12-18 stsp goto done;
8250 6458efa5 2020-11-24 stsp if (worktree)
8251 6458efa5 2020-11-24 stsp repo_path =
8252 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8253 6458efa5 2020-11-24 stsp else
8254 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8255 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8256 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8257 c156c7a4 2020-12-18 stsp goto done;
8258 c156c7a4 2020-12-18 stsp }
8259 6458efa5 2020-11-24 stsp }
8260 6458efa5 2020-11-24 stsp
8261 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8262 6458efa5 2020-11-24 stsp if (error != NULL)
8263 6458efa5 2020-11-24 stsp goto done;
8264 6458efa5 2020-11-24 stsp
8265 6458efa5 2020-11-24 stsp init_curses();
8266 6458efa5 2020-11-24 stsp
8267 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8268 51a10b52 2020-12-26 stsp if (error)
8269 51a10b52 2020-12-26 stsp goto done;
8270 51a10b52 2020-12-26 stsp
8271 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8272 6458efa5 2020-11-24 stsp if (error)
8273 6458efa5 2020-11-24 stsp goto done;
8274 6458efa5 2020-11-24 stsp
8275 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8276 6458efa5 2020-11-24 stsp if (view == NULL) {
8277 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8278 6458efa5 2020-11-24 stsp goto done;
8279 6458efa5 2020-11-24 stsp }
8280 6458efa5 2020-11-24 stsp
8281 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8282 6458efa5 2020-11-24 stsp if (error)
8283 6458efa5 2020-11-24 stsp goto done;
8284 6458efa5 2020-11-24 stsp
8285 6458efa5 2020-11-24 stsp if (worktree) {
8286 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8287 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8288 6458efa5 2020-11-24 stsp worktree = NULL;
8289 6458efa5 2020-11-24 stsp }
8290 6458efa5 2020-11-24 stsp error = view_loop(view);
8291 6458efa5 2020-11-24 stsp done:
8292 6458efa5 2020-11-24 stsp free(repo_path);
8293 6458efa5 2020-11-24 stsp free(cwd);
8294 1d0f4054 2021-06-17 stsp if (repo) {
8295 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8296 1d0f4054 2021-06-17 stsp if (close_err)
8297 1d0f4054 2021-06-17 stsp error = close_err;
8298 1d0f4054 2021-06-17 stsp }
8299 7cd52833 2022-06-23 thomas if (pack_fds) {
8300 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8301 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8302 7cd52833 2022-06-23 thomas if (error == NULL)
8303 7cd52833 2022-06-23 thomas error = pack_err;
8304 7cd52833 2022-06-23 thomas }
8305 51a10b52 2020-12-26 stsp tog_free_refs();
8306 6458efa5 2020-11-24 stsp return error;
8307 6458efa5 2020-11-24 stsp }
8308 6458efa5 2020-11-24 stsp
8309 2a31b33b 2022-07-23 thomas static const struct got_error *
8310 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8311 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8312 2a31b33b 2022-07-23 thomas {
8313 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8314 2a31b33b 2022-07-23 thomas
8315 2a31b33b 2022-07-23 thomas *new_view = NULL;
8316 2a31b33b 2022-07-23 thomas
8317 2a31b33b 2022-07-23 thomas switch (request) {
8318 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8319 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8320 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8321 2a31b33b 2022-07-23 thomas
8322 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8323 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8324 2a31b33b 2022-07-23 thomas view, s->repo);
8325 2a31b33b 2022-07-23 thomas } else
8326 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8327 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8328 2a31b33b 2022-07-23 thomas break;
8329 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8330 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8331 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8332 2a31b33b 2022-07-23 thomas
8333 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8334 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8335 2a31b33b 2022-07-23 thomas s->repo);
8336 2a31b33b 2022-07-23 thomas } else
8337 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8338 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8339 2a31b33b 2022-07-23 thomas break;
8340 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8341 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8342 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8343 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8344 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8345 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8346 2a31b33b 2022-07-23 thomas &view->state.tree);
8347 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8348 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8349 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8350 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8351 2a31b33b 2022-07-23 thomas else
8352 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8353 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8354 2a31b33b 2022-07-23 thomas break;
8355 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8356 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8357 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8358 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8359 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8360 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8361 2a31b33b 2022-07-23 thomas view->state.log.repo);
8362 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8363 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8364 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8365 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8366 2a31b33b 2022-07-23 thomas else
8367 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8368 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8369 2a31b33b 2022-07-23 thomas break;
8370 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8371 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8372 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8373 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8374 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8375 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8376 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8377 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8378 2a31b33b 2022-07-23 thomas else
8379 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8380 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8381 2a31b33b 2022-07-23 thomas if (err)
8382 2a31b33b 2022-07-23 thomas view_close(*new_view);
8383 2a31b33b 2022-07-23 thomas break;
8384 2a31b33b 2022-07-23 thomas default:
8385 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8386 2a31b33b 2022-07-23 thomas }
8387 2a31b33b 2022-07-23 thomas
8388 2a31b33b 2022-07-23 thomas return err;
8389 2a31b33b 2022-07-23 thomas }
8390 2a31b33b 2022-07-23 thomas
8391 a5d43cac 2022-07-01 thomas /*
8392 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8393 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8394 a5d43cac 2022-07-01 thomas */
8395 6458efa5 2020-11-24 stsp static void
8396 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8397 a5d43cac 2022-07-01 thomas {
8398 a5d43cac 2022-07-01 thomas switch (view->type) {
8399 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8400 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8401 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8402 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8403 a5d43cac 2022-07-01 thomas 1);
8404 a5d43cac 2022-07-01 thomas break;
8405 a5d43cac 2022-07-01 thomas }
8406 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8407 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8408 a5d43cac 2022-07-01 thomas else
8409 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8410 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8411 a5d43cac 2022-07-01 thomas break;
8412 a5d43cac 2022-07-01 thomas }
8413 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8414 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8415 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8416 a5d43cac 2022-07-01 thomas break;
8417 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8418 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8419 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8420 a5d43cac 2022-07-01 thomas break;
8421 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8422 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8423 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8424 a5d43cac 2022-07-01 thomas break;
8425 a5d43cac 2022-07-01 thomas default:
8426 a5d43cac 2022-07-01 thomas break;
8427 a5d43cac 2022-07-01 thomas }
8428 a5d43cac 2022-07-01 thomas
8429 a5d43cac 2022-07-01 thomas view->offset = 0;
8430 a5d43cac 2022-07-01 thomas }
8431 a5d43cac 2022-07-01 thomas
8432 a5d43cac 2022-07-01 thomas /*
8433 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8434 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8435 a5d43cac 2022-07-01 thomas */
8436 a5d43cac 2022-07-01 thomas static const struct got_error *
8437 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8438 a5d43cac 2022-07-01 thomas {
8439 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8440 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8441 a5d43cac 2022-07-01 thomas int *selected = NULL;
8442 a5d43cac 2022-07-01 thomas int header, offset;
8443 a5d43cac 2022-07-01 thomas
8444 a5d43cac 2022-07-01 thomas switch (view->type) {
8445 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8446 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8447 a5d43cac 2022-07-01 thomas header = 3;
8448 a5d43cac 2022-07-01 thomas scrolld = NULL;
8449 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8450 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8451 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8452 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8453 a5d43cac 2022-07-01 thomas view->offset = offset;
8454 a5d43cac 2022-07-01 thomas }
8455 a5d43cac 2022-07-01 thomas break;
8456 a5d43cac 2022-07-01 thomas }
8457 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8458 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8459 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8460 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8461 a5d43cac 2022-07-01 thomas selected = &s->selected;
8462 a5d43cac 2022-07-01 thomas break;
8463 a5d43cac 2022-07-01 thomas }
8464 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8465 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8466 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8467 a5d43cac 2022-07-01 thomas header = 3;
8468 a5d43cac 2022-07-01 thomas selected = &s->selected;
8469 a5d43cac 2022-07-01 thomas break;
8470 a5d43cac 2022-07-01 thomas }
8471 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8472 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8473 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8474 a5d43cac 2022-07-01 thomas header = 5;
8475 a5d43cac 2022-07-01 thomas selected = &s->selected;
8476 a5d43cac 2022-07-01 thomas break;
8477 a5d43cac 2022-07-01 thomas }
8478 a5d43cac 2022-07-01 thomas default:
8479 a5d43cac 2022-07-01 thomas selected = NULL;
8480 a5d43cac 2022-07-01 thomas scrolld = NULL;
8481 a5d43cac 2022-07-01 thomas header = 0;
8482 a5d43cac 2022-07-01 thomas break;
8483 a5d43cac 2022-07-01 thomas }
8484 a5d43cac 2022-07-01 thomas
8485 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8486 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8487 a5d43cac 2022-07-01 thomas view->offset = offset;
8488 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8489 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8490 a5d43cac 2022-07-01 thomas *selected -= offset;
8491 a5d43cac 2022-07-01 thomas }
8492 a5d43cac 2022-07-01 thomas }
8493 a5d43cac 2022-07-01 thomas
8494 a5d43cac 2022-07-01 thomas return err;
8495 a5d43cac 2022-07-01 thomas }
8496 a5d43cac 2022-07-01 thomas
8497 a5d43cac 2022-07-01 thomas static void
8498 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8499 ce5b7c56 2019-07-09 stsp {
8500 6059809a 2020-12-17 stsp size_t i;
8501 9f7d7167 2018-04-29 stsp
8502 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8503 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8504 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8505 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8506 ce5b7c56 2019-07-09 stsp }
8507 6879ba42 2020-10-01 naddy fputc('\n', fp);
8508 ce5b7c56 2019-07-09 stsp }
8509 ce5b7c56 2019-07-09 stsp
8510 4ed7e80c 2018-05-20 stsp __dead static void
8511 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8512 9f7d7167 2018-04-29 stsp {
8513 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8514 6879ba42 2020-10-01 naddy
8515 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8516 6879ba42 2020-10-01 naddy getprogname());
8517 6879ba42 2020-10-01 naddy if (hflag) {
8518 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8519 6879ba42 2020-10-01 naddy list_commands(fp);
8520 ee85c5e8 2020-02-29 stsp }
8521 6879ba42 2020-10-01 naddy exit(status);
8522 9f7d7167 2018-04-29 stsp }
8523 9f7d7167 2018-04-29 stsp
8524 c2301be8 2018-04-30 stsp static char **
8525 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8526 c2301be8 2018-04-30 stsp {
8527 ee85c5e8 2020-02-29 stsp va_list ap;
8528 c2301be8 2018-04-30 stsp char **argv;
8529 ee85c5e8 2020-02-29 stsp int i;
8530 c2301be8 2018-04-30 stsp
8531 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8532 ee85c5e8 2020-02-29 stsp
8533 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8534 c2301be8 2018-04-30 stsp if (argv == NULL)
8535 c2301be8 2018-04-30 stsp err(1, "calloc");
8536 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8537 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8538 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8539 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8540 c2301be8 2018-04-30 stsp }
8541 c2301be8 2018-04-30 stsp
8542 ee85c5e8 2020-02-29 stsp va_end(ap);
8543 c2301be8 2018-04-30 stsp return argv;
8544 ee85c5e8 2020-02-29 stsp }
8545 ee85c5e8 2020-02-29 stsp
8546 ee85c5e8 2020-02-29 stsp /*
8547 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8548 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8549 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8550 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8551 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8552 ee85c5e8 2020-02-29 stsp */
8553 ee85c5e8 2020-02-29 stsp static const struct got_error *
8554 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8555 ee85c5e8 2020-02-29 stsp {
8556 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8557 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8558 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8559 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8560 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8561 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8562 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8563 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8564 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8565 84de9106 2020-12-26 stsp
8566 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8567 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8568 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8569 ee85c5e8 2020-02-29 stsp
8570 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8571 7cd52833 2022-06-23 thomas if (error != NULL)
8572 7cd52833 2022-06-23 thomas goto done;
8573 7cd52833 2022-06-23 thomas
8574 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8575 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8576 ee85c5e8 2020-02-29 stsp goto done;
8577 ee85c5e8 2020-02-29 stsp
8578 ee85c5e8 2020-02-29 stsp if (worktree)
8579 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8580 ee85c5e8 2020-02-29 stsp else
8581 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8582 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8583 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8584 ee85c5e8 2020-02-29 stsp goto done;
8585 ee85c5e8 2020-02-29 stsp }
8586 ee85c5e8 2020-02-29 stsp
8587 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8588 ee85c5e8 2020-02-29 stsp if (error != NULL)
8589 ee85c5e8 2020-02-29 stsp goto done;
8590 ee85c5e8 2020-02-29 stsp
8591 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8592 ee85c5e8 2020-02-29 stsp repo, worktree);
8593 ee85c5e8 2020-02-29 stsp if (error)
8594 ee85c5e8 2020-02-29 stsp goto done;
8595 ee85c5e8 2020-02-29 stsp
8596 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8597 84de9106 2020-12-26 stsp if (error)
8598 84de9106 2020-12-26 stsp goto done;
8599 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8600 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8601 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8602 ee85c5e8 2020-02-29 stsp if (error)
8603 ee85c5e8 2020-02-29 stsp goto done;
8604 ee85c5e8 2020-02-29 stsp
8605 ee85c5e8 2020-02-29 stsp if (worktree) {
8606 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8607 ee85c5e8 2020-02-29 stsp worktree = NULL;
8608 ee85c5e8 2020-02-29 stsp }
8609 ee85c5e8 2020-02-29 stsp
8610 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8611 945f9229 2022-04-16 thomas if (error)
8612 945f9229 2022-04-16 thomas goto done;
8613 945f9229 2022-04-16 thomas
8614 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8615 ee85c5e8 2020-02-29 stsp if (error) {
8616 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8617 ee85c5e8 2020-02-29 stsp goto done;
8618 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8619 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8620 6879ba42 2020-10-01 naddy usage(1, 1);
8621 ee85c5e8 2020-02-29 stsp /* not reached */
8622 ee85c5e8 2020-02-29 stsp }
8623 ee85c5e8 2020-02-29 stsp
8624 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8625 ee85c5e8 2020-02-29 stsp if (error)
8626 ee85c5e8 2020-02-29 stsp goto done;
8627 ee85c5e8 2020-02-29 stsp
8628 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8629 ee85c5e8 2020-02-29 stsp argc = 4;
8630 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8631 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8632 ee85c5e8 2020-02-29 stsp done:
8633 1d0f4054 2021-06-17 stsp if (repo) {
8634 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8635 1d0f4054 2021-06-17 stsp if (error == NULL)
8636 1d0f4054 2021-06-17 stsp error = close_err;
8637 1d0f4054 2021-06-17 stsp }
8638 945f9229 2022-04-16 thomas if (commit)
8639 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8640 ee85c5e8 2020-02-29 stsp if (worktree)
8641 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8642 7cd52833 2022-06-23 thomas if (pack_fds) {
8643 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8644 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8645 7cd52833 2022-06-23 thomas if (error == NULL)
8646 7cd52833 2022-06-23 thomas error = pack_err;
8647 7cd52833 2022-06-23 thomas }
8648 ee85c5e8 2020-02-29 stsp free(id);
8649 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8650 ee85c5e8 2020-02-29 stsp free(commit_id);
8651 ee85c5e8 2020-02-29 stsp free(cwd);
8652 ee85c5e8 2020-02-29 stsp free(repo_path);
8653 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8654 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8655 ee85c5e8 2020-02-29 stsp int i;
8656 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8657 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8658 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8659 ee85c5e8 2020-02-29 stsp }
8660 87670572 2020-12-26 naddy tog_free_refs();
8661 ee85c5e8 2020-02-29 stsp return error;
8662 c2301be8 2018-04-30 stsp }
8663 c2301be8 2018-04-30 stsp
8664 9f7d7167 2018-04-29 stsp int
8665 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8666 9f7d7167 2018-04-29 stsp {
8667 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8668 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8669 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8670 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8671 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8672 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8673 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8674 83cd27f8 2020-01-13 stsp };
8675 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8676 9f7d7167 2018-04-29 stsp
8677 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
8678 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
8679 cacf1690 2022-09-06 thomas
8680 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8681 9f7d7167 2018-04-29 stsp
8682 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8683 9f7d7167 2018-04-29 stsp switch (ch) {
8684 9f7d7167 2018-04-29 stsp case 'h':
8685 9f7d7167 2018-04-29 stsp hflag = 1;
8686 9f7d7167 2018-04-29 stsp break;
8687 53ccebc2 2019-07-30 stsp case 'V':
8688 53ccebc2 2019-07-30 stsp Vflag = 1;
8689 53ccebc2 2019-07-30 stsp break;
8690 9f7d7167 2018-04-29 stsp default:
8691 6879ba42 2020-10-01 naddy usage(hflag, 1);
8692 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8693 9f7d7167 2018-04-29 stsp }
8694 9f7d7167 2018-04-29 stsp }
8695 9f7d7167 2018-04-29 stsp
8696 9f7d7167 2018-04-29 stsp argc -= optind;
8697 9f7d7167 2018-04-29 stsp argv += optind;
8698 9814e6a3 2020-09-27 naddy optind = 1;
8699 c2301be8 2018-04-30 stsp optreset = 1;
8700 9f7d7167 2018-04-29 stsp
8701 53ccebc2 2019-07-30 stsp if (Vflag) {
8702 53ccebc2 2019-07-30 stsp got_version_print_str();
8703 6879ba42 2020-10-01 naddy return 0;
8704 53ccebc2 2019-07-30 stsp }
8705 4010e238 2020-12-04 stsp
8706 4010e238 2020-12-04 stsp #ifndef PROFILE
8707 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8708 4010e238 2020-12-04 stsp NULL) == -1)
8709 4010e238 2020-12-04 stsp err(1, "pledge");
8710 4010e238 2020-12-04 stsp #endif
8711 53ccebc2 2019-07-30 stsp
8712 c2301be8 2018-04-30 stsp if (argc == 0) {
8713 f29d3e89 2018-06-23 stsp if (hflag)
8714 6879ba42 2020-10-01 naddy usage(hflag, 0);
8715 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8716 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8717 c2301be8 2018-04-30 stsp argc = 1;
8718 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8719 c2301be8 2018-04-30 stsp } else {
8720 6059809a 2020-12-17 stsp size_t i;
8721 9f7d7167 2018-04-29 stsp
8722 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8723 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8724 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8725 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8726 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8727 9f7d7167 2018-04-29 stsp break;
8728 9f7d7167 2018-04-29 stsp }
8729 9f7d7167 2018-04-29 stsp }
8730 ee85c5e8 2020-02-29 stsp }
8731 3642c4c6 2019-07-09 stsp
8732 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8733 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8734 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8735 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8736 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8737 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8738 adf4c9e0 2022-07-03 thomas }
8739 adf4c9e0 2022-07-03 thomas
8740 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8741 ee85c5e8 2020-02-29 stsp if (argc != 1)
8742 6879ba42 2020-10-01 naddy usage(0, 1);
8743 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8744 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8745 ee85c5e8 2020-02-29 stsp } else {
8746 ee85c5e8 2020-02-29 stsp if (hflag)
8747 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8748 ee85c5e8 2020-02-29 stsp else
8749 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8750 9f7d7167 2018-04-29 stsp }
8751 9f7d7167 2018-04-29 stsp
8752 9f7d7167 2018-04-29 stsp endwin();
8753 b46c1e04 2020-09-20 naddy putchar('\n');
8754 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8755 a2f4a359 2020-02-28 stsp int i;
8756 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8757 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8758 a2f4a359 2020-02-28 stsp free(cmd_argv);
8759 a2f4a359 2020-02-28 stsp }
8760 a2f4a359 2020-02-28 stsp
8761 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8762 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8763 9f7d7167 2018-04-29 stsp return 0;
8764 9f7d7167 2018-04-29 stsp }