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 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 9f7d7167 2018-04-29 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
21 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
22 80ddbec8 2018-04-29 stsp
23 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
24 31120ada 2018-04-30 stsp #include <errno.h>
25 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
26 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
27 def2f970 2021-09-28 thomas #endif
28 9f7d7167 2018-04-29 stsp #include <curses.h>
29 9f7d7167 2018-04-29 stsp #include <panel.h>
30 9f7d7167 2018-04-29 stsp #include <locale.h>
31 61266923 2020-01-14 stsp #include <signal.h>
32 9f7d7167 2018-04-29 stsp #include <stdlib.h>
33 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
34 26ed57b2 2018-05-19 stsp #include <stdio.h>
35 9f7d7167 2018-04-29 stsp #include <getopt.h>
36 9f7d7167 2018-04-29 stsp #include <string.h>
37 9f7d7167 2018-04-29 stsp #include <err.h>
38 80ddbec8 2018-04-29 stsp #include <unistd.h>
39 26ed57b2 2018-05-19 stsp #include <limits.h>
40 61e69b96 2018-05-20 stsp #include <wchar.h>
41 788c352e 2018-06-16 stsp #include <time.h>
42 84451b3e 2018-07-10 stsp #include <pthread.h>
43 5036bf37 2018-09-24 stsp #include <libgen.h>
44 60493ae3 2019-06-20 stsp #include <regex.h>
45 3da8ef85 2021-09-21 stsp #include <sched.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 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
112 fc2737d5 2022-09-15 thomas };
113 fc2737d5 2022-09-15 thomas
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
124 d6b05b5b 2018-08-04 stsp };
125 c3e9aa98 2019-05-13 jcs
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
130 a5d43cac 2022-07-01 thomas };
131 a5d43cac 2022-07-01 thomas
132 87a675e0 2023-04-22 thomas #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
133 a5d43cac 2022-07-01 thomas
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
135 d6b05b5b 2018-08-04 stsp
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
140 1a76625f 2018-10-22 stsp int idx;
141 ba4f502b 2018-08-04 stsp };
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
146 6d17833f 2019-11-08 stsp };
147 6d17833f 2019-11-08 stsp
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
152 15a087fe 2019-02-21 stsp };
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
154 11b20872 2019-11-08 stsp
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
158 2183bbf6 2022-01-23 thomas
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
162 2183bbf6 2022-01-23 thomas {
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
166 2183bbf6 2022-01-23 thomas
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
176 2183bbf6 2022-01-23 thomas }
177 2183bbf6 2022-01-23 thomas
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
180 2183bbf6 2022-01-23 thomas }
181 51a10b52 2020-12-26 stsp
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
184 51a10b52 2020-12-26 stsp {
185 51a10b52 2020-12-26 stsp const struct got_error *err;
186 51a10b52 2020-12-26 stsp
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 3bfadbd4 2021-11-20 thomas repo);
190 51a10b52 2020-12-26 stsp if (err)
191 51a10b52 2020-12-26 stsp return err;
192 51a10b52 2020-12-26 stsp
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
194 51a10b52 2020-12-26 stsp repo);
195 51a10b52 2020-12-26 stsp }
196 51a10b52 2020-12-26 stsp
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
199 51a10b52 2020-12-26 stsp {
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
203 51a10b52 2020-12-26 stsp }
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
205 51a10b52 2020-12-26 stsp }
206 51a10b52 2020-12-26 stsp
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
210 11b20872 2019-11-08 stsp {
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
214 11b20872 2019-11-08 stsp
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
217 11b20872 2019-11-08 stsp
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
219 11b20872 2019-11-08 stsp
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 11b20872 2019-11-08 stsp free(tc);
234 11b20872 2019-11-08 stsp return err;
235 11b20872 2019-11-08 stsp }
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 11b20872 2019-11-08 stsp free(tc);
251 11b20872 2019-11-08 stsp }
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
258 11b20872 2019-11-08 stsp
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
261 11b20872 2019-11-08 stsp return tc;
262 11b20872 2019-11-08 stsp }
263 11b20872 2019-11-08 stsp
264 11b20872 2019-11-08 stsp return NULL;
265 11b20872 2019-11-08 stsp }
266 11b20872 2019-11-08 stsp
267 11b20872 2019-11-08 stsp static int
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
269 11b20872 2019-11-08 stsp {
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
300 11b20872 2019-11-08 stsp
301 11b20872 2019-11-08 stsp return -1;
302 11b20872 2019-11-08 stsp }
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp static int
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
306 11b20872 2019-11-08 stsp {
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
308 11b20872 2019-11-08 stsp
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
311 15a087fe 2019-02-21 stsp
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
329 11b20872 2019-11-08 stsp return -1;
330 11b20872 2019-11-08 stsp
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
332 11b20872 2019-11-08 stsp }
333 11b20872 2019-11-08 stsp
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
342 15a087fe 2019-02-21 stsp int eof;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
351 15a087fe 2019-02-21 stsp
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
354 b01e7d3b 2018-08-04 stsp };
355 b01e7d3b 2018-08-04 stsp
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
358 1a76625f 2018-10-22 stsp
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
381 1a76625f 2018-10-22 stsp };
382 1a76625f 2018-10-22 stsp
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
405 ba4f502b 2018-08-04 stsp };
406 11b20872 2019-11-08 stsp
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
422 ba4f502b 2018-08-04 stsp
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
426 e9424729 2018-08-04 stsp
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp int *quit;
430 e9424729 2018-08-04 stsp };
431 e9424729 2018-08-04 stsp
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
439 1134ebde 2023-04-22 thomas pthread_cond_t blame_complete;
440 e9424729 2018-08-04 stsp };
441 e9424729 2018-08-04 stsp
442 e9424729 2018-08-04 stsp struct tog_blame {
443 e9424729 2018-08-04 stsp FILE *f;
444 be659d10 2020-11-18 stsp off_t filesize;
445 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
446 6fcac457 2018-11-19 stsp int nlines;
447 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
448 e9424729 2018-08-04 stsp pthread_t thread;
449 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
450 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
451 e9424729 2018-08-04 stsp const char *path;
452 7cd52833 2022-06-23 thomas int *pack_fds;
453 e9424729 2018-08-04 stsp };
454 e9424729 2018-08-04 stsp
455 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
456 7cbe629d 2018-08-04 stsp int first_displayed_line;
457 7cbe629d 2018-08-04 stsp int last_displayed_line;
458 7cbe629d 2018-08-04 stsp int selected_line;
459 4fc71f3b 2022-07-12 thomas int last_diffed_line;
460 7cbe629d 2018-08-04 stsp int blame_complete;
461 e5a0f69f 2018-08-18 stsp int eof;
462 e5a0f69f 2018-08-18 stsp int done;
463 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
464 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
465 e5a0f69f 2018-08-18 stsp char *path;
466 7cbe629d 2018-08-04 stsp struct got_repository *repo;
467 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
468 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
469 e9424729 2018-08-04 stsp struct tog_blame blame;
470 6c4c42e0 2019-06-24 stsp int matched_line;
471 11b20872 2019-11-08 stsp struct tog_colors colors;
472 ad80ab7b 2018-08-04 stsp };
473 ad80ab7b 2018-08-04 stsp
474 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
475 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
476 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
478 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
479 ad80ab7b 2018-08-04 stsp int selected;
480 ad80ab7b 2018-08-04 stsp };
481 ad80ab7b 2018-08-04 stsp
482 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 ad80ab7b 2018-08-04 stsp
484 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
485 ad80ab7b 2018-08-04 stsp char *tree_label;
486 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
488 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
491 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
492 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
493 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
495 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
496 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
497 6458efa5 2020-11-24 stsp struct tog_colors colors;
498 6458efa5 2020-11-24 stsp };
499 6458efa5 2020-11-24 stsp
500 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
501 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
502 6458efa5 2020-11-24 stsp struct got_reference *ref;
503 6458efa5 2020-11-24 stsp int idx;
504 6458efa5 2020-11-24 stsp };
505 6458efa5 2020-11-24 stsp
506 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 6458efa5 2020-11-24 stsp
508 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
509 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
512 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
513 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
514 6458efa5 2020-11-24 stsp struct got_repository *repo;
515 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
516 bddb1296 2019-11-08 stsp struct tog_colors colors;
517 fc2737d5 2022-09-15 thomas };
518 fc2737d5 2022-09-15 thomas
519 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
520 fc2737d5 2022-09-15 thomas FILE *f;
521 fc2737d5 2022-09-15 thomas off_t *line_offsets;
522 fc2737d5 2022-09-15 thomas size_t nlines;
523 fc2737d5 2022-09-15 thomas int lineno;
524 fc2737d5 2022-09-15 thomas int first_displayed_line;
525 fc2737d5 2022-09-15 thomas int last_displayed_line;
526 fc2737d5 2022-09-15 thomas int eof;
527 fc2737d5 2022-09-15 thomas int matched_line;
528 fc2737d5 2022-09-15 thomas int selected_line;
529 fc2737d5 2022-09-15 thomas int all;
530 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
531 fc2737d5 2022-09-15 thomas };
532 fc2737d5 2022-09-15 thomas
533 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
534 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
535 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
536 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
541 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
542 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
543 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
544 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
545 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
546 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
547 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
548 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
550 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
552 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
553 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
554 c282f787 2023-04-22 thomas KEY_("S", "Switch split-screen layout"), \
555 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
556 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
557 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
558 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
559 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
560 fc2737d5 2022-09-15 thomas \
561 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
562 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
564 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
565 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
566 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
567 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
568 fc2737d5 2022-09-15 thomas " commit"), \
569 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
570 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
571 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
572 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
573 fc2737d5 2022-09-15 thomas \
574 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
575 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
576 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
577 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
578 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
579 fc2737d5 2022-09-15 thomas " data"), \
580 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
583 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
584 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
585 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
586 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
587 fc2737d5 2022-09-15 thomas \
588 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
589 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
590 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
591 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
592 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's commit"), \
595 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
596 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
597 fc2737d5 2022-09-15 thomas \
598 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
599 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
600 fc2737d5 2022-09-15 thomas " selected file"), \
601 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
602 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
603 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
604 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
605 fc2737d5 2022-09-15 thomas \
606 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
607 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
608 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
610 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
611 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
612 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
613 fc2737d5 2022-09-15 thomas
614 fc2737d5 2022-09-15 thomas struct tog_key_map {
615 fc2737d5 2022-09-15 thomas const char *keys;
616 fc2737d5 2022-09-15 thomas const char *info;
617 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
618 7cbe629d 2018-08-04 stsp };
619 7cbe629d 2018-08-04 stsp
620 b85a3496 2023-04-14 thomas /* curses io for tog regress */
621 b85a3496 2023-04-14 thomas struct tog_io {
622 b85a3496 2023-04-14 thomas FILE *cin;
623 b85a3496 2023-04-14 thomas FILE *cout;
624 b85a3496 2023-04-14 thomas FILE *f;
625 5b3a801d 2023-04-22 thomas FILE *sdump;
626 8e778ade 2023-04-22 thomas int wait_for_ui;
627 557d3365 2023-04-14 thomas } tog_io;
628 557d3365 2023-04-14 thomas static int using_mock_io;
629 b85a3496 2023-04-14 thomas
630 13bc6832 2023-04-22 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
631 b85a3496 2023-04-14 thomas
632 669b5ffa 2018-10-07 stsp /*
633 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
634 669b5ffa 2018-10-07 stsp *
635 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
636 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
637 669b5ffa 2018-10-07 stsp * there is enough screen estate.
638 669b5ffa 2018-10-07 stsp *
639 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
640 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
641 669b5ffa 2018-10-07 stsp *
642 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
643 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
644 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
645 669b5ffa 2018-10-07 stsp *
646 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
647 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
648 669b5ffa 2018-10-07 stsp */
649 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
650 669b5ffa 2018-10-07 stsp
651 cc3c9aac 2018-08-01 stsp struct tog_view {
652 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
653 26ed57b2 2018-05-19 stsp WINDOW *window;
654 26ed57b2 2018-05-19 stsp PANEL *panel;
655 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
656 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
657 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
658 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
659 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
660 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
661 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
662 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
663 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
664 9970f7fc 2020-12-03 stsp int dying;
665 669b5ffa 2018-10-07 stsp struct tog_view *parent;
666 669b5ffa 2018-10-07 stsp struct tog_view *child;
667 5dc9f4bc 2018-08-04 stsp
668 e78dc838 2020-12-04 stsp /*
669 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
670 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
671 e78dc838 2020-12-04 stsp * between parent and child.
672 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
673 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
674 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
675 e78dc838 2020-12-04 stsp * situations.
676 e78dc838 2020-12-04 stsp */
677 e78dc838 2020-12-04 stsp int focus_child;
678 e78dc838 2020-12-04 stsp
679 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
680 5dc9f4bc 2018-08-04 stsp /* type-specific state */
681 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
682 5dc9f4bc 2018-08-04 stsp union {
683 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
684 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
685 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
686 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
687 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
688 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
689 5dc9f4bc 2018-08-04 stsp } state;
690 e5a0f69f 2018-08-18 stsp
691 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
692 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
693 e78dc838 2020-12-04 stsp struct tog_view *, int);
694 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
695 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
696 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
697 60493ae3 2019-06-20 stsp
698 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
699 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
700 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
701 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
702 c0c4acc8 2021-01-24 stsp int search_started;
703 60493ae3 2019-06-20 stsp int searching;
704 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
705 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
706 60493ae3 2019-06-20 stsp int search_next_done;
707 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
708 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
709 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
710 1803e47f 2019-06-22 stsp regex_t regex;
711 41605754 2020-11-12 stsp regmatch_t regmatch;
712 f2d06bef 2023-01-23 thomas const char *action;
713 cc3c9aac 2018-08-01 stsp };
714 cd0acaa7 2018-05-20 stsp
715 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
716 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
717 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
718 78756c87 2020-11-24 stsp struct got_repository *);
719 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
720 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
721 e78dc838 2020-12-04 stsp struct tog_view *, int);
722 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
723 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
724 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
725 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
726 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
727 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp
729 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
730 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
731 78756c87 2020-11-24 stsp const char *, const char *, int);
732 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
733 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
734 e78dc838 2020-12-04 stsp struct tog_view *, int);
735 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
737 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
738 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
739 e5a0f69f 2018-08-18 stsp
740 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
741 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
742 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
743 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
744 e78dc838 2020-12-04 stsp struct tog_view *, int);
745 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
746 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
747 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
748 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
749 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
750 e5a0f69f 2018-08-18 stsp
751 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
752 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
753 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
754 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
755 e78dc838 2020-12-04 stsp struct tog_view *, int);
756 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
757 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
758 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
759 6458efa5 2020-11-24 stsp
760 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
761 6458efa5 2020-11-24 stsp struct got_repository *);
762 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
763 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
764 e78dc838 2020-12-04 stsp struct tog_view *, int);
765 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
766 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
767 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
768 2a7d3cd7 2022-09-17 thomas
769 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
770 2a7d3cd7 2022-09-17 thomas struct tog_view *);
771 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
772 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
773 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
774 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
775 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
776 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
777 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
778 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
779 25791caa 2018-10-24 stsp
780 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
781 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
782 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
783 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
784 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
785 25791caa 2018-10-24 stsp
786 25791caa 2018-10-24 stsp static void
787 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
788 25791caa 2018-10-24 stsp {
789 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
790 83baff54 2019-08-12 stsp }
791 83baff54 2019-08-12 stsp
792 83baff54 2019-08-12 stsp static void
793 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
794 83baff54 2019-08-12 stsp {
795 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
796 25791caa 2018-10-24 stsp }
797 26ed57b2 2018-05-19 stsp
798 61266923 2020-01-14 stsp static void
799 61266923 2020-01-14 stsp tog_sigcont(int signo)
800 61266923 2020-01-14 stsp {
801 61266923 2020-01-14 stsp tog_sigcont_received = 1;
802 61266923 2020-01-14 stsp }
803 61266923 2020-01-14 stsp
804 296152d1 2022-05-31 thomas static void
805 296152d1 2022-05-31 thomas tog_sigint(int signo)
806 296152d1 2022-05-31 thomas {
807 296152d1 2022-05-31 thomas tog_sigint_received = 1;
808 296152d1 2022-05-31 thomas }
809 296152d1 2022-05-31 thomas
810 296152d1 2022-05-31 thomas static void
811 296152d1 2022-05-31 thomas tog_sigterm(int signo)
812 296152d1 2022-05-31 thomas {
813 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
814 296152d1 2022-05-31 thomas }
815 296152d1 2022-05-31 thomas
816 296152d1 2022-05-31 thomas static int
817 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
818 296152d1 2022-05-31 thomas {
819 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
820 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
821 296152d1 2022-05-31 thomas }
822 296152d1 2022-05-31 thomas
823 e5a0f69f 2018-08-18 stsp static const struct got_error *
824 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
825 ea5e7bb5 2018-08-01 stsp {
826 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
827 e5a0f69f 2018-08-18 stsp
828 669b5ffa 2018-10-07 stsp if (view->child) {
829 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
830 669b5ffa 2018-10-07 stsp view->child = NULL;
831 669b5ffa 2018-10-07 stsp }
832 e5a0f69f 2018-08-18 stsp if (view->close)
833 e5a0f69f 2018-08-18 stsp err = view->close(view);
834 ea5e7bb5 2018-08-01 stsp if (view->panel)
835 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
836 ea5e7bb5 2018-08-01 stsp if (view->window)
837 ea5e7bb5 2018-08-01 stsp delwin(view->window);
838 ea5e7bb5 2018-08-01 stsp free(view);
839 f2d749db 2022-07-12 thomas return err ? err : child_err;
840 ea5e7bb5 2018-08-01 stsp }
841 ea5e7bb5 2018-08-01 stsp
842 ea5e7bb5 2018-08-01 stsp static struct tog_view *
843 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
844 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
845 ea5e7bb5 2018-08-01 stsp {
846 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
847 ea5e7bb5 2018-08-01 stsp
848 ea5e7bb5 2018-08-01 stsp if (view == NULL)
849 ea5e7bb5 2018-08-01 stsp return NULL;
850 ea5e7bb5 2018-08-01 stsp
851 d6b05b5b 2018-08-04 stsp view->type = type;
852 f7d12f7e 2018-08-01 stsp view->lines = LINES;
853 f7d12f7e 2018-08-01 stsp view->cols = COLS;
854 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
855 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
856 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
857 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
858 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
859 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
860 96a765a8 2018-08-04 stsp view_close(view);
861 ea5e7bb5 2018-08-01 stsp return NULL;
862 ea5e7bb5 2018-08-01 stsp }
863 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
864 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
865 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
866 96a765a8 2018-08-04 stsp view_close(view);
867 ea5e7bb5 2018-08-01 stsp return NULL;
868 ea5e7bb5 2018-08-01 stsp }
869 ea5e7bb5 2018-08-01 stsp
870 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
871 ea5e7bb5 2018-08-01 stsp return view;
872 cdf1ee82 2018-08-01 stsp }
873 cdf1ee82 2018-08-01 stsp
874 0cf4efb1 2018-09-29 stsp static int
875 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
876 0cf4efb1 2018-09-29 stsp {
877 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
878 0cf4efb1 2018-09-29 stsp return 0;
879 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
880 5c60c32a 2018-10-18 stsp }
881 5c60c32a 2018-10-18 stsp
882 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
883 a5d43cac 2022-07-01 thomas static int
884 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
885 a5d43cac 2022-07-01 thomas {
886 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
887 a5d43cac 2022-07-01 thomas }
888 a5d43cac 2022-07-01 thomas
889 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
890 5c60c32a 2018-10-18 stsp
891 5c60c32a 2018-10-18 stsp static const struct got_error *
892 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
893 5c60c32a 2018-10-18 stsp {
894 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
895 5c60c32a 2018-10-18 stsp
896 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
897 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
898 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
899 53d2bdd3 2022-07-10 thomas else
900 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
901 a5d43cac 2022-07-01 thomas view->begin_x = 0;
902 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
903 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
904 53d2bdd3 2022-07-10 thomas view->cols > 119)
905 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
906 53d2bdd3 2022-07-10 thomas else
907 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
908 a5d43cac 2022-07-01 thomas view->begin_y = 0;
909 a5d43cac 2022-07-01 thomas }
910 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
911 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
912 5c60c32a 2018-10-18 stsp view->lines = LINES;
913 5c60c32a 2018-10-18 stsp view->cols = COLS;
914 5c60c32a 2018-10-18 stsp err = view_resize(view);
915 5c60c32a 2018-10-18 stsp if (err)
916 5c60c32a 2018-10-18 stsp return err;
917 5c60c32a 2018-10-18 stsp
918 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
919 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
920 a5d43cac 2022-07-01 thomas
921 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
922 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
923 5c60c32a 2018-10-18 stsp
924 5c60c32a 2018-10-18 stsp return NULL;
925 5c60c32a 2018-10-18 stsp }
926 5c60c32a 2018-10-18 stsp
927 5c60c32a 2018-10-18 stsp static const struct got_error *
928 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
929 5c60c32a 2018-10-18 stsp {
930 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
931 5c60c32a 2018-10-18 stsp
932 5c60c32a 2018-10-18 stsp view->begin_x = 0;
933 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
934 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
935 5c60c32a 2018-10-18 stsp view->ncols = COLS;
936 5c60c32a 2018-10-18 stsp view->lines = LINES;
937 5c60c32a 2018-10-18 stsp view->cols = COLS;
938 5c60c32a 2018-10-18 stsp err = view_resize(view);
939 5c60c32a 2018-10-18 stsp if (err)
940 5c60c32a 2018-10-18 stsp return err;
941 5c60c32a 2018-10-18 stsp
942 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
943 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
944 5c60c32a 2018-10-18 stsp
945 5c60c32a 2018-10-18 stsp return NULL;
946 0cf4efb1 2018-09-29 stsp }
947 0cf4efb1 2018-09-29 stsp
948 5c60c32a 2018-10-18 stsp static int
949 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
950 5c60c32a 2018-10-18 stsp {
951 5c60c32a 2018-10-18 stsp return view->parent == NULL;
952 5c60c32a 2018-10-18 stsp }
953 5c60c32a 2018-10-18 stsp
954 b65b3ea0 2022-06-23 thomas static int
955 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
956 b65b3ea0 2022-06-23 thomas {
957 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
958 e44940c3 2022-07-01 thomas }
959 e44940c3 2022-07-01 thomas
960 e44940c3 2022-07-01 thomas static int
961 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
962 e44940c3 2022-07-01 thomas {
963 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
964 b65b3ea0 2022-06-23 thomas }
965 b65b3ea0 2022-06-23 thomas
966 444d5325 2022-07-03 thomas static int
967 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
968 444d5325 2022-07-03 thomas {
969 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
970 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
971 444d5325 2022-07-03 thomas }
972 444d5325 2022-07-03 thomas
973 a5d43cac 2022-07-01 thomas static void
974 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
975 a5d43cac 2022-07-01 thomas {
976 a5d43cac 2022-07-01 thomas PANEL *panel;
977 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
978 b65b3ea0 2022-06-23 thomas
979 a5d43cac 2022-07-01 thomas if (view->parent)
980 a5d43cac 2022-07-01 thomas return view_border(view->parent);
981 a5d43cac 2022-07-01 thomas
982 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
983 a5d43cac 2022-07-01 thomas if (panel == NULL)
984 a5d43cac 2022-07-01 thomas return;
985 a5d43cac 2022-07-01 thomas
986 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
987 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
988 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
989 88e8b64f 2023-04-22 thomas view->begin_x, ACS_HLINE, view->ncols);
990 a5d43cac 2022-07-01 thomas else
991 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
992 88e8b64f 2023-04-22 thomas ACS_VLINE, view->nlines);
993 a5d43cac 2022-07-01 thomas }
994 a5d43cac 2022-07-01 thomas
995 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
996 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
997 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
998 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
999 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
1000 a5d43cac 2022-07-01 thomas
1001 4d8c2215 2018-08-19 stsp static const struct got_error *
1002 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1003 f7d12f7e 2018-08-01 stsp {
1004 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1005 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1006 f7d12f7e 2018-08-01 stsp
1007 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1008 a5d43cac 2022-07-01 thomas
1009 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1010 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1011 0cf4efb1 2018-09-29 stsp else
1012 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1013 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1014 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1015 0cf4efb1 2018-09-29 stsp else
1016 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1017 6d0fee91 2018-08-01 stsp
1018 4918811f 2022-07-01 thomas if (view->child) {
1019 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1020 a5d43cac 2022-07-01 thomas
1021 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1022 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1023 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1024 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1025 40236d76 2022-06-23 thomas ncols = COLS;
1026 40236d76 2022-06-23 thomas
1027 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1028 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1029 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1030 5c60c32a 2018-10-18 stsp else
1031 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1032 5c60c32a 2018-10-18 stsp } else {
1033 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1034 40236d76 2022-06-23 thomas
1035 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1036 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1037 a5d43cac 2022-07-01 thomas }
1038 a5d43cac 2022-07-01 thomas /*
1039 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1040 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1041 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1042 a5d43cac 2022-07-01 thomas */
1043 a5d43cac 2022-07-01 thomas if (hs) {
1044 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1045 a5d43cac 2022-07-01 thomas if (err)
1046 a5d43cac 2022-07-01 thomas return err;
1047 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1048 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1049 a5d43cac 2022-07-01 thomas if (err)
1050 a5d43cac 2022-07-01 thomas return err;
1051 a5d43cac 2022-07-01 thomas }
1052 a5d43cac 2022-07-01 thomas view_border(view);
1053 a5d43cac 2022-07-01 thomas update_panels();
1054 a5d43cac 2022-07-01 thomas doupdate();
1055 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1056 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1057 a5d43cac 2022-07-01 thomas }
1058 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1059 40236d76 2022-06-23 thomas ncols = COLS;
1060 669b5ffa 2018-10-07 stsp
1061 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1062 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1063 ea0bff04 2022-07-19 thomas if (err)
1064 ea0bff04 2022-07-19 thomas return err;
1065 ea0bff04 2022-07-19 thomas }
1066 ea0bff04 2022-07-19 thomas
1067 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1068 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1069 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1070 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1071 40236d76 2022-06-23 thomas wclear(view->window);
1072 40236d76 2022-06-23 thomas
1073 40236d76 2022-06-23 thomas view->nlines = nlines;
1074 40236d76 2022-06-23 thomas view->ncols = ncols;
1075 40236d76 2022-06-23 thomas view->lines = LINES;
1076 40236d76 2022-06-23 thomas view->cols = COLS;
1077 40236d76 2022-06-23 thomas
1078 5c60c32a 2018-10-18 stsp return NULL;
1079 ea0bff04 2022-07-19 thomas }
1080 ea0bff04 2022-07-19 thomas
1081 ea0bff04 2022-07-19 thomas static const struct got_error *
1082 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1083 ea0bff04 2022-07-19 thomas {
1084 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1085 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1086 3a0139e8 2022-07-22 thomas int n = 0;
1087 ea0bff04 2022-07-19 thomas
1088 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1089 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1090 3a0139e8 2022-07-22 thomas
1091 ea0bff04 2022-07-19 thomas /*
1092 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1093 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1094 ea0bff04 2022-07-19 thomas */
1095 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1096 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1097 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1098 ea0bff04 2022-07-19 thomas }
1099 ea0bff04 2022-07-19 thomas
1100 ea0bff04 2022-07-19 thomas return err;
1101 97cb21cd 2022-07-12 thomas }
1102 97cb21cd 2022-07-12 thomas
1103 97cb21cd 2022-07-12 thomas static void
1104 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1105 97cb21cd 2022-07-12 thomas {
1106 97cb21cd 2022-07-12 thomas if (n == 0)
1107 97cb21cd 2022-07-12 thomas return;
1108 97cb21cd 2022-07-12 thomas
1109 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1110 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1111 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1112 97cb21cd 2022-07-12 thomas else
1113 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1114 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1115 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1116 97cb21cd 2022-07-12 thomas view->offset -= n;
1117 97cb21cd 2022-07-12 thomas else
1118 97cb21cd 2022-07-12 thomas view->offset = 0;
1119 97cb21cd 2022-07-12 thomas }
1120 53d2bdd3 2022-07-10 thomas }
1121 53d2bdd3 2022-07-10 thomas
1122 53d2bdd3 2022-07-10 thomas static const struct got_error *
1123 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1124 53d2bdd3 2022-07-10 thomas {
1125 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1126 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1127 53d2bdd3 2022-07-10 thomas
1128 53d2bdd3 2022-07-10 thomas if (view->parent)
1129 53d2bdd3 2022-07-10 thomas v = view->parent;
1130 53d2bdd3 2022-07-10 thomas else
1131 53d2bdd3 2022-07-10 thomas v = view;
1132 53d2bdd3 2022-07-10 thomas
1133 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1134 53d2bdd3 2022-07-10 thomas return NULL;
1135 53d2bdd3 2022-07-10 thomas
1136 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1137 53d2bdd3 2022-07-10 thomas
1138 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1139 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1140 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1141 53d2bdd3 2022-07-10 thomas if (view->parent)
1142 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1143 53d2bdd3 2022-07-10 thomas else
1144 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1145 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1146 53d2bdd3 2022-07-10 thomas view->count = 0;
1147 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1148 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1149 53d2bdd3 2022-07-10 thomas view->count = 0;
1150 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1151 53d2bdd3 2022-07-10 thomas }
1152 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1153 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1154 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1155 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1156 53d2bdd3 2022-07-10 thomas if (err)
1157 53d2bdd3 2022-07-10 thomas return err;
1158 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1159 53d2bdd3 2022-07-10 thomas } else {
1160 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1161 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1162 53d2bdd3 2022-07-10 thomas if (view->parent)
1163 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1164 53d2bdd3 2022-07-10 thomas else
1165 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1166 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1167 53d2bdd3 2022-07-10 thomas view->count = 0;
1168 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1169 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1170 53d2bdd3 2022-07-10 thomas view->count = 0;
1171 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1172 53d2bdd3 2022-07-10 thomas }
1173 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1174 53d2bdd3 2022-07-10 thomas }
1175 53d2bdd3 2022-07-10 thomas
1176 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1177 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1178 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1179 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1180 53d2bdd3 2022-07-10 thomas
1181 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1182 53d2bdd3 2022-07-10 thomas if (err)
1183 53d2bdd3 2022-07-10 thomas return err;
1184 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1185 53d2bdd3 2022-07-10 thomas if (err)
1186 53d2bdd3 2022-07-10 thomas return err;
1187 53d2bdd3 2022-07-10 thomas
1188 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1189 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1190 53d2bdd3 2022-07-10 thomas if (err)
1191 53d2bdd3 2022-07-10 thomas return err;
1192 53d2bdd3 2022-07-10 thomas }
1193 53d2bdd3 2022-07-10 thomas
1194 3a0139e8 2022-07-22 thomas if (v->resize)
1195 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1196 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1197 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1198 53d2bdd3 2022-07-10 thomas
1199 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1200 53d2bdd3 2022-07-10 thomas
1201 53d2bdd3 2022-07-10 thomas return err;
1202 53d2bdd3 2022-07-10 thomas }
1203 53d2bdd3 2022-07-10 thomas
1204 53d2bdd3 2022-07-10 thomas static void
1205 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1206 53d2bdd3 2022-07-10 thomas {
1207 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1208 53d2bdd3 2022-07-10 thomas
1209 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1210 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1211 669b5ffa 2018-10-07 stsp }
1212 669b5ffa 2018-10-07 stsp
1213 669b5ffa 2018-10-07 stsp static const struct got_error *
1214 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1215 669b5ffa 2018-10-07 stsp {
1216 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1217 669b5ffa 2018-10-07 stsp
1218 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1219 669b5ffa 2018-10-07 stsp return NULL;
1220 669b5ffa 2018-10-07 stsp
1221 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1222 669b5ffa 2018-10-07 stsp view->child = NULL;
1223 669b5ffa 2018-10-07 stsp return err;
1224 669b5ffa 2018-10-07 stsp }
1225 669b5ffa 2018-10-07 stsp
1226 40236d76 2022-06-23 thomas static const struct got_error *
1227 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1228 669b5ffa 2018-10-07 stsp {
1229 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1230 53d2bdd3 2022-07-10 thomas
1231 669b5ffa 2018-10-07 stsp view->child = child;
1232 669b5ffa 2018-10-07 stsp child->parent = view;
1233 40236d76 2022-06-23 thomas
1234 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1235 53d2bdd3 2022-07-10 thomas if (err)
1236 53d2bdd3 2022-07-10 thomas return err;
1237 53d2bdd3 2022-07-10 thomas
1238 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1239 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1240 53d2bdd3 2022-07-10 thomas
1241 53d2bdd3 2022-07-10 thomas return err;
1242 bfddd0d9 2018-09-29 stsp }
1243 2a31b33b 2022-07-23 thomas
1244 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1245 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1246 2a31b33b 2022-07-23 thomas
1247 2a31b33b 2022-07-23 thomas static const struct got_error *
1248 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1249 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1250 2a31b33b 2022-07-23 thomas {
1251 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1252 2a31b33b 2022-07-23 thomas const struct got_error *err;
1253 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1254 2a31b33b 2022-07-23 thomas
1255 2a31b33b 2022-07-23 thomas *requested = NULL;
1256 2a31b33b 2022-07-23 thomas
1257 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1258 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1259 bfddd0d9 2018-09-29 stsp
1260 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1261 2a31b33b 2022-07-23 thomas if (err)
1262 2a31b33b 2022-07-23 thomas return err;
1263 2a31b33b 2022-07-23 thomas
1264 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1265 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1266 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1269 2a31b33b 2022-07-23 thomas }
1270 2a31b33b 2022-07-23 thomas
1271 2a31b33b 2022-07-23 thomas view->focussed = 0;
1272 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1273 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1274 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1275 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1276 2a31b33b 2022-07-23 thomas
1277 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1278 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1279 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1280 2a31b33b 2022-07-23 thomas if (err)
1281 2a31b33b 2022-07-23 thomas return err;
1282 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1283 2a31b33b 2022-07-23 thomas if (err)
1284 2a31b33b 2022-07-23 thomas return err;
1285 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1286 2a31b33b 2022-07-23 thomas } else
1287 2a31b33b 2022-07-23 thomas *requested = new_view;
1288 2a31b33b 2022-07-23 thomas
1289 2a31b33b 2022-07-23 thomas return NULL;
1290 2a31b33b 2022-07-23 thomas }
1291 2a31b33b 2022-07-23 thomas
1292 34bc9ec9 2019-02-22 stsp static void
1293 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1294 25791caa 2018-10-24 stsp {
1295 25791caa 2018-10-24 stsp int cols, lines;
1296 25791caa 2018-10-24 stsp struct winsize size;
1297 25791caa 2018-10-24 stsp
1298 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1299 25791caa 2018-10-24 stsp cols = 80; /* Default */
1300 25791caa 2018-10-24 stsp lines = 24;
1301 25791caa 2018-10-24 stsp } else {
1302 25791caa 2018-10-24 stsp cols = size.ws_col;
1303 25791caa 2018-10-24 stsp lines = size.ws_row;
1304 25791caa 2018-10-24 stsp }
1305 25791caa 2018-10-24 stsp resize_term(lines, cols);
1306 2b49a8ae 2019-06-22 stsp }
1307 2b49a8ae 2019-06-22 stsp
1308 2b49a8ae 2019-06-22 stsp static const struct got_error *
1309 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1310 2b49a8ae 2019-06-22 stsp {
1311 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1312 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1313 2b49a8ae 2019-06-22 stsp char pattern[1024];
1314 2b49a8ae 2019-06-22 stsp int ret;
1315 c0c4acc8 2021-01-24 stsp
1316 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1317 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1318 c0c4acc8 2021-01-24 stsp view->searching = 0;
1319 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1320 c0c4acc8 2021-01-24 stsp }
1321 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1322 2b49a8ae 2019-06-22 stsp
1323 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1324 2b49a8ae 2019-06-22 stsp return NULL;
1325 2b49a8ae 2019-06-22 stsp
1326 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1327 a5d43cac 2022-07-01 thomas v = view->child;
1328 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1329 d07291c6 2022-12-30 thomas v = view->parent;
1330 2b49a8ae 2019-06-22 stsp
1331 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1332 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1333 a5d43cac 2022-07-01 thomas
1334 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1335 2b49a8ae 2019-06-22 stsp nocbreak();
1336 2b49a8ae 2019-06-22 stsp echo();
1337 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1338 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1339 2b49a8ae 2019-06-22 stsp cbreak();
1340 2b49a8ae 2019-06-22 stsp noecho();
1341 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1342 557d3365 2023-04-14 thomas if (!fast_refresh && !using_mock_io)
1343 f54d892e 2023-02-17 thomas halfdelay(10);
1344 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1345 2b49a8ae 2019-06-22 stsp return NULL;
1346 2b49a8ae 2019-06-22 stsp
1347 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1348 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1349 7c32bd05 2019-06-22 stsp if (err) {
1350 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1351 7c32bd05 2019-06-22 stsp return err;
1352 7c32bd05 2019-06-22 stsp }
1353 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1354 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1355 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1356 2b49a8ae 2019-06-22 stsp view->search_next(view);
1357 2b49a8ae 2019-06-22 stsp }
1358 2b49a8ae 2019-06-22 stsp
1359 2b49a8ae 2019-06-22 stsp return NULL;
1360 64486692 2022-07-07 thomas }
1361 64486692 2022-07-07 thomas
1362 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1363 64486692 2022-07-07 thomas static const struct got_error *
1364 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1365 64486692 2022-07-07 thomas {
1366 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1367 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1368 64486692 2022-07-07 thomas
1369 64486692 2022-07-07 thomas if (view->parent)
1370 64486692 2022-07-07 thomas v = view->parent;
1371 64486692 2022-07-07 thomas else
1372 64486692 2022-07-07 thomas v = view;
1373 64486692 2022-07-07 thomas
1374 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1375 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1376 ddbc4d37 2022-07-12 thomas else
1377 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1378 64486692 2022-07-07 thomas
1379 ddbc4d37 2022-07-12 thomas if (!v->child)
1380 ddbc4d37 2022-07-12 thomas return NULL;
1381 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1382 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1383 ddbc4d37 2022-07-12 thomas
1384 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1385 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1386 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1387 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1388 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1389 64486692 2022-07-07 thomas
1390 ddbc4d37 2022-07-12 thomas
1391 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1392 64486692 2022-07-07 thomas v->ncols = COLS;
1393 64486692 2022-07-07 thomas v->child->ncols = COLS;
1394 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1395 64486692 2022-07-07 thomas
1396 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1397 64486692 2022-07-07 thomas if (err)
1398 64486692 2022-07-07 thomas return err;
1399 64486692 2022-07-07 thomas }
1400 64486692 2022-07-07 thomas v->child->mode = v->mode;
1401 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1402 64486692 2022-07-07 thomas v->focus_child = 1;
1403 64486692 2022-07-07 thomas
1404 64486692 2022-07-07 thomas err = view_fullscreen(v);
1405 64486692 2022-07-07 thomas if (err)
1406 64486692 2022-07-07 thomas return err;
1407 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1408 64486692 2022-07-07 thomas if (err)
1409 64486692 2022-07-07 thomas return err;
1410 64486692 2022-07-07 thomas
1411 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1412 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1413 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1414 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1415 cf1fe301 2022-07-29 thomas if (err)
1416 cf1fe301 2022-07-29 thomas return err;
1417 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1418 cf1fe301 2022-07-29 thomas if (err)
1419 cf1fe301 2022-07-29 thomas return err;
1420 ddbc4d37 2022-07-12 thomas } else {
1421 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1422 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1423 64486692 2022-07-07 thomas }
1424 f4e6231a 2022-07-22 thomas if (v->resize)
1425 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1426 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1427 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1428 64486692 2022-07-07 thomas
1429 64486692 2022-07-07 thomas return err;
1430 0cf4efb1 2018-09-29 stsp }
1431 6d0fee91 2018-08-01 stsp
1432 07b0611c 2022-06-23 thomas /*
1433 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1434 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1435 b85a3496 2023-04-14 thomas */
1436 b85a3496 2023-04-14 thomas static void
1437 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1438 b85a3496 2023-04-14 thomas {
1439 b85a3496 2023-04-14 thomas size_t x = *n;
1440 b85a3496 2023-04-14 thomas
1441 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1442 b85a3496 2023-04-14 thomas return;
1443 b85a3496 2023-04-14 thomas
1444 b85a3496 2023-04-14 thomas if (x < 0)
1445 b85a3496 2023-04-14 thomas x = strlen(str);
1446 b85a3496 2023-04-14 thomas
1447 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1448 b85a3496 2023-04-14 thomas str[x] = '\0';
1449 b85a3496 2023-04-14 thomas
1450 b85a3496 2023-04-14 thomas *n = x + 1;
1451 b85a3496 2023-04-14 thomas }
1452 b85a3496 2023-04-14 thomas
1453 b85a3496 2023-04-14 thomas /*
1454 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1455 13bc6832 2023-04-22 thomas * and strip trailing whitespace. If vline is set, overwrite
1456 13bc6832 2023-04-22 thomas * line[vline] with '|' because the ACS_VLINE character is
1457 13bc6832 2023-04-22 thomas * written out as 'x'. Write the line to file f.
1458 b85a3496 2023-04-14 thomas */
1459 b85a3496 2023-04-14 thomas static const struct got_error *
1460 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1461 b85a3496 2023-04-14 thomas {
1462 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1463 b85a3496 2023-04-14 thomas int r, w;
1464 b85a3496 2023-04-14 thomas
1465 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1466 b85a3496 2023-04-14 thomas if (r == ERR)
1467 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1468 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1469 b85a3496 2023-04-14 thomas
1470 b85a3496 2023-04-14 thomas /*
1471 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1472 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1473 b85a3496 2023-04-14 thomas */
1474 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1475 b85a3496 2023-04-14 thomas
1476 13bc6832 2023-04-22 thomas if (vline > 0)
1477 b85a3496 2023-04-14 thomas line[vline] = '|';
1478 b85a3496 2023-04-14 thomas
1479 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1480 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1481 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1482 b85a3496 2023-04-14 thomas
1483 b85a3496 2023-04-14 thomas return NULL;
1484 b85a3496 2023-04-14 thomas }
1485 b85a3496 2023-04-14 thomas
1486 b85a3496 2023-04-14 thomas /*
1487 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1488 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1489 b85a3496 2023-04-14 thomas */
1490 b85a3496 2023-04-14 thomas static const struct got_error *
1491 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1492 b85a3496 2023-04-14 thomas {
1493 b85a3496 2023-04-14 thomas const struct got_error *err;
1494 b85a3496 2023-04-14 thomas int i;
1495 b85a3496 2023-04-14 thomas
1496 5b3a801d 2023-04-22 thomas err = got_opentemp_truncate(tog_io.sdump);
1497 5b3a801d 2023-04-22 thomas if (err)
1498 5b3a801d 2023-04-22 thomas return err;
1499 b85a3496 2023-04-14 thomas
1500 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1501 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1502 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1503 b85a3496 2023-04-14 thomas
1504 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1505 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1506 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, ncols - 1);
1507 b85a3496 2023-04-14 thomas if (err)
1508 b85a3496 2023-04-14 thomas goto done;
1509 b85a3496 2023-04-14 thomas }
1510 b85a3496 2023-04-14 thomas } else {
1511 b85a3496 2023-04-14 thomas int hline = 0;
1512 b85a3496 2023-04-14 thomas
1513 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1514 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1515 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1516 b85a3496 2023-04-14 thomas hline = view->child ?
1517 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1518 b85a3496 2023-04-14 thomas
1519 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1520 13bc6832 2023-04-22 thomas if (hline && i == hline - 1) {
1521 b85a3496 2023-04-14 thomas int c;
1522 b85a3496 2023-04-14 thomas
1523 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1524 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1525 5b3a801d 2023-04-22 thomas fputc('-', tog_io.sdump);
1526 5b3a801d 2023-04-22 thomas fputc('\n', tog_io.sdump);
1527 b85a3496 2023-04-14 thomas continue;
1528 b85a3496 2023-04-14 thomas }
1529 b85a3496 2023-04-14 thomas
1530 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, 0);
1531 b85a3496 2023-04-14 thomas if (err)
1532 b85a3496 2023-04-14 thomas goto done;
1533 b85a3496 2023-04-14 thomas }
1534 b85a3496 2023-04-14 thomas }
1535 b85a3496 2023-04-14 thomas
1536 b85a3496 2023-04-14 thomas done:
1537 b85a3496 2023-04-14 thomas return err;
1538 b85a3496 2023-04-14 thomas }
1539 b85a3496 2023-04-14 thomas
1540 b85a3496 2023-04-14 thomas /*
1541 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1542 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1543 07b0611c 2022-06-23 thomas */
1544 07b0611c 2022-06-23 thomas static int
1545 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1546 07b0611c 2022-06-23 thomas {
1547 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1548 a5d43cac 2022-07-01 thomas int x, n = 0;
1549 07b0611c 2022-06-23 thomas
1550 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1551 a5d43cac 2022-07-01 thomas v = view->child;
1552 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1553 a5d43cac 2022-07-01 thomas v = view->parent;
1554 a5d43cac 2022-07-01 thomas
1555 07b0611c 2022-06-23 thomas view->count = 0;
1556 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1557 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1558 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1559 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1560 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1561 07b0611c 2022-06-23 thomas
1562 07b0611c 2022-06-23 thomas do {
1563 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1564 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1565 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1566 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1567 a5d43cac 2022-07-01 thomas }
1568 a5d43cac 2022-07-01 thomas
1569 07b0611c 2022-06-23 thomas /*
1570 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1571 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1572 07b0611c 2022-06-23 thomas */
1573 07b0611c 2022-06-23 thomas if (n >= 9999999)
1574 07b0611c 2022-06-23 thomas n = 9999999;
1575 07b0611c 2022-06-23 thomas else
1576 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1577 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1578 07b0611c 2022-06-23 thomas
1579 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1580 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1581 07dd3ed3 2022-08-06 thomas n = 0;
1582 07dd3ed3 2022-08-06 thomas c = 0;
1583 07dd3ed3 2022-08-06 thomas }
1584 07dd3ed3 2022-08-06 thomas
1585 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1586 07b0611c 2022-06-23 thomas view->count = n;
1587 07b0611c 2022-06-23 thomas
1588 07b0611c 2022-06-23 thomas return c;
1589 f2d06bef 2023-01-23 thomas }
1590 f2d06bef 2023-01-23 thomas
1591 f2d06bef 2023-01-23 thomas static void
1592 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1593 f2d06bef 2023-01-23 thomas {
1594 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1595 f2d06bef 2023-01-23 thomas
1596 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1597 f2d06bef 2023-01-23 thomas v = view->child;
1598 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1599 f2d06bef 2023-01-23 thomas v = view->parent;
1600 f2d06bef 2023-01-23 thomas
1601 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1602 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1603 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1604 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1605 f2d06bef 2023-01-23 thomas
1606 f2d06bef 2023-01-23 thomas /*
1607 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1608 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1609 f2d06bef 2023-01-23 thomas */
1610 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1611 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1612 f2d06bef 2023-01-23 thomas view->action = NULL;
1613 f2d06bef 2023-01-23 thomas } else
1614 f2d06bef 2023-01-23 thomas view->action = NULL;
1615 07b0611c 2022-06-23 thomas }
1616 07b0611c 2022-06-23 thomas
1617 b85a3496 2023-04-14 thomas /*
1618 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1619 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1620 b85a3496 2023-04-14 thomas */
1621 0cf4efb1 2018-09-29 stsp static const struct got_error *
1622 a184c764 2023-04-22 thomas tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1623 b85a3496 2023-04-14 thomas {
1624 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1625 b85a3496 2023-04-14 thomas char *line = NULL;
1626 b85a3496 2023-04-14 thomas size_t linesz = 0;
1627 8e778ade 2023-04-22 thomas
1628 a184c764 2023-04-22 thomas if (view->count && --view->count) {
1629 a184c764 2023-04-22 thomas *ch = view->ch;
1630 a184c764 2023-04-22 thomas return NULL;
1631 a184c764 2023-04-22 thomas } else
1632 a184c764 2023-04-22 thomas *ch = -1;
1633 b85a3496 2023-04-14 thomas
1634 b85a3496 2023-04-14 thomas if (getline(&line, &linesz, script) == -1) {
1635 b85a3496 2023-04-14 thomas if (feof(script)) {
1636 b85a3496 2023-04-14 thomas *done = 1;
1637 b85a3496 2023-04-14 thomas goto done;
1638 b85a3496 2023-04-14 thomas } else {
1639 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1640 b85a3496 2023-04-14 thomas goto done;
1641 b85a3496 2023-04-14 thomas }
1642 8e778ade 2023-04-22 thomas }
1643 1134ebde 2023-04-22 thomas
1644 8e778ade 2023-04-22 thomas if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1645 8e778ade 2023-04-22 thomas tog_io.wait_for_ui = 1;
1646 8e778ade 2023-04-22 thomas else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1647 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1648 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1649 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1650 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1651 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1652 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1653 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1654 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1655 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1656 52c5094b 2023-04-28 thomas else if (strncasecmp(line, "TAB", 3) == 0)
1657 52c5094b 2023-04-28 thomas *ch = '\t';
1658 13bc6832 2023-04-22 thomas else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1659 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1660 a184c764 2023-04-22 thomas else if (isdigit((unsigned char)*line)) {
1661 a184c764 2023-04-22 thomas char *t = line;
1662 a184c764 2023-04-22 thomas
1663 a184c764 2023-04-22 thomas while (isdigit((unsigned char)*t))
1664 a184c764 2023-04-22 thomas ++t;
1665 a184c764 2023-04-22 thomas view->ch = *ch = *t;
1666 a184c764 2023-04-22 thomas *t = '\0';
1667 a184c764 2023-04-22 thomas /* ignore error, view->count is 0 if instruction is invalid */
1668 a184c764 2023-04-22 thomas view->count = strtonum(line, 0, INT_MAX, NULL);
1669 a184c764 2023-04-22 thomas } else
1670 b85a3496 2023-04-14 thomas *ch = *line;
1671 b85a3496 2023-04-14 thomas
1672 b85a3496 2023-04-14 thomas done:
1673 b85a3496 2023-04-14 thomas free(line);
1674 b85a3496 2023-04-14 thomas return err;
1675 b85a3496 2023-04-14 thomas }
1676 b85a3496 2023-04-14 thomas
1677 b85a3496 2023-04-14 thomas static const struct got_error *
1678 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1679 557d3365 2023-04-14 thomas struct tog_view_list_head *views, int fast_refresh)
1680 e5a0f69f 2018-08-18 stsp {
1681 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1682 669b5ffa 2018-10-07 stsp struct tog_view *v;
1683 1a76625f 2018-10-22 stsp int ch, errcode;
1684 e5a0f69f 2018-08-18 stsp
1685 e5a0f69f 2018-08-18 stsp *new = NULL;
1686 f2d06bef 2023-01-23 thomas
1687 f2d06bef 2023-01-23 thomas if (view->action)
1688 f2d06bef 2023-01-23 thomas action_report(view);
1689 8f4ed634 2020-03-26 stsp
1690 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1691 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1692 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1693 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1694 07b0611c 2022-06-23 thomas view->count = 0;
1695 07b0611c 2022-06-23 thomas }
1696 e5a0f69f 2018-08-18 stsp
1697 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1698 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1699 82954512 2020-02-03 stsp if (errcode)
1700 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1701 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1702 3da8ef85 2021-09-21 stsp sched_yield();
1703 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1704 82954512 2020-02-03 stsp if (errcode)
1705 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1706 82954512 2020-02-03 stsp "pthread_mutex_lock");
1707 60493ae3 2019-06-20 stsp view->search_next(view);
1708 60493ae3 2019-06-20 stsp return NULL;
1709 60493ae3 2019-06-20 stsp }
1710 60493ae3 2019-06-20 stsp
1711 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1713 1a76625f 2018-10-22 stsp if (errcode)
1714 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1715 b85a3496 2023-04-14 thomas
1716 557d3365 2023-04-14 thomas if (using_mock_io) {
1717 a184c764 2023-04-22 thomas err = tog_read_script_key(tog_io.f, view, &ch, done);
1718 9d9cab5e 2023-04-22 thomas if (err) {
1719 9d9cab5e 2023-04-22 thomas errcode = pthread_mutex_lock(&tog_mutex);
1720 b85a3496 2023-04-14 thomas return err;
1721 9d9cab5e 2023-04-22 thomas }
1722 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1723 634cb454 2022-07-03 thomas cbreak();
1724 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1725 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1726 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1727 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1728 634cb454 2022-07-03 thomas view->count = 0;
1729 634cb454 2022-07-03 thomas else
1730 634cb454 2022-07-03 thomas ch = view->ch;
1731 634cb454 2022-07-03 thomas } else {
1732 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1733 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1734 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1735 07b0611c 2022-06-23 thomas }
1736 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1737 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1738 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1739 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1740 1a76625f 2018-10-22 stsp if (errcode)
1741 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1742 25791caa 2018-10-24 stsp
1743 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1744 25791caa 2018-10-24 stsp tog_resizeterm();
1745 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1746 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1747 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1748 25791caa 2018-10-24 stsp err = view_resize(v);
1749 25791caa 2018-10-24 stsp if (err)
1750 25791caa 2018-10-24 stsp return err;
1751 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1752 25791caa 2018-10-24 stsp if (err)
1753 25791caa 2018-10-24 stsp return err;
1754 cdfcfb03 2020-12-06 stsp if (v->child) {
1755 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1756 cdfcfb03 2020-12-06 stsp if (err)
1757 cdfcfb03 2020-12-06 stsp return err;
1758 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1759 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1760 cdfcfb03 2020-12-06 stsp if (err)
1761 cdfcfb03 2020-12-06 stsp return err;
1762 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1763 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1764 53d2bdd3 2022-07-10 thomas if (err)
1765 53d2bdd3 2022-07-10 thomas return err;
1766 53d2bdd3 2022-07-10 thomas }
1767 cdfcfb03 2020-12-06 stsp }
1768 25791caa 2018-10-24 stsp }
1769 25791caa 2018-10-24 stsp }
1770 25791caa 2018-10-24 stsp
1771 e5a0f69f 2018-08-18 stsp switch (ch) {
1772 fc2737d5 2022-09-15 thomas case '?':
1773 fc2737d5 2022-09-15 thomas case 'H':
1774 fc2737d5 2022-09-15 thomas case KEY_F(1):
1775 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1776 fc2737d5 2022-09-15 thomas err = view->reset(view);
1777 fc2737d5 2022-09-15 thomas else
1778 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1779 fc2737d5 2022-09-15 thomas break;
1780 1e37a5c2 2019-05-12 jcs case '\t':
1781 07b0611c 2022-06-23 thomas view->count = 0;
1782 1e37a5c2 2019-05-12 jcs if (view->child) {
1783 e78dc838 2020-12-04 stsp view->focussed = 0;
1784 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1785 e78dc838 2020-12-04 stsp view->focus_child = 1;
1786 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1787 e78dc838 2020-12-04 stsp view->focussed = 0;
1788 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1789 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1790 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1791 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1792 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1793 3a0139e8 2022-07-22 thomas 0);
1794 a5d43cac 2022-07-01 thomas if (err)
1795 a5d43cac 2022-07-01 thomas return err;
1796 a5d43cac 2022-07-01 thomas }
1797 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1798 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1799 a5d43cac 2022-07-01 thomas if (err)
1800 a5d43cac 2022-07-01 thomas return err;
1801 a5d43cac 2022-07-01 thomas }
1802 1e37a5c2 2019-05-12 jcs }
1803 1e37a5c2 2019-05-12 jcs break;
1804 1e37a5c2 2019-05-12 jcs case 'q':
1805 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1806 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1807 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1808 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1809 a5d43cac 2022-07-01 thomas if (err)
1810 a5d43cac 2022-07-01 thomas break;
1811 a5d43cac 2022-07-01 thomas }
1812 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1813 a5d43cac 2022-07-01 thomas }
1814 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1815 9970f7fc 2020-12-03 stsp view->dying = 1;
1816 1e37a5c2 2019-05-12 jcs break;
1817 1e37a5c2 2019-05-12 jcs case 'Q':
1818 1e37a5c2 2019-05-12 jcs *done = 1;
1819 1e37a5c2 2019-05-12 jcs break;
1820 1c5e5faa 2022-06-23 thomas case 'F':
1821 07b0611c 2022-06-23 thomas view->count = 0;
1822 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1823 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1824 1e37a5c2 2019-05-12 jcs break;
1825 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1826 e78dc838 2020-12-04 stsp view->focussed = 0;
1827 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1828 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1829 53d2bdd3 2022-07-10 thomas } else {
1830 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1831 53d2bdd3 2022-07-10 thomas if (!err)
1832 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1833 53d2bdd3 2022-07-10 thomas }
1834 1e37a5c2 2019-05-12 jcs if (err)
1835 1e37a5c2 2019-05-12 jcs break;
1836 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1837 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1838 1e37a5c2 2019-05-12 jcs } else {
1839 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1840 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1841 e78dc838 2020-12-04 stsp view->focussed = 1;
1842 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1843 1e37a5c2 2019-05-12 jcs } else {
1844 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1845 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1846 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1847 53d2bdd3 2022-07-10 thomas if (!err)
1848 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1849 669b5ffa 2018-10-07 stsp }
1850 1e37a5c2 2019-05-12 jcs if (err)
1851 1e37a5c2 2019-05-12 jcs break;
1852 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1853 a5d43cac 2022-07-01 thomas }
1854 a5d43cac 2022-07-01 thomas if (err)
1855 a5d43cac 2022-07-01 thomas break;
1856 3a0139e8 2022-07-22 thomas if (view->resize) {
1857 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1858 a5d43cac 2022-07-01 thomas if (err)
1859 a5d43cac 2022-07-01 thomas break;
1860 1e37a5c2 2019-05-12 jcs }
1861 e0bcabc5 2023-04-28 thomas if (view->parent) {
1862 e0bcabc5 2023-04-28 thomas if (view->parent->resize) {
1863 e0bcabc5 2023-04-28 thomas err = view->parent->resize(view->parent, 0);
1864 e0bcabc5 2023-04-28 thomas if (err != NULL)
1865 e0bcabc5 2023-04-28 thomas break;
1866 e0bcabc5 2023-04-28 thomas }
1867 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1868 e0bcabc5 2023-04-28 thomas if (err != NULL)
1869 e0bcabc5 2023-04-28 thomas break;
1870 e0bcabc5 2023-04-28 thomas }
1871 e0bcabc5 2023-04-28 thomas err = offset_selection_down(view);
1872 1e37a5c2 2019-05-12 jcs break;
1873 64486692 2022-07-07 thomas case 'S':
1874 53d2bdd3 2022-07-10 thomas view->count = 0;
1875 64486692 2022-07-07 thomas err = switch_split(view);
1876 53d2bdd3 2022-07-10 thomas break;
1877 53d2bdd3 2022-07-10 thomas case '-':
1878 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1879 64486692 2022-07-07 thomas break;
1880 53d2bdd3 2022-07-10 thomas case '+':
1881 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1882 53d2bdd3 2022-07-10 thomas break;
1883 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1884 60493ae3 2019-06-20 stsp break;
1885 60493ae3 2019-06-20 stsp case '/':
1886 07b0611c 2022-06-23 thomas view->count = 0;
1887 60493ae3 2019-06-20 stsp if (view->search_start)
1888 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1889 60493ae3 2019-06-20 stsp else
1890 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1891 1e37a5c2 2019-05-12 jcs break;
1892 b1bf1435 2019-06-21 stsp case 'N':
1893 60493ae3 2019-06-20 stsp case 'n':
1894 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1895 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1896 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1897 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1898 60493ae3 2019-06-20 stsp view->search_next(view);
1899 60493ae3 2019-06-20 stsp } else
1900 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1901 adf4c9e0 2022-07-03 thomas break;
1902 adf4c9e0 2022-07-03 thomas case 'A':
1903 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1904 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1905 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1906 f2d06bef 2023-01-23 thomas } else {
1907 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1908 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1909 f2d06bef 2023-01-23 thomas }
1910 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1911 adf4c9e0 2022-07-03 thomas if (v->reset) {
1912 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1913 adf4c9e0 2022-07-03 thomas if (err)
1914 adf4c9e0 2022-07-03 thomas return err;
1915 adf4c9e0 2022-07-03 thomas }
1916 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1917 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1918 adf4c9e0 2022-07-03 thomas if (err)
1919 adf4c9e0 2022-07-03 thomas return err;
1920 adf4c9e0 2022-07-03 thomas }
1921 adf4c9e0 2022-07-03 thomas }
1922 60493ae3 2019-06-20 stsp break;
1923 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1924 b85a3496 2023-04-14 thomas err = screendump(view);
1925 b85a3496 2023-04-14 thomas break;
1926 1e37a5c2 2019-05-12 jcs default:
1927 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1928 1e37a5c2 2019-05-12 jcs break;
1929 e5a0f69f 2018-08-18 stsp }
1930 e5a0f69f 2018-08-18 stsp
1931 e5a0f69f 2018-08-18 stsp return err;
1932 bcbd79e2 2018-08-19 stsp }
1933 bcbd79e2 2018-08-19 stsp
1934 ef20f542 2022-06-26 thomas static int
1935 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1936 a3404814 2018-09-02 stsp {
1937 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1938 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1939 669b5ffa 2018-10-07 stsp return 0;
1940 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1941 669b5ffa 2018-10-07 stsp return 0;
1942 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1943 a3404814 2018-09-02 stsp return 0;
1944 a3404814 2018-09-02 stsp
1945 669b5ffa 2018-10-07 stsp return view->focussed;
1946 a3404814 2018-09-02 stsp }
1947 a3404814 2018-09-02 stsp
1948 bcbd79e2 2018-08-19 stsp static const struct got_error *
1949 557d3365 2023-04-14 thomas tog_io_close(void)
1950 e5a0f69f 2018-08-18 stsp {
1951 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1952 b85a3496 2023-04-14 thomas
1953 557d3365 2023-04-14 thomas if (tog_io.cin && fclose(tog_io.cin) == EOF)
1954 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cin, GOT_ERR_IO);
1955 557d3365 2023-04-14 thomas if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1956 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cout, GOT_ERR_IO);
1957 557d3365 2023-04-14 thomas if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1958 557d3365 2023-04-14 thomas err = got_ferror(tog_io.f, GOT_ERR_IO);
1959 5b3a801d 2023-04-22 thomas if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1960 5b3a801d 2023-04-22 thomas err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1961 b85a3496 2023-04-14 thomas
1962 b85a3496 2023-04-14 thomas return err;
1963 b85a3496 2023-04-14 thomas }
1964 b85a3496 2023-04-14 thomas
1965 b85a3496 2023-04-14 thomas static const struct got_error *
1966 557d3365 2023-04-14 thomas view_loop(struct tog_view *view)
1967 b85a3496 2023-04-14 thomas {
1968 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1969 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1970 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1971 64486692 2022-07-07 thomas char *mode;
1972 fd823528 2018-10-22 stsp int fast_refresh = 10;
1973 1a76625f 2018-10-22 stsp int done = 0, errcode;
1974 e5a0f69f 2018-08-18 stsp
1975 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1976 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1977 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1978 64486692 2022-07-07 thomas else
1979 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1980 64486692 2022-07-07 thomas
1981 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1982 1a76625f 2018-10-22 stsp if (errcode)
1983 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1984 1a76625f 2018-10-22 stsp
1985 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1986 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1987 e5a0f69f 2018-08-18 stsp
1988 1004088d 2018-09-29 stsp view->focussed = 1;
1989 878940b7 2018-09-29 stsp err = view->show(view);
1990 0cf4efb1 2018-09-29 stsp if (err)
1991 0cf4efb1 2018-09-29 stsp return err;
1992 0cf4efb1 2018-09-29 stsp update_panels();
1993 0cf4efb1 2018-09-29 stsp doupdate();
1994 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1995 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1996 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1997 557d3365 2023-04-14 thomas if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1998 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1999 fd823528 2018-10-22 stsp
2000 557d3365 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
2001 e5a0f69f 2018-08-18 stsp if (err)
2002 e5a0f69f 2018-08-18 stsp break;
2003 6ec3d39c 2023-01-23 thomas
2004 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
2005 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
2006 6ec3d39c 2023-01-23 thomas done = 1;
2007 6ec3d39c 2023-01-23 thomas if (done) {
2008 6ec3d39c 2023-01-23 thomas struct tog_view *v;
2009 6ec3d39c 2023-01-23 thomas
2010 6ec3d39c 2023-01-23 thomas /*
2011 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
2012 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
2013 6ec3d39c 2023-01-23 thomas */
2014 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
2015 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
2016 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
2017 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
2018 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
2019 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
2020 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
2021 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
2022 6ec3d39c 2023-01-23 thomas }
2023 6ec3d39c 2023-01-23 thomas }
2024 6ec3d39c 2023-01-23 thomas doupdate();
2025 6ec3d39c 2023-01-23 thomas }
2026 6ec3d39c 2023-01-23 thomas
2027 9970f7fc 2020-12-03 stsp if (view->dying) {
2028 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2029 669b5ffa 2018-10-07 stsp
2030 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2031 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2032 9970f7fc 2020-12-03 stsp entry);
2033 e78dc838 2020-12-04 stsp else if (view->parent)
2034 669b5ffa 2018-10-07 stsp prev = view->parent;
2035 669b5ffa 2018-10-07 stsp
2036 e78dc838 2020-12-04 stsp if (view->parent) {
2037 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2038 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2039 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2040 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2041 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2042 40236d76 2022-06-23 thomas if (err)
2043 40236d76 2022-06-23 thomas break;
2044 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2045 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2046 e78dc838 2020-12-04 stsp } else
2047 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2048 669b5ffa 2018-10-07 stsp
2049 9970f7fc 2020-12-03 stsp err = view_close(view);
2050 fb59748f 2020-12-05 stsp if (err)
2051 e5a0f69f 2018-08-18 stsp goto done;
2052 669b5ffa 2018-10-07 stsp
2053 e78dc838 2020-12-04 stsp view = NULL;
2054 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2055 e78dc838 2020-12-04 stsp if (v->focussed)
2056 e78dc838 2020-12-04 stsp break;
2057 0cf4efb1 2018-09-29 stsp }
2058 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2059 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2060 e78dc838 2020-12-04 stsp if (prev)
2061 e78dc838 2020-12-04 stsp view = prev;
2062 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2063 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2064 e78dc838 2020-12-04 stsp tog_view_list_head);
2065 e78dc838 2020-12-04 stsp }
2066 e78dc838 2020-12-04 stsp if (view) {
2067 e78dc838 2020-12-04 stsp if (view->focus_child) {
2068 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2069 e78dc838 2020-12-04 stsp view = view->child;
2070 e78dc838 2020-12-04 stsp } else
2071 e78dc838 2020-12-04 stsp view->focussed = 1;
2072 e78dc838 2020-12-04 stsp }
2073 e78dc838 2020-12-04 stsp }
2074 e5a0f69f 2018-08-18 stsp }
2075 bcbd79e2 2018-08-19 stsp if (new_view) {
2076 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2077 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2078 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2079 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2080 86c66b02 2018-10-18 stsp continue;
2081 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2082 86c66b02 2018-10-18 stsp err = view_close(v);
2083 86c66b02 2018-10-18 stsp if (err)
2084 86c66b02 2018-10-18 stsp goto done;
2085 86c66b02 2018-10-18 stsp break;
2086 86c66b02 2018-10-18 stsp }
2087 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2088 fed7eaa8 2018-10-24 stsp view = new_view;
2089 7cd52833 2022-06-23 thomas }
2090 6ec3d39c 2023-01-23 thomas if (view && !done) {
2091 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2092 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2093 e78dc838 2020-12-04 stsp view = view->child;
2094 e78dc838 2020-12-04 stsp } else {
2095 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2096 e78dc838 2020-12-04 stsp view = view->parent;
2097 1a76625f 2018-10-22 stsp }
2098 e78dc838 2020-12-04 stsp show_panel(view->panel);
2099 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2100 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2101 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2102 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2103 669b5ffa 2018-10-07 stsp if (err)
2104 1a76625f 2018-10-22 stsp goto done;
2105 669b5ffa 2018-10-07 stsp }
2106 669b5ffa 2018-10-07 stsp err = view->show(view);
2107 0cf4efb1 2018-09-29 stsp if (err)
2108 1a76625f 2018-10-22 stsp goto done;
2109 669b5ffa 2018-10-07 stsp if (view->child) {
2110 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2111 669b5ffa 2018-10-07 stsp if (err)
2112 1a76625f 2018-10-22 stsp goto done;
2113 669b5ffa 2018-10-07 stsp }
2114 1a76625f 2018-10-22 stsp update_panels();
2115 1a76625f 2018-10-22 stsp doupdate();
2116 0cf4efb1 2018-09-29 stsp }
2117 e5a0f69f 2018-08-18 stsp }
2118 e5a0f69f 2018-08-18 stsp done:
2119 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2120 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2121 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2122 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2123 f2d749db 2022-07-12 thomas close_err = view_close(view);
2124 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2125 f2d749db 2022-07-12 thomas err = close_err;
2126 e5a0f69f 2018-08-18 stsp }
2127 1a76625f 2018-10-22 stsp
2128 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2129 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2130 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2131 1a76625f 2018-10-22 stsp
2132 e5a0f69f 2018-08-18 stsp return err;
2133 ea5e7bb5 2018-08-01 stsp }
2134 ea5e7bb5 2018-08-01 stsp
2135 4ed7e80c 2018-05-20 stsp __dead static void
2136 9f7d7167 2018-04-29 stsp usage_log(void)
2137 9f7d7167 2018-04-29 stsp {
2138 80ddbec8 2018-04-29 stsp endwin();
2139 c70c5802 2018-08-01 stsp fprintf(stderr,
2140 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2141 9f7d7167 2018-04-29 stsp getprogname());
2142 9f7d7167 2018-04-29 stsp exit(1);
2143 80ddbec8 2018-04-29 stsp }
2144 80ddbec8 2018-04-29 stsp
2145 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2146 80ddbec8 2018-04-29 stsp static const struct got_error *
2147 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2148 963b370f 2018-05-20 stsp {
2149 00dfcb92 2018-06-11 stsp char *vis = NULL;
2150 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2151 963b370f 2018-05-20 stsp
2152 963b370f 2018-05-20 stsp *ws = NULL;
2153 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2154 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2155 00dfcb92 2018-06-11 stsp int vislen;
2156 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2157 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2158 00dfcb92 2018-06-11 stsp
2159 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2160 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2161 00dfcb92 2018-06-11 stsp if (err)
2162 00dfcb92 2018-06-11 stsp return err;
2163 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2164 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2165 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2166 a7f50699 2018-06-11 stsp goto done;
2167 a7f50699 2018-06-11 stsp }
2168 00dfcb92 2018-06-11 stsp }
2169 963b370f 2018-05-20 stsp
2170 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2171 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2172 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2173 a7f50699 2018-06-11 stsp goto done;
2174 a7f50699 2018-06-11 stsp }
2175 963b370f 2018-05-20 stsp
2176 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2178 a7f50699 2018-06-11 stsp done:
2179 00dfcb92 2018-06-11 stsp free(vis);
2180 963b370f 2018-05-20 stsp if (err) {
2181 963b370f 2018-05-20 stsp free(*ws);
2182 963b370f 2018-05-20 stsp *ws = NULL;
2183 963b370f 2018-05-20 stsp *wlen = 0;
2184 963b370f 2018-05-20 stsp }
2185 963b370f 2018-05-20 stsp return err;
2186 05171be4 2022-06-23 thomas }
2187 05171be4 2022-06-23 thomas
2188 05171be4 2022-06-23 thomas static const struct got_error *
2189 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2190 05171be4 2022-06-23 thomas {
2191 05171be4 2022-06-23 thomas char *dst;
2192 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2193 05171be4 2022-06-23 thomas
2194 05171be4 2022-06-23 thomas *ptr = NULL;
2195 05171be4 2022-06-23 thomas n = len = strlen(src);
2196 83316834 2022-06-23 thomas dst = malloc(n + 1);
2197 05171be4 2022-06-23 thomas if (dst == NULL)
2198 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2199 05171be4 2022-06-23 thomas
2200 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2201 05171be4 2022-06-23 thomas const char c = src[idx];
2202 05171be4 2022-06-23 thomas
2203 05171be4 2022-06-23 thomas if (c == '\t') {
2204 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2205 b235ad5d 2022-06-23 thomas char *p;
2206 b235ad5d 2022-06-23 thomas
2207 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2208 83316834 2022-06-23 thomas if (p == NULL) {
2209 83316834 2022-06-23 thomas free(dst);
2210 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2211 83316834 2022-06-23 thomas
2212 83316834 2022-06-23 thomas }
2213 83316834 2022-06-23 thomas dst = p;
2214 05171be4 2022-06-23 thomas n += nb;
2215 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2216 05171be4 2022-06-23 thomas sz += nb;
2217 05171be4 2022-06-23 thomas } else
2218 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2219 05171be4 2022-06-23 thomas ++idx;
2220 05171be4 2022-06-23 thomas }
2221 05171be4 2022-06-23 thomas
2222 05171be4 2022-06-23 thomas dst[sz] = '\0';
2223 05171be4 2022-06-23 thomas *ptr = dst;
2224 05171be4 2022-06-23 thomas return NULL;
2225 963b370f 2018-05-20 stsp }
2226 963b370f 2018-05-20 stsp
2227 8d208d34 2022-06-23 thomas /*
2228 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2229 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2230 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
2231 8d208d34 2022-06-23 thomas * *rcol.
2232 f91a2b48 2022-06-23 thomas */
2233 8d208d34 2022-06-23 thomas static int
2234 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2235 8d208d34 2022-06-23 thomas {
2236 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2237 f91a2b48 2022-06-23 thomas
2238 8d208d34 2022-06-23 thomas if (n == 0) {
2239 8d208d34 2022-06-23 thomas *rcol = cols;
2240 8d208d34 2022-06-23 thomas return off;
2241 8d208d34 2022-06-23 thomas }
2242 f91a2b48 2022-06-23 thomas
2243 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2244 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2245 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2246 8d208d34 2022-06-23 thomas else
2247 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2248 f91a2b48 2022-06-23 thomas
2249 8d208d34 2022-06-23 thomas if (width == -1) {
2250 8d208d34 2022-06-23 thomas width = 1;
2251 8d208d34 2022-06-23 thomas wline[i] = L'.';
2252 f91a2b48 2022-06-23 thomas }
2253 f91a2b48 2022-06-23 thomas
2254 8d208d34 2022-06-23 thomas if (cols + width > n)
2255 8d208d34 2022-06-23 thomas break;
2256 8d208d34 2022-06-23 thomas cols += width;
2257 f91a2b48 2022-06-23 thomas }
2258 f91a2b48 2022-06-23 thomas
2259 8d208d34 2022-06-23 thomas *rcol = cols;
2260 8d208d34 2022-06-23 thomas return i;
2261 f91a2b48 2022-06-23 thomas }
2262 f91a2b48 2022-06-23 thomas
2263 f91a2b48 2022-06-23 thomas /*
2264 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2265 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2266 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2267 f91a2b48 2022-06-23 thomas */
2268 f91a2b48 2022-06-23 thomas static const struct got_error *
2269 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2270 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2271 f91a2b48 2022-06-23 thomas {
2272 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2273 8d208d34 2022-06-23 thomas int cols;
2274 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2275 05171be4 2022-06-23 thomas char *exstr = NULL;
2276 963b370f 2018-05-20 stsp size_t wlen;
2277 8d208d34 2022-06-23 thomas int i, scrollx;
2278 963b370f 2018-05-20 stsp
2279 963b370f 2018-05-20 stsp *wlinep = NULL;
2280 b700b5d6 2018-07-10 stsp *widthp = 0;
2281 963b370f 2018-05-20 stsp
2282 05171be4 2022-06-23 thomas if (expand) {
2283 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2284 05171be4 2022-06-23 thomas if (err)
2285 05171be4 2022-06-23 thomas return err;
2286 05171be4 2022-06-23 thomas }
2287 05171be4 2022-06-23 thomas
2288 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2289 05171be4 2022-06-23 thomas free(exstr);
2290 963b370f 2018-05-20 stsp if (err)
2291 963b370f 2018-05-20 stsp return err;
2292 963b370f 2018-05-20 stsp
2293 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2294 f91a2b48 2022-06-23 thomas
2295 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2296 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2297 3f670bfb 2020-12-10 stsp wlen--;
2298 3f670bfb 2020-12-10 stsp }
2299 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2300 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2301 3f670bfb 2020-12-10 stsp wlen--;
2302 3f670bfb 2020-12-10 stsp }
2303 3f670bfb 2020-12-10 stsp
2304 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2305 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2306 27a741e5 2019-09-11 stsp
2307 b700b5d6 2018-07-10 stsp if (widthp)
2308 b700b5d6 2018-07-10 stsp *widthp = cols;
2309 f91a2b48 2022-06-23 thomas if (scrollxp)
2310 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2311 963b370f 2018-05-20 stsp if (err)
2312 963b370f 2018-05-20 stsp free(wline);
2313 963b370f 2018-05-20 stsp else
2314 963b370f 2018-05-20 stsp *wlinep = wline;
2315 963b370f 2018-05-20 stsp return err;
2316 963b370f 2018-05-20 stsp }
2317 963b370f 2018-05-20 stsp
2318 8b473291 2019-02-21 stsp static const struct got_error*
2319 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2320 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2321 8b473291 2019-02-21 stsp {
2322 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2323 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2324 8b473291 2019-02-21 stsp char *s;
2325 8b473291 2019-02-21 stsp const char *name;
2326 8b473291 2019-02-21 stsp
2327 8b473291 2019-02-21 stsp *refs_str = NULL;
2328 8b473291 2019-02-21 stsp
2329 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2330 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2331 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2332 52b5abe1 2019-08-13 stsp int cmp;
2333 52b5abe1 2019-08-13 stsp
2334 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2335 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2336 8b473291 2019-02-21 stsp continue;
2337 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2338 8b473291 2019-02-21 stsp name += 5;
2339 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2340 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2341 7143d404 2019-03-12 stsp continue;
2342 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2343 8b473291 2019-02-21 stsp name += 6;
2344 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2345 8b473291 2019-02-21 stsp name += 8;
2346 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2347 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2348 79cc719f 2020-04-24 stsp continue;
2349 79cc719f 2020-04-24 stsp }
2350 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2351 48cae60d 2020-09-22 stsp if (err)
2352 48cae60d 2020-09-22 stsp break;
2353 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2354 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2355 5d844a1e 2019-08-13 stsp if (err) {
2356 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2357 48cae60d 2020-09-22 stsp free(ref_id);
2358 5d844a1e 2019-08-13 stsp break;
2359 48cae60d 2020-09-22 stsp }
2360 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2361 5d844a1e 2019-08-13 stsp err = NULL;
2362 5d844a1e 2019-08-13 stsp tag = NULL;
2363 5d844a1e 2019-08-13 stsp }
2364 52b5abe1 2019-08-13 stsp }
2365 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2366 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2367 48cae60d 2020-09-22 stsp free(ref_id);
2368 52b5abe1 2019-08-13 stsp if (tag)
2369 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2370 52b5abe1 2019-08-13 stsp if (cmp != 0)
2371 52b5abe1 2019-08-13 stsp continue;
2372 8b473291 2019-02-21 stsp s = *refs_str;
2373 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2374 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2375 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2376 8b473291 2019-02-21 stsp free(s);
2377 8b473291 2019-02-21 stsp *refs_str = NULL;
2378 8b473291 2019-02-21 stsp break;
2379 8b473291 2019-02-21 stsp }
2380 8b473291 2019-02-21 stsp free(s);
2381 8b473291 2019-02-21 stsp }
2382 8b473291 2019-02-21 stsp
2383 8b473291 2019-02-21 stsp return err;
2384 8b473291 2019-02-21 stsp }
2385 8b473291 2019-02-21 stsp
2386 963b370f 2018-05-20 stsp static const struct got_error *
2387 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2388 27a741e5 2019-09-11 stsp int col_tab_align)
2389 5813d178 2019-03-09 stsp {
2390 e6b8b890 2020-12-29 naddy char *smallerthan;
2391 5813d178 2019-03-09 stsp
2392 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2393 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2394 5813d178 2019-03-09 stsp author = smallerthan + 1;
2395 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2396 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2397 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2398 5813d178 2019-03-09 stsp }
2399 5813d178 2019-03-09 stsp
2400 5813d178 2019-03-09 stsp static const struct got_error *
2401 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2402 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2403 8fdc79fe 2020-12-01 naddy int author_display_cols)
2404 80ddbec8 2018-04-29 stsp {
2405 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2406 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2407 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2408 61dc16bb 2023-05-17 thomas char *refs_str = NULL;
2409 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2410 5813d178 2019-03-09 stsp char *author = NULL;
2411 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2412 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2413 9472af95 2023-05-15 thomas size_t wrefstr_len = 0;
2414 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2415 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2416 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2417 ccb26ccd 2018-11-05 stsp struct tm tm;
2418 45d799e2 2018-12-23 stsp time_t committer_time;
2419 11b20872 2019-11-08 stsp struct tog_color *tc;
2420 9472af95 2023-05-15 thomas struct got_reflist_head *refs;
2421 80ddbec8 2018-04-29 stsp
2422 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2423 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2424 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2425 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2426 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2427 b39d25c7 2018-07-10 stsp
2428 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2429 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2430 b39d25c7 2018-07-10 stsp else
2431 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2432 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2433 11b20872 2019-11-08 stsp if (tc)
2434 11b20872 2019-11-08 stsp wattr_on(view->window,
2435 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2436 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2437 11b20872 2019-11-08 stsp if (tc)
2438 11b20872 2019-11-08 stsp wattr_off(view->window,
2439 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2440 27a741e5 2019-09-11 stsp col = limit;
2441 b39d25c7 2018-07-10 stsp if (col > avail)
2442 b39d25c7 2018-07-10 stsp goto done;
2443 6570a66d 2019-11-08 stsp
2444 6570a66d 2019-11-08 stsp if (avail >= 120) {
2445 6570a66d 2019-11-08 stsp char *id_str;
2446 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2447 6570a66d 2019-11-08 stsp if (err)
2448 6570a66d 2019-11-08 stsp goto done;
2449 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2450 11b20872 2019-11-08 stsp if (tc)
2451 11b20872 2019-11-08 stsp wattr_on(view->window,
2452 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2453 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2454 11b20872 2019-11-08 stsp if (tc)
2455 11b20872 2019-11-08 stsp wattr_off(view->window,
2456 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2457 6570a66d 2019-11-08 stsp free(id_str);
2458 6570a66d 2019-11-08 stsp col += 9;
2459 6570a66d 2019-11-08 stsp if (col > avail)
2460 6570a66d 2019-11-08 stsp goto done;
2461 6570a66d 2019-11-08 stsp }
2462 b39d25c7 2018-07-10 stsp
2463 f69c5a46 2022-07-19 thomas if (s->use_committer)
2464 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2465 f69c5a46 2022-07-19 thomas else
2466 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2467 5813d178 2019-03-09 stsp if (author == NULL) {
2468 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2469 80ddbec8 2018-04-29 stsp goto done;
2470 80ddbec8 2018-04-29 stsp }
2471 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2472 bb737323 2018-05-20 stsp if (err)
2473 bb737323 2018-05-20 stsp goto done;
2474 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2475 11b20872 2019-11-08 stsp if (tc)
2476 11b20872 2019-11-08 stsp wattr_on(view->window,
2477 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2478 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2479 bb737323 2018-05-20 stsp col += author_width;
2480 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2481 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2482 bb737323 2018-05-20 stsp col++;
2483 bb737323 2018-05-20 stsp author_width++;
2484 bb737323 2018-05-20 stsp }
2485 f31d6c3b 2022-09-09 thomas if (tc)
2486 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2487 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2488 9c2eaf34 2018-05-20 stsp if (col > avail)
2489 9c2eaf34 2018-05-20 stsp goto done;
2490 80ddbec8 2018-04-29 stsp
2491 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2492 5943eee2 2019-08-13 stsp if (err)
2493 6d9fbc00 2018-04-29 stsp goto done;
2494 bb737323 2018-05-20 stsp logmsg = logmsg0;
2495 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2496 bb737323 2018-05-20 stsp logmsg++;
2497 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2498 bb737323 2018-05-20 stsp if (newline)
2499 bb737323 2018-05-20 stsp *newline = '\0';
2500 9472af95 2023-05-15 thomas
2501 9472af95 2023-05-15 thomas /* Prepend reference labels to log message if possible .*/
2502 9472af95 2023-05-15 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2503 61dc16bb 2023-05-17 thomas if (refs)
2504 9472af95 2023-05-15 thomas err = build_refs_str(&refs_str, refs, id, s->repo);
2505 61dc16bb 2023-05-17 thomas if (err)
2506 61dc16bb 2023-05-17 thomas goto done;
2507 61dc16bb 2023-05-17 thomas if (refs_str) {
2508 61dc16bb 2023-05-17 thomas char *newlogmsg;
2509 61dc16bb 2023-05-17 thomas wchar_t *ws;
2510 9472af95 2023-05-15 thomas
2511 9472af95 2023-05-15 thomas /*
2512 9472af95 2023-05-15 thomas * The length of this wide-char sub-string will be
2513 9472af95 2023-05-15 thomas * needed later for colorization.
2514 9472af95 2023-05-15 thomas */
2515 f292b271 2023-05-15 thomas err = mbs2ws(&ws, &wrefstr_len, refs_str);
2516 9472af95 2023-05-15 thomas if (err)
2517 9472af95 2023-05-15 thomas goto done;
2518 9472af95 2023-05-15 thomas free(ws);
2519 9472af95 2023-05-15 thomas
2520 f292b271 2023-05-15 thomas wrefstr_len += 2; /* account for '[' and ']' */
2521 f292b271 2023-05-15 thomas
2522 f292b271 2023-05-15 thomas if (asprintf(&newlogmsg, "[%s] %s", refs_str, logmsg) == -1) {
2523 9472af95 2023-05-15 thomas err = got_error_from_errno("asprintf");
2524 9472af95 2023-05-15 thomas goto done;
2525 9472af95 2023-05-15 thomas }
2526 9472af95 2023-05-15 thomas
2527 9472af95 2023-05-15 thomas free(logmsg0);
2528 9472af95 2023-05-15 thomas logmsg0 = newlogmsg;
2529 9472af95 2023-05-15 thomas logmsg = logmsg0;
2530 9472af95 2023-05-15 thomas }
2531 9472af95 2023-05-15 thomas
2532 f91a2b48 2022-06-23 thomas limit = avail - col;
2533 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2534 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2535 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2536 f91a2b48 2022-06-23 thomas limit, col, 1);
2537 331b1a16 2022-06-23 thomas if (err)
2538 331b1a16 2022-06-23 thomas goto done;
2539 9472af95 2023-05-15 thomas if (wrefstr_len > 0 && scrollx < wrefstr_len) {
2540 9472af95 2023-05-15 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2541 9472af95 2023-05-15 thomas if (tc)
2542 9472af95 2023-05-15 thomas wattr_on(view->window,
2543 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2544 9472af95 2023-05-15 thomas waddnwstr(view->window, &wlogmsg[scrollx],
2545 9472af95 2023-05-15 thomas wrefstr_len - scrollx);
2546 9472af95 2023-05-15 thomas if (tc)
2547 9472af95 2023-05-15 thomas wattr_off(view->window,
2548 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2549 9472af95 2023-05-15 thomas waddwstr(view->window, &wlogmsg[wrefstr_len]);
2550 9472af95 2023-05-15 thomas } else
2551 9472af95 2023-05-15 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2552 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2553 27a741e5 2019-09-11 stsp while (col < avail) {
2554 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2555 bb737323 2018-05-20 stsp col++;
2556 881b2d3e 2018-04-30 stsp }
2557 80ddbec8 2018-04-29 stsp done:
2558 80ddbec8 2018-04-29 stsp free(logmsg0);
2559 bb737323 2018-05-20 stsp free(wlogmsg);
2560 61dc16bb 2023-05-17 thomas free(refs_str);
2561 5813d178 2019-03-09 stsp free(author);
2562 bb737323 2018-05-20 stsp free(wauthor);
2563 80ddbec8 2018-04-29 stsp free(line);
2564 80ddbec8 2018-04-29 stsp return err;
2565 80ddbec8 2018-04-29 stsp }
2566 26ed57b2 2018-05-19 stsp
2567 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2568 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2569 899d86c2 2018-05-10 stsp struct got_object_id *id)
2570 80ddbec8 2018-04-29 stsp {
2571 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2572 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2573 80ddbec8 2018-04-29 stsp
2574 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2575 80ddbec8 2018-04-29 stsp if (entry == NULL)
2576 899d86c2 2018-05-10 stsp return NULL;
2577 99db9666 2018-05-07 stsp
2578 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2579 d68b9737 2022-09-05 thomas if (dup == NULL) {
2580 d68b9737 2022-09-05 thomas free(entry);
2581 d68b9737 2022-09-05 thomas return NULL;
2582 d68b9737 2022-09-05 thomas }
2583 d68b9737 2022-09-05 thomas
2584 d68b9737 2022-09-05 thomas entry->id = dup;
2585 99db9666 2018-05-07 stsp entry->commit = commit;
2586 899d86c2 2018-05-10 stsp return entry;
2587 99db9666 2018-05-07 stsp }
2588 80ddbec8 2018-04-29 stsp
2589 99db9666 2018-05-07 stsp static void
2590 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2591 99db9666 2018-05-07 stsp {
2592 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2593 99db9666 2018-05-07 stsp
2594 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2595 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2596 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2597 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2598 d68b9737 2022-09-05 thomas free(entry->id);
2599 99db9666 2018-05-07 stsp free(entry);
2600 99db9666 2018-05-07 stsp }
2601 99db9666 2018-05-07 stsp
2602 99db9666 2018-05-07 stsp static void
2603 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2604 99db9666 2018-05-07 stsp {
2605 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2606 99db9666 2018-05-07 stsp pop_commit(commits);
2607 c4972b91 2018-05-07 stsp }
2608 c4972b91 2018-05-07 stsp
2609 c4972b91 2018-05-07 stsp static const struct got_error *
2610 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2611 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2612 13add988 2019-10-15 stsp {
2613 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2614 13add988 2019-10-15 stsp regmatch_t regmatch;
2615 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2616 13add988 2019-10-15 stsp
2617 13add988 2019-10-15 stsp *have_match = 0;
2618 13add988 2019-10-15 stsp
2619 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2620 13add988 2019-10-15 stsp if (err)
2621 13add988 2019-10-15 stsp return err;
2622 13add988 2019-10-15 stsp
2623 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2624 13add988 2019-10-15 stsp if (err)
2625 13add988 2019-10-15 stsp goto done;
2626 13add988 2019-10-15 stsp
2627 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2628 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2629 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2630 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2631 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2632 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2633 13add988 2019-10-15 stsp *have_match = 1;
2634 13add988 2019-10-15 stsp done:
2635 13add988 2019-10-15 stsp free(id_str);
2636 13add988 2019-10-15 stsp free(logmsg);
2637 13add988 2019-10-15 stsp return err;
2638 13add988 2019-10-15 stsp }
2639 13add988 2019-10-15 stsp
2640 13add988 2019-10-15 stsp static const struct got_error *
2641 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2642 c4972b91 2018-05-07 stsp {
2643 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2644 9ba79e04 2018-06-11 stsp
2645 1a76625f 2018-10-22 stsp /*
2646 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2647 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2648 1a76625f 2018-10-22 stsp * while updating the display.
2649 1a76625f 2018-10-22 stsp */
2650 4e0d2870 2020-12-07 naddy do {
2651 7210b715 2022-09-11 thomas struct got_object_id id;
2652 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2653 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2654 7e8004ba 2022-09-11 thomas int limit_match = 0;
2655 1a76625f 2018-10-22 stsp int errcode;
2656 899d86c2 2018-05-10 stsp
2657 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2658 4e0d2870 2020-12-07 naddy NULL, NULL);
2659 7210b715 2022-09-11 thomas if (err)
2660 ecb28ae0 2018-07-16 stsp break;
2661 899d86c2 2018-05-10 stsp
2662 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2663 9ba79e04 2018-06-11 stsp if (err)
2664 9ba79e04 2018-06-11 stsp break;
2665 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2666 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2667 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2668 9ba79e04 2018-06-11 stsp break;
2669 9ba79e04 2018-06-11 stsp }
2670 93e45b7c 2018-09-24 stsp
2671 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2672 1a76625f 2018-10-22 stsp if (errcode) {
2673 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2674 13add988 2019-10-15 stsp "pthread_mutex_lock");
2675 1a76625f 2018-10-22 stsp break;
2676 1a76625f 2018-10-22 stsp }
2677 1a76625f 2018-10-22 stsp
2678 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2679 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2680 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2681 1a76625f 2018-10-22 stsp
2682 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2683 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2684 7e8004ba 2022-09-11 thomas a->limit_regex);
2685 7e8004ba 2022-09-11 thomas if (err)
2686 7e8004ba 2022-09-11 thomas break;
2687 7e8004ba 2022-09-11 thomas
2688 7e8004ba 2022-09-11 thomas if (limit_match) {
2689 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2690 7e8004ba 2022-09-11 thomas
2691 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2692 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2693 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2694 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2695 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2696 7e8004ba 2022-09-11 thomas break;
2697 7e8004ba 2022-09-11 thomas }
2698 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2699 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2700 7e8004ba 2022-09-11 thomas
2701 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2702 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2703 7e8004ba 2022-09-11 thomas matched, entry);
2704 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2705 7e8004ba 2022-09-11 thomas }
2706 7e8004ba 2022-09-11 thomas
2707 7e8004ba 2022-09-11 thomas /*
2708 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2709 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2710 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2711 7e8004ba 2022-09-11 thomas */
2712 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2713 7e8004ba 2022-09-11 thomas }
2714 7e8004ba 2022-09-11 thomas
2715 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2716 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2717 7c1452c1 2020-03-26 stsp int have_match;
2718 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2719 7c1452c1 2020-03-26 stsp if (err)
2720 7c1452c1 2020-03-26 stsp break;
2721 7e8004ba 2022-09-11 thomas
2722 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2723 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2724 7e8004ba 2022-09-11 thomas *a->search_next_done =
2725 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2726 7e8004ba 2022-09-11 thomas } else if (have_match)
2727 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2728 13add988 2019-10-15 stsp }
2729 13add988 2019-10-15 stsp
2730 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2731 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2732 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2733 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2734 7c1452c1 2020-03-26 stsp if (err)
2735 13add988 2019-10-15 stsp break;
2736 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2737 899d86c2 2018-05-10 stsp
2738 9ba79e04 2018-06-11 stsp return err;
2739 0553a4e3 2018-04-30 stsp }
2740 0553a4e3 2018-04-30 stsp
2741 2b779855 2020-12-05 naddy static void
2742 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2743 2b779855 2020-12-05 naddy {
2744 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2745 2b779855 2020-12-05 naddy int ncommits = 0;
2746 2b779855 2020-12-05 naddy
2747 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2748 2b779855 2020-12-05 naddy while (entry) {
2749 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2750 2b779855 2020-12-05 naddy s->selected_entry = entry;
2751 2b779855 2020-12-05 naddy break;
2752 2b779855 2020-12-05 naddy }
2753 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2754 2b779855 2020-12-05 naddy ncommits++;
2755 2b779855 2020-12-05 naddy }
2756 2b779855 2020-12-05 naddy }
2757 2b779855 2020-12-05 naddy
2758 0553a4e3 2018-04-30 stsp static const struct got_error *
2759 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2760 0553a4e3 2018-04-30 stsp {
2761 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2762 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2763 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2764 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2765 60493ae3 2019-06-20 stsp int width;
2766 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2767 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2768 8b473291 2019-02-21 stsp char *refs_str = NULL;
2769 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2770 11b20872 2019-11-08 stsp struct tog_color *tc;
2771 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2772 bd3f8225 2022-08-12 thomas
2773 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2774 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2775 0553a4e3 2018-04-30 stsp
2776 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2777 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2778 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2779 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2780 1a76625f 2018-10-22 stsp if (err)
2781 ecb28ae0 2018-07-16 stsp return err;
2782 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2783 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2784 d2075bf3 2020-12-25 stsp if (refs) {
2785 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2786 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2787 d2075bf3 2020-12-25 stsp if (err)
2788 d2075bf3 2020-12-25 stsp goto done;
2789 d2075bf3 2020-12-25 stsp }
2790 867c6645 2018-07-10 stsp }
2791 359bfafd 2019-02-22 stsp
2792 557d3365 2023-04-14 thomas if (s->thread_args.commits_needed == 0 && !using_mock_io)
2793 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2794 1a76625f 2018-10-22 stsp
2795 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2796 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2797 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2798 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2799 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2800 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2801 8f4ed634 2020-03-26 stsp goto done;
2802 8f4ed634 2020-03-26 stsp }
2803 8f4ed634 2020-03-26 stsp } else {
2804 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2805 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2806 f9686aa5 2020-03-27 stsp
2807 f9686aa5 2020-03-27 stsp if (view->searching) {
2808 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2809 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2810 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2811 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2812 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2813 f9686aa5 2020-03-27 stsp search_str = "searching...";
2814 f9686aa5 2020-03-27 stsp }
2815 f9686aa5 2020-03-27 stsp
2816 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2817 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2818 7e8004ba 2022-09-11 thomas
2819 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2820 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2821 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2822 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2823 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2824 8f4ed634 2020-03-26 stsp goto done;
2825 8f4ed634 2020-03-26 stsp }
2826 8b473291 2019-02-21 stsp }
2827 1a76625f 2018-10-22 stsp
2828 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2829 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2830 91198554 2022-06-23 thomas "........................................",
2831 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2832 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2833 1a76625f 2018-10-22 stsp header = NULL;
2834 1a76625f 2018-10-22 stsp goto done;
2835 1a76625f 2018-10-22 stsp }
2836 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2837 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2838 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2839 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2840 1a76625f 2018-10-22 stsp header = NULL;
2841 1a76625f 2018-10-22 stsp goto done;
2842 ecb28ae0 2018-07-16 stsp }
2843 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2844 1a76625f 2018-10-22 stsp if (err)
2845 1a76625f 2018-10-22 stsp goto done;
2846 867c6645 2018-07-10 stsp
2847 2814baeb 2018-08-01 stsp werase(view->window);
2848 867c6645 2018-07-10 stsp
2849 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2850 a3404814 2018-09-02 stsp wstandout(view->window);
2851 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2852 11b20872 2019-11-08 stsp if (tc)
2853 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2854 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2855 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2856 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2857 1a76625f 2018-10-22 stsp width++;
2858 1a76625f 2018-10-22 stsp }
2859 86f4aab9 2022-09-09 thomas if (tc)
2860 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2861 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2862 a3404814 2018-09-02 stsp wstandend(view->window);
2863 ecb28ae0 2018-07-16 stsp free(wline);
2864 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2865 1a76625f 2018-10-22 stsp goto done;
2866 0553a4e3 2018-04-30 stsp
2867 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2868 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2869 5813d178 2019-03-09 stsp ncommits = 0;
2870 05171be4 2022-06-23 thomas view->maxx = 0;
2871 5813d178 2019-03-09 stsp while (entry) {
2872 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2873 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2874 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2875 5813d178 2019-03-09 stsp int width;
2876 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2877 5813d178 2019-03-09 stsp break;
2878 f69c5a46 2022-07-19 thomas if (s->use_committer)
2879 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2880 f69c5a46 2022-07-19 thomas else
2881 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2882 5813d178 2019-03-09 stsp if (author == NULL) {
2883 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2884 5813d178 2019-03-09 stsp goto done;
2885 5813d178 2019-03-09 stsp }
2886 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2887 27a741e5 2019-09-11 stsp date_display_cols);
2888 5813d178 2019-03-09 stsp if (author_cols < width)
2889 5813d178 2019-03-09 stsp author_cols = width;
2890 5813d178 2019-03-09 stsp free(wauthor);
2891 5813d178 2019-03-09 stsp free(author);
2892 ef944b8b 2022-07-21 thomas if (err)
2893 ef944b8b 2022-07-21 thomas goto done;
2894 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2895 05171be4 2022-06-23 thomas if (err)
2896 05171be4 2022-06-23 thomas goto done;
2897 05171be4 2022-06-23 thomas msg = msg0;
2898 05171be4 2022-06-23 thomas while (*msg == '\n')
2899 05171be4 2022-06-23 thomas ++msg;
2900 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2901 331b1a16 2022-06-23 thomas *eol = '\0';
2902 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2903 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2904 331b1a16 2022-06-23 thomas if (err)
2905 331b1a16 2022-06-23 thomas goto done;
2906 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2907 05171be4 2022-06-23 thomas free(msg0);
2908 331b1a16 2022-06-23 thomas free(wmsg);
2909 7ca04879 2019-10-19 stsp ncommits++;
2910 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2911 5813d178 2019-03-09 stsp }
2912 5813d178 2019-03-09 stsp
2913 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2914 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2915 867c6645 2018-07-10 stsp ncommits = 0;
2916 899d86c2 2018-05-10 stsp while (entry) {
2917 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2918 899d86c2 2018-05-10 stsp break;
2919 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2920 2814baeb 2018-08-01 stsp wstandout(view->window);
2921 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2922 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2923 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2924 2814baeb 2018-08-01 stsp wstandend(view->window);
2925 0553a4e3 2018-04-30 stsp if (err)
2926 60493ae3 2019-06-20 stsp goto done;
2927 0553a4e3 2018-04-30 stsp ncommits++;
2928 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2929 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2930 80ddbec8 2018-04-29 stsp }
2931 80ddbec8 2018-04-29 stsp
2932 a5d43cac 2022-07-01 thomas view_border(view);
2933 1a76625f 2018-10-22 stsp done:
2934 1a76625f 2018-10-22 stsp free(id_str);
2935 8b473291 2019-02-21 stsp free(refs_str);
2936 1a76625f 2018-10-22 stsp free(ncommits_str);
2937 1a76625f 2018-10-22 stsp free(header);
2938 80ddbec8 2018-04-29 stsp return err;
2939 9f7d7167 2018-04-29 stsp }
2940 07b55e75 2018-05-10 stsp
2941 07b55e75 2018-05-10 stsp static void
2942 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2943 07b55e75 2018-05-10 stsp {
2944 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2945 07b55e75 2018-05-10 stsp int nscrolled = 0;
2946 07b55e75 2018-05-10 stsp
2947 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2948 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2949 07b55e75 2018-05-10 stsp return;
2950 9f7d7167 2018-04-29 stsp
2951 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2952 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2953 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2954 07b55e75 2018-05-10 stsp if (entry) {
2955 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2956 07b55e75 2018-05-10 stsp nscrolled++;
2957 07b55e75 2018-05-10 stsp }
2958 07b55e75 2018-05-10 stsp }
2959 aa075928 2018-05-10 stsp }
2960 aa075928 2018-05-10 stsp
2961 aa075928 2018-05-10 stsp static const struct got_error *
2962 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2963 aa075928 2018-05-10 stsp {
2964 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2965 5e224a3e 2019-02-22 stsp int errcode;
2966 8a42fca8 2019-02-22 stsp
2967 557d3365 2023-04-14 thomas if (!using_mock_io)
2968 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while loading commits */
2969 aa075928 2018-05-10 stsp
2970 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2971 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2972 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2973 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2974 7aafa0d1 2019-02-22 stsp if (errcode)
2975 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2976 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2977 7c1452c1 2020-03-26 stsp
2978 7c1452c1 2020-03-26 stsp /*
2979 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2980 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2981 7c1452c1 2020-03-26 stsp */
2982 7c1452c1 2020-03-26 stsp if (!wait)
2983 7c1452c1 2020-03-26 stsp break;
2984 7c1452c1 2020-03-26 stsp
2985 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2986 ffe38506 2020-12-01 naddy show_log_view(view);
2987 7c1452c1 2020-03-26 stsp update_panels();
2988 7c1452c1 2020-03-26 stsp doupdate();
2989 7c1452c1 2020-03-26 stsp
2990 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2991 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2992 82954512 2020-02-03 stsp if (errcode)
2993 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2994 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2995 82954512 2020-02-03 stsp
2996 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2997 ffe38506 2020-12-01 naddy show_log_view(view);
2998 7c1452c1 2020-03-26 stsp update_panels();
2999 7c1452c1 2020-03-26 stsp doupdate();
3000 5e224a3e 2019-02-22 stsp }
3001 5e224a3e 2019-02-22 stsp
3002 5e224a3e 2019-02-22 stsp return NULL;
3003 a5d43cac 2022-07-01 thomas }
3004 a5d43cac 2022-07-01 thomas
3005 a5d43cac 2022-07-01 thomas static const struct got_error *
3006 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
3007 a5d43cac 2022-07-01 thomas {
3008 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
3009 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3010 fe731b51 2022-07-14 thomas
3011 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
3012 fe731b51 2022-07-14 thomas return NULL;
3013 a5d43cac 2022-07-01 thomas
3014 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
3015 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
3016 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3017 a5d43cac 2022-07-01 thomas
3018 a5d43cac 2022-07-01 thomas return err;
3019 5e224a3e 2019-02-22 stsp }
3020 5e224a3e 2019-02-22 stsp
3021 5e224a3e 2019-02-22 stsp static const struct got_error *
3022 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
3023 5e224a3e 2019-02-22 stsp {
3024 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
3025 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
3026 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
3027 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
3028 5e224a3e 2019-02-22 stsp
3029 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
3030 5e224a3e 2019-02-22 stsp return NULL;
3031 5e224a3e 2019-02-22 stsp
3032 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3033 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
3034 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
3035 08ebd0a9 2019-02-22 stsp /*
3036 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
3037 08ebd0a9 2019-02-22 stsp */
3038 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
3039 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
3040 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3041 5e224a3e 2019-02-22 stsp if (err)
3042 5e224a3e 2019-02-22 stsp return err;
3043 7aafa0d1 2019-02-22 stsp }
3044 b295e71b 2019-02-22 stsp
3045 7aafa0d1 2019-02-22 stsp do {
3046 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3047 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3048 88048b54 2019-02-21 stsp break;
3049 88048b54 2019-02-21 stsp
3050 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
3051 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
3052 aa075928 2018-05-10 stsp
3053 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3054 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
3055 dd0a52c1 2018-05-20 stsp break;
3056 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
3057 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
3058 aa075928 2018-05-10 stsp
3059 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3060 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
3061 a5d43cac 2022-07-01 thomas else
3062 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3063 a5d43cac 2022-07-01 thomas
3064 dd0a52c1 2018-05-20 stsp return err;
3065 07b55e75 2018-05-10 stsp }
3066 4a7f7875 2018-05-10 stsp
3067 cd0acaa7 2018-05-20 stsp static const struct got_error *
3068 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3069 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
3070 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3071 cd0acaa7 2018-05-20 stsp {
3072 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3073 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3074 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3075 cd0acaa7 2018-05-20 stsp
3076 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3077 15a94983 2018-12-23 stsp if (diff_view == NULL)
3078 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3079 ea5e7bb5 2018-08-01 stsp
3080 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3081 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3082 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3083 e5a0f69f 2018-08-18 stsp if (err == NULL)
3084 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3085 cd0acaa7 2018-05-20 stsp return err;
3086 4a7f7875 2018-05-10 stsp }
3087 4a7f7875 2018-05-10 stsp
3088 80ddbec8 2018-04-29 stsp static const struct got_error *
3089 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3090 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3091 9343a5fb 2018-06-23 stsp {
3092 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3093 941e9f74 2019-05-21 stsp
3094 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3095 941e9f74 2019-05-21 stsp if (parent == NULL)
3096 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3097 941e9f74 2019-05-21 stsp
3098 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3099 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3100 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3101 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3102 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3103 941e9f74 2019-05-21 stsp s->tree = subtree;
3104 941e9f74 2019-05-21 stsp s->selected = 0;
3105 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3106 941e9f74 2019-05-21 stsp return NULL;
3107 941e9f74 2019-05-21 stsp }
3108 941e9f74 2019-05-21 stsp
3109 941e9f74 2019-05-21 stsp static const struct got_error *
3110 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3111 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3112 941e9f74 2019-05-21 stsp {
3113 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3114 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3115 941e9f74 2019-05-21 stsp const char *p;
3116 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3117 9343a5fb 2018-06-23 stsp
3118 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3119 941e9f74 2019-05-21 stsp p = path;
3120 941e9f74 2019-05-21 stsp while (*p) {
3121 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3122 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3123 56e0773d 2019-11-28 stsp char *te_name;
3124 33cbf02b 2020-01-12 stsp
3125 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3126 33cbf02b 2020-01-12 stsp p++;
3127 941e9f74 2019-05-21 stsp
3128 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3129 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3130 941e9f74 2019-05-21 stsp if (slash == NULL)
3131 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3132 33cbf02b 2020-01-12 stsp else
3133 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3134 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3135 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3136 56e0773d 2019-11-28 stsp break;
3137 941e9f74 2019-05-21 stsp }
3138 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3139 56e0773d 2019-11-28 stsp if (te == NULL) {
3140 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3141 56e0773d 2019-11-28 stsp free(te_name);
3142 941e9f74 2019-05-21 stsp break;
3143 941e9f74 2019-05-21 stsp }
3144 56e0773d 2019-11-28 stsp free(te_name);
3145 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3146 941e9f74 2019-05-21 stsp
3147 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3148 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3149 b03c880f 2019-05-21 stsp
3150 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3151 941e9f74 2019-05-21 stsp if (slash)
3152 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3153 941e9f74 2019-05-21 stsp else
3154 941e9f74 2019-05-21 stsp subpath = strdup(path);
3155 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3156 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3157 941e9f74 2019-05-21 stsp break;
3158 941e9f74 2019-05-21 stsp }
3159 941e9f74 2019-05-21 stsp
3160 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3161 941e9f74 2019-05-21 stsp subpath);
3162 941e9f74 2019-05-21 stsp if (err)
3163 941e9f74 2019-05-21 stsp break;
3164 941e9f74 2019-05-21 stsp
3165 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3166 941e9f74 2019-05-21 stsp free(tree_id);
3167 941e9f74 2019-05-21 stsp if (err)
3168 941e9f74 2019-05-21 stsp break;
3169 941e9f74 2019-05-21 stsp
3170 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3171 941e9f74 2019-05-21 stsp if (err) {
3172 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3173 941e9f74 2019-05-21 stsp break;
3174 941e9f74 2019-05-21 stsp }
3175 941e9f74 2019-05-21 stsp if (slash == NULL)
3176 941e9f74 2019-05-21 stsp break;
3177 941e9f74 2019-05-21 stsp free(subpath);
3178 941e9f74 2019-05-21 stsp subpath = NULL;
3179 941e9f74 2019-05-21 stsp p = slash;
3180 941e9f74 2019-05-21 stsp }
3181 941e9f74 2019-05-21 stsp
3182 941e9f74 2019-05-21 stsp free(subpath);
3183 1a76625f 2018-10-22 stsp return err;
3184 61266923 2020-01-14 stsp }
3185 61266923 2020-01-14 stsp
3186 61266923 2020-01-14 stsp static const struct got_error *
3187 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3188 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3189 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3190 55cccc34 2020-02-20 stsp {
3191 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3192 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3193 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3194 55cccc34 2020-02-20 stsp
3195 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3196 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3197 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3198 55cccc34 2020-02-20 stsp
3199 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3200 bc573f3b 2021-07-10 stsp if (err)
3201 55cccc34 2020-02-20 stsp return err;
3202 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3203 55cccc34 2020-02-20 stsp
3204 55cccc34 2020-02-20 stsp *new_view = tree_view;
3205 55cccc34 2020-02-20 stsp
3206 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3207 55cccc34 2020-02-20 stsp return NULL;
3208 55cccc34 2020-02-20 stsp
3209 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3210 55cccc34 2020-02-20 stsp }
3211 55cccc34 2020-02-20 stsp
3212 55cccc34 2020-02-20 stsp static const struct got_error *
3213 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3214 61266923 2020-01-14 stsp {
3215 61266923 2020-01-14 stsp sigset_t sigset;
3216 61266923 2020-01-14 stsp int errcode;
3217 61266923 2020-01-14 stsp
3218 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3219 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3220 61266923 2020-01-14 stsp
3221 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3222 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3223 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3224 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3225 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3226 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3227 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3228 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3229 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3230 61266923 2020-01-14 stsp
3231 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3232 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3233 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3234 61266923 2020-01-14 stsp
3235 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3236 61266923 2020-01-14 stsp if (errcode)
3237 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3238 61266923 2020-01-14 stsp
3239 61266923 2020-01-14 stsp return NULL;
3240 1a76625f 2018-10-22 stsp }
3241 1a76625f 2018-10-22 stsp
3242 1a76625f 2018-10-22 stsp static void *
3243 1a76625f 2018-10-22 stsp log_thread(void *arg)
3244 1a76625f 2018-10-22 stsp {
3245 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3246 1a76625f 2018-10-22 stsp int errcode = 0;
3247 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3248 1a76625f 2018-10-22 stsp int done = 0;
3249 61266923 2020-01-14 stsp
3250 f2d749db 2022-07-12 thomas /*
3251 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3252 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3253 f2d749db 2022-07-12 thomas */
3254 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3255 f2d749db 2022-07-12 thomas if (errcode) {
3256 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3257 61266923 2020-01-14 stsp return (void *)err;
3258 f2d749db 2022-07-12 thomas }
3259 1a76625f 2018-10-22 stsp
3260 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3261 f2d749db 2022-07-12 thomas if (err) {
3262 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3263 f2d749db 2022-07-12 thomas goto done;
3264 f2d749db 2022-07-12 thomas }
3265 f2d749db 2022-07-12 thomas
3266 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3267 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3268 f2d749db 2022-07-12 thomas if (errcode) {
3269 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3270 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3271 f2d749db 2022-07-12 thomas goto done;
3272 f2d749db 2022-07-12 thomas }
3273 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3274 1a76625f 2018-10-22 stsp if (err) {
3275 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3276 f2d749db 2022-07-12 thomas goto done;
3277 1a76625f 2018-10-22 stsp err = NULL;
3278 1a76625f 2018-10-22 stsp done = 1;
3279 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3280 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3281 7e8004ba 2022-09-11 thomas if (a->limit_match)
3282 7e8004ba 2022-09-11 thomas a->commits_needed--;
3283 7e8004ba 2022-09-11 thomas } else
3284 7e8004ba 2022-09-11 thomas a->commits_needed--;
3285 7e8004ba 2022-09-11 thomas }
3286 1a76625f 2018-10-22 stsp
3287 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3288 3abe8080 2019-04-10 stsp if (errcode) {
3289 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3290 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3291 f2d749db 2022-07-12 thomas goto done;
3292 3abe8080 2019-04-10 stsp } else if (*a->quit)
3293 1a76625f 2018-10-22 stsp done = 1;
3294 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3295 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3296 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3297 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3298 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3299 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3300 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3301 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3302 1a76625f 2018-10-22 stsp }
3303 1a76625f 2018-10-22 stsp
3304 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3305 7c1452c1 2020-03-26 stsp if (errcode) {
3306 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3307 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3308 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3309 f2d749db 2022-07-12 thomas goto done;
3310 7c1452c1 2020-03-26 stsp }
3311 7c1452c1 2020-03-26 stsp
3312 1a76625f 2018-10-22 stsp if (done)
3313 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3314 7c1452c1 2020-03-26 stsp else {
3315 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3316 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3317 7c1452c1 2020-03-26 stsp &tog_mutex);
3318 f2d749db 2022-07-12 thomas if (errcode) {
3319 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3320 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3321 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3322 f2d749db 2022-07-12 thomas goto done;
3323 f2d749db 2022-07-12 thomas }
3324 21355643 2020-12-06 stsp if (*a->quit)
3325 21355643 2020-12-06 stsp done = 1;
3326 7c1452c1 2020-03-26 stsp }
3327 1a76625f 2018-10-22 stsp }
3328 1a76625f 2018-10-22 stsp }
3329 3abe8080 2019-04-10 stsp a->log_complete = 1;
3330 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3331 f2d749db 2022-07-12 thomas if (errcode)
3332 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3333 f2d749db 2022-07-12 thomas done:
3334 f2d749db 2022-07-12 thomas if (err) {
3335 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3336 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3337 f2d749db 2022-07-12 thomas }
3338 1a76625f 2018-10-22 stsp return (void *)err;
3339 1a76625f 2018-10-22 stsp }
3340 1a76625f 2018-10-22 stsp
3341 1a76625f 2018-10-22 stsp static const struct got_error *
3342 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3343 1a76625f 2018-10-22 stsp {
3344 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3345 1a76625f 2018-10-22 stsp int errcode;
3346 1a76625f 2018-10-22 stsp
3347 1a76625f 2018-10-22 stsp if (s->thread) {
3348 1a76625f 2018-10-22 stsp s->quit = 1;
3349 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3350 1a76625f 2018-10-22 stsp if (errcode)
3351 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3352 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3353 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3354 1a76625f 2018-10-22 stsp if (errcode)
3355 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3356 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3357 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3358 1a76625f 2018-10-22 stsp if (errcode)
3359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3360 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3361 1a76625f 2018-10-22 stsp if (errcode)
3362 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3363 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3364 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3365 1a76625f 2018-10-22 stsp }
3366 1a76625f 2018-10-22 stsp
3367 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3368 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3369 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3370 1af5eddf 2022-06-23 thomas }
3371 1af5eddf 2022-06-23 thomas
3372 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3373 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3374 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3375 1af5eddf 2022-06-23 thomas if (err == NULL)
3376 1af5eddf 2022-06-23 thomas err = pack_err;
3377 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3378 1a76625f 2018-10-22 stsp }
3379 1a76625f 2018-10-22 stsp
3380 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3381 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3382 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3383 1a76625f 2018-10-22 stsp }
3384 1a76625f 2018-10-22 stsp
3385 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3386 9343a5fb 2018-06-23 stsp }
3387 9343a5fb 2018-06-23 stsp
3388 9343a5fb 2018-06-23 stsp static const struct got_error *
3389 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3390 1a76625f 2018-10-22 stsp {
3391 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3392 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3393 276b94a1 2020-11-13 naddy int errcode;
3394 1a76625f 2018-10-22 stsp
3395 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3396 276b94a1 2020-11-13 naddy
3397 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3398 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3399 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3400 276b94a1 2020-11-13 naddy
3401 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3402 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3403 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3404 276b94a1 2020-11-13 naddy
3405 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3406 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3407 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3408 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3409 1a76625f 2018-10-22 stsp free(s->start_id);
3410 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3411 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3412 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3413 1a76625f 2018-10-22 stsp return err;
3414 1a76625f 2018-10-22 stsp }
3415 1a76625f 2018-10-22 stsp
3416 7e8004ba 2022-09-11 thomas /*
3417 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3418 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3419 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3420 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3421 7e8004ba 2022-09-11 thomas * works with very slight change.
3422 7e8004ba 2022-09-11 thomas */
3423 1a76625f 2018-10-22 stsp static const struct got_error *
3424 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3425 7e8004ba 2022-09-11 thomas {
3426 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3427 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3428 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3429 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3430 7e8004ba 2022-09-11 thomas char pattern[1024];
3431 7e8004ba 2022-09-11 thomas int ret;
3432 7e8004ba 2022-09-11 thomas
3433 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3434 7e8004ba 2022-09-11 thomas v = view->child;
3435 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3436 7e8004ba 2022-09-11 thomas v = view->parent;
3437 7e8004ba 2022-09-11 thomas
3438 7e8004ba 2022-09-11 thomas /* Get the pattern */
3439 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3440 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3441 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3442 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3443 7e8004ba 2022-09-11 thomas nocbreak();
3444 7e8004ba 2022-09-11 thomas echo();
3445 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3446 7e8004ba 2022-09-11 thomas cbreak();
3447 7e8004ba 2022-09-11 thomas noecho();
3448 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3449 7e8004ba 2022-09-11 thomas if (ret == ERR)
3450 7e8004ba 2022-09-11 thomas return NULL;
3451 7e8004ba 2022-09-11 thomas
3452 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3453 7e8004ba 2022-09-11 thomas /*
3454 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3455 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3456 7e8004ba 2022-09-11 thomas */
3457 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3458 7e8004ba 2022-09-11 thomas return NULL;
3459 7e8004ba 2022-09-11 thomas
3460 7e8004ba 2022-09-11 thomas /*
3461 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3462 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3463 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3464 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3465 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3466 7e8004ba 2022-09-11 thomas * the queue.
3467 7e8004ba 2022-09-11 thomas */
3468 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3469 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3470 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3471 7e8004ba 2022-09-11 thomas s->selected = 0;
3472 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3473 7e8004ba 2022-09-11 thomas
3474 7e8004ba 2022-09-11 thomas return NULL;
3475 7e8004ba 2022-09-11 thomas }
3476 7e8004ba 2022-09-11 thomas
3477 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3478 7e8004ba 2022-09-11 thomas return NULL;
3479 7e8004ba 2022-09-11 thomas
3480 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3481 7e8004ba 2022-09-11 thomas
3482 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3483 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3484 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3485 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3486 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3487 7e8004ba 2022-09-11 thomas
3488 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3489 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3490 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3491 7e8004ba 2022-09-11 thomas
3492 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3493 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3494 7e8004ba 2022-09-11 thomas int have_match = 0;
3495 7e8004ba 2022-09-11 thomas
3496 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3497 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3498 7e8004ba 2022-09-11 thomas if (err)
3499 7e8004ba 2022-09-11 thomas return err;
3500 7e8004ba 2022-09-11 thomas
3501 7e8004ba 2022-09-11 thomas if (have_match) {
3502 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3503 7e8004ba 2022-09-11 thomas
3504 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3505 7e8004ba 2022-09-11 thomas entry->id);
3506 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3507 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3508 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3509 7e8004ba 2022-09-11 thomas break;
3510 7e8004ba 2022-09-11 thomas }
3511 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3512 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3513 7e8004ba 2022-09-11 thomas
3514 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3515 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3516 7e8004ba 2022-09-11 thomas matched, entry);
3517 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3518 7e8004ba 2022-09-11 thomas }
3519 7e8004ba 2022-09-11 thomas }
3520 7e8004ba 2022-09-11 thomas
3521 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3522 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3523 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3524 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3525 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3526 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3527 7e8004ba 2022-09-11 thomas if (err)
3528 7e8004ba 2022-09-11 thomas return err;
3529 7e8004ba 2022-09-11 thomas }
3530 7e8004ba 2022-09-11 thomas
3531 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3532 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3533 7e8004ba 2022-09-11 thomas s->selected = 0;
3534 7e8004ba 2022-09-11 thomas
3535 7e8004ba 2022-09-11 thomas return NULL;
3536 7e8004ba 2022-09-11 thomas }
3537 7e8004ba 2022-09-11 thomas
3538 7e8004ba 2022-09-11 thomas static const struct got_error *
3539 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3540 60493ae3 2019-06-20 stsp {
3541 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3542 60493ae3 2019-06-20 stsp
3543 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3544 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3545 60493ae3 2019-06-20 stsp return NULL;
3546 60493ae3 2019-06-20 stsp }
3547 60493ae3 2019-06-20 stsp
3548 60493ae3 2019-06-20 stsp static const struct got_error *
3549 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3550 60493ae3 2019-06-20 stsp {
3551 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3552 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3553 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3554 60493ae3 2019-06-20 stsp
3555 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3556 f9686aa5 2020-03-27 stsp show_log_view(view);
3557 f9686aa5 2020-03-27 stsp update_panels();
3558 f9686aa5 2020-03-27 stsp doupdate();
3559 f9686aa5 2020-03-27 stsp
3560 96e2b566 2019-07-08 stsp if (s->search_entry) {
3561 13add988 2019-10-15 stsp int errcode, ch;
3562 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3563 13add988 2019-10-15 stsp if (errcode)
3564 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3565 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3566 13add988 2019-10-15 stsp ch = wgetch(view->window);
3567 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3568 13add988 2019-10-15 stsp if (errcode)
3569 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3570 13add988 2019-10-15 stsp "pthread_mutex_lock");
3571 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3572 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3573 678cbce5 2019-07-28 stsp return NULL;
3574 678cbce5 2019-07-28 stsp }
3575 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3576 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3577 96e2b566 2019-07-08 stsp else
3578 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3579 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3580 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3581 df5e841d 2022-06-23 thomas /*
3582 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3583 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3584 1f4d5162 2022-06-23 thomas * might have changed.
3585 df5e841d 2022-06-23 thomas */
3586 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3587 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3588 e7baaec8 2022-09-13 thomas else
3589 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3590 e7baaec8 2022-09-13 thomas entry);
3591 20be8d96 2019-06-21 stsp } else {
3592 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3593 20be8d96 2019-06-21 stsp }
3594 60493ae3 2019-06-20 stsp
3595 60493ae3 2019-06-20 stsp while (1) {
3596 13add988 2019-10-15 stsp int have_match = 0;
3597 13add988 2019-10-15 stsp
3598 60493ae3 2019-06-20 stsp if (entry == NULL) {
3599 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3600 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3601 f9967bca 2020-03-27 stsp view->search_next_done =
3602 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3603 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3604 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3605 f801134a 2019-06-25 stsp return NULL;
3606 60493ae3 2019-06-20 stsp }
3607 96e2b566 2019-07-08 stsp /*
3608 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3609 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3610 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3611 96e2b566 2019-07-08 stsp */
3612 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3613 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3614 60493ae3 2019-06-20 stsp }
3615 60493ae3 2019-06-20 stsp
3616 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3617 13add988 2019-10-15 stsp &view->regex);
3618 5943eee2 2019-08-13 stsp if (err)
3619 13add988 2019-10-15 stsp break;
3620 13add988 2019-10-15 stsp if (have_match) {
3621 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3622 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3623 60493ae3 2019-06-20 stsp break;
3624 60493ae3 2019-06-20 stsp }
3625 13add988 2019-10-15 stsp
3626 96e2b566 2019-07-08 stsp s->search_entry = entry;
3627 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3628 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3629 b1bf1435 2019-06-21 stsp else
3630 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3631 60493ae3 2019-06-20 stsp }
3632 60493ae3 2019-06-20 stsp
3633 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3634 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3635 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3636 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3637 60493ae3 2019-06-20 stsp if (err)
3638 60493ae3 2019-06-20 stsp return err;
3639 ead14cbe 2019-06-21 stsp cur++;
3640 ead14cbe 2019-06-21 stsp }
3641 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3642 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3643 60493ae3 2019-06-20 stsp if (err)
3644 60493ae3 2019-06-20 stsp return err;
3645 ead14cbe 2019-06-21 stsp cur--;
3646 60493ae3 2019-06-20 stsp }
3647 60493ae3 2019-06-20 stsp }
3648 60493ae3 2019-06-20 stsp
3649 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3650 96e2b566 2019-07-08 stsp
3651 60493ae3 2019-06-20 stsp return NULL;
3652 60493ae3 2019-06-20 stsp }
3653 60493ae3 2019-06-20 stsp
3654 60493ae3 2019-06-20 stsp static const struct got_error *
3655 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3656 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3657 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3658 80ddbec8 2018-04-29 stsp {
3659 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3660 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3661 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3662 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3663 1a76625f 2018-10-22 stsp int errcode;
3664 80ddbec8 2018-04-29 stsp
3665 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3666 f135c941 2020-02-20 stsp free(s->in_repo_path);
3667 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3668 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3669 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3670 51b7e1c3 2023-04-14 thomas goto done;
3671 51b7e1c3 2023-04-14 thomas }
3672 f135c941 2020-02-20 stsp }
3673 ecb28ae0 2018-07-16 stsp
3674 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3675 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3676 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3677 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3678 78756c87 2020-11-24 stsp
3679 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3680 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3681 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3682 7e8004ba 2022-09-11 thomas
3683 fb2756b9 2018-08-04 stsp s->repo = repo;
3684 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3685 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3686 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3687 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3688 9cd7cbd1 2020-12-07 stsp goto done;
3689 9cd7cbd1 2020-12-07 stsp }
3690 9cd7cbd1 2020-12-07 stsp }
3691 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3692 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3693 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3694 5036bf37 2018-09-24 stsp goto done;
3695 5036bf37 2018-09-24 stsp }
3696 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3697 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3698 e5a0f69f 2018-08-18 stsp
3699 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3700 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3701 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3702 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3703 11b20872 2019-11-08 stsp if (err)
3704 11b20872 2019-11-08 stsp goto done;
3705 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3706 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3707 11b20872 2019-11-08 stsp if (err) {
3708 11b20872 2019-11-08 stsp free_colors(&s->colors);
3709 11b20872 2019-11-08 stsp goto done;
3710 11b20872 2019-11-08 stsp }
3711 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3712 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3713 11b20872 2019-11-08 stsp if (err) {
3714 11b20872 2019-11-08 stsp free_colors(&s->colors);
3715 11b20872 2019-11-08 stsp goto done;
3716 11b20872 2019-11-08 stsp }
3717 11b20872 2019-11-08 stsp }
3718 11b20872 2019-11-08 stsp
3719 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3720 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3721 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3722 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3723 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3724 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3725 1a76625f 2018-10-22 stsp
3726 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3727 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3728 1af5eddf 2022-06-23 thomas if (err)
3729 1af5eddf 2022-06-23 thomas goto done;
3730 1af5eddf 2022-06-23 thomas }
3731 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3732 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3733 7cd52833 2022-06-23 thomas if (err)
3734 7cd52833 2022-06-23 thomas goto done;
3735 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3736 b672a97a 2020-01-27 stsp !s->log_branches);
3737 1a76625f 2018-10-22 stsp if (err)
3738 1a76625f 2018-10-22 stsp goto done;
3739 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3740 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3741 c5b78334 2020-01-12 stsp if (err)
3742 c5b78334 2020-01-12 stsp goto done;
3743 1a76625f 2018-10-22 stsp
3744 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3745 1a76625f 2018-10-22 stsp if (errcode) {
3746 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3747 1a76625f 2018-10-22 stsp goto done;
3748 1a76625f 2018-10-22 stsp }
3749 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3750 7c1452c1 2020-03-26 stsp if (errcode) {
3751 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3752 7c1452c1 2020-03-26 stsp goto done;
3753 7c1452c1 2020-03-26 stsp }
3754 1a76625f 2018-10-22 stsp
3755 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3756 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3757 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3758 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3759 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3760 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3761 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3762 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3763 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3764 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3765 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3766 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3767 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3768 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3769 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3770 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3771 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3772 ba4f502b 2018-08-04 stsp done:
3773 51b7e1c3 2023-04-14 thomas if (err) {
3774 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3775 51b7e1c3 2023-04-14 thomas close_log_view(view);
3776 51b7e1c3 2023-04-14 thomas view_close(view);
3777 51b7e1c3 2023-04-14 thomas }
3778 ba4f502b 2018-08-04 stsp return err;
3779 ba4f502b 2018-08-04 stsp }
3780 ba4f502b 2018-08-04 stsp
3781 e5a0f69f 2018-08-18 stsp static const struct got_error *
3782 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3783 ba4f502b 2018-08-04 stsp {
3784 f2f6d207 2020-11-24 stsp const struct got_error *err;
3785 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3786 ba4f502b 2018-08-04 stsp
3787 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3788 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3789 2b380cc8 2018-10-24 stsp &s->thread_args);
3790 2b380cc8 2018-10-24 stsp if (errcode)
3791 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3792 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3793 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3794 f2f6d207 2020-11-24 stsp if (err)
3795 f2f6d207 2020-11-24 stsp return err;
3796 f2f6d207 2020-11-24 stsp }
3797 2b380cc8 2018-10-24 stsp }
3798 2b380cc8 2018-10-24 stsp
3799 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3800 b31f89ff 2022-07-01 thomas }
3801 b31f89ff 2022-07-01 thomas
3802 b31f89ff 2022-07-01 thomas static void
3803 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3804 b31f89ff 2022-07-01 thomas {
3805 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3806 b31f89ff 2022-07-01 thomas
3807 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3808 b31f89ff 2022-07-01 thomas return;
3809 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3810 7e8004ba 2022-09-11 thomas view->count = 0;
3811 b31f89ff 2022-07-01 thomas
3812 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3813 b31f89ff 2022-07-01 thomas || home)
3814 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3815 b31f89ff 2022-07-01 thomas
3816 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3817 b31f89ff 2022-07-01 thomas --s->selected;
3818 b31f89ff 2022-07-01 thomas else
3819 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3820 b31f89ff 2022-07-01 thomas
3821 b31f89ff 2022-07-01 thomas select_commit(s);
3822 b31f89ff 2022-07-01 thomas return;
3823 b31f89ff 2022-07-01 thomas }
3824 b31f89ff 2022-07-01 thomas
3825 b31f89ff 2022-07-01 thomas static const struct got_error *
3826 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3827 b31f89ff 2022-07-01 thomas {
3828 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3829 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3830 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3831 b31f89ff 2022-07-01 thomas
3832 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3833 7e8004ba 2022-09-11 thomas return NULL;
3834 7e8004ba 2022-09-11 thomas
3835 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3836 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3837 b31f89ff 2022-07-01 thomas return NULL;
3838 b31f89ff 2022-07-01 thomas
3839 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3840 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3841 b31f89ff 2022-07-01 thomas
3842 7ed048bd 2022-08-12 thomas if (!page) {
3843 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3844 b31f89ff 2022-07-01 thomas ++s->selected;
3845 b31f89ff 2022-07-01 thomas else
3846 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3847 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3848 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3849 7ed048bd 2022-08-12 thomas int n;
3850 7ed048bd 2022-08-12 thomas
3851 7ed048bd 2022-08-12 thomas s->selected = 0;
3852 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3853 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3854 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3855 7ed048bd 2022-08-12 thomas if (entry == NULL)
3856 7ed048bd 2022-08-12 thomas break;
3857 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3858 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3859 7ed048bd 2022-08-12 thomas }
3860 7ed048bd 2022-08-12 thomas if (n > 0)
3861 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3862 b31f89ff 2022-07-01 thomas } else {
3863 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3864 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3865 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3866 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3867 bd3f8225 2022-08-12 thomas else
3868 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3869 b31f89ff 2022-07-01 thomas }
3870 b31f89ff 2022-07-01 thomas if (err)
3871 b31f89ff 2022-07-01 thomas return err;
3872 b31f89ff 2022-07-01 thomas
3873 a5d43cac 2022-07-01 thomas /*
3874 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3875 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3876 a5d43cac 2022-07-01 thomas */
3877 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3878 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3879 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3880 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3881 25100026 2022-08-13 thomas }
3882 a5d43cac 2022-07-01 thomas
3883 b31f89ff 2022-07-01 thomas select_commit(s);
3884 b31f89ff 2022-07-01 thomas
3885 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3886 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3887 b31f89ff 2022-07-01 thomas view->count = 0;
3888 b31f89ff 2022-07-01 thomas
3889 b31f89ff 2022-07-01 thomas return NULL;
3890 e5a0f69f 2018-08-18 stsp }
3891 04cc582a 2018-08-01 stsp
3892 a5d43cac 2022-07-01 thomas static void
3893 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3894 a5d43cac 2022-07-01 thomas {
3895 24415785 2022-07-03 thomas *x = 0;
3896 24415785 2022-07-03 thomas *y = 0;
3897 24415785 2022-07-03 thomas
3898 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3899 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3900 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3901 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3902 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3903 53d2bdd3 2022-07-10 thomas else
3904 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3905 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3906 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3907 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3908 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3909 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3910 53d2bdd3 2022-07-10 thomas else
3911 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3912 53d2bdd3 2022-07-10 thomas }
3913 a5d43cac 2022-07-01 thomas }
3914 a5d43cac 2022-07-01 thomas
3915 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3916 e5a0f69f 2018-08-18 stsp static const struct got_error *
3917 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3918 a5d43cac 2022-07-01 thomas {
3919 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3920 a5d43cac 2022-07-01 thomas
3921 a5d43cac 2022-07-01 thomas view->nlines = y;
3922 64486692 2022-07-07 thomas view->ncols = COLS;
3923 a5d43cac 2022-07-01 thomas err = view_resize(view);
3924 a5d43cac 2022-07-01 thomas if (err)
3925 a5d43cac 2022-07-01 thomas return err;
3926 a5d43cac 2022-07-01 thomas
3927 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3928 a5d43cac 2022-07-01 thomas
3929 a5d43cac 2022-07-01 thomas return err;
3930 07dd3ed3 2022-08-06 thomas }
3931 07dd3ed3 2022-08-06 thomas
3932 07dd3ed3 2022-08-06 thomas static const struct got_error *
3933 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3934 07dd3ed3 2022-08-06 thomas {
3935 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3936 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3937 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3938 25100026 2022-08-13 thomas
3939 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3940 25100026 2022-08-13 thomas return NULL;
3941 07dd3ed3 2022-08-06 thomas
3942 07dd3ed3 2022-08-06 thomas g = view->gline;
3943 07dd3ed3 2022-08-06 thomas view->gline = 0;
3944 07dd3ed3 2022-08-06 thomas
3945 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3946 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3947 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3948 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3949 07dd3ed3 2022-08-06 thomas select_commit(s);
3950 07dd3ed3 2022-08-06 thomas return NULL;
3951 07dd3ed3 2022-08-06 thomas }
3952 07dd3ed3 2022-08-06 thomas
3953 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3954 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3955 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3956 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3957 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3958 07dd3ed3 2022-08-06 thomas if (err)
3959 07dd3ed3 2022-08-06 thomas return err;
3960 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3961 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3962 07dd3ed3 2022-08-06 thomas
3963 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3964 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3965 07dd3ed3 2022-08-06 thomas
3966 07dd3ed3 2022-08-06 thomas select_commit(s);
3967 07dd3ed3 2022-08-06 thomas return NULL;
3968 c72de8ab 2023-02-03 thomas
3969 c72de8ab 2023-02-03 thomas }
3970 c72de8ab 2023-02-03 thomas
3971 c72de8ab 2023-02-03 thomas static void
3972 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
3973 c72de8ab 2023-02-03 thomas {
3974 07dd3ed3 2022-08-06 thomas
3975 c72de8ab 2023-02-03 thomas switch (ch) {
3976 c72de8ab 2023-02-03 thomas case KEY_LEFT:
3977 c72de8ab 2023-02-03 thomas case 'h':
3978 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
3979 c72de8ab 2023-02-03 thomas if (view->x <= 0)
3980 c72de8ab 2023-02-03 thomas view->count = 0;
3981 c72de8ab 2023-02-03 thomas break;
3982 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
3983 c72de8ab 2023-02-03 thomas case 'l':
3984 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
3985 c72de8ab 2023-02-03 thomas view->x += 2;
3986 c72de8ab 2023-02-03 thomas else
3987 c72de8ab 2023-02-03 thomas view->count = 0;
3988 c72de8ab 2023-02-03 thomas break;
3989 c72de8ab 2023-02-03 thomas case '0':
3990 c72de8ab 2023-02-03 thomas view->x = 0;
3991 c72de8ab 2023-02-03 thomas break;
3992 c72de8ab 2023-02-03 thomas case '$':
3993 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3994 c72de8ab 2023-02-03 thomas view->count = 0;
3995 c72de8ab 2023-02-03 thomas break;
3996 c72de8ab 2023-02-03 thomas default:
3997 c72de8ab 2023-02-03 thomas break;
3998 c72de8ab 2023-02-03 thomas }
3999 a5d43cac 2022-07-01 thomas }
4000 a5d43cac 2022-07-01 thomas
4001 a5d43cac 2022-07-01 thomas static const struct got_error *
4002 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4003 e5a0f69f 2018-08-18 stsp {
4004 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4005 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4006 7ed048bd 2022-08-12 thomas int eos, nscroll;
4007 80ddbec8 2018-04-29 stsp
4008 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4009 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4010 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4011 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4012 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4013 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4014 fb280deb 2021-08-30 stsp }
4015 c0be8933 2022-08-12 thomas if (err)
4016 c0be8933 2022-08-12 thomas return err;
4017 528dedf3 2021-08-30 stsp }
4018 068ab281 2022-07-01 thomas
4019 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4020 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4021 068ab281 2022-07-01 thomas --eos; /* border */
4022 07dd3ed3 2022-08-06 thomas
4023 07dd3ed3 2022-08-06 thomas if (view->gline)
4024 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4025 068ab281 2022-07-01 thomas
4026 528dedf3 2021-08-30 stsp switch (ch) {
4027 7e8004ba 2022-09-11 thomas case '&':
4028 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4029 7e8004ba 2022-09-11 thomas break;
4030 1e37a5c2 2019-05-12 jcs case 'q':
4031 1e37a5c2 2019-05-12 jcs s->quit = 1;
4032 05171be4 2022-06-23 thomas break;
4033 05171be4 2022-06-23 thomas case '0':
4034 05171be4 2022-06-23 thomas case '$':
4035 05171be4 2022-06-23 thomas case KEY_RIGHT:
4036 05171be4 2022-06-23 thomas case 'l':
4037 05171be4 2022-06-23 thomas case KEY_LEFT:
4038 05171be4 2022-06-23 thomas case 'h':
4039 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4040 05171be4 2022-06-23 thomas break;
4041 1e37a5c2 2019-05-12 jcs case 'k':
4042 1e37a5c2 2019-05-12 jcs case KEY_UP:
4043 1e37a5c2 2019-05-12 jcs case '<':
4044 1e37a5c2 2019-05-12 jcs case ',':
4045 f7140bf5 2021-10-17 thomas case CTRL('p'):
4046 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4047 912a3f79 2021-08-30 j break;
4048 912a3f79 2021-08-30 j case 'g':
4049 aa7a1117 2023-01-09 thomas case '=':
4050 912a3f79 2021-08-30 j case KEY_HOME:
4051 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4052 07b0611c 2022-06-23 thomas view->count = 0;
4053 1e37a5c2 2019-05-12 jcs break;
4054 70f17a53 2022-06-13 thomas case CTRL('u'):
4055 23427b14 2022-06-23 thomas case 'u':
4056 70f17a53 2022-06-13 thomas nscroll /= 2;
4057 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4058 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4059 a4292ac5 2019-05-12 jcs case CTRL('b'):
4060 1c5e5faa 2022-06-23 thomas case 'b':
4061 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4062 1e37a5c2 2019-05-12 jcs break;
4063 1e37a5c2 2019-05-12 jcs case 'j':
4064 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4065 1e37a5c2 2019-05-12 jcs case '>':
4066 1e37a5c2 2019-05-12 jcs case '.':
4067 f7140bf5 2021-10-17 thomas case CTRL('n'):
4068 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4069 912a3f79 2021-08-30 j break;
4070 f69c5a46 2022-07-19 thomas case '@':
4071 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4072 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4073 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4074 f69c5a46 2022-07-19 thomas break;
4075 912a3f79 2021-08-30 j case 'G':
4076 aa7a1117 2023-01-09 thomas case '*':
4077 912a3f79 2021-08-30 j case KEY_END: {
4078 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4079 912a3f79 2021-08-30 j * traverse them all. */
4080 07b0611c 2022-06-23 thomas view->count = 0;
4081 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4082 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4083 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4084 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4085 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4086 1e37a5c2 2019-05-12 jcs break;
4087 912a3f79 2021-08-30 j }
4088 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4089 23427b14 2022-06-23 thomas case 'd':
4090 70f17a53 2022-06-13 thomas nscroll /= 2;
4091 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4092 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4093 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4094 4c2d69cb 2022-06-23 thomas case 'f':
4095 b31f89ff 2022-07-01 thomas case ' ':
4096 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4097 1e37a5c2 2019-05-12 jcs break;
4098 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4099 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4100 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4101 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4102 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4103 2b779855 2020-12-05 naddy select_commit(s);
4104 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4105 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4106 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4107 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4108 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4109 0bf7f153 2020-12-02 naddy }
4110 1e37a5c2 2019-05-12 jcs break;
4111 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4112 444d5325 2022-07-03 thomas case '\r':
4113 07b0611c 2022-06-23 thomas view->count = 0;
4114 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4115 e5a0f69f 2018-08-18 stsp break;
4116 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4117 1e37a5c2 2019-05-12 jcs break;
4118 1be4947a 2022-07-22 thomas case 'T':
4119 07b0611c 2022-06-23 thomas view->count = 0;
4120 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4121 5036bf37 2018-09-24 stsp break;
4122 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4123 1e37a5c2 2019-05-12 jcs break;
4124 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4125 21355643 2020-12-06 stsp case CTRL('l'):
4126 21355643 2020-12-06 stsp case 'B':
4127 07b0611c 2022-06-23 thomas view->count = 0;
4128 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4129 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4130 1e37a5c2 2019-05-12 jcs break;
4131 21355643 2020-12-06 stsp err = stop_log_thread(s);
4132 74cfe85e 2020-10-20 stsp if (err)
4133 74cfe85e 2020-10-20 stsp return err;
4134 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4135 21355643 2020-12-06 stsp char *parent_path;
4136 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4137 21355643 2020-12-06 stsp if (err)
4138 21355643 2020-12-06 stsp return err;
4139 21355643 2020-12-06 stsp free(s->in_repo_path);
4140 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4141 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4142 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4143 21355643 2020-12-06 stsp struct got_object_id *start_id;
4144 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4145 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4146 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4147 466429a1 2022-11-17 thomas if (err) {
4148 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4149 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4150 466429a1 2022-11-17 thomas return err;
4151 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4152 466429a1 2022-11-17 thomas free(s->head_ref_name);
4153 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4154 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4155 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4156 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4157 466429a1 2022-11-17 thomas if (err)
4158 466429a1 2022-11-17 thomas return err;
4159 466429a1 2022-11-17 thomas }
4160 21355643 2020-12-06 stsp free(s->start_id);
4161 21355643 2020-12-06 stsp s->start_id = start_id;
4162 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4163 21355643 2020-12-06 stsp } else /* 'B' */
4164 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4165 21355643 2020-12-06 stsp
4166 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4167 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4168 bf7e79b3 2022-06-23 thomas if (err)
4169 bf7e79b3 2022-06-23 thomas return err;
4170 bf7e79b3 2022-06-23 thomas }
4171 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4172 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4173 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4174 74cfe85e 2020-10-20 stsp if (err)
4175 21355643 2020-12-06 stsp return err;
4176 51a10b52 2020-12-26 stsp tog_free_refs();
4177 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4178 ca51c541 2020-12-07 stsp if (err)
4179 ca51c541 2020-12-07 stsp return err;
4180 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4181 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4182 d01904d4 2019-06-25 stsp if (err)
4183 d01904d4 2019-06-25 stsp return err;
4184 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4185 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4186 b672a97a 2020-01-27 stsp if (err)
4187 b672a97a 2020-01-27 stsp return err;
4188 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4189 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4190 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4191 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4192 21355643 2020-12-06 stsp s->selected_entry = NULL;
4193 21355643 2020-12-06 stsp s->selected = 0;
4194 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4195 21355643 2020-12-06 stsp s->quit = 0;
4196 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4197 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4198 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4199 94ecf40d 2022-07-22 thomas view->offset = 0;
4200 d01904d4 2019-06-25 stsp break;
4201 1be4947a 2022-07-22 thomas case 'R':
4202 07b0611c 2022-06-23 thomas view->count = 0;
4203 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4204 6458efa5 2020-11-24 stsp break;
4205 1e37a5c2 2019-05-12 jcs default:
4206 07b0611c 2022-06-23 thomas view->count = 0;
4207 1e37a5c2 2019-05-12 jcs break;
4208 899d86c2 2018-05-10 stsp }
4209 e5a0f69f 2018-08-18 stsp
4210 80ddbec8 2018-04-29 stsp return err;
4211 80ddbec8 2018-04-29 stsp }
4212 80ddbec8 2018-04-29 stsp
4213 4ed7e80c 2018-05-20 stsp static const struct got_error *
4214 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4215 c2db6724 2019-01-04 stsp {
4216 c2db6724 2019-01-04 stsp const struct got_error *error;
4217 c2db6724 2019-01-04 stsp
4218 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4219 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4220 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4221 37c06ea4 2019-07-15 stsp #endif
4222 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4223 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4224 c2db6724 2019-01-04 stsp
4225 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4226 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4227 c2db6724 2019-01-04 stsp
4228 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4229 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4230 c2db6724 2019-01-04 stsp
4231 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4232 c2db6724 2019-01-04 stsp if (error != NULL)
4233 c2db6724 2019-01-04 stsp return error;
4234 c2db6724 2019-01-04 stsp
4235 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4236 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4237 c2db6724 2019-01-04 stsp
4238 c2db6724 2019-01-04 stsp return NULL;
4239 c2db6724 2019-01-04 stsp }
4240 c2db6724 2019-01-04 stsp
4241 b85a3496 2023-04-14 thomas static const struct got_error *
4242 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4243 b85a3496 2023-04-14 thomas {
4244 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4245 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4246 f4086c71 2023-04-22 thomas int in;
4247 b85a3496 2023-04-14 thomas
4248 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4249 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4250 b85a3496 2023-04-14 thomas
4251 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4252 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4253 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4254 b85a3496 2023-04-14 thomas test_script_path);
4255 b85a3496 2023-04-14 thomas goto done;
4256 b85a3496 2023-04-14 thomas }
4257 b85a3496 2023-04-14 thomas
4258 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4259 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4260 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4261 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4262 b85a3496 2023-04-14 thomas goto done;
4263 b85a3496 2023-04-14 thomas }
4264 b85a3496 2023-04-14 thomas
4265 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4266 f4086c71 2023-04-22 thomas if (in == -1) {
4267 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4268 f4086c71 2023-04-22 thomas goto done;
4269 f4086c71 2023-04-22 thomas }
4270 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4271 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4272 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4273 f4086c71 2023-04-22 thomas close(in);
4274 b85a3496 2023-04-14 thomas goto done;
4275 b85a3496 2023-04-14 thomas }
4276 b85a3496 2023-04-14 thomas
4277 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4278 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4279 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4280 5b3a801d 2023-04-22 thomas tog_io.sdump = fopen(screen_dump_path, "wex");
4281 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4282 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4283 5b3a801d 2023-04-22 thomas goto done;
4284 5b3a801d 2023-04-22 thomas }
4285 5b3a801d 2023-04-22 thomas
4286 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4287 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4288 b85a3496 2023-04-14 thomas goto done;
4289 b85a3496 2023-04-14 thomas }
4290 b85a3496 2023-04-14 thomas
4291 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4292 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4293 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4294 557d3365 2023-04-14 thomas
4295 557d3365 2023-04-14 thomas using_mock_io = 1;
4296 1134ebde 2023-04-22 thomas
4297 b85a3496 2023-04-14 thomas done:
4298 b85a3496 2023-04-14 thomas if (err)
4299 557d3365 2023-04-14 thomas tog_io_close();
4300 b85a3496 2023-04-14 thomas return err;
4301 b85a3496 2023-04-14 thomas }
4302 b85a3496 2023-04-14 thomas
4303 557d3365 2023-04-14 thomas static void
4304 557d3365 2023-04-14 thomas init_curses(void)
4305 a915003a 2019-02-05 stsp {
4306 296152d1 2022-05-31 thomas /*
4307 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
4308 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
4309 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
4310 296152d1 2022-05-31 thomas */
4311 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
4312 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
4313 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
4314 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
4315 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
4316 296152d1 2022-05-31 thomas
4317 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4318 557d3365 2023-04-14 thomas return;
4319 b85a3496 2023-04-14 thomas
4320 557d3365 2023-04-14 thomas initscr();
4321 557d3365 2023-04-14 thomas
4322 a915003a 2019-02-05 stsp cbreak();
4323 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4324 a915003a 2019-02-05 stsp noecho();
4325 a915003a 2019-02-05 stsp nonl();
4326 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4327 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4328 a915003a 2019-02-05 stsp curs_set(0);
4329 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4330 6d17833f 2019-11-08 stsp start_color();
4331 6d17833f 2019-11-08 stsp use_default_colors();
4332 6d17833f 2019-11-08 stsp }
4333 b85a3496 2023-04-14 thomas
4334 557d3365 2023-04-14 thomas return;
4335 a915003a 2019-02-05 stsp }
4336 a915003a 2019-02-05 stsp
4337 c2db6724 2019-01-04 stsp static const struct got_error *
4338 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4339 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4340 f135c941 2020-02-20 stsp {
4341 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4342 f135c941 2020-02-20 stsp
4343 f135c941 2020-02-20 stsp if (argc == 0) {
4344 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4345 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4346 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4347 f135c941 2020-02-20 stsp return NULL;
4348 f135c941 2020-02-20 stsp }
4349 f135c941 2020-02-20 stsp
4350 f135c941 2020-02-20 stsp if (worktree) {
4351 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4352 bfd61697 2020-11-14 stsp char *p;
4353 f135c941 2020-02-20 stsp
4354 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4355 f135c941 2020-02-20 stsp if (err)
4356 f135c941 2020-02-20 stsp return err;
4357 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4358 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4359 bfd61697 2020-11-14 stsp p) == -1) {
4360 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4361 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4362 f135c941 2020-02-20 stsp }
4363 f135c941 2020-02-20 stsp free(p);
4364 f135c941 2020-02-20 stsp } else
4365 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4366 f135c941 2020-02-20 stsp
4367 f135c941 2020-02-20 stsp return err;
4368 f135c941 2020-02-20 stsp }
4369 f135c941 2020-02-20 stsp
4370 f135c941 2020-02-20 stsp static const struct got_error *
4371 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4372 9f7d7167 2018-04-29 stsp {
4373 1d98034b 2023-04-14 thomas const struct got_error *error;
4374 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4375 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4376 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4377 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4378 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4379 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4380 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4381 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4382 04cc582a 2018-08-01 stsp struct tog_view *view;
4383 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4384 80ddbec8 2018-04-29 stsp
4385 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4386 80ddbec8 2018-04-29 stsp switch (ch) {
4387 b672a97a 2020-01-27 stsp case 'b':
4388 b672a97a 2020-01-27 stsp log_branches = 1;
4389 b672a97a 2020-01-27 stsp break;
4390 80ddbec8 2018-04-29 stsp case 'c':
4391 80ddbec8 2018-04-29 stsp start_commit = optarg;
4392 80ddbec8 2018-04-29 stsp break;
4393 ecb28ae0 2018-07-16 stsp case 'r':
4394 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4395 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4396 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4397 9ba1d308 2019-10-21 stsp optarg);
4398 ecb28ae0 2018-07-16 stsp break;
4399 80ddbec8 2018-04-29 stsp default:
4400 17020d27 2019-03-07 stsp usage_log();
4401 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4402 80ddbec8 2018-04-29 stsp }
4403 80ddbec8 2018-04-29 stsp }
4404 80ddbec8 2018-04-29 stsp
4405 80ddbec8 2018-04-29 stsp argc -= optind;
4406 80ddbec8 2018-04-29 stsp argv += optind;
4407 80ddbec8 2018-04-29 stsp
4408 f135c941 2020-02-20 stsp if (argc > 1)
4409 f135c941 2020-02-20 stsp usage_log();
4410 963f97a1 2019-03-18 stsp
4411 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4412 7cd52833 2022-06-23 thomas if (error != NULL)
4413 7cd52833 2022-06-23 thomas goto done;
4414 7cd52833 2022-06-23 thomas
4415 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4416 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4417 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4418 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4419 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4420 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4421 c156c7a4 2020-12-18 stsp goto done;
4422 a1fbf39a 2019-08-11 stsp if (worktree)
4423 6962eb72 2020-02-20 stsp repo_path =
4424 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4425 a1fbf39a 2019-08-11 stsp else
4426 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4427 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4428 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4429 c156c7a4 2020-12-18 stsp goto done;
4430 c156c7a4 2020-12-18 stsp }
4431 ecb28ae0 2018-07-16 stsp }
4432 ecb28ae0 2018-07-16 stsp
4433 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4434 80ddbec8 2018-04-29 stsp if (error != NULL)
4435 ecb28ae0 2018-07-16 stsp goto done;
4436 80ddbec8 2018-04-29 stsp
4437 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4438 f135c941 2020-02-20 stsp repo, worktree);
4439 f135c941 2020-02-20 stsp if (error)
4440 f135c941 2020-02-20 stsp goto done;
4441 f135c941 2020-02-20 stsp
4442 557d3365 2023-04-14 thomas init_curses();
4443 f135c941 2020-02-20 stsp
4444 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4445 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4446 c02c541e 2019-03-29 stsp if (error)
4447 c02c541e 2019-03-29 stsp goto done;
4448 c02c541e 2019-03-29 stsp
4449 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4450 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4451 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4452 87670572 2020-12-26 naddy if (error)
4453 87670572 2020-12-26 naddy goto done;
4454 87670572 2020-12-26 naddy }
4455 51a10b52 2020-12-26 stsp
4456 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4457 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4458 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4459 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4460 d8f38dc4 2020-12-05 stsp if (error)
4461 d8f38dc4 2020-12-05 stsp goto done;
4462 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4463 d8f38dc4 2020-12-05 stsp } else {
4464 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4465 d8f38dc4 2020-12-05 stsp if (error == NULL)
4466 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4467 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4468 d8f38dc4 2020-12-05 stsp goto done;
4469 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4470 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4471 d8f38dc4 2020-12-05 stsp if (error)
4472 d8f38dc4 2020-12-05 stsp goto done;
4473 d8f38dc4 2020-12-05 stsp }
4474 8b473291 2019-02-21 stsp
4475 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4476 04cc582a 2018-08-01 stsp if (view == NULL) {
4477 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4478 04cc582a 2018-08-01 stsp goto done;
4479 04cc582a 2018-08-01 stsp }
4480 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4481 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4482 ba4f502b 2018-08-04 stsp if (error)
4483 ba4f502b 2018-08-04 stsp goto done;
4484 2fc00ff4 2019-08-31 stsp if (worktree) {
4485 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4486 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4487 2fc00ff4 2019-08-31 stsp worktree = NULL;
4488 2fc00ff4 2019-08-31 stsp }
4489 557d3365 2023-04-14 thomas error = view_loop(view);
4490 ecb28ae0 2018-07-16 stsp done:
4491 f135c941 2020-02-20 stsp free(in_repo_path);
4492 ecb28ae0 2018-07-16 stsp free(repo_path);
4493 ecb28ae0 2018-07-16 stsp free(cwd);
4494 899d86c2 2018-05-10 stsp free(start_id);
4495 d8f38dc4 2020-12-05 stsp free(label);
4496 d8f38dc4 2020-12-05 stsp if (ref)
4497 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4498 1d0f4054 2021-06-17 stsp if (repo) {
4499 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4500 1d0f4054 2021-06-17 stsp if (error == NULL)
4501 1d0f4054 2021-06-17 stsp error = close_err;
4502 1d0f4054 2021-06-17 stsp }
4503 ec142235 2019-03-07 stsp if (worktree)
4504 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4505 7cd52833 2022-06-23 thomas if (pack_fds) {
4506 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4507 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4508 7cd52833 2022-06-23 thomas if (error == NULL)
4509 7cd52833 2022-06-23 thomas error = pack_err;
4510 b85a3496 2023-04-14 thomas }
4511 51a10b52 2020-12-26 stsp tog_free_refs();
4512 80ddbec8 2018-04-29 stsp return error;
4513 9f7d7167 2018-04-29 stsp }
4514 9f7d7167 2018-04-29 stsp
4515 4ed7e80c 2018-05-20 stsp __dead static void
4516 9f7d7167 2018-04-29 stsp usage_diff(void)
4517 9f7d7167 2018-04-29 stsp {
4518 80ddbec8 2018-04-29 stsp endwin();
4519 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4520 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4521 9f7d7167 2018-04-29 stsp exit(1);
4522 b304db33 2018-05-20 stsp }
4523 b304db33 2018-05-20 stsp
4524 6d17833f 2019-11-08 stsp static int
4525 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4526 41605754 2020-11-12 stsp regmatch_t *regmatch)
4527 6d17833f 2019-11-08 stsp {
4528 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4529 6d17833f 2019-11-08 stsp }
4530 6d17833f 2019-11-08 stsp
4531 ef20f542 2022-06-26 thomas static struct tog_color *
4532 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4533 6d17833f 2019-11-08 stsp {
4534 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4535 6d17833f 2019-11-08 stsp
4536 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4537 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4538 6d17833f 2019-11-08 stsp return tc;
4539 6d17833f 2019-11-08 stsp }
4540 6d17833f 2019-11-08 stsp
4541 6d17833f 2019-11-08 stsp return NULL;
4542 6d17833f 2019-11-08 stsp }
4543 6d17833f 2019-11-08 stsp
4544 4ed7e80c 2018-05-20 stsp static const struct got_error *
4545 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4546 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4547 41605754 2020-11-12 stsp {
4548 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4549 666f7b10 2022-06-23 thomas char *exstr = NULL;
4550 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4551 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4552 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4553 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4554 41605754 2020-11-12 stsp
4555 41605754 2020-11-12 stsp *wtotal = 0;
4556 cbea3800 2022-06-23 thomas
4557 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4558 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4559 41605754 2020-11-12 stsp
4560 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4561 666f7b10 2022-06-23 thomas if (err)
4562 666f7b10 2022-06-23 thomas return err;
4563 666f7b10 2022-06-23 thomas
4564 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4565 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4566 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4567 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4568 666f7b10 2022-06-23 thomas goto done;
4569 666f7b10 2022-06-23 thomas }
4570 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4571 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4572 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4573 cbea3800 2022-06-23 thomas goto done;
4574 cbea3800 2022-06-23 thomas }
4575 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4576 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4577 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4578 cbea3800 2022-06-23 thomas goto done;
4579 cbea3800 2022-06-23 thomas }
4580 05171be4 2022-06-23 thomas
4581 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4582 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4583 cbea3800 2022-06-23 thomas col_tab_align, 1);
4584 cbea3800 2022-06-23 thomas if (err)
4585 cbea3800 2022-06-23 thomas goto done;
4586 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4587 05171be4 2022-06-23 thomas if (n) {
4588 cbea3800 2022-06-23 thomas free(wline);
4589 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4590 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4591 cbea3800 2022-06-23 thomas if (err)
4592 cbea3800 2022-06-23 thomas goto done;
4593 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4594 cbea3800 2022-06-23 thomas wlimit -= width;
4595 cbea3800 2022-06-23 thomas *wtotal += width;
4596 41605754 2020-11-12 stsp }
4597 41605754 2020-11-12 stsp
4598 41605754 2020-11-12 stsp if (wlimit > 0) {
4599 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4600 cbea3800 2022-06-23 thomas size_t wlen;
4601 cbea3800 2022-06-23 thomas
4602 cbea3800 2022-06-23 thomas free(wline);
4603 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4604 cbea3800 2022-06-23 thomas col_tab_align, 1);
4605 cbea3800 2022-06-23 thomas if (err)
4606 cbea3800 2022-06-23 thomas goto done;
4607 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4608 cbea3800 2022-06-23 thomas while (i < wlen) {
4609 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4610 cbea3800 2022-06-23 thomas if (width == -1) {
4611 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4612 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4613 cbea3800 2022-06-23 thomas goto done;
4614 cbea3800 2022-06-23 thomas }
4615 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4616 cbea3800 2022-06-23 thomas break;
4617 1c5e5faa 2022-06-23 thomas w += width;
4618 cbea3800 2022-06-23 thomas i++;
4619 41605754 2020-11-12 stsp }
4620 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4621 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4622 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4623 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4624 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4625 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4626 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4627 41605754 2020-11-12 stsp }
4628 41605754 2020-11-12 stsp }
4629 41605754 2020-11-12 stsp
4630 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4631 cbea3800 2022-06-23 thomas free(wline);
4632 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4633 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4634 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4635 cbea3800 2022-06-23 thomas col_tab_align, 1);
4636 cbea3800 2022-06-23 thomas if (err)
4637 cbea3800 2022-06-23 thomas goto done;
4638 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4639 cbea3800 2022-06-23 thomas } else {
4640 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4641 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4642 cbea3800 2022-06-23 thomas if (err)
4643 cbea3800 2022-06-23 thomas goto done;
4644 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4645 cbea3800 2022-06-23 thomas }
4646 cbea3800 2022-06-23 thomas *wtotal += width2;
4647 41605754 2020-11-12 stsp }
4648 cbea3800 2022-06-23 thomas done:
4649 05171be4 2022-06-23 thomas free(wline);
4650 666f7b10 2022-06-23 thomas free(exstr);
4651 cbea3800 2022-06-23 thomas free(seg0);
4652 cbea3800 2022-06-23 thomas free(seg1);
4653 cbea3800 2022-06-23 thomas free(seg2);
4654 cbea3800 2022-06-23 thomas return err;
4655 41605754 2020-11-12 stsp }
4656 07dd3ed3 2022-08-06 thomas
4657 07dd3ed3 2022-08-06 thomas static int
4658 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4659 07dd3ed3 2022-08-06 thomas {
4660 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4661 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4662 07dd3ed3 2022-08-06 thomas
4663 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4664 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4665 fc2737d5 2022-09-15 thomas
4666 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4667 fc2737d5 2022-09-15 thomas selected = first;
4668 fc2737d5 2022-09-15 thomas eof = &s->eof;
4669 fc2737d5 2022-09-15 thomas f = s->f;
4670 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4671 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4672 41605754 2020-11-12 stsp
4673 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4674 07dd3ed3 2022-08-06 thomas selected = first;
4675 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4676 07dd3ed3 2022-08-06 thomas f = s->f;
4677 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4678 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4679 07dd3ed3 2022-08-06 thomas
4680 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4681 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4682 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4683 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4684 07dd3ed3 2022-08-06 thomas } else
4685 07dd3ed3 2022-08-06 thomas return 0;
4686 07dd3ed3 2022-08-06 thomas
4687 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4688 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4689 07dd3ed3 2022-08-06 thomas return 0;
4690 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4691 07dd3ed3 2022-08-06 thomas rewind(f);
4692 07dd3ed3 2022-08-06 thomas *eof = 0;
4693 07dd3ed3 2022-08-06 thomas *first = 1;
4694 07dd3ed3 2022-08-06 thomas *lineno = 0;
4695 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4696 07dd3ed3 2022-08-06 thomas return 0;
4697 07dd3ed3 2022-08-06 thomas }
4698 07dd3ed3 2022-08-06 thomas
4699 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4700 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4701 07dd3ed3 2022-08-06 thomas view->gline = 0;
4702 07dd3ed3 2022-08-06 thomas
4703 07dd3ed3 2022-08-06 thomas return 1;
4704 07dd3ed3 2022-08-06 thomas }
4705 07dd3ed3 2022-08-06 thomas
4706 41605754 2020-11-12 stsp static const struct got_error *
4707 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4708 26ed57b2 2018-05-19 stsp {
4709 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4710 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4711 61e69b96 2018-05-20 stsp const struct got_error *err;
4712 82c78e96 2022-08-06 thomas int nprinted = 0;
4713 b304db33 2018-05-20 stsp char *line;
4714 826082fe 2020-12-10 stsp size_t linesize = 0;
4715 826082fe 2020-12-10 stsp ssize_t linelen;
4716 61e69b96 2018-05-20 stsp wchar_t *wline;
4717 e0b650dd 2018-05-20 stsp int width;
4718 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4719 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4720 fe621944 2020-11-10 stsp off_t line_offset;
4721 26ed57b2 2018-05-19 stsp
4722 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4723 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4724 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4725 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4726 fe621944 2020-11-10 stsp
4727 f7d12f7e 2018-08-01 stsp werase(view->window);
4728 a3404814 2018-09-02 stsp
4729 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4730 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4731 07dd3ed3 2022-08-06 thomas
4732 a3404814 2018-09-02 stsp if (header) {
4733 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4734 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4735 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4736 07dd3ed3 2022-08-06 thomas
4737 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4738 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4739 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4740 f91a2b48 2022-06-23 thomas 0, 0);
4741 135a2da0 2020-11-11 stsp free(line);
4742 135a2da0 2020-11-11 stsp if (err)
4743 a3404814 2018-09-02 stsp return err;
4744 a3404814 2018-09-02 stsp
4745 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4746 a3404814 2018-09-02 stsp wstandout(view->window);
4747 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4748 e54cc94a 2020-11-11 stsp free(wline);
4749 e54cc94a 2020-11-11 stsp wline = NULL;
4750 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4751 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4752 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4753 a3404814 2018-09-02 stsp wstandend(view->window);
4754 26ed57b2 2018-05-19 stsp
4755 a3404814 2018-09-02 stsp if (max_lines <= 1)
4756 a3404814 2018-09-02 stsp return NULL;
4757 a3404814 2018-09-02 stsp max_lines--;
4758 a3404814 2018-09-02 stsp }
4759 a3404814 2018-09-02 stsp
4760 89f1a395 2020-12-01 naddy s->eof = 0;
4761 05171be4 2022-06-23 thomas view->maxx = 0;
4762 826082fe 2020-12-10 stsp line = NULL;
4763 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4764 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4765 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4766 6568f0aa 2022-08-06 thomas
4767 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4768 826082fe 2020-12-10 stsp if (linelen == -1) {
4769 826082fe 2020-12-10 stsp if (feof(s->f)) {
4770 826082fe 2020-12-10 stsp s->eof = 1;
4771 826082fe 2020-12-10 stsp break;
4772 826082fe 2020-12-10 stsp }
4773 826082fe 2020-12-10 stsp free(line);
4774 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4775 61e69b96 2018-05-20 stsp }
4776 05171be4 2022-06-23 thomas
4777 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4778 07dd3ed3 2022-08-06 thomas continue;
4779 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4780 07dd3ed3 2022-08-06 thomas continue;
4781 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4782 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4783 07dd3ed3 2022-08-06 thomas
4784 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4785 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4786 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4787 cbea3800 2022-06-23 thomas if (err) {
4788 cbea3800 2022-06-23 thomas free(line);
4789 cbea3800 2022-06-23 thomas return err;
4790 cbea3800 2022-06-23 thomas }
4791 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4792 cbea3800 2022-06-23 thomas free(wline);
4793 cbea3800 2022-06-23 thomas wline = NULL;
4794 cbea3800 2022-06-23 thomas
4795 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4796 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4797 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4798 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4799 07dd3ed3 2022-08-06 thomas if (attr)
4800 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4801 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4802 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4803 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4804 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4805 41605754 2020-11-12 stsp if (err) {
4806 41605754 2020-11-12 stsp free(line);
4807 41605754 2020-11-12 stsp return err;
4808 41605754 2020-11-12 stsp }
4809 41605754 2020-11-12 stsp } else {
4810 f1c0ec19 2022-06-23 thomas int skip;
4811 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4812 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4813 f1c0ec19 2022-06-23 thomas if (err) {
4814 f1c0ec19 2022-06-23 thomas free(line);
4815 f1c0ec19 2022-06-23 thomas return err;
4816 f1c0ec19 2022-06-23 thomas }
4817 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4818 f1c0ec19 2022-06-23 thomas free(wline);
4819 41605754 2020-11-12 stsp wline = NULL;
4820 41605754 2020-11-12 stsp }
4821 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4822 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4823 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4824 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4825 07dd3ed3 2022-08-06 thomas } else {
4826 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4827 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4828 07dd3ed3 2022-08-06 thomas }
4829 07dd3ed3 2022-08-06 thomas if (attr)
4830 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4831 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4832 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4833 826082fe 2020-12-10 stsp }
4834 826082fe 2020-12-10 stsp free(line);
4835 fe621944 2020-11-10 stsp if (nprinted >= 1)
4836 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4837 89f1a395 2020-12-01 naddy (nprinted - 1);
4838 fe621944 2020-11-10 stsp else
4839 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4840 26ed57b2 2018-05-19 stsp
4841 a5d43cac 2022-07-01 thomas view_border(view);
4842 c3e9aa98 2019-05-13 jcs
4843 89f1a395 2020-12-01 naddy if (s->eof) {
4844 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4845 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4846 c3e9aa98 2019-05-13 jcs nprinted++;
4847 c3e9aa98 2019-05-13 jcs }
4848 c3e9aa98 2019-05-13 jcs
4849 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4850 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4851 c3e9aa98 2019-05-13 jcs if (err) {
4852 c3e9aa98 2019-05-13 jcs return err;
4853 c3e9aa98 2019-05-13 jcs }
4854 26ed57b2 2018-05-19 stsp
4855 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4856 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4857 e54cc94a 2020-11-11 stsp free(wline);
4858 e54cc94a 2020-11-11 stsp wline = NULL;
4859 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4860 c3e9aa98 2019-05-13 jcs }
4861 c3e9aa98 2019-05-13 jcs
4862 26ed57b2 2018-05-19 stsp return NULL;
4863 abd2672a 2018-12-23 stsp }
4864 abd2672a 2018-12-23 stsp
4865 abd2672a 2018-12-23 stsp static char *
4866 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4867 abd2672a 2018-12-23 stsp {
4868 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4869 09867e48 2019-08-13 stsp char *p, *s;
4870 09867e48 2019-08-13 stsp
4871 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4872 09867e48 2019-08-13 stsp if (tm == NULL)
4873 09867e48 2019-08-13 stsp return NULL;
4874 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4875 09867e48 2019-08-13 stsp if (s == NULL)
4876 09867e48 2019-08-13 stsp return NULL;
4877 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4878 abd2672a 2018-12-23 stsp if (p)
4879 abd2672a 2018-12-23 stsp *p = '\0';
4880 abd2672a 2018-12-23 stsp return s;
4881 9f7d7167 2018-04-29 stsp }
4882 9f7d7167 2018-04-29 stsp
4883 4ed7e80c 2018-05-20 stsp static const struct got_error *
4884 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4885 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4886 abd2672a 2018-12-23 stsp {
4887 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4888 fe621944 2020-11-10 stsp
4889 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4890 fe621944 2020-11-10 stsp if (p == NULL)
4891 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4892 82c78e96 2022-08-06 thomas *lines = p;
4893 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4894 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4895 fe621944 2020-11-10 stsp (*nlines)++;
4896 82c78e96 2022-08-06 thomas
4897 fe621944 2020-11-10 stsp return NULL;
4898 fe621944 2020-11-10 stsp }
4899 fe621944 2020-11-10 stsp
4900 fe621944 2020-11-10 stsp static const struct got_error *
4901 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4902 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4903 be97ab03 2023-01-19 thomas {
4904 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4905 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4906 be97ab03 2023-01-19 thomas size_t i, r;
4907 be97ab03 2023-01-19 thomas
4908 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4909 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4910 be97ab03 2023-01-19 thomas
4911 be97ab03 2023-01-19 thomas for (;;) {
4912 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4913 be97ab03 2023-01-19 thomas if (r == 0) {
4914 be97ab03 2023-01-19 thomas if (ferror(src))
4915 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4916 be97ab03 2023-01-19 thomas if (feof(src))
4917 be97ab03 2023-01-19 thomas break;
4918 be97ab03 2023-01-19 thomas }
4919 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4920 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4921 be97ab03 2023-01-19 thomas }
4922 be97ab03 2023-01-19 thomas
4923 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4924 9382c10a 2023-02-23 thomas return NULL;
4925 9382c10a 2023-02-23 thomas
4926 be97ab03 2023-01-19 thomas /*
4927 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4928 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4929 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4930 be97ab03 2023-01-19 thomas */
4931 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4932 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4933 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4934 be97ab03 2023-01-19 thomas
4935 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4936 9382c10a 2023-02-23 thomas --s_nlines;
4937 9382c10a 2023-02-23 thomas ++s_lines;
4938 9382c10a 2023-02-23 thomas }
4939 9382c10a 2023-02-23 thomas }
4940 be97ab03 2023-01-19 thomas
4941 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4942 be97ab03 2023-01-19 thomas if (p == NULL) {
4943 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4944 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4945 be97ab03 2023-01-19 thomas }
4946 be97ab03 2023-01-19 thomas
4947 be97ab03 2023-01-19 thomas *d_lines = p;
4948 be97ab03 2023-01-19 thomas
4949 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4950 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4951 be97ab03 2023-01-19 thomas
4952 be97ab03 2023-01-19 thomas return NULL;
4953 be97ab03 2023-01-19 thomas }
4954 be97ab03 2023-01-19 thomas
4955 be97ab03 2023-01-19 thomas static const struct got_error *
4956 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4957 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4958 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4959 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4960 fe621944 2020-11-10 stsp {
4961 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4962 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4963 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4964 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4965 45d799e2 2018-12-23 stsp time_t committer_time;
4966 45d799e2 2018-12-23 stsp const char *author, *committer;
4967 8b473291 2019-02-21 stsp char *refs_str = NULL;
4968 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4969 fe621944 2020-11-10 stsp off_t outoff = 0;
4970 fe621944 2020-11-10 stsp int n;
4971 abd2672a 2018-12-23 stsp
4972 8b473291 2019-02-21 stsp if (refs) {
4973 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4974 8b473291 2019-02-21 stsp if (err)
4975 8b473291 2019-02-21 stsp return err;
4976 8b473291 2019-02-21 stsp }
4977 8b473291 2019-02-21 stsp
4978 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4979 abd2672a 2018-12-23 stsp if (err)
4980 abd2672a 2018-12-23 stsp return err;
4981 abd2672a 2018-12-23 stsp
4982 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4983 15a94983 2018-12-23 stsp if (err) {
4984 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4985 15a94983 2018-12-23 stsp goto done;
4986 15a94983 2018-12-23 stsp }
4987 abd2672a 2018-12-23 stsp
4988 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4989 fe621944 2020-11-10 stsp if (err)
4990 fe621944 2020-11-10 stsp goto done;
4991 fe621944 2020-11-10 stsp
4992 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4993 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4994 fe621944 2020-11-10 stsp if (n < 0) {
4995 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4996 abd2672a 2018-12-23 stsp goto done;
4997 abd2672a 2018-12-23 stsp }
4998 fe621944 2020-11-10 stsp outoff += n;
4999 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5000 fe621944 2020-11-10 stsp if (err)
5001 fe621944 2020-11-10 stsp goto done;
5002 fe621944 2020-11-10 stsp
5003 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5004 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5005 fe621944 2020-11-10 stsp if (n < 0) {
5006 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5007 abd2672a 2018-12-23 stsp goto done;
5008 abd2672a 2018-12-23 stsp }
5009 fe621944 2020-11-10 stsp outoff += n;
5010 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5011 fe621944 2020-11-10 stsp if (err)
5012 fe621944 2020-11-10 stsp goto done;
5013 fe621944 2020-11-10 stsp
5014 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5015 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5016 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5017 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5018 fe621944 2020-11-10 stsp if (n < 0) {
5019 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5020 fe621944 2020-11-10 stsp goto done;
5021 fe621944 2020-11-10 stsp }
5022 fe621944 2020-11-10 stsp outoff += n;
5023 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5024 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5025 fe621944 2020-11-10 stsp if (err)
5026 fe621944 2020-11-10 stsp goto done;
5027 abd2672a 2018-12-23 stsp }
5028 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5029 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5030 48830929 2023-01-07 thomas if (datestr) {
5031 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5032 fe621944 2020-11-10 stsp if (n < 0) {
5033 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5034 fe621944 2020-11-10 stsp goto done;
5035 fe621944 2020-11-10 stsp }
5036 fe621944 2020-11-10 stsp outoff += n;
5037 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5038 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5039 fe621944 2020-11-10 stsp if (err)
5040 fe621944 2020-11-10 stsp goto done;
5041 abd2672a 2018-12-23 stsp }
5042 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5043 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5044 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5045 ac4dc263 2021-09-24 thomas int pn = 1;
5046 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5047 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5048 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5049 ac4dc263 2021-09-24 thomas if (err)
5050 ac4dc263 2021-09-24 thomas goto done;
5051 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5052 ac4dc263 2021-09-24 thomas if (n < 0) {
5053 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5054 ac4dc263 2021-09-24 thomas goto done;
5055 ac4dc263 2021-09-24 thomas }
5056 ac4dc263 2021-09-24 thomas outoff += n;
5057 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5058 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5059 ac4dc263 2021-09-24 thomas if (err)
5060 ac4dc263 2021-09-24 thomas goto done;
5061 ac4dc263 2021-09-24 thomas free(id_str);
5062 ac4dc263 2021-09-24 thomas id_str = NULL;
5063 ac4dc263 2021-09-24 thomas }
5064 ac4dc263 2021-09-24 thomas }
5065 ac4dc263 2021-09-24 thomas
5066 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5067 5943eee2 2019-08-13 stsp if (err)
5068 5943eee2 2019-08-13 stsp goto done;
5069 fe621944 2020-11-10 stsp s = logmsg;
5070 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5071 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5072 fe621944 2020-11-10 stsp if (n < 0) {
5073 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5074 fe621944 2020-11-10 stsp goto done;
5075 fe621944 2020-11-10 stsp }
5076 fe621944 2020-11-10 stsp outoff += n;
5077 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5078 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5079 fe621944 2020-11-10 stsp if (err)
5080 fe621944 2020-11-10 stsp goto done;
5081 abd2672a 2018-12-23 stsp }
5082 fe621944 2020-11-10 stsp
5083 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5084 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5085 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5086 772fcad5 2023-01-07 thomas
5087 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5088 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5089 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5090 fe621944 2020-11-10 stsp if (n < 0) {
5091 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5092 fe621944 2020-11-10 stsp goto done;
5093 fe621944 2020-11-10 stsp }
5094 fe621944 2020-11-10 stsp outoff += n;
5095 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5096 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5097 fe621944 2020-11-10 stsp if (err)
5098 fe621944 2020-11-10 stsp goto done;
5099 0208f208 2020-05-05 stsp }
5100 fe621944 2020-11-10 stsp
5101 0208f208 2020-05-05 stsp fputc('\n', outfile);
5102 fe621944 2020-11-10 stsp outoff++;
5103 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5104 772fcad5 2023-01-07 thomas if (err)
5105 772fcad5 2023-01-07 thomas goto done;
5106 772fcad5 2023-01-07 thomas
5107 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5108 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5109 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5110 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5111 772fcad5 2023-01-07 thomas if (n < 0) {
5112 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5113 772fcad5 2023-01-07 thomas goto done;
5114 772fcad5 2023-01-07 thomas }
5115 772fcad5 2023-01-07 thomas outoff += n;
5116 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5117 772fcad5 2023-01-07 thomas if (err)
5118 772fcad5 2023-01-07 thomas goto done;
5119 772fcad5 2023-01-07 thomas
5120 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5121 772fcad5 2023-01-07 thomas outoff++;
5122 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5123 abd2672a 2018-12-23 stsp done:
5124 abd2672a 2018-12-23 stsp free(id_str);
5125 5943eee2 2019-08-13 stsp free(logmsg);
5126 8b473291 2019-02-21 stsp free(refs_str);
5127 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5128 7510f233 2020-08-09 stsp if (err) {
5129 82c78e96 2022-08-06 thomas free(*lines);
5130 82c78e96 2022-08-06 thomas *lines = NULL;
5131 ae6a6978 2020-08-09 stsp *nlines = 0;
5132 7510f233 2020-08-09 stsp }
5133 fe621944 2020-11-10 stsp return err;
5134 abd2672a 2018-12-23 stsp }
5135 abd2672a 2018-12-23 stsp
5136 abd2672a 2018-12-23 stsp static const struct got_error *
5137 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5138 26ed57b2 2018-05-19 stsp {
5139 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5140 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5141 15a94983 2018-12-23 stsp int obj_type;
5142 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5143 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5144 fe621944 2020-11-10 stsp
5145 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5146 be97ab03 2023-01-19 thomas
5147 82c78e96 2022-08-06 thomas free(s->lines);
5148 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5149 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5150 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5151 fe621944 2020-11-10 stsp s->nlines = 0;
5152 26ed57b2 2018-05-19 stsp
5153 511a516b 2018-05-19 stsp f = got_opentemp();
5154 48ae06ee 2018-10-18 stsp if (f == NULL) {
5155 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5156 48ae06ee 2018-10-18 stsp goto done;
5157 48ae06ee 2018-10-18 stsp }
5158 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5159 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5160 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5161 be97ab03 2023-01-19 thomas goto done;
5162 be97ab03 2023-01-19 thomas }
5163 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5164 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5165 fb43ecf1 2019-02-11 stsp goto done;
5166 fb43ecf1 2019-02-11 stsp }
5167 48ae06ee 2018-10-18 stsp s->f = f;
5168 26ed57b2 2018-05-19 stsp
5169 15a94983 2018-12-23 stsp if (s->id1)
5170 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5171 15a94983 2018-12-23 stsp else
5172 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5173 15a94983 2018-12-23 stsp if (err)
5174 15a94983 2018-12-23 stsp goto done;
5175 15a94983 2018-12-23 stsp
5176 15a94983 2018-12-23 stsp switch (obj_type) {
5177 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5178 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5179 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5180 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5181 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5182 53d03f97 2023-01-10 thomas s->f);
5183 26ed57b2 2018-05-19 stsp break;
5184 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5185 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5186 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5187 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5188 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5189 26ed57b2 2018-05-19 stsp break;
5190 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5191 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5192 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5193 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5194 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5195 be97ab03 2023-01-19 thomas size_t nlines = 0;
5196 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5197 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5198 be97ab03 2023-01-19 thomas &changed_paths,
5199 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5200 be97ab03 2023-01-19 thomas s->force_text_diff,
5201 be97ab03 2023-01-19 thomas tog_diff_algo
5202 be97ab03 2023-01-19 thomas };
5203 abd2672a 2018-12-23 stsp
5204 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5205 be97ab03 2023-01-19 thomas if (lines == NULL) {
5206 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5207 be97ab03 2023-01-19 thomas goto done;
5208 be97ab03 2023-01-19 thomas }
5209 be97ab03 2023-01-19 thomas
5210 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5211 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5212 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5213 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5214 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5215 be97ab03 2023-01-19 thomas if (err)
5216 be97ab03 2023-01-19 thomas break;
5217 be97ab03 2023-01-19 thomas
5218 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5219 abd2672a 2018-12-23 stsp if (err)
5220 3ffacbe1 2020-02-02 tracey goto done;
5221 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5222 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5223 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5224 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5225 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5226 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5227 f44b1f58 2020-02-02 tracey if (err)
5228 f44b1f58 2020-02-02 tracey goto done;
5229 f44b1f58 2020-02-02 tracey } else {
5230 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5231 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5232 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5233 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5234 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5235 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5236 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5237 f44b1f58 2020-02-02 tracey if (err)
5238 f44b1f58 2020-02-02 tracey goto done;
5239 f5404e4e 2020-02-02 tracey break;
5240 15a087fe 2019-02-21 stsp }
5241 abd2672a 2018-12-23 stsp }
5242 abd2672a 2018-12-23 stsp }
5243 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5244 abd2672a 2018-12-23 stsp
5245 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5246 be97ab03 2023-01-19 thomas lines, nlines);
5247 26ed57b2 2018-05-19 stsp break;
5248 abd2672a 2018-12-23 stsp }
5249 26ed57b2 2018-05-19 stsp default:
5250 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5251 48ae06ee 2018-10-18 stsp break;
5252 26ed57b2 2018-05-19 stsp }
5253 48ae06ee 2018-10-18 stsp done:
5254 be97ab03 2023-01-19 thomas free(lines);
5255 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5256 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5257 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5258 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5259 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5260 48ae06ee 2018-10-18 stsp return err;
5261 48ae06ee 2018-10-18 stsp }
5262 26ed57b2 2018-05-19 stsp
5263 f5215bb9 2019-02-22 stsp static void
5264 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5265 f5215bb9 2019-02-22 stsp {
5266 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5267 f5215bb9 2019-02-22 stsp update_panels();
5268 f5215bb9 2019-02-22 stsp doupdate();
5269 f44b1f58 2020-02-02 tracey }
5270 f44b1f58 2020-02-02 tracey
5271 f44b1f58 2020-02-02 tracey static const struct got_error *
5272 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5273 f44b1f58 2020-02-02 tracey {
5274 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5275 f44b1f58 2020-02-02 tracey
5276 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5277 f44b1f58 2020-02-02 tracey return NULL;
5278 f44b1f58 2020-02-02 tracey }
5279 f44b1f58 2020-02-02 tracey
5280 2a7d3cd7 2022-09-17 thomas static void
5281 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5282 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5283 f44b1f58 2020-02-02 tracey {
5284 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5285 fc2737d5 2022-09-15 thomas
5286 2a7d3cd7 2022-09-17 thomas *f = s->f;
5287 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5288 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5289 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5290 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5291 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5292 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5293 fc2737d5 2022-09-15 thomas }
5294 fc2737d5 2022-09-15 thomas
5295 fc2737d5 2022-09-15 thomas static const struct got_error *
5296 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5297 fc2737d5 2022-09-15 thomas {
5298 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5299 fc2737d5 2022-09-15 thomas FILE *f;
5300 f44b1f58 2020-02-02 tracey int lineno;
5301 78eecffc 2022-06-23 thomas char *line = NULL;
5302 826082fe 2020-12-10 stsp size_t linesize = 0;
5303 826082fe 2020-12-10 stsp ssize_t linelen;
5304 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5305 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5306 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5307 f44b1f58 2020-02-02 tracey
5308 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5309 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5310 2a7d3cd7 2022-09-17 thomas "view search not supported");
5311 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5312 fc2737d5 2022-09-15 thomas &match, &selected);
5313 fc2737d5 2022-09-15 thomas
5314 f44b1f58 2020-02-02 tracey if (!view->searching) {
5315 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5316 f44b1f58 2020-02-02 tracey return NULL;
5317 f44b1f58 2020-02-02 tracey }
5318 f44b1f58 2020-02-02 tracey
5319 fc2737d5 2022-09-15 thomas if (*match) {
5320 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5321 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5322 f44b1f58 2020-02-02 tracey else
5323 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5324 4b3f9dac 2021-12-17 thomas } else
5325 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5326 f44b1f58 2020-02-02 tracey
5327 f44b1f58 2020-02-02 tracey while (1) {
5328 f44b1f58 2020-02-02 tracey off_t offset;
5329 f44b1f58 2020-02-02 tracey
5330 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5331 fc2737d5 2022-09-15 thomas if (*match == 0) {
5332 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5333 f44b1f58 2020-02-02 tracey break;
5334 f44b1f58 2020-02-02 tracey }
5335 f44b1f58 2020-02-02 tracey
5336 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5337 f44b1f58 2020-02-02 tracey lineno = 1;
5338 f44b1f58 2020-02-02 tracey else
5339 fc2737d5 2022-09-15 thomas lineno = nlines;
5340 f44b1f58 2020-02-02 tracey }
5341 f44b1f58 2020-02-02 tracey
5342 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5343 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5344 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5345 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5346 f44b1f58 2020-02-02 tracey free(line);
5347 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5348 f44b1f58 2020-02-02 tracey }
5349 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5350 78eecffc 2022-06-23 thomas if (linelen != -1) {
5351 78eecffc 2022-06-23 thomas char *exstr;
5352 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5353 78eecffc 2022-06-23 thomas if (err)
5354 78eecffc 2022-06-23 thomas break;
5355 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5356 78eecffc 2022-06-23 thomas &view->regmatch)) {
5357 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5358 fc2737d5 2022-09-15 thomas *match = lineno;
5359 78eecffc 2022-06-23 thomas free(exstr);
5360 78eecffc 2022-06-23 thomas break;
5361 78eecffc 2022-06-23 thomas }
5362 78eecffc 2022-06-23 thomas free(exstr);
5363 f44b1f58 2020-02-02 tracey }
5364 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5365 f44b1f58 2020-02-02 tracey lineno++;
5366 f44b1f58 2020-02-02 tracey else
5367 f44b1f58 2020-02-02 tracey lineno--;
5368 f44b1f58 2020-02-02 tracey }
5369 826082fe 2020-12-10 stsp free(line);
5370 f44b1f58 2020-02-02 tracey
5371 fc2737d5 2022-09-15 thomas if (*match) {
5372 fc2737d5 2022-09-15 thomas *first = *match;
5373 fc2737d5 2022-09-15 thomas *selected = 1;
5374 f44b1f58 2020-02-02 tracey }
5375 f44b1f58 2020-02-02 tracey
5376 05171be4 2022-06-23 thomas return err;
5377 f5215bb9 2019-02-22 stsp }
5378 f5215bb9 2019-02-22 stsp
5379 48ae06ee 2018-10-18 stsp static const struct got_error *
5380 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5381 a0f32f33 2022-06-13 thomas {
5382 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5383 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5384 a0f32f33 2022-06-13 thomas
5385 a0f32f33 2022-06-13 thomas free(s->id1);
5386 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5387 a0f32f33 2022-06-13 thomas free(s->id2);
5388 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5389 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5390 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5391 a0f32f33 2022-06-13 thomas s->f = NULL;
5392 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5393 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5394 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5395 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5396 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5397 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5398 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5399 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5400 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5401 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5402 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5403 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5404 82c78e96 2022-08-06 thomas free(s->lines);
5405 82c78e96 2022-08-06 thomas s->lines = NULL;
5406 a0f32f33 2022-06-13 thomas s->nlines = 0;
5407 a0f32f33 2022-06-13 thomas return err;
5408 a0f32f33 2022-06-13 thomas }
5409 a0f32f33 2022-06-13 thomas
5410 a0f32f33 2022-06-13 thomas static const struct got_error *
5411 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5412 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5413 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5414 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5415 48ae06ee 2018-10-18 stsp {
5416 48ae06ee 2018-10-18 stsp const struct got_error *err;
5417 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5418 5dc9f4bc 2018-08-04 stsp
5419 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5420 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5421 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5422 a0f32f33 2022-06-13 thomas
5423 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5424 51b7e1c3 2023-04-14 thomas int type1, type2;
5425 15a94983 2018-12-23 stsp
5426 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5427 51b7e1c3 2023-04-14 thomas if (err)
5428 51b7e1c3 2023-04-14 thomas goto done;
5429 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5430 51b7e1c3 2023-04-14 thomas if (err)
5431 51b7e1c3 2023-04-14 thomas goto done;
5432 51b7e1c3 2023-04-14 thomas
5433 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5434 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5435 51b7e1c3 2023-04-14 thomas goto done;
5436 51b7e1c3 2023-04-14 thomas }
5437 15a94983 2018-12-23 stsp }
5438 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5439 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5440 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5441 f44b1f58 2020-02-02 tracey s->repo = repo;
5442 f44b1f58 2020-02-02 tracey s->id1 = id1;
5443 f44b1f58 2020-02-02 tracey s->id2 = id2;
5444 3dbaef42 2020-11-24 stsp s->label1 = label1;
5445 3dbaef42 2020-11-24 stsp s->label2 = label2;
5446 48ae06ee 2018-10-18 stsp
5447 15a94983 2018-12-23 stsp if (id1) {
5448 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5449 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5450 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5451 51b7e1c3 2023-04-14 thomas goto done;
5452 51b7e1c3 2023-04-14 thomas }
5453 48ae06ee 2018-10-18 stsp } else
5454 5465d566 2020-02-01 tracey s->id1 = NULL;
5455 48ae06ee 2018-10-18 stsp
5456 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5457 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5458 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5459 a0f32f33 2022-06-13 thomas goto done;
5460 48ae06ee 2018-10-18 stsp }
5461 a0f32f33 2022-06-13 thomas
5462 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5463 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5464 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5465 9a1d7435 2022-06-23 thomas goto done;
5466 9a1d7435 2022-06-23 thomas }
5467 9a1d7435 2022-06-23 thomas
5468 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5469 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5470 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5471 a0f32f33 2022-06-13 thomas goto done;
5472 a0f32f33 2022-06-13 thomas }
5473 a0f32f33 2022-06-13 thomas
5474 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5475 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5476 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5477 19a6a6b5 2022-07-01 thomas goto done;
5478 19a6a6b5 2022-07-01 thomas }
5479 19a6a6b5 2022-07-01 thomas
5480 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5481 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5482 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5483 19a6a6b5 2022-07-01 thomas goto done;
5484 19a6a6b5 2022-07-01 thomas }
5485 19a6a6b5 2022-07-01 thomas
5486 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5487 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5488 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5489 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5490 5465d566 2020-02-01 tracey s->repo = repo;
5491 6d17833f 2019-11-08 stsp
5492 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5493 6568f0aa 2022-08-06 thomas int rc;
5494 6d17833f 2019-11-08 stsp
5495 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5496 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5497 6568f0aa 2022-08-06 thomas if (rc != ERR)
5498 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5499 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5500 6568f0aa 2022-08-06 thomas if (rc != ERR)
5501 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5502 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5503 6568f0aa 2022-08-06 thomas if (rc != ERR)
5504 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5505 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5506 6568f0aa 2022-08-06 thomas if (rc != ERR)
5507 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5508 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5509 6568f0aa 2022-08-06 thomas if (rc != ERR)
5510 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5511 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5512 6568f0aa 2022-08-06 thomas if (rc != ERR)
5513 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5514 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5515 6568f0aa 2022-08-06 thomas if (rc != ERR)
5516 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5517 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5518 6568f0aa 2022-08-06 thomas if (rc != ERR)
5519 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5520 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5521 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5522 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5523 a0f32f33 2022-06-13 thomas goto done;
5524 6568f0aa 2022-08-06 thomas }
5525 6d17833f 2019-11-08 stsp }
5526 5dc9f4bc 2018-08-04 stsp
5527 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5528 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5529 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5530 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5531 f5215bb9 2019-02-22 stsp
5532 5465d566 2020-02-01 tracey err = create_diff(s);
5533 48ae06ee 2018-10-18 stsp
5534 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5535 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5536 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5537 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5538 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5539 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5540 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5541 a0f32f33 2022-06-13 thomas done:
5542 51b7e1c3 2023-04-14 thomas if (err) {
5543 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5544 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5545 51b7e1c3 2023-04-14 thomas view_close(view);
5546 51b7e1c3 2023-04-14 thomas }
5547 e5a0f69f 2018-08-18 stsp return err;
5548 5dc9f4bc 2018-08-04 stsp }
5549 5dc9f4bc 2018-08-04 stsp
5550 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5551 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5552 5dc9f4bc 2018-08-04 stsp {
5553 a3404814 2018-09-02 stsp const struct got_error *err;
5554 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5555 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5556 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5557 a3404814 2018-09-02 stsp
5558 a3404814 2018-09-02 stsp if (s->id1) {
5559 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5560 a3404814 2018-09-02 stsp if (err)
5561 a3404814 2018-09-02 stsp return err;
5562 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5563 3dbaef42 2020-11-24 stsp } else
5564 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5565 3dbaef42 2020-11-24 stsp
5566 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5567 a3404814 2018-09-02 stsp if (err)
5568 a3404814 2018-09-02 stsp return err;
5569 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5570 26ed57b2 2018-05-19 stsp
5571 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5572 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5573 a3404814 2018-09-02 stsp free(id_str1);
5574 a3404814 2018-09-02 stsp free(id_str2);
5575 a3404814 2018-09-02 stsp return err;
5576 a3404814 2018-09-02 stsp }
5577 a3404814 2018-09-02 stsp free(id_str1);
5578 a3404814 2018-09-02 stsp free(id_str2);
5579 a3404814 2018-09-02 stsp
5580 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5581 267bb3b8 2021-08-01 stsp free(header);
5582 267bb3b8 2021-08-01 stsp return err;
5583 15a087fe 2019-02-21 stsp }
5584 15a087fe 2019-02-21 stsp
5585 15a087fe 2019-02-21 stsp static const struct got_error *
5586 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5587 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5588 15a087fe 2019-02-21 stsp {
5589 d7a04538 2019-02-21 stsp const struct got_error *err;
5590 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5591 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5592 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5593 15a087fe 2019-02-21 stsp
5594 15a087fe 2019-02-21 stsp free(s->id2);
5595 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5596 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5597 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5598 15a087fe 2019-02-21 stsp
5599 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5600 d7a04538 2019-02-21 stsp if (err)
5601 d7a04538 2019-02-21 stsp return err;
5602 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5603 15a087fe 2019-02-21 stsp free(s->id1);
5604 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5605 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5606 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5607 15a087fe 2019-02-21 stsp return NULL;
5608 0cf4efb1 2018-09-29 stsp }
5609 0cf4efb1 2018-09-29 stsp
5610 0cf4efb1 2018-09-29 stsp static const struct got_error *
5611 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5612 adf4c9e0 2022-07-03 thomas {
5613 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5614 adf4c9e0 2022-07-03 thomas
5615 adf4c9e0 2022-07-03 thomas view->count = 0;
5616 adf4c9e0 2022-07-03 thomas wclear(view->window);
5617 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5618 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5619 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5620 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5621 adf4c9e0 2022-07-03 thomas return create_diff(s);
5622 82c78e96 2022-08-06 thomas }
5623 82c78e96 2022-08-06 thomas
5624 82c78e96 2022-08-06 thomas static void
5625 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5626 82c78e96 2022-08-06 thomas {
5627 82c78e96 2022-08-06 thomas int start, i;
5628 82c78e96 2022-08-06 thomas
5629 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5630 82c78e96 2022-08-06 thomas
5631 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5632 82c78e96 2022-08-06 thomas if (i == 0)
5633 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5634 82c78e96 2022-08-06 thomas if (--i == start)
5635 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5636 82c78e96 2022-08-06 thomas }
5637 82c78e96 2022-08-06 thomas
5638 82c78e96 2022-08-06 thomas s->selected_line = 1;
5639 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5640 adf4c9e0 2022-07-03 thomas }
5641 adf4c9e0 2022-07-03 thomas
5642 82c78e96 2022-08-06 thomas static void
5643 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5644 82c78e96 2022-08-06 thomas {
5645 82c78e96 2022-08-06 thomas int start, i;
5646 82c78e96 2022-08-06 thomas
5647 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5648 82c78e96 2022-08-06 thomas
5649 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5650 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5651 82c78e96 2022-08-06 thomas i = 0;
5652 82c78e96 2022-08-06 thomas if (++i == start)
5653 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5654 82c78e96 2022-08-06 thomas }
5655 82c78e96 2022-08-06 thomas
5656 82c78e96 2022-08-06 thomas s->selected_line = 1;
5657 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5658 82c78e96 2022-08-06 thomas }
5659 82c78e96 2022-08-06 thomas
5660 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5661 4fc71f3b 2022-07-12 thomas int, int, int);
5662 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5663 4fc71f3b 2022-07-12 thomas int, int);
5664 4fc71f3b 2022-07-12 thomas
5665 adf4c9e0 2022-07-03 thomas static const struct got_error *
5666 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5667 e5a0f69f 2018-08-18 stsp {
5668 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5669 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5670 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5671 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5672 826082fe 2020-12-10 stsp char *line = NULL;
5673 826082fe 2020-12-10 stsp size_t linesize = 0;
5674 826082fe 2020-12-10 stsp ssize_t linelen;
5675 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5676 e5a0f69f 2018-08-18 stsp
5677 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5678 82c78e96 2022-08-06 thomas
5679 e5a0f69f 2018-08-18 stsp switch (ch) {
5680 05171be4 2022-06-23 thomas case '0':
5681 05171be4 2022-06-23 thomas case '$':
5682 05171be4 2022-06-23 thomas case KEY_RIGHT:
5683 05171be4 2022-06-23 thomas case 'l':
5684 05171be4 2022-06-23 thomas case KEY_LEFT:
5685 05171be4 2022-06-23 thomas case 'h':
5686 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5687 05171be4 2022-06-23 thomas break;
5688 64453f7e 2020-11-21 stsp case 'a':
5689 3dbaef42 2020-11-24 stsp case 'w':
5690 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5691 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5692 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5693 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5694 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5695 f2d06bef 2023-01-23 thomas }
5696 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5697 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5698 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5699 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5700 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5701 f2d06bef 2023-01-23 thomas }
5702 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5703 912a3f79 2021-08-30 j break;
5704 912a3f79 2021-08-30 j case 'g':
5705 912a3f79 2021-08-30 j case KEY_HOME:
5706 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5707 07b0611c 2022-06-23 thomas view->count = 0;
5708 64453f7e 2020-11-21 stsp break;
5709 912a3f79 2021-08-30 j case 'G':
5710 912a3f79 2021-08-30 j case KEY_END:
5711 07b0611c 2022-06-23 thomas view->count = 0;
5712 912a3f79 2021-08-30 j if (s->eof)
5713 912a3f79 2021-08-30 j break;
5714 912a3f79 2021-08-30 j
5715 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5716 912a3f79 2021-08-30 j s->eof = 1;
5717 912a3f79 2021-08-30 j break;
5718 1e37a5c2 2019-05-12 jcs case 'k':
5719 1e37a5c2 2019-05-12 jcs case KEY_UP:
5720 f7140bf5 2021-10-17 thomas case CTRL('p'):
5721 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5722 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5723 07b0611c 2022-06-23 thomas else
5724 07b0611c 2022-06-23 thomas view->count = 0;
5725 1e37a5c2 2019-05-12 jcs break;
5726 70f17a53 2022-06-13 thomas case CTRL('u'):
5727 23427b14 2022-06-23 thomas case 'u':
5728 70f17a53 2022-06-13 thomas nscroll /= 2;
5729 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5730 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5731 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5732 1c5e5faa 2022-06-23 thomas case 'b':
5733 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5734 07b0611c 2022-06-23 thomas view->count = 0;
5735 26ed57b2 2018-05-19 stsp break;
5736 07b0611c 2022-06-23 thomas }
5737 1e37a5c2 2019-05-12 jcs i = 0;
5738 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5739 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5740 1e37a5c2 2019-05-12 jcs break;
5741 1e37a5c2 2019-05-12 jcs case 'j':
5742 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5743 f7140bf5 2021-10-17 thomas case CTRL('n'):
5744 1e37a5c2 2019-05-12 jcs if (!s->eof)
5745 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5746 07b0611c 2022-06-23 thomas else
5747 07b0611c 2022-06-23 thomas view->count = 0;
5748 1e37a5c2 2019-05-12 jcs break;
5749 70f17a53 2022-06-13 thomas case CTRL('d'):
5750 23427b14 2022-06-23 thomas case 'd':
5751 70f17a53 2022-06-13 thomas nscroll /= 2;
5752 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5753 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5754 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5755 1c5e5faa 2022-06-23 thomas case 'f':
5756 1e37a5c2 2019-05-12 jcs case ' ':
5757 07b0611c 2022-06-23 thomas if (s->eof) {
5758 07b0611c 2022-06-23 thomas view->count = 0;
5759 1e37a5c2 2019-05-12 jcs break;
5760 07b0611c 2022-06-23 thomas }
5761 1e37a5c2 2019-05-12 jcs i = 0;
5762 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5763 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5764 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5765 826082fe 2020-12-10 stsp if (linelen == -1) {
5766 826082fe 2020-12-10 stsp if (feof(s->f)) {
5767 826082fe 2020-12-10 stsp s->eof = 1;
5768 826082fe 2020-12-10 stsp } else
5769 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5770 34bc9ec9 2019-02-22 stsp break;
5771 826082fe 2020-12-10 stsp }
5772 1e37a5c2 2019-05-12 jcs }
5773 826082fe 2020-12-10 stsp free(line);
5774 1e37a5c2 2019-05-12 jcs break;
5775 82c78e96 2022-08-06 thomas case '(':
5776 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5777 82c78e96 2022-08-06 thomas break;
5778 82c78e96 2022-08-06 thomas case ')':
5779 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5780 82c78e96 2022-08-06 thomas break;
5781 82c78e96 2022-08-06 thomas case '{':
5782 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5783 82c78e96 2022-08-06 thomas break;
5784 82c78e96 2022-08-06 thomas case '}':
5785 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5786 82c78e96 2022-08-06 thomas break;
5787 1e37a5c2 2019-05-12 jcs case '[':
5788 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5789 1e37a5c2 2019-05-12 jcs s->diff_context--;
5790 aa61903a 2021-12-31 thomas s->matched_line = 0;
5791 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5792 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5793 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5794 27829c9e 2020-11-21 stsp s->nlines) {
5795 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5796 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5797 27829c9e 2020-11-21 stsp }
5798 07b0611c 2022-06-23 thomas } else
5799 07b0611c 2022-06-23 thomas view->count = 0;
5800 1e37a5c2 2019-05-12 jcs break;
5801 1e37a5c2 2019-05-12 jcs case ']':
5802 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5803 1e37a5c2 2019-05-12 jcs s->diff_context++;
5804 aa61903a 2021-12-31 thomas s->matched_line = 0;
5805 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5806 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5807 07b0611c 2022-06-23 thomas } else
5808 07b0611c 2022-06-23 thomas view->count = 0;
5809 1e37a5c2 2019-05-12 jcs break;
5810 1e37a5c2 2019-05-12 jcs case '<':
5811 1e37a5c2 2019-05-12 jcs case ',':
5812 777aae21 2022-07-20 thomas case 'K':
5813 4fc71f3b 2022-07-12 thomas up = 1;
5814 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5815 4fc71f3b 2022-07-12 thomas case '>':
5816 4fc71f3b 2022-07-12 thomas case '.':
5817 777aae21 2022-07-20 thomas case 'J':
5818 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5819 07b0611c 2022-06-23 thomas view->count = 0;
5820 48ae06ee 2018-10-18 stsp break;
5821 07b0611c 2022-06-23 thomas }
5822 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5823 6524637e 2019-02-21 stsp
5824 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5825 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5826 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5827 15a087fe 2019-02-21 stsp
5828 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5829 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5830 4fc71f3b 2022-07-12 thomas if (err)
5831 4fc71f3b 2022-07-12 thomas break;
5832 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5833 15a087fe 2019-02-21 stsp
5834 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5835 4fc71f3b 2022-07-12 thomas break;
5836 15a087fe 2019-02-21 stsp
5837 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5838 4fc71f3b 2022-07-12 thomas if (err)
5839 4fc71f3b 2022-07-12 thomas break;
5840 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5841 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5842 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5843 5e224a3e 2019-02-22 stsp
5844 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5845 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5846 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5847 4fc71f3b 2022-07-12 thomas
5848 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5849 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5850 4fc71f3b 2022-07-12 thomas if (err)
5851 4fc71f3b 2022-07-12 thomas break;
5852 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5853 5a8b5076 2020-12-05 stsp
5854 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5855 4fc71f3b 2022-07-12 thomas break;
5856 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5857 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5858 4fc71f3b 2022-07-12 thomas bs->selected_line);
5859 4fc71f3b 2022-07-12 thomas if (id == NULL)
5860 4fc71f3b 2022-07-12 thomas break;
5861 15a087fe 2019-02-21 stsp
5862 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5863 4fc71f3b 2022-07-12 thomas break;
5864 15a087fe 2019-02-21 stsp
5865 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5866 4fc71f3b 2022-07-12 thomas if (err)
5867 4fc71f3b 2022-07-12 thomas break;
5868 4fc71f3b 2022-07-12 thomas }
5869 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5870 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5871 aa61903a 2021-12-31 thomas s->matched_line = 0;
5872 05171be4 2022-06-23 thomas view->x = 0;
5873 1e37a5c2 2019-05-12 jcs
5874 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5875 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5876 1e37a5c2 2019-05-12 jcs break;
5877 1e37a5c2 2019-05-12 jcs default:
5878 07b0611c 2022-06-23 thomas view->count = 0;
5879 1e37a5c2 2019-05-12 jcs break;
5880 26ed57b2 2018-05-19 stsp }
5881 e5a0f69f 2018-08-18 stsp
5882 bcbd79e2 2018-08-19 stsp return err;
5883 26ed57b2 2018-05-19 stsp }
5884 26ed57b2 2018-05-19 stsp
5885 4ed7e80c 2018-05-20 stsp static const struct got_error *
5886 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5887 9f7d7167 2018-04-29 stsp {
5888 1d98034b 2023-04-14 thomas const struct got_error *error;
5889 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5890 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5891 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5892 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5893 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5894 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5895 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5896 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5897 3dbaef42 2020-11-24 stsp const char *errstr;
5898 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5899 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5900 70ac5f84 2019-03-28 stsp
5901 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5902 26ed57b2 2018-05-19 stsp switch (ch) {
5903 64453f7e 2020-11-21 stsp case 'a':
5904 64453f7e 2020-11-21 stsp force_text_diff = 1;
5905 3dbaef42 2020-11-24 stsp break;
5906 3dbaef42 2020-11-24 stsp case 'C':
5907 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5908 3dbaef42 2020-11-24 stsp &errstr);
5909 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5910 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5911 8f666e67 2022-02-12 thomas errstr, errstr);
5912 64453f7e 2020-11-21 stsp break;
5913 09b5bff8 2020-02-23 naddy case 'r':
5914 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5915 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5916 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5917 09b5bff8 2020-02-23 naddy optarg);
5918 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5919 09b5bff8 2020-02-23 naddy break;
5920 3dbaef42 2020-11-24 stsp case 'w':
5921 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5922 3dbaef42 2020-11-24 stsp break;
5923 26ed57b2 2018-05-19 stsp default:
5924 17020d27 2019-03-07 stsp usage_diff();
5925 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5926 26ed57b2 2018-05-19 stsp }
5927 26ed57b2 2018-05-19 stsp }
5928 26ed57b2 2018-05-19 stsp
5929 26ed57b2 2018-05-19 stsp argc -= optind;
5930 26ed57b2 2018-05-19 stsp argv += optind;
5931 26ed57b2 2018-05-19 stsp
5932 26ed57b2 2018-05-19 stsp if (argc == 0) {
5933 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5934 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5935 15a94983 2018-12-23 stsp id_str1 = argv[0];
5936 15a94983 2018-12-23 stsp id_str2 = argv[1];
5937 26ed57b2 2018-05-19 stsp } else
5938 26ed57b2 2018-05-19 stsp usage_diff();
5939 eb6600df 2019-01-04 stsp
5940 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5941 7cd52833 2022-06-23 thomas if (error)
5942 7cd52833 2022-06-23 thomas goto done;
5943 7cd52833 2022-06-23 thomas
5944 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5945 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5946 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5947 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5948 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5949 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5950 c156c7a4 2020-12-18 stsp goto done;
5951 a273ac94 2020-02-23 naddy if (worktree)
5952 a273ac94 2020-02-23 naddy repo_path =
5953 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5954 a273ac94 2020-02-23 naddy else
5955 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5956 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5957 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5958 c156c7a4 2020-12-18 stsp goto done;
5959 c156c7a4 2020-12-18 stsp }
5960 a273ac94 2020-02-23 naddy }
5961 a273ac94 2020-02-23 naddy
5962 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5963 eb6600df 2019-01-04 stsp if (error)
5964 eb6600df 2019-01-04 stsp goto done;
5965 26ed57b2 2018-05-19 stsp
5966 557d3365 2023-04-14 thomas init_curses();
5967 a273ac94 2020-02-23 naddy
5968 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5969 51a10b52 2020-12-26 stsp if (error)
5970 51a10b52 2020-12-26 stsp goto done;
5971 51a10b52 2020-12-26 stsp
5972 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5973 26ed57b2 2018-05-19 stsp if (error)
5974 26ed57b2 2018-05-19 stsp goto done;
5975 26ed57b2 2018-05-19 stsp
5976 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5977 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5978 26ed57b2 2018-05-19 stsp if (error)
5979 26ed57b2 2018-05-19 stsp goto done;
5980 26ed57b2 2018-05-19 stsp
5981 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5982 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5983 26ed57b2 2018-05-19 stsp if (error)
5984 26ed57b2 2018-05-19 stsp goto done;
5985 26ed57b2 2018-05-19 stsp
5986 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5987 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5988 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5989 ea5e7bb5 2018-08-01 stsp goto done;
5990 ea5e7bb5 2018-08-01 stsp }
5991 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5992 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5993 5dc9f4bc 2018-08-04 stsp if (error)
5994 5dc9f4bc 2018-08-04 stsp goto done;
5995 557d3365 2023-04-14 thomas error = view_loop(view);
5996 26ed57b2 2018-05-19 stsp done:
5997 3dbaef42 2020-11-24 stsp free(label1);
5998 3dbaef42 2020-11-24 stsp free(label2);
5999 c02c541e 2019-03-29 stsp free(repo_path);
6000 a273ac94 2020-02-23 naddy free(cwd);
6001 1d0f4054 2021-06-17 stsp if (repo) {
6002 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6003 1d0f4054 2021-06-17 stsp if (error == NULL)
6004 1d0f4054 2021-06-17 stsp error = close_err;
6005 1d0f4054 2021-06-17 stsp }
6006 a273ac94 2020-02-23 naddy if (worktree)
6007 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6008 7cd52833 2022-06-23 thomas if (pack_fds) {
6009 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6010 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6011 7cd52833 2022-06-23 thomas if (error == NULL)
6012 7cd52833 2022-06-23 thomas error = pack_err;
6013 b85a3496 2023-04-14 thomas }
6014 51a10b52 2020-12-26 stsp tog_free_refs();
6015 26ed57b2 2018-05-19 stsp return error;
6016 9f7d7167 2018-04-29 stsp }
6017 9f7d7167 2018-04-29 stsp
6018 4ed7e80c 2018-05-20 stsp __dead static void
6019 9f7d7167 2018-04-29 stsp usage_blame(void)
6020 9f7d7167 2018-04-29 stsp {
6021 80ddbec8 2018-04-29 stsp endwin();
6022 91198554 2022-06-23 thomas fprintf(stderr,
6023 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6024 9f7d7167 2018-04-29 stsp getprogname());
6025 9f7d7167 2018-04-29 stsp exit(1);
6026 9f7d7167 2018-04-29 stsp }
6027 84451b3e 2018-07-10 stsp
6028 84451b3e 2018-07-10 stsp struct tog_blame_line {
6029 84451b3e 2018-07-10 stsp int annotated;
6030 84451b3e 2018-07-10 stsp struct got_object_id *id;
6031 84451b3e 2018-07-10 stsp };
6032 9f7d7167 2018-04-29 stsp
6033 4ed7e80c 2018-05-20 stsp static const struct got_error *
6034 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6035 84451b3e 2018-07-10 stsp {
6036 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6037 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6038 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6039 84451b3e 2018-07-10 stsp const struct got_error *err;
6040 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6041 826082fe 2020-12-10 stsp char *line = NULL;
6042 826082fe 2020-12-10 stsp size_t linesize = 0;
6043 826082fe 2020-12-10 stsp ssize_t linelen;
6044 84451b3e 2018-07-10 stsp wchar_t *wline;
6045 27a741e5 2019-09-11 stsp int width;
6046 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6047 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6048 ab089a2a 2018-07-12 stsp char *id_str;
6049 11b20872 2019-11-08 stsp struct tog_color *tc;
6050 ab089a2a 2018-07-12 stsp
6051 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6052 ab089a2a 2018-07-12 stsp if (err)
6053 ab089a2a 2018-07-12 stsp return err;
6054 84451b3e 2018-07-10 stsp
6055 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6056 f7d12f7e 2018-08-01 stsp werase(view->window);
6057 84451b3e 2018-07-10 stsp
6058 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6059 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6060 ab089a2a 2018-07-12 stsp free(id_str);
6061 ab089a2a 2018-07-12 stsp return err;
6062 ab089a2a 2018-07-12 stsp }
6063 ab089a2a 2018-07-12 stsp
6064 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6065 ab089a2a 2018-07-12 stsp free(line);
6066 2550e4c3 2018-07-13 stsp line = NULL;
6067 1cae65b4 2019-09-22 stsp if (err)
6068 1cae65b4 2019-09-22 stsp return err;
6069 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6070 a3404814 2018-09-02 stsp wstandout(view->window);
6071 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6072 11b20872 2019-11-08 stsp if (tc)
6073 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6074 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6075 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6076 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6077 11b20872 2019-11-08 stsp if (tc)
6078 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6079 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6080 a3404814 2018-09-02 stsp wstandend(view->window);
6081 2550e4c3 2018-07-13 stsp free(wline);
6082 2550e4c3 2018-07-13 stsp wline = NULL;
6083 ab089a2a 2018-07-12 stsp
6084 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6085 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6086 07dd3ed3 2022-08-06 thomas
6087 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6088 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6089 1134ebde 2023-04-22 thomas int rc;
6090 1134ebde 2023-04-22 thomas
6091 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6092 1134ebde 2023-04-22 thomas if (rc)
6093 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6094 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6095 1134ebde 2023-04-22 thomas }
6096 1134ebde 2023-04-22 thomas
6097 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6098 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6099 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6100 ab089a2a 2018-07-12 stsp free(id_str);
6101 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6102 ab089a2a 2018-07-12 stsp }
6103 ab089a2a 2018-07-12 stsp free(id_str);
6104 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6105 3f60a8ef 2018-07-10 stsp free(line);
6106 2550e4c3 2018-07-13 stsp line = NULL;
6107 3f60a8ef 2018-07-10 stsp if (err)
6108 3f60a8ef 2018-07-10 stsp return err;
6109 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6110 2550e4c3 2018-07-13 stsp free(wline);
6111 2550e4c3 2018-07-13 stsp wline = NULL;
6112 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6113 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6114 3f60a8ef 2018-07-10 stsp
6115 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6116 05171be4 2022-06-23 thomas view->maxx = 0;
6117 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6118 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6119 826082fe 2020-12-10 stsp if (linelen == -1) {
6120 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6121 826082fe 2020-12-10 stsp s->eof = 1;
6122 826082fe 2020-12-10 stsp break;
6123 826082fe 2020-12-10 stsp }
6124 84451b3e 2018-07-10 stsp free(line);
6125 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6126 84451b3e 2018-07-10 stsp }
6127 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6128 07dd3ed3 2022-08-06 thomas continue;
6129 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6130 826082fe 2020-12-10 stsp continue;
6131 cbea3800 2022-06-23 thomas
6132 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6133 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6134 cbea3800 2022-06-23 thomas if (err) {
6135 cbea3800 2022-06-23 thomas free(line);
6136 cbea3800 2022-06-23 thomas return err;
6137 cbea3800 2022-06-23 thomas }
6138 cbea3800 2022-06-23 thomas free(wline);
6139 cbea3800 2022-06-23 thomas wline = NULL;
6140 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6141 84451b3e 2018-07-10 stsp
6142 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6143 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6144 b700b5d6 2018-07-10 stsp
6145 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6146 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6147 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6148 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6149 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6150 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6151 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6152 8d0fe45a 2019-08-12 stsp char *id_str;
6153 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6154 91198554 2022-06-23 thomas blame_line->id);
6155 8d0fe45a 2019-08-12 stsp if (err) {
6156 8d0fe45a 2019-08-12 stsp free(line);
6157 8d0fe45a 2019-08-12 stsp return err;
6158 8d0fe45a 2019-08-12 stsp }
6159 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6160 11b20872 2019-11-08 stsp if (tc)
6161 11b20872 2019-11-08 stsp wattr_on(view->window,
6162 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6163 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6164 11b20872 2019-11-08 stsp if (tc)
6165 11b20872 2019-11-08 stsp wattr_off(view->window,
6166 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6167 8d0fe45a 2019-08-12 stsp free(id_str);
6168 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6169 8d0fe45a 2019-08-12 stsp } else {
6170 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6171 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6172 84451b3e 2018-07-10 stsp }
6173 ee41ec32 2018-07-10 stsp } else {
6174 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6175 ee41ec32 2018-07-10 stsp prev_id = NULL;
6176 ee41ec32 2018-07-10 stsp }
6177 84451b3e 2018-07-10 stsp
6178 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6179 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6180 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6181 27a741e5 2019-09-11 stsp
6182 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6183 41605754 2020-11-12 stsp width = 9;
6184 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6185 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6186 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6187 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6188 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6189 41605754 2020-11-12 stsp if (err) {
6190 41605754 2020-11-12 stsp free(line);
6191 41605754 2020-11-12 stsp return err;
6192 41605754 2020-11-12 stsp }
6193 41605754 2020-11-12 stsp width += 9;
6194 41605754 2020-11-12 stsp } else {
6195 923086fd 2022-06-23 thomas int skip;
6196 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6197 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6198 923086fd 2022-06-23 thomas if (err) {
6199 923086fd 2022-06-23 thomas free(line);
6200 923086fd 2022-06-23 thomas return err;
6201 05171be4 2022-06-23 thomas }
6202 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6203 02bce7e2 2022-06-23 thomas width += 9;
6204 41605754 2020-11-12 stsp free(wline);
6205 41605754 2020-11-12 stsp wline = NULL;
6206 41605754 2020-11-12 stsp }
6207 41605754 2020-11-12 stsp
6208 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6209 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6210 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6211 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6212 84451b3e 2018-07-10 stsp }
6213 826082fe 2020-12-10 stsp free(line);
6214 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6215 84451b3e 2018-07-10 stsp
6216 a5d43cac 2022-07-01 thomas view_border(view);
6217 84451b3e 2018-07-10 stsp
6218 84451b3e 2018-07-10 stsp return NULL;
6219 84451b3e 2018-07-10 stsp }
6220 84451b3e 2018-07-10 stsp
6221 84451b3e 2018-07-10 stsp static const struct got_error *
6222 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6223 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6224 84451b3e 2018-07-10 stsp {
6225 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6226 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6227 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6228 1a76625f 2018-10-22 stsp int errcode;
6229 84451b3e 2018-07-10 stsp
6230 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6231 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6232 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6233 84451b3e 2018-07-10 stsp
6234 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6235 1a76625f 2018-10-22 stsp if (errcode)
6236 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6237 84451b3e 2018-07-10 stsp
6238 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6239 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6240 d68a0a7d 2018-07-10 stsp goto done;
6241 d68a0a7d 2018-07-10 stsp }
6242 d68a0a7d 2018-07-10 stsp
6243 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6244 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6245 d68a0a7d 2018-07-10 stsp
6246 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6247 d68a0a7d 2018-07-10 stsp if (line->annotated)
6248 d68a0a7d 2018-07-10 stsp goto done;
6249 d68a0a7d 2018-07-10 stsp
6250 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6251 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6253 84451b3e 2018-07-10 stsp goto done;
6254 84451b3e 2018-07-10 stsp }
6255 84451b3e 2018-07-10 stsp line->annotated = 1;
6256 84451b3e 2018-07-10 stsp done:
6257 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6258 1a76625f 2018-10-22 stsp if (errcode)
6259 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6260 84451b3e 2018-07-10 stsp return err;
6261 84451b3e 2018-07-10 stsp }
6262 84451b3e 2018-07-10 stsp
6263 84451b3e 2018-07-10 stsp static void *
6264 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6265 84451b3e 2018-07-10 stsp {
6266 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6267 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6268 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6269 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6270 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6271 00a8878e 2022-07-01 thomas
6272 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6273 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6274 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6275 9117a7b7 2022-07-01 thomas
6276 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6277 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6278 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6279 9117a7b7 2022-07-01 thomas goto done;
6280 9117a7b7 2022-07-01 thomas }
6281 18430de3 2018-07-10 stsp
6282 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6283 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6284 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6285 9117a7b7 2022-07-01 thomas goto done;
6286 9117a7b7 2022-07-01 thomas }
6287 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6288 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6289 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6290 9117a7b7 2022-07-01 thomas goto done;
6291 9117a7b7 2022-07-01 thomas }
6292 9117a7b7 2022-07-01 thomas
6293 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6294 61266923 2020-01-14 stsp if (err)
6295 9117a7b7 2022-07-01 thomas goto done;
6296 61266923 2020-01-14 stsp
6297 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6298 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6299 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6300 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6301 fc06ba56 2019-08-22 stsp err = NULL;
6302 18430de3 2018-07-10 stsp
6303 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6304 9117a7b7 2022-07-01 thomas if (errcode) {
6305 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6306 9117a7b7 2022-07-01 thomas goto done;
6307 9117a7b7 2022-07-01 thomas }
6308 18430de3 2018-07-10 stsp
6309 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6310 1d0f4054 2021-06-17 stsp if (err == NULL)
6311 1d0f4054 2021-06-17 stsp err = close_err;
6312 c9beca56 2018-07-22 stsp ta->repo = NULL;
6313 c9beca56 2018-07-22 stsp *ta->complete = 1;
6314 1134ebde 2023-04-22 thomas
6315 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6316 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6317 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6318 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6319 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6320 1134ebde 2023-04-22 thomas }
6321 18430de3 2018-07-10 stsp
6322 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6323 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6324 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6325 18430de3 2018-07-10 stsp
6326 9117a7b7 2022-07-01 thomas done:
6327 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6328 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6329 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6330 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6331 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6332 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6333 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6334 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6335 00a8878e 2022-07-01 thomas
6336 18430de3 2018-07-10 stsp return (void *)err;
6337 84451b3e 2018-07-10 stsp }
6338 84451b3e 2018-07-10 stsp
6339 245d91c1 2018-07-12 stsp static struct got_object_id *
6340 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6341 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6342 245d91c1 2018-07-12 stsp {
6343 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6344 8d0fe45a 2019-08-12 stsp
6345 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6346 8d0fe45a 2019-08-12 stsp return NULL;
6347 b880a918 2018-07-10 stsp
6348 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6349 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6350 4fc71f3b 2022-07-12 thomas return NULL;
6351 4fc71f3b 2022-07-12 thomas
6352 4fc71f3b 2022-07-12 thomas return line->id;
6353 4fc71f3b 2022-07-12 thomas }
6354 4fc71f3b 2022-07-12 thomas
6355 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6356 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6357 4fc71f3b 2022-07-12 thomas int lineno)
6358 4fc71f3b 2022-07-12 thomas {
6359 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6360 4fc71f3b 2022-07-12 thomas
6361 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6362 4fc71f3b 2022-07-12 thomas return NULL;
6363 4fc71f3b 2022-07-12 thomas
6364 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6365 245d91c1 2018-07-12 stsp if (!line->annotated)
6366 245d91c1 2018-07-12 stsp return NULL;
6367 245d91c1 2018-07-12 stsp
6368 245d91c1 2018-07-12 stsp return line->id;
6369 b880a918 2018-07-10 stsp }
6370 245d91c1 2018-07-12 stsp
6371 b880a918 2018-07-10 stsp static const struct got_error *
6372 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6373 a70480e0 2018-06-23 stsp {
6374 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6375 245d91c1 2018-07-12 stsp int i;
6376 245d91c1 2018-07-12 stsp
6377 245d91c1 2018-07-12 stsp if (blame->thread) {
6378 1a76625f 2018-10-22 stsp int errcode;
6379 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6380 1a76625f 2018-10-22 stsp if (errcode)
6381 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6382 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6383 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6384 1a76625f 2018-10-22 stsp if (errcode)
6385 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6386 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6387 1a76625f 2018-10-22 stsp if (errcode)
6388 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6389 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6390 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6391 245d91c1 2018-07-12 stsp err = NULL;
6392 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6393 245d91c1 2018-07-12 stsp }
6394 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6395 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6396 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6397 1d0f4054 2021-06-17 stsp if (err == NULL)
6398 1d0f4054 2021-06-17 stsp err = close_err;
6399 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6400 245d91c1 2018-07-12 stsp }
6401 245d91c1 2018-07-12 stsp if (blame->f) {
6402 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6403 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6404 245d91c1 2018-07-12 stsp blame->f = NULL;
6405 245d91c1 2018-07-12 stsp }
6406 57670559 2018-12-23 stsp if (blame->lines) {
6407 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6408 57670559 2018-12-23 stsp free(blame->lines[i].id);
6409 57670559 2018-12-23 stsp free(blame->lines);
6410 57670559 2018-12-23 stsp blame->lines = NULL;
6411 57670559 2018-12-23 stsp }
6412 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6413 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6414 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6415 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6416 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6417 7cd52833 2022-06-23 thomas if (err == NULL)
6418 7cd52833 2022-06-23 thomas err = pack_err;
6419 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6420 7cd52833 2022-06-23 thomas }
6421 245d91c1 2018-07-12 stsp return err;
6422 245d91c1 2018-07-12 stsp }
6423 245d91c1 2018-07-12 stsp
6424 245d91c1 2018-07-12 stsp static const struct got_error *
6425 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6426 fc06ba56 2019-08-22 stsp {
6427 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6428 fc06ba56 2019-08-22 stsp int *done = arg;
6429 fc06ba56 2019-08-22 stsp int errcode;
6430 fc06ba56 2019-08-22 stsp
6431 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6432 fc06ba56 2019-08-22 stsp if (errcode)
6433 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6434 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6435 fc06ba56 2019-08-22 stsp
6436 fc06ba56 2019-08-22 stsp if (*done)
6437 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6438 fc06ba56 2019-08-22 stsp
6439 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6440 fc06ba56 2019-08-22 stsp if (errcode)
6441 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6442 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6443 fc06ba56 2019-08-22 stsp
6444 fc06ba56 2019-08-22 stsp return err;
6445 fc06ba56 2019-08-22 stsp }
6446 fc06ba56 2019-08-22 stsp
6447 fc06ba56 2019-08-22 stsp static const struct got_error *
6448 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6449 245d91c1 2018-07-12 stsp {
6450 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6451 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6452 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6453 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6454 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6455 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6456 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6457 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6458 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6459 a70480e0 2018-06-23 stsp
6460 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6461 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6462 27d434c2 2018-09-15 stsp if (err)
6463 15a94983 2018-12-23 stsp return err;
6464 f4ae6ddb 2022-07-01 thomas
6465 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6466 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6467 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6468 f4ae6ddb 2022-07-01 thomas goto done;
6469 f4ae6ddb 2022-07-01 thomas }
6470 945f9229 2022-04-16 thomas
6471 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6472 945f9229 2022-04-16 thomas if (err)
6473 945f9229 2022-04-16 thomas goto done;
6474 27d434c2 2018-09-15 stsp
6475 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6476 84451b3e 2018-07-10 stsp if (err)
6477 84451b3e 2018-07-10 stsp goto done;
6478 27d434c2 2018-09-15 stsp
6479 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6480 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6481 84451b3e 2018-07-10 stsp goto done;
6482 84451b3e 2018-07-10 stsp }
6483 a70480e0 2018-06-23 stsp
6484 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6485 a70480e0 2018-06-23 stsp if (err)
6486 a70480e0 2018-06-23 stsp goto done;
6487 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6488 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6489 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6490 84451b3e 2018-07-10 stsp goto done;
6491 84451b3e 2018-07-10 stsp }
6492 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6493 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6494 1fddf795 2021-01-20 stsp if (err)
6495 1fddf795 2021-01-20 stsp goto done;
6496 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6497 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6498 84451b3e 2018-07-10 stsp goto done;
6499 1fddf795 2021-01-20 stsp }
6500 b02560ec 2019-08-19 stsp
6501 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6502 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6503 b02560ec 2019-08-19 stsp blame->nlines--;
6504 a70480e0 2018-06-23 stsp
6505 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6506 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6507 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6508 84451b3e 2018-07-10 stsp goto done;
6509 84451b3e 2018-07-10 stsp }
6510 a70480e0 2018-06-23 stsp
6511 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6512 bd24772e 2018-07-11 stsp if (err)
6513 bd24772e 2018-07-11 stsp goto done;
6514 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6515 7cd52833 2022-06-23 thomas pack_fds);
6516 7cd52833 2022-06-23 thomas if (err)
6517 7cd52833 2022-06-23 thomas goto done;
6518 bd24772e 2018-07-11 stsp
6519 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6520 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6521 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6522 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6523 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6524 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6525 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6526 245d91c1 2018-07-12 stsp goto done;
6527 245d91c1 2018-07-12 stsp }
6528 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6529 245d91c1 2018-07-12 stsp
6530 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6531 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6532 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6533 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6534 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6535 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6536 a5388363 2020-12-01 naddy s->blame_complete = 0;
6537 f5a09613 2020-12-13 naddy
6538 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6539 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6540 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6541 f5a09613 2020-12-13 naddy s->selected_line = 1;
6542 f5a09613 2020-12-13 naddy }
6543 aa61903a 2021-12-31 thomas s->matched_line = 0;
6544 245d91c1 2018-07-12 stsp
6545 245d91c1 2018-07-12 stsp done:
6546 945f9229 2022-04-16 thomas if (commit)
6547 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6548 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6549 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6550 245d91c1 2018-07-12 stsp if (blob)
6551 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6552 27d434c2 2018-09-15 stsp free(obj_id);
6553 245d91c1 2018-07-12 stsp if (err)
6554 245d91c1 2018-07-12 stsp stop_blame(blame);
6555 245d91c1 2018-07-12 stsp return err;
6556 245d91c1 2018-07-12 stsp }
6557 245d91c1 2018-07-12 stsp
6558 245d91c1 2018-07-12 stsp static const struct got_error *
6559 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6560 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6561 245d91c1 2018-07-12 stsp {
6562 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6563 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6564 dbc6a6b6 2018-07-12 stsp
6565 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6566 245d91c1 2018-07-12 stsp
6567 c4843652 2019-08-12 stsp s->path = strdup(path);
6568 c4843652 2019-08-12 stsp if (s->path == NULL)
6569 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6570 c4843652 2019-08-12 stsp
6571 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6572 c4843652 2019-08-12 stsp if (err) {
6573 c4843652 2019-08-12 stsp free(s->path);
6574 7cbe629d 2018-08-04 stsp return err;
6575 c4843652 2019-08-12 stsp }
6576 245d91c1 2018-07-12 stsp
6577 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6578 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6579 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6580 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6581 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6582 fb2756b9 2018-08-04 stsp s->repo = repo;
6583 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6584 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6585 7cbe629d 2018-08-04 stsp
6586 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6587 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6588 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6589 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6590 11b20872 2019-11-08 stsp if (err)
6591 11b20872 2019-11-08 stsp return err;
6592 11b20872 2019-11-08 stsp }
6593 11b20872 2019-11-08 stsp
6594 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6595 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6596 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6597 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6598 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6599 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6600 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6601 1134ebde 2023-04-22 thomas
6602 1134ebde 2023-04-22 thomas if (using_mock_io) {
6603 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6604 1134ebde 2023-04-22 thomas int rc;
6605 1134ebde 2023-04-22 thomas
6606 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6607 1134ebde 2023-04-22 thomas if (rc)
6608 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6609 1134ebde 2023-04-22 thomas }
6610 e5a0f69f 2018-08-18 stsp
6611 a5388363 2020-12-01 naddy return run_blame(view);
6612 7cbe629d 2018-08-04 stsp }
6613 7cbe629d 2018-08-04 stsp
6614 e5a0f69f 2018-08-18 stsp static const struct got_error *
6615 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6616 7cbe629d 2018-08-04 stsp {
6617 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6618 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6619 7cbe629d 2018-08-04 stsp
6620 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6621 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6622 e5a0f69f 2018-08-18 stsp
6623 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6624 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6625 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6626 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6627 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6628 1134ebde 2023-04-22 thomas }
6629 1134ebde 2023-04-22 thomas
6630 1134ebde 2023-04-22 thomas if (using_mock_io) {
6631 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6632 1134ebde 2023-04-22 thomas int rc;
6633 1134ebde 2023-04-22 thomas
6634 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6635 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6636 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6637 7cbe629d 2018-08-04 stsp }
6638 e5a0f69f 2018-08-18 stsp
6639 e5a0f69f 2018-08-18 stsp free(s->path);
6640 11b20872 2019-11-08 stsp free_colors(&s->colors);
6641 e5a0f69f 2018-08-18 stsp return err;
6642 7cbe629d 2018-08-04 stsp }
6643 7cbe629d 2018-08-04 stsp
6644 7cbe629d 2018-08-04 stsp static const struct got_error *
6645 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6646 6c4c42e0 2019-06-24 stsp {
6647 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6648 6c4c42e0 2019-06-24 stsp
6649 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6650 6c4c42e0 2019-06-24 stsp return NULL;
6651 2a7d3cd7 2022-09-17 thomas }
6652 2a7d3cd7 2022-09-17 thomas
6653 2a7d3cd7 2022-09-17 thomas static void
6654 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6655 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6656 2a7d3cd7 2022-09-17 thomas {
6657 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6658 2a7d3cd7 2022-09-17 thomas
6659 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6660 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6661 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6662 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6663 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6664 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6665 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6666 6c4c42e0 2019-06-24 stsp }
6667 6c4c42e0 2019-06-24 stsp
6668 6c4c42e0 2019-06-24 stsp static const struct got_error *
6669 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6670 7cbe629d 2018-08-04 stsp {
6671 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6672 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6673 2b380cc8 2018-10-24 stsp int errcode;
6674 2b380cc8 2018-10-24 stsp
6675 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6676 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6677 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6678 2b380cc8 2018-10-24 stsp if (errcode)
6679 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6680 51fe7530 2019-08-19 stsp
6681 557d3365 2023-04-14 thomas if (!using_mock_io)
6682 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6683 2b380cc8 2018-10-24 stsp }
6684 e5a0f69f 2018-08-18 stsp
6685 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6686 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6687 51fe7530 2019-08-19 stsp
6688 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6689 e5a0f69f 2018-08-18 stsp
6690 a5d43cac 2022-07-01 thomas view_border(view);
6691 e5a0f69f 2018-08-18 stsp return err;
6692 e5a0f69f 2018-08-18 stsp }
6693 e5a0f69f 2018-08-18 stsp
6694 e5a0f69f 2018-08-18 stsp static const struct got_error *
6695 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6696 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6697 eaeaa612 2022-07-20 thomas {
6698 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6699 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6700 eaeaa612 2022-07-20 thomas
6701 eaeaa612 2022-07-20 thomas *new_view = NULL;
6702 eaeaa612 2022-07-20 thomas
6703 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6704 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6705 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6706 eaeaa612 2022-07-20 thomas
6707 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6708 eaeaa612 2022-07-20 thomas if (err)
6709 eaeaa612 2022-07-20 thomas view_close(log_view);
6710 eaeaa612 2022-07-20 thomas else
6711 eaeaa612 2022-07-20 thomas *new_view = log_view;
6712 eaeaa612 2022-07-20 thomas
6713 eaeaa612 2022-07-20 thomas return err;
6714 eaeaa612 2022-07-20 thomas }
6715 eaeaa612 2022-07-20 thomas
6716 eaeaa612 2022-07-20 thomas static const struct got_error *
6717 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6718 e5a0f69f 2018-08-18 stsp {
6719 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6720 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6721 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6722 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6723 a5d43cac 2022-07-01 thomas
6724 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6725 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6726 a5d43cac 2022-07-01 thomas --eos; /* border */
6727 7cbe629d 2018-08-04 stsp
6728 e5a0f69f 2018-08-18 stsp switch (ch) {
6729 05171be4 2022-06-23 thomas case '0':
6730 05171be4 2022-06-23 thomas case '$':
6731 05171be4 2022-06-23 thomas case KEY_RIGHT:
6732 05171be4 2022-06-23 thomas case 'l':
6733 05171be4 2022-06-23 thomas case KEY_LEFT:
6734 05171be4 2022-06-23 thomas case 'h':
6735 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6736 05171be4 2022-06-23 thomas break;
6737 1e37a5c2 2019-05-12 jcs case 'q':
6738 1e37a5c2 2019-05-12 jcs s->done = 1;
6739 4deef56f 2021-09-02 naddy break;
6740 4deef56f 2021-09-02 naddy case 'g':
6741 4deef56f 2021-09-02 naddy case KEY_HOME:
6742 4deef56f 2021-09-02 naddy s->selected_line = 1;
6743 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6744 07b0611c 2022-06-23 thomas view->count = 0;
6745 4deef56f 2021-09-02 naddy break;
6746 4deef56f 2021-09-02 naddy case 'G':
6747 4deef56f 2021-09-02 naddy case KEY_END:
6748 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6749 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6750 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6751 4deef56f 2021-09-02 naddy } else {
6752 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6753 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6754 4deef56f 2021-09-02 naddy }
6755 07b0611c 2022-06-23 thomas view->count = 0;
6756 1e37a5c2 2019-05-12 jcs break;
6757 1e37a5c2 2019-05-12 jcs case 'k':
6758 1e37a5c2 2019-05-12 jcs case KEY_UP:
6759 f7140bf5 2021-10-17 thomas case CTRL('p'):
6760 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6761 1e37a5c2 2019-05-12 jcs s->selected_line--;
6762 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6763 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6764 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6765 07b0611c 2022-06-23 thomas else
6766 07b0611c 2022-06-23 thomas view->count = 0;
6767 1e37a5c2 2019-05-12 jcs break;
6768 70f17a53 2022-06-13 thomas case CTRL('u'):
6769 23427b14 2022-06-23 thomas case 'u':
6770 70f17a53 2022-06-13 thomas nscroll /= 2;
6771 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6772 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6773 ea025d1d 2020-02-22 naddy case CTRL('b'):
6774 1c5e5faa 2022-06-23 thomas case 'b':
6775 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6776 07b0611c 2022-06-23 thomas if (view->count > 1)
6777 07b0611c 2022-06-23 thomas nscroll += nscroll;
6778 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6779 07b0611c 2022-06-23 thomas view->count = 0;
6780 e5a0f69f 2018-08-18 stsp break;
6781 1e37a5c2 2019-05-12 jcs }
6782 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6783 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6784 1e37a5c2 2019-05-12 jcs else
6785 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6786 1e37a5c2 2019-05-12 jcs break;
6787 1e37a5c2 2019-05-12 jcs case 'j':
6788 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6789 f7140bf5 2021-10-17 thomas case CTRL('n'):
6790 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6791 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6792 1e37a5c2 2019-05-12 jcs s->selected_line++;
6793 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6794 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6795 07b0611c 2022-06-23 thomas else
6796 07b0611c 2022-06-23 thomas view->count = 0;
6797 1e37a5c2 2019-05-12 jcs break;
6798 1c5e5faa 2022-06-23 thomas case 'c':
6799 1e37a5c2 2019-05-12 jcs case 'p': {
6800 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6801 07b0611c 2022-06-23 thomas
6802 07b0611c 2022-06-23 thomas view->count = 0;
6803 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6804 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6805 1e37a5c2 2019-05-12 jcs if (id == NULL)
6806 e5a0f69f 2018-08-18 stsp break;
6807 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6808 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6809 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6810 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6811 1e37a5c2 2019-05-12 jcs int obj_type;
6812 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6813 1e37a5c2 2019-05-12 jcs s->repo, id);
6814 e5a0f69f 2018-08-18 stsp if (err)
6815 e5a0f69f 2018-08-18 stsp break;
6816 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6817 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6818 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6819 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6820 e5a0f69f 2018-08-18 stsp break;
6821 e5a0f69f 2018-08-18 stsp }
6822 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6823 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6824 ec242592 2022-04-22 thomas s->repo, &pid->id);
6825 945f9229 2022-04-16 thomas if (err)
6826 945f9229 2022-04-16 thomas break;
6827 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6828 945f9229 2022-04-16 thomas pcommit, s->path);
6829 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6830 e5a0f69f 2018-08-18 stsp if (err) {
6831 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6832 1e37a5c2 2019-05-12 jcs err = NULL;
6833 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6834 e5a0f69f 2018-08-18 stsp break;
6835 e5a0f69f 2018-08-18 stsp }
6836 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6837 1e37a5c2 2019-05-12 jcs blob_id);
6838 1e37a5c2 2019-05-12 jcs free(blob_id);
6839 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6840 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6841 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6842 e5a0f69f 2018-08-18 stsp break;
6843 1e37a5c2 2019-05-12 jcs }
6844 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6845 ec242592 2022-04-22 thomas &pid->id);
6846 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6847 1e37a5c2 2019-05-12 jcs } else {
6848 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6849 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6850 1e37a5c2 2019-05-12 jcs break;
6851 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6852 1e37a5c2 2019-05-12 jcs id);
6853 1e37a5c2 2019-05-12 jcs }
6854 1e37a5c2 2019-05-12 jcs if (err)
6855 e5a0f69f 2018-08-18 stsp break;
6856 1e37a5c2 2019-05-12 jcs s->done = 1;
6857 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6858 1e37a5c2 2019-05-12 jcs s->done = 0;
6859 1e37a5c2 2019-05-12 jcs if (thread_err)
6860 1e37a5c2 2019-05-12 jcs break;
6861 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6862 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6863 a5388363 2020-12-01 naddy err = run_blame(view);
6864 1e37a5c2 2019-05-12 jcs if (err)
6865 1e37a5c2 2019-05-12 jcs break;
6866 1e37a5c2 2019-05-12 jcs break;
6867 1e37a5c2 2019-05-12 jcs }
6868 1c5e5faa 2022-06-23 thomas case 'C': {
6869 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6870 07b0611c 2022-06-23 thomas
6871 07b0611c 2022-06-23 thomas view->count = 0;
6872 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6873 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6874 1e37a5c2 2019-05-12 jcs break;
6875 1e37a5c2 2019-05-12 jcs s->done = 1;
6876 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6877 1e37a5c2 2019-05-12 jcs s->done = 0;
6878 1e37a5c2 2019-05-12 jcs if (thread_err)
6879 1e37a5c2 2019-05-12 jcs break;
6880 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6881 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6882 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6883 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6884 a5388363 2020-12-01 naddy err = run_blame(view);
6885 1e37a5c2 2019-05-12 jcs if (err)
6886 1e37a5c2 2019-05-12 jcs break;
6887 1e37a5c2 2019-05-12 jcs break;
6888 1e37a5c2 2019-05-12 jcs }
6889 2a31b33b 2022-07-23 thomas case 'L':
6890 eaeaa612 2022-07-20 thomas view->count = 0;
6891 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6892 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6893 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6894 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6895 eaeaa612 2022-07-20 thomas break;
6896 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6897 1e37a5c2 2019-05-12 jcs case '\r': {
6898 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6899 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6900 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6901 07b0611c 2022-06-23 thomas
6902 07b0611c 2022-06-23 thomas view->count = 0;
6903 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6904 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6905 1e37a5c2 2019-05-12 jcs if (id == NULL)
6906 1e37a5c2 2019-05-12 jcs break;
6907 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6908 1e37a5c2 2019-05-12 jcs if (err)
6909 1e37a5c2 2019-05-12 jcs break;
6910 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6911 4fc71f3b 2022-07-12 thomas if (*new_view) {
6912 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6913 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6914 4fc71f3b 2022-07-12 thomas if (err)
6915 4fc71f3b 2022-07-12 thomas break;
6916 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6917 4fc71f3b 2022-07-12 thomas } else {
6918 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6919 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6920 a5d43cac 2022-07-01 thomas
6921 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6922 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6923 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6924 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6925 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6926 4fc71f3b 2022-07-12 thomas break;
6927 4fc71f3b 2022-07-12 thomas }
6928 15a94983 2018-12-23 stsp }
6929 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6930 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6931 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6932 1e37a5c2 2019-05-12 jcs if (err) {
6933 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6934 1e37a5c2 2019-05-12 jcs break;
6935 1e37a5c2 2019-05-12 jcs }
6936 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6937 4fc71f3b 2022-07-12 thomas s->selected_line;
6938 4fc71f3b 2022-07-12 thomas if (*new_view)
6939 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6940 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6941 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6942 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6943 a5d43cac 2022-07-01 thomas if (err)
6944 a5d43cac 2022-07-01 thomas break;
6945 a5d43cac 2022-07-01 thomas }
6946 a5d43cac 2022-07-01 thomas
6947 e78dc838 2020-12-04 stsp view->focussed = 0;
6948 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6949 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6950 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6951 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6952 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6953 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6954 1e37a5c2 2019-05-12 jcs if (err)
6955 34bc9ec9 2019-02-22 stsp break;
6956 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6957 40236d76 2022-06-23 thomas if (err)
6958 40236d76 2022-06-23 thomas break;
6959 e78dc838 2020-12-04 stsp view->focus_child = 1;
6960 1e37a5c2 2019-05-12 jcs } else
6961 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6962 1e37a5c2 2019-05-12 jcs if (err)
6963 e5a0f69f 2018-08-18 stsp break;
6964 1e37a5c2 2019-05-12 jcs break;
6965 1e37a5c2 2019-05-12 jcs }
6966 70f17a53 2022-06-13 thomas case CTRL('d'):
6967 23427b14 2022-06-23 thomas case 'd':
6968 70f17a53 2022-06-13 thomas nscroll /= 2;
6969 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6970 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6971 ea025d1d 2020-02-22 naddy case CTRL('f'):
6972 1c5e5faa 2022-06-23 thomas case 'f':
6973 1e37a5c2 2019-05-12 jcs case ' ':
6974 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6975 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6976 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6977 07b0611c 2022-06-23 thomas view->count = 0;
6978 e5a0f69f 2018-08-18 stsp break;
6979 1e37a5c2 2019-05-12 jcs }
6980 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6981 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6982 70f17a53 2022-06-13 thomas s->selected_line +=
6983 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6984 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6985 1e37a5c2 2019-05-12 jcs }
6986 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6987 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6988 1e37a5c2 2019-05-12 jcs else
6989 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6990 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6991 1e37a5c2 2019-05-12 jcs break;
6992 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6993 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6994 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6995 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6996 1e37a5c2 2019-05-12 jcs }
6997 1e37a5c2 2019-05-12 jcs break;
6998 1e37a5c2 2019-05-12 jcs default:
6999 07b0611c 2022-06-23 thomas view->count = 0;
7000 1e37a5c2 2019-05-12 jcs break;
7001 a70480e0 2018-06-23 stsp }
7002 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7003 adf4c9e0 2022-07-03 thomas }
7004 adf4c9e0 2022-07-03 thomas
7005 adf4c9e0 2022-07-03 thomas static const struct got_error *
7006 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7007 adf4c9e0 2022-07-03 thomas {
7008 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7009 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7010 adf4c9e0 2022-07-03 thomas
7011 adf4c9e0 2022-07-03 thomas view->count = 0;
7012 adf4c9e0 2022-07-03 thomas s->done = 1;
7013 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7014 adf4c9e0 2022-07-03 thomas s->done = 0;
7015 adf4c9e0 2022-07-03 thomas if (err)
7016 adf4c9e0 2022-07-03 thomas return err;
7017 adf4c9e0 2022-07-03 thomas return run_blame(view);
7018 a70480e0 2018-06-23 stsp }
7019 a70480e0 2018-06-23 stsp
7020 a70480e0 2018-06-23 stsp static const struct got_error *
7021 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7022 9f7d7167 2018-04-29 stsp {
7023 1d98034b 2023-04-14 thomas const struct got_error *error;
7024 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7025 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7026 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7027 0587e10c 2020-07-23 stsp char *link_target = NULL;
7028 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7029 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7030 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
7031 a70480e0 2018-06-23 stsp int ch;
7032 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7033 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7034 a70480e0 2018-06-23 stsp
7035 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7036 a70480e0 2018-06-23 stsp switch (ch) {
7037 a70480e0 2018-06-23 stsp case 'c':
7038 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7039 a70480e0 2018-06-23 stsp break;
7040 69069811 2018-08-02 stsp case 'r':
7041 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7042 69069811 2018-08-02 stsp if (repo_path == NULL)
7043 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7044 9ba1d308 2019-10-21 stsp optarg);
7045 69069811 2018-08-02 stsp break;
7046 a70480e0 2018-06-23 stsp default:
7047 17020d27 2019-03-07 stsp usage_blame();
7048 a70480e0 2018-06-23 stsp /* NOTREACHED */
7049 a70480e0 2018-06-23 stsp }
7050 a70480e0 2018-06-23 stsp }
7051 a70480e0 2018-06-23 stsp
7052 a70480e0 2018-06-23 stsp argc -= optind;
7053 a70480e0 2018-06-23 stsp argv += optind;
7054 a70480e0 2018-06-23 stsp
7055 f135c941 2020-02-20 stsp if (argc != 1)
7056 a70480e0 2018-06-23 stsp usage_blame();
7057 6962eb72 2020-02-20 stsp
7058 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7059 7cd52833 2022-06-23 thomas if (error != NULL)
7060 7cd52833 2022-06-23 thomas goto done;
7061 7cd52833 2022-06-23 thomas
7062 69069811 2018-08-02 stsp if (repo_path == NULL) {
7063 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7064 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7065 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7066 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7067 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7068 c156c7a4 2020-12-18 stsp goto done;
7069 f135c941 2020-02-20 stsp if (worktree)
7070 eb41ed75 2019-02-05 stsp repo_path =
7071 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7072 f135c941 2020-02-20 stsp else
7073 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7074 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7075 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7076 c156c7a4 2020-12-18 stsp goto done;
7077 c156c7a4 2020-12-18 stsp }
7078 f135c941 2020-02-20 stsp }
7079 a915003a 2019-02-05 stsp
7080 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7081 c02c541e 2019-03-29 stsp if (error != NULL)
7082 8e94dd5b 2019-01-04 stsp goto done;
7083 69069811 2018-08-02 stsp
7084 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7085 f135c941 2020-02-20 stsp worktree);
7086 c02c541e 2019-03-29 stsp if (error)
7087 92205607 2019-01-04 stsp goto done;
7088 69069811 2018-08-02 stsp
7089 557d3365 2023-04-14 thomas init_curses();
7090 f135c941 2020-02-20 stsp
7091 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7092 51a10b52 2020-12-26 stsp if (error)
7093 51a10b52 2020-12-26 stsp goto done;
7094 51a10b52 2020-12-26 stsp
7095 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7096 eb41ed75 2019-02-05 stsp if (error)
7097 69069811 2018-08-02 stsp goto done;
7098 a70480e0 2018-06-23 stsp
7099 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7100 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7101 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7102 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7103 a70480e0 2018-06-23 stsp if (error != NULL)
7104 66b4983c 2018-06-23 stsp goto done;
7105 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7106 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7107 a70480e0 2018-06-23 stsp } else {
7108 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7109 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7110 a70480e0 2018-06-23 stsp }
7111 a19e88aa 2018-06-23 stsp if (error != NULL)
7112 e1cd8fed 2018-08-01 stsp goto done;
7113 0587e10c 2020-07-23 stsp
7114 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7115 945f9229 2022-04-16 thomas if (error)
7116 945f9229 2022-04-16 thomas goto done;
7117 945f9229 2022-04-16 thomas
7118 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7119 945f9229 2022-04-16 thomas commit, repo);
7120 7cbe629d 2018-08-04 stsp if (error)
7121 7cbe629d 2018-08-04 stsp goto done;
7122 0587e10c 2020-07-23 stsp
7123 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7124 4334634c 2023-04-22 thomas if (view == NULL) {
7125 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7126 4334634c 2023-04-22 thomas goto done;
7127 4334634c 2023-04-22 thomas }
7128 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7129 78756c87 2020-11-24 stsp commit_id, repo);
7130 4334634c 2023-04-22 thomas if (error != NULL) {
7131 4334634c 2023-04-22 thomas if (view->close == NULL)
7132 4334634c 2023-04-22 thomas close_blame_view(view);
7133 4334634c 2023-04-22 thomas view_close(view);
7134 0587e10c 2020-07-23 stsp goto done;
7135 4334634c 2023-04-22 thomas }
7136 12314ad4 2019-08-31 stsp if (worktree) {
7137 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7138 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7139 12314ad4 2019-08-31 stsp worktree = NULL;
7140 12314ad4 2019-08-31 stsp }
7141 557d3365 2023-04-14 thomas error = view_loop(view);
7142 a70480e0 2018-06-23 stsp done:
7143 69069811 2018-08-02 stsp free(repo_path);
7144 f135c941 2020-02-20 stsp free(in_repo_path);
7145 0587e10c 2020-07-23 stsp free(link_target);
7146 69069811 2018-08-02 stsp free(cwd);
7147 a70480e0 2018-06-23 stsp free(commit_id);
7148 945f9229 2022-04-16 thomas if (commit)
7149 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7150 eb41ed75 2019-02-05 stsp if (worktree)
7151 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7152 1d0f4054 2021-06-17 stsp if (repo) {
7153 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7154 1d0f4054 2021-06-17 stsp if (error == NULL)
7155 1d0f4054 2021-06-17 stsp error = close_err;
7156 1d0f4054 2021-06-17 stsp }
7157 7cd52833 2022-06-23 thomas if (pack_fds) {
7158 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7159 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7160 7cd52833 2022-06-23 thomas if (error == NULL)
7161 7cd52833 2022-06-23 thomas error = pack_err;
7162 7cd52833 2022-06-23 thomas }
7163 51a10b52 2020-12-26 stsp tog_free_refs();
7164 a70480e0 2018-06-23 stsp return error;
7165 ffd1d5e5 2018-06-23 stsp }
7166 ffd1d5e5 2018-06-23 stsp
7167 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7168 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7169 ffd1d5e5 2018-06-23 stsp {
7170 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7171 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7172 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7173 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7174 86f4aab9 2022-09-09 thomas char *index = NULL;
7175 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7176 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7177 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7178 ffd1d5e5 2018-06-23 stsp
7179 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7180 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7181 a5d43cac 2022-07-01 thomas --limit; /* border */
7182 ffd1d5e5 2018-06-23 stsp
7183 f7d12f7e 2018-08-01 stsp werase(view->window);
7184 ffd1d5e5 2018-06-23 stsp
7185 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7186 ffd1d5e5 2018-06-23 stsp return NULL;
7187 ffd1d5e5 2018-06-23 stsp
7188 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7189 f91a2b48 2022-06-23 thomas 0, 0);
7190 ffd1d5e5 2018-06-23 stsp if (err)
7191 ffd1d5e5 2018-06-23 stsp return err;
7192 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7193 a3404814 2018-09-02 stsp wstandout(view->window);
7194 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7195 11b20872 2019-11-08 stsp if (tc)
7196 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7197 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7198 86f4aab9 2022-09-09 thomas free(wline);
7199 86f4aab9 2022-09-09 thomas wline = NULL;
7200 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7201 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7202 11b20872 2019-11-08 stsp if (tc)
7203 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7204 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7205 a3404814 2018-09-02 stsp wstandend(view->window);
7206 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7207 86f4aab9 2022-09-09 thomas return NULL;
7208 07dd3ed3 2022-08-06 thomas
7209 6f5f393a 2022-08-12 thomas i += s->selected;
7210 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7211 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7212 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7213 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7214 07dd3ed3 2022-08-06 thomas }
7215 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7216 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7217 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7218 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7219 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7220 86f4aab9 2022-09-09 thomas free(index);
7221 ce52c690 2018-06-23 stsp if (err)
7222 ce52c690 2018-06-23 stsp return err;
7223 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7224 2550e4c3 2018-07-13 stsp free(wline);
7225 2550e4c3 2018-07-13 stsp wline = NULL;
7226 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7227 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7228 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7229 ffd1d5e5 2018-06-23 stsp return NULL;
7230 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7231 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7232 a1eca9bb 2018-06-23 stsp return NULL;
7233 ffd1d5e5 2018-06-23 stsp
7234 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7235 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7236 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7237 0cf4efb1 2018-09-29 stsp if (view->focussed)
7238 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7239 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7240 ffd1d5e5 2018-06-23 stsp }
7241 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7242 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7243 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7244 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7245 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7246 ffd1d5e5 2018-06-23 stsp return NULL;
7247 ffd1d5e5 2018-06-23 stsp n = 1;
7248 ffd1d5e5 2018-06-23 stsp } else {
7249 ffd1d5e5 2018-06-23 stsp n = 0;
7250 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7251 ffd1d5e5 2018-06-23 stsp }
7252 ffd1d5e5 2018-06-23 stsp
7253 c72de8ab 2023-02-03 thomas view->maxx = 0;
7254 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7255 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7256 848d6979 2019-08-12 stsp const char *modestr = "";
7257 56e0773d 2019-11-28 stsp mode_t mode;
7258 1d13200f 2018-07-12 stsp
7259 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7260 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7261 56e0773d 2019-11-28 stsp
7262 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7263 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7264 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7265 1d13200f 2018-07-12 stsp if (err)
7266 638f9024 2019-05-13 stsp return got_error_from_errno(
7267 230a42bd 2019-05-11 jcs "got_object_id_str");
7268 1d13200f 2018-07-12 stsp }
7269 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7270 63c5ca5d 2019-08-24 stsp modestr = "$";
7271 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7272 0d6c6ee3 2020-05-20 stsp int i;
7273 0d6c6ee3 2020-05-20 stsp
7274 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7275 d86d3b18 2020-12-01 naddy te, s->repo);
7276 0d6c6ee3 2020-05-20 stsp if (err) {
7277 0d6c6ee3 2020-05-20 stsp free(id_str);
7278 0d6c6ee3 2020-05-20 stsp return err;
7279 0d6c6ee3 2020-05-20 stsp }
7280 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
7281 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7282 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7283 0d6c6ee3 2020-05-20 stsp }
7284 848d6979 2019-08-12 stsp modestr = "@";
7285 0d6c6ee3 2020-05-20 stsp }
7286 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7287 848d6979 2019-08-12 stsp modestr = "/";
7288 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7289 848d6979 2019-08-12 stsp modestr = "*";
7290 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7291 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7292 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7293 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7294 1d13200f 2018-07-12 stsp free(id_str);
7295 0d6c6ee3 2020-05-20 stsp free(link_target);
7296 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7297 1d13200f 2018-07-12 stsp }
7298 1d13200f 2018-07-12 stsp free(id_str);
7299 0d6c6ee3 2020-05-20 stsp free(link_target);
7300 c72de8ab 2023-02-03 thomas
7301 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7302 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7303 ffd1d5e5 2018-06-23 stsp if (err) {
7304 ffd1d5e5 2018-06-23 stsp free(line);
7305 ffd1d5e5 2018-06-23 stsp break;
7306 ffd1d5e5 2018-06-23 stsp }
7307 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7308 c72de8ab 2023-02-03 thomas free(wline);
7309 c72de8ab 2023-02-03 thomas wline = NULL;
7310 c72de8ab 2023-02-03 thomas
7311 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7312 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7313 c72de8ab 2023-02-03 thomas if (err) {
7314 c72de8ab 2023-02-03 thomas free(line);
7315 c72de8ab 2023-02-03 thomas break;
7316 c72de8ab 2023-02-03 thomas }
7317 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7318 0cf4efb1 2018-09-29 stsp if (view->focussed)
7319 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7320 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7321 ffd1d5e5 2018-06-23 stsp }
7322 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7323 f26dddb7 2019-11-08 stsp if (tc)
7324 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7325 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7326 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7327 f26dddb7 2019-11-08 stsp if (tc)
7328 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7329 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7330 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7331 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7332 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7333 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7334 ffd1d5e5 2018-06-23 stsp free(line);
7335 2550e4c3 2018-07-13 stsp free(wline);
7336 2550e4c3 2018-07-13 stsp wline = NULL;
7337 ffd1d5e5 2018-06-23 stsp n++;
7338 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7339 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7340 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7341 ffd1d5e5 2018-06-23 stsp break;
7342 ffd1d5e5 2018-06-23 stsp }
7343 ffd1d5e5 2018-06-23 stsp
7344 ffd1d5e5 2018-06-23 stsp return err;
7345 ffd1d5e5 2018-06-23 stsp }
7346 ffd1d5e5 2018-06-23 stsp
7347 ffd1d5e5 2018-06-23 stsp static void
7348 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7349 ffd1d5e5 2018-06-23 stsp {
7350 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7351 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7352 fa86c4bf 2020-11-29 stsp int i = 0;
7353 ffd1d5e5 2018-06-23 stsp
7354 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7355 ffd1d5e5 2018-06-23 stsp return;
7356 ffd1d5e5 2018-06-23 stsp
7357 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7358 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7359 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7360 fa86c4bf 2020-11-29 stsp if (!isroot)
7361 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7362 fa86c4bf 2020-11-29 stsp break;
7363 fa86c4bf 2020-11-29 stsp }
7364 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7365 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7366 ffd1d5e5 2018-06-23 stsp }
7367 ffd1d5e5 2018-06-23 stsp }
7368 ffd1d5e5 2018-06-23 stsp
7369 a5d43cac 2022-07-01 thomas static const struct got_error *
7370 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7371 ffd1d5e5 2018-06-23 stsp {
7372 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7373 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7374 ffd1d5e5 2018-06-23 stsp int n = 0;
7375 ffd1d5e5 2018-06-23 stsp
7376 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7377 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7378 694d3271 2020-12-01 naddy s->first_displayed_entry);
7379 694d3271 2020-12-01 naddy else
7380 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7381 56e0773d 2019-11-28 stsp
7382 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7383 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7384 07dd3ed3 2022-08-06 thomas if (last) {
7385 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7386 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7387 07dd3ed3 2022-08-06 thomas }
7388 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7389 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7390 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7391 768394f3 2019-01-24 stsp }
7392 ffd1d5e5 2018-06-23 stsp }
7393 a5d43cac 2022-07-01 thomas
7394 a5d43cac 2022-07-01 thomas return NULL;
7395 ffd1d5e5 2018-06-23 stsp }
7396 ffd1d5e5 2018-06-23 stsp
7397 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7398 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7399 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7400 ffd1d5e5 2018-06-23 stsp {
7401 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7402 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7403 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7404 ffd1d5e5 2018-06-23 stsp
7405 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7406 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7407 56e0773d 2019-11-28 stsp + 1 /* slash */;
7408 ce52c690 2018-06-23 stsp if (te)
7409 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7410 ce52c690 2018-06-23 stsp
7411 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7412 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7413 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7414 ffd1d5e5 2018-06-23 stsp
7415 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7416 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7417 d9765a41 2018-06-23 stsp while (pt) {
7418 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7419 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7420 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7421 cb2ebc8a 2018-06-23 stsp goto done;
7422 cb2ebc8a 2018-06-23 stsp }
7423 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7424 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7425 cb2ebc8a 2018-06-23 stsp goto done;
7426 cb2ebc8a 2018-06-23 stsp }
7427 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7428 ffd1d5e5 2018-06-23 stsp }
7429 ce52c690 2018-06-23 stsp if (te) {
7430 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7431 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7432 ce52c690 2018-06-23 stsp goto done;
7433 ce52c690 2018-06-23 stsp }
7434 cb2ebc8a 2018-06-23 stsp }
7435 ce52c690 2018-06-23 stsp done:
7436 ce52c690 2018-06-23 stsp if (err) {
7437 ce52c690 2018-06-23 stsp free(*path);
7438 ce52c690 2018-06-23 stsp *path = NULL;
7439 ce52c690 2018-06-23 stsp }
7440 ce52c690 2018-06-23 stsp return err;
7441 ce52c690 2018-06-23 stsp }
7442 ce52c690 2018-06-23 stsp
7443 ce52c690 2018-06-23 stsp static const struct got_error *
7444 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7445 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7446 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7447 ce52c690 2018-06-23 stsp {
7448 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7449 ce52c690 2018-06-23 stsp char *path;
7450 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7451 a0de39f3 2019-08-09 stsp
7452 a0de39f3 2019-08-09 stsp *new_view = NULL;
7453 69efd4c4 2018-07-18 stsp
7454 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7455 ce52c690 2018-06-23 stsp if (err)
7456 ce52c690 2018-06-23 stsp return err;
7457 ffd1d5e5 2018-06-23 stsp
7458 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7459 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7460 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7461 83ce39e3 2019-08-12 stsp goto done;
7462 83ce39e3 2019-08-12 stsp }
7463 cdf1ee82 2018-08-01 stsp
7464 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7465 e5a0f69f 2018-08-18 stsp if (err) {
7466 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7467 fc06ba56 2019-08-22 stsp err = NULL;
7468 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7469 e5a0f69f 2018-08-18 stsp } else
7470 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7471 83ce39e3 2019-08-12 stsp done:
7472 83ce39e3 2019-08-12 stsp free(path);
7473 69efd4c4 2018-07-18 stsp return err;
7474 69efd4c4 2018-07-18 stsp }
7475 69efd4c4 2018-07-18 stsp
7476 69efd4c4 2018-07-18 stsp static const struct got_error *
7477 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7478 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7479 69efd4c4 2018-07-18 stsp {
7480 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7481 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7482 69efd4c4 2018-07-18 stsp char *path;
7483 a0de39f3 2019-08-09 stsp
7484 a0de39f3 2019-08-09 stsp *new_view = NULL;
7485 69efd4c4 2018-07-18 stsp
7486 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7487 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7488 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7489 e5a0f69f 2018-08-18 stsp
7490 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7491 69efd4c4 2018-07-18 stsp if (err)
7492 69efd4c4 2018-07-18 stsp return err;
7493 69efd4c4 2018-07-18 stsp
7494 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7495 4e97c21c 2020-12-06 stsp path, 0);
7496 ba4f502b 2018-08-04 stsp if (err)
7497 e5a0f69f 2018-08-18 stsp view_close(log_view);
7498 e5a0f69f 2018-08-18 stsp else
7499 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7500 cb2ebc8a 2018-06-23 stsp free(path);
7501 cb2ebc8a 2018-06-23 stsp return err;
7502 ffd1d5e5 2018-06-23 stsp }
7503 ffd1d5e5 2018-06-23 stsp
7504 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7505 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7506 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7507 ffd1d5e5 2018-06-23 stsp {
7508 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7509 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7510 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7511 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7512 ffd1d5e5 2018-06-23 stsp
7513 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7514 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7515 bc573f3b 2021-07-10 stsp
7516 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7517 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7518 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7519 51b7e1c3 2023-04-14 thomas goto done;
7520 51b7e1c3 2023-04-14 thomas }
7521 ffd1d5e5 2018-06-23 stsp
7522 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7523 bc573f3b 2021-07-10 stsp if (err)
7524 bc573f3b 2021-07-10 stsp goto done;
7525 bc573f3b 2021-07-10 stsp
7526 bc573f3b 2021-07-10 stsp /*
7527 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7528 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7529 bc573f3b 2021-07-10 stsp * closed on demand.
7530 bc573f3b 2021-07-10 stsp */
7531 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7532 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7533 bc573f3b 2021-07-10 stsp if (err)
7534 bc573f3b 2021-07-10 stsp goto done;
7535 bc573f3b 2021-07-10 stsp s->tree = s->root;
7536 bc573f3b 2021-07-10 stsp
7537 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7538 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7539 ffd1d5e5 2018-06-23 stsp goto done;
7540 ffd1d5e5 2018-06-23 stsp
7541 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7542 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7543 ffd1d5e5 2018-06-23 stsp goto done;
7544 ffd1d5e5 2018-06-23 stsp }
7545 ffd1d5e5 2018-06-23 stsp
7546 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7547 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7548 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7549 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7550 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7551 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7552 9cd7cbd1 2020-12-07 stsp goto done;
7553 9cd7cbd1 2020-12-07 stsp }
7554 9cd7cbd1 2020-12-07 stsp }
7555 fb2756b9 2018-08-04 stsp s->repo = repo;
7556 c0b01bdb 2019-11-08 stsp
7557 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7558 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7559 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7560 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7561 c0b01bdb 2019-11-08 stsp if (err)
7562 c0b01bdb 2019-11-08 stsp goto done;
7563 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7565 bc573f3b 2021-07-10 stsp if (err)
7566 c0b01bdb 2019-11-08 stsp goto done;
7567 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7568 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7569 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7570 bc573f3b 2021-07-10 stsp if (err)
7571 c0b01bdb 2019-11-08 stsp goto done;
7572 e5a0f69f 2018-08-18 stsp
7573 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7574 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7575 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7576 bc573f3b 2021-07-10 stsp if (err)
7577 11b20872 2019-11-08 stsp goto done;
7578 11b20872 2019-11-08 stsp
7579 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7580 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7581 bc573f3b 2021-07-10 stsp if (err)
7582 c0b01bdb 2019-11-08 stsp goto done;
7583 c0b01bdb 2019-11-08 stsp }
7584 c0b01bdb 2019-11-08 stsp
7585 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7586 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7587 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7588 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7589 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7590 ad80ab7b 2018-08-04 stsp done:
7591 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7592 bc573f3b 2021-07-10 stsp if (commit)
7593 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7594 51b7e1c3 2023-04-14 thomas if (err) {
7595 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7596 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7597 51b7e1c3 2023-04-14 thomas view_close(view);
7598 51b7e1c3 2023-04-14 thomas }
7599 ad80ab7b 2018-08-04 stsp return err;
7600 ad80ab7b 2018-08-04 stsp }
7601 ad80ab7b 2018-08-04 stsp
7602 e5a0f69f 2018-08-18 stsp static const struct got_error *
7603 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7604 ad80ab7b 2018-08-04 stsp {
7605 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7606 ad80ab7b 2018-08-04 stsp
7607 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7608 fb2756b9 2018-08-04 stsp free(s->tree_label);
7609 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7610 6484ec90 2018-09-29 stsp free(s->commit_id);
7611 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7612 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7613 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7614 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7615 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7616 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7617 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7618 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7619 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7620 ad80ab7b 2018-08-04 stsp free(parent);
7621 ad80ab7b 2018-08-04 stsp
7622 ad80ab7b 2018-08-04 stsp }
7623 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7624 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7625 bc573f3b 2021-07-10 stsp if (s->root)
7626 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7627 7c32bd05 2019-06-22 stsp return NULL;
7628 7c32bd05 2019-06-22 stsp }
7629 7c32bd05 2019-06-22 stsp
7630 7c32bd05 2019-06-22 stsp static const struct got_error *
7631 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7632 7c32bd05 2019-06-22 stsp {
7633 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7634 7c32bd05 2019-06-22 stsp
7635 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7636 7c32bd05 2019-06-22 stsp return NULL;
7637 7c32bd05 2019-06-22 stsp }
7638 7c32bd05 2019-06-22 stsp
7639 7c32bd05 2019-06-22 stsp static int
7640 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7641 7c32bd05 2019-06-22 stsp {
7642 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7643 7c32bd05 2019-06-22 stsp
7644 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7645 56e0773d 2019-11-28 stsp 0) == 0;
7646 7c32bd05 2019-06-22 stsp }
7647 7c32bd05 2019-06-22 stsp
7648 7c32bd05 2019-06-22 stsp static const struct got_error *
7649 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7650 7c32bd05 2019-06-22 stsp {
7651 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7652 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7653 7c32bd05 2019-06-22 stsp
7654 7c32bd05 2019-06-22 stsp if (!view->searching) {
7655 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7656 7c32bd05 2019-06-22 stsp return NULL;
7657 7c32bd05 2019-06-22 stsp }
7658 7c32bd05 2019-06-22 stsp
7659 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7660 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7661 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7662 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7663 56e0773d 2019-11-28 stsp s->selected_entry);
7664 7c32bd05 2019-06-22 stsp else
7665 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7666 56e0773d 2019-11-28 stsp } else {
7667 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7668 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7669 56e0773d 2019-11-28 stsp else
7670 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7671 56e0773d 2019-11-28 stsp s->selected_entry);
7672 7c32bd05 2019-06-22 stsp }
7673 7c32bd05 2019-06-22 stsp } else {
7674 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7675 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7676 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7677 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7678 56e0773d 2019-11-28 stsp else
7679 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7680 7c32bd05 2019-06-22 stsp }
7681 7c32bd05 2019-06-22 stsp
7682 7c32bd05 2019-06-22 stsp while (1) {
7683 56e0773d 2019-11-28 stsp if (te == NULL) {
7684 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7685 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7686 ac66afb8 2019-06-24 stsp return NULL;
7687 ac66afb8 2019-06-24 stsp }
7688 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7689 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7690 56e0773d 2019-11-28 stsp else
7691 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7692 7c32bd05 2019-06-22 stsp }
7693 7c32bd05 2019-06-22 stsp
7694 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7695 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7696 56e0773d 2019-11-28 stsp s->matched_entry = te;
7697 7c32bd05 2019-06-22 stsp break;
7698 7c32bd05 2019-06-22 stsp }
7699 7c32bd05 2019-06-22 stsp
7700 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7701 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7702 56e0773d 2019-11-28 stsp else
7703 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7704 7c32bd05 2019-06-22 stsp }
7705 e5a0f69f 2018-08-18 stsp
7706 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7707 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7708 7c32bd05 2019-06-22 stsp s->selected = 0;
7709 7c32bd05 2019-06-22 stsp }
7710 7c32bd05 2019-06-22 stsp
7711 e5a0f69f 2018-08-18 stsp return NULL;
7712 ad80ab7b 2018-08-04 stsp }
7713 ad80ab7b 2018-08-04 stsp
7714 ad80ab7b 2018-08-04 stsp static const struct got_error *
7715 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7716 ad80ab7b 2018-08-04 stsp {
7717 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7718 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7719 e5a0f69f 2018-08-18 stsp char *parent_path;
7720 ad80ab7b 2018-08-04 stsp
7721 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7722 e5a0f69f 2018-08-18 stsp if (err)
7723 e5a0f69f 2018-08-18 stsp return err;
7724 ffd1d5e5 2018-06-23 stsp
7725 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7726 e5a0f69f 2018-08-18 stsp free(parent_path);
7727 669b5ffa 2018-10-07 stsp
7728 a5d43cac 2022-07-01 thomas view_border(view);
7729 e5a0f69f 2018-08-18 stsp return err;
7730 07dd3ed3 2022-08-06 thomas }
7731 07dd3ed3 2022-08-06 thomas
7732 07dd3ed3 2022-08-06 thomas static const struct got_error *
7733 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7734 07dd3ed3 2022-08-06 thomas {
7735 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7736 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7737 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7738 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7739 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7740 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7741 07dd3ed3 2022-08-06 thomas
7742 07dd3ed3 2022-08-06 thomas g = view->gline;
7743 07dd3ed3 2022-08-06 thomas view->gline = 0;
7744 07dd3ed3 2022-08-06 thomas
7745 07dd3ed3 2022-08-06 thomas if (g == 0)
7746 07dd3ed3 2022-08-06 thomas g = 1;
7747 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7748 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7749 07dd3ed3 2022-08-06 thomas
7750 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7751 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7752 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7753 07dd3ed3 2022-08-06 thomas
7754 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7755 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7756 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7757 07dd3ed3 2022-08-06 thomas }
7758 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7759 07dd3ed3 2022-08-06 thomas last += off;
7760 07dd3ed3 2022-08-06 thomas
7761 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7762 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7763 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7764 07dd3ed3 2022-08-06 thomas }
7765 07dd3ed3 2022-08-06 thomas
7766 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7767 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7768 07dd3ed3 2022-08-06 thomas i += off;
7769 07dd3ed3 2022-08-06 thomas }
7770 07dd3ed3 2022-08-06 thomas
7771 07dd3ed3 2022-08-06 thomas if (i < g) {
7772 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7773 07dd3ed3 2022-08-06 thomas if (err)
7774 07dd3ed3 2022-08-06 thomas return err;
7775 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7776 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7777 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7778 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7779 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7780 07dd3ed3 2022-08-06 thomas first += off;
7781 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7782 07dd3ed3 2022-08-06 thomas }
7783 07dd3ed3 2022-08-06 thomas } else if (i > g)
7784 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7785 07dd3ed3 2022-08-06 thomas
7786 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7787 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7788 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7789 07dd3ed3 2022-08-06 thomas
7790 07dd3ed3 2022-08-06 thomas return NULL;
7791 e5a0f69f 2018-08-18 stsp }
7792 ce52c690 2018-06-23 stsp
7793 e5a0f69f 2018-08-18 stsp static const struct got_error *
7794 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7795 e5a0f69f 2018-08-18 stsp {
7796 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7797 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7798 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7799 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7800 ffd1d5e5 2018-06-23 stsp
7801 07dd3ed3 2022-08-06 thomas if (view->gline)
7802 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7803 07dd3ed3 2022-08-06 thomas
7804 e5a0f69f 2018-08-18 stsp switch (ch) {
7805 c72de8ab 2023-02-03 thomas case '0':
7806 c72de8ab 2023-02-03 thomas case '$':
7807 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7808 c72de8ab 2023-02-03 thomas case 'l':
7809 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7810 c72de8ab 2023-02-03 thomas case 'h':
7811 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7812 c72de8ab 2023-02-03 thomas break;
7813 1e37a5c2 2019-05-12 jcs case 'i':
7814 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7815 07b0611c 2022-06-23 thomas view->count = 0;
7816 1e37a5c2 2019-05-12 jcs break;
7817 1be4947a 2022-07-22 thomas case 'L':
7818 07b0611c 2022-06-23 thomas view->count = 0;
7819 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7820 ffd1d5e5 2018-06-23 stsp break;
7821 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7822 152c1c93 2020-11-29 stsp break;
7823 1be4947a 2022-07-22 thomas case 'R':
7824 07b0611c 2022-06-23 thomas view->count = 0;
7825 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7826 e4526bf5 2021-09-03 naddy break;
7827 e4526bf5 2021-09-03 naddy case 'g':
7828 aa7a1117 2023-01-09 thomas case '=':
7829 e4526bf5 2021-09-03 naddy case KEY_HOME:
7830 e4526bf5 2021-09-03 naddy s->selected = 0;
7831 07b0611c 2022-06-23 thomas view->count = 0;
7832 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7833 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7834 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7835 e4526bf5 2021-09-03 naddy else
7836 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7837 1e37a5c2 2019-05-12 jcs break;
7838 e4526bf5 2021-09-03 naddy case 'G':
7839 aa7a1117 2023-01-09 thomas case '*':
7840 a5d43cac 2022-07-01 thomas case KEY_END: {
7841 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7842 a5d43cac 2022-07-01 thomas
7843 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7844 a5d43cac 2022-07-01 thomas --eos; /* border */
7845 e4526bf5 2021-09-03 naddy s->selected = 0;
7846 07b0611c 2022-06-23 thomas view->count = 0;
7847 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7848 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7849 e4526bf5 2021-09-03 naddy if (te == NULL) {
7850 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7851 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7852 e4526bf5 2021-09-03 naddy n++;
7853 e4526bf5 2021-09-03 naddy }
7854 e4526bf5 2021-09-03 naddy break;
7855 e4526bf5 2021-09-03 naddy }
7856 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7857 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7858 e4526bf5 2021-09-03 naddy }
7859 e4526bf5 2021-09-03 naddy if (n > 0)
7860 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7861 e4526bf5 2021-09-03 naddy break;
7862 a5d43cac 2022-07-01 thomas }
7863 1e37a5c2 2019-05-12 jcs case 'k':
7864 1e37a5c2 2019-05-12 jcs case KEY_UP:
7865 f7140bf5 2021-10-17 thomas case CTRL('p'):
7866 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7867 1e37a5c2 2019-05-12 jcs s->selected--;
7868 fa86c4bf 2020-11-29 stsp break;
7869 1e37a5c2 2019-05-12 jcs }
7870 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7871 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7872 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7873 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7874 07b0611c 2022-06-23 thomas view->count = 0;
7875 1e37a5c2 2019-05-12 jcs break;
7876 70f17a53 2022-06-13 thomas case CTRL('u'):
7877 23427b14 2022-06-23 thomas case 'u':
7878 70f17a53 2022-06-13 thomas nscroll /= 2;
7879 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7880 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7881 ea025d1d 2020-02-22 naddy case CTRL('b'):
7882 1c5e5faa 2022-06-23 thomas case 'b':
7883 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7884 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7885 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7886 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7887 fa86c4bf 2020-11-29 stsp } else {
7888 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7889 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7890 fa86c4bf 2020-11-29 stsp }
7891 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7892 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7893 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7894 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7895 07b0611c 2022-06-23 thomas view->count = 0;
7896 1e37a5c2 2019-05-12 jcs break;
7897 1e37a5c2 2019-05-12 jcs case 'j':
7898 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7899 f7140bf5 2021-10-17 thomas case CTRL('n'):
7900 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7901 1e37a5c2 2019-05-12 jcs s->selected++;
7902 1e37a5c2 2019-05-12 jcs break;
7903 1e37a5c2 2019-05-12 jcs }
7904 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7905 07b0611c 2022-06-23 thomas == NULL) {
7906 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7907 07b0611c 2022-06-23 thomas view->count = 0;
7908 1e37a5c2 2019-05-12 jcs break;
7909 07b0611c 2022-06-23 thomas }
7910 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7911 1e37a5c2 2019-05-12 jcs break;
7912 70f17a53 2022-06-13 thomas case CTRL('d'):
7913 23427b14 2022-06-23 thomas case 'd':
7914 70f17a53 2022-06-13 thomas nscroll /= 2;
7915 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7916 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7917 ea025d1d 2020-02-22 naddy case CTRL('f'):
7918 1c5e5faa 2022-06-23 thomas case 'f':
7919 4c2d69cb 2022-06-23 thomas case ' ':
7920 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7921 1e37a5c2 2019-05-12 jcs == NULL) {
7922 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7923 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7924 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7925 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7926 07b0611c 2022-06-23 thomas else
7927 07b0611c 2022-06-23 thomas view->count = 0;
7928 1e37a5c2 2019-05-12 jcs break;
7929 1e37a5c2 2019-05-12 jcs }
7930 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7931 1e37a5c2 2019-05-12 jcs break;
7932 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7933 1e37a5c2 2019-05-12 jcs case '\r':
7934 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7935 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7936 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7937 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7938 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7939 07b0611c 2022-06-23 thomas view->count = 0;
7940 1e37a5c2 2019-05-12 jcs break;
7941 07b0611c 2022-06-23 thomas }
7942 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7943 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7944 1e37a5c2 2019-05-12 jcs entry);
7945 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7946 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7947 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7948 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7949 1e37a5c2 2019-05-12 jcs s->selected_entry =
7950 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7951 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7952 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7953 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7954 a5d43cac 2022-07-01 thomas if (err)
7955 a5d43cac 2022-07-01 thomas break;
7956 a5d43cac 2022-07-01 thomas }
7957 1e37a5c2 2019-05-12 jcs free(parent);
7958 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7959 56e0773d 2019-11-28 stsp s->selected_entry))) {
7960 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7961 07b0611c 2022-06-23 thomas view->count = 0;
7962 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7963 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7964 1e37a5c2 2019-05-12 jcs if (err)
7965 1e37a5c2 2019-05-12 jcs break;
7966 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7967 941e9f74 2019-05-21 stsp if (err) {
7968 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7969 1e37a5c2 2019-05-12 jcs break;
7970 1e37a5c2 2019-05-12 jcs }
7971 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7972 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7973 1e37a5c2 2019-05-12 jcs break;
7974 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7975 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7976 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7977 07b0611c 2022-06-23 thomas view->count = 0;
7978 1e37a5c2 2019-05-12 jcs break;
7979 1e37a5c2 2019-05-12 jcs default:
7980 07b0611c 2022-06-23 thomas view->count = 0;
7981 1e37a5c2 2019-05-12 jcs break;
7982 ffd1d5e5 2018-06-23 stsp }
7983 e5a0f69f 2018-08-18 stsp
7984 ffd1d5e5 2018-06-23 stsp return err;
7985 9f7d7167 2018-04-29 stsp }
7986 9f7d7167 2018-04-29 stsp
7987 ffd1d5e5 2018-06-23 stsp __dead static void
7988 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7989 ffd1d5e5 2018-06-23 stsp {
7990 ffd1d5e5 2018-06-23 stsp endwin();
7991 91198554 2022-06-23 thomas fprintf(stderr,
7992 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7993 ffd1d5e5 2018-06-23 stsp getprogname());
7994 ffd1d5e5 2018-06-23 stsp exit(1);
7995 ffd1d5e5 2018-06-23 stsp }
7996 ffd1d5e5 2018-06-23 stsp
7997 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7998 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7999 ffd1d5e5 2018-06-23 stsp {
8000 1d98034b 2023-04-14 thomas const struct got_error *error;
8001 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8002 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8003 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8004 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8005 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8006 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8007 4e97c21c 2020-12-06 stsp char *label = NULL;
8008 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8009 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8010 ffd1d5e5 2018-06-23 stsp int ch;
8011 5221c383 2018-08-01 stsp struct tog_view *view;
8012 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8013 70ac5f84 2019-03-28 stsp
8014 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8015 ffd1d5e5 2018-06-23 stsp switch (ch) {
8016 ffd1d5e5 2018-06-23 stsp case 'c':
8017 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8018 74283ab8 2020-02-07 stsp break;
8019 74283ab8 2020-02-07 stsp case 'r':
8020 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8021 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8022 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8023 74283ab8 2020-02-07 stsp optarg);
8024 ffd1d5e5 2018-06-23 stsp break;
8025 ffd1d5e5 2018-06-23 stsp default:
8026 e99e2d15 2020-11-24 naddy usage_tree();
8027 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8028 ffd1d5e5 2018-06-23 stsp }
8029 ffd1d5e5 2018-06-23 stsp }
8030 ffd1d5e5 2018-06-23 stsp
8031 ffd1d5e5 2018-06-23 stsp argc -= optind;
8032 ffd1d5e5 2018-06-23 stsp argv += optind;
8033 ffd1d5e5 2018-06-23 stsp
8034 55cccc34 2020-02-20 stsp if (argc > 1)
8035 e99e2d15 2020-11-24 naddy usage_tree();
8036 7cd52833 2022-06-23 thomas
8037 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8038 7cd52833 2022-06-23 thomas if (error != NULL)
8039 7cd52833 2022-06-23 thomas goto done;
8040 74283ab8 2020-02-07 stsp
8041 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8042 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8043 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8044 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8045 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8046 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8047 c156c7a4 2020-12-18 stsp goto done;
8048 55cccc34 2020-02-20 stsp if (worktree)
8049 52185f70 2019-02-05 stsp repo_path =
8050 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8051 55cccc34 2020-02-20 stsp else
8052 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8053 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8054 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8055 c156c7a4 2020-12-18 stsp goto done;
8056 c156c7a4 2020-12-18 stsp }
8057 55cccc34 2020-02-20 stsp }
8058 a915003a 2019-02-05 stsp
8059 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8060 c02c541e 2019-03-29 stsp if (error != NULL)
8061 52185f70 2019-02-05 stsp goto done;
8062 d188b9a6 2019-01-04 stsp
8063 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8064 55cccc34 2020-02-20 stsp repo, worktree);
8065 55cccc34 2020-02-20 stsp if (error)
8066 55cccc34 2020-02-20 stsp goto done;
8067 55cccc34 2020-02-20 stsp
8068 557d3365 2023-04-14 thomas init_curses();
8069 55cccc34 2020-02-20 stsp
8070 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8071 c02c541e 2019-03-29 stsp if (error)
8072 52185f70 2019-02-05 stsp goto done;
8073 ffd1d5e5 2018-06-23 stsp
8074 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8075 51a10b52 2020-12-26 stsp if (error)
8076 51a10b52 2020-12-26 stsp goto done;
8077 51a10b52 2020-12-26 stsp
8078 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8079 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8080 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8081 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8082 4e97c21c 2020-12-06 stsp if (error)
8083 4e97c21c 2020-12-06 stsp goto done;
8084 4e97c21c 2020-12-06 stsp head_ref_name = label;
8085 4e97c21c 2020-12-06 stsp } else {
8086 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8087 4e97c21c 2020-12-06 stsp if (error == NULL)
8088 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8089 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8090 4e97c21c 2020-12-06 stsp goto done;
8091 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8092 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8093 4e97c21c 2020-12-06 stsp if (error)
8094 4e97c21c 2020-12-06 stsp goto done;
8095 4e97c21c 2020-12-06 stsp }
8096 ffd1d5e5 2018-06-23 stsp
8097 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8098 945f9229 2022-04-16 thomas if (error)
8099 945f9229 2022-04-16 thomas goto done;
8100 945f9229 2022-04-16 thomas
8101 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8102 5221c383 2018-08-01 stsp if (view == NULL) {
8103 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8104 5221c383 2018-08-01 stsp goto done;
8105 5221c383 2018-08-01 stsp }
8106 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8107 ad80ab7b 2018-08-04 stsp if (error)
8108 ad80ab7b 2018-08-04 stsp goto done;
8109 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8110 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8111 d91faf3b 2020-12-01 naddy in_repo_path);
8112 55cccc34 2020-02-20 stsp if (error)
8113 55cccc34 2020-02-20 stsp goto done;
8114 55cccc34 2020-02-20 stsp }
8115 55cccc34 2020-02-20 stsp
8116 55cccc34 2020-02-20 stsp if (worktree) {
8117 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8118 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8119 55cccc34 2020-02-20 stsp worktree = NULL;
8120 55cccc34 2020-02-20 stsp }
8121 557d3365 2023-04-14 thomas error = view_loop(view);
8122 ffd1d5e5 2018-06-23 stsp done:
8123 52185f70 2019-02-05 stsp free(repo_path);
8124 e4a0e26d 2020-02-20 stsp free(cwd);
8125 ffd1d5e5 2018-06-23 stsp free(commit_id);
8126 4e97c21c 2020-12-06 stsp free(label);
8127 486cd271 2020-12-06 stsp if (ref)
8128 486cd271 2020-12-06 stsp got_ref_close(ref);
8129 1d0f4054 2021-06-17 stsp if (repo) {
8130 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8131 1d0f4054 2021-06-17 stsp if (error == NULL)
8132 1d0f4054 2021-06-17 stsp error = close_err;
8133 1d0f4054 2021-06-17 stsp }
8134 7cd52833 2022-06-23 thomas if (pack_fds) {
8135 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8136 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8137 7cd52833 2022-06-23 thomas if (error == NULL)
8138 7cd52833 2022-06-23 thomas error = pack_err;
8139 b85a3496 2023-04-14 thomas }
8140 51a10b52 2020-12-26 stsp tog_free_refs();
8141 ffd1d5e5 2018-06-23 stsp return error;
8142 6458efa5 2020-11-24 stsp }
8143 6458efa5 2020-11-24 stsp
8144 6458efa5 2020-11-24 stsp static const struct got_error *
8145 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8146 6458efa5 2020-11-24 stsp {
8147 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8148 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8149 6458efa5 2020-11-24 stsp
8150 6458efa5 2020-11-24 stsp s->nrefs = 0;
8151 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8152 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8153 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8154 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8155 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8156 6458efa5 2020-11-24 stsp continue;
8157 6458efa5 2020-11-24 stsp
8158 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8159 6458efa5 2020-11-24 stsp if (re == NULL)
8160 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8161 6458efa5 2020-11-24 stsp
8162 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8163 8924d611 2020-12-26 stsp if (re->ref == NULL)
8164 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8165 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8166 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8167 6458efa5 2020-11-24 stsp }
8168 6458efa5 2020-11-24 stsp
8169 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8170 6458efa5 2020-11-24 stsp return NULL;
8171 6458efa5 2020-11-24 stsp }
8172 6458efa5 2020-11-24 stsp
8173 ef20f542 2022-06-26 thomas static void
8174 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8175 6458efa5 2020-11-24 stsp {
8176 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8177 6458efa5 2020-11-24 stsp
8178 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8179 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8180 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8181 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8182 6458efa5 2020-11-24 stsp free(re);
8183 6458efa5 2020-11-24 stsp }
8184 6458efa5 2020-11-24 stsp }
8185 6458efa5 2020-11-24 stsp
8186 6458efa5 2020-11-24 stsp static const struct got_error *
8187 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8188 6458efa5 2020-11-24 stsp {
8189 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8190 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8191 6458efa5 2020-11-24 stsp
8192 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8193 6458efa5 2020-11-24 stsp s->repo = repo;
8194 6458efa5 2020-11-24 stsp
8195 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8196 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8197 6458efa5 2020-11-24 stsp
8198 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8199 6458efa5 2020-11-24 stsp if (err)
8200 51b7e1c3 2023-04-14 thomas goto done;
8201 34ba6917 2020-11-30 stsp
8202 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8203 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8204 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8205 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8206 6458efa5 2020-11-24 stsp if (err)
8207 6458efa5 2020-11-24 stsp goto done;
8208 6458efa5 2020-11-24 stsp
8209 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8210 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8211 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8212 6458efa5 2020-11-24 stsp if (err)
8213 6458efa5 2020-11-24 stsp goto done;
8214 6458efa5 2020-11-24 stsp
8215 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8216 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8217 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8218 2183bbf6 2022-01-23 thomas if (err)
8219 2183bbf6 2022-01-23 thomas goto done;
8220 2183bbf6 2022-01-23 thomas
8221 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8222 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8223 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8224 6458efa5 2020-11-24 stsp if (err)
8225 6458efa5 2020-11-24 stsp goto done;
8226 6458efa5 2020-11-24 stsp }
8227 6458efa5 2020-11-24 stsp
8228 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8229 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8230 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8231 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8232 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8233 6458efa5 2020-11-24 stsp done:
8234 51b7e1c3 2023-04-14 thomas if (err) {
8235 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8236 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8237 51b7e1c3 2023-04-14 thomas view_close(view);
8238 51b7e1c3 2023-04-14 thomas }
8239 6458efa5 2020-11-24 stsp return err;
8240 6458efa5 2020-11-24 stsp }
8241 6458efa5 2020-11-24 stsp
8242 6458efa5 2020-11-24 stsp static const struct got_error *
8243 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8244 6458efa5 2020-11-24 stsp {
8245 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8246 6458efa5 2020-11-24 stsp
8247 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8248 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8249 6458efa5 2020-11-24 stsp
8250 6458efa5 2020-11-24 stsp return NULL;
8251 9f7d7167 2018-04-29 stsp }
8252 ce5b7c56 2019-07-09 stsp
8253 6458efa5 2020-11-24 stsp static const struct got_error *
8254 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8255 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8256 6458efa5 2020-11-24 stsp {
8257 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8258 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8259 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8260 6458efa5 2020-11-24 stsp int obj_type;
8261 6458efa5 2020-11-24 stsp
8262 c42c9805 2020-11-24 stsp *commit_id = NULL;
8263 6458efa5 2020-11-24 stsp
8264 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8265 6458efa5 2020-11-24 stsp if (err)
8266 6458efa5 2020-11-24 stsp return err;
8267 6458efa5 2020-11-24 stsp
8268 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8269 6458efa5 2020-11-24 stsp if (err)
8270 6458efa5 2020-11-24 stsp goto done;
8271 6458efa5 2020-11-24 stsp
8272 6458efa5 2020-11-24 stsp switch (obj_type) {
8273 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8274 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8275 6458efa5 2020-11-24 stsp break;
8276 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8277 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8278 6458efa5 2020-11-24 stsp if (err)
8279 6458efa5 2020-11-24 stsp goto done;
8280 c42c9805 2020-11-24 stsp free(obj_id);
8281 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8282 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8283 c42c9805 2020-11-24 stsp if (err)
8284 6458efa5 2020-11-24 stsp goto done;
8285 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8286 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8287 c42c9805 2020-11-24 stsp goto done;
8288 c42c9805 2020-11-24 stsp }
8289 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8290 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8291 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8292 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8293 c42c9805 2020-11-24 stsp goto done;
8294 c42c9805 2020-11-24 stsp }
8295 6458efa5 2020-11-24 stsp break;
8296 6458efa5 2020-11-24 stsp default:
8297 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8298 c42c9805 2020-11-24 stsp break;
8299 6458efa5 2020-11-24 stsp }
8300 6458efa5 2020-11-24 stsp
8301 c42c9805 2020-11-24 stsp done:
8302 c42c9805 2020-11-24 stsp if (tag)
8303 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8304 c42c9805 2020-11-24 stsp if (err) {
8305 c42c9805 2020-11-24 stsp free(*commit_id);
8306 c42c9805 2020-11-24 stsp *commit_id = NULL;
8307 c42c9805 2020-11-24 stsp }
8308 c42c9805 2020-11-24 stsp return err;
8309 c42c9805 2020-11-24 stsp }
8310 c42c9805 2020-11-24 stsp
8311 c42c9805 2020-11-24 stsp static const struct got_error *
8312 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8313 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8314 c42c9805 2020-11-24 stsp {
8315 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8316 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8317 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8318 c42c9805 2020-11-24 stsp
8319 c42c9805 2020-11-24 stsp *new_view = NULL;
8320 c42c9805 2020-11-24 stsp
8321 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8322 c42c9805 2020-11-24 stsp if (err) {
8323 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8324 c42c9805 2020-11-24 stsp return err;
8325 c42c9805 2020-11-24 stsp else
8326 c42c9805 2020-11-24 stsp return NULL;
8327 c42c9805 2020-11-24 stsp }
8328 c42c9805 2020-11-24 stsp
8329 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8330 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8331 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8332 6458efa5 2020-11-24 stsp goto done;
8333 6458efa5 2020-11-24 stsp }
8334 6458efa5 2020-11-24 stsp
8335 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8336 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8337 6458efa5 2020-11-24 stsp done:
8338 6458efa5 2020-11-24 stsp if (err)
8339 6458efa5 2020-11-24 stsp view_close(log_view);
8340 6458efa5 2020-11-24 stsp else
8341 6458efa5 2020-11-24 stsp *new_view = log_view;
8342 c42c9805 2020-11-24 stsp free(commit_id);
8343 6458efa5 2020-11-24 stsp return err;
8344 6458efa5 2020-11-24 stsp }
8345 6458efa5 2020-11-24 stsp
8346 ce5b7c56 2019-07-09 stsp static void
8347 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8348 6458efa5 2020-11-24 stsp {
8349 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8350 34ba6917 2020-11-30 stsp int i = 0;
8351 6458efa5 2020-11-24 stsp
8352 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8353 6458efa5 2020-11-24 stsp return;
8354 6458efa5 2020-11-24 stsp
8355 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8356 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8357 34ba6917 2020-11-30 stsp if (re == NULL)
8358 34ba6917 2020-11-30 stsp break;
8359 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8360 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8361 6458efa5 2020-11-24 stsp }
8362 6458efa5 2020-11-24 stsp }
8363 6458efa5 2020-11-24 stsp
8364 a5d43cac 2022-07-01 thomas static const struct got_error *
8365 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8366 6458efa5 2020-11-24 stsp {
8367 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8368 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8369 6458efa5 2020-11-24 stsp int n = 0;
8370 6458efa5 2020-11-24 stsp
8371 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8372 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8373 6458efa5 2020-11-24 stsp else
8374 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8375 6458efa5 2020-11-24 stsp
8376 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8377 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8378 07dd3ed3 2022-08-06 thomas if (last) {
8379 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8380 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8381 07dd3ed3 2022-08-06 thomas }
8382 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8383 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8384 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8385 6458efa5 2020-11-24 stsp }
8386 6458efa5 2020-11-24 stsp }
8387 a5d43cac 2022-07-01 thomas
8388 a5d43cac 2022-07-01 thomas return NULL;
8389 6458efa5 2020-11-24 stsp }
8390 6458efa5 2020-11-24 stsp
8391 6458efa5 2020-11-24 stsp static const struct got_error *
8392 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8393 6458efa5 2020-11-24 stsp {
8394 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8395 6458efa5 2020-11-24 stsp
8396 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8397 6458efa5 2020-11-24 stsp return NULL;
8398 6458efa5 2020-11-24 stsp }
8399 6458efa5 2020-11-24 stsp
8400 6458efa5 2020-11-24 stsp static int
8401 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8402 6458efa5 2020-11-24 stsp {
8403 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8404 6458efa5 2020-11-24 stsp
8405 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8406 6458efa5 2020-11-24 stsp 0) == 0;
8407 6458efa5 2020-11-24 stsp }
8408 6458efa5 2020-11-24 stsp
8409 6458efa5 2020-11-24 stsp static const struct got_error *
8410 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8411 6458efa5 2020-11-24 stsp {
8412 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8413 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8414 6458efa5 2020-11-24 stsp
8415 6458efa5 2020-11-24 stsp if (!view->searching) {
8416 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8417 6458efa5 2020-11-24 stsp return NULL;
8418 6458efa5 2020-11-24 stsp }
8419 6458efa5 2020-11-24 stsp
8420 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8421 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8422 6458efa5 2020-11-24 stsp if (s->selected_entry)
8423 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8424 6458efa5 2020-11-24 stsp else
8425 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8426 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8427 6458efa5 2020-11-24 stsp } else {
8428 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8429 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8430 6458efa5 2020-11-24 stsp else
8431 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8432 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8433 6458efa5 2020-11-24 stsp }
8434 6458efa5 2020-11-24 stsp } else {
8435 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8436 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8437 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8438 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8439 6458efa5 2020-11-24 stsp else
8440 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8441 6458efa5 2020-11-24 stsp }
8442 6458efa5 2020-11-24 stsp
8443 6458efa5 2020-11-24 stsp while (1) {
8444 6458efa5 2020-11-24 stsp if (re == NULL) {
8445 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8446 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8447 6458efa5 2020-11-24 stsp return NULL;
8448 6458efa5 2020-11-24 stsp }
8449 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8450 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8451 6458efa5 2020-11-24 stsp else
8452 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8453 6458efa5 2020-11-24 stsp }
8454 6458efa5 2020-11-24 stsp
8455 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8456 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8457 6458efa5 2020-11-24 stsp s->matched_entry = re;
8458 6458efa5 2020-11-24 stsp break;
8459 6458efa5 2020-11-24 stsp }
8460 6458efa5 2020-11-24 stsp
8461 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8462 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8463 6458efa5 2020-11-24 stsp else
8464 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8465 6458efa5 2020-11-24 stsp }
8466 6458efa5 2020-11-24 stsp
8467 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8468 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8469 6458efa5 2020-11-24 stsp s->selected = 0;
8470 6458efa5 2020-11-24 stsp }
8471 6458efa5 2020-11-24 stsp
8472 6458efa5 2020-11-24 stsp return NULL;
8473 6458efa5 2020-11-24 stsp }
8474 6458efa5 2020-11-24 stsp
8475 6458efa5 2020-11-24 stsp static const struct got_error *
8476 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8477 6458efa5 2020-11-24 stsp {
8478 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8479 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8481 6458efa5 2020-11-24 stsp char *line = NULL;
8482 6458efa5 2020-11-24 stsp wchar_t *wline;
8483 6458efa5 2020-11-24 stsp struct tog_color *tc;
8484 66a70b97 2023-02-03 thomas int width, n, scrollx;
8485 6458efa5 2020-11-24 stsp int limit = view->nlines;
8486 6458efa5 2020-11-24 stsp
8487 6458efa5 2020-11-24 stsp werase(view->window);
8488 6458efa5 2020-11-24 stsp
8489 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8490 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8491 a5d43cac 2022-07-01 thomas --limit; /* border */
8492 6458efa5 2020-11-24 stsp
8493 6458efa5 2020-11-24 stsp if (limit == 0)
8494 6458efa5 2020-11-24 stsp return NULL;
8495 6458efa5 2020-11-24 stsp
8496 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8497 6458efa5 2020-11-24 stsp
8498 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8499 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8500 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8501 6458efa5 2020-11-24 stsp
8502 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8503 6458efa5 2020-11-24 stsp if (err) {
8504 6458efa5 2020-11-24 stsp free(line);
8505 6458efa5 2020-11-24 stsp return err;
8506 6458efa5 2020-11-24 stsp }
8507 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8508 6458efa5 2020-11-24 stsp wstandout(view->window);
8509 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8510 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8511 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8512 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8513 6458efa5 2020-11-24 stsp wstandend(view->window);
8514 6458efa5 2020-11-24 stsp free(wline);
8515 6458efa5 2020-11-24 stsp wline = NULL;
8516 6458efa5 2020-11-24 stsp free(line);
8517 6458efa5 2020-11-24 stsp line = NULL;
8518 6458efa5 2020-11-24 stsp if (--limit <= 0)
8519 6458efa5 2020-11-24 stsp return NULL;
8520 6458efa5 2020-11-24 stsp
8521 6458efa5 2020-11-24 stsp n = 0;
8522 66a70b97 2023-02-03 thomas view->maxx = 0;
8523 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8524 6458efa5 2020-11-24 stsp char *line = NULL;
8525 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8526 6458efa5 2020-11-24 stsp
8527 84227eb1 2022-06-23 thomas if (s->show_date) {
8528 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8529 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8530 84227eb1 2022-06-23 thomas struct got_object_id *id;
8531 84227eb1 2022-06-23 thomas struct tm tm;
8532 84227eb1 2022-06-23 thomas time_t t;
8533 84227eb1 2022-06-23 thomas
8534 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8535 84227eb1 2022-06-23 thomas if (err)
8536 84227eb1 2022-06-23 thomas return err;
8537 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8538 84227eb1 2022-06-23 thomas if (err) {
8539 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8540 84227eb1 2022-06-23 thomas free(id);
8541 84227eb1 2022-06-23 thomas return err;
8542 84227eb1 2022-06-23 thomas }
8543 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8544 84227eb1 2022-06-23 thomas id);
8545 84227eb1 2022-06-23 thomas if (err) {
8546 84227eb1 2022-06-23 thomas free(id);
8547 84227eb1 2022-06-23 thomas return err;
8548 84227eb1 2022-06-23 thomas }
8549 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8550 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8551 84227eb1 2022-06-23 thomas } else {
8552 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8553 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8554 84227eb1 2022-06-23 thomas }
8555 84227eb1 2022-06-23 thomas free(id);
8556 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8557 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8558 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8559 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8560 84227eb1 2022-06-23 thomas }
8561 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8562 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8563 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8564 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8565 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8566 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8567 6458efa5 2020-11-24 stsp struct got_object_id *id;
8568 6458efa5 2020-11-24 stsp char *id_str;
8569 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8570 6458efa5 2020-11-24 stsp if (err)
8571 6458efa5 2020-11-24 stsp return err;
8572 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8573 6458efa5 2020-11-24 stsp if (err) {
8574 6458efa5 2020-11-24 stsp free(id);
8575 6458efa5 2020-11-24 stsp return err;
8576 6458efa5 2020-11-24 stsp }
8577 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8578 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8579 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8580 6458efa5 2020-11-24 stsp free(id);
8581 6458efa5 2020-11-24 stsp free(id_str);
8582 6458efa5 2020-11-24 stsp return err;
8583 6458efa5 2020-11-24 stsp }
8584 6458efa5 2020-11-24 stsp free(id);
8585 6458efa5 2020-11-24 stsp free(id_str);
8586 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8587 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8588 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8589 6458efa5 2020-11-24 stsp
8590 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8591 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8592 6458efa5 2020-11-24 stsp if (err) {
8593 6458efa5 2020-11-24 stsp free(line);
8594 6458efa5 2020-11-24 stsp return err;
8595 6458efa5 2020-11-24 stsp }
8596 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8597 66a70b97 2023-02-03 thomas free(wline);
8598 66a70b97 2023-02-03 thomas wline = NULL;
8599 66a70b97 2023-02-03 thomas
8600 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8601 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8602 66a70b97 2023-02-03 thomas if (err) {
8603 66a70b97 2023-02-03 thomas free(line);
8604 66a70b97 2023-02-03 thomas return err;
8605 66a70b97 2023-02-03 thomas }
8606 6458efa5 2020-11-24 stsp if (n == s->selected) {
8607 6458efa5 2020-11-24 stsp if (view->focussed)
8608 6458efa5 2020-11-24 stsp wstandout(view->window);
8609 6458efa5 2020-11-24 stsp s->selected_entry = re;
8610 6458efa5 2020-11-24 stsp }
8611 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8612 6458efa5 2020-11-24 stsp if (tc)
8613 6458efa5 2020-11-24 stsp wattr_on(view->window,
8614 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8615 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8616 6458efa5 2020-11-24 stsp if (tc)
8617 6458efa5 2020-11-24 stsp wattr_off(view->window,
8618 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8619 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8620 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8621 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8622 6458efa5 2020-11-24 stsp wstandend(view->window);
8623 6458efa5 2020-11-24 stsp free(line);
8624 6458efa5 2020-11-24 stsp free(wline);
8625 6458efa5 2020-11-24 stsp wline = NULL;
8626 6458efa5 2020-11-24 stsp n++;
8627 6458efa5 2020-11-24 stsp s->ndisplayed++;
8628 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8629 6458efa5 2020-11-24 stsp
8630 6458efa5 2020-11-24 stsp limit--;
8631 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8632 6458efa5 2020-11-24 stsp }
8633 6458efa5 2020-11-24 stsp
8634 a5d43cac 2022-07-01 thomas view_border(view);
8635 6458efa5 2020-11-24 stsp return err;
8636 6458efa5 2020-11-24 stsp }
8637 6458efa5 2020-11-24 stsp
8638 6458efa5 2020-11-24 stsp static const struct got_error *
8639 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8640 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8641 c42c9805 2020-11-24 stsp {
8642 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8643 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8644 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8645 c42c9805 2020-11-24 stsp
8646 c42c9805 2020-11-24 stsp *new_view = NULL;
8647 c42c9805 2020-11-24 stsp
8648 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8649 c42c9805 2020-11-24 stsp if (err) {
8650 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8651 c42c9805 2020-11-24 stsp return err;
8652 c42c9805 2020-11-24 stsp else
8653 c42c9805 2020-11-24 stsp return NULL;
8654 c42c9805 2020-11-24 stsp }
8655 c42c9805 2020-11-24 stsp
8656 c42c9805 2020-11-24 stsp
8657 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8658 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8659 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8660 c42c9805 2020-11-24 stsp goto done;
8661 c42c9805 2020-11-24 stsp }
8662 c42c9805 2020-11-24 stsp
8663 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8664 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8665 c42c9805 2020-11-24 stsp if (err)
8666 c42c9805 2020-11-24 stsp goto done;
8667 c42c9805 2020-11-24 stsp
8668 c42c9805 2020-11-24 stsp *new_view = tree_view;
8669 c42c9805 2020-11-24 stsp done:
8670 c42c9805 2020-11-24 stsp free(commit_id);
8671 c42c9805 2020-11-24 stsp return err;
8672 07dd3ed3 2022-08-06 thomas }
8673 07dd3ed3 2022-08-06 thomas
8674 07dd3ed3 2022-08-06 thomas static const struct got_error *
8675 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8676 07dd3ed3 2022-08-06 thomas {
8677 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8678 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8679 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8680 07dd3ed3 2022-08-06 thomas
8681 07dd3ed3 2022-08-06 thomas g = view->gline;
8682 07dd3ed3 2022-08-06 thomas view->gline = 0;
8683 07dd3ed3 2022-08-06 thomas
8684 07dd3ed3 2022-08-06 thomas if (g == 0)
8685 07dd3ed3 2022-08-06 thomas g = 1;
8686 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8687 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8688 07dd3ed3 2022-08-06 thomas
8689 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8690 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8691 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8692 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8693 07dd3ed3 2022-08-06 thomas return NULL;
8694 07dd3ed3 2022-08-06 thomas }
8695 07dd3ed3 2022-08-06 thomas
8696 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8697 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8698 07dd3ed3 2022-08-06 thomas if (err)
8699 07dd3ed3 2022-08-06 thomas return err;
8700 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8701 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8702 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8703 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8704 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8705 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8706 07dd3ed3 2022-08-06 thomas
8707 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8708 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8709 07dd3ed3 2022-08-06 thomas
8710 07dd3ed3 2022-08-06 thomas return NULL;
8711 07dd3ed3 2022-08-06 thomas
8712 c42c9805 2020-11-24 stsp }
8713 07dd3ed3 2022-08-06 thomas
8714 c42c9805 2020-11-24 stsp static const struct got_error *
8715 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8716 6458efa5 2020-11-24 stsp {
8717 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8718 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8719 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8720 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8721 6458efa5 2020-11-24 stsp
8722 07dd3ed3 2022-08-06 thomas if (view->gline)
8723 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8724 07dd3ed3 2022-08-06 thomas
8725 6458efa5 2020-11-24 stsp switch (ch) {
8726 66a70b97 2023-02-03 thomas case '0':
8727 66a70b97 2023-02-03 thomas case '$':
8728 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8729 66a70b97 2023-02-03 thomas case 'l':
8730 66a70b97 2023-02-03 thomas case KEY_LEFT:
8731 66a70b97 2023-02-03 thomas case 'h':
8732 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8733 66a70b97 2023-02-03 thomas break;
8734 6458efa5 2020-11-24 stsp case 'i':
8735 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8736 07b0611c 2022-06-23 thomas view->count = 0;
8737 3bfadbd4 2021-11-20 thomas break;
8738 84227eb1 2022-06-23 thomas case 'm':
8739 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8740 07b0611c 2022-06-23 thomas view->count = 0;
8741 84227eb1 2022-06-23 thomas break;
8742 98182bd0 2021-11-20 thomas case 'o':
8743 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8744 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8745 07b0611c 2022-06-23 thomas view->count = 0;
8746 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8747 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8748 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8749 c591440f 2021-11-20 thomas if (err)
8750 c591440f 2021-11-20 thomas break;
8751 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8752 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8753 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8754 3bfadbd4 2021-11-20 thomas if (err)
8755 3bfadbd4 2021-11-20 thomas break;
8756 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8757 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8758 6458efa5 2020-11-24 stsp break;
8759 6458efa5 2020-11-24 stsp case KEY_ENTER:
8760 6458efa5 2020-11-24 stsp case '\r':
8761 07b0611c 2022-06-23 thomas view->count = 0;
8762 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8763 a5d43cac 2022-07-01 thomas break;
8764 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8765 6458efa5 2020-11-24 stsp break;
8766 1be4947a 2022-07-22 thomas case 'T':
8767 07b0611c 2022-06-23 thomas view->count = 0;
8768 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8769 c42c9805 2020-11-24 stsp break;
8770 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8771 c42c9805 2020-11-24 stsp break;
8772 e4526bf5 2021-09-03 naddy case 'g':
8773 aa7a1117 2023-01-09 thomas case '=':
8774 e4526bf5 2021-09-03 naddy case KEY_HOME:
8775 e4526bf5 2021-09-03 naddy s->selected = 0;
8776 07b0611c 2022-06-23 thomas view->count = 0;
8777 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8778 e4526bf5 2021-09-03 naddy break;
8779 e4526bf5 2021-09-03 naddy case 'G':
8780 aa7a1117 2023-01-09 thomas case '*':
8781 a5d43cac 2022-07-01 thomas case KEY_END: {
8782 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8783 a5d43cac 2022-07-01 thomas
8784 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8785 a5d43cac 2022-07-01 thomas --eos; /* border */
8786 e4526bf5 2021-09-03 naddy s->selected = 0;
8787 07b0611c 2022-06-23 thomas view->count = 0;
8788 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8789 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8790 e4526bf5 2021-09-03 naddy if (re == NULL)
8791 e4526bf5 2021-09-03 naddy break;
8792 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8793 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8794 e4526bf5 2021-09-03 naddy }
8795 e4526bf5 2021-09-03 naddy if (n > 0)
8796 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8797 e4526bf5 2021-09-03 naddy break;
8798 a5d43cac 2022-07-01 thomas }
8799 6458efa5 2020-11-24 stsp case 'k':
8800 6458efa5 2020-11-24 stsp case KEY_UP:
8801 f7140bf5 2021-10-17 thomas case CTRL('p'):
8802 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8803 6458efa5 2020-11-24 stsp s->selected--;
8804 6458efa5 2020-11-24 stsp break;
8805 34ba6917 2020-11-30 stsp }
8806 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8807 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8808 07b0611c 2022-06-23 thomas view->count = 0;
8809 6458efa5 2020-11-24 stsp break;
8810 70f17a53 2022-06-13 thomas case CTRL('u'):
8811 23427b14 2022-06-23 thomas case 'u':
8812 70f17a53 2022-06-13 thomas nscroll /= 2;
8813 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8814 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8815 6458efa5 2020-11-24 stsp case CTRL('b'):
8816 1c5e5faa 2022-06-23 thomas case 'b':
8817 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8818 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8819 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8820 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8821 07b0611c 2022-06-23 thomas view->count = 0;
8822 6458efa5 2020-11-24 stsp break;
8823 6458efa5 2020-11-24 stsp case 'j':
8824 6458efa5 2020-11-24 stsp case KEY_DOWN:
8825 f7140bf5 2021-10-17 thomas case CTRL('n'):
8826 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8827 6458efa5 2020-11-24 stsp s->selected++;
8828 6458efa5 2020-11-24 stsp break;
8829 6458efa5 2020-11-24 stsp }
8830 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8831 6458efa5 2020-11-24 stsp /* can't scroll any further */
8832 07b0611c 2022-06-23 thomas view->count = 0;
8833 6458efa5 2020-11-24 stsp break;
8834 07b0611c 2022-06-23 thomas }
8835 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8836 6458efa5 2020-11-24 stsp break;
8837 70f17a53 2022-06-13 thomas case CTRL('d'):
8838 23427b14 2022-06-23 thomas case 'd':
8839 70f17a53 2022-06-13 thomas nscroll /= 2;
8840 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8841 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8842 6458efa5 2020-11-24 stsp case CTRL('f'):
8843 1c5e5faa 2022-06-23 thomas case 'f':
8844 4c2d69cb 2022-06-23 thomas case ' ':
8845 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8846 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8847 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8848 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8849 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8850 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8851 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8852 07b0611c 2022-06-23 thomas view->count = 0;
8853 6458efa5 2020-11-24 stsp break;
8854 6458efa5 2020-11-24 stsp }
8855 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8856 6458efa5 2020-11-24 stsp break;
8857 6458efa5 2020-11-24 stsp case CTRL('l'):
8858 07b0611c 2022-06-23 thomas view->count = 0;
8859 8924d611 2020-12-26 stsp tog_free_refs();
8860 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8861 8924d611 2020-12-26 stsp if (err)
8862 8924d611 2020-12-26 stsp break;
8863 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8864 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8865 6458efa5 2020-11-24 stsp break;
8866 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8867 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8868 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8869 6458efa5 2020-11-24 stsp break;
8870 6458efa5 2020-11-24 stsp default:
8871 07b0611c 2022-06-23 thomas view->count = 0;
8872 6458efa5 2020-11-24 stsp break;
8873 6458efa5 2020-11-24 stsp }
8874 6458efa5 2020-11-24 stsp
8875 6458efa5 2020-11-24 stsp return err;
8876 6458efa5 2020-11-24 stsp }
8877 6458efa5 2020-11-24 stsp
8878 6458efa5 2020-11-24 stsp __dead static void
8879 6458efa5 2020-11-24 stsp usage_ref(void)
8880 6458efa5 2020-11-24 stsp {
8881 6458efa5 2020-11-24 stsp endwin();
8882 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8883 6458efa5 2020-11-24 stsp getprogname());
8884 6458efa5 2020-11-24 stsp exit(1);
8885 6458efa5 2020-11-24 stsp }
8886 6458efa5 2020-11-24 stsp
8887 6458efa5 2020-11-24 stsp static const struct got_error *
8888 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8889 6458efa5 2020-11-24 stsp {
8890 1d98034b 2023-04-14 thomas const struct got_error *error;
8891 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8892 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8893 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8894 6458efa5 2020-11-24 stsp int ch;
8895 6458efa5 2020-11-24 stsp struct tog_view *view;
8896 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8897 6458efa5 2020-11-24 stsp
8898 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8899 6458efa5 2020-11-24 stsp switch (ch) {
8900 6458efa5 2020-11-24 stsp case 'r':
8901 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8902 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8903 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8904 6458efa5 2020-11-24 stsp optarg);
8905 6458efa5 2020-11-24 stsp break;
8906 6458efa5 2020-11-24 stsp default:
8907 e99e2d15 2020-11-24 naddy usage_ref();
8908 6458efa5 2020-11-24 stsp /* NOTREACHED */
8909 6458efa5 2020-11-24 stsp }
8910 6458efa5 2020-11-24 stsp }
8911 6458efa5 2020-11-24 stsp
8912 6458efa5 2020-11-24 stsp argc -= optind;
8913 6458efa5 2020-11-24 stsp argv += optind;
8914 6458efa5 2020-11-24 stsp
8915 6458efa5 2020-11-24 stsp if (argc > 1)
8916 e99e2d15 2020-11-24 naddy usage_ref();
8917 7cd52833 2022-06-23 thomas
8918 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8919 7cd52833 2022-06-23 thomas if (error != NULL)
8920 7cd52833 2022-06-23 thomas goto done;
8921 6458efa5 2020-11-24 stsp
8922 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8923 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8924 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8925 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8926 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8927 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8928 c156c7a4 2020-12-18 stsp goto done;
8929 6458efa5 2020-11-24 stsp if (worktree)
8930 6458efa5 2020-11-24 stsp repo_path =
8931 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8932 6458efa5 2020-11-24 stsp else
8933 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8934 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8935 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8936 c156c7a4 2020-12-18 stsp goto done;
8937 c156c7a4 2020-12-18 stsp }
8938 6458efa5 2020-11-24 stsp }
8939 6458efa5 2020-11-24 stsp
8940 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8941 6458efa5 2020-11-24 stsp if (error != NULL)
8942 6458efa5 2020-11-24 stsp goto done;
8943 6458efa5 2020-11-24 stsp
8944 557d3365 2023-04-14 thomas init_curses();
8945 6458efa5 2020-11-24 stsp
8946 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8947 51a10b52 2020-12-26 stsp if (error)
8948 51a10b52 2020-12-26 stsp goto done;
8949 51a10b52 2020-12-26 stsp
8950 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8951 6458efa5 2020-11-24 stsp if (error)
8952 6458efa5 2020-11-24 stsp goto done;
8953 6458efa5 2020-11-24 stsp
8954 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8955 6458efa5 2020-11-24 stsp if (view == NULL) {
8956 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8957 6458efa5 2020-11-24 stsp goto done;
8958 6458efa5 2020-11-24 stsp }
8959 6458efa5 2020-11-24 stsp
8960 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8961 6458efa5 2020-11-24 stsp if (error)
8962 6458efa5 2020-11-24 stsp goto done;
8963 6458efa5 2020-11-24 stsp
8964 6458efa5 2020-11-24 stsp if (worktree) {
8965 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8966 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8967 6458efa5 2020-11-24 stsp worktree = NULL;
8968 6458efa5 2020-11-24 stsp }
8969 557d3365 2023-04-14 thomas error = view_loop(view);
8970 6458efa5 2020-11-24 stsp done:
8971 6458efa5 2020-11-24 stsp free(repo_path);
8972 6458efa5 2020-11-24 stsp free(cwd);
8973 1d0f4054 2021-06-17 stsp if (repo) {
8974 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8975 1d0f4054 2021-06-17 stsp if (close_err)
8976 1d0f4054 2021-06-17 stsp error = close_err;
8977 1d0f4054 2021-06-17 stsp }
8978 7cd52833 2022-06-23 thomas if (pack_fds) {
8979 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8980 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8981 7cd52833 2022-06-23 thomas if (error == NULL)
8982 7cd52833 2022-06-23 thomas error = pack_err;
8983 b85a3496 2023-04-14 thomas }
8984 51a10b52 2020-12-26 stsp tog_free_refs();
8985 6458efa5 2020-11-24 stsp return error;
8986 6458efa5 2020-11-24 stsp }
8987 6458efa5 2020-11-24 stsp
8988 fc2737d5 2022-09-15 thomas static const struct got_error*
8989 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8990 fc2737d5 2022-09-15 thomas const char *str)
8991 fc2737d5 2022-09-15 thomas {
8992 fc2737d5 2022-09-15 thomas size_t len;
8993 fc2737d5 2022-09-15 thomas
8994 fc2737d5 2022-09-15 thomas if (win == NULL)
8995 fc2737d5 2022-09-15 thomas win = stdscr;
8996 fc2737d5 2022-09-15 thomas
8997 fc2737d5 2022-09-15 thomas len = strlen(str);
8998 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8999 fc2737d5 2022-09-15 thomas
9000 fc2737d5 2022-09-15 thomas if (focus)
9001 fc2737d5 2022-09-15 thomas wstandout(win);
9002 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9003 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9004 fc2737d5 2022-09-15 thomas if (focus)
9005 fc2737d5 2022-09-15 thomas wstandend(win);
9006 fc2737d5 2022-09-15 thomas
9007 fc2737d5 2022-09-15 thomas return NULL;
9008 fc2737d5 2022-09-15 thomas }
9009 fc2737d5 2022-09-15 thomas
9010 2a31b33b 2022-07-23 thomas static const struct got_error *
9011 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9012 fc2737d5 2022-09-15 thomas {
9013 fc2737d5 2022-09-15 thomas off_t *p;
9014 fc2737d5 2022-09-15 thomas
9015 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9016 fc2737d5 2022-09-15 thomas if (p == NULL) {
9017 fc2737d5 2022-09-15 thomas free(*line_offsets);
9018 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9019 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9020 fc2737d5 2022-09-15 thomas }
9021 fc2737d5 2022-09-15 thomas
9022 fc2737d5 2022-09-15 thomas *line_offsets = p;
9023 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9024 fc2737d5 2022-09-15 thomas ++(*nlines);
9025 fc2737d5 2022-09-15 thomas return NULL;
9026 fc2737d5 2022-09-15 thomas }
9027 fc2737d5 2022-09-15 thomas
9028 fc2737d5 2022-09-15 thomas static const struct got_error *
9029 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9030 fc2737d5 2022-09-15 thomas {
9031 fc2737d5 2022-09-15 thomas *ret = 0;
9032 fc2737d5 2022-09-15 thomas
9033 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9034 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9035 fc2737d5 2022-09-15 thomas size_t len = 1;
9036 fc2737d5 2022-09-15 thomas
9037 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9038 fc2737d5 2022-09-15 thomas continue;
9039 fc2737d5 2022-09-15 thomas
9040 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9041 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9042 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9043 fc2737d5 2022-09-15 thomas
9044 fc2737d5 2022-09-15 thomas len += strlen(t);
9045 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9046 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9047 fc2737d5 2022-09-15 thomas free(t0);
9048 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9049 fc2737d5 2022-09-15 thomas }
9050 fc2737d5 2022-09-15 thomas
9051 fc2737d5 2022-09-15 thomas return NULL;
9052 fc2737d5 2022-09-15 thomas }
9053 fc2737d5 2022-09-15 thomas
9054 fc2737d5 2022-09-15 thomas /*
9055 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9056 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9057 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9058 fc2737d5 2022-09-15 thomas */
9059 fc2737d5 2022-09-15 thomas static const struct got_error *
9060 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9061 fc2737d5 2022-09-15 thomas {
9062 fc2737d5 2022-09-15 thomas int n, len = width;
9063 fc2737d5 2022-09-15 thomas
9064 fc2737d5 2022-09-15 thomas if (km->keys) {
9065 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9066 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9067 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9068 30e77f6a 2022-09-18 thomas };
9069 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9070 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9071 fc2737d5 2022-09-15 thomas
9072 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9073 fc2737d5 2022-09-15 thomas
9074 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9075 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9076 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9077 fc2737d5 2022-09-15 thomas
9078 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9079 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9080 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9081 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9082 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9083 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9084 fc2737d5 2022-09-15 thomas if (n < 0) {
9085 fc2737d5 2022-09-15 thomas free(t0);
9086 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9087 fc2737d5 2022-09-15 thomas }
9088 fc2737d5 2022-09-15 thomas first = 0;
9089 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9090 fc2737d5 2022-09-15 thomas *off += n;
9091 fc2737d5 2022-09-15 thomas }
9092 fc2737d5 2022-09-15 thomas free(t0);
9093 fc2737d5 2022-09-15 thomas }
9094 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9095 fc2737d5 2022-09-15 thomas if (n < 0)
9096 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9097 fc2737d5 2022-09-15 thomas *off += n;
9098 fc2737d5 2022-09-15 thomas
9099 fc2737d5 2022-09-15 thomas return NULL;
9100 fc2737d5 2022-09-15 thomas }
9101 fc2737d5 2022-09-15 thomas
9102 fc2737d5 2022-09-15 thomas static const struct got_error *
9103 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9104 fc2737d5 2022-09-15 thomas {
9105 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9106 fc2737d5 2022-09-15 thomas off_t off = 0;
9107 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9108 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9109 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9110 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9111 fc2737d5 2022-09-15 thomas GENERATE_HELP
9112 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9113 fc2737d5 2022-09-15 thomas #undef KEY_
9114 fc2737d5 2022-09-15 thomas };
9115 fc2737d5 2022-09-15 thomas
9116 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9117 fc2737d5 2022-09-15 thomas if (err)
9118 fc2737d5 2022-09-15 thomas return err;
9119 fc2737d5 2022-09-15 thomas
9120 fc2737d5 2022-09-15 thomas n = nitems(km);
9121 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9122 fc2737d5 2022-09-15 thomas if (err)
9123 fc2737d5 2022-09-15 thomas return err;
9124 fc2737d5 2022-09-15 thomas
9125 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9126 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9127 fc2737d5 2022-09-15 thomas show = s->all;
9128 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9129 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9130 fc2737d5 2022-09-15 thomas show = 1;
9131 fc2737d5 2022-09-15 thomas }
9132 fc2737d5 2022-09-15 thomas if (show) {
9133 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9134 fc2737d5 2022-09-15 thomas if (err)
9135 fc2737d5 2022-09-15 thomas return err;
9136 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9137 fc2737d5 2022-09-15 thomas if (err)
9138 fc2737d5 2022-09-15 thomas return err;
9139 fc2737d5 2022-09-15 thomas }
9140 fc2737d5 2022-09-15 thomas }
9141 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9142 fc2737d5 2022-09-15 thomas ++off;
9143 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9144 fc2737d5 2022-09-15 thomas return err;
9145 fc2737d5 2022-09-15 thomas }
9146 fc2737d5 2022-09-15 thomas
9147 fc2737d5 2022-09-15 thomas static const struct got_error *
9148 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9149 fc2737d5 2022-09-15 thomas {
9150 fc2737d5 2022-09-15 thomas FILE *f;
9151 fc2737d5 2022-09-15 thomas const struct got_error *err;
9152 fc2737d5 2022-09-15 thomas
9153 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9154 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9155 fc2737d5 2022-09-15 thomas s->nlines = 0;
9156 fc2737d5 2022-09-15 thomas
9157 fc2737d5 2022-09-15 thomas f = got_opentemp();
9158 fc2737d5 2022-09-15 thomas if (f == NULL)
9159 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9160 fc2737d5 2022-09-15 thomas s->f = f;
9161 fc2737d5 2022-09-15 thomas
9162 fc2737d5 2022-09-15 thomas err = format_help(s);
9163 fc2737d5 2022-09-15 thomas if (err)
9164 fc2737d5 2022-09-15 thomas return err;
9165 fc2737d5 2022-09-15 thomas
9166 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9167 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9168 fc2737d5 2022-09-15 thomas
9169 fc2737d5 2022-09-15 thomas return NULL;
9170 fc2737d5 2022-09-15 thomas }
9171 fc2737d5 2022-09-15 thomas
9172 fc2737d5 2022-09-15 thomas static const struct got_error *
9173 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9174 fc2737d5 2022-09-15 thomas {
9175 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9176 fc2737d5 2022-09-15 thomas return NULL;
9177 fc2737d5 2022-09-15 thomas }
9178 fc2737d5 2022-09-15 thomas
9179 2a7d3cd7 2022-09-17 thomas static void
9180 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9181 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9182 2a7d3cd7 2022-09-17 thomas {
9183 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9184 2a7d3cd7 2022-09-17 thomas
9185 2a7d3cd7 2022-09-17 thomas *f = s->f;
9186 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9187 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9188 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9189 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9190 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9191 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9192 2a7d3cd7 2022-09-17 thomas }
9193 2a7d3cd7 2022-09-17 thomas
9194 fc2737d5 2022-09-15 thomas static const struct got_error *
9195 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9196 fc2737d5 2022-09-15 thomas {
9197 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9198 fc2737d5 2022-09-15 thomas const struct got_error *err;
9199 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9200 fc2737d5 2022-09-15 thomas wchar_t *wline;
9201 fc2737d5 2022-09-15 thomas char *line;
9202 fc2737d5 2022-09-15 thomas ssize_t linelen;
9203 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9204 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9205 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9206 fc2737d5 2022-09-15 thomas
9207 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9208 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9209 fc2737d5 2022-09-15 thomas
9210 fc2737d5 2022-09-15 thomas s->lineno = 0;
9211 fc2737d5 2022-09-15 thomas rewind(s->f);
9212 fc2737d5 2022-09-15 thomas werase(view->window);
9213 fc2737d5 2022-09-15 thomas
9214 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9215 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9216 fc2737d5 2022-09-15 thomas
9217 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9218 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9219 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9220 fc2737d5 2022-09-15 thomas if (err)
9221 fc2737d5 2022-09-15 thomas return err;
9222 fc2737d5 2022-09-15 thomas if (eos <= 1)
9223 fc2737d5 2022-09-15 thomas return NULL;
9224 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9225 fc2737d5 2022-09-15 thomas eos -= 2;
9226 fc2737d5 2022-09-15 thomas
9227 fc2737d5 2022-09-15 thomas s->eof = 0;
9228 fc2737d5 2022-09-15 thomas view->maxx = 0;
9229 fc2737d5 2022-09-15 thomas line = NULL;
9230 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9231 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9232 fc2737d5 2022-09-15 thomas
9233 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9234 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9235 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9236 fc2737d5 2022-09-15 thomas free(line);
9237 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9238 fc2737d5 2022-09-15 thomas }
9239 fc2737d5 2022-09-15 thomas s->eof = 1;
9240 fc2737d5 2022-09-15 thomas break;
9241 fc2737d5 2022-09-15 thomas }
9242 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9243 fc2737d5 2022-09-15 thomas continue;
9244 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9245 fc2737d5 2022-09-15 thomas continue;
9246 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9247 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9248 fc2737d5 2022-09-15 thomas
9249 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9250 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9251 fc2737d5 2022-09-15 thomas if (err) {
9252 fc2737d5 2022-09-15 thomas free(line);
9253 fc2737d5 2022-09-15 thomas return err;
9254 fc2737d5 2022-09-15 thomas }
9255 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9256 fc2737d5 2022-09-15 thomas free(wline);
9257 fc2737d5 2022-09-15 thomas wline = NULL;
9258 fc2737d5 2022-09-15 thomas
9259 fc2737d5 2022-09-15 thomas if (attr)
9260 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9261 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9262 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9263 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9264 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9265 fc2737d5 2022-09-15 thomas if (err) {
9266 fc2737d5 2022-09-15 thomas free(line);
9267 fc2737d5 2022-09-15 thomas return err;
9268 fc2737d5 2022-09-15 thomas }
9269 fc2737d5 2022-09-15 thomas } else {
9270 fc2737d5 2022-09-15 thomas int skip;
9271 fc2737d5 2022-09-15 thomas
9272 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9273 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9274 fc2737d5 2022-09-15 thomas if (err) {
9275 fc2737d5 2022-09-15 thomas free(line);
9276 fc2737d5 2022-09-15 thomas return err;
9277 fc2737d5 2022-09-15 thomas }
9278 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9279 fc2737d5 2022-09-15 thomas free(wline);
9280 fc2737d5 2022-09-15 thomas wline = NULL;
9281 fc2737d5 2022-09-15 thomas }
9282 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9283 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9284 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9285 fc2737d5 2022-09-15 thomas } else {
9286 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9287 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9288 fc2737d5 2022-09-15 thomas }
9289 fc2737d5 2022-09-15 thomas if (attr)
9290 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9291 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9292 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9293 fc2737d5 2022-09-15 thomas }
9294 fc2737d5 2022-09-15 thomas free(line);
9295 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9296 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9297 fc2737d5 2022-09-15 thomas else
9298 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9299 fc2737d5 2022-09-15 thomas
9300 fc2737d5 2022-09-15 thomas view_border(view);
9301 fc2737d5 2022-09-15 thomas
9302 fc2737d5 2022-09-15 thomas if (s->eof) {
9303 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9304 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9305 fc2737d5 2022-09-15 thomas view->ncols - 1);
9306 fc2737d5 2022-09-15 thomas if (rc == ERR)
9307 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9308 fc2737d5 2022-09-15 thomas } else {
9309 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9310 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9311 fc2737d5 2022-09-15 thomas wstandout(view->window);
9312 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9313 fc2737d5 2022-09-15 thomas view->ncols - 1);
9314 fc2737d5 2022-09-15 thomas if (rc == ERR)
9315 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9316 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9317 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9318 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9319 fc2737d5 2022-09-15 thomas if (rc == ERR)
9320 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9321 fc2737d5 2022-09-15 thomas }
9322 fc2737d5 2022-09-15 thomas wstandend(view->window);
9323 fc2737d5 2022-09-15 thomas }
9324 fc2737d5 2022-09-15 thomas
9325 fc2737d5 2022-09-15 thomas return NULL;
9326 fc2737d5 2022-09-15 thomas }
9327 fc2737d5 2022-09-15 thomas
9328 fc2737d5 2022-09-15 thomas static const struct got_error *
9329 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9330 fc2737d5 2022-09-15 thomas {
9331 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9332 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9333 fc2737d5 2022-09-15 thomas char *line = NULL;
9334 fc2737d5 2022-09-15 thomas ssize_t linelen;
9335 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9336 fc2737d5 2022-09-15 thomas int eos, nscroll;
9337 fc2737d5 2022-09-15 thomas
9338 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9339 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9340 fc2737d5 2022-09-15 thomas --eos; /* border */
9341 fc2737d5 2022-09-15 thomas
9342 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9343 fc2737d5 2022-09-15 thomas
9344 fc2737d5 2022-09-15 thomas switch (ch) {
9345 fc2737d5 2022-09-15 thomas case '0':
9346 fc2737d5 2022-09-15 thomas case '$':
9347 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9348 fc2737d5 2022-09-15 thomas case 'l':
9349 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9350 fc2737d5 2022-09-15 thomas case 'h':
9351 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9352 fc2737d5 2022-09-15 thomas break;
9353 fc2737d5 2022-09-15 thomas case 'g':
9354 fc2737d5 2022-09-15 thomas case KEY_HOME:
9355 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9356 fc2737d5 2022-09-15 thomas view->count = 0;
9357 fc2737d5 2022-09-15 thomas break;
9358 fc2737d5 2022-09-15 thomas case 'G':
9359 fc2737d5 2022-09-15 thomas case KEY_END:
9360 fc2737d5 2022-09-15 thomas view->count = 0;
9361 fc2737d5 2022-09-15 thomas if (s->eof)
9362 fc2737d5 2022-09-15 thomas break;
9363 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9364 fc2737d5 2022-09-15 thomas s->eof = 1;
9365 fc2737d5 2022-09-15 thomas break;
9366 fc2737d5 2022-09-15 thomas case 'k':
9367 fc2737d5 2022-09-15 thomas case KEY_UP:
9368 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9369 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9370 fc2737d5 2022-09-15 thomas else
9371 fc2737d5 2022-09-15 thomas view->count = 0;
9372 fc2737d5 2022-09-15 thomas break;
9373 fc2737d5 2022-09-15 thomas case CTRL('u'):
9374 fc2737d5 2022-09-15 thomas case 'u':
9375 fc2737d5 2022-09-15 thomas nscroll /= 2;
9376 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9377 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9378 fc2737d5 2022-09-15 thomas case CTRL('b'):
9379 fc2737d5 2022-09-15 thomas case 'b':
9380 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9381 fc2737d5 2022-09-15 thomas view->count = 0;
9382 fc2737d5 2022-09-15 thomas break;
9383 fc2737d5 2022-09-15 thomas }
9384 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9385 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9386 fc2737d5 2022-09-15 thomas break;
9387 fc2737d5 2022-09-15 thomas case 'j':
9388 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9389 fc2737d5 2022-09-15 thomas case CTRL('n'):
9390 fc2737d5 2022-09-15 thomas if (!s->eof)
9391 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9392 fc2737d5 2022-09-15 thomas else
9393 fc2737d5 2022-09-15 thomas view->count = 0;
9394 fc2737d5 2022-09-15 thomas break;
9395 fc2737d5 2022-09-15 thomas case CTRL('d'):
9396 fc2737d5 2022-09-15 thomas case 'd':
9397 fc2737d5 2022-09-15 thomas nscroll /= 2;
9398 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9399 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9400 fc2737d5 2022-09-15 thomas case CTRL('f'):
9401 fc2737d5 2022-09-15 thomas case 'f':
9402 fc2737d5 2022-09-15 thomas case ' ':
9403 fc2737d5 2022-09-15 thomas if (s->eof) {
9404 fc2737d5 2022-09-15 thomas view->count = 0;
9405 fc2737d5 2022-09-15 thomas break;
9406 fc2737d5 2022-09-15 thomas }
9407 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9408 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9409 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9410 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9411 fc2737d5 2022-09-15 thomas if (feof(s->f))
9412 fc2737d5 2022-09-15 thomas s->eof = 1;
9413 fc2737d5 2022-09-15 thomas else
9414 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9415 fc2737d5 2022-09-15 thomas break;
9416 fc2737d5 2022-09-15 thomas }
9417 fc2737d5 2022-09-15 thomas }
9418 fc2737d5 2022-09-15 thomas free(line);
9419 fc2737d5 2022-09-15 thomas break;
9420 fc2737d5 2022-09-15 thomas default:
9421 fc2737d5 2022-09-15 thomas view->count = 0;
9422 fc2737d5 2022-09-15 thomas break;
9423 fc2737d5 2022-09-15 thomas }
9424 fc2737d5 2022-09-15 thomas
9425 fc2737d5 2022-09-15 thomas return err;
9426 fc2737d5 2022-09-15 thomas }
9427 fc2737d5 2022-09-15 thomas
9428 fc2737d5 2022-09-15 thomas static const struct got_error *
9429 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9430 fc2737d5 2022-09-15 thomas {
9431 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9432 fc2737d5 2022-09-15 thomas
9433 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9434 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9435 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9436 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9437 fc2737d5 2022-09-15 thomas
9438 fc2737d5 2022-09-15 thomas return NULL;
9439 fc2737d5 2022-09-15 thomas }
9440 fc2737d5 2022-09-15 thomas
9441 fc2737d5 2022-09-15 thomas static const struct got_error *
9442 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9443 fc2737d5 2022-09-15 thomas {
9444 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9445 fc2737d5 2022-09-15 thomas
9446 fc2737d5 2022-09-15 thomas
9447 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9448 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9449 fc2737d5 2022-09-15 thomas
9450 fc2737d5 2022-09-15 thomas wclear(view->window);
9451 fc2737d5 2022-09-15 thomas view->count = 0;
9452 fc2737d5 2022-09-15 thomas view->x = 0;
9453 fc2737d5 2022-09-15 thomas s->all = !s->all;
9454 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9455 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9456 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9457 fc2737d5 2022-09-15 thomas
9458 fc2737d5 2022-09-15 thomas return create_help(s);
9459 fc2737d5 2022-09-15 thomas }
9460 fc2737d5 2022-09-15 thomas
9461 fc2737d5 2022-09-15 thomas static const struct got_error *
9462 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9463 fc2737d5 2022-09-15 thomas {
9464 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9465 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9466 fc2737d5 2022-09-15 thomas
9467 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9468 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9469 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9470 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9471 fc2737d5 2022-09-15 thomas
9472 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9473 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9474 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9475 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9476 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9477 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9478 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9479 fc2737d5 2022-09-15 thomas
9480 fc2737d5 2022-09-15 thomas err = create_help(s);
9481 fc2737d5 2022-09-15 thomas return err;
9482 fc2737d5 2022-09-15 thomas }
9483 fc2737d5 2022-09-15 thomas
9484 fc2737d5 2022-09-15 thomas static const struct got_error *
9485 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9486 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9487 2a31b33b 2022-07-23 thomas {
9488 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9489 2a31b33b 2022-07-23 thomas
9490 2a31b33b 2022-07-23 thomas *new_view = NULL;
9491 2a31b33b 2022-07-23 thomas
9492 2a31b33b 2022-07-23 thomas switch (request) {
9493 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9494 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9495 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9496 2a31b33b 2022-07-23 thomas
9497 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9498 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9499 2a31b33b 2022-07-23 thomas view, s->repo);
9500 2a31b33b 2022-07-23 thomas } else
9501 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9502 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9503 2a31b33b 2022-07-23 thomas break;
9504 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9505 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9506 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9507 2a31b33b 2022-07-23 thomas
9508 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9509 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9510 2a31b33b 2022-07-23 thomas s->repo);
9511 2a31b33b 2022-07-23 thomas } else
9512 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9513 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9514 2a31b33b 2022-07-23 thomas break;
9515 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9516 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9517 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9518 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9519 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9520 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9521 2a31b33b 2022-07-23 thomas &view->state.tree);
9522 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9523 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9524 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9525 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9526 2a31b33b 2022-07-23 thomas else
9527 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9528 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9529 2a31b33b 2022-07-23 thomas break;
9530 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9531 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9532 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9533 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9534 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9535 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9536 2a31b33b 2022-07-23 thomas view->state.log.repo);
9537 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9538 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9539 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9540 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9541 2a31b33b 2022-07-23 thomas else
9542 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9543 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9544 2a31b33b 2022-07-23 thomas break;
9545 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9546 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9547 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9548 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9549 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9550 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9551 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9552 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9553 2a31b33b 2022-07-23 thomas else
9554 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9555 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9556 2a31b33b 2022-07-23 thomas if (err)
9557 2a31b33b 2022-07-23 thomas view_close(*new_view);
9558 2a31b33b 2022-07-23 thomas break;
9559 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9560 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9561 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9562 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9563 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9564 fc2737d5 2022-09-15 thomas if (err)
9565 fc2737d5 2022-09-15 thomas view_close(*new_view);
9566 fc2737d5 2022-09-15 thomas break;
9567 2a31b33b 2022-07-23 thomas default:
9568 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9569 2a31b33b 2022-07-23 thomas }
9570 2a31b33b 2022-07-23 thomas
9571 2a31b33b 2022-07-23 thomas return err;
9572 2a31b33b 2022-07-23 thomas }
9573 2a31b33b 2022-07-23 thomas
9574 a5d43cac 2022-07-01 thomas /*
9575 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9576 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9577 a5d43cac 2022-07-01 thomas */
9578 6458efa5 2020-11-24 stsp static void
9579 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9580 a5d43cac 2022-07-01 thomas {
9581 a5d43cac 2022-07-01 thomas switch (view->type) {
9582 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9583 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9584 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9585 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9586 a5d43cac 2022-07-01 thomas 1);
9587 a5d43cac 2022-07-01 thomas break;
9588 a5d43cac 2022-07-01 thomas }
9589 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9590 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9591 a5d43cac 2022-07-01 thomas else
9592 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9593 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9594 a5d43cac 2022-07-01 thomas break;
9595 a5d43cac 2022-07-01 thomas }
9596 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9597 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9598 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9599 a5d43cac 2022-07-01 thomas break;
9600 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9601 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9602 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9603 a5d43cac 2022-07-01 thomas break;
9604 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9605 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9606 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9607 a5d43cac 2022-07-01 thomas break;
9608 a5d43cac 2022-07-01 thomas default:
9609 a5d43cac 2022-07-01 thomas break;
9610 a5d43cac 2022-07-01 thomas }
9611 a5d43cac 2022-07-01 thomas
9612 a5d43cac 2022-07-01 thomas view->offset = 0;
9613 a5d43cac 2022-07-01 thomas }
9614 a5d43cac 2022-07-01 thomas
9615 a5d43cac 2022-07-01 thomas /*
9616 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9617 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9618 a5d43cac 2022-07-01 thomas */
9619 a5d43cac 2022-07-01 thomas static const struct got_error *
9620 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9621 a5d43cac 2022-07-01 thomas {
9622 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9623 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9624 a5d43cac 2022-07-01 thomas int *selected = NULL;
9625 a5d43cac 2022-07-01 thomas int header, offset;
9626 a5d43cac 2022-07-01 thomas
9627 a5d43cac 2022-07-01 thomas switch (view->type) {
9628 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9629 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9630 a5d43cac 2022-07-01 thomas header = 3;
9631 a5d43cac 2022-07-01 thomas scrolld = NULL;
9632 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9633 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9634 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9635 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9636 a5d43cac 2022-07-01 thomas view->offset = offset;
9637 a5d43cac 2022-07-01 thomas }
9638 a5d43cac 2022-07-01 thomas break;
9639 a5d43cac 2022-07-01 thomas }
9640 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9641 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9642 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9643 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9644 a5d43cac 2022-07-01 thomas selected = &s->selected;
9645 a5d43cac 2022-07-01 thomas break;
9646 a5d43cac 2022-07-01 thomas }
9647 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9648 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9649 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9650 a5d43cac 2022-07-01 thomas header = 3;
9651 a5d43cac 2022-07-01 thomas selected = &s->selected;
9652 a5d43cac 2022-07-01 thomas break;
9653 a5d43cac 2022-07-01 thomas }
9654 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9655 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9656 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9657 a5d43cac 2022-07-01 thomas header = 5;
9658 a5d43cac 2022-07-01 thomas selected = &s->selected;
9659 a5d43cac 2022-07-01 thomas break;
9660 a5d43cac 2022-07-01 thomas }
9661 a5d43cac 2022-07-01 thomas default:
9662 a5d43cac 2022-07-01 thomas selected = NULL;
9663 a5d43cac 2022-07-01 thomas scrolld = NULL;
9664 a5d43cac 2022-07-01 thomas header = 0;
9665 a5d43cac 2022-07-01 thomas break;
9666 a5d43cac 2022-07-01 thomas }
9667 a5d43cac 2022-07-01 thomas
9668 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9669 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9670 a5d43cac 2022-07-01 thomas view->offset = offset;
9671 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9672 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9673 a5d43cac 2022-07-01 thomas *selected -= offset;
9674 a5d43cac 2022-07-01 thomas }
9675 a5d43cac 2022-07-01 thomas }
9676 a5d43cac 2022-07-01 thomas
9677 a5d43cac 2022-07-01 thomas return err;
9678 a5d43cac 2022-07-01 thomas }
9679 a5d43cac 2022-07-01 thomas
9680 a5d43cac 2022-07-01 thomas static void
9681 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9682 ce5b7c56 2019-07-09 stsp {
9683 6059809a 2020-12-17 stsp size_t i;
9684 9f7d7167 2018-04-29 stsp
9685 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9686 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9687 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9688 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9689 ce5b7c56 2019-07-09 stsp }
9690 6879ba42 2020-10-01 naddy fputc('\n', fp);
9691 ce5b7c56 2019-07-09 stsp }
9692 ce5b7c56 2019-07-09 stsp
9693 4ed7e80c 2018-05-20 stsp __dead static void
9694 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9695 9f7d7167 2018-04-29 stsp {
9696 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9697 6879ba42 2020-10-01 naddy
9698 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9699 6879ba42 2020-10-01 naddy getprogname());
9700 6879ba42 2020-10-01 naddy if (hflag) {
9701 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9702 6879ba42 2020-10-01 naddy list_commands(fp);
9703 ee85c5e8 2020-02-29 stsp }
9704 6879ba42 2020-10-01 naddy exit(status);
9705 9f7d7167 2018-04-29 stsp }
9706 9f7d7167 2018-04-29 stsp
9707 c2301be8 2018-04-30 stsp static char **
9708 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9709 c2301be8 2018-04-30 stsp {
9710 ee85c5e8 2020-02-29 stsp va_list ap;
9711 c2301be8 2018-04-30 stsp char **argv;
9712 ee85c5e8 2020-02-29 stsp int i;
9713 c2301be8 2018-04-30 stsp
9714 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9715 ee85c5e8 2020-02-29 stsp
9716 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9717 c2301be8 2018-04-30 stsp if (argv == NULL)
9718 c2301be8 2018-04-30 stsp err(1, "calloc");
9719 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9720 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9721 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9722 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9723 c2301be8 2018-04-30 stsp }
9724 c2301be8 2018-04-30 stsp
9725 ee85c5e8 2020-02-29 stsp va_end(ap);
9726 c2301be8 2018-04-30 stsp return argv;
9727 ee85c5e8 2020-02-29 stsp }
9728 ee85c5e8 2020-02-29 stsp
9729 ee85c5e8 2020-02-29 stsp /*
9730 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9731 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9732 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9733 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9734 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9735 ee85c5e8 2020-02-29 stsp */
9736 ee85c5e8 2020-02-29 stsp static const struct got_error *
9737 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9738 ee85c5e8 2020-02-29 stsp {
9739 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9740 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9741 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9742 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9743 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9744 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9745 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9746 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9747 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9748 84de9106 2020-12-26 stsp
9749 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9750 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9751 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9752 ee85c5e8 2020-02-29 stsp
9753 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9754 7cd52833 2022-06-23 thomas if (error != NULL)
9755 7cd52833 2022-06-23 thomas goto done;
9756 7cd52833 2022-06-23 thomas
9757 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9758 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9759 ee85c5e8 2020-02-29 stsp goto done;
9760 ee85c5e8 2020-02-29 stsp
9761 ee85c5e8 2020-02-29 stsp if (worktree)
9762 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9763 ee85c5e8 2020-02-29 stsp else
9764 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9765 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9766 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9767 ee85c5e8 2020-02-29 stsp goto done;
9768 ee85c5e8 2020-02-29 stsp }
9769 ee85c5e8 2020-02-29 stsp
9770 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9771 ee85c5e8 2020-02-29 stsp if (error != NULL)
9772 ee85c5e8 2020-02-29 stsp goto done;
9773 ee85c5e8 2020-02-29 stsp
9774 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9775 ee85c5e8 2020-02-29 stsp repo, worktree);
9776 ee85c5e8 2020-02-29 stsp if (error)
9777 ee85c5e8 2020-02-29 stsp goto done;
9778 ee85c5e8 2020-02-29 stsp
9779 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9780 84de9106 2020-12-26 stsp if (error)
9781 84de9106 2020-12-26 stsp goto done;
9782 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9783 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9784 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9785 ee85c5e8 2020-02-29 stsp if (error)
9786 ee85c5e8 2020-02-29 stsp goto done;
9787 ee85c5e8 2020-02-29 stsp
9788 ee85c5e8 2020-02-29 stsp if (worktree) {
9789 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9790 ee85c5e8 2020-02-29 stsp worktree = NULL;
9791 ee85c5e8 2020-02-29 stsp }
9792 ee85c5e8 2020-02-29 stsp
9793 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9794 945f9229 2022-04-16 thomas if (error)
9795 945f9229 2022-04-16 thomas goto done;
9796 945f9229 2022-04-16 thomas
9797 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9798 ee85c5e8 2020-02-29 stsp if (error) {
9799 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9800 ee85c5e8 2020-02-29 stsp goto done;
9801 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9802 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9803 6879ba42 2020-10-01 naddy usage(1, 1);
9804 ee85c5e8 2020-02-29 stsp /* not reached */
9805 ee85c5e8 2020-02-29 stsp }
9806 ee85c5e8 2020-02-29 stsp
9807 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9808 ee85c5e8 2020-02-29 stsp if (error)
9809 ee85c5e8 2020-02-29 stsp goto done;
9810 ee85c5e8 2020-02-29 stsp
9811 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9812 ee85c5e8 2020-02-29 stsp argc = 4;
9813 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9814 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9815 ee85c5e8 2020-02-29 stsp done:
9816 1d0f4054 2021-06-17 stsp if (repo) {
9817 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9818 1d0f4054 2021-06-17 stsp if (error == NULL)
9819 1d0f4054 2021-06-17 stsp error = close_err;
9820 1d0f4054 2021-06-17 stsp }
9821 945f9229 2022-04-16 thomas if (commit)
9822 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9823 ee85c5e8 2020-02-29 stsp if (worktree)
9824 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9825 7cd52833 2022-06-23 thomas if (pack_fds) {
9826 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9827 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9828 7cd52833 2022-06-23 thomas if (error == NULL)
9829 7cd52833 2022-06-23 thomas error = pack_err;
9830 7cd52833 2022-06-23 thomas }
9831 ee85c5e8 2020-02-29 stsp free(id);
9832 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9833 ee85c5e8 2020-02-29 stsp free(commit_id);
9834 ee85c5e8 2020-02-29 stsp free(cwd);
9835 ee85c5e8 2020-02-29 stsp free(repo_path);
9836 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9837 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9838 ee85c5e8 2020-02-29 stsp int i;
9839 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9840 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9841 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9842 ee85c5e8 2020-02-29 stsp }
9843 87670572 2020-12-26 naddy tog_free_refs();
9844 ee85c5e8 2020-02-29 stsp return error;
9845 c2301be8 2018-04-30 stsp }
9846 c2301be8 2018-04-30 stsp
9847 9f7d7167 2018-04-29 stsp int
9848 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9849 9f7d7167 2018-04-29 stsp {
9850 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
9851 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9852 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9853 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9854 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9855 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9856 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9857 83cd27f8 2020-01-13 stsp };
9858 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9859 557d3365 2023-04-14 thomas const char *test_script_path;
9860 a0abeae5 2023-02-17 thomas
9861 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9862 a0abeae5 2023-02-17 thomas
9863 557d3365 2023-04-14 thomas /*
9864 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
9865 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
9866 557d3365 2023-04-14 thomas */
9867 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
9868 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
9869 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
9870 557d3365 2023-04-14 thomas if (error) {
9871 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9872 557d3365 2023-04-14 thomas return 1;
9873 557d3365 2023-04-14 thomas }
9874 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
9875 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
9876 557d3365 2023-04-14 thomas
9877 557d3365 2023-04-14 thomas #if !defined(PROFILE)
9878 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9879 a0abeae5 2023-02-17 thomas NULL) == -1)
9880 a0abeae5 2023-02-17 thomas err(1, "pledge");
9881 a0abeae5 2023-02-17 thomas #endif
9882 9f7d7167 2018-04-29 stsp
9883 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9884 9f7d7167 2018-04-29 stsp switch (ch) {
9885 9f7d7167 2018-04-29 stsp case 'h':
9886 9f7d7167 2018-04-29 stsp hflag = 1;
9887 9f7d7167 2018-04-29 stsp break;
9888 53ccebc2 2019-07-30 stsp case 'V':
9889 53ccebc2 2019-07-30 stsp Vflag = 1;
9890 53ccebc2 2019-07-30 stsp break;
9891 9f7d7167 2018-04-29 stsp default:
9892 6879ba42 2020-10-01 naddy usage(hflag, 1);
9893 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9894 9f7d7167 2018-04-29 stsp }
9895 9f7d7167 2018-04-29 stsp }
9896 9f7d7167 2018-04-29 stsp
9897 9f7d7167 2018-04-29 stsp argc -= optind;
9898 9f7d7167 2018-04-29 stsp argv += optind;
9899 9814e6a3 2020-09-27 naddy optind = 1;
9900 c2301be8 2018-04-30 stsp optreset = 1;
9901 9f7d7167 2018-04-29 stsp
9902 53ccebc2 2019-07-30 stsp if (Vflag) {
9903 53ccebc2 2019-07-30 stsp got_version_print_str();
9904 6879ba42 2020-10-01 naddy return 0;
9905 53ccebc2 2019-07-30 stsp }
9906 4010e238 2020-12-04 stsp
9907 c2301be8 2018-04-30 stsp if (argc == 0) {
9908 f29d3e89 2018-06-23 stsp if (hflag)
9909 6879ba42 2020-10-01 naddy usage(hflag, 0);
9910 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9911 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9912 c2301be8 2018-04-30 stsp argc = 1;
9913 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9914 c2301be8 2018-04-30 stsp } else {
9915 6059809a 2020-12-17 stsp size_t i;
9916 9f7d7167 2018-04-29 stsp
9917 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9918 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9919 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9920 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9921 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9922 9f7d7167 2018-04-29 stsp break;
9923 9f7d7167 2018-04-29 stsp }
9924 9f7d7167 2018-04-29 stsp }
9925 ee85c5e8 2020-02-29 stsp }
9926 3642c4c6 2019-07-09 stsp
9927 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9928 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9929 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9930 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9931 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9932 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9933 adf4c9e0 2022-07-03 thomas }
9934 adf4c9e0 2022-07-03 thomas
9935 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9936 ee85c5e8 2020-02-29 stsp if (argc != 1)
9937 6879ba42 2020-10-01 naddy usage(0, 1);
9938 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9939 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9940 ee85c5e8 2020-02-29 stsp } else {
9941 ee85c5e8 2020-02-29 stsp if (hflag)
9942 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9943 ee85c5e8 2020-02-29 stsp else
9944 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9945 9f7d7167 2018-04-29 stsp }
9946 9f7d7167 2018-04-29 stsp
9947 1d98034b 2023-04-14 thomas if (using_mock_io) {
9948 1d98034b 2023-04-14 thomas io_err = tog_io_close();
9949 1d98034b 2023-04-14 thomas if (error == NULL)
9950 1d98034b 2023-04-14 thomas error = io_err;
9951 1d98034b 2023-04-14 thomas }
9952 9f7d7167 2018-04-29 stsp endwin();
9953 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9954 a2f4a359 2020-02-28 stsp int i;
9955 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9956 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9957 a2f4a359 2020-02-28 stsp free(cmd_argv);
9958 a2f4a359 2020-02-28 stsp }
9959 a2f4a359 2020-02-28 stsp
9960 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9961 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9962 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9963 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9964 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9965 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9966 9f7d7167 2018-04-29 stsp return 0;
9967 9f7d7167 2018-04-29 stsp }