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 66b04f8f 2023-07-19 thomas #include "got_keyword.h"
62 9f7d7167 2018-04-29 stsp
63 881b2d3e 2018-04-30 stsp #ifndef MIN
64 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 881b2d3e 2018-04-30 stsp #endif
66 881b2d3e 2018-04-30 stsp
67 2bd27830 2018-10-22 stsp #ifndef MAX
68 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 2bd27830 2018-10-22 stsp #endif
70 2bd27830 2018-10-22 stsp
71 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
72 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
73 acf52a76 2021-09-21 thomas.ad #endif
74 2bd27830 2018-10-22 stsp
75 9f7d7167 2018-04-29 stsp #ifndef nitems
76 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 9f7d7167 2018-04-29 stsp #endif
78 9f7d7167 2018-04-29 stsp
79 9f7d7167 2018-04-29 stsp struct tog_cmd {
80 c2301be8 2018-04-30 stsp const char *name;
81 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
83 9f7d7167 2018-04-29 stsp };
84 9f7d7167 2018-04-29 stsp
85 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
89 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
90 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
91 9f7d7167 2018-04-29 stsp
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
95 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
96 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
97 9f7d7167 2018-04-29 stsp
98 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
99 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
100 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
101 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
102 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
103 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
104 9f7d7167 2018-04-29 stsp };
105 9f7d7167 2018-04-29 stsp
106 d6b05b5b 2018-08-04 stsp enum tog_view_type {
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
109 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
110 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
111 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
112 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
113 fc2737d5 2022-09-15 thomas };
114 fc2737d5 2022-09-15 thomas
115 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
116 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
124 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
125 d6b05b5b 2018-08-04 stsp };
126 c3e9aa98 2019-05-13 jcs
127 a5d43cac 2022-07-01 thomas enum tog_view_mode {
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
130 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
131 a5d43cac 2022-07-01 thomas };
132 a5d43cac 2022-07-01 thomas
133 87a675e0 2023-04-22 thomas #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
134 a5d43cac 2022-07-01 thomas
135 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
136 d6b05b5b 2018-08-04 stsp
137 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
138 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
139 ba4f502b 2018-08-04 stsp struct got_object_id *id;
140 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
141 1a76625f 2018-10-22 stsp int idx;
142 ba4f502b 2018-08-04 stsp };
143 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
144 ba4f502b 2018-08-04 stsp struct commit_queue {
145 ba4f502b 2018-08-04 stsp int ncommits;
146 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
147 6d17833f 2019-11-08 stsp };
148 6d17833f 2019-11-08 stsp
149 f26dddb7 2019-11-08 stsp struct tog_color {
150 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
151 6d17833f 2019-11-08 stsp regex_t regex;
152 6d17833f 2019-11-08 stsp short colorpair;
153 15a087fe 2019-02-21 stsp };
154 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
155 11b20872 2019-11-08 stsp
156 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
157 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
158 349dfd1e 2023-07-23 thomas static struct {
159 349dfd1e 2023-07-23 thomas struct got_object_id *id;
160 349dfd1e 2023-07-23 thomas int idx;
161 349dfd1e 2023-07-23 thomas char marker;
162 349dfd1e 2023-07-23 thomas } tog_base_commit;
163 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas static const struct got_error *
166 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
167 2183bbf6 2022-01-23 thomas struct got_reference* re2)
168 2183bbf6 2022-01-23 thomas {
169 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
170 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
171 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
172 2183bbf6 2022-01-23 thomas
173 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
174 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
175 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
176 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
177 2183bbf6 2022-01-23 thomas *cmp = -1;
178 2183bbf6 2022-01-23 thomas return NULL;
179 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
180 2183bbf6 2022-01-23 thomas *cmp = 1;
181 2183bbf6 2022-01-23 thomas return NULL;
182 2183bbf6 2022-01-23 thomas }
183 2183bbf6 2022-01-23 thomas
184 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
185 2183bbf6 2022-01-23 thomas return NULL;
186 2183bbf6 2022-01-23 thomas }
187 51a10b52 2020-12-26 stsp
188 11b20872 2019-11-08 stsp static const struct got_error *
189 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
190 51a10b52 2020-12-26 stsp {
191 51a10b52 2020-12-26 stsp const struct got_error *err;
192 51a10b52 2020-12-26 stsp
193 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
194 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
195 3bfadbd4 2021-11-20 thomas repo);
196 51a10b52 2020-12-26 stsp if (err)
197 51a10b52 2020-12-26 stsp return err;
198 51a10b52 2020-12-26 stsp
199 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
200 51a10b52 2020-12-26 stsp repo);
201 51a10b52 2020-12-26 stsp }
202 51a10b52 2020-12-26 stsp
203 51a10b52 2020-12-26 stsp static void
204 51a10b52 2020-12-26 stsp tog_free_refs(void)
205 51a10b52 2020-12-26 stsp {
206 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
207 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
208 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
209 51a10b52 2020-12-26 stsp }
210 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
211 51a10b52 2020-12-26 stsp }
212 51a10b52 2020-12-26 stsp
213 51a10b52 2020-12-26 stsp static const struct got_error *
214 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
215 11b20872 2019-11-08 stsp int idx, short color)
216 11b20872 2019-11-08 stsp {
217 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
218 11b20872 2019-11-08 stsp struct tog_color *tc;
219 11b20872 2019-11-08 stsp int regerr = 0;
220 11b20872 2019-11-08 stsp
221 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
222 11b20872 2019-11-08 stsp return NULL;
223 11b20872 2019-11-08 stsp
224 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
225 11b20872 2019-11-08 stsp
226 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
227 11b20872 2019-11-08 stsp if (tc == NULL)
228 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
229 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
230 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
231 11b20872 2019-11-08 stsp if (regerr) {
232 11b20872 2019-11-08 stsp static char regerr_msg[512];
233 11b20872 2019-11-08 stsp static char err_msg[512];
234 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
235 11b20872 2019-11-08 stsp sizeof(regerr_msg));
236 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
237 11b20872 2019-11-08 stsp regerr_msg);
238 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
239 11b20872 2019-11-08 stsp free(tc);
240 11b20872 2019-11-08 stsp return err;
241 11b20872 2019-11-08 stsp }
242 11b20872 2019-11-08 stsp tc->colorpair = idx;
243 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
244 11b20872 2019-11-08 stsp return NULL;
245 11b20872 2019-11-08 stsp }
246 11b20872 2019-11-08 stsp
247 11b20872 2019-11-08 stsp static void
248 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
249 11b20872 2019-11-08 stsp {
250 11b20872 2019-11-08 stsp struct tog_color *tc;
251 11b20872 2019-11-08 stsp
252 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
253 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
254 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
255 11b20872 2019-11-08 stsp regfree(&tc->regex);
256 11b20872 2019-11-08 stsp free(tc);
257 11b20872 2019-11-08 stsp }
258 11b20872 2019-11-08 stsp }
259 11b20872 2019-11-08 stsp
260 ef20f542 2022-06-26 thomas static struct tog_color *
261 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
262 11b20872 2019-11-08 stsp {
263 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
264 11b20872 2019-11-08 stsp
265 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
266 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
267 11b20872 2019-11-08 stsp return tc;
268 11b20872 2019-11-08 stsp }
269 11b20872 2019-11-08 stsp
270 11b20872 2019-11-08 stsp return NULL;
271 11b20872 2019-11-08 stsp }
272 11b20872 2019-11-08 stsp
273 11b20872 2019-11-08 stsp static int
274 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
275 11b20872 2019-11-08 stsp {
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
277 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
279 11b20872 2019-11-08 stsp return COLOR_CYAN;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
281 11b20872 2019-11-08 stsp return COLOR_YELLOW;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
283 11b20872 2019-11-08 stsp return COLOR_GREEN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
287 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
289 91b8c405 2020-01-25 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_GREEN;
292 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
293 11b20872 2019-11-08 stsp return COLOR_GREEN;
294 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
295 11b20872 2019-11-08 stsp return COLOR_CYAN;
296 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
297 11b20872 2019-11-08 stsp return COLOR_YELLOW;
298 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
299 6458efa5 2020-11-24 stsp return COLOR_GREEN;
300 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
301 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
302 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
303 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
304 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
305 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
306 11b20872 2019-11-08 stsp
307 11b20872 2019-11-08 stsp return -1;
308 11b20872 2019-11-08 stsp }
309 11b20872 2019-11-08 stsp
310 11b20872 2019-11-08 stsp static int
311 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
312 11b20872 2019-11-08 stsp {
313 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
314 11b20872 2019-11-08 stsp
315 11b20872 2019-11-08 stsp if (val == NULL)
316 11b20872 2019-11-08 stsp return default_color_value(envvar);
317 15a087fe 2019-02-21 stsp
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
319 11b20872 2019-11-08 stsp return COLOR_BLACK;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
321 11b20872 2019-11-08 stsp return COLOR_RED;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
323 11b20872 2019-11-08 stsp return COLOR_GREEN;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
325 11b20872 2019-11-08 stsp return COLOR_YELLOW;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
327 11b20872 2019-11-08 stsp return COLOR_BLUE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
329 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
330 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
331 11b20872 2019-11-08 stsp return COLOR_CYAN;
332 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
333 11b20872 2019-11-08 stsp return COLOR_WHITE;
334 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
335 11b20872 2019-11-08 stsp return -1;
336 11b20872 2019-11-08 stsp
337 11b20872 2019-11-08 stsp return default_color_value(envvar);
338 11b20872 2019-11-08 stsp }
339 11b20872 2019-11-08 stsp
340 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
341 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
342 3dbaef42 2020-11-24 stsp const char *label1, *label2;
343 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
344 19a6a6b5 2022-07-01 thomas int fd1, fd2;
345 82c78e96 2022-08-06 thomas int lineno;
346 15a087fe 2019-02-21 stsp int first_displayed_line;
347 15a087fe 2019-02-21 stsp int last_displayed_line;
348 15a087fe 2019-02-21 stsp int eof;
349 15a087fe 2019-02-21 stsp int diff_context;
350 3dbaef42 2020-11-24 stsp int ignore_whitespace;
351 64453f7e 2020-11-21 stsp int force_text_diff;
352 15a087fe 2019-02-21 stsp struct got_repository *repo;
353 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
354 fe621944 2020-11-10 stsp size_t nlines;
355 f44b1f58 2020-02-02 tracey int matched_line;
356 f44b1f58 2020-02-02 tracey int selected_line;
357 15a087fe 2019-02-21 stsp
358 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
359 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
360 b01e7d3b 2018-08-04 stsp };
361 b01e7d3b 2018-08-04 stsp
362 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
363 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
364 1a76625f 2018-10-22 stsp
365 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
366 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
367 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
368 1a76625f 2018-10-22 stsp int commits_needed;
369 fb280deb 2021-08-30 stsp int load_all;
370 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
371 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
372 1a76625f 2018-10-22 stsp const char *in_repo_path;
373 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
374 1a76625f 2018-10-22 stsp struct got_repository *repo;
375 1af5eddf 2022-06-23 thomas int *pack_fds;
376 1a76625f 2018-10-22 stsp int log_complete;
377 92845f09 2023-07-26 thomas pthread_cond_t log_loaded;
378 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
379 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
380 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
381 13add988 2019-10-15 stsp int *searching;
382 13add988 2019-10-15 stsp int *search_next_done;
383 13add988 2019-10-15 stsp regex_t *regex;
384 7e8004ba 2022-09-11 thomas int *limiting;
385 7e8004ba 2022-09-11 thomas int limit_match;
386 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
387 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
388 92845f09 2023-07-26 thomas struct got_worktree *worktree;
389 92845f09 2023-07-26 thomas int need_commit_marker;
390 1a76625f 2018-10-22 stsp };
391 1a76625f 2018-10-22 stsp
392 1a76625f 2018-10-22 stsp struct tog_log_view_state {
393 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
394 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
395 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
396 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
397 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
398 b01e7d3b 2018-08-04 stsp int selected;
399 b01e7d3b 2018-08-04 stsp char *in_repo_path;
400 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
401 b672a97a 2020-01-27 stsp int log_branches;
402 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
403 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
404 1a76625f 2018-10-22 stsp sig_atomic_t quit;
405 1a76625f 2018-10-22 stsp pthread_t thread;
406 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
407 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
408 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
409 11b20872 2019-11-08 stsp struct tog_colors colors;
410 f69c5a46 2022-07-19 thomas int use_committer;
411 7e8004ba 2022-09-11 thomas int limit_view;
412 7e8004ba 2022-09-11 thomas regex_t limit_regex;
413 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
414 ba4f502b 2018-08-04 stsp };
415 11b20872 2019-11-08 stsp
416 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
417 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
418 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
419 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
420 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
421 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
422 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
423 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
424 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
425 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
426 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
427 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
428 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
429 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
430 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
431 ba4f502b 2018-08-04 stsp
432 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
433 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
434 e9424729 2018-08-04 stsp int nlines;
435 e9424729 2018-08-04 stsp
436 e9424729 2018-08-04 stsp struct tog_view *view;
437 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
438 e9424729 2018-08-04 stsp int *quit;
439 e9424729 2018-08-04 stsp };
440 e9424729 2018-08-04 stsp
441 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
442 e9424729 2018-08-04 stsp const char *path;
443 e9424729 2018-08-04 stsp struct got_repository *repo;
444 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
445 e9424729 2018-08-04 stsp int *complete;
446 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
447 fc06ba56 2019-08-22 stsp void *cancel_arg;
448 1134ebde 2023-04-22 thomas pthread_cond_t blame_complete;
449 e9424729 2018-08-04 stsp };
450 e9424729 2018-08-04 stsp
451 e9424729 2018-08-04 stsp struct tog_blame {
452 e9424729 2018-08-04 stsp FILE *f;
453 be659d10 2020-11-18 stsp off_t filesize;
454 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
455 6fcac457 2018-11-19 stsp int nlines;
456 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
457 e9424729 2018-08-04 stsp pthread_t thread;
458 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
459 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
460 e9424729 2018-08-04 stsp const char *path;
461 7cd52833 2022-06-23 thomas int *pack_fds;
462 e9424729 2018-08-04 stsp };
463 e9424729 2018-08-04 stsp
464 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
465 7cbe629d 2018-08-04 stsp int first_displayed_line;
466 7cbe629d 2018-08-04 stsp int last_displayed_line;
467 7cbe629d 2018-08-04 stsp int selected_line;
468 4fc71f3b 2022-07-12 thomas int last_diffed_line;
469 7cbe629d 2018-08-04 stsp int blame_complete;
470 e5a0f69f 2018-08-18 stsp int eof;
471 e5a0f69f 2018-08-18 stsp int done;
472 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
473 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
474 e5a0f69f 2018-08-18 stsp char *path;
475 7cbe629d 2018-08-04 stsp struct got_repository *repo;
476 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
477 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
478 e9424729 2018-08-04 stsp struct tog_blame blame;
479 6c4c42e0 2019-06-24 stsp int matched_line;
480 11b20872 2019-11-08 stsp struct tog_colors colors;
481 ad80ab7b 2018-08-04 stsp };
482 ad80ab7b 2018-08-04 stsp
483 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
484 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
486 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
487 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
488 ad80ab7b 2018-08-04 stsp int selected;
489 ad80ab7b 2018-08-04 stsp };
490 ad80ab7b 2018-08-04 stsp
491 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
492 ad80ab7b 2018-08-04 stsp
493 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
494 ad80ab7b 2018-08-04 stsp char *tree_label;
495 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
496 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
497 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
498 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
499 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
500 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
501 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
502 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
503 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
504 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
505 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
506 6458efa5 2020-11-24 stsp struct tog_colors colors;
507 6458efa5 2020-11-24 stsp };
508 6458efa5 2020-11-24 stsp
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
510 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
511 6458efa5 2020-11-24 stsp struct got_reference *ref;
512 6458efa5 2020-11-24 stsp int idx;
513 6458efa5 2020-11-24 stsp };
514 6458efa5 2020-11-24 stsp
515 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
516 6458efa5 2020-11-24 stsp
517 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
518 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
519 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
520 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
521 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
522 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
523 6458efa5 2020-11-24 stsp struct got_repository *repo;
524 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
525 bddb1296 2019-11-08 stsp struct tog_colors colors;
526 fc2737d5 2022-09-15 thomas };
527 fc2737d5 2022-09-15 thomas
528 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
529 fc2737d5 2022-09-15 thomas FILE *f;
530 fc2737d5 2022-09-15 thomas off_t *line_offsets;
531 fc2737d5 2022-09-15 thomas size_t nlines;
532 fc2737d5 2022-09-15 thomas int lineno;
533 fc2737d5 2022-09-15 thomas int first_displayed_line;
534 fc2737d5 2022-09-15 thomas int last_displayed_line;
535 fc2737d5 2022-09-15 thomas int eof;
536 fc2737d5 2022-09-15 thomas int matched_line;
537 fc2737d5 2022-09-15 thomas int selected_line;
538 fc2737d5 2022-09-15 thomas int all;
539 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
540 fc2737d5 2022-09-15 thomas };
541 fc2737d5 2022-09-15 thomas
542 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
543 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
544 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
545 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
546 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
547 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
548 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
549 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
550 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
551 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
552 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
553 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
554 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
555 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
556 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
557 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
558 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
559 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
560 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
561 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
562 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
563 c282f787 2023-04-22 thomas KEY_("S", "Switch split-screen layout"), \
564 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
565 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
566 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
567 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
568 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
569 fc2737d5 2022-09-15 thomas \
570 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
571 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
572 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
573 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
574 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
575 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
576 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
577 fc2737d5 2022-09-15 thomas " commit"), \
578 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
579 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
580 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
581 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
582 fc2737d5 2022-09-15 thomas \
583 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
584 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
585 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
586 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
587 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
588 fc2737d5 2022-09-15 thomas " data"), \
589 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
590 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
591 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
592 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
593 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
594 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
595 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
596 fc2737d5 2022-09-15 thomas \
597 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
598 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
599 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
600 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
601 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
602 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
603 fc2737d5 2022-09-15 thomas " selected line's commit"), \
604 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
605 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
606 fc2737d5 2022-09-15 thomas \
607 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
608 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
609 fc2737d5 2022-09-15 thomas " selected file"), \
610 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
611 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
612 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
613 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
614 fc2737d5 2022-09-15 thomas \
615 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
616 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
617 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
618 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
619 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
620 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
621 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
622 fc2737d5 2022-09-15 thomas
623 fc2737d5 2022-09-15 thomas struct tog_key_map {
624 fc2737d5 2022-09-15 thomas const char *keys;
625 fc2737d5 2022-09-15 thomas const char *info;
626 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
627 7cbe629d 2018-08-04 stsp };
628 7cbe629d 2018-08-04 stsp
629 b85a3496 2023-04-14 thomas /* curses io for tog regress */
630 b85a3496 2023-04-14 thomas struct tog_io {
631 b85a3496 2023-04-14 thomas FILE *cin;
632 b85a3496 2023-04-14 thomas FILE *cout;
633 b85a3496 2023-04-14 thomas FILE *f;
634 5b3a801d 2023-04-22 thomas FILE *sdump;
635 8e778ade 2023-04-22 thomas int wait_for_ui;
636 557d3365 2023-04-14 thomas } tog_io;
637 557d3365 2023-04-14 thomas static int using_mock_io;
638 b85a3496 2023-04-14 thomas
639 13bc6832 2023-04-22 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
640 b85a3496 2023-04-14 thomas
641 669b5ffa 2018-10-07 stsp /*
642 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
643 669b5ffa 2018-10-07 stsp *
644 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
645 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
646 669b5ffa 2018-10-07 stsp * there is enough screen estate.
647 669b5ffa 2018-10-07 stsp *
648 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
649 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
650 669b5ffa 2018-10-07 stsp *
651 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
652 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
653 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
654 669b5ffa 2018-10-07 stsp *
655 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
656 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
657 669b5ffa 2018-10-07 stsp */
658 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
659 669b5ffa 2018-10-07 stsp
660 cc3c9aac 2018-08-01 stsp struct tog_view {
661 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
662 26ed57b2 2018-05-19 stsp WINDOW *window;
663 26ed57b2 2018-05-19 stsp PANEL *panel;
664 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
665 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
666 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
667 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
668 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
669 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
670 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
671 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
672 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
673 9970f7fc 2020-12-03 stsp int dying;
674 669b5ffa 2018-10-07 stsp struct tog_view *parent;
675 669b5ffa 2018-10-07 stsp struct tog_view *child;
676 5dc9f4bc 2018-08-04 stsp
677 e78dc838 2020-12-04 stsp /*
678 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
679 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
680 e78dc838 2020-12-04 stsp * between parent and child.
681 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
682 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
683 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
684 e78dc838 2020-12-04 stsp * situations.
685 e78dc838 2020-12-04 stsp */
686 e78dc838 2020-12-04 stsp int focus_child;
687 e78dc838 2020-12-04 stsp
688 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
689 5dc9f4bc 2018-08-04 stsp /* type-specific state */
690 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
691 5dc9f4bc 2018-08-04 stsp union {
692 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
693 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
694 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
695 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
696 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
697 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
698 5dc9f4bc 2018-08-04 stsp } state;
699 e5a0f69f 2018-08-18 stsp
700 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
701 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
702 e78dc838 2020-12-04 stsp struct tog_view *, int);
703 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
704 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
705 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
706 60493ae3 2019-06-20 stsp
707 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
708 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
709 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
710 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
711 c0c4acc8 2021-01-24 stsp int search_started;
712 60493ae3 2019-06-20 stsp int searching;
713 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
714 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
715 60493ae3 2019-06-20 stsp int search_next_done;
716 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
717 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
718 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
719 1803e47f 2019-06-22 stsp regex_t regex;
720 41605754 2020-11-12 stsp regmatch_t regmatch;
721 f2d06bef 2023-01-23 thomas const char *action;
722 cc3c9aac 2018-08-01 stsp };
723 cd0acaa7 2018-05-20 stsp
724 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
725 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
726 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
727 78756c87 2020-11-24 stsp struct got_repository *);
728 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
730 e78dc838 2020-12-04 stsp struct tog_view *, int);
731 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
732 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
733 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
734 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
735 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
736 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
737 e5a0f69f 2018-08-18 stsp
738 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
739 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
740 92845f09 2023-07-26 thomas const char *, const char *, int, struct got_worktree *);
741 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
742 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
743 e78dc838 2020-12-04 stsp struct tog_view *, int);
744 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
745 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
746 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
747 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
748 e5a0f69f 2018-08-18 stsp
749 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
750 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
751 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
752 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
753 e78dc838 2020-12-04 stsp struct tog_view *, int);
754 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
755 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
756 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
757 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
758 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
759 e5a0f69f 2018-08-18 stsp
760 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
761 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
762 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
763 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
764 e78dc838 2020-12-04 stsp struct tog_view *, int);
765 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
766 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
767 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
768 6458efa5 2020-11-24 stsp
769 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
770 6458efa5 2020-11-24 stsp struct got_repository *);
771 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
772 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
773 e78dc838 2020-12-04 stsp struct tog_view *, int);
774 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
775 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
776 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
777 2a7d3cd7 2022-09-17 thomas
778 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
779 2a7d3cd7 2022-09-17 thomas struct tog_view *);
780 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
781 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
782 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
783 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
784 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
785 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
786 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
787 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
788 25791caa 2018-10-24 stsp
789 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
790 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
791 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
792 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
793 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
794 25791caa 2018-10-24 stsp
795 25791caa 2018-10-24 stsp static void
796 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
797 25791caa 2018-10-24 stsp {
798 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
799 83baff54 2019-08-12 stsp }
800 83baff54 2019-08-12 stsp
801 83baff54 2019-08-12 stsp static void
802 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
803 83baff54 2019-08-12 stsp {
804 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
805 25791caa 2018-10-24 stsp }
806 26ed57b2 2018-05-19 stsp
807 61266923 2020-01-14 stsp static void
808 61266923 2020-01-14 stsp tog_sigcont(int signo)
809 61266923 2020-01-14 stsp {
810 61266923 2020-01-14 stsp tog_sigcont_received = 1;
811 61266923 2020-01-14 stsp }
812 61266923 2020-01-14 stsp
813 296152d1 2022-05-31 thomas static void
814 296152d1 2022-05-31 thomas tog_sigint(int signo)
815 296152d1 2022-05-31 thomas {
816 296152d1 2022-05-31 thomas tog_sigint_received = 1;
817 296152d1 2022-05-31 thomas }
818 296152d1 2022-05-31 thomas
819 296152d1 2022-05-31 thomas static void
820 296152d1 2022-05-31 thomas tog_sigterm(int signo)
821 296152d1 2022-05-31 thomas {
822 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
823 296152d1 2022-05-31 thomas }
824 296152d1 2022-05-31 thomas
825 296152d1 2022-05-31 thomas static int
826 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
827 296152d1 2022-05-31 thomas {
828 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
829 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
830 296152d1 2022-05-31 thomas }
831 296152d1 2022-05-31 thomas
832 e5a0f69f 2018-08-18 stsp static const struct got_error *
833 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
834 ea5e7bb5 2018-08-01 stsp {
835 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
836 e5a0f69f 2018-08-18 stsp
837 669b5ffa 2018-10-07 stsp if (view->child) {
838 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
839 669b5ffa 2018-10-07 stsp view->child = NULL;
840 669b5ffa 2018-10-07 stsp }
841 e5a0f69f 2018-08-18 stsp if (view->close)
842 e5a0f69f 2018-08-18 stsp err = view->close(view);
843 ea5e7bb5 2018-08-01 stsp if (view->panel)
844 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
845 ea5e7bb5 2018-08-01 stsp if (view->window)
846 ea5e7bb5 2018-08-01 stsp delwin(view->window);
847 ea5e7bb5 2018-08-01 stsp free(view);
848 f2d749db 2022-07-12 thomas return err ? err : child_err;
849 ea5e7bb5 2018-08-01 stsp }
850 ea5e7bb5 2018-08-01 stsp
851 ea5e7bb5 2018-08-01 stsp static struct tog_view *
852 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
853 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
854 ea5e7bb5 2018-08-01 stsp {
855 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
856 ea5e7bb5 2018-08-01 stsp
857 ea5e7bb5 2018-08-01 stsp if (view == NULL)
858 ea5e7bb5 2018-08-01 stsp return NULL;
859 ea5e7bb5 2018-08-01 stsp
860 d6b05b5b 2018-08-04 stsp view->type = type;
861 f7d12f7e 2018-08-01 stsp view->lines = LINES;
862 f7d12f7e 2018-08-01 stsp view->cols = COLS;
863 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
864 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
865 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
866 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
867 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
868 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
869 96a765a8 2018-08-04 stsp view_close(view);
870 ea5e7bb5 2018-08-01 stsp return NULL;
871 ea5e7bb5 2018-08-01 stsp }
872 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
873 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
874 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
875 96a765a8 2018-08-04 stsp view_close(view);
876 ea5e7bb5 2018-08-01 stsp return NULL;
877 ea5e7bb5 2018-08-01 stsp }
878 ea5e7bb5 2018-08-01 stsp
879 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
880 ea5e7bb5 2018-08-01 stsp return view;
881 cdf1ee82 2018-08-01 stsp }
882 cdf1ee82 2018-08-01 stsp
883 0cf4efb1 2018-09-29 stsp static int
884 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
885 0cf4efb1 2018-09-29 stsp {
886 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
887 0cf4efb1 2018-09-29 stsp return 0;
888 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
889 5c60c32a 2018-10-18 stsp }
890 5c60c32a 2018-10-18 stsp
891 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
892 a5d43cac 2022-07-01 thomas static int
893 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
894 a5d43cac 2022-07-01 thomas {
895 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
896 a5d43cac 2022-07-01 thomas }
897 a5d43cac 2022-07-01 thomas
898 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
899 5c60c32a 2018-10-18 stsp
900 5c60c32a 2018-10-18 stsp static const struct got_error *
901 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
902 5c60c32a 2018-10-18 stsp {
903 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
904 5c60c32a 2018-10-18 stsp
905 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
906 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
907 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
908 53d2bdd3 2022-07-10 thomas else
909 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
910 a5d43cac 2022-07-01 thomas view->begin_x = 0;
911 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
912 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
913 53d2bdd3 2022-07-10 thomas view->cols > 119)
914 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
915 53d2bdd3 2022-07-10 thomas else
916 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
917 a5d43cac 2022-07-01 thomas view->begin_y = 0;
918 a5d43cac 2022-07-01 thomas }
919 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
920 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
921 5c60c32a 2018-10-18 stsp view->lines = LINES;
922 5c60c32a 2018-10-18 stsp view->cols = COLS;
923 5c60c32a 2018-10-18 stsp err = view_resize(view);
924 5c60c32a 2018-10-18 stsp if (err)
925 5c60c32a 2018-10-18 stsp return err;
926 5c60c32a 2018-10-18 stsp
927 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
928 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
929 a5d43cac 2022-07-01 thomas
930 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
931 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
932 5c60c32a 2018-10-18 stsp
933 5c60c32a 2018-10-18 stsp return NULL;
934 5c60c32a 2018-10-18 stsp }
935 5c60c32a 2018-10-18 stsp
936 5c60c32a 2018-10-18 stsp static const struct got_error *
937 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
938 5c60c32a 2018-10-18 stsp {
939 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
940 5c60c32a 2018-10-18 stsp
941 5c60c32a 2018-10-18 stsp view->begin_x = 0;
942 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
943 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
944 5c60c32a 2018-10-18 stsp view->ncols = COLS;
945 5c60c32a 2018-10-18 stsp view->lines = LINES;
946 5c60c32a 2018-10-18 stsp view->cols = COLS;
947 5c60c32a 2018-10-18 stsp err = view_resize(view);
948 5c60c32a 2018-10-18 stsp if (err)
949 5c60c32a 2018-10-18 stsp return err;
950 5c60c32a 2018-10-18 stsp
951 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
952 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
953 5c60c32a 2018-10-18 stsp
954 5c60c32a 2018-10-18 stsp return NULL;
955 0cf4efb1 2018-09-29 stsp }
956 0cf4efb1 2018-09-29 stsp
957 5c60c32a 2018-10-18 stsp static int
958 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
959 5c60c32a 2018-10-18 stsp {
960 5c60c32a 2018-10-18 stsp return view->parent == NULL;
961 5c60c32a 2018-10-18 stsp }
962 5c60c32a 2018-10-18 stsp
963 b65b3ea0 2022-06-23 thomas static int
964 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
965 b65b3ea0 2022-06-23 thomas {
966 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
967 e44940c3 2022-07-01 thomas }
968 e44940c3 2022-07-01 thomas
969 e44940c3 2022-07-01 thomas static int
970 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
971 e44940c3 2022-07-01 thomas {
972 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
973 b65b3ea0 2022-06-23 thomas }
974 b65b3ea0 2022-06-23 thomas
975 444d5325 2022-07-03 thomas static int
976 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
977 444d5325 2022-07-03 thomas {
978 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
979 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
980 444d5325 2022-07-03 thomas }
981 444d5325 2022-07-03 thomas
982 a5d43cac 2022-07-01 thomas static void
983 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
984 a5d43cac 2022-07-01 thomas {
985 a5d43cac 2022-07-01 thomas PANEL *panel;
986 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
987 b65b3ea0 2022-06-23 thomas
988 a5d43cac 2022-07-01 thomas if (view->parent)
989 a5d43cac 2022-07-01 thomas return view_border(view->parent);
990 a5d43cac 2022-07-01 thomas
991 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
992 a5d43cac 2022-07-01 thomas if (panel == NULL)
993 a5d43cac 2022-07-01 thomas return;
994 a5d43cac 2022-07-01 thomas
995 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
996 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
997 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
998 88e8b64f 2023-04-22 thomas view->begin_x, ACS_HLINE, view->ncols);
999 a5d43cac 2022-07-01 thomas else
1000 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1001 88e8b64f 2023-04-22 thomas ACS_VLINE, view->nlines);
1002 a5d43cac 2022-07-01 thomas }
1003 a5d43cac 2022-07-01 thomas
1004 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
1005 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
1006 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
1007 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
1008 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
1009 a5d43cac 2022-07-01 thomas
1010 4d8c2215 2018-08-19 stsp static const struct got_error *
1011 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1012 f7d12f7e 2018-08-01 stsp {
1013 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1014 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1015 f7d12f7e 2018-08-01 stsp
1016 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1017 a5d43cac 2022-07-01 thomas
1018 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1019 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1020 0cf4efb1 2018-09-29 stsp else
1021 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1022 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1023 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1024 0cf4efb1 2018-09-29 stsp else
1025 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1026 6d0fee91 2018-08-01 stsp
1027 4918811f 2022-07-01 thomas if (view->child) {
1028 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1029 a5d43cac 2022-07-01 thomas
1030 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1031 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1032 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1033 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1034 40236d76 2022-06-23 thomas ncols = COLS;
1035 40236d76 2022-06-23 thomas
1036 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1037 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1038 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1039 5c60c32a 2018-10-18 stsp else
1040 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1041 5c60c32a 2018-10-18 stsp } else {
1042 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1043 40236d76 2022-06-23 thomas
1044 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1045 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1046 a5d43cac 2022-07-01 thomas }
1047 a5d43cac 2022-07-01 thomas /*
1048 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1049 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1050 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1051 a5d43cac 2022-07-01 thomas */
1052 a5d43cac 2022-07-01 thomas if (hs) {
1053 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1054 a5d43cac 2022-07-01 thomas if (err)
1055 a5d43cac 2022-07-01 thomas return err;
1056 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1057 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1058 a5d43cac 2022-07-01 thomas if (err)
1059 a5d43cac 2022-07-01 thomas return err;
1060 a5d43cac 2022-07-01 thomas }
1061 a5d43cac 2022-07-01 thomas view_border(view);
1062 a5d43cac 2022-07-01 thomas update_panels();
1063 a5d43cac 2022-07-01 thomas doupdate();
1064 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1065 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1066 a5d43cac 2022-07-01 thomas }
1067 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1068 40236d76 2022-06-23 thomas ncols = COLS;
1069 669b5ffa 2018-10-07 stsp
1070 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1071 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1072 ea0bff04 2022-07-19 thomas if (err)
1073 ea0bff04 2022-07-19 thomas return err;
1074 ea0bff04 2022-07-19 thomas }
1075 ea0bff04 2022-07-19 thomas
1076 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1077 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1078 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1079 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1080 40236d76 2022-06-23 thomas wclear(view->window);
1081 40236d76 2022-06-23 thomas
1082 40236d76 2022-06-23 thomas view->nlines = nlines;
1083 40236d76 2022-06-23 thomas view->ncols = ncols;
1084 40236d76 2022-06-23 thomas view->lines = LINES;
1085 40236d76 2022-06-23 thomas view->cols = COLS;
1086 40236d76 2022-06-23 thomas
1087 5c60c32a 2018-10-18 stsp return NULL;
1088 ea0bff04 2022-07-19 thomas }
1089 ea0bff04 2022-07-19 thomas
1090 ea0bff04 2022-07-19 thomas static const struct got_error *
1091 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1092 ea0bff04 2022-07-19 thomas {
1093 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1094 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1095 3a0139e8 2022-07-22 thomas int n = 0;
1096 ea0bff04 2022-07-19 thomas
1097 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1098 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1099 3a0139e8 2022-07-22 thomas
1100 ea0bff04 2022-07-19 thomas /*
1101 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1102 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1103 ea0bff04 2022-07-19 thomas */
1104 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1105 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1106 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1107 ea0bff04 2022-07-19 thomas }
1108 ea0bff04 2022-07-19 thomas
1109 ea0bff04 2022-07-19 thomas return err;
1110 97cb21cd 2022-07-12 thomas }
1111 97cb21cd 2022-07-12 thomas
1112 97cb21cd 2022-07-12 thomas static void
1113 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1114 97cb21cd 2022-07-12 thomas {
1115 97cb21cd 2022-07-12 thomas if (n == 0)
1116 97cb21cd 2022-07-12 thomas return;
1117 97cb21cd 2022-07-12 thomas
1118 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1119 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1120 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1121 97cb21cd 2022-07-12 thomas else
1122 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1123 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1124 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1125 97cb21cd 2022-07-12 thomas view->offset -= n;
1126 97cb21cd 2022-07-12 thomas else
1127 97cb21cd 2022-07-12 thomas view->offset = 0;
1128 97cb21cd 2022-07-12 thomas }
1129 53d2bdd3 2022-07-10 thomas }
1130 53d2bdd3 2022-07-10 thomas
1131 53d2bdd3 2022-07-10 thomas static const struct got_error *
1132 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1133 53d2bdd3 2022-07-10 thomas {
1134 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1135 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1136 53d2bdd3 2022-07-10 thomas
1137 53d2bdd3 2022-07-10 thomas if (view->parent)
1138 53d2bdd3 2022-07-10 thomas v = view->parent;
1139 53d2bdd3 2022-07-10 thomas else
1140 53d2bdd3 2022-07-10 thomas v = view;
1141 53d2bdd3 2022-07-10 thomas
1142 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1143 53d2bdd3 2022-07-10 thomas return NULL;
1144 53d2bdd3 2022-07-10 thomas
1145 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1146 53d2bdd3 2022-07-10 thomas
1147 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1148 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1149 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1150 53d2bdd3 2022-07-10 thomas if (view->parent)
1151 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1152 53d2bdd3 2022-07-10 thomas else
1153 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1154 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1155 53d2bdd3 2022-07-10 thomas view->count = 0;
1156 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1157 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1158 53d2bdd3 2022-07-10 thomas view->count = 0;
1159 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1160 53d2bdd3 2022-07-10 thomas }
1161 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1162 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1163 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1164 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1165 53d2bdd3 2022-07-10 thomas if (err)
1166 53d2bdd3 2022-07-10 thomas return err;
1167 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1168 53d2bdd3 2022-07-10 thomas } else {
1169 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1170 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1171 53d2bdd3 2022-07-10 thomas if (view->parent)
1172 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1173 53d2bdd3 2022-07-10 thomas else
1174 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1175 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1176 53d2bdd3 2022-07-10 thomas view->count = 0;
1177 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1178 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1179 53d2bdd3 2022-07-10 thomas view->count = 0;
1180 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1181 53d2bdd3 2022-07-10 thomas }
1182 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1183 53d2bdd3 2022-07-10 thomas }
1184 53d2bdd3 2022-07-10 thomas
1185 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1186 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1187 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1188 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1189 53d2bdd3 2022-07-10 thomas
1190 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1191 53d2bdd3 2022-07-10 thomas if (err)
1192 53d2bdd3 2022-07-10 thomas return err;
1193 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1194 53d2bdd3 2022-07-10 thomas if (err)
1195 53d2bdd3 2022-07-10 thomas return err;
1196 53d2bdd3 2022-07-10 thomas
1197 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1198 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1199 53d2bdd3 2022-07-10 thomas if (err)
1200 53d2bdd3 2022-07-10 thomas return err;
1201 53d2bdd3 2022-07-10 thomas }
1202 53d2bdd3 2022-07-10 thomas
1203 3a0139e8 2022-07-22 thomas if (v->resize)
1204 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1205 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1206 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1207 53d2bdd3 2022-07-10 thomas
1208 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1209 53d2bdd3 2022-07-10 thomas
1210 53d2bdd3 2022-07-10 thomas return err;
1211 53d2bdd3 2022-07-10 thomas }
1212 53d2bdd3 2022-07-10 thomas
1213 53d2bdd3 2022-07-10 thomas static void
1214 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1215 53d2bdd3 2022-07-10 thomas {
1216 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1217 53d2bdd3 2022-07-10 thomas
1218 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1219 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1220 669b5ffa 2018-10-07 stsp }
1221 669b5ffa 2018-10-07 stsp
1222 669b5ffa 2018-10-07 stsp static const struct got_error *
1223 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1224 669b5ffa 2018-10-07 stsp {
1225 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1226 669b5ffa 2018-10-07 stsp
1227 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1228 669b5ffa 2018-10-07 stsp return NULL;
1229 669b5ffa 2018-10-07 stsp
1230 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1231 669b5ffa 2018-10-07 stsp view->child = NULL;
1232 669b5ffa 2018-10-07 stsp return err;
1233 669b5ffa 2018-10-07 stsp }
1234 669b5ffa 2018-10-07 stsp
1235 40236d76 2022-06-23 thomas static const struct got_error *
1236 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1237 669b5ffa 2018-10-07 stsp {
1238 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1239 53d2bdd3 2022-07-10 thomas
1240 669b5ffa 2018-10-07 stsp view->child = child;
1241 669b5ffa 2018-10-07 stsp child->parent = view;
1242 40236d76 2022-06-23 thomas
1243 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1244 53d2bdd3 2022-07-10 thomas if (err)
1245 53d2bdd3 2022-07-10 thomas return err;
1246 53d2bdd3 2022-07-10 thomas
1247 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1248 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1249 53d2bdd3 2022-07-10 thomas
1250 53d2bdd3 2022-07-10 thomas return err;
1251 bfddd0d9 2018-09-29 stsp }
1252 2a31b33b 2022-07-23 thomas
1253 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1254 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1255 2a31b33b 2022-07-23 thomas
1256 2a31b33b 2022-07-23 thomas static const struct got_error *
1257 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1258 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1259 2a31b33b 2022-07-23 thomas {
1260 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1261 2a31b33b 2022-07-23 thomas const struct got_error *err;
1262 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1263 2a31b33b 2022-07-23 thomas
1264 2a31b33b 2022-07-23 thomas *requested = NULL;
1265 2a31b33b 2022-07-23 thomas
1266 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1267 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1268 bfddd0d9 2018-09-29 stsp
1269 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1270 2a31b33b 2022-07-23 thomas if (err)
1271 2a31b33b 2022-07-23 thomas return err;
1272 2a31b33b 2022-07-23 thomas
1273 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1274 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1275 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1276 2a31b33b 2022-07-23 thomas if (err)
1277 2a31b33b 2022-07-23 thomas return err;
1278 2a31b33b 2022-07-23 thomas }
1279 2a31b33b 2022-07-23 thomas
1280 2a31b33b 2022-07-23 thomas view->focussed = 0;
1281 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1282 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1283 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1284 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1285 2a31b33b 2022-07-23 thomas
1286 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1287 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1288 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1289 2a31b33b 2022-07-23 thomas if (err)
1290 2a31b33b 2022-07-23 thomas return err;
1291 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1292 2a31b33b 2022-07-23 thomas if (err)
1293 2a31b33b 2022-07-23 thomas return err;
1294 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1295 2a31b33b 2022-07-23 thomas } else
1296 2a31b33b 2022-07-23 thomas *requested = new_view;
1297 2a31b33b 2022-07-23 thomas
1298 2a31b33b 2022-07-23 thomas return NULL;
1299 2a31b33b 2022-07-23 thomas }
1300 2a31b33b 2022-07-23 thomas
1301 34bc9ec9 2019-02-22 stsp static void
1302 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1303 25791caa 2018-10-24 stsp {
1304 25791caa 2018-10-24 stsp int cols, lines;
1305 25791caa 2018-10-24 stsp struct winsize size;
1306 25791caa 2018-10-24 stsp
1307 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1308 25791caa 2018-10-24 stsp cols = 80; /* Default */
1309 25791caa 2018-10-24 stsp lines = 24;
1310 25791caa 2018-10-24 stsp } else {
1311 25791caa 2018-10-24 stsp cols = size.ws_col;
1312 25791caa 2018-10-24 stsp lines = size.ws_row;
1313 25791caa 2018-10-24 stsp }
1314 25791caa 2018-10-24 stsp resize_term(lines, cols);
1315 2b49a8ae 2019-06-22 stsp }
1316 2b49a8ae 2019-06-22 stsp
1317 2b49a8ae 2019-06-22 stsp static const struct got_error *
1318 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1319 2b49a8ae 2019-06-22 stsp {
1320 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1321 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1322 2b49a8ae 2019-06-22 stsp char pattern[1024];
1323 2b49a8ae 2019-06-22 stsp int ret;
1324 c0c4acc8 2021-01-24 stsp
1325 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1326 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1327 c0c4acc8 2021-01-24 stsp view->searching = 0;
1328 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1329 c0c4acc8 2021-01-24 stsp }
1330 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1331 2b49a8ae 2019-06-22 stsp
1332 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1333 2b49a8ae 2019-06-22 stsp return NULL;
1334 2b49a8ae 2019-06-22 stsp
1335 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1336 a5d43cac 2022-07-01 thomas v = view->child;
1337 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1338 d07291c6 2022-12-30 thomas v = view->parent;
1339 2b49a8ae 2019-06-22 stsp
1340 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1341 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1342 a5d43cac 2022-07-01 thomas
1343 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1344 2b49a8ae 2019-06-22 stsp nocbreak();
1345 2b49a8ae 2019-06-22 stsp echo();
1346 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1347 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1348 2b49a8ae 2019-06-22 stsp cbreak();
1349 2b49a8ae 2019-06-22 stsp noecho();
1350 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1351 557d3365 2023-04-14 thomas if (!fast_refresh && !using_mock_io)
1352 f54d892e 2023-02-17 thomas halfdelay(10);
1353 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1354 2b49a8ae 2019-06-22 stsp return NULL;
1355 2b49a8ae 2019-06-22 stsp
1356 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1357 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1358 7c32bd05 2019-06-22 stsp if (err) {
1359 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1360 7c32bd05 2019-06-22 stsp return err;
1361 7c32bd05 2019-06-22 stsp }
1362 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1363 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1364 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1365 2b49a8ae 2019-06-22 stsp view->search_next(view);
1366 2b49a8ae 2019-06-22 stsp }
1367 2b49a8ae 2019-06-22 stsp
1368 2b49a8ae 2019-06-22 stsp return NULL;
1369 64486692 2022-07-07 thomas }
1370 64486692 2022-07-07 thomas
1371 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1372 64486692 2022-07-07 thomas static const struct got_error *
1373 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1374 64486692 2022-07-07 thomas {
1375 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1376 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1377 64486692 2022-07-07 thomas
1378 64486692 2022-07-07 thomas if (view->parent)
1379 64486692 2022-07-07 thomas v = view->parent;
1380 64486692 2022-07-07 thomas else
1381 64486692 2022-07-07 thomas v = view;
1382 64486692 2022-07-07 thomas
1383 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1384 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1385 ddbc4d37 2022-07-12 thomas else
1386 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1387 64486692 2022-07-07 thomas
1388 ddbc4d37 2022-07-12 thomas if (!v->child)
1389 ddbc4d37 2022-07-12 thomas return NULL;
1390 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1391 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1392 ddbc4d37 2022-07-12 thomas
1393 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1394 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1395 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1396 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1397 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1398 64486692 2022-07-07 thomas
1399 ddbc4d37 2022-07-12 thomas
1400 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1401 64486692 2022-07-07 thomas v->ncols = COLS;
1402 64486692 2022-07-07 thomas v->child->ncols = COLS;
1403 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1404 64486692 2022-07-07 thomas
1405 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1406 64486692 2022-07-07 thomas if (err)
1407 64486692 2022-07-07 thomas return err;
1408 64486692 2022-07-07 thomas }
1409 64486692 2022-07-07 thomas v->child->mode = v->mode;
1410 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1411 64486692 2022-07-07 thomas v->focus_child = 1;
1412 64486692 2022-07-07 thomas
1413 64486692 2022-07-07 thomas err = view_fullscreen(v);
1414 64486692 2022-07-07 thomas if (err)
1415 64486692 2022-07-07 thomas return err;
1416 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1417 64486692 2022-07-07 thomas if (err)
1418 64486692 2022-07-07 thomas return err;
1419 64486692 2022-07-07 thomas
1420 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1421 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1422 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1423 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1424 cf1fe301 2022-07-29 thomas if (err)
1425 cf1fe301 2022-07-29 thomas return err;
1426 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1427 cf1fe301 2022-07-29 thomas if (err)
1428 cf1fe301 2022-07-29 thomas return err;
1429 ddbc4d37 2022-07-12 thomas } else {
1430 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1431 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1432 64486692 2022-07-07 thomas }
1433 f4e6231a 2022-07-22 thomas if (v->resize)
1434 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1435 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1436 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1437 64486692 2022-07-07 thomas
1438 64486692 2022-07-07 thomas return err;
1439 0cf4efb1 2018-09-29 stsp }
1440 6d0fee91 2018-08-01 stsp
1441 07b0611c 2022-06-23 thomas /*
1442 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1443 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1444 b85a3496 2023-04-14 thomas */
1445 b85a3496 2023-04-14 thomas static void
1446 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1447 b85a3496 2023-04-14 thomas {
1448 b85a3496 2023-04-14 thomas size_t x = *n;
1449 b85a3496 2023-04-14 thomas
1450 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1451 b85a3496 2023-04-14 thomas return;
1452 b85a3496 2023-04-14 thomas
1453 b85a3496 2023-04-14 thomas if (x < 0)
1454 b85a3496 2023-04-14 thomas x = strlen(str);
1455 b85a3496 2023-04-14 thomas
1456 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1457 b85a3496 2023-04-14 thomas str[x] = '\0';
1458 b85a3496 2023-04-14 thomas
1459 b85a3496 2023-04-14 thomas *n = x + 1;
1460 b85a3496 2023-04-14 thomas }
1461 b85a3496 2023-04-14 thomas
1462 b85a3496 2023-04-14 thomas /*
1463 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1464 13bc6832 2023-04-22 thomas * and strip trailing whitespace. If vline is set, overwrite
1465 13bc6832 2023-04-22 thomas * line[vline] with '|' because the ACS_VLINE character is
1466 13bc6832 2023-04-22 thomas * written out as 'x'. Write the line to file f.
1467 b85a3496 2023-04-14 thomas */
1468 b85a3496 2023-04-14 thomas static const struct got_error *
1469 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1470 b85a3496 2023-04-14 thomas {
1471 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1472 b85a3496 2023-04-14 thomas int r, w;
1473 b85a3496 2023-04-14 thomas
1474 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1475 b85a3496 2023-04-14 thomas if (r == ERR)
1476 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1477 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1478 b85a3496 2023-04-14 thomas
1479 b85a3496 2023-04-14 thomas /*
1480 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1481 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1482 b85a3496 2023-04-14 thomas */
1483 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1484 b85a3496 2023-04-14 thomas
1485 13bc6832 2023-04-22 thomas if (vline > 0)
1486 b85a3496 2023-04-14 thomas line[vline] = '|';
1487 b85a3496 2023-04-14 thomas
1488 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1489 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1490 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1491 b85a3496 2023-04-14 thomas
1492 b85a3496 2023-04-14 thomas return NULL;
1493 b85a3496 2023-04-14 thomas }
1494 b85a3496 2023-04-14 thomas
1495 b85a3496 2023-04-14 thomas /*
1496 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1497 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1498 b85a3496 2023-04-14 thomas */
1499 b85a3496 2023-04-14 thomas static const struct got_error *
1500 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1501 b85a3496 2023-04-14 thomas {
1502 b85a3496 2023-04-14 thomas const struct got_error *err;
1503 b85a3496 2023-04-14 thomas int i;
1504 b85a3496 2023-04-14 thomas
1505 5b3a801d 2023-04-22 thomas err = got_opentemp_truncate(tog_io.sdump);
1506 5b3a801d 2023-04-22 thomas if (err)
1507 5b3a801d 2023-04-22 thomas return err;
1508 b85a3496 2023-04-14 thomas
1509 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1510 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1511 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1512 b85a3496 2023-04-14 thomas
1513 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1514 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1515 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, ncols - 1);
1516 b85a3496 2023-04-14 thomas if (err)
1517 b85a3496 2023-04-14 thomas goto done;
1518 b85a3496 2023-04-14 thomas }
1519 b85a3496 2023-04-14 thomas } else {
1520 b85a3496 2023-04-14 thomas int hline = 0;
1521 b85a3496 2023-04-14 thomas
1522 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1523 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1524 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1525 b85a3496 2023-04-14 thomas hline = view->child ?
1526 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1527 b85a3496 2023-04-14 thomas
1528 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1529 13bc6832 2023-04-22 thomas if (hline && i == hline - 1) {
1530 b85a3496 2023-04-14 thomas int c;
1531 b85a3496 2023-04-14 thomas
1532 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1533 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1534 5b3a801d 2023-04-22 thomas fputc('-', tog_io.sdump);
1535 5b3a801d 2023-04-22 thomas fputc('\n', tog_io.sdump);
1536 b85a3496 2023-04-14 thomas continue;
1537 b85a3496 2023-04-14 thomas }
1538 b85a3496 2023-04-14 thomas
1539 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, 0);
1540 b85a3496 2023-04-14 thomas if (err)
1541 b85a3496 2023-04-14 thomas goto done;
1542 b85a3496 2023-04-14 thomas }
1543 b85a3496 2023-04-14 thomas }
1544 b85a3496 2023-04-14 thomas
1545 b85a3496 2023-04-14 thomas done:
1546 b85a3496 2023-04-14 thomas return err;
1547 b85a3496 2023-04-14 thomas }
1548 b85a3496 2023-04-14 thomas
1549 b85a3496 2023-04-14 thomas /*
1550 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1551 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1552 07b0611c 2022-06-23 thomas */
1553 07b0611c 2022-06-23 thomas static int
1554 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1555 07b0611c 2022-06-23 thomas {
1556 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1557 a5d43cac 2022-07-01 thomas int x, n = 0;
1558 07b0611c 2022-06-23 thomas
1559 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1560 a5d43cac 2022-07-01 thomas v = view->child;
1561 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1562 a5d43cac 2022-07-01 thomas v = view->parent;
1563 a5d43cac 2022-07-01 thomas
1564 07b0611c 2022-06-23 thomas view->count = 0;
1565 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1566 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1567 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1568 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1569 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1570 07b0611c 2022-06-23 thomas
1571 07b0611c 2022-06-23 thomas do {
1572 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1573 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1574 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1575 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1576 a5d43cac 2022-07-01 thomas }
1577 a5d43cac 2022-07-01 thomas
1578 07b0611c 2022-06-23 thomas /*
1579 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1580 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1581 07b0611c 2022-06-23 thomas */
1582 07b0611c 2022-06-23 thomas if (n >= 9999999)
1583 07b0611c 2022-06-23 thomas n = 9999999;
1584 07b0611c 2022-06-23 thomas else
1585 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1586 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1587 07b0611c 2022-06-23 thomas
1588 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1589 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1590 07dd3ed3 2022-08-06 thomas n = 0;
1591 07dd3ed3 2022-08-06 thomas c = 0;
1592 07dd3ed3 2022-08-06 thomas }
1593 07dd3ed3 2022-08-06 thomas
1594 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1595 07b0611c 2022-06-23 thomas view->count = n;
1596 07b0611c 2022-06-23 thomas
1597 07b0611c 2022-06-23 thomas return c;
1598 f2d06bef 2023-01-23 thomas }
1599 f2d06bef 2023-01-23 thomas
1600 f2d06bef 2023-01-23 thomas static void
1601 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1602 f2d06bef 2023-01-23 thomas {
1603 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1604 f2d06bef 2023-01-23 thomas
1605 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1606 f2d06bef 2023-01-23 thomas v = view->child;
1607 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1608 f2d06bef 2023-01-23 thomas v = view->parent;
1609 f2d06bef 2023-01-23 thomas
1610 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1611 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1612 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1613 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1614 f2d06bef 2023-01-23 thomas
1615 f2d06bef 2023-01-23 thomas /*
1616 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1617 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1618 f2d06bef 2023-01-23 thomas */
1619 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1620 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1621 f2d06bef 2023-01-23 thomas view->action = NULL;
1622 f2d06bef 2023-01-23 thomas } else
1623 f2d06bef 2023-01-23 thomas view->action = NULL;
1624 07b0611c 2022-06-23 thomas }
1625 07b0611c 2022-06-23 thomas
1626 b85a3496 2023-04-14 thomas /*
1627 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1628 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1629 b85a3496 2023-04-14 thomas */
1630 0cf4efb1 2018-09-29 stsp static const struct got_error *
1631 a184c764 2023-04-22 thomas tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1632 b85a3496 2023-04-14 thomas {
1633 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1634 b85a3496 2023-04-14 thomas char *line = NULL;
1635 b85a3496 2023-04-14 thomas size_t linesz = 0;
1636 8e778ade 2023-04-22 thomas
1637 a184c764 2023-04-22 thomas if (view->count && --view->count) {
1638 a184c764 2023-04-22 thomas *ch = view->ch;
1639 a184c764 2023-04-22 thomas return NULL;
1640 a184c764 2023-04-22 thomas } else
1641 a184c764 2023-04-22 thomas *ch = -1;
1642 b85a3496 2023-04-14 thomas
1643 b85a3496 2023-04-14 thomas if (getline(&line, &linesz, script) == -1) {
1644 b85a3496 2023-04-14 thomas if (feof(script)) {
1645 b85a3496 2023-04-14 thomas *done = 1;
1646 b85a3496 2023-04-14 thomas goto done;
1647 b85a3496 2023-04-14 thomas } else {
1648 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1649 b85a3496 2023-04-14 thomas goto done;
1650 b85a3496 2023-04-14 thomas }
1651 8e778ade 2023-04-22 thomas }
1652 1134ebde 2023-04-22 thomas
1653 8e778ade 2023-04-22 thomas if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1654 8e778ade 2023-04-22 thomas tog_io.wait_for_ui = 1;
1655 8e778ade 2023-04-22 thomas else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1656 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1657 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1658 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1659 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1660 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1661 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1662 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1663 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1664 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1665 52c5094b 2023-04-28 thomas else if (strncasecmp(line, "TAB", 3) == 0)
1666 52c5094b 2023-04-28 thomas *ch = '\t';
1667 13bc6832 2023-04-22 thomas else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1668 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1669 a184c764 2023-04-22 thomas else if (isdigit((unsigned char)*line)) {
1670 a184c764 2023-04-22 thomas char *t = line;
1671 a184c764 2023-04-22 thomas
1672 a184c764 2023-04-22 thomas while (isdigit((unsigned char)*t))
1673 a184c764 2023-04-22 thomas ++t;
1674 a184c764 2023-04-22 thomas view->ch = *ch = *t;
1675 a184c764 2023-04-22 thomas *t = '\0';
1676 a184c764 2023-04-22 thomas /* ignore error, view->count is 0 if instruction is invalid */
1677 a184c764 2023-04-22 thomas view->count = strtonum(line, 0, INT_MAX, NULL);
1678 a184c764 2023-04-22 thomas } else
1679 b85a3496 2023-04-14 thomas *ch = *line;
1680 b85a3496 2023-04-14 thomas
1681 b85a3496 2023-04-14 thomas done:
1682 b85a3496 2023-04-14 thomas free(line);
1683 b85a3496 2023-04-14 thomas return err;
1684 b85a3496 2023-04-14 thomas }
1685 b85a3496 2023-04-14 thomas
1686 b85a3496 2023-04-14 thomas static const struct got_error *
1687 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1688 557d3365 2023-04-14 thomas struct tog_view_list_head *views, int fast_refresh)
1689 e5a0f69f 2018-08-18 stsp {
1690 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1691 669b5ffa 2018-10-07 stsp struct tog_view *v;
1692 1a76625f 2018-10-22 stsp int ch, errcode;
1693 e5a0f69f 2018-08-18 stsp
1694 e5a0f69f 2018-08-18 stsp *new = NULL;
1695 f2d06bef 2023-01-23 thomas
1696 f2d06bef 2023-01-23 thomas if (view->action)
1697 f2d06bef 2023-01-23 thomas action_report(view);
1698 8f4ed634 2020-03-26 stsp
1699 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1700 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1701 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1702 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1703 07b0611c 2022-06-23 thomas view->count = 0;
1704 07b0611c 2022-06-23 thomas }
1705 e5a0f69f 2018-08-18 stsp
1706 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1707 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1708 82954512 2020-02-03 stsp if (errcode)
1709 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1710 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1711 3da8ef85 2021-09-21 stsp sched_yield();
1712 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1713 82954512 2020-02-03 stsp if (errcode)
1714 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1715 82954512 2020-02-03 stsp "pthread_mutex_lock");
1716 60493ae3 2019-06-20 stsp view->search_next(view);
1717 60493ae3 2019-06-20 stsp return NULL;
1718 60493ae3 2019-06-20 stsp }
1719 60493ae3 2019-06-20 stsp
1720 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1721 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1722 1a76625f 2018-10-22 stsp if (errcode)
1723 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1724 b85a3496 2023-04-14 thomas
1725 557d3365 2023-04-14 thomas if (using_mock_io) {
1726 a184c764 2023-04-22 thomas err = tog_read_script_key(tog_io.f, view, &ch, done);
1727 9d9cab5e 2023-04-22 thomas if (err) {
1728 9d9cab5e 2023-04-22 thomas errcode = pthread_mutex_lock(&tog_mutex);
1729 b85a3496 2023-04-14 thomas return err;
1730 9d9cab5e 2023-04-22 thomas }
1731 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1732 634cb454 2022-07-03 thomas cbreak();
1733 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1734 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1735 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1736 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1737 634cb454 2022-07-03 thomas view->count = 0;
1738 634cb454 2022-07-03 thomas else
1739 634cb454 2022-07-03 thomas ch = view->ch;
1740 634cb454 2022-07-03 thomas } else {
1741 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1742 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1743 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1744 07b0611c 2022-06-23 thomas }
1745 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1746 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1747 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1748 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1749 1a76625f 2018-10-22 stsp if (errcode)
1750 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1751 25791caa 2018-10-24 stsp
1752 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1753 25791caa 2018-10-24 stsp tog_resizeterm();
1754 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1755 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1756 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1757 25791caa 2018-10-24 stsp err = view_resize(v);
1758 25791caa 2018-10-24 stsp if (err)
1759 25791caa 2018-10-24 stsp return err;
1760 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1761 25791caa 2018-10-24 stsp if (err)
1762 25791caa 2018-10-24 stsp return err;
1763 cdfcfb03 2020-12-06 stsp if (v->child) {
1764 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1765 cdfcfb03 2020-12-06 stsp if (err)
1766 cdfcfb03 2020-12-06 stsp return err;
1767 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1768 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1769 cdfcfb03 2020-12-06 stsp if (err)
1770 cdfcfb03 2020-12-06 stsp return err;
1771 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1772 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1773 53d2bdd3 2022-07-10 thomas if (err)
1774 53d2bdd3 2022-07-10 thomas return err;
1775 53d2bdd3 2022-07-10 thomas }
1776 cdfcfb03 2020-12-06 stsp }
1777 25791caa 2018-10-24 stsp }
1778 25791caa 2018-10-24 stsp }
1779 25791caa 2018-10-24 stsp
1780 e5a0f69f 2018-08-18 stsp switch (ch) {
1781 fc2737d5 2022-09-15 thomas case '?':
1782 fc2737d5 2022-09-15 thomas case 'H':
1783 fc2737d5 2022-09-15 thomas case KEY_F(1):
1784 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1785 fc2737d5 2022-09-15 thomas err = view->reset(view);
1786 fc2737d5 2022-09-15 thomas else
1787 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1788 fc2737d5 2022-09-15 thomas break;
1789 1e37a5c2 2019-05-12 jcs case '\t':
1790 07b0611c 2022-06-23 thomas view->count = 0;
1791 1e37a5c2 2019-05-12 jcs if (view->child) {
1792 e78dc838 2020-12-04 stsp view->focussed = 0;
1793 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1794 e78dc838 2020-12-04 stsp view->focus_child = 1;
1795 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1796 e78dc838 2020-12-04 stsp view->focussed = 0;
1797 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1798 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1799 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1800 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1801 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1802 3a0139e8 2022-07-22 thomas 0);
1803 a5d43cac 2022-07-01 thomas if (err)
1804 a5d43cac 2022-07-01 thomas return err;
1805 a5d43cac 2022-07-01 thomas }
1806 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1807 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1808 a5d43cac 2022-07-01 thomas if (err)
1809 a5d43cac 2022-07-01 thomas return err;
1810 a5d43cac 2022-07-01 thomas }
1811 1e37a5c2 2019-05-12 jcs }
1812 1e37a5c2 2019-05-12 jcs break;
1813 1e37a5c2 2019-05-12 jcs case 'q':
1814 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1815 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1816 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1817 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1818 a5d43cac 2022-07-01 thomas if (err)
1819 a5d43cac 2022-07-01 thomas break;
1820 a5d43cac 2022-07-01 thomas }
1821 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1822 a5d43cac 2022-07-01 thomas }
1823 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1824 9970f7fc 2020-12-03 stsp view->dying = 1;
1825 1e37a5c2 2019-05-12 jcs break;
1826 1e37a5c2 2019-05-12 jcs case 'Q':
1827 1e37a5c2 2019-05-12 jcs *done = 1;
1828 1e37a5c2 2019-05-12 jcs break;
1829 1c5e5faa 2022-06-23 thomas case 'F':
1830 07b0611c 2022-06-23 thomas view->count = 0;
1831 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1832 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1833 1e37a5c2 2019-05-12 jcs break;
1834 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1835 e78dc838 2020-12-04 stsp view->focussed = 0;
1836 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1837 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1838 53d2bdd3 2022-07-10 thomas } else {
1839 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1840 53d2bdd3 2022-07-10 thomas if (!err)
1841 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1842 53d2bdd3 2022-07-10 thomas }
1843 1e37a5c2 2019-05-12 jcs if (err)
1844 1e37a5c2 2019-05-12 jcs break;
1845 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1846 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1847 1e37a5c2 2019-05-12 jcs } else {
1848 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1849 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1850 e78dc838 2020-12-04 stsp view->focussed = 1;
1851 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1852 1e37a5c2 2019-05-12 jcs } else {
1853 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1854 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1855 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1856 53d2bdd3 2022-07-10 thomas if (!err)
1857 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1858 669b5ffa 2018-10-07 stsp }
1859 1e37a5c2 2019-05-12 jcs if (err)
1860 1e37a5c2 2019-05-12 jcs break;
1861 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1862 a5d43cac 2022-07-01 thomas }
1863 a5d43cac 2022-07-01 thomas if (err)
1864 a5d43cac 2022-07-01 thomas break;
1865 3a0139e8 2022-07-22 thomas if (view->resize) {
1866 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1867 a5d43cac 2022-07-01 thomas if (err)
1868 a5d43cac 2022-07-01 thomas break;
1869 1e37a5c2 2019-05-12 jcs }
1870 e0bcabc5 2023-04-28 thomas if (view->parent) {
1871 e0bcabc5 2023-04-28 thomas if (view->parent->resize) {
1872 e0bcabc5 2023-04-28 thomas err = view->parent->resize(view->parent, 0);
1873 e0bcabc5 2023-04-28 thomas if (err != NULL)
1874 e0bcabc5 2023-04-28 thomas break;
1875 e0bcabc5 2023-04-28 thomas }
1876 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1877 e0bcabc5 2023-04-28 thomas if (err != NULL)
1878 e0bcabc5 2023-04-28 thomas break;
1879 e0bcabc5 2023-04-28 thomas }
1880 e0bcabc5 2023-04-28 thomas err = offset_selection_down(view);
1881 1e37a5c2 2019-05-12 jcs break;
1882 64486692 2022-07-07 thomas case 'S':
1883 53d2bdd3 2022-07-10 thomas view->count = 0;
1884 64486692 2022-07-07 thomas err = switch_split(view);
1885 53d2bdd3 2022-07-10 thomas break;
1886 53d2bdd3 2022-07-10 thomas case '-':
1887 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1888 64486692 2022-07-07 thomas break;
1889 53d2bdd3 2022-07-10 thomas case '+':
1890 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1891 53d2bdd3 2022-07-10 thomas break;
1892 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1893 60493ae3 2019-06-20 stsp break;
1894 60493ae3 2019-06-20 stsp case '/':
1895 07b0611c 2022-06-23 thomas view->count = 0;
1896 60493ae3 2019-06-20 stsp if (view->search_start)
1897 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1898 60493ae3 2019-06-20 stsp else
1899 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1900 1e37a5c2 2019-05-12 jcs break;
1901 b1bf1435 2019-06-21 stsp case 'N':
1902 60493ae3 2019-06-20 stsp case 'n':
1903 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1904 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1905 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1906 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1907 60493ae3 2019-06-20 stsp view->search_next(view);
1908 60493ae3 2019-06-20 stsp } else
1909 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1910 adf4c9e0 2022-07-03 thomas break;
1911 adf4c9e0 2022-07-03 thomas case 'A':
1912 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1913 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1914 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1915 f2d06bef 2023-01-23 thomas } else {
1916 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1917 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1918 f2d06bef 2023-01-23 thomas }
1919 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1920 adf4c9e0 2022-07-03 thomas if (v->reset) {
1921 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1922 adf4c9e0 2022-07-03 thomas if (err)
1923 adf4c9e0 2022-07-03 thomas return err;
1924 adf4c9e0 2022-07-03 thomas }
1925 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1926 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1927 adf4c9e0 2022-07-03 thomas if (err)
1928 adf4c9e0 2022-07-03 thomas return err;
1929 adf4c9e0 2022-07-03 thomas }
1930 adf4c9e0 2022-07-03 thomas }
1931 60493ae3 2019-06-20 stsp break;
1932 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1933 b85a3496 2023-04-14 thomas err = screendump(view);
1934 b85a3496 2023-04-14 thomas break;
1935 1e37a5c2 2019-05-12 jcs default:
1936 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1937 1e37a5c2 2019-05-12 jcs break;
1938 e5a0f69f 2018-08-18 stsp }
1939 e5a0f69f 2018-08-18 stsp
1940 e5a0f69f 2018-08-18 stsp return err;
1941 bcbd79e2 2018-08-19 stsp }
1942 bcbd79e2 2018-08-19 stsp
1943 ef20f542 2022-06-26 thomas static int
1944 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1945 a3404814 2018-09-02 stsp {
1946 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1947 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1948 669b5ffa 2018-10-07 stsp return 0;
1949 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1950 669b5ffa 2018-10-07 stsp return 0;
1951 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1952 a3404814 2018-09-02 stsp return 0;
1953 a3404814 2018-09-02 stsp
1954 669b5ffa 2018-10-07 stsp return view->focussed;
1955 a3404814 2018-09-02 stsp }
1956 a3404814 2018-09-02 stsp
1957 bcbd79e2 2018-08-19 stsp static const struct got_error *
1958 557d3365 2023-04-14 thomas tog_io_close(void)
1959 e5a0f69f 2018-08-18 stsp {
1960 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1961 b85a3496 2023-04-14 thomas
1962 557d3365 2023-04-14 thomas if (tog_io.cin && fclose(tog_io.cin) == EOF)
1963 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cin, GOT_ERR_IO);
1964 557d3365 2023-04-14 thomas if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1965 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cout, GOT_ERR_IO);
1966 557d3365 2023-04-14 thomas if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1967 557d3365 2023-04-14 thomas err = got_ferror(tog_io.f, GOT_ERR_IO);
1968 5b3a801d 2023-04-22 thomas if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1969 5b3a801d 2023-04-22 thomas err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1970 b85a3496 2023-04-14 thomas
1971 b85a3496 2023-04-14 thomas return err;
1972 b85a3496 2023-04-14 thomas }
1973 b85a3496 2023-04-14 thomas
1974 b85a3496 2023-04-14 thomas static const struct got_error *
1975 557d3365 2023-04-14 thomas view_loop(struct tog_view *view)
1976 b85a3496 2023-04-14 thomas {
1977 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1978 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1979 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1980 64486692 2022-07-07 thomas char *mode;
1981 fd823528 2018-10-22 stsp int fast_refresh = 10;
1982 1a76625f 2018-10-22 stsp int done = 0, errcode;
1983 e5a0f69f 2018-08-18 stsp
1984 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1985 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1986 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1987 64486692 2022-07-07 thomas else
1988 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1989 64486692 2022-07-07 thomas
1990 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1991 1a76625f 2018-10-22 stsp if (errcode)
1992 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1993 1a76625f 2018-10-22 stsp
1994 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1995 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1996 e5a0f69f 2018-08-18 stsp
1997 1004088d 2018-09-29 stsp view->focussed = 1;
1998 878940b7 2018-09-29 stsp err = view->show(view);
1999 0cf4efb1 2018-09-29 stsp if (err)
2000 0cf4efb1 2018-09-29 stsp return err;
2001 0cf4efb1 2018-09-29 stsp update_panels();
2002 0cf4efb1 2018-09-29 stsp doupdate();
2003 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
2004 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
2005 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
2006 557d3365 2023-04-14 thomas if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
2007 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
2008 fd823528 2018-10-22 stsp
2009 557d3365 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
2010 e5a0f69f 2018-08-18 stsp if (err)
2011 e5a0f69f 2018-08-18 stsp break;
2012 6ec3d39c 2023-01-23 thomas
2013 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
2014 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
2015 6ec3d39c 2023-01-23 thomas done = 1;
2016 6ec3d39c 2023-01-23 thomas if (done) {
2017 6ec3d39c 2023-01-23 thomas struct tog_view *v;
2018 6ec3d39c 2023-01-23 thomas
2019 6ec3d39c 2023-01-23 thomas /*
2020 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
2021 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
2022 6ec3d39c 2023-01-23 thomas */
2023 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
2024 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
2025 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
2026 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
2027 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
2028 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
2029 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
2030 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
2031 6ec3d39c 2023-01-23 thomas }
2032 6ec3d39c 2023-01-23 thomas }
2033 6ec3d39c 2023-01-23 thomas doupdate();
2034 6ec3d39c 2023-01-23 thomas }
2035 6ec3d39c 2023-01-23 thomas
2036 9970f7fc 2020-12-03 stsp if (view->dying) {
2037 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2038 669b5ffa 2018-10-07 stsp
2039 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2040 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2041 9970f7fc 2020-12-03 stsp entry);
2042 e78dc838 2020-12-04 stsp else if (view->parent)
2043 669b5ffa 2018-10-07 stsp prev = view->parent;
2044 669b5ffa 2018-10-07 stsp
2045 e78dc838 2020-12-04 stsp if (view->parent) {
2046 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2047 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2048 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2049 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2050 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2051 40236d76 2022-06-23 thomas if (err)
2052 40236d76 2022-06-23 thomas break;
2053 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2054 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2055 e78dc838 2020-12-04 stsp } else
2056 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2057 669b5ffa 2018-10-07 stsp
2058 9970f7fc 2020-12-03 stsp err = view_close(view);
2059 fb59748f 2020-12-05 stsp if (err)
2060 e5a0f69f 2018-08-18 stsp goto done;
2061 669b5ffa 2018-10-07 stsp
2062 e78dc838 2020-12-04 stsp view = NULL;
2063 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2064 e78dc838 2020-12-04 stsp if (v->focussed)
2065 e78dc838 2020-12-04 stsp break;
2066 0cf4efb1 2018-09-29 stsp }
2067 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2068 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2069 e78dc838 2020-12-04 stsp if (prev)
2070 e78dc838 2020-12-04 stsp view = prev;
2071 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2072 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2073 e78dc838 2020-12-04 stsp tog_view_list_head);
2074 e78dc838 2020-12-04 stsp }
2075 e78dc838 2020-12-04 stsp if (view) {
2076 e78dc838 2020-12-04 stsp if (view->focus_child) {
2077 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2078 e78dc838 2020-12-04 stsp view = view->child;
2079 e78dc838 2020-12-04 stsp } else
2080 e78dc838 2020-12-04 stsp view->focussed = 1;
2081 e78dc838 2020-12-04 stsp }
2082 e78dc838 2020-12-04 stsp }
2083 e5a0f69f 2018-08-18 stsp }
2084 bcbd79e2 2018-08-19 stsp if (new_view) {
2085 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2086 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2087 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2088 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2089 86c66b02 2018-10-18 stsp continue;
2090 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2091 86c66b02 2018-10-18 stsp err = view_close(v);
2092 86c66b02 2018-10-18 stsp if (err)
2093 86c66b02 2018-10-18 stsp goto done;
2094 86c66b02 2018-10-18 stsp break;
2095 86c66b02 2018-10-18 stsp }
2096 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2097 fed7eaa8 2018-10-24 stsp view = new_view;
2098 7cd52833 2022-06-23 thomas }
2099 6ec3d39c 2023-01-23 thomas if (view && !done) {
2100 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2101 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2102 e78dc838 2020-12-04 stsp view = view->child;
2103 e78dc838 2020-12-04 stsp } else {
2104 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2105 e78dc838 2020-12-04 stsp view = view->parent;
2106 1a76625f 2018-10-22 stsp }
2107 e78dc838 2020-12-04 stsp show_panel(view->panel);
2108 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2109 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2110 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2111 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2112 669b5ffa 2018-10-07 stsp if (err)
2113 1a76625f 2018-10-22 stsp goto done;
2114 669b5ffa 2018-10-07 stsp }
2115 669b5ffa 2018-10-07 stsp err = view->show(view);
2116 0cf4efb1 2018-09-29 stsp if (err)
2117 1a76625f 2018-10-22 stsp goto done;
2118 669b5ffa 2018-10-07 stsp if (view->child) {
2119 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2120 669b5ffa 2018-10-07 stsp if (err)
2121 1a76625f 2018-10-22 stsp goto done;
2122 669b5ffa 2018-10-07 stsp }
2123 1a76625f 2018-10-22 stsp update_panels();
2124 1a76625f 2018-10-22 stsp doupdate();
2125 0cf4efb1 2018-09-29 stsp }
2126 e5a0f69f 2018-08-18 stsp }
2127 e5a0f69f 2018-08-18 stsp done:
2128 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2129 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2130 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2131 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2132 f2d749db 2022-07-12 thomas close_err = view_close(view);
2133 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2134 f2d749db 2022-07-12 thomas err = close_err;
2135 e5a0f69f 2018-08-18 stsp }
2136 1a76625f 2018-10-22 stsp
2137 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2138 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2139 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2140 1a76625f 2018-10-22 stsp
2141 e5a0f69f 2018-08-18 stsp return err;
2142 ea5e7bb5 2018-08-01 stsp }
2143 ea5e7bb5 2018-08-01 stsp
2144 4ed7e80c 2018-05-20 stsp __dead static void
2145 9f7d7167 2018-04-29 stsp usage_log(void)
2146 9f7d7167 2018-04-29 stsp {
2147 80ddbec8 2018-04-29 stsp endwin();
2148 c70c5802 2018-08-01 stsp fprintf(stderr,
2149 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2150 9f7d7167 2018-04-29 stsp getprogname());
2151 9f7d7167 2018-04-29 stsp exit(1);
2152 80ddbec8 2018-04-29 stsp }
2153 80ddbec8 2018-04-29 stsp
2154 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2155 80ddbec8 2018-04-29 stsp static const struct got_error *
2156 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2157 963b370f 2018-05-20 stsp {
2158 00dfcb92 2018-06-11 stsp char *vis = NULL;
2159 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2160 963b370f 2018-05-20 stsp
2161 963b370f 2018-05-20 stsp *ws = NULL;
2162 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2163 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2164 00dfcb92 2018-06-11 stsp int vislen;
2165 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2166 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2167 00dfcb92 2018-06-11 stsp
2168 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2169 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2170 00dfcb92 2018-06-11 stsp if (err)
2171 00dfcb92 2018-06-11 stsp return err;
2172 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2173 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2174 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2175 a7f50699 2018-06-11 stsp goto done;
2176 a7f50699 2018-06-11 stsp }
2177 00dfcb92 2018-06-11 stsp }
2178 963b370f 2018-05-20 stsp
2179 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2180 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2181 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2182 a7f50699 2018-06-11 stsp goto done;
2183 a7f50699 2018-06-11 stsp }
2184 963b370f 2018-05-20 stsp
2185 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2186 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2187 a7f50699 2018-06-11 stsp done:
2188 00dfcb92 2018-06-11 stsp free(vis);
2189 963b370f 2018-05-20 stsp if (err) {
2190 963b370f 2018-05-20 stsp free(*ws);
2191 963b370f 2018-05-20 stsp *ws = NULL;
2192 963b370f 2018-05-20 stsp *wlen = 0;
2193 963b370f 2018-05-20 stsp }
2194 963b370f 2018-05-20 stsp return err;
2195 05171be4 2022-06-23 thomas }
2196 05171be4 2022-06-23 thomas
2197 05171be4 2022-06-23 thomas static const struct got_error *
2198 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2199 05171be4 2022-06-23 thomas {
2200 05171be4 2022-06-23 thomas char *dst;
2201 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2202 05171be4 2022-06-23 thomas
2203 05171be4 2022-06-23 thomas *ptr = NULL;
2204 05171be4 2022-06-23 thomas n = len = strlen(src);
2205 83316834 2022-06-23 thomas dst = malloc(n + 1);
2206 05171be4 2022-06-23 thomas if (dst == NULL)
2207 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2208 05171be4 2022-06-23 thomas
2209 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2210 05171be4 2022-06-23 thomas const char c = src[idx];
2211 05171be4 2022-06-23 thomas
2212 05171be4 2022-06-23 thomas if (c == '\t') {
2213 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2214 b235ad5d 2022-06-23 thomas char *p;
2215 b235ad5d 2022-06-23 thomas
2216 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2217 83316834 2022-06-23 thomas if (p == NULL) {
2218 83316834 2022-06-23 thomas free(dst);
2219 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2220 83316834 2022-06-23 thomas
2221 83316834 2022-06-23 thomas }
2222 83316834 2022-06-23 thomas dst = p;
2223 05171be4 2022-06-23 thomas n += nb;
2224 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2225 05171be4 2022-06-23 thomas sz += nb;
2226 05171be4 2022-06-23 thomas } else
2227 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2228 05171be4 2022-06-23 thomas ++idx;
2229 05171be4 2022-06-23 thomas }
2230 05171be4 2022-06-23 thomas
2231 05171be4 2022-06-23 thomas dst[sz] = '\0';
2232 05171be4 2022-06-23 thomas *ptr = dst;
2233 05171be4 2022-06-23 thomas return NULL;
2234 963b370f 2018-05-20 stsp }
2235 963b370f 2018-05-20 stsp
2236 8d208d34 2022-06-23 thomas /*
2237 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2238 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2239 20181212 2023-06-01 thomas * Return the combined column width of all spanned wide characters in
2240 8d208d34 2022-06-23 thomas * *rcol.
2241 f91a2b48 2022-06-23 thomas */
2242 8d208d34 2022-06-23 thomas static int
2243 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2244 8d208d34 2022-06-23 thomas {
2245 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2246 f91a2b48 2022-06-23 thomas
2247 8d208d34 2022-06-23 thomas if (n == 0) {
2248 8d208d34 2022-06-23 thomas *rcol = cols;
2249 8d208d34 2022-06-23 thomas return off;
2250 8d208d34 2022-06-23 thomas }
2251 f91a2b48 2022-06-23 thomas
2252 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2253 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2254 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2255 8d208d34 2022-06-23 thomas else
2256 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2257 f91a2b48 2022-06-23 thomas
2258 8d208d34 2022-06-23 thomas if (width == -1) {
2259 8d208d34 2022-06-23 thomas width = 1;
2260 8d208d34 2022-06-23 thomas wline[i] = L'.';
2261 f91a2b48 2022-06-23 thomas }
2262 f91a2b48 2022-06-23 thomas
2263 8d208d34 2022-06-23 thomas if (cols + width > n)
2264 8d208d34 2022-06-23 thomas break;
2265 8d208d34 2022-06-23 thomas cols += width;
2266 f91a2b48 2022-06-23 thomas }
2267 f91a2b48 2022-06-23 thomas
2268 8d208d34 2022-06-23 thomas *rcol = cols;
2269 8d208d34 2022-06-23 thomas return i;
2270 f91a2b48 2022-06-23 thomas }
2271 f91a2b48 2022-06-23 thomas
2272 f91a2b48 2022-06-23 thomas /*
2273 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2274 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2275 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2276 f91a2b48 2022-06-23 thomas */
2277 f91a2b48 2022-06-23 thomas static const struct got_error *
2278 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2279 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2280 f91a2b48 2022-06-23 thomas {
2281 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2282 8d208d34 2022-06-23 thomas int cols;
2283 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2284 05171be4 2022-06-23 thomas char *exstr = NULL;
2285 963b370f 2018-05-20 stsp size_t wlen;
2286 8d208d34 2022-06-23 thomas int i, scrollx;
2287 963b370f 2018-05-20 stsp
2288 963b370f 2018-05-20 stsp *wlinep = NULL;
2289 b700b5d6 2018-07-10 stsp *widthp = 0;
2290 963b370f 2018-05-20 stsp
2291 05171be4 2022-06-23 thomas if (expand) {
2292 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2293 05171be4 2022-06-23 thomas if (err)
2294 05171be4 2022-06-23 thomas return err;
2295 05171be4 2022-06-23 thomas }
2296 05171be4 2022-06-23 thomas
2297 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2298 05171be4 2022-06-23 thomas free(exstr);
2299 963b370f 2018-05-20 stsp if (err)
2300 963b370f 2018-05-20 stsp return err;
2301 963b370f 2018-05-20 stsp
2302 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2303 f91a2b48 2022-06-23 thomas
2304 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2305 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2306 3f670bfb 2020-12-10 stsp wlen--;
2307 3f670bfb 2020-12-10 stsp }
2308 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2309 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2310 3f670bfb 2020-12-10 stsp wlen--;
2311 3f670bfb 2020-12-10 stsp }
2312 3f670bfb 2020-12-10 stsp
2313 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2314 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2315 27a741e5 2019-09-11 stsp
2316 b700b5d6 2018-07-10 stsp if (widthp)
2317 b700b5d6 2018-07-10 stsp *widthp = cols;
2318 f91a2b48 2022-06-23 thomas if (scrollxp)
2319 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2320 963b370f 2018-05-20 stsp if (err)
2321 963b370f 2018-05-20 stsp free(wline);
2322 963b370f 2018-05-20 stsp else
2323 963b370f 2018-05-20 stsp *wlinep = wline;
2324 963b370f 2018-05-20 stsp return err;
2325 963b370f 2018-05-20 stsp }
2326 963b370f 2018-05-20 stsp
2327 8b473291 2019-02-21 stsp static const struct got_error*
2328 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2329 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2330 8b473291 2019-02-21 stsp {
2331 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2332 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2333 8b473291 2019-02-21 stsp char *s;
2334 8b473291 2019-02-21 stsp const char *name;
2335 8b473291 2019-02-21 stsp
2336 8b473291 2019-02-21 stsp *refs_str = NULL;
2337 00ddec2f 2023-05-17 thomas
2338 00ddec2f 2023-05-17 thomas if (refs == NULL)
2339 00ddec2f 2023-05-17 thomas return NULL;
2340 8b473291 2019-02-21 stsp
2341 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2342 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2343 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2344 52b5abe1 2019-08-13 stsp int cmp;
2345 52b5abe1 2019-08-13 stsp
2346 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2347 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2348 8b473291 2019-02-21 stsp continue;
2349 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2350 8b473291 2019-02-21 stsp name += 5;
2351 0d095295 2023-06-01 thomas if (strncmp(name, "got/", 4) == 0)
2352 7143d404 2019-03-12 stsp continue;
2353 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2354 8b473291 2019-02-21 stsp name += 6;
2355 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2356 8b473291 2019-02-21 stsp name += 8;
2357 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2358 081e3dc2 2023-06-15 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2359 79cc719f 2020-04-24 stsp continue;
2360 79cc719f 2020-04-24 stsp }
2361 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2362 48cae60d 2020-09-22 stsp if (err)
2363 48cae60d 2020-09-22 stsp break;
2364 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2365 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2366 5d844a1e 2019-08-13 stsp if (err) {
2367 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2368 48cae60d 2020-09-22 stsp free(ref_id);
2369 5d844a1e 2019-08-13 stsp break;
2370 48cae60d 2020-09-22 stsp }
2371 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2372 5d844a1e 2019-08-13 stsp err = NULL;
2373 5d844a1e 2019-08-13 stsp tag = NULL;
2374 5d844a1e 2019-08-13 stsp }
2375 52b5abe1 2019-08-13 stsp }
2376 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2377 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2378 48cae60d 2020-09-22 stsp free(ref_id);
2379 52b5abe1 2019-08-13 stsp if (tag)
2380 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2381 52b5abe1 2019-08-13 stsp if (cmp != 0)
2382 52b5abe1 2019-08-13 stsp continue;
2383 8b473291 2019-02-21 stsp s = *refs_str;
2384 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2385 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2386 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2387 8b473291 2019-02-21 stsp free(s);
2388 8b473291 2019-02-21 stsp *refs_str = NULL;
2389 8b473291 2019-02-21 stsp break;
2390 8b473291 2019-02-21 stsp }
2391 8b473291 2019-02-21 stsp free(s);
2392 8b473291 2019-02-21 stsp }
2393 8b473291 2019-02-21 stsp
2394 8b473291 2019-02-21 stsp return err;
2395 8b473291 2019-02-21 stsp }
2396 8b473291 2019-02-21 stsp
2397 963b370f 2018-05-20 stsp static const struct got_error *
2398 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2399 27a741e5 2019-09-11 stsp int col_tab_align)
2400 5813d178 2019-03-09 stsp {
2401 e6b8b890 2020-12-29 naddy char *smallerthan;
2402 5813d178 2019-03-09 stsp
2403 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2404 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2405 5813d178 2019-03-09 stsp author = smallerthan + 1;
2406 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2407 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2408 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2409 5813d178 2019-03-09 stsp }
2410 5813d178 2019-03-09 stsp
2411 5813d178 2019-03-09 stsp static const struct got_error *
2412 349dfd1e 2023-07-23 thomas draw_commit(struct tog_view *view, struct commit_queue_entry *entry,
2413 349dfd1e 2023-07-23 thomas const size_t date_display_cols, int author_display_cols)
2414 80ddbec8 2018-04-29 stsp {
2415 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2416 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2417 349dfd1e 2023-07-23 thomas struct got_commit_object *commit = entry->commit;
2418 349dfd1e 2023-07-23 thomas struct got_object_id *id = entry->id;
2419 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2420 61dc16bb 2023-05-17 thomas char *refs_str = NULL;
2421 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2422 5813d178 2019-03-09 stsp char *author = NULL;
2423 cabb4cfd 2023-06-01 thomas wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2424 cabb4cfd 2023-06-01 thomas int author_width, refstr_width, logmsg_width;
2425 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2426 cabb4cfd 2023-06-01 thomas int col, limit, scrollx, logmsg_x;
2427 349dfd1e 2023-07-23 thomas const int avail = view->ncols, marker_column = author_display_cols + 1;
2428 ccb26ccd 2018-11-05 stsp struct tm tm;
2429 45d799e2 2018-12-23 stsp time_t committer_time;
2430 11b20872 2019-11-08 stsp struct tog_color *tc;
2431 9472af95 2023-05-15 thomas struct got_reflist_head *refs;
2432 80ddbec8 2018-04-29 stsp
2433 92845f09 2023-07-26 thomas if (tog_base_commit.id != NULL && tog_base_commit.idx == -1 &&
2434 92845f09 2023-07-26 thomas got_object_id_cmp(id, tog_base_commit.id) == 0)
2435 92845f09 2023-07-26 thomas tog_base_commit.idx = entry->idx;
2436 3a333429 2023-07-26 thomas if (tog_io.wait_for_ui && s->thread_args.need_commit_marker) {
2437 3a333429 2023-07-26 thomas int rc;
2438 3a333429 2023-07-26 thomas
2439 3a333429 2023-07-26 thomas rc = pthread_cond_wait(&s->thread_args.log_loaded, &tog_mutex);
2440 3a333429 2023-07-26 thomas if (rc)
2441 3a333429 2023-07-26 thomas return got_error_set_errno(rc, "pthread_cond_wait");
2442 3a333429 2023-07-26 thomas }
2443 92845f09 2023-07-26 thomas
2444 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2445 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2446 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2447 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2448 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2449 b39d25c7 2018-07-10 stsp
2450 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2451 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2452 b39d25c7 2018-07-10 stsp else
2453 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2454 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2455 11b20872 2019-11-08 stsp if (tc)
2456 11b20872 2019-11-08 stsp wattr_on(view->window,
2457 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2458 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2459 11b20872 2019-11-08 stsp if (tc)
2460 11b20872 2019-11-08 stsp wattr_off(view->window,
2461 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2462 27a741e5 2019-09-11 stsp col = limit;
2463 b39d25c7 2018-07-10 stsp if (col > avail)
2464 b39d25c7 2018-07-10 stsp goto done;
2465 6570a66d 2019-11-08 stsp
2466 6570a66d 2019-11-08 stsp if (avail >= 120) {
2467 6570a66d 2019-11-08 stsp char *id_str;
2468 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2469 6570a66d 2019-11-08 stsp if (err)
2470 6570a66d 2019-11-08 stsp goto done;
2471 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2472 11b20872 2019-11-08 stsp if (tc)
2473 11b20872 2019-11-08 stsp wattr_on(view->window,
2474 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2475 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2476 11b20872 2019-11-08 stsp if (tc)
2477 11b20872 2019-11-08 stsp wattr_off(view->window,
2478 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2479 6570a66d 2019-11-08 stsp free(id_str);
2480 6570a66d 2019-11-08 stsp col += 9;
2481 6570a66d 2019-11-08 stsp if (col > avail)
2482 6570a66d 2019-11-08 stsp goto done;
2483 6570a66d 2019-11-08 stsp }
2484 b39d25c7 2018-07-10 stsp
2485 f69c5a46 2022-07-19 thomas if (s->use_committer)
2486 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2487 f69c5a46 2022-07-19 thomas else
2488 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2489 5813d178 2019-03-09 stsp if (author == NULL) {
2490 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2491 80ddbec8 2018-04-29 stsp goto done;
2492 80ddbec8 2018-04-29 stsp }
2493 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2494 bb737323 2018-05-20 stsp if (err)
2495 bb737323 2018-05-20 stsp goto done;
2496 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2497 11b20872 2019-11-08 stsp if (tc)
2498 11b20872 2019-11-08 stsp wattr_on(view->window,
2499 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2500 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2501 bb737323 2018-05-20 stsp col += author_width;
2502 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2503 92845f09 2023-07-26 thomas if (tog_base_commit.marker != GOT_WORKTREE_STATE_UNKNOWN &&
2504 349dfd1e 2023-07-23 thomas author_width == marker_column &&
2505 9403b695 2023-08-23 thomas entry->idx == tog_base_commit.idx && !s->limit_view) {
2506 f517f81a 2023-07-23 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2507 f517f81a 2023-07-23 thomas if (tc)
2508 f517f81a 2023-07-23 thomas wattr_on(view->window,
2509 f517f81a 2023-07-23 thomas COLOR_PAIR(tc->colorpair), NULL);
2510 349dfd1e 2023-07-23 thomas waddch(view->window, tog_base_commit.marker);
2511 f517f81a 2023-07-23 thomas if (tc)
2512 f517f81a 2023-07-23 thomas wattr_off(view->window,
2513 f517f81a 2023-07-23 thomas COLOR_PAIR(tc->colorpair), NULL);
2514 f517f81a 2023-07-23 thomas } else
2515 349dfd1e 2023-07-23 thomas waddch(view->window, ' ');
2516 bb737323 2018-05-20 stsp col++;
2517 bb737323 2018-05-20 stsp author_width++;
2518 bb737323 2018-05-20 stsp }
2519 f31d6c3b 2022-09-09 thomas if (tc)
2520 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2521 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2522 9c2eaf34 2018-05-20 stsp if (col > avail)
2523 9c2eaf34 2018-05-20 stsp goto done;
2524 80ddbec8 2018-04-29 stsp
2525 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2526 5943eee2 2019-08-13 stsp if (err)
2527 6d9fbc00 2018-04-29 stsp goto done;
2528 bb737323 2018-05-20 stsp logmsg = logmsg0;
2529 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2530 bb737323 2018-05-20 stsp logmsg++;
2531 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2532 bb737323 2018-05-20 stsp if (newline)
2533 bb737323 2018-05-20 stsp *newline = '\0';
2534 9472af95 2023-05-15 thomas
2535 cabb4cfd 2023-06-01 thomas limit = avail - col;
2536 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2537 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2538 cabb4cfd 2023-06-01 thomas
2539 9472af95 2023-05-15 thomas /* Prepend reference labels to log message if possible .*/
2540 9472af95 2023-05-15 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2541 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, id, s->repo);
2542 61dc16bb 2023-05-17 thomas if (err)
2543 61dc16bb 2023-05-17 thomas goto done;
2544 61dc16bb 2023-05-17 thomas if (refs_str) {
2545 cabb4cfd 2023-06-01 thomas char *rs;
2546 fcfb26c3 2023-05-26 thomas
2547 cabb4cfd 2023-06-01 thomas if (asprintf(&rs, "[%s]", refs_str) == -1) {
2548 9472af95 2023-05-15 thomas err = got_error_from_errno("asprintf");
2549 9472af95 2023-05-15 thomas goto done;
2550 9472af95 2023-05-15 thomas }
2551 cabb4cfd 2023-06-01 thomas err = format_line(&wrefstr, &refstr_width,
2552 cabb4cfd 2023-06-01 thomas &scrollx, rs, view->x, limit, col, 1);
2553 cabb4cfd 2023-06-01 thomas free(rs);
2554 cabb4cfd 2023-06-01 thomas if (err)
2555 cabb4cfd 2023-06-01 thomas goto done;
2556 9472af95 2023-05-15 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2557 9472af95 2023-05-15 thomas if (tc)
2558 9472af95 2023-05-15 thomas wattr_on(view->window,
2559 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2560 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wrefstr[scrollx]);
2561 9472af95 2023-05-15 thomas if (tc)
2562 9472af95 2023-05-15 thomas wattr_off(view->window,
2563 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2564 cabb4cfd 2023-06-01 thomas col += MAX(refstr_width, 0);
2565 cabb4cfd 2023-06-01 thomas if (col > avail)
2566 cabb4cfd 2023-06-01 thomas goto done;
2567 cabb4cfd 2023-06-01 thomas
2568 cabb4cfd 2023-06-01 thomas if (col < avail) {
2569 cabb4cfd 2023-06-01 thomas waddch(view->window, ' ');
2570 cabb4cfd 2023-06-01 thomas col++;
2571 cabb4cfd 2023-06-01 thomas }
2572 cabb4cfd 2023-06-01 thomas
2573 cabb4cfd 2023-06-01 thomas if (refstr_width > 0)
2574 cabb4cfd 2023-06-01 thomas logmsg_x = 0;
2575 cabb4cfd 2023-06-01 thomas else {
2576 cabb4cfd 2023-06-01 thomas int unscrolled_refstr_width;
2577 cabb4cfd 2023-06-01 thomas size_t len = wcslen(wrefstr);
2578 cabb4cfd 2023-06-01 thomas
2579 cabb4cfd 2023-06-01 thomas /*
2580 cabb4cfd 2023-06-01 thomas * No need to check for -1 return value here since
2581 cabb4cfd 2023-06-01 thomas * unprintables have been replaced by span_wline().
2582 cabb4cfd 2023-06-01 thomas */
2583 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width = wcswidth(wrefstr, len);
2584 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width += 1; /* trailing space */
2585 cabb4cfd 2023-06-01 thomas logmsg_x = view->x - unscrolled_refstr_width;
2586 cabb4cfd 2023-06-01 thomas }
2587 cabb4cfd 2023-06-01 thomas
2588 cabb4cfd 2023-06-01 thomas limit = avail - col;
2589 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2590 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2591 9472af95 2023-05-15 thomas } else
2592 cabb4cfd 2023-06-01 thomas logmsg_x = view->x;
2593 cabb4cfd 2023-06-01 thomas
2594 cabb4cfd 2023-06-01 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2595 cabb4cfd 2023-06-01 thomas limit, col, 1);
2596 cabb4cfd 2023-06-01 thomas if (err)
2597 cabb4cfd 2023-06-01 thomas goto done;
2598 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2599 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2600 27a741e5 2019-09-11 stsp while (col < avail) {
2601 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2602 bb737323 2018-05-20 stsp col++;
2603 881b2d3e 2018-04-30 stsp }
2604 80ddbec8 2018-04-29 stsp done:
2605 80ddbec8 2018-04-29 stsp free(logmsg0);
2606 bb737323 2018-05-20 stsp free(wlogmsg);
2607 cabb4cfd 2023-06-01 thomas free(wrefstr);
2608 61dc16bb 2023-05-17 thomas free(refs_str);
2609 5813d178 2019-03-09 stsp free(author);
2610 bb737323 2018-05-20 stsp free(wauthor);
2611 80ddbec8 2018-04-29 stsp free(line);
2612 80ddbec8 2018-04-29 stsp return err;
2613 80ddbec8 2018-04-29 stsp }
2614 26ed57b2 2018-05-19 stsp
2615 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2616 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2617 899d86c2 2018-05-10 stsp struct got_object_id *id)
2618 80ddbec8 2018-04-29 stsp {
2619 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2620 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2621 80ddbec8 2018-04-29 stsp
2622 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2623 80ddbec8 2018-04-29 stsp if (entry == NULL)
2624 899d86c2 2018-05-10 stsp return NULL;
2625 99db9666 2018-05-07 stsp
2626 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2627 d68b9737 2022-09-05 thomas if (dup == NULL) {
2628 d68b9737 2022-09-05 thomas free(entry);
2629 d68b9737 2022-09-05 thomas return NULL;
2630 d68b9737 2022-09-05 thomas }
2631 d68b9737 2022-09-05 thomas
2632 d68b9737 2022-09-05 thomas entry->id = dup;
2633 99db9666 2018-05-07 stsp entry->commit = commit;
2634 899d86c2 2018-05-10 stsp return entry;
2635 99db9666 2018-05-07 stsp }
2636 80ddbec8 2018-04-29 stsp
2637 99db9666 2018-05-07 stsp static void
2638 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2639 99db9666 2018-05-07 stsp {
2640 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2641 99db9666 2018-05-07 stsp
2642 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2643 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2644 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2645 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2646 d68b9737 2022-09-05 thomas free(entry->id);
2647 99db9666 2018-05-07 stsp free(entry);
2648 99db9666 2018-05-07 stsp }
2649 99db9666 2018-05-07 stsp
2650 99db9666 2018-05-07 stsp static void
2651 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2652 99db9666 2018-05-07 stsp {
2653 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2654 99db9666 2018-05-07 stsp pop_commit(commits);
2655 c4972b91 2018-05-07 stsp }
2656 c4972b91 2018-05-07 stsp
2657 c4972b91 2018-05-07 stsp static const struct got_error *
2658 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2659 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2660 13add988 2019-10-15 stsp {
2661 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2662 13add988 2019-10-15 stsp regmatch_t regmatch;
2663 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2664 13add988 2019-10-15 stsp
2665 13add988 2019-10-15 stsp *have_match = 0;
2666 13add988 2019-10-15 stsp
2667 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2668 13add988 2019-10-15 stsp if (err)
2669 13add988 2019-10-15 stsp return err;
2670 13add988 2019-10-15 stsp
2671 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2672 13add988 2019-10-15 stsp if (err)
2673 13add988 2019-10-15 stsp goto done;
2674 13add988 2019-10-15 stsp
2675 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2676 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2677 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2678 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2679 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2680 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2681 13add988 2019-10-15 stsp *have_match = 1;
2682 13add988 2019-10-15 stsp done:
2683 13add988 2019-10-15 stsp free(id_str);
2684 13add988 2019-10-15 stsp free(logmsg);
2685 13add988 2019-10-15 stsp return err;
2686 13add988 2019-10-15 stsp }
2687 13add988 2019-10-15 stsp
2688 13add988 2019-10-15 stsp static const struct got_error *
2689 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2690 c4972b91 2018-05-07 stsp {
2691 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2692 9ba79e04 2018-06-11 stsp
2693 1a76625f 2018-10-22 stsp /*
2694 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2695 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2696 1a76625f 2018-10-22 stsp * while updating the display.
2697 1a76625f 2018-10-22 stsp */
2698 4e0d2870 2020-12-07 naddy do {
2699 7210b715 2022-09-11 thomas struct got_object_id id;
2700 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2701 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2702 7e8004ba 2022-09-11 thomas int limit_match = 0;
2703 1a76625f 2018-10-22 stsp int errcode;
2704 899d86c2 2018-05-10 stsp
2705 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2706 4e0d2870 2020-12-07 naddy NULL, NULL);
2707 7210b715 2022-09-11 thomas if (err)
2708 ecb28ae0 2018-07-16 stsp break;
2709 899d86c2 2018-05-10 stsp
2710 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2711 9ba79e04 2018-06-11 stsp if (err)
2712 9ba79e04 2018-06-11 stsp break;
2713 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2714 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2715 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2716 9ba79e04 2018-06-11 stsp break;
2717 9ba79e04 2018-06-11 stsp }
2718 93e45b7c 2018-09-24 stsp
2719 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2720 1a76625f 2018-10-22 stsp if (errcode) {
2721 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2722 13add988 2019-10-15 stsp "pthread_mutex_lock");
2723 1a76625f 2018-10-22 stsp break;
2724 1a76625f 2018-10-22 stsp }
2725 1a76625f 2018-10-22 stsp
2726 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2727 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2728 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2729 1a76625f 2018-10-22 stsp
2730 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2731 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2732 7e8004ba 2022-09-11 thomas a->limit_regex);
2733 7e8004ba 2022-09-11 thomas if (err)
2734 7e8004ba 2022-09-11 thomas break;
2735 7e8004ba 2022-09-11 thomas
2736 7e8004ba 2022-09-11 thomas if (limit_match) {
2737 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2738 7e8004ba 2022-09-11 thomas
2739 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2740 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2741 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2742 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2743 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2744 7e8004ba 2022-09-11 thomas break;
2745 7e8004ba 2022-09-11 thomas }
2746 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2747 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2748 7e8004ba 2022-09-11 thomas
2749 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2750 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2751 7e8004ba 2022-09-11 thomas matched, entry);
2752 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2753 7e8004ba 2022-09-11 thomas }
2754 7e8004ba 2022-09-11 thomas
2755 7e8004ba 2022-09-11 thomas /*
2756 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2757 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2758 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2759 7e8004ba 2022-09-11 thomas */
2760 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2761 7e8004ba 2022-09-11 thomas }
2762 7e8004ba 2022-09-11 thomas
2763 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2764 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2765 7c1452c1 2020-03-26 stsp int have_match;
2766 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2767 7c1452c1 2020-03-26 stsp if (err)
2768 7c1452c1 2020-03-26 stsp break;
2769 7e8004ba 2022-09-11 thomas
2770 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2771 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2772 7e8004ba 2022-09-11 thomas *a->search_next_done =
2773 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2774 7e8004ba 2022-09-11 thomas } else if (have_match)
2775 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2776 13add988 2019-10-15 stsp }
2777 13add988 2019-10-15 stsp
2778 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2779 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2780 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2781 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2782 7c1452c1 2020-03-26 stsp if (err)
2783 13add988 2019-10-15 stsp break;
2784 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2785 899d86c2 2018-05-10 stsp
2786 9ba79e04 2018-06-11 stsp return err;
2787 0553a4e3 2018-04-30 stsp }
2788 0553a4e3 2018-04-30 stsp
2789 2b779855 2020-12-05 naddy static void
2790 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2791 2b779855 2020-12-05 naddy {
2792 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2793 2b779855 2020-12-05 naddy int ncommits = 0;
2794 2b779855 2020-12-05 naddy
2795 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2796 2b779855 2020-12-05 naddy while (entry) {
2797 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2798 2b779855 2020-12-05 naddy s->selected_entry = entry;
2799 2b779855 2020-12-05 naddy break;
2800 2b779855 2020-12-05 naddy }
2801 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2802 2b779855 2020-12-05 naddy ncommits++;
2803 2b779855 2020-12-05 naddy }
2804 2b779855 2020-12-05 naddy }
2805 2b779855 2020-12-05 naddy
2806 0553a4e3 2018-04-30 stsp static const struct got_error *
2807 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2808 0553a4e3 2018-04-30 stsp {
2809 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2810 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2811 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2812 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2813 60493ae3 2019-06-20 stsp int width;
2814 cabb4cfd 2023-06-01 thomas int ncommits, author_cols = 4, refstr_cols;
2815 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2816 8b473291 2019-02-21 stsp char *refs_str = NULL;
2817 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2818 11b20872 2019-11-08 stsp struct tog_color *tc;
2819 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2820 cabb4cfd 2023-06-01 thomas struct got_reflist_head *refs;
2821 bd3f8225 2022-08-12 thomas
2822 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2823 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2824 0553a4e3 2018-04-30 stsp
2825 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2826 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2827 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2828 1a76625f 2018-10-22 stsp if (err)
2829 ecb28ae0 2018-07-16 stsp return err;
2830 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2831 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2832 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2833 00ddec2f 2023-05-17 thomas s->repo);
2834 00ddec2f 2023-05-17 thomas if (err)
2835 00ddec2f 2023-05-17 thomas goto done;
2836 867c6645 2018-07-10 stsp }
2837 359bfafd 2019-02-22 stsp
2838 557d3365 2023-04-14 thomas if (s->thread_args.commits_needed == 0 && !using_mock_io)
2839 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2840 1a76625f 2018-10-22 stsp
2841 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2842 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2843 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2844 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2845 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2846 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2847 8f4ed634 2020-03-26 stsp goto done;
2848 8f4ed634 2020-03-26 stsp }
2849 8f4ed634 2020-03-26 stsp } else {
2850 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2851 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2852 f9686aa5 2020-03-27 stsp
2853 f9686aa5 2020-03-27 stsp if (view->searching) {
2854 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2855 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2856 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2857 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2858 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2859 f9686aa5 2020-03-27 stsp search_str = "searching...";
2860 f9686aa5 2020-03-27 stsp }
2861 f9686aa5 2020-03-27 stsp
2862 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2863 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2864 7e8004ba 2022-09-11 thomas
2865 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2866 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2867 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2868 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2869 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2870 8f4ed634 2020-03-26 stsp goto done;
2871 8f4ed634 2020-03-26 stsp }
2872 8b473291 2019-02-21 stsp }
2873 1a76625f 2018-10-22 stsp
2874 00580e07 2023-06-01 thomas free(refs_str);
2875 00580e07 2023-06-01 thomas refs_str = NULL;
2876 00580e07 2023-06-01 thomas
2877 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2878 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2879 91198554 2022-06-23 thomas "........................................",
2880 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2881 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2882 1a76625f 2018-10-22 stsp header = NULL;
2883 1a76625f 2018-10-22 stsp goto done;
2884 1a76625f 2018-10-22 stsp }
2885 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2886 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2887 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2888 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2889 1a76625f 2018-10-22 stsp header = NULL;
2890 1a76625f 2018-10-22 stsp goto done;
2891 ecb28ae0 2018-07-16 stsp }
2892 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2893 1a76625f 2018-10-22 stsp if (err)
2894 1a76625f 2018-10-22 stsp goto done;
2895 867c6645 2018-07-10 stsp
2896 2814baeb 2018-08-01 stsp werase(view->window);
2897 867c6645 2018-07-10 stsp
2898 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2899 a3404814 2018-09-02 stsp wstandout(view->window);
2900 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2901 11b20872 2019-11-08 stsp if (tc)
2902 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2903 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2904 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2905 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2906 1a76625f 2018-10-22 stsp width++;
2907 1a76625f 2018-10-22 stsp }
2908 86f4aab9 2022-09-09 thomas if (tc)
2909 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2910 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2911 a3404814 2018-09-02 stsp wstandend(view->window);
2912 ecb28ae0 2018-07-16 stsp free(wline);
2913 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2914 1a76625f 2018-10-22 stsp goto done;
2915 0553a4e3 2018-04-30 stsp
2916 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2917 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2918 5813d178 2019-03-09 stsp ncommits = 0;
2919 05171be4 2022-06-23 thomas view->maxx = 0;
2920 5813d178 2019-03-09 stsp while (entry) {
2921 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2922 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2923 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2924 5813d178 2019-03-09 stsp int width;
2925 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2926 5813d178 2019-03-09 stsp break;
2927 f69c5a46 2022-07-19 thomas if (s->use_committer)
2928 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2929 f69c5a46 2022-07-19 thomas else
2930 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2931 5813d178 2019-03-09 stsp if (author == NULL) {
2932 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2933 5813d178 2019-03-09 stsp goto done;
2934 5813d178 2019-03-09 stsp }
2935 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2936 27a741e5 2019-09-11 stsp date_display_cols);
2937 5813d178 2019-03-09 stsp if (author_cols < width)
2938 5813d178 2019-03-09 stsp author_cols = width;
2939 5813d178 2019-03-09 stsp free(wauthor);
2940 5813d178 2019-03-09 stsp free(author);
2941 ef944b8b 2022-07-21 thomas if (err)
2942 ef944b8b 2022-07-21 thomas goto done;
2943 cabb4cfd 2023-06-01 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2944 cabb4cfd 2023-06-01 thomas entry->id);
2945 cabb4cfd 2023-06-01 thomas err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2946 cabb4cfd 2023-06-01 thomas if (err)
2947 cabb4cfd 2023-06-01 thomas goto done;
2948 cabb4cfd 2023-06-01 thomas if (refs_str) {
2949 cabb4cfd 2023-06-01 thomas wchar_t *ws;
2950 cabb4cfd 2023-06-01 thomas err = format_line(&ws, &width, NULL, refs_str,
2951 cabb4cfd 2023-06-01 thomas 0, INT_MAX, date_display_cols + author_cols, 0);
2952 cabb4cfd 2023-06-01 thomas free(ws);
2953 00580e07 2023-06-01 thomas free(refs_str);
2954 00580e07 2023-06-01 thomas refs_str = NULL;
2955 cabb4cfd 2023-06-01 thomas if (err)
2956 cabb4cfd 2023-06-01 thomas goto done;
2957 cabb4cfd 2023-06-01 thomas refstr_cols = width + 3; /* account for [ ] + space */
2958 cabb4cfd 2023-06-01 thomas } else
2959 cabb4cfd 2023-06-01 thomas refstr_cols = 0;
2960 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2961 05171be4 2022-06-23 thomas if (err)
2962 05171be4 2022-06-23 thomas goto done;
2963 05171be4 2022-06-23 thomas msg = msg0;
2964 05171be4 2022-06-23 thomas while (*msg == '\n')
2965 05171be4 2022-06-23 thomas ++msg;
2966 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2967 331b1a16 2022-06-23 thomas *eol = '\0';
2968 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2969 cabb4cfd 2023-06-01 thomas date_display_cols + author_cols + refstr_cols, 0);
2970 331b1a16 2022-06-23 thomas if (err)
2971 331b1a16 2022-06-23 thomas goto done;
2972 cabb4cfd 2023-06-01 thomas view->maxx = MAX(view->maxx, width + refstr_cols);
2973 05171be4 2022-06-23 thomas free(msg0);
2974 331b1a16 2022-06-23 thomas free(wmsg);
2975 7ca04879 2019-10-19 stsp ncommits++;
2976 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2977 5813d178 2019-03-09 stsp }
2978 5813d178 2019-03-09 stsp
2979 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2980 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2981 867c6645 2018-07-10 stsp ncommits = 0;
2982 899d86c2 2018-05-10 stsp while (entry) {
2983 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2984 899d86c2 2018-05-10 stsp break;
2985 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2986 2814baeb 2018-08-01 stsp wstandout(view->window);
2987 349dfd1e 2023-07-23 thomas err = draw_commit(view, entry, date_display_cols, author_cols);
2988 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2989 2814baeb 2018-08-01 stsp wstandend(view->window);
2990 0553a4e3 2018-04-30 stsp if (err)
2991 60493ae3 2019-06-20 stsp goto done;
2992 0553a4e3 2018-04-30 stsp ncommits++;
2993 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2994 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2995 80ddbec8 2018-04-29 stsp }
2996 80ddbec8 2018-04-29 stsp
2997 a5d43cac 2022-07-01 thomas view_border(view);
2998 1a76625f 2018-10-22 stsp done:
2999 1a76625f 2018-10-22 stsp free(id_str);
3000 8b473291 2019-02-21 stsp free(refs_str);
3001 1a76625f 2018-10-22 stsp free(ncommits_str);
3002 1a76625f 2018-10-22 stsp free(header);
3003 80ddbec8 2018-04-29 stsp return err;
3004 9f7d7167 2018-04-29 stsp }
3005 07b55e75 2018-05-10 stsp
3006 07b55e75 2018-05-10 stsp static void
3007 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
3008 07b55e75 2018-05-10 stsp {
3009 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
3010 07b55e75 2018-05-10 stsp int nscrolled = 0;
3011 07b55e75 2018-05-10 stsp
3012 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
3013 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
3014 07b55e75 2018-05-10 stsp return;
3015 9f7d7167 2018-04-29 stsp
3016 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
3017 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
3018 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3019 07b55e75 2018-05-10 stsp if (entry) {
3020 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
3021 07b55e75 2018-05-10 stsp nscrolled++;
3022 07b55e75 2018-05-10 stsp }
3023 07b55e75 2018-05-10 stsp }
3024 aa075928 2018-05-10 stsp }
3025 aa075928 2018-05-10 stsp
3026 aa075928 2018-05-10 stsp static const struct got_error *
3027 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
3028 aa075928 2018-05-10 stsp {
3029 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
3030 5e224a3e 2019-02-22 stsp int errcode;
3031 8a42fca8 2019-02-22 stsp
3032 557d3365 2023-04-14 thomas if (!using_mock_io)
3033 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while loading commits */
3034 aa075928 2018-05-10 stsp
3035 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
3036 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
3037 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
3038 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
3039 7aafa0d1 2019-02-22 stsp if (errcode)
3040 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3041 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3042 7c1452c1 2020-03-26 stsp
3043 7c1452c1 2020-03-26 stsp /*
3044 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
3045 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
3046 7c1452c1 2020-03-26 stsp */
3047 7c1452c1 2020-03-26 stsp if (!wait)
3048 7c1452c1 2020-03-26 stsp break;
3049 7c1452c1 2020-03-26 stsp
3050 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3051 ffe38506 2020-12-01 naddy show_log_view(view);
3052 7c1452c1 2020-03-26 stsp update_panels();
3053 7c1452c1 2020-03-26 stsp doupdate();
3054 7c1452c1 2020-03-26 stsp
3055 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
3056 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3057 82954512 2020-02-03 stsp if (errcode)
3058 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
3059 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3060 82954512 2020-02-03 stsp
3061 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3062 ffe38506 2020-12-01 naddy show_log_view(view);
3063 7c1452c1 2020-03-26 stsp update_panels();
3064 7c1452c1 2020-03-26 stsp doupdate();
3065 5e224a3e 2019-02-22 stsp }
3066 5e224a3e 2019-02-22 stsp
3067 5e224a3e 2019-02-22 stsp return NULL;
3068 a5d43cac 2022-07-01 thomas }
3069 a5d43cac 2022-07-01 thomas
3070 a5d43cac 2022-07-01 thomas static const struct got_error *
3071 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
3072 a5d43cac 2022-07-01 thomas {
3073 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
3074 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3075 fe731b51 2022-07-14 thomas
3076 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
3077 fe731b51 2022-07-14 thomas return NULL;
3078 a5d43cac 2022-07-01 thomas
3079 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
3080 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
3081 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3082 a5d43cac 2022-07-01 thomas
3083 a5d43cac 2022-07-01 thomas return err;
3084 5e224a3e 2019-02-22 stsp }
3085 5e224a3e 2019-02-22 stsp
3086 5e224a3e 2019-02-22 stsp static const struct got_error *
3087 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
3088 5e224a3e 2019-02-22 stsp {
3089 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
3090 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
3091 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
3092 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
3093 5e224a3e 2019-02-22 stsp
3094 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
3095 5e224a3e 2019-02-22 stsp return NULL;
3096 5e224a3e 2019-02-22 stsp
3097 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3098 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
3099 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
3100 08ebd0a9 2019-02-22 stsp /*
3101 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
3102 08ebd0a9 2019-02-22 stsp */
3103 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
3104 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
3105 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3106 5e224a3e 2019-02-22 stsp if (err)
3107 5e224a3e 2019-02-22 stsp return err;
3108 7aafa0d1 2019-02-22 stsp }
3109 b295e71b 2019-02-22 stsp
3110 7aafa0d1 2019-02-22 stsp do {
3111 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3112 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3113 88048b54 2019-02-21 stsp break;
3114 88048b54 2019-02-21 stsp
3115 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
3116 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
3117 aa075928 2018-05-10 stsp
3118 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3119 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
3120 dd0a52c1 2018-05-20 stsp break;
3121 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
3122 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
3123 aa075928 2018-05-10 stsp
3124 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3125 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
3126 a5d43cac 2022-07-01 thomas else
3127 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3128 a5d43cac 2022-07-01 thomas
3129 dd0a52c1 2018-05-20 stsp return err;
3130 07b55e75 2018-05-10 stsp }
3131 4a7f7875 2018-05-10 stsp
3132 cd0acaa7 2018-05-20 stsp static const struct got_error *
3133 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3134 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
3135 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3136 cd0acaa7 2018-05-20 stsp {
3137 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3138 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3139 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3140 cd0acaa7 2018-05-20 stsp
3141 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3142 15a94983 2018-12-23 stsp if (diff_view == NULL)
3143 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3144 ea5e7bb5 2018-08-01 stsp
3145 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3146 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3147 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3148 e5a0f69f 2018-08-18 stsp if (err == NULL)
3149 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3150 cd0acaa7 2018-05-20 stsp return err;
3151 4a7f7875 2018-05-10 stsp }
3152 4a7f7875 2018-05-10 stsp
3153 80ddbec8 2018-04-29 stsp static const struct got_error *
3154 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3155 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3156 9343a5fb 2018-06-23 stsp {
3157 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3158 941e9f74 2019-05-21 stsp
3159 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3160 941e9f74 2019-05-21 stsp if (parent == NULL)
3161 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3162 941e9f74 2019-05-21 stsp
3163 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3164 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3165 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3166 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3167 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3168 941e9f74 2019-05-21 stsp s->tree = subtree;
3169 941e9f74 2019-05-21 stsp s->selected = 0;
3170 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3171 941e9f74 2019-05-21 stsp return NULL;
3172 941e9f74 2019-05-21 stsp }
3173 941e9f74 2019-05-21 stsp
3174 941e9f74 2019-05-21 stsp static const struct got_error *
3175 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3176 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3177 941e9f74 2019-05-21 stsp {
3178 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3179 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3180 941e9f74 2019-05-21 stsp const char *p;
3181 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3182 9343a5fb 2018-06-23 stsp
3183 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3184 941e9f74 2019-05-21 stsp p = path;
3185 941e9f74 2019-05-21 stsp while (*p) {
3186 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3187 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3188 56e0773d 2019-11-28 stsp char *te_name;
3189 33cbf02b 2020-01-12 stsp
3190 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3191 33cbf02b 2020-01-12 stsp p++;
3192 941e9f74 2019-05-21 stsp
3193 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3194 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3195 941e9f74 2019-05-21 stsp if (slash == NULL)
3196 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3197 33cbf02b 2020-01-12 stsp else
3198 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3199 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3200 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3201 56e0773d 2019-11-28 stsp break;
3202 941e9f74 2019-05-21 stsp }
3203 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3204 56e0773d 2019-11-28 stsp if (te == NULL) {
3205 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3206 56e0773d 2019-11-28 stsp free(te_name);
3207 941e9f74 2019-05-21 stsp break;
3208 941e9f74 2019-05-21 stsp }
3209 56e0773d 2019-11-28 stsp free(te_name);
3210 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3211 941e9f74 2019-05-21 stsp
3212 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3213 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3214 b03c880f 2019-05-21 stsp
3215 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3216 941e9f74 2019-05-21 stsp if (slash)
3217 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3218 941e9f74 2019-05-21 stsp else
3219 941e9f74 2019-05-21 stsp subpath = strdup(path);
3220 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3221 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3222 941e9f74 2019-05-21 stsp break;
3223 941e9f74 2019-05-21 stsp }
3224 941e9f74 2019-05-21 stsp
3225 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3226 941e9f74 2019-05-21 stsp subpath);
3227 941e9f74 2019-05-21 stsp if (err)
3228 941e9f74 2019-05-21 stsp break;
3229 941e9f74 2019-05-21 stsp
3230 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3231 941e9f74 2019-05-21 stsp free(tree_id);
3232 941e9f74 2019-05-21 stsp if (err)
3233 941e9f74 2019-05-21 stsp break;
3234 941e9f74 2019-05-21 stsp
3235 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3236 941e9f74 2019-05-21 stsp if (err) {
3237 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3238 941e9f74 2019-05-21 stsp break;
3239 941e9f74 2019-05-21 stsp }
3240 941e9f74 2019-05-21 stsp if (slash == NULL)
3241 941e9f74 2019-05-21 stsp break;
3242 941e9f74 2019-05-21 stsp free(subpath);
3243 941e9f74 2019-05-21 stsp subpath = NULL;
3244 941e9f74 2019-05-21 stsp p = slash;
3245 941e9f74 2019-05-21 stsp }
3246 941e9f74 2019-05-21 stsp
3247 941e9f74 2019-05-21 stsp free(subpath);
3248 1a76625f 2018-10-22 stsp return err;
3249 61266923 2020-01-14 stsp }
3250 61266923 2020-01-14 stsp
3251 61266923 2020-01-14 stsp static const struct got_error *
3252 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3253 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3254 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3255 55cccc34 2020-02-20 stsp {
3256 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3257 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3258 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3259 55cccc34 2020-02-20 stsp
3260 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3261 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3262 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3263 55cccc34 2020-02-20 stsp
3264 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3265 bc573f3b 2021-07-10 stsp if (err)
3266 55cccc34 2020-02-20 stsp return err;
3267 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3268 55cccc34 2020-02-20 stsp
3269 55cccc34 2020-02-20 stsp *new_view = tree_view;
3270 55cccc34 2020-02-20 stsp
3271 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3272 55cccc34 2020-02-20 stsp return NULL;
3273 55cccc34 2020-02-20 stsp
3274 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3275 55cccc34 2020-02-20 stsp }
3276 55cccc34 2020-02-20 stsp
3277 55cccc34 2020-02-20 stsp static const struct got_error *
3278 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3279 61266923 2020-01-14 stsp {
3280 61266923 2020-01-14 stsp sigset_t sigset;
3281 61266923 2020-01-14 stsp int errcode;
3282 61266923 2020-01-14 stsp
3283 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3284 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3285 61266923 2020-01-14 stsp
3286 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3287 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3288 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3289 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3290 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3291 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3292 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3293 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3294 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3295 61266923 2020-01-14 stsp
3296 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3297 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3298 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3299 61266923 2020-01-14 stsp
3300 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3301 61266923 2020-01-14 stsp if (errcode)
3302 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3303 61266923 2020-01-14 stsp
3304 61266923 2020-01-14 stsp return NULL;
3305 1a76625f 2018-10-22 stsp }
3306 1a76625f 2018-10-22 stsp
3307 1a76625f 2018-10-22 stsp static void *
3308 1a76625f 2018-10-22 stsp log_thread(void *arg)
3309 1a76625f 2018-10-22 stsp {
3310 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3311 1a76625f 2018-10-22 stsp int errcode = 0;
3312 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3313 1a76625f 2018-10-22 stsp int done = 0;
3314 61266923 2020-01-14 stsp
3315 f2d749db 2022-07-12 thomas /*
3316 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3317 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3318 f2d749db 2022-07-12 thomas */
3319 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3320 f2d749db 2022-07-12 thomas if (errcode) {
3321 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3322 61266923 2020-01-14 stsp return (void *)err;
3323 f2d749db 2022-07-12 thomas }
3324 1a76625f 2018-10-22 stsp
3325 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3326 f2d749db 2022-07-12 thomas if (err) {
3327 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3328 f2d749db 2022-07-12 thomas goto done;
3329 f2d749db 2022-07-12 thomas }
3330 f2d749db 2022-07-12 thomas
3331 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3332 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3333 f2d749db 2022-07-12 thomas if (errcode) {
3334 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3335 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3336 f2d749db 2022-07-12 thomas goto done;
3337 f2d749db 2022-07-12 thomas }
3338 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3339 1a76625f 2018-10-22 stsp if (err) {
3340 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3341 f2d749db 2022-07-12 thomas goto done;
3342 1a76625f 2018-10-22 stsp err = NULL;
3343 1a76625f 2018-10-22 stsp done = 1;
3344 92845f09 2023-07-26 thomas a->commits_needed = 0;
3345 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3346 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3347 7e8004ba 2022-09-11 thomas if (a->limit_match)
3348 7e8004ba 2022-09-11 thomas a->commits_needed--;
3349 7e8004ba 2022-09-11 thomas } else
3350 7e8004ba 2022-09-11 thomas a->commits_needed--;
3351 7e8004ba 2022-09-11 thomas }
3352 1a76625f 2018-10-22 stsp
3353 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3354 3abe8080 2019-04-10 stsp if (errcode) {
3355 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3356 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3357 f2d749db 2022-07-12 thomas goto done;
3358 3abe8080 2019-04-10 stsp } else if (*a->quit)
3359 1a76625f 2018-10-22 stsp done = 1;
3360 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3361 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3362 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3363 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3364 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3365 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3366 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3367 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3368 1a76625f 2018-10-22 stsp }
3369 1a76625f 2018-10-22 stsp
3370 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3371 7c1452c1 2020-03-26 stsp if (errcode) {
3372 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3373 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3374 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3375 f2d749db 2022-07-12 thomas goto done;
3376 7c1452c1 2020-03-26 stsp }
3377 7c1452c1 2020-03-26 stsp
3378 92845f09 2023-07-26 thomas if (a->commits_needed == 0 &&
3379 92845f09 2023-07-26 thomas a->need_commit_marker && a->worktree) {
3380 92845f09 2023-07-26 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3381 92845f09 2023-07-26 thomas if (errcode) {
3382 92845f09 2023-07-26 thomas err = got_error_set_errno(errcode,
3383 92845f09 2023-07-26 thomas "pthread_mutex_unlock");
3384 92845f09 2023-07-26 thomas goto done;
3385 92845f09 2023-07-26 thomas }
3386 92845f09 2023-07-26 thomas err = got_worktree_get_state(&tog_base_commit.marker,
3387 6ebb2263 2023-07-26 thomas a->repo, a->worktree, NULL, NULL);
3388 92845f09 2023-07-26 thomas if (err)
3389 92845f09 2023-07-26 thomas goto done;
3390 92845f09 2023-07-26 thomas errcode = pthread_mutex_lock(&tog_mutex);
3391 92845f09 2023-07-26 thomas if (errcode) {
3392 92845f09 2023-07-26 thomas err = got_error_set_errno(errcode,
3393 92845f09 2023-07-26 thomas "pthread_mutex_lock");
3394 92845f09 2023-07-26 thomas goto done;
3395 92845f09 2023-07-26 thomas }
3396 92845f09 2023-07-26 thomas a->need_commit_marker = 0;
3397 92845f09 2023-07-26 thomas /*
3398 92845f09 2023-07-26 thomas * The main thread did not close this
3399 92845f09 2023-07-26 thomas * work tree yet. Close it now.
3400 92845f09 2023-07-26 thomas */
3401 92845f09 2023-07-26 thomas got_worktree_close(a->worktree);
3402 92845f09 2023-07-26 thomas a->worktree = NULL;
3403 92845f09 2023-07-26 thomas
3404 92845f09 2023-07-26 thomas if (*a->quit)
3405 92845f09 2023-07-26 thomas done = 1;
3406 92845f09 2023-07-26 thomas }
3407 92845f09 2023-07-26 thomas
3408 1a76625f 2018-10-22 stsp if (done)
3409 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3410 7c1452c1 2020-03-26 stsp else {
3411 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3412 92845f09 2023-07-26 thomas if (tog_io.wait_for_ui) {
3413 92845f09 2023-07-26 thomas errcode = pthread_cond_signal(
3414 92845f09 2023-07-26 thomas &a->log_loaded);
3415 92845f09 2023-07-26 thomas if (errcode && err == NULL)
3416 92845f09 2023-07-26 thomas err = got_error_set_errno(
3417 92845f09 2023-07-26 thomas errcode,
3418 92845f09 2023-07-26 thomas "pthread_cond_signal");
3419 92845f09 2023-07-26 thomas }
3420 92845f09 2023-07-26 thomas
3421 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3422 7c1452c1 2020-03-26 stsp &tog_mutex);
3423 f2d749db 2022-07-12 thomas if (errcode) {
3424 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3425 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3426 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3427 f2d749db 2022-07-12 thomas goto done;
3428 f2d749db 2022-07-12 thomas }
3429 21355643 2020-12-06 stsp if (*a->quit)
3430 21355643 2020-12-06 stsp done = 1;
3431 7c1452c1 2020-03-26 stsp }
3432 1a76625f 2018-10-22 stsp }
3433 1a76625f 2018-10-22 stsp }
3434 3abe8080 2019-04-10 stsp a->log_complete = 1;
3435 92845f09 2023-07-26 thomas if (tog_io.wait_for_ui) {
3436 92845f09 2023-07-26 thomas errcode = pthread_cond_signal(&a->log_loaded);
3437 92845f09 2023-07-26 thomas if (errcode && err == NULL)
3438 92845f09 2023-07-26 thomas err = got_error_set_errno(errcode,
3439 92845f09 2023-07-26 thomas "pthread_cond_signal");
3440 92845f09 2023-07-26 thomas }
3441 92845f09 2023-07-26 thomas
3442 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3443 f2d749db 2022-07-12 thomas if (errcode)
3444 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3445 f2d749db 2022-07-12 thomas done:
3446 f2d749db 2022-07-12 thomas if (err) {
3447 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3448 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3449 92845f09 2023-07-26 thomas if (a->worktree) {
3450 92845f09 2023-07-26 thomas got_worktree_close(a->worktree);
3451 92845f09 2023-07-26 thomas a->worktree = NULL;
3452 92845f09 2023-07-26 thomas }
3453 f2d749db 2022-07-12 thomas }
3454 1a76625f 2018-10-22 stsp return (void *)err;
3455 1a76625f 2018-10-22 stsp }
3456 1a76625f 2018-10-22 stsp
3457 1a76625f 2018-10-22 stsp static const struct got_error *
3458 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3459 1a76625f 2018-10-22 stsp {
3460 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3461 1a76625f 2018-10-22 stsp int errcode;
3462 1a76625f 2018-10-22 stsp
3463 1a76625f 2018-10-22 stsp if (s->thread) {
3464 1a76625f 2018-10-22 stsp s->quit = 1;
3465 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3466 1a76625f 2018-10-22 stsp if (errcode)
3467 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3468 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3469 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3470 1a76625f 2018-10-22 stsp if (errcode)
3471 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3472 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3473 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3474 1a76625f 2018-10-22 stsp if (errcode)
3475 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3476 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3477 1a76625f 2018-10-22 stsp if (errcode)
3478 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3479 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3480 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3481 1a76625f 2018-10-22 stsp }
3482 1a76625f 2018-10-22 stsp
3483 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3484 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3485 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3486 1af5eddf 2022-06-23 thomas }
3487 1af5eddf 2022-06-23 thomas
3488 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3489 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3490 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3491 1af5eddf 2022-06-23 thomas if (err == NULL)
3492 1af5eddf 2022-06-23 thomas err = pack_err;
3493 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3494 1a76625f 2018-10-22 stsp }
3495 1a76625f 2018-10-22 stsp
3496 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3497 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3498 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3499 1a76625f 2018-10-22 stsp }
3500 1a76625f 2018-10-22 stsp
3501 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3502 9343a5fb 2018-06-23 stsp }
3503 9343a5fb 2018-06-23 stsp
3504 9343a5fb 2018-06-23 stsp static const struct got_error *
3505 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3506 1a76625f 2018-10-22 stsp {
3507 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3508 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3509 276b94a1 2020-11-13 naddy int errcode;
3510 1a76625f 2018-10-22 stsp
3511 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3512 276b94a1 2020-11-13 naddy
3513 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3514 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3515 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3516 276b94a1 2020-11-13 naddy
3517 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3518 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3519 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3520 276b94a1 2020-11-13 naddy
3521 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3522 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3523 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3524 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3525 1a76625f 2018-10-22 stsp free(s->start_id);
3526 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3527 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3528 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3529 1a76625f 2018-10-22 stsp return err;
3530 1a76625f 2018-10-22 stsp }
3531 1a76625f 2018-10-22 stsp
3532 7e8004ba 2022-09-11 thomas /*
3533 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3534 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3535 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3536 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3537 7e8004ba 2022-09-11 thomas * works with very slight change.
3538 7e8004ba 2022-09-11 thomas */
3539 1a76625f 2018-10-22 stsp static const struct got_error *
3540 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3541 7e8004ba 2022-09-11 thomas {
3542 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3543 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3544 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3545 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3546 7e8004ba 2022-09-11 thomas char pattern[1024];
3547 7e8004ba 2022-09-11 thomas int ret;
3548 7e8004ba 2022-09-11 thomas
3549 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3550 7e8004ba 2022-09-11 thomas v = view->child;
3551 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3552 7e8004ba 2022-09-11 thomas v = view->parent;
3553 7e8004ba 2022-09-11 thomas
3554 7e8004ba 2022-09-11 thomas /* Get the pattern */
3555 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3556 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3557 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3558 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3559 7e8004ba 2022-09-11 thomas nocbreak();
3560 7e8004ba 2022-09-11 thomas echo();
3561 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3562 7e8004ba 2022-09-11 thomas cbreak();
3563 7e8004ba 2022-09-11 thomas noecho();
3564 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3565 7e8004ba 2022-09-11 thomas if (ret == ERR)
3566 7e8004ba 2022-09-11 thomas return NULL;
3567 7e8004ba 2022-09-11 thomas
3568 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3569 7e8004ba 2022-09-11 thomas /*
3570 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3571 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3572 7e8004ba 2022-09-11 thomas */
3573 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3574 7e8004ba 2022-09-11 thomas return NULL;
3575 7e8004ba 2022-09-11 thomas
3576 7e8004ba 2022-09-11 thomas /*
3577 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3578 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3579 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3580 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3581 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3582 7e8004ba 2022-09-11 thomas * the queue.
3583 7e8004ba 2022-09-11 thomas */
3584 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3585 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3586 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3587 7e8004ba 2022-09-11 thomas s->selected = 0;
3588 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3589 7e8004ba 2022-09-11 thomas
3590 7e8004ba 2022-09-11 thomas return NULL;
3591 7e8004ba 2022-09-11 thomas }
3592 7e8004ba 2022-09-11 thomas
3593 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3594 7e8004ba 2022-09-11 thomas return NULL;
3595 7e8004ba 2022-09-11 thomas
3596 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3597 7e8004ba 2022-09-11 thomas
3598 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3599 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3600 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3601 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3602 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3603 7e8004ba 2022-09-11 thomas
3604 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3605 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3606 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3607 7e8004ba 2022-09-11 thomas
3608 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3609 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3610 7e8004ba 2022-09-11 thomas int have_match = 0;
3611 7e8004ba 2022-09-11 thomas
3612 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3613 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3614 7e8004ba 2022-09-11 thomas if (err)
3615 7e8004ba 2022-09-11 thomas return err;
3616 7e8004ba 2022-09-11 thomas
3617 7e8004ba 2022-09-11 thomas if (have_match) {
3618 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3619 7e8004ba 2022-09-11 thomas
3620 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3621 7e8004ba 2022-09-11 thomas entry->id);
3622 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3623 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3624 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3625 7e8004ba 2022-09-11 thomas break;
3626 7e8004ba 2022-09-11 thomas }
3627 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3628 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3629 7e8004ba 2022-09-11 thomas
3630 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3631 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3632 7e8004ba 2022-09-11 thomas matched, entry);
3633 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3634 7e8004ba 2022-09-11 thomas }
3635 7e8004ba 2022-09-11 thomas }
3636 7e8004ba 2022-09-11 thomas
3637 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3638 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3639 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3640 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3641 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3642 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3643 7e8004ba 2022-09-11 thomas if (err)
3644 7e8004ba 2022-09-11 thomas return err;
3645 7e8004ba 2022-09-11 thomas }
3646 7e8004ba 2022-09-11 thomas
3647 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3648 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3649 7e8004ba 2022-09-11 thomas s->selected = 0;
3650 7e8004ba 2022-09-11 thomas
3651 7e8004ba 2022-09-11 thomas return NULL;
3652 7e8004ba 2022-09-11 thomas }
3653 7e8004ba 2022-09-11 thomas
3654 7e8004ba 2022-09-11 thomas static const struct got_error *
3655 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3656 60493ae3 2019-06-20 stsp {
3657 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3658 60493ae3 2019-06-20 stsp
3659 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3660 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3661 60493ae3 2019-06-20 stsp return NULL;
3662 60493ae3 2019-06-20 stsp }
3663 60493ae3 2019-06-20 stsp
3664 60493ae3 2019-06-20 stsp static const struct got_error *
3665 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3666 60493ae3 2019-06-20 stsp {
3667 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3668 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3669 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3670 60493ae3 2019-06-20 stsp
3671 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3672 f9686aa5 2020-03-27 stsp show_log_view(view);
3673 f9686aa5 2020-03-27 stsp update_panels();
3674 f9686aa5 2020-03-27 stsp doupdate();
3675 f9686aa5 2020-03-27 stsp
3676 96e2b566 2019-07-08 stsp if (s->search_entry) {
3677 13add988 2019-10-15 stsp int errcode, ch;
3678 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3679 13add988 2019-10-15 stsp if (errcode)
3680 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3681 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3682 13add988 2019-10-15 stsp ch = wgetch(view->window);
3683 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3684 13add988 2019-10-15 stsp if (errcode)
3685 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3686 13add988 2019-10-15 stsp "pthread_mutex_lock");
3687 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3688 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3689 678cbce5 2019-07-28 stsp return NULL;
3690 678cbce5 2019-07-28 stsp }
3691 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3692 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3693 96e2b566 2019-07-08 stsp else
3694 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3695 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3696 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3697 df5e841d 2022-06-23 thomas /*
3698 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3699 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3700 1f4d5162 2022-06-23 thomas * might have changed.
3701 df5e841d 2022-06-23 thomas */
3702 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3703 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3704 e7baaec8 2022-09-13 thomas else
3705 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3706 e7baaec8 2022-09-13 thomas entry);
3707 20be8d96 2019-06-21 stsp } else {
3708 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3709 20be8d96 2019-06-21 stsp }
3710 60493ae3 2019-06-20 stsp
3711 60493ae3 2019-06-20 stsp while (1) {
3712 13add988 2019-10-15 stsp int have_match = 0;
3713 13add988 2019-10-15 stsp
3714 60493ae3 2019-06-20 stsp if (entry == NULL) {
3715 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3716 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3717 f9967bca 2020-03-27 stsp view->search_next_done =
3718 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3719 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3720 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3721 f801134a 2019-06-25 stsp return NULL;
3722 60493ae3 2019-06-20 stsp }
3723 96e2b566 2019-07-08 stsp /*
3724 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3725 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3726 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3727 96e2b566 2019-07-08 stsp */
3728 49dff0bd 2023-08-12 thomas s->search_entry = s->selected_entry;
3729 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3730 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3731 60493ae3 2019-06-20 stsp }
3732 60493ae3 2019-06-20 stsp
3733 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3734 13add988 2019-10-15 stsp &view->regex);
3735 5943eee2 2019-08-13 stsp if (err)
3736 13add988 2019-10-15 stsp break;
3737 13add988 2019-10-15 stsp if (have_match) {
3738 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3739 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3740 60493ae3 2019-06-20 stsp break;
3741 60493ae3 2019-06-20 stsp }
3742 13add988 2019-10-15 stsp
3743 96e2b566 2019-07-08 stsp s->search_entry = entry;
3744 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3745 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3746 b1bf1435 2019-06-21 stsp else
3747 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3748 60493ae3 2019-06-20 stsp }
3749 60493ae3 2019-06-20 stsp
3750 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3751 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3752 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3753 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3754 60493ae3 2019-06-20 stsp if (err)
3755 60493ae3 2019-06-20 stsp return err;
3756 ead14cbe 2019-06-21 stsp cur++;
3757 ead14cbe 2019-06-21 stsp }
3758 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3759 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3760 60493ae3 2019-06-20 stsp if (err)
3761 60493ae3 2019-06-20 stsp return err;
3762 ead14cbe 2019-06-21 stsp cur--;
3763 60493ae3 2019-06-20 stsp }
3764 60493ae3 2019-06-20 stsp }
3765 60493ae3 2019-06-20 stsp
3766 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3767 96e2b566 2019-07-08 stsp
3768 60493ae3 2019-06-20 stsp return NULL;
3769 60493ae3 2019-06-20 stsp }
3770 60493ae3 2019-06-20 stsp
3771 60493ae3 2019-06-20 stsp static const struct got_error *
3772 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3773 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3774 92845f09 2023-07-26 thomas const char *in_repo_path, int log_branches,
3775 92845f09 2023-07-26 thomas struct got_worktree *worktree)
3776 80ddbec8 2018-04-29 stsp {
3777 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3778 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3779 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3780 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3781 1a76625f 2018-10-22 stsp int errcode;
3782 80ddbec8 2018-04-29 stsp
3783 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3784 f135c941 2020-02-20 stsp free(s->in_repo_path);
3785 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3786 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3787 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3788 51b7e1c3 2023-04-14 thomas goto done;
3789 51b7e1c3 2023-04-14 thomas }
3790 f135c941 2020-02-20 stsp }
3791 ecb28ae0 2018-07-16 stsp
3792 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3793 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3794 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3795 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3796 78756c87 2020-11-24 stsp
3797 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3798 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3799 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3800 7e8004ba 2022-09-11 thomas
3801 fb2756b9 2018-08-04 stsp s->repo = repo;
3802 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3803 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3804 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3805 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3806 9cd7cbd1 2020-12-07 stsp goto done;
3807 9cd7cbd1 2020-12-07 stsp }
3808 9cd7cbd1 2020-12-07 stsp }
3809 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3810 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3811 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3812 5036bf37 2018-09-24 stsp goto done;
3813 5036bf37 2018-09-24 stsp }
3814 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3815 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3816 e5a0f69f 2018-08-18 stsp
3817 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3818 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3819 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3820 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3821 11b20872 2019-11-08 stsp if (err)
3822 11b20872 2019-11-08 stsp goto done;
3823 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3824 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3825 11b20872 2019-11-08 stsp if (err) {
3826 11b20872 2019-11-08 stsp free_colors(&s->colors);
3827 11b20872 2019-11-08 stsp goto done;
3828 11b20872 2019-11-08 stsp }
3829 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3830 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3831 11b20872 2019-11-08 stsp if (err) {
3832 11b20872 2019-11-08 stsp free_colors(&s->colors);
3833 11b20872 2019-11-08 stsp goto done;
3834 11b20872 2019-11-08 stsp }
3835 11b20872 2019-11-08 stsp }
3836 11b20872 2019-11-08 stsp
3837 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3838 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3839 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3840 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3841 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3842 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3843 1a76625f 2018-10-22 stsp
3844 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3845 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3846 1af5eddf 2022-06-23 thomas if (err)
3847 1af5eddf 2022-06-23 thomas goto done;
3848 1af5eddf 2022-06-23 thomas }
3849 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3850 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3851 7cd52833 2022-06-23 thomas if (err)
3852 7cd52833 2022-06-23 thomas goto done;
3853 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3854 b672a97a 2020-01-27 stsp !s->log_branches);
3855 1a76625f 2018-10-22 stsp if (err)
3856 1a76625f 2018-10-22 stsp goto done;
3857 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3858 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3859 c5b78334 2020-01-12 stsp if (err)
3860 c5b78334 2020-01-12 stsp goto done;
3861 1a76625f 2018-10-22 stsp
3862 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3863 1a76625f 2018-10-22 stsp if (errcode) {
3864 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3865 1a76625f 2018-10-22 stsp goto done;
3866 1a76625f 2018-10-22 stsp }
3867 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3868 7c1452c1 2020-03-26 stsp if (errcode) {
3869 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3870 7c1452c1 2020-03-26 stsp goto done;
3871 7c1452c1 2020-03-26 stsp }
3872 1a76625f 2018-10-22 stsp
3873 92845f09 2023-07-26 thomas if (using_mock_io) {
3874 92845f09 2023-07-26 thomas int rc;
3875 92845f09 2023-07-26 thomas
3876 92845f09 2023-07-26 thomas rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3877 92845f09 2023-07-26 thomas if (rc)
3878 92845f09 2023-07-26 thomas return got_error_set_errno(rc, "pthread_cond_init");
3879 92845f09 2023-07-26 thomas }
3880 92845f09 2023-07-26 thomas
3881 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3882 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3883 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3884 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3885 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3886 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3887 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3888 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3889 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3890 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3891 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3892 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3893 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3894 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3895 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3896 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3897 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3898 92845f09 2023-07-26 thomas s->thread_args.worktree = worktree;
3899 92845f09 2023-07-26 thomas if (worktree)
3900 92845f09 2023-07-26 thomas s->thread_args.need_commit_marker = 1;
3901 ba4f502b 2018-08-04 stsp done:
3902 51b7e1c3 2023-04-14 thomas if (err) {
3903 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3904 51b7e1c3 2023-04-14 thomas close_log_view(view);
3905 51b7e1c3 2023-04-14 thomas view_close(view);
3906 51b7e1c3 2023-04-14 thomas }
3907 ba4f502b 2018-08-04 stsp return err;
3908 ba4f502b 2018-08-04 stsp }
3909 ba4f502b 2018-08-04 stsp
3910 e5a0f69f 2018-08-18 stsp static const struct got_error *
3911 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3912 ba4f502b 2018-08-04 stsp {
3913 f2f6d207 2020-11-24 stsp const struct got_error *err;
3914 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3915 ba4f502b 2018-08-04 stsp
3916 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3917 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3918 2b380cc8 2018-10-24 stsp &s->thread_args);
3919 2b380cc8 2018-10-24 stsp if (errcode)
3920 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3921 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3922 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3923 f2f6d207 2020-11-24 stsp if (err)
3924 f2f6d207 2020-11-24 stsp return err;
3925 f2f6d207 2020-11-24 stsp }
3926 2b380cc8 2018-10-24 stsp }
3927 2b380cc8 2018-10-24 stsp
3928 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3929 b31f89ff 2022-07-01 thomas }
3930 b31f89ff 2022-07-01 thomas
3931 b31f89ff 2022-07-01 thomas static void
3932 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3933 b31f89ff 2022-07-01 thomas {
3934 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3935 b31f89ff 2022-07-01 thomas
3936 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3937 b31f89ff 2022-07-01 thomas return;
3938 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3939 7e8004ba 2022-09-11 thomas view->count = 0;
3940 b31f89ff 2022-07-01 thomas
3941 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3942 b31f89ff 2022-07-01 thomas || home)
3943 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3944 b31f89ff 2022-07-01 thomas
3945 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3946 b31f89ff 2022-07-01 thomas --s->selected;
3947 b31f89ff 2022-07-01 thomas else
3948 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3949 b31f89ff 2022-07-01 thomas
3950 b31f89ff 2022-07-01 thomas select_commit(s);
3951 b31f89ff 2022-07-01 thomas return;
3952 b31f89ff 2022-07-01 thomas }
3953 b31f89ff 2022-07-01 thomas
3954 b31f89ff 2022-07-01 thomas static const struct got_error *
3955 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3956 b31f89ff 2022-07-01 thomas {
3957 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3958 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3959 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3960 b31f89ff 2022-07-01 thomas
3961 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3962 7e8004ba 2022-09-11 thomas return NULL;
3963 7e8004ba 2022-09-11 thomas
3964 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3965 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3966 b31f89ff 2022-07-01 thomas return NULL;
3967 b31f89ff 2022-07-01 thomas
3968 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3969 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3970 b31f89ff 2022-07-01 thomas
3971 7ed048bd 2022-08-12 thomas if (!page) {
3972 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3973 b31f89ff 2022-07-01 thomas ++s->selected;
3974 b31f89ff 2022-07-01 thomas else
3975 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3976 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3977 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3978 7ed048bd 2022-08-12 thomas int n;
3979 7ed048bd 2022-08-12 thomas
3980 7ed048bd 2022-08-12 thomas s->selected = 0;
3981 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3982 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3983 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3984 7ed048bd 2022-08-12 thomas if (entry == NULL)
3985 7ed048bd 2022-08-12 thomas break;
3986 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3987 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3988 7ed048bd 2022-08-12 thomas }
3989 7ed048bd 2022-08-12 thomas if (n > 0)
3990 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3991 b31f89ff 2022-07-01 thomas } else {
3992 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3993 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3994 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3995 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3996 bd3f8225 2022-08-12 thomas else
3997 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3998 b31f89ff 2022-07-01 thomas }
3999 b31f89ff 2022-07-01 thomas if (err)
4000 b31f89ff 2022-07-01 thomas return err;
4001 b31f89ff 2022-07-01 thomas
4002 a5d43cac 2022-07-01 thomas /*
4003 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
4004 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
4005 a5d43cac 2022-07-01 thomas */
4006 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
4007 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
4008 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
4009 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
4010 25100026 2022-08-13 thomas }
4011 a5d43cac 2022-07-01 thomas
4012 b31f89ff 2022-07-01 thomas select_commit(s);
4013 b31f89ff 2022-07-01 thomas
4014 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
4015 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
4016 b31f89ff 2022-07-01 thomas view->count = 0;
4017 b31f89ff 2022-07-01 thomas
4018 b31f89ff 2022-07-01 thomas return NULL;
4019 e5a0f69f 2018-08-18 stsp }
4020 04cc582a 2018-08-01 stsp
4021 a5d43cac 2022-07-01 thomas static void
4022 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
4023 a5d43cac 2022-07-01 thomas {
4024 24415785 2022-07-03 thomas *x = 0;
4025 24415785 2022-07-03 thomas *y = 0;
4026 24415785 2022-07-03 thomas
4027 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4028 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
4029 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
4030 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
4031 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
4032 53d2bdd3 2022-07-10 thomas else
4033 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
4034 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4035 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
4036 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
4037 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
4038 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
4039 53d2bdd3 2022-07-10 thomas else
4040 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
4041 53d2bdd3 2022-07-10 thomas }
4042 a5d43cac 2022-07-01 thomas }
4043 a5d43cac 2022-07-01 thomas
4044 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
4045 e5a0f69f 2018-08-18 stsp static const struct got_error *
4046 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
4047 a5d43cac 2022-07-01 thomas {
4048 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
4049 a5d43cac 2022-07-01 thomas
4050 a5d43cac 2022-07-01 thomas view->nlines = y;
4051 64486692 2022-07-07 thomas view->ncols = COLS;
4052 a5d43cac 2022-07-01 thomas err = view_resize(view);
4053 a5d43cac 2022-07-01 thomas if (err)
4054 a5d43cac 2022-07-01 thomas return err;
4055 a5d43cac 2022-07-01 thomas
4056 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
4057 a5d43cac 2022-07-01 thomas
4058 a5d43cac 2022-07-01 thomas return err;
4059 07dd3ed3 2022-08-06 thomas }
4060 07dd3ed3 2022-08-06 thomas
4061 07dd3ed3 2022-08-06 thomas static const struct got_error *
4062 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
4063 07dd3ed3 2022-08-06 thomas {
4064 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
4065 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
4066 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
4067 25100026 2022-08-13 thomas
4068 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4069 25100026 2022-08-13 thomas return NULL;
4070 07dd3ed3 2022-08-06 thomas
4071 07dd3ed3 2022-08-06 thomas g = view->gline;
4072 07dd3ed3 2022-08-06 thomas view->gline = 0;
4073 07dd3ed3 2022-08-06 thomas
4074 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
4075 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
4076 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
4077 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
4078 07dd3ed3 2022-08-06 thomas select_commit(s);
4079 07dd3ed3 2022-08-06 thomas return NULL;
4080 07dd3ed3 2022-08-06 thomas }
4081 07dd3ed3 2022-08-06 thomas
4082 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
4083 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
4084 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
4085 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
4086 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
4087 07dd3ed3 2022-08-06 thomas if (err)
4088 07dd3ed3 2022-08-06 thomas return err;
4089 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
4090 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
4091 07dd3ed3 2022-08-06 thomas
4092 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
4093 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
4094 07dd3ed3 2022-08-06 thomas
4095 07dd3ed3 2022-08-06 thomas select_commit(s);
4096 07dd3ed3 2022-08-06 thomas return NULL;
4097 c72de8ab 2023-02-03 thomas
4098 c72de8ab 2023-02-03 thomas }
4099 c72de8ab 2023-02-03 thomas
4100 c72de8ab 2023-02-03 thomas static void
4101 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
4102 c72de8ab 2023-02-03 thomas {
4103 07dd3ed3 2022-08-06 thomas
4104 c72de8ab 2023-02-03 thomas switch (ch) {
4105 c72de8ab 2023-02-03 thomas case KEY_LEFT:
4106 c72de8ab 2023-02-03 thomas case 'h':
4107 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
4108 c72de8ab 2023-02-03 thomas if (view->x <= 0)
4109 c72de8ab 2023-02-03 thomas view->count = 0;
4110 c72de8ab 2023-02-03 thomas break;
4111 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
4112 c72de8ab 2023-02-03 thomas case 'l':
4113 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
4114 c72de8ab 2023-02-03 thomas view->x += 2;
4115 c72de8ab 2023-02-03 thomas else
4116 c72de8ab 2023-02-03 thomas view->count = 0;
4117 c72de8ab 2023-02-03 thomas break;
4118 c72de8ab 2023-02-03 thomas case '0':
4119 c72de8ab 2023-02-03 thomas view->x = 0;
4120 c72de8ab 2023-02-03 thomas break;
4121 c72de8ab 2023-02-03 thomas case '$':
4122 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
4123 c72de8ab 2023-02-03 thomas view->count = 0;
4124 c72de8ab 2023-02-03 thomas break;
4125 c72de8ab 2023-02-03 thomas default:
4126 c72de8ab 2023-02-03 thomas break;
4127 c72de8ab 2023-02-03 thomas }
4128 a5d43cac 2022-07-01 thomas }
4129 a5d43cac 2022-07-01 thomas
4130 a5d43cac 2022-07-01 thomas static const struct got_error *
4131 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4132 e5a0f69f 2018-08-18 stsp {
4133 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4134 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4135 7ed048bd 2022-08-12 thomas int eos, nscroll;
4136 80ddbec8 2018-04-29 stsp
4137 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4138 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4139 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4140 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4141 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4142 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4143 fb280deb 2021-08-30 stsp }
4144 c0be8933 2022-08-12 thomas if (err)
4145 c0be8933 2022-08-12 thomas return err;
4146 528dedf3 2021-08-30 stsp }
4147 068ab281 2022-07-01 thomas
4148 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4149 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4150 068ab281 2022-07-01 thomas --eos; /* border */
4151 07dd3ed3 2022-08-06 thomas
4152 07dd3ed3 2022-08-06 thomas if (view->gline)
4153 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4154 068ab281 2022-07-01 thomas
4155 528dedf3 2021-08-30 stsp switch (ch) {
4156 7e8004ba 2022-09-11 thomas case '&':
4157 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4158 7e8004ba 2022-09-11 thomas break;
4159 1e37a5c2 2019-05-12 jcs case 'q':
4160 1e37a5c2 2019-05-12 jcs s->quit = 1;
4161 05171be4 2022-06-23 thomas break;
4162 05171be4 2022-06-23 thomas case '0':
4163 05171be4 2022-06-23 thomas case '$':
4164 05171be4 2022-06-23 thomas case KEY_RIGHT:
4165 05171be4 2022-06-23 thomas case 'l':
4166 05171be4 2022-06-23 thomas case KEY_LEFT:
4167 05171be4 2022-06-23 thomas case 'h':
4168 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4169 05171be4 2022-06-23 thomas break;
4170 1e37a5c2 2019-05-12 jcs case 'k':
4171 1e37a5c2 2019-05-12 jcs case KEY_UP:
4172 1e37a5c2 2019-05-12 jcs case '<':
4173 1e37a5c2 2019-05-12 jcs case ',':
4174 f7140bf5 2021-10-17 thomas case CTRL('p'):
4175 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4176 912a3f79 2021-08-30 j break;
4177 912a3f79 2021-08-30 j case 'g':
4178 aa7a1117 2023-01-09 thomas case '=':
4179 912a3f79 2021-08-30 j case KEY_HOME:
4180 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4181 07b0611c 2022-06-23 thomas view->count = 0;
4182 1e37a5c2 2019-05-12 jcs break;
4183 70f17a53 2022-06-13 thomas case CTRL('u'):
4184 23427b14 2022-06-23 thomas case 'u':
4185 70f17a53 2022-06-13 thomas nscroll /= 2;
4186 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4187 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4188 a4292ac5 2019-05-12 jcs case CTRL('b'):
4189 1c5e5faa 2022-06-23 thomas case 'b':
4190 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4191 1e37a5c2 2019-05-12 jcs break;
4192 1e37a5c2 2019-05-12 jcs case 'j':
4193 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4194 1e37a5c2 2019-05-12 jcs case '>':
4195 1e37a5c2 2019-05-12 jcs case '.':
4196 f7140bf5 2021-10-17 thomas case CTRL('n'):
4197 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4198 912a3f79 2021-08-30 j break;
4199 f69c5a46 2022-07-19 thomas case '@':
4200 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4201 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4202 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4203 f69c5a46 2022-07-19 thomas break;
4204 912a3f79 2021-08-30 j case 'G':
4205 aa7a1117 2023-01-09 thomas case '*':
4206 912a3f79 2021-08-30 j case KEY_END: {
4207 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4208 912a3f79 2021-08-30 j * traverse them all. */
4209 07b0611c 2022-06-23 thomas view->count = 0;
4210 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4211 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4212 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4213 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4214 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4215 1e37a5c2 2019-05-12 jcs break;
4216 912a3f79 2021-08-30 j }
4217 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4218 23427b14 2022-06-23 thomas case 'd':
4219 70f17a53 2022-06-13 thomas nscroll /= 2;
4220 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4221 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4222 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4223 4c2d69cb 2022-06-23 thomas case 'f':
4224 b31f89ff 2022-07-01 thomas case ' ':
4225 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4226 1e37a5c2 2019-05-12 jcs break;
4227 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4228 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4229 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4230 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4231 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4232 2b779855 2020-12-05 naddy select_commit(s);
4233 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4234 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4235 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4236 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4237 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4238 0bf7f153 2020-12-02 naddy }
4239 1e37a5c2 2019-05-12 jcs break;
4240 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4241 444d5325 2022-07-03 thomas case '\r':
4242 07b0611c 2022-06-23 thomas view->count = 0;
4243 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4244 e5a0f69f 2018-08-18 stsp break;
4245 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4246 1e37a5c2 2019-05-12 jcs break;
4247 1be4947a 2022-07-22 thomas case 'T':
4248 07b0611c 2022-06-23 thomas view->count = 0;
4249 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4250 5036bf37 2018-09-24 stsp break;
4251 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4252 1e37a5c2 2019-05-12 jcs break;
4253 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4254 21355643 2020-12-06 stsp case CTRL('l'):
4255 21355643 2020-12-06 stsp case 'B':
4256 07b0611c 2022-06-23 thomas view->count = 0;
4257 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4258 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4259 1e37a5c2 2019-05-12 jcs break;
4260 21355643 2020-12-06 stsp err = stop_log_thread(s);
4261 74cfe85e 2020-10-20 stsp if (err)
4262 74cfe85e 2020-10-20 stsp return err;
4263 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4264 21355643 2020-12-06 stsp char *parent_path;
4265 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4266 21355643 2020-12-06 stsp if (err)
4267 21355643 2020-12-06 stsp return err;
4268 21355643 2020-12-06 stsp free(s->in_repo_path);
4269 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4270 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4271 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4272 21355643 2020-12-06 stsp struct got_object_id *start_id;
4273 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4274 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4275 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4276 466429a1 2022-11-17 thomas if (err) {
4277 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4278 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4279 466429a1 2022-11-17 thomas return err;
4280 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4281 466429a1 2022-11-17 thomas free(s->head_ref_name);
4282 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4283 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4284 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4285 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4286 466429a1 2022-11-17 thomas if (err)
4287 466429a1 2022-11-17 thomas return err;
4288 466429a1 2022-11-17 thomas }
4289 21355643 2020-12-06 stsp free(s->start_id);
4290 21355643 2020-12-06 stsp s->start_id = start_id;
4291 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4292 21355643 2020-12-06 stsp } else /* 'B' */
4293 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4294 21355643 2020-12-06 stsp
4295 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4296 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4297 bf7e79b3 2022-06-23 thomas if (err)
4298 bf7e79b3 2022-06-23 thomas return err;
4299 bf7e79b3 2022-06-23 thomas }
4300 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4301 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4302 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4303 74cfe85e 2020-10-20 stsp if (err)
4304 21355643 2020-12-06 stsp return err;
4305 51a10b52 2020-12-26 stsp tog_free_refs();
4306 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4307 ca51c541 2020-12-07 stsp if (err)
4308 ca51c541 2020-12-07 stsp return err;
4309 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4310 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4311 d01904d4 2019-06-25 stsp if (err)
4312 d01904d4 2019-06-25 stsp return err;
4313 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4314 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4315 b672a97a 2020-01-27 stsp if (err)
4316 b672a97a 2020-01-27 stsp return err;
4317 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4318 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4319 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4320 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4321 21355643 2020-12-06 stsp s->selected_entry = NULL;
4322 21355643 2020-12-06 stsp s->selected = 0;
4323 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4324 21355643 2020-12-06 stsp s->quit = 0;
4325 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4326 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4327 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4328 94ecf40d 2022-07-22 thomas view->offset = 0;
4329 d01904d4 2019-06-25 stsp break;
4330 1be4947a 2022-07-22 thomas case 'R':
4331 07b0611c 2022-06-23 thomas view->count = 0;
4332 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4333 6458efa5 2020-11-24 stsp break;
4334 1e37a5c2 2019-05-12 jcs default:
4335 07b0611c 2022-06-23 thomas view->count = 0;
4336 1e37a5c2 2019-05-12 jcs break;
4337 899d86c2 2018-05-10 stsp }
4338 e5a0f69f 2018-08-18 stsp
4339 80ddbec8 2018-04-29 stsp return err;
4340 80ddbec8 2018-04-29 stsp }
4341 80ddbec8 2018-04-29 stsp
4342 4ed7e80c 2018-05-20 stsp static const struct got_error *
4343 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4344 c2db6724 2019-01-04 stsp {
4345 c2db6724 2019-01-04 stsp const struct got_error *error;
4346 c2db6724 2019-01-04 stsp
4347 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4348 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4349 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4350 37c06ea4 2019-07-15 stsp #endif
4351 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4352 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4353 c2db6724 2019-01-04 stsp
4354 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4355 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4356 c2db6724 2019-01-04 stsp
4357 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4358 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4359 c2db6724 2019-01-04 stsp
4360 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4361 c2db6724 2019-01-04 stsp if (error != NULL)
4362 c2db6724 2019-01-04 stsp return error;
4363 c2db6724 2019-01-04 stsp
4364 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4365 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4366 c2db6724 2019-01-04 stsp
4367 c2db6724 2019-01-04 stsp return NULL;
4368 c2db6724 2019-01-04 stsp }
4369 c2db6724 2019-01-04 stsp
4370 b85a3496 2023-04-14 thomas static const struct got_error *
4371 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4372 b85a3496 2023-04-14 thomas {
4373 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4374 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4375 f4086c71 2023-04-22 thomas int in;
4376 b85a3496 2023-04-14 thomas
4377 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4378 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4379 b85a3496 2023-04-14 thomas
4380 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4381 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4382 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4383 b85a3496 2023-04-14 thomas test_script_path);
4384 b85a3496 2023-04-14 thomas goto done;
4385 b85a3496 2023-04-14 thomas }
4386 b85a3496 2023-04-14 thomas
4387 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4388 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4389 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4390 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4391 b85a3496 2023-04-14 thomas goto done;
4392 b85a3496 2023-04-14 thomas }
4393 b85a3496 2023-04-14 thomas
4394 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4395 f4086c71 2023-04-22 thomas if (in == -1) {
4396 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4397 f4086c71 2023-04-22 thomas goto done;
4398 f4086c71 2023-04-22 thomas }
4399 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4400 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4401 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4402 f4086c71 2023-04-22 thomas close(in);
4403 b85a3496 2023-04-14 thomas goto done;
4404 b85a3496 2023-04-14 thomas }
4405 b85a3496 2023-04-14 thomas
4406 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4407 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4408 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4409 66b04f8f 2023-07-19 thomas tog_io.sdump = fopen(screen_dump_path, "we");
4410 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4411 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4412 5b3a801d 2023-04-22 thomas goto done;
4413 5b3a801d 2023-04-22 thomas }
4414 5b3a801d 2023-04-22 thomas
4415 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4416 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4417 b85a3496 2023-04-14 thomas goto done;
4418 b85a3496 2023-04-14 thomas }
4419 b85a3496 2023-04-14 thomas
4420 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4421 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4422 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4423 557d3365 2023-04-14 thomas
4424 557d3365 2023-04-14 thomas using_mock_io = 1;
4425 1134ebde 2023-04-22 thomas
4426 b85a3496 2023-04-14 thomas done:
4427 b85a3496 2023-04-14 thomas if (err)
4428 557d3365 2023-04-14 thomas tog_io_close();
4429 b85a3496 2023-04-14 thomas return err;
4430 b85a3496 2023-04-14 thomas }
4431 b85a3496 2023-04-14 thomas
4432 557d3365 2023-04-14 thomas static void
4433 557d3365 2023-04-14 thomas init_curses(void)
4434 a915003a 2019-02-05 stsp {
4435 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4436 557d3365 2023-04-14 thomas return;
4437 b85a3496 2023-04-14 thomas
4438 557d3365 2023-04-14 thomas initscr();
4439 557d3365 2023-04-14 thomas
4440 a915003a 2019-02-05 stsp cbreak();
4441 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4442 a915003a 2019-02-05 stsp noecho();
4443 a915003a 2019-02-05 stsp nonl();
4444 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4445 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4446 a915003a 2019-02-05 stsp curs_set(0);
4447 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4448 6d17833f 2019-11-08 stsp start_color();
4449 6d17833f 2019-11-08 stsp use_default_colors();
4450 6d17833f 2019-11-08 stsp }
4451 b85a3496 2023-04-14 thomas
4452 557d3365 2023-04-14 thomas return;
4453 a915003a 2019-02-05 stsp }
4454 a915003a 2019-02-05 stsp
4455 c2db6724 2019-01-04 stsp static const struct got_error *
4456 349dfd1e 2023-07-23 thomas set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4457 349dfd1e 2023-07-23 thomas {
4458 349dfd1e 2023-07-23 thomas tog_base_commit.id = got_object_id_dup(
4459 349dfd1e 2023-07-23 thomas got_worktree_get_base_commit_id(worktree));
4460 349dfd1e 2023-07-23 thomas if (tog_base_commit.id == NULL)
4461 349dfd1e 2023-07-23 thomas return got_error_from_errno( "got_object_id_dup");
4462 349dfd1e 2023-07-23 thomas
4463 92845f09 2023-07-26 thomas return NULL;
4464 349dfd1e 2023-07-23 thomas }
4465 349dfd1e 2023-07-23 thomas
4466 349dfd1e 2023-07-23 thomas static const struct got_error *
4467 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4468 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4469 f135c941 2020-02-20 stsp {
4470 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4471 f135c941 2020-02-20 stsp
4472 f135c941 2020-02-20 stsp if (argc == 0) {
4473 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4474 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4475 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4476 f135c941 2020-02-20 stsp return NULL;
4477 f135c941 2020-02-20 stsp }
4478 f135c941 2020-02-20 stsp
4479 f135c941 2020-02-20 stsp if (worktree) {
4480 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4481 bfd61697 2020-11-14 stsp char *p;
4482 f135c941 2020-02-20 stsp
4483 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4484 f135c941 2020-02-20 stsp if (err)
4485 f135c941 2020-02-20 stsp return err;
4486 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4487 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4488 bfd61697 2020-11-14 stsp p) == -1) {
4489 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4490 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4491 f135c941 2020-02-20 stsp }
4492 f135c941 2020-02-20 stsp free(p);
4493 f135c941 2020-02-20 stsp } else
4494 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4495 f135c941 2020-02-20 stsp
4496 f135c941 2020-02-20 stsp return err;
4497 f135c941 2020-02-20 stsp }
4498 f135c941 2020-02-20 stsp
4499 f135c941 2020-02-20 stsp static const struct got_error *
4500 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4501 9f7d7167 2018-04-29 stsp {
4502 1d98034b 2023-04-14 thomas const struct got_error *error;
4503 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4504 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4505 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4506 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4507 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4508 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4509 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4510 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4511 04cc582a 2018-08-01 stsp struct tog_view *view;
4512 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4513 80ddbec8 2018-04-29 stsp
4514 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4515 80ddbec8 2018-04-29 stsp switch (ch) {
4516 b672a97a 2020-01-27 stsp case 'b':
4517 b672a97a 2020-01-27 stsp log_branches = 1;
4518 b672a97a 2020-01-27 stsp break;
4519 80ddbec8 2018-04-29 stsp case 'c':
4520 80ddbec8 2018-04-29 stsp start_commit = optarg;
4521 80ddbec8 2018-04-29 stsp break;
4522 ecb28ae0 2018-07-16 stsp case 'r':
4523 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4524 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4525 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4526 9ba1d308 2019-10-21 stsp optarg);
4527 ecb28ae0 2018-07-16 stsp break;
4528 80ddbec8 2018-04-29 stsp default:
4529 17020d27 2019-03-07 stsp usage_log();
4530 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4531 80ddbec8 2018-04-29 stsp }
4532 80ddbec8 2018-04-29 stsp }
4533 80ddbec8 2018-04-29 stsp
4534 80ddbec8 2018-04-29 stsp argc -= optind;
4535 80ddbec8 2018-04-29 stsp argv += optind;
4536 80ddbec8 2018-04-29 stsp
4537 f135c941 2020-02-20 stsp if (argc > 1)
4538 f135c941 2020-02-20 stsp usage_log();
4539 963f97a1 2019-03-18 stsp
4540 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4541 7cd52833 2022-06-23 thomas if (error != NULL)
4542 7cd52833 2022-06-23 thomas goto done;
4543 7cd52833 2022-06-23 thomas
4544 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4545 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4546 5a5defb7 2023-07-23 thomas if (cwd == NULL) {
4547 5a5defb7 2023-07-23 thomas error = got_error_from_errno("getcwd");
4548 5a5defb7 2023-07-23 thomas goto done;
4549 5a5defb7 2023-07-23 thomas }
4550 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
4551 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4552 c156c7a4 2020-12-18 stsp goto done;
4553 a1fbf39a 2019-08-11 stsp if (worktree)
4554 6962eb72 2020-02-20 stsp repo_path =
4555 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4556 a1fbf39a 2019-08-11 stsp else
4557 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4558 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4559 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4560 c156c7a4 2020-12-18 stsp goto done;
4561 c156c7a4 2020-12-18 stsp }
4562 ecb28ae0 2018-07-16 stsp }
4563 ecb28ae0 2018-07-16 stsp
4564 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4565 80ddbec8 2018-04-29 stsp if (error != NULL)
4566 ecb28ae0 2018-07-16 stsp goto done;
4567 80ddbec8 2018-04-29 stsp
4568 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4569 f135c941 2020-02-20 stsp repo, worktree);
4570 f135c941 2020-02-20 stsp if (error)
4571 f135c941 2020-02-20 stsp goto done;
4572 f135c941 2020-02-20 stsp
4573 557d3365 2023-04-14 thomas init_curses();
4574 f135c941 2020-02-20 stsp
4575 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4576 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4577 c02c541e 2019-03-29 stsp if (error)
4578 c02c541e 2019-03-29 stsp goto done;
4579 c02c541e 2019-03-29 stsp
4580 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4581 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4582 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4583 87670572 2020-12-26 naddy if (error)
4584 87670572 2020-12-26 naddy goto done;
4585 87670572 2020-12-26 naddy }
4586 51a10b52 2020-12-26 stsp
4587 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4588 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4589 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4590 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4591 d8f38dc4 2020-12-05 stsp if (error)
4592 d8f38dc4 2020-12-05 stsp goto done;
4593 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4594 d8f38dc4 2020-12-05 stsp } else {
4595 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4596 66b04f8f 2023-07-19 thomas repo, worktree);
4597 66b04f8f 2023-07-19 thomas if (error != NULL)
4598 66b04f8f 2023-07-19 thomas goto done;
4599 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
4600 66b04f8f 2023-07-19 thomas start_commit = keyword_idstr;
4601 66b04f8f 2023-07-19 thomas
4602 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4603 d8f38dc4 2020-12-05 stsp if (error == NULL)
4604 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4605 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4606 d8f38dc4 2020-12-05 stsp goto done;
4607 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4608 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4609 d8f38dc4 2020-12-05 stsp if (error)
4610 d8f38dc4 2020-12-05 stsp goto done;
4611 d8f38dc4 2020-12-05 stsp }
4612 8b473291 2019-02-21 stsp
4613 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4614 04cc582a 2018-08-01 stsp if (view == NULL) {
4615 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4616 04cc582a 2018-08-01 stsp goto done;
4617 04cc582a 2018-08-01 stsp }
4618 349dfd1e 2023-07-23 thomas
4619 2fc00ff4 2019-08-31 stsp if (worktree) {
4620 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
4621 349dfd1e 2023-07-23 thomas if (error != NULL)
4622 349dfd1e 2023-07-23 thomas goto done;
4623 92845f09 2023-07-26 thomas }
4624 349dfd1e 2023-07-23 thomas
4625 92845f09 2023-07-26 thomas error = open_log_view(view, start_id, repo, head_ref_name,
4626 92845f09 2023-07-26 thomas in_repo_path, log_branches, worktree);
4627 92845f09 2023-07-26 thomas if (error)
4628 92845f09 2023-07-26 thomas goto done;
4629 92845f09 2023-07-26 thomas
4630 92845f09 2023-07-26 thomas if (worktree) {
4631 92845f09 2023-07-26 thomas /* The work tree will be closed by the log thread. */
4632 2fc00ff4 2019-08-31 stsp worktree = NULL;
4633 2fc00ff4 2019-08-31 stsp }
4634 349dfd1e 2023-07-23 thomas
4635 557d3365 2023-04-14 thomas error = view_loop(view);
4636 349dfd1e 2023-07-23 thomas
4637 ecb28ae0 2018-07-16 stsp done:
4638 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
4639 66b04f8f 2023-07-19 thomas free(keyword_idstr);
4640 f135c941 2020-02-20 stsp free(in_repo_path);
4641 ecb28ae0 2018-07-16 stsp free(repo_path);
4642 ecb28ae0 2018-07-16 stsp free(cwd);
4643 899d86c2 2018-05-10 stsp free(start_id);
4644 d8f38dc4 2020-12-05 stsp free(label);
4645 d8f38dc4 2020-12-05 stsp if (ref)
4646 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4647 1d0f4054 2021-06-17 stsp if (repo) {
4648 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4649 1d0f4054 2021-06-17 stsp if (error == NULL)
4650 1d0f4054 2021-06-17 stsp error = close_err;
4651 1d0f4054 2021-06-17 stsp }
4652 ec142235 2019-03-07 stsp if (worktree)
4653 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4654 7cd52833 2022-06-23 thomas if (pack_fds) {
4655 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4656 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4657 7cd52833 2022-06-23 thomas if (error == NULL)
4658 7cd52833 2022-06-23 thomas error = pack_err;
4659 b85a3496 2023-04-14 thomas }
4660 51a10b52 2020-12-26 stsp tog_free_refs();
4661 80ddbec8 2018-04-29 stsp return error;
4662 9f7d7167 2018-04-29 stsp }
4663 9f7d7167 2018-04-29 stsp
4664 4ed7e80c 2018-05-20 stsp __dead static void
4665 9f7d7167 2018-04-29 stsp usage_diff(void)
4666 9f7d7167 2018-04-29 stsp {
4667 80ddbec8 2018-04-29 stsp endwin();
4668 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4669 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4670 9f7d7167 2018-04-29 stsp exit(1);
4671 b304db33 2018-05-20 stsp }
4672 b304db33 2018-05-20 stsp
4673 6d17833f 2019-11-08 stsp static int
4674 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4675 41605754 2020-11-12 stsp regmatch_t *regmatch)
4676 6d17833f 2019-11-08 stsp {
4677 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4678 6d17833f 2019-11-08 stsp }
4679 6d17833f 2019-11-08 stsp
4680 ef20f542 2022-06-26 thomas static struct tog_color *
4681 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4682 6d17833f 2019-11-08 stsp {
4683 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4684 6d17833f 2019-11-08 stsp
4685 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4686 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4687 6d17833f 2019-11-08 stsp return tc;
4688 6d17833f 2019-11-08 stsp }
4689 6d17833f 2019-11-08 stsp
4690 6d17833f 2019-11-08 stsp return NULL;
4691 6d17833f 2019-11-08 stsp }
4692 6d17833f 2019-11-08 stsp
4693 4ed7e80c 2018-05-20 stsp static const struct got_error *
4694 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4695 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4696 41605754 2020-11-12 stsp {
4697 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4698 666f7b10 2022-06-23 thomas char *exstr = NULL;
4699 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4700 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4701 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4702 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4703 41605754 2020-11-12 stsp
4704 41605754 2020-11-12 stsp *wtotal = 0;
4705 cbea3800 2022-06-23 thomas
4706 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4707 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4708 41605754 2020-11-12 stsp
4709 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4710 666f7b10 2022-06-23 thomas if (err)
4711 666f7b10 2022-06-23 thomas return err;
4712 666f7b10 2022-06-23 thomas
4713 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4714 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4715 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4716 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4717 666f7b10 2022-06-23 thomas goto done;
4718 666f7b10 2022-06-23 thomas }
4719 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4720 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4721 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4722 cbea3800 2022-06-23 thomas goto done;
4723 cbea3800 2022-06-23 thomas }
4724 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4725 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4726 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4727 cbea3800 2022-06-23 thomas goto done;
4728 cbea3800 2022-06-23 thomas }
4729 05171be4 2022-06-23 thomas
4730 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4731 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4732 cbea3800 2022-06-23 thomas col_tab_align, 1);
4733 cbea3800 2022-06-23 thomas if (err)
4734 cbea3800 2022-06-23 thomas goto done;
4735 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4736 05171be4 2022-06-23 thomas if (n) {
4737 cbea3800 2022-06-23 thomas free(wline);
4738 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4739 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4740 cbea3800 2022-06-23 thomas if (err)
4741 cbea3800 2022-06-23 thomas goto done;
4742 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4743 cbea3800 2022-06-23 thomas wlimit -= width;
4744 cbea3800 2022-06-23 thomas *wtotal += width;
4745 41605754 2020-11-12 stsp }
4746 41605754 2020-11-12 stsp
4747 41605754 2020-11-12 stsp if (wlimit > 0) {
4748 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4749 cbea3800 2022-06-23 thomas size_t wlen;
4750 cbea3800 2022-06-23 thomas
4751 cbea3800 2022-06-23 thomas free(wline);
4752 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4753 cbea3800 2022-06-23 thomas col_tab_align, 1);
4754 cbea3800 2022-06-23 thomas if (err)
4755 cbea3800 2022-06-23 thomas goto done;
4756 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4757 cbea3800 2022-06-23 thomas while (i < wlen) {
4758 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4759 cbea3800 2022-06-23 thomas if (width == -1) {
4760 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4761 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4762 cbea3800 2022-06-23 thomas goto done;
4763 cbea3800 2022-06-23 thomas }
4764 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4765 cbea3800 2022-06-23 thomas break;
4766 1c5e5faa 2022-06-23 thomas w += width;
4767 cbea3800 2022-06-23 thomas i++;
4768 41605754 2020-11-12 stsp }
4769 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4770 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4771 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4772 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4773 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4774 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4775 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4776 41605754 2020-11-12 stsp }
4777 41605754 2020-11-12 stsp }
4778 41605754 2020-11-12 stsp
4779 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4780 cbea3800 2022-06-23 thomas free(wline);
4781 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4782 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4783 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4784 cbea3800 2022-06-23 thomas col_tab_align, 1);
4785 cbea3800 2022-06-23 thomas if (err)
4786 cbea3800 2022-06-23 thomas goto done;
4787 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4788 cbea3800 2022-06-23 thomas } else {
4789 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4790 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4791 cbea3800 2022-06-23 thomas if (err)
4792 cbea3800 2022-06-23 thomas goto done;
4793 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4794 cbea3800 2022-06-23 thomas }
4795 cbea3800 2022-06-23 thomas *wtotal += width2;
4796 41605754 2020-11-12 stsp }
4797 cbea3800 2022-06-23 thomas done:
4798 05171be4 2022-06-23 thomas free(wline);
4799 666f7b10 2022-06-23 thomas free(exstr);
4800 cbea3800 2022-06-23 thomas free(seg0);
4801 cbea3800 2022-06-23 thomas free(seg1);
4802 cbea3800 2022-06-23 thomas free(seg2);
4803 cbea3800 2022-06-23 thomas return err;
4804 41605754 2020-11-12 stsp }
4805 07dd3ed3 2022-08-06 thomas
4806 07dd3ed3 2022-08-06 thomas static int
4807 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4808 07dd3ed3 2022-08-06 thomas {
4809 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4810 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4811 07dd3ed3 2022-08-06 thomas
4812 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4813 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4814 fc2737d5 2022-09-15 thomas
4815 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4816 fc2737d5 2022-09-15 thomas selected = first;
4817 fc2737d5 2022-09-15 thomas eof = &s->eof;
4818 fc2737d5 2022-09-15 thomas f = s->f;
4819 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4820 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4821 41605754 2020-11-12 stsp
4822 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4823 07dd3ed3 2022-08-06 thomas selected = first;
4824 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4825 07dd3ed3 2022-08-06 thomas f = s->f;
4826 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4827 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4828 07dd3ed3 2022-08-06 thomas
4829 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4830 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4831 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4832 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4833 07dd3ed3 2022-08-06 thomas } else
4834 07dd3ed3 2022-08-06 thomas return 0;
4835 07dd3ed3 2022-08-06 thomas
4836 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4837 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4838 07dd3ed3 2022-08-06 thomas return 0;
4839 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4840 07dd3ed3 2022-08-06 thomas rewind(f);
4841 07dd3ed3 2022-08-06 thomas *eof = 0;
4842 07dd3ed3 2022-08-06 thomas *first = 1;
4843 07dd3ed3 2022-08-06 thomas *lineno = 0;
4844 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4845 07dd3ed3 2022-08-06 thomas return 0;
4846 07dd3ed3 2022-08-06 thomas }
4847 07dd3ed3 2022-08-06 thomas
4848 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4849 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4850 07dd3ed3 2022-08-06 thomas view->gline = 0;
4851 07dd3ed3 2022-08-06 thomas
4852 07dd3ed3 2022-08-06 thomas return 1;
4853 07dd3ed3 2022-08-06 thomas }
4854 07dd3ed3 2022-08-06 thomas
4855 41605754 2020-11-12 stsp static const struct got_error *
4856 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4857 26ed57b2 2018-05-19 stsp {
4858 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4859 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4860 61e69b96 2018-05-20 stsp const struct got_error *err;
4861 82c78e96 2022-08-06 thomas int nprinted = 0;
4862 b304db33 2018-05-20 stsp char *line;
4863 826082fe 2020-12-10 stsp size_t linesize = 0;
4864 826082fe 2020-12-10 stsp ssize_t linelen;
4865 61e69b96 2018-05-20 stsp wchar_t *wline;
4866 e0b650dd 2018-05-20 stsp int width;
4867 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4868 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4869 fe621944 2020-11-10 stsp off_t line_offset;
4870 26ed57b2 2018-05-19 stsp
4871 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4872 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4873 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4874 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4875 fe621944 2020-11-10 stsp
4876 f7d12f7e 2018-08-01 stsp werase(view->window);
4877 a3404814 2018-09-02 stsp
4878 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4879 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4880 07dd3ed3 2022-08-06 thomas
4881 a3404814 2018-09-02 stsp if (header) {
4882 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4883 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4884 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4885 07dd3ed3 2022-08-06 thomas
4886 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4887 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4888 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4889 f91a2b48 2022-06-23 thomas 0, 0);
4890 135a2da0 2020-11-11 stsp free(line);
4891 135a2da0 2020-11-11 stsp if (err)
4892 a3404814 2018-09-02 stsp return err;
4893 a3404814 2018-09-02 stsp
4894 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4895 a3404814 2018-09-02 stsp wstandout(view->window);
4896 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4897 e54cc94a 2020-11-11 stsp free(wline);
4898 e54cc94a 2020-11-11 stsp wline = NULL;
4899 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4900 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4901 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4902 a3404814 2018-09-02 stsp wstandend(view->window);
4903 26ed57b2 2018-05-19 stsp
4904 a3404814 2018-09-02 stsp if (max_lines <= 1)
4905 a3404814 2018-09-02 stsp return NULL;
4906 a3404814 2018-09-02 stsp max_lines--;
4907 a3404814 2018-09-02 stsp }
4908 a3404814 2018-09-02 stsp
4909 89f1a395 2020-12-01 naddy s->eof = 0;
4910 05171be4 2022-06-23 thomas view->maxx = 0;
4911 826082fe 2020-12-10 stsp line = NULL;
4912 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4913 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4914 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4915 6568f0aa 2022-08-06 thomas
4916 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4917 826082fe 2020-12-10 stsp if (linelen == -1) {
4918 826082fe 2020-12-10 stsp if (feof(s->f)) {
4919 826082fe 2020-12-10 stsp s->eof = 1;
4920 826082fe 2020-12-10 stsp break;
4921 826082fe 2020-12-10 stsp }
4922 826082fe 2020-12-10 stsp free(line);
4923 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4924 61e69b96 2018-05-20 stsp }
4925 05171be4 2022-06-23 thomas
4926 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4927 07dd3ed3 2022-08-06 thomas continue;
4928 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4929 07dd3ed3 2022-08-06 thomas continue;
4930 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4931 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4932 07dd3ed3 2022-08-06 thomas
4933 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4934 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4935 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4936 cbea3800 2022-06-23 thomas if (err) {
4937 cbea3800 2022-06-23 thomas free(line);
4938 cbea3800 2022-06-23 thomas return err;
4939 cbea3800 2022-06-23 thomas }
4940 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4941 cbea3800 2022-06-23 thomas free(wline);
4942 cbea3800 2022-06-23 thomas wline = NULL;
4943 cbea3800 2022-06-23 thomas
4944 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4945 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4946 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4947 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4948 07dd3ed3 2022-08-06 thomas if (attr)
4949 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4950 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4951 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4952 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4953 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4954 41605754 2020-11-12 stsp if (err) {
4955 41605754 2020-11-12 stsp free(line);
4956 41605754 2020-11-12 stsp return err;
4957 41605754 2020-11-12 stsp }
4958 41605754 2020-11-12 stsp } else {
4959 f1c0ec19 2022-06-23 thomas int skip;
4960 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4961 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4962 f1c0ec19 2022-06-23 thomas if (err) {
4963 f1c0ec19 2022-06-23 thomas free(line);
4964 f1c0ec19 2022-06-23 thomas return err;
4965 f1c0ec19 2022-06-23 thomas }
4966 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4967 f1c0ec19 2022-06-23 thomas free(wline);
4968 41605754 2020-11-12 stsp wline = NULL;
4969 41605754 2020-11-12 stsp }
4970 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4971 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4972 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4973 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4974 07dd3ed3 2022-08-06 thomas } else {
4975 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4976 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4977 07dd3ed3 2022-08-06 thomas }
4978 07dd3ed3 2022-08-06 thomas if (attr)
4979 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4980 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4981 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4982 826082fe 2020-12-10 stsp }
4983 826082fe 2020-12-10 stsp free(line);
4984 fe621944 2020-11-10 stsp if (nprinted >= 1)
4985 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4986 89f1a395 2020-12-01 naddy (nprinted - 1);
4987 fe621944 2020-11-10 stsp else
4988 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4989 26ed57b2 2018-05-19 stsp
4990 a5d43cac 2022-07-01 thomas view_border(view);
4991 c3e9aa98 2019-05-13 jcs
4992 89f1a395 2020-12-01 naddy if (s->eof) {
4993 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4994 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4995 c3e9aa98 2019-05-13 jcs nprinted++;
4996 c3e9aa98 2019-05-13 jcs }
4997 c3e9aa98 2019-05-13 jcs
4998 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4999 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
5000 c3e9aa98 2019-05-13 jcs if (err) {
5001 c3e9aa98 2019-05-13 jcs return err;
5002 c3e9aa98 2019-05-13 jcs }
5003 26ed57b2 2018-05-19 stsp
5004 c3e9aa98 2019-05-13 jcs wstandout(view->window);
5005 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
5006 e54cc94a 2020-11-11 stsp free(wline);
5007 e54cc94a 2020-11-11 stsp wline = NULL;
5008 c3e9aa98 2019-05-13 jcs wstandend(view->window);
5009 c3e9aa98 2019-05-13 jcs }
5010 c3e9aa98 2019-05-13 jcs
5011 26ed57b2 2018-05-19 stsp return NULL;
5012 abd2672a 2018-12-23 stsp }
5013 abd2672a 2018-12-23 stsp
5014 abd2672a 2018-12-23 stsp static char *
5015 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
5016 abd2672a 2018-12-23 stsp {
5017 09867e48 2019-08-13 stsp struct tm mytm, *tm;
5018 09867e48 2019-08-13 stsp char *p, *s;
5019 09867e48 2019-08-13 stsp
5020 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
5021 09867e48 2019-08-13 stsp if (tm == NULL)
5022 09867e48 2019-08-13 stsp return NULL;
5023 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
5024 09867e48 2019-08-13 stsp if (s == NULL)
5025 09867e48 2019-08-13 stsp return NULL;
5026 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
5027 abd2672a 2018-12-23 stsp if (p)
5028 abd2672a 2018-12-23 stsp *p = '\0';
5029 abd2672a 2018-12-23 stsp return s;
5030 9f7d7167 2018-04-29 stsp }
5031 9f7d7167 2018-04-29 stsp
5032 4ed7e80c 2018-05-20 stsp static const struct got_error *
5033 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5034 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
5035 abd2672a 2018-12-23 stsp {
5036 82c78e96 2022-08-06 thomas struct got_diff_line *p;
5037 fe621944 2020-11-10 stsp
5038 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5039 fe621944 2020-11-10 stsp if (p == NULL)
5040 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
5041 82c78e96 2022-08-06 thomas *lines = p;
5042 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
5043 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
5044 fe621944 2020-11-10 stsp (*nlines)++;
5045 82c78e96 2022-08-06 thomas
5046 fe621944 2020-11-10 stsp return NULL;
5047 fe621944 2020-11-10 stsp }
5048 fe621944 2020-11-10 stsp
5049 fe621944 2020-11-10 stsp static const struct got_error *
5050 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5051 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
5052 be97ab03 2023-01-19 thomas {
5053 be97ab03 2023-01-19 thomas struct got_diff_line *p;
5054 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
5055 be97ab03 2023-01-19 thomas size_t i, r;
5056 be97ab03 2023-01-19 thomas
5057 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
5058 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
5059 be97ab03 2023-01-19 thomas
5060 be97ab03 2023-01-19 thomas for (;;) {
5061 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
5062 be97ab03 2023-01-19 thomas if (r == 0) {
5063 be97ab03 2023-01-19 thomas if (ferror(src))
5064 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
5065 be97ab03 2023-01-19 thomas if (feof(src))
5066 be97ab03 2023-01-19 thomas break;
5067 be97ab03 2023-01-19 thomas }
5068 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
5069 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
5070 be97ab03 2023-01-19 thomas }
5071 be97ab03 2023-01-19 thomas
5072 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
5073 9382c10a 2023-02-23 thomas return NULL;
5074 9382c10a 2023-02-23 thomas
5075 be97ab03 2023-01-19 thomas /*
5076 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
5077 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
5078 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
5079 be97ab03 2023-01-19 thomas */
5080 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
5081 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
5082 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5083 be97ab03 2023-01-19 thomas
5084 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
5085 9382c10a 2023-02-23 thomas --s_nlines;
5086 9382c10a 2023-02-23 thomas ++s_lines;
5087 9382c10a 2023-02-23 thomas }
5088 9382c10a 2023-02-23 thomas }
5089 be97ab03 2023-01-19 thomas
5090 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5091 be97ab03 2023-01-19 thomas if (p == NULL) {
5092 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
5093 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
5094 be97ab03 2023-01-19 thomas }
5095 be97ab03 2023-01-19 thomas
5096 be97ab03 2023-01-19 thomas *d_lines = p;
5097 be97ab03 2023-01-19 thomas
5098 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5099 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
5100 be97ab03 2023-01-19 thomas
5101 be97ab03 2023-01-19 thomas return NULL;
5102 be97ab03 2023-01-19 thomas }
5103 be97ab03 2023-01-19 thomas
5104 be97ab03 2023-01-19 thomas static const struct got_error *
5105 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
5106 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5107 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
5108 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
5109 fe621944 2020-11-10 stsp {
5110 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
5111 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
5112 15a94983 2018-12-23 stsp struct got_commit_object *commit;
5113 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5114 45d799e2 2018-12-23 stsp time_t committer_time;
5115 45d799e2 2018-12-23 stsp const char *author, *committer;
5116 8b473291 2019-02-21 stsp char *refs_str = NULL;
5117 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
5118 fe621944 2020-11-10 stsp off_t outoff = 0;
5119 fe621944 2020-11-10 stsp int n;
5120 abd2672a 2018-12-23 stsp
5121 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, commit_id, repo);
5122 00ddec2f 2023-05-17 thomas if (err)
5123 00ddec2f 2023-05-17 thomas return err;
5124 8b473291 2019-02-21 stsp
5125 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5126 abd2672a 2018-12-23 stsp if (err)
5127 abd2672a 2018-12-23 stsp return err;
5128 abd2672a 2018-12-23 stsp
5129 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
5130 15a94983 2018-12-23 stsp if (err) {
5131 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
5132 15a94983 2018-12-23 stsp goto done;
5133 15a94983 2018-12-23 stsp }
5134 abd2672a 2018-12-23 stsp
5135 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5136 fe621944 2020-11-10 stsp if (err)
5137 fe621944 2020-11-10 stsp goto done;
5138 fe621944 2020-11-10 stsp
5139 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5140 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
5141 fe621944 2020-11-10 stsp if (n < 0) {
5142 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5143 abd2672a 2018-12-23 stsp goto done;
5144 abd2672a 2018-12-23 stsp }
5145 fe621944 2020-11-10 stsp outoff += n;
5146 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5147 fe621944 2020-11-10 stsp if (err)
5148 fe621944 2020-11-10 stsp goto done;
5149 fe621944 2020-11-10 stsp
5150 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5151 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5152 fe621944 2020-11-10 stsp if (n < 0) {
5153 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5154 abd2672a 2018-12-23 stsp goto done;
5155 abd2672a 2018-12-23 stsp }
5156 fe621944 2020-11-10 stsp outoff += n;
5157 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5158 fe621944 2020-11-10 stsp if (err)
5159 fe621944 2020-11-10 stsp goto done;
5160 fe621944 2020-11-10 stsp
5161 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5162 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5163 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5164 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5165 fe621944 2020-11-10 stsp if (n < 0) {
5166 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5167 fe621944 2020-11-10 stsp goto done;
5168 fe621944 2020-11-10 stsp }
5169 fe621944 2020-11-10 stsp outoff += n;
5170 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5171 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5172 fe621944 2020-11-10 stsp if (err)
5173 fe621944 2020-11-10 stsp goto done;
5174 abd2672a 2018-12-23 stsp }
5175 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5176 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5177 48830929 2023-01-07 thomas if (datestr) {
5178 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5179 fe621944 2020-11-10 stsp if (n < 0) {
5180 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5181 fe621944 2020-11-10 stsp goto done;
5182 fe621944 2020-11-10 stsp }
5183 fe621944 2020-11-10 stsp outoff += n;
5184 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5185 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5186 fe621944 2020-11-10 stsp if (err)
5187 fe621944 2020-11-10 stsp goto done;
5188 abd2672a 2018-12-23 stsp }
5189 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5190 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5191 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5192 ac4dc263 2021-09-24 thomas int pn = 1;
5193 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5194 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5195 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5196 ac4dc263 2021-09-24 thomas if (err)
5197 ac4dc263 2021-09-24 thomas goto done;
5198 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5199 ac4dc263 2021-09-24 thomas if (n < 0) {
5200 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5201 ac4dc263 2021-09-24 thomas goto done;
5202 ac4dc263 2021-09-24 thomas }
5203 ac4dc263 2021-09-24 thomas outoff += n;
5204 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5205 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5206 ac4dc263 2021-09-24 thomas if (err)
5207 ac4dc263 2021-09-24 thomas goto done;
5208 ac4dc263 2021-09-24 thomas free(id_str);
5209 ac4dc263 2021-09-24 thomas id_str = NULL;
5210 ac4dc263 2021-09-24 thomas }
5211 ac4dc263 2021-09-24 thomas }
5212 ac4dc263 2021-09-24 thomas
5213 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5214 5943eee2 2019-08-13 stsp if (err)
5215 5943eee2 2019-08-13 stsp goto done;
5216 fe621944 2020-11-10 stsp s = logmsg;
5217 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5218 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5219 fe621944 2020-11-10 stsp if (n < 0) {
5220 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5221 fe621944 2020-11-10 stsp goto done;
5222 fe621944 2020-11-10 stsp }
5223 fe621944 2020-11-10 stsp outoff += n;
5224 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5225 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5226 fe621944 2020-11-10 stsp if (err)
5227 fe621944 2020-11-10 stsp goto done;
5228 abd2672a 2018-12-23 stsp }
5229 fe621944 2020-11-10 stsp
5230 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5231 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5232 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5233 772fcad5 2023-01-07 thomas
5234 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5235 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5236 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5237 fe621944 2020-11-10 stsp if (n < 0) {
5238 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5239 fe621944 2020-11-10 stsp goto done;
5240 fe621944 2020-11-10 stsp }
5241 fe621944 2020-11-10 stsp outoff += n;
5242 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5243 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5244 fe621944 2020-11-10 stsp if (err)
5245 fe621944 2020-11-10 stsp goto done;
5246 0208f208 2020-05-05 stsp }
5247 fe621944 2020-11-10 stsp
5248 0208f208 2020-05-05 stsp fputc('\n', outfile);
5249 fe621944 2020-11-10 stsp outoff++;
5250 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5251 772fcad5 2023-01-07 thomas if (err)
5252 772fcad5 2023-01-07 thomas goto done;
5253 772fcad5 2023-01-07 thomas
5254 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5255 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5256 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5257 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5258 772fcad5 2023-01-07 thomas if (n < 0) {
5259 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5260 772fcad5 2023-01-07 thomas goto done;
5261 772fcad5 2023-01-07 thomas }
5262 772fcad5 2023-01-07 thomas outoff += n;
5263 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5264 772fcad5 2023-01-07 thomas if (err)
5265 772fcad5 2023-01-07 thomas goto done;
5266 772fcad5 2023-01-07 thomas
5267 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5268 772fcad5 2023-01-07 thomas outoff++;
5269 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5270 abd2672a 2018-12-23 stsp done:
5271 abd2672a 2018-12-23 stsp free(id_str);
5272 5943eee2 2019-08-13 stsp free(logmsg);
5273 8b473291 2019-02-21 stsp free(refs_str);
5274 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5275 7510f233 2020-08-09 stsp if (err) {
5276 82c78e96 2022-08-06 thomas free(*lines);
5277 82c78e96 2022-08-06 thomas *lines = NULL;
5278 ae6a6978 2020-08-09 stsp *nlines = 0;
5279 7510f233 2020-08-09 stsp }
5280 fe621944 2020-11-10 stsp return err;
5281 abd2672a 2018-12-23 stsp }
5282 abd2672a 2018-12-23 stsp
5283 abd2672a 2018-12-23 stsp static const struct got_error *
5284 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5285 26ed57b2 2018-05-19 stsp {
5286 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5287 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5288 15a94983 2018-12-23 stsp int obj_type;
5289 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5290 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5291 fe621944 2020-11-10 stsp
5292 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5293 be97ab03 2023-01-19 thomas
5294 82c78e96 2022-08-06 thomas free(s->lines);
5295 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5296 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5297 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5298 fe621944 2020-11-10 stsp s->nlines = 0;
5299 26ed57b2 2018-05-19 stsp
5300 511a516b 2018-05-19 stsp f = got_opentemp();
5301 48ae06ee 2018-10-18 stsp if (f == NULL) {
5302 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5303 48ae06ee 2018-10-18 stsp goto done;
5304 48ae06ee 2018-10-18 stsp }
5305 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5306 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5307 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5308 be97ab03 2023-01-19 thomas goto done;
5309 be97ab03 2023-01-19 thomas }
5310 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5311 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5312 fb43ecf1 2019-02-11 stsp goto done;
5313 fb43ecf1 2019-02-11 stsp }
5314 48ae06ee 2018-10-18 stsp s->f = f;
5315 26ed57b2 2018-05-19 stsp
5316 15a94983 2018-12-23 stsp if (s->id1)
5317 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5318 15a94983 2018-12-23 stsp else
5319 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5320 15a94983 2018-12-23 stsp if (err)
5321 15a94983 2018-12-23 stsp goto done;
5322 15a94983 2018-12-23 stsp
5323 15a94983 2018-12-23 stsp switch (obj_type) {
5324 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5325 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5326 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5327 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5328 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5329 53d03f97 2023-01-10 thomas s->f);
5330 26ed57b2 2018-05-19 stsp break;
5331 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5332 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5333 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5334 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5335 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5336 26ed57b2 2018-05-19 stsp break;
5337 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5338 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5339 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5340 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5341 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5342 be97ab03 2023-01-19 thomas size_t nlines = 0;
5343 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5344 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5345 be97ab03 2023-01-19 thomas &changed_paths,
5346 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5347 be97ab03 2023-01-19 thomas s->force_text_diff,
5348 be97ab03 2023-01-19 thomas tog_diff_algo
5349 be97ab03 2023-01-19 thomas };
5350 abd2672a 2018-12-23 stsp
5351 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5352 be97ab03 2023-01-19 thomas if (lines == NULL) {
5353 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5354 be97ab03 2023-01-19 thomas goto done;
5355 be97ab03 2023-01-19 thomas }
5356 be97ab03 2023-01-19 thomas
5357 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5358 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5359 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5360 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5361 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5362 be97ab03 2023-01-19 thomas if (err)
5363 be97ab03 2023-01-19 thomas break;
5364 be97ab03 2023-01-19 thomas
5365 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5366 abd2672a 2018-12-23 stsp if (err)
5367 3ffacbe1 2020-02-02 tracey goto done;
5368 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5369 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5370 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5371 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5372 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5373 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5374 f44b1f58 2020-02-02 tracey if (err)
5375 f44b1f58 2020-02-02 tracey goto done;
5376 f44b1f58 2020-02-02 tracey } else {
5377 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5378 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5379 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5380 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5381 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5382 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5383 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5384 f44b1f58 2020-02-02 tracey if (err)
5385 f44b1f58 2020-02-02 tracey goto done;
5386 f5404e4e 2020-02-02 tracey break;
5387 15a087fe 2019-02-21 stsp }
5388 abd2672a 2018-12-23 stsp }
5389 abd2672a 2018-12-23 stsp }
5390 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5391 abd2672a 2018-12-23 stsp
5392 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5393 be97ab03 2023-01-19 thomas lines, nlines);
5394 26ed57b2 2018-05-19 stsp break;
5395 abd2672a 2018-12-23 stsp }
5396 26ed57b2 2018-05-19 stsp default:
5397 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5398 48ae06ee 2018-10-18 stsp break;
5399 26ed57b2 2018-05-19 stsp }
5400 48ae06ee 2018-10-18 stsp done:
5401 be97ab03 2023-01-19 thomas free(lines);
5402 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5403 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5404 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5405 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5406 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5407 48ae06ee 2018-10-18 stsp return err;
5408 48ae06ee 2018-10-18 stsp }
5409 26ed57b2 2018-05-19 stsp
5410 f5215bb9 2019-02-22 stsp static void
5411 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5412 f5215bb9 2019-02-22 stsp {
5413 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5414 f5215bb9 2019-02-22 stsp update_panels();
5415 f5215bb9 2019-02-22 stsp doupdate();
5416 f44b1f58 2020-02-02 tracey }
5417 f44b1f58 2020-02-02 tracey
5418 f44b1f58 2020-02-02 tracey static const struct got_error *
5419 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5420 f44b1f58 2020-02-02 tracey {
5421 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5422 f44b1f58 2020-02-02 tracey
5423 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5424 f44b1f58 2020-02-02 tracey return NULL;
5425 f44b1f58 2020-02-02 tracey }
5426 f44b1f58 2020-02-02 tracey
5427 2a7d3cd7 2022-09-17 thomas static void
5428 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5429 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5430 f44b1f58 2020-02-02 tracey {
5431 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5432 fc2737d5 2022-09-15 thomas
5433 2a7d3cd7 2022-09-17 thomas *f = s->f;
5434 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5435 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5436 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5437 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5438 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5439 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5440 fc2737d5 2022-09-15 thomas }
5441 fc2737d5 2022-09-15 thomas
5442 fc2737d5 2022-09-15 thomas static const struct got_error *
5443 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5444 fc2737d5 2022-09-15 thomas {
5445 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5446 fc2737d5 2022-09-15 thomas FILE *f;
5447 f44b1f58 2020-02-02 tracey int lineno;
5448 78eecffc 2022-06-23 thomas char *line = NULL;
5449 826082fe 2020-12-10 stsp size_t linesize = 0;
5450 826082fe 2020-12-10 stsp ssize_t linelen;
5451 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5452 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5453 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5454 f44b1f58 2020-02-02 tracey
5455 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5456 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5457 2a7d3cd7 2022-09-17 thomas "view search not supported");
5458 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5459 fc2737d5 2022-09-15 thomas &match, &selected);
5460 fc2737d5 2022-09-15 thomas
5461 f44b1f58 2020-02-02 tracey if (!view->searching) {
5462 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5463 f44b1f58 2020-02-02 tracey return NULL;
5464 f44b1f58 2020-02-02 tracey }
5465 f44b1f58 2020-02-02 tracey
5466 fc2737d5 2022-09-15 thomas if (*match) {
5467 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5468 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5469 f44b1f58 2020-02-02 tracey else
5470 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5471 4b3f9dac 2021-12-17 thomas } else
5472 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5473 f44b1f58 2020-02-02 tracey
5474 f44b1f58 2020-02-02 tracey while (1) {
5475 f44b1f58 2020-02-02 tracey off_t offset;
5476 f44b1f58 2020-02-02 tracey
5477 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5478 fc2737d5 2022-09-15 thomas if (*match == 0) {
5479 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5480 f44b1f58 2020-02-02 tracey break;
5481 f44b1f58 2020-02-02 tracey }
5482 f44b1f58 2020-02-02 tracey
5483 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5484 f44b1f58 2020-02-02 tracey lineno = 1;
5485 f44b1f58 2020-02-02 tracey else
5486 fc2737d5 2022-09-15 thomas lineno = nlines;
5487 f44b1f58 2020-02-02 tracey }
5488 f44b1f58 2020-02-02 tracey
5489 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5490 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5491 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5492 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5493 f44b1f58 2020-02-02 tracey free(line);
5494 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5495 f44b1f58 2020-02-02 tracey }
5496 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5497 78eecffc 2022-06-23 thomas if (linelen != -1) {
5498 78eecffc 2022-06-23 thomas char *exstr;
5499 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5500 78eecffc 2022-06-23 thomas if (err)
5501 78eecffc 2022-06-23 thomas break;
5502 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5503 78eecffc 2022-06-23 thomas &view->regmatch)) {
5504 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5505 fc2737d5 2022-09-15 thomas *match = lineno;
5506 78eecffc 2022-06-23 thomas free(exstr);
5507 78eecffc 2022-06-23 thomas break;
5508 78eecffc 2022-06-23 thomas }
5509 78eecffc 2022-06-23 thomas free(exstr);
5510 f44b1f58 2020-02-02 tracey }
5511 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5512 f44b1f58 2020-02-02 tracey lineno++;
5513 f44b1f58 2020-02-02 tracey else
5514 f44b1f58 2020-02-02 tracey lineno--;
5515 f44b1f58 2020-02-02 tracey }
5516 826082fe 2020-12-10 stsp free(line);
5517 f44b1f58 2020-02-02 tracey
5518 fc2737d5 2022-09-15 thomas if (*match) {
5519 fc2737d5 2022-09-15 thomas *first = *match;
5520 fc2737d5 2022-09-15 thomas *selected = 1;
5521 f44b1f58 2020-02-02 tracey }
5522 f44b1f58 2020-02-02 tracey
5523 05171be4 2022-06-23 thomas return err;
5524 f5215bb9 2019-02-22 stsp }
5525 f5215bb9 2019-02-22 stsp
5526 48ae06ee 2018-10-18 stsp static const struct got_error *
5527 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5528 a0f32f33 2022-06-13 thomas {
5529 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5530 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5531 a0f32f33 2022-06-13 thomas
5532 a0f32f33 2022-06-13 thomas free(s->id1);
5533 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5534 a0f32f33 2022-06-13 thomas free(s->id2);
5535 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5536 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5537 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5538 a0f32f33 2022-06-13 thomas s->f = NULL;
5539 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5540 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5541 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5542 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5543 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5544 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5545 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5546 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5547 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5548 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5549 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5550 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5551 82c78e96 2022-08-06 thomas free(s->lines);
5552 82c78e96 2022-08-06 thomas s->lines = NULL;
5553 a0f32f33 2022-06-13 thomas s->nlines = 0;
5554 a0f32f33 2022-06-13 thomas return err;
5555 a0f32f33 2022-06-13 thomas }
5556 a0f32f33 2022-06-13 thomas
5557 a0f32f33 2022-06-13 thomas static const struct got_error *
5558 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5559 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5560 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5561 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5562 48ae06ee 2018-10-18 stsp {
5563 48ae06ee 2018-10-18 stsp const struct got_error *err;
5564 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5565 5dc9f4bc 2018-08-04 stsp
5566 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5567 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5568 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5569 a0f32f33 2022-06-13 thomas
5570 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5571 51b7e1c3 2023-04-14 thomas int type1, type2;
5572 15a94983 2018-12-23 stsp
5573 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5574 51b7e1c3 2023-04-14 thomas if (err)
5575 51b7e1c3 2023-04-14 thomas goto done;
5576 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5577 51b7e1c3 2023-04-14 thomas if (err)
5578 51b7e1c3 2023-04-14 thomas goto done;
5579 51b7e1c3 2023-04-14 thomas
5580 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5581 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5582 51b7e1c3 2023-04-14 thomas goto done;
5583 51b7e1c3 2023-04-14 thomas }
5584 15a94983 2018-12-23 stsp }
5585 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5586 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5587 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5588 f44b1f58 2020-02-02 tracey s->repo = repo;
5589 f44b1f58 2020-02-02 tracey s->id1 = id1;
5590 f44b1f58 2020-02-02 tracey s->id2 = id2;
5591 3dbaef42 2020-11-24 stsp s->label1 = label1;
5592 3dbaef42 2020-11-24 stsp s->label2 = label2;
5593 48ae06ee 2018-10-18 stsp
5594 15a94983 2018-12-23 stsp if (id1) {
5595 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5596 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5597 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5598 51b7e1c3 2023-04-14 thomas goto done;
5599 51b7e1c3 2023-04-14 thomas }
5600 48ae06ee 2018-10-18 stsp } else
5601 5465d566 2020-02-01 tracey s->id1 = NULL;
5602 48ae06ee 2018-10-18 stsp
5603 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5604 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5605 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5606 a0f32f33 2022-06-13 thomas goto done;
5607 48ae06ee 2018-10-18 stsp }
5608 a0f32f33 2022-06-13 thomas
5609 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5610 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5611 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5612 9a1d7435 2022-06-23 thomas goto done;
5613 9a1d7435 2022-06-23 thomas }
5614 9a1d7435 2022-06-23 thomas
5615 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5616 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5617 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5618 a0f32f33 2022-06-13 thomas goto done;
5619 a0f32f33 2022-06-13 thomas }
5620 a0f32f33 2022-06-13 thomas
5621 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5622 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5623 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5624 19a6a6b5 2022-07-01 thomas goto done;
5625 19a6a6b5 2022-07-01 thomas }
5626 19a6a6b5 2022-07-01 thomas
5627 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5628 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5629 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5630 19a6a6b5 2022-07-01 thomas goto done;
5631 19a6a6b5 2022-07-01 thomas }
5632 19a6a6b5 2022-07-01 thomas
5633 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5634 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5635 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5636 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5637 5465d566 2020-02-01 tracey s->repo = repo;
5638 6d17833f 2019-11-08 stsp
5639 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5640 6568f0aa 2022-08-06 thomas int rc;
5641 6d17833f 2019-11-08 stsp
5642 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5643 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5644 6568f0aa 2022-08-06 thomas if (rc != ERR)
5645 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5646 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5647 6568f0aa 2022-08-06 thomas if (rc != ERR)
5648 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5649 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5650 6568f0aa 2022-08-06 thomas if (rc != ERR)
5651 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5652 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5653 6568f0aa 2022-08-06 thomas if (rc != ERR)
5654 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5655 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5656 6568f0aa 2022-08-06 thomas if (rc != ERR)
5657 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5658 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5659 6568f0aa 2022-08-06 thomas if (rc != ERR)
5660 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5661 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5662 6568f0aa 2022-08-06 thomas if (rc != ERR)
5663 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5664 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5665 6568f0aa 2022-08-06 thomas if (rc != ERR)
5666 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5667 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5668 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5669 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5670 a0f32f33 2022-06-13 thomas goto done;
5671 6568f0aa 2022-08-06 thomas }
5672 6d17833f 2019-11-08 stsp }
5673 5dc9f4bc 2018-08-04 stsp
5674 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5675 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5676 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5677 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5678 f5215bb9 2019-02-22 stsp
5679 5465d566 2020-02-01 tracey err = create_diff(s);
5680 48ae06ee 2018-10-18 stsp
5681 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5682 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5683 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5684 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5685 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5686 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5687 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5688 a0f32f33 2022-06-13 thomas done:
5689 51b7e1c3 2023-04-14 thomas if (err) {
5690 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5691 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5692 51b7e1c3 2023-04-14 thomas view_close(view);
5693 51b7e1c3 2023-04-14 thomas }
5694 e5a0f69f 2018-08-18 stsp return err;
5695 5dc9f4bc 2018-08-04 stsp }
5696 5dc9f4bc 2018-08-04 stsp
5697 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5698 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5699 5dc9f4bc 2018-08-04 stsp {
5700 a3404814 2018-09-02 stsp const struct got_error *err;
5701 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5702 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5703 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5704 a3404814 2018-09-02 stsp
5705 a3404814 2018-09-02 stsp if (s->id1) {
5706 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5707 a3404814 2018-09-02 stsp if (err)
5708 a3404814 2018-09-02 stsp return err;
5709 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5710 3dbaef42 2020-11-24 stsp } else
5711 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5712 3dbaef42 2020-11-24 stsp
5713 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5714 a3404814 2018-09-02 stsp if (err)
5715 a3404814 2018-09-02 stsp return err;
5716 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5717 26ed57b2 2018-05-19 stsp
5718 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5719 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5720 a3404814 2018-09-02 stsp free(id_str1);
5721 a3404814 2018-09-02 stsp free(id_str2);
5722 a3404814 2018-09-02 stsp return err;
5723 a3404814 2018-09-02 stsp }
5724 a3404814 2018-09-02 stsp free(id_str1);
5725 a3404814 2018-09-02 stsp free(id_str2);
5726 a3404814 2018-09-02 stsp
5727 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5728 267bb3b8 2021-08-01 stsp free(header);
5729 267bb3b8 2021-08-01 stsp return err;
5730 15a087fe 2019-02-21 stsp }
5731 15a087fe 2019-02-21 stsp
5732 15a087fe 2019-02-21 stsp static const struct got_error *
5733 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5734 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5735 15a087fe 2019-02-21 stsp {
5736 d7a04538 2019-02-21 stsp const struct got_error *err;
5737 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5738 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5739 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5740 15a087fe 2019-02-21 stsp
5741 15a087fe 2019-02-21 stsp free(s->id2);
5742 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5743 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5744 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5745 15a087fe 2019-02-21 stsp
5746 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5747 d7a04538 2019-02-21 stsp if (err)
5748 d7a04538 2019-02-21 stsp return err;
5749 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5750 15a087fe 2019-02-21 stsp free(s->id1);
5751 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5752 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5753 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5754 15a087fe 2019-02-21 stsp return NULL;
5755 0cf4efb1 2018-09-29 stsp }
5756 0cf4efb1 2018-09-29 stsp
5757 0cf4efb1 2018-09-29 stsp static const struct got_error *
5758 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5759 adf4c9e0 2022-07-03 thomas {
5760 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5761 adf4c9e0 2022-07-03 thomas
5762 adf4c9e0 2022-07-03 thomas view->count = 0;
5763 adf4c9e0 2022-07-03 thomas wclear(view->window);
5764 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5765 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5766 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5767 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5768 adf4c9e0 2022-07-03 thomas return create_diff(s);
5769 82c78e96 2022-08-06 thomas }
5770 82c78e96 2022-08-06 thomas
5771 82c78e96 2022-08-06 thomas static void
5772 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5773 82c78e96 2022-08-06 thomas {
5774 82c78e96 2022-08-06 thomas int start, i;
5775 82c78e96 2022-08-06 thomas
5776 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5777 82c78e96 2022-08-06 thomas
5778 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5779 82c78e96 2022-08-06 thomas if (i == 0)
5780 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5781 82c78e96 2022-08-06 thomas if (--i == start)
5782 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5783 82c78e96 2022-08-06 thomas }
5784 82c78e96 2022-08-06 thomas
5785 82c78e96 2022-08-06 thomas s->selected_line = 1;
5786 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5787 adf4c9e0 2022-07-03 thomas }
5788 adf4c9e0 2022-07-03 thomas
5789 82c78e96 2022-08-06 thomas static void
5790 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5791 82c78e96 2022-08-06 thomas {
5792 82c78e96 2022-08-06 thomas int start, i;
5793 82c78e96 2022-08-06 thomas
5794 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5795 82c78e96 2022-08-06 thomas
5796 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5797 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5798 82c78e96 2022-08-06 thomas i = 0;
5799 82c78e96 2022-08-06 thomas if (++i == start)
5800 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5801 82c78e96 2022-08-06 thomas }
5802 82c78e96 2022-08-06 thomas
5803 82c78e96 2022-08-06 thomas s->selected_line = 1;
5804 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5805 82c78e96 2022-08-06 thomas }
5806 82c78e96 2022-08-06 thomas
5807 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5808 4fc71f3b 2022-07-12 thomas int, int, int);
5809 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5810 4fc71f3b 2022-07-12 thomas int, int);
5811 4fc71f3b 2022-07-12 thomas
5812 adf4c9e0 2022-07-03 thomas static const struct got_error *
5813 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5814 e5a0f69f 2018-08-18 stsp {
5815 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5816 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5817 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5818 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5819 826082fe 2020-12-10 stsp char *line = NULL;
5820 826082fe 2020-12-10 stsp size_t linesize = 0;
5821 826082fe 2020-12-10 stsp ssize_t linelen;
5822 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5823 e5a0f69f 2018-08-18 stsp
5824 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5825 82c78e96 2022-08-06 thomas
5826 e5a0f69f 2018-08-18 stsp switch (ch) {
5827 05171be4 2022-06-23 thomas case '0':
5828 05171be4 2022-06-23 thomas case '$':
5829 05171be4 2022-06-23 thomas case KEY_RIGHT:
5830 05171be4 2022-06-23 thomas case 'l':
5831 05171be4 2022-06-23 thomas case KEY_LEFT:
5832 05171be4 2022-06-23 thomas case 'h':
5833 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5834 05171be4 2022-06-23 thomas break;
5835 64453f7e 2020-11-21 stsp case 'a':
5836 3dbaef42 2020-11-24 stsp case 'w':
5837 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5838 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5839 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5840 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5841 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5842 f2d06bef 2023-01-23 thomas }
5843 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5844 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5845 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5846 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5847 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5848 f2d06bef 2023-01-23 thomas }
5849 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5850 912a3f79 2021-08-30 j break;
5851 912a3f79 2021-08-30 j case 'g':
5852 912a3f79 2021-08-30 j case KEY_HOME:
5853 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5854 07b0611c 2022-06-23 thomas view->count = 0;
5855 64453f7e 2020-11-21 stsp break;
5856 912a3f79 2021-08-30 j case 'G':
5857 912a3f79 2021-08-30 j case KEY_END:
5858 07b0611c 2022-06-23 thomas view->count = 0;
5859 912a3f79 2021-08-30 j if (s->eof)
5860 912a3f79 2021-08-30 j break;
5861 912a3f79 2021-08-30 j
5862 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5863 912a3f79 2021-08-30 j s->eof = 1;
5864 912a3f79 2021-08-30 j break;
5865 1e37a5c2 2019-05-12 jcs case 'k':
5866 1e37a5c2 2019-05-12 jcs case KEY_UP:
5867 f7140bf5 2021-10-17 thomas case CTRL('p'):
5868 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5869 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5870 07b0611c 2022-06-23 thomas else
5871 07b0611c 2022-06-23 thomas view->count = 0;
5872 1e37a5c2 2019-05-12 jcs break;
5873 70f17a53 2022-06-13 thomas case CTRL('u'):
5874 23427b14 2022-06-23 thomas case 'u':
5875 70f17a53 2022-06-13 thomas nscroll /= 2;
5876 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5877 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5878 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5879 1c5e5faa 2022-06-23 thomas case 'b':
5880 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5881 07b0611c 2022-06-23 thomas view->count = 0;
5882 26ed57b2 2018-05-19 stsp break;
5883 07b0611c 2022-06-23 thomas }
5884 1e37a5c2 2019-05-12 jcs i = 0;
5885 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5886 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5887 1e37a5c2 2019-05-12 jcs break;
5888 1e37a5c2 2019-05-12 jcs case 'j':
5889 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5890 f7140bf5 2021-10-17 thomas case CTRL('n'):
5891 1e37a5c2 2019-05-12 jcs if (!s->eof)
5892 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5893 07b0611c 2022-06-23 thomas else
5894 07b0611c 2022-06-23 thomas view->count = 0;
5895 1e37a5c2 2019-05-12 jcs break;
5896 70f17a53 2022-06-13 thomas case CTRL('d'):
5897 23427b14 2022-06-23 thomas case 'd':
5898 70f17a53 2022-06-13 thomas nscroll /= 2;
5899 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5900 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5901 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5902 1c5e5faa 2022-06-23 thomas case 'f':
5903 1e37a5c2 2019-05-12 jcs case ' ':
5904 07b0611c 2022-06-23 thomas if (s->eof) {
5905 07b0611c 2022-06-23 thomas view->count = 0;
5906 1e37a5c2 2019-05-12 jcs break;
5907 07b0611c 2022-06-23 thomas }
5908 1e37a5c2 2019-05-12 jcs i = 0;
5909 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5910 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5911 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5912 826082fe 2020-12-10 stsp if (linelen == -1) {
5913 826082fe 2020-12-10 stsp if (feof(s->f)) {
5914 826082fe 2020-12-10 stsp s->eof = 1;
5915 826082fe 2020-12-10 stsp } else
5916 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5917 34bc9ec9 2019-02-22 stsp break;
5918 826082fe 2020-12-10 stsp }
5919 1e37a5c2 2019-05-12 jcs }
5920 826082fe 2020-12-10 stsp free(line);
5921 1e37a5c2 2019-05-12 jcs break;
5922 82c78e96 2022-08-06 thomas case '(':
5923 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5924 82c78e96 2022-08-06 thomas break;
5925 82c78e96 2022-08-06 thomas case ')':
5926 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5927 82c78e96 2022-08-06 thomas break;
5928 82c78e96 2022-08-06 thomas case '{':
5929 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5930 82c78e96 2022-08-06 thomas break;
5931 82c78e96 2022-08-06 thomas case '}':
5932 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5933 82c78e96 2022-08-06 thomas break;
5934 1e37a5c2 2019-05-12 jcs case '[':
5935 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5936 1e37a5c2 2019-05-12 jcs s->diff_context--;
5937 aa61903a 2021-12-31 thomas s->matched_line = 0;
5938 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5939 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5940 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5941 27829c9e 2020-11-21 stsp s->nlines) {
5942 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5943 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5944 27829c9e 2020-11-21 stsp }
5945 07b0611c 2022-06-23 thomas } else
5946 07b0611c 2022-06-23 thomas view->count = 0;
5947 1e37a5c2 2019-05-12 jcs break;
5948 1e37a5c2 2019-05-12 jcs case ']':
5949 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5950 1e37a5c2 2019-05-12 jcs s->diff_context++;
5951 aa61903a 2021-12-31 thomas s->matched_line = 0;
5952 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5953 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5954 07b0611c 2022-06-23 thomas } else
5955 07b0611c 2022-06-23 thomas view->count = 0;
5956 1e37a5c2 2019-05-12 jcs break;
5957 1e37a5c2 2019-05-12 jcs case '<':
5958 1e37a5c2 2019-05-12 jcs case ',':
5959 777aae21 2022-07-20 thomas case 'K':
5960 4fc71f3b 2022-07-12 thomas up = 1;
5961 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5962 4fc71f3b 2022-07-12 thomas case '>':
5963 4fc71f3b 2022-07-12 thomas case '.':
5964 777aae21 2022-07-20 thomas case 'J':
5965 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5966 07b0611c 2022-06-23 thomas view->count = 0;
5967 48ae06ee 2018-10-18 stsp break;
5968 07b0611c 2022-06-23 thomas }
5969 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5970 6524637e 2019-02-21 stsp
5971 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5972 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5973 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5974 15a087fe 2019-02-21 stsp
5975 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5976 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5977 4fc71f3b 2022-07-12 thomas if (err)
5978 4fc71f3b 2022-07-12 thomas break;
5979 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5980 15a087fe 2019-02-21 stsp
5981 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5982 4fc71f3b 2022-07-12 thomas break;
5983 15a087fe 2019-02-21 stsp
5984 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5985 4fc71f3b 2022-07-12 thomas if (err)
5986 4fc71f3b 2022-07-12 thomas break;
5987 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5988 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5989 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5990 5e224a3e 2019-02-22 stsp
5991 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5992 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5993 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5994 4fc71f3b 2022-07-12 thomas
5995 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5996 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5997 4fc71f3b 2022-07-12 thomas if (err)
5998 4fc71f3b 2022-07-12 thomas break;
5999 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
6000 5a8b5076 2020-12-05 stsp
6001 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
6002 4fc71f3b 2022-07-12 thomas break;
6003 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
6004 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
6005 4fc71f3b 2022-07-12 thomas bs->selected_line);
6006 4fc71f3b 2022-07-12 thomas if (id == NULL)
6007 4fc71f3b 2022-07-12 thomas break;
6008 15a087fe 2019-02-21 stsp
6009 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
6010 4fc71f3b 2022-07-12 thomas break;
6011 15a087fe 2019-02-21 stsp
6012 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6013 4fc71f3b 2022-07-12 thomas if (err)
6014 4fc71f3b 2022-07-12 thomas break;
6015 4fc71f3b 2022-07-12 thomas }
6016 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6017 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
6018 aa61903a 2021-12-31 thomas s->matched_line = 0;
6019 05171be4 2022-06-23 thomas view->x = 0;
6020 1e37a5c2 2019-05-12 jcs
6021 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
6022 1e37a5c2 2019-05-12 jcs err = create_diff(s);
6023 1e37a5c2 2019-05-12 jcs break;
6024 1e37a5c2 2019-05-12 jcs default:
6025 07b0611c 2022-06-23 thomas view->count = 0;
6026 1e37a5c2 2019-05-12 jcs break;
6027 26ed57b2 2018-05-19 stsp }
6028 e5a0f69f 2018-08-18 stsp
6029 bcbd79e2 2018-08-19 stsp return err;
6030 26ed57b2 2018-05-19 stsp }
6031 26ed57b2 2018-05-19 stsp
6032 4ed7e80c 2018-05-20 stsp static const struct got_error *
6033 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
6034 9f7d7167 2018-04-29 stsp {
6035 1d98034b 2023-04-14 thomas const struct got_error *error;
6036 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
6037 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
6038 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
6039 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
6040 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
6041 66b04f8f 2023-07-19 thomas char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6042 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
6043 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
6044 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
6045 3dbaef42 2020-11-24 stsp const char *errstr;
6046 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
6047 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6048 70ac5f84 2019-03-28 stsp
6049 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6050 26ed57b2 2018-05-19 stsp switch (ch) {
6051 64453f7e 2020-11-21 stsp case 'a':
6052 64453f7e 2020-11-21 stsp force_text_diff = 1;
6053 3dbaef42 2020-11-24 stsp break;
6054 3dbaef42 2020-11-24 stsp case 'C':
6055 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6056 3dbaef42 2020-11-24 stsp &errstr);
6057 3dbaef42 2020-11-24 stsp if (errstr != NULL)
6058 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
6059 8f666e67 2022-02-12 thomas errstr, errstr);
6060 64453f7e 2020-11-21 stsp break;
6061 09b5bff8 2020-02-23 naddy case 'r':
6062 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
6063 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
6064 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
6065 09b5bff8 2020-02-23 naddy optarg);
6066 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
6067 09b5bff8 2020-02-23 naddy break;
6068 3dbaef42 2020-11-24 stsp case 'w':
6069 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
6070 3dbaef42 2020-11-24 stsp break;
6071 26ed57b2 2018-05-19 stsp default:
6072 17020d27 2019-03-07 stsp usage_diff();
6073 26ed57b2 2018-05-19 stsp /* NOTREACHED */
6074 26ed57b2 2018-05-19 stsp }
6075 26ed57b2 2018-05-19 stsp }
6076 26ed57b2 2018-05-19 stsp
6077 26ed57b2 2018-05-19 stsp argc -= optind;
6078 26ed57b2 2018-05-19 stsp argv += optind;
6079 26ed57b2 2018-05-19 stsp
6080 26ed57b2 2018-05-19 stsp if (argc == 0) {
6081 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
6082 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
6083 15a94983 2018-12-23 stsp id_str1 = argv[0];
6084 15a94983 2018-12-23 stsp id_str2 = argv[1];
6085 26ed57b2 2018-05-19 stsp } else
6086 26ed57b2 2018-05-19 stsp usage_diff();
6087 eb6600df 2019-01-04 stsp
6088 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6089 7cd52833 2022-06-23 thomas if (error)
6090 7cd52833 2022-06-23 thomas goto done;
6091 7cd52833 2022-06-23 thomas
6092 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
6093 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6094 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6095 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6096 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
6097 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6098 c156c7a4 2020-12-18 stsp goto done;
6099 a273ac94 2020-02-23 naddy if (worktree)
6100 a273ac94 2020-02-23 naddy repo_path =
6101 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
6102 a273ac94 2020-02-23 naddy else
6103 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
6104 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6105 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6106 c156c7a4 2020-12-18 stsp goto done;
6107 c156c7a4 2020-12-18 stsp }
6108 a273ac94 2020-02-23 naddy }
6109 a273ac94 2020-02-23 naddy
6110 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6111 eb6600df 2019-01-04 stsp if (error)
6112 eb6600df 2019-01-04 stsp goto done;
6113 26ed57b2 2018-05-19 stsp
6114 557d3365 2023-04-14 thomas init_curses();
6115 a273ac94 2020-02-23 naddy
6116 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6117 51a10b52 2020-12-26 stsp if (error)
6118 51a10b52 2020-12-26 stsp goto done;
6119 51a10b52 2020-12-26 stsp
6120 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6121 26ed57b2 2018-05-19 stsp if (error)
6122 26ed57b2 2018-05-19 stsp goto done;
6123 26ed57b2 2018-05-19 stsp
6124 66b04f8f 2023-07-19 thomas if (id_str1 != NULL) {
6125 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6126 66b04f8f 2023-07-19 thomas repo, worktree);
6127 66b04f8f 2023-07-19 thomas if (error != NULL)
6128 66b04f8f 2023-07-19 thomas goto done;
6129 66b04f8f 2023-07-19 thomas if (keyword_idstr1 != NULL)
6130 66b04f8f 2023-07-19 thomas id_str1 = keyword_idstr1;
6131 66b04f8f 2023-07-19 thomas }
6132 66b04f8f 2023-07-19 thomas if (id_str2 != NULL) {
6133 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6134 66b04f8f 2023-07-19 thomas repo, worktree);
6135 66b04f8f 2023-07-19 thomas if (error != NULL)
6136 66b04f8f 2023-07-19 thomas goto done;
6137 66b04f8f 2023-07-19 thomas if (keyword_idstr2 != NULL)
6138 66b04f8f 2023-07-19 thomas id_str2 = keyword_idstr2;
6139 66b04f8f 2023-07-19 thomas }
6140 66b04f8f 2023-07-19 thomas
6141 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
6142 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6143 26ed57b2 2018-05-19 stsp if (error)
6144 26ed57b2 2018-05-19 stsp goto done;
6145 26ed57b2 2018-05-19 stsp
6146 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
6147 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6148 26ed57b2 2018-05-19 stsp if (error)
6149 26ed57b2 2018-05-19 stsp goto done;
6150 26ed57b2 2018-05-19 stsp
6151 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6152 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
6153 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6154 ea5e7bb5 2018-08-01 stsp goto done;
6155 ea5e7bb5 2018-08-01 stsp }
6156 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6157 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
6158 5dc9f4bc 2018-08-04 stsp if (error)
6159 5dc9f4bc 2018-08-04 stsp goto done;
6160 349dfd1e 2023-07-23 thomas
6161 349dfd1e 2023-07-23 thomas if (worktree) {
6162 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
6163 349dfd1e 2023-07-23 thomas if (error != NULL)
6164 349dfd1e 2023-07-23 thomas goto done;
6165 349dfd1e 2023-07-23 thomas }
6166 349dfd1e 2023-07-23 thomas
6167 557d3365 2023-04-14 thomas error = view_loop(view);
6168 349dfd1e 2023-07-23 thomas
6169 26ed57b2 2018-05-19 stsp done:
6170 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
6171 66b04f8f 2023-07-19 thomas free(keyword_idstr1);
6172 66b04f8f 2023-07-19 thomas free(keyword_idstr2);
6173 3dbaef42 2020-11-24 stsp free(label1);
6174 3dbaef42 2020-11-24 stsp free(label2);
6175 c02c541e 2019-03-29 stsp free(repo_path);
6176 a273ac94 2020-02-23 naddy free(cwd);
6177 1d0f4054 2021-06-17 stsp if (repo) {
6178 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6179 1d0f4054 2021-06-17 stsp if (error == NULL)
6180 1d0f4054 2021-06-17 stsp error = close_err;
6181 1d0f4054 2021-06-17 stsp }
6182 a273ac94 2020-02-23 naddy if (worktree)
6183 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6184 7cd52833 2022-06-23 thomas if (pack_fds) {
6185 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6186 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6187 7cd52833 2022-06-23 thomas if (error == NULL)
6188 7cd52833 2022-06-23 thomas error = pack_err;
6189 b85a3496 2023-04-14 thomas }
6190 51a10b52 2020-12-26 stsp tog_free_refs();
6191 26ed57b2 2018-05-19 stsp return error;
6192 9f7d7167 2018-04-29 stsp }
6193 9f7d7167 2018-04-29 stsp
6194 4ed7e80c 2018-05-20 stsp __dead static void
6195 9f7d7167 2018-04-29 stsp usage_blame(void)
6196 9f7d7167 2018-04-29 stsp {
6197 80ddbec8 2018-04-29 stsp endwin();
6198 91198554 2022-06-23 thomas fprintf(stderr,
6199 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6200 9f7d7167 2018-04-29 stsp getprogname());
6201 9f7d7167 2018-04-29 stsp exit(1);
6202 9f7d7167 2018-04-29 stsp }
6203 84451b3e 2018-07-10 stsp
6204 84451b3e 2018-07-10 stsp struct tog_blame_line {
6205 84451b3e 2018-07-10 stsp int annotated;
6206 84451b3e 2018-07-10 stsp struct got_object_id *id;
6207 84451b3e 2018-07-10 stsp };
6208 9f7d7167 2018-04-29 stsp
6209 4ed7e80c 2018-05-20 stsp static const struct got_error *
6210 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6211 84451b3e 2018-07-10 stsp {
6212 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6213 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6214 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6215 84451b3e 2018-07-10 stsp const struct got_error *err;
6216 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6217 826082fe 2020-12-10 stsp char *line = NULL;
6218 826082fe 2020-12-10 stsp size_t linesize = 0;
6219 826082fe 2020-12-10 stsp ssize_t linelen;
6220 84451b3e 2018-07-10 stsp wchar_t *wline;
6221 27a741e5 2019-09-11 stsp int width;
6222 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6223 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6224 ab089a2a 2018-07-12 stsp char *id_str;
6225 11b20872 2019-11-08 stsp struct tog_color *tc;
6226 ab089a2a 2018-07-12 stsp
6227 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6228 ab089a2a 2018-07-12 stsp if (err)
6229 ab089a2a 2018-07-12 stsp return err;
6230 84451b3e 2018-07-10 stsp
6231 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6232 f7d12f7e 2018-08-01 stsp werase(view->window);
6233 84451b3e 2018-07-10 stsp
6234 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6235 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6236 ab089a2a 2018-07-12 stsp free(id_str);
6237 ab089a2a 2018-07-12 stsp return err;
6238 ab089a2a 2018-07-12 stsp }
6239 ab089a2a 2018-07-12 stsp
6240 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6241 ab089a2a 2018-07-12 stsp free(line);
6242 2550e4c3 2018-07-13 stsp line = NULL;
6243 1cae65b4 2019-09-22 stsp if (err)
6244 1cae65b4 2019-09-22 stsp return err;
6245 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6246 a3404814 2018-09-02 stsp wstandout(view->window);
6247 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6248 11b20872 2019-11-08 stsp if (tc)
6249 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6250 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6251 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6252 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6253 11b20872 2019-11-08 stsp if (tc)
6254 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6255 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6256 a3404814 2018-09-02 stsp wstandend(view->window);
6257 2550e4c3 2018-07-13 stsp free(wline);
6258 2550e4c3 2018-07-13 stsp wline = NULL;
6259 ab089a2a 2018-07-12 stsp
6260 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6261 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6262 07dd3ed3 2022-08-06 thomas
6263 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6264 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6265 1134ebde 2023-04-22 thomas int rc;
6266 1134ebde 2023-04-22 thomas
6267 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6268 1134ebde 2023-04-22 thomas if (rc)
6269 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6270 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6271 1134ebde 2023-04-22 thomas }
6272 1134ebde 2023-04-22 thomas
6273 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6274 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6275 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6276 ab089a2a 2018-07-12 stsp free(id_str);
6277 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6278 ab089a2a 2018-07-12 stsp }
6279 ab089a2a 2018-07-12 stsp free(id_str);
6280 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6281 3f60a8ef 2018-07-10 stsp free(line);
6282 2550e4c3 2018-07-13 stsp line = NULL;
6283 3f60a8ef 2018-07-10 stsp if (err)
6284 3f60a8ef 2018-07-10 stsp return err;
6285 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6286 2550e4c3 2018-07-13 stsp free(wline);
6287 2550e4c3 2018-07-13 stsp wline = NULL;
6288 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6289 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6290 3f60a8ef 2018-07-10 stsp
6291 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6292 05171be4 2022-06-23 thomas view->maxx = 0;
6293 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6294 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6295 826082fe 2020-12-10 stsp if (linelen == -1) {
6296 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6297 826082fe 2020-12-10 stsp s->eof = 1;
6298 826082fe 2020-12-10 stsp break;
6299 826082fe 2020-12-10 stsp }
6300 84451b3e 2018-07-10 stsp free(line);
6301 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6302 84451b3e 2018-07-10 stsp }
6303 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6304 07dd3ed3 2022-08-06 thomas continue;
6305 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6306 826082fe 2020-12-10 stsp continue;
6307 cbea3800 2022-06-23 thomas
6308 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6309 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6310 cbea3800 2022-06-23 thomas if (err) {
6311 cbea3800 2022-06-23 thomas free(line);
6312 cbea3800 2022-06-23 thomas return err;
6313 cbea3800 2022-06-23 thomas }
6314 cbea3800 2022-06-23 thomas free(wline);
6315 cbea3800 2022-06-23 thomas wline = NULL;
6316 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6317 84451b3e 2018-07-10 stsp
6318 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6319 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6320 b700b5d6 2018-07-10 stsp
6321 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6322 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6323 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6324 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6325 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6326 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6327 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6328 8d0fe45a 2019-08-12 stsp char *id_str;
6329 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6330 91198554 2022-06-23 thomas blame_line->id);
6331 8d0fe45a 2019-08-12 stsp if (err) {
6332 8d0fe45a 2019-08-12 stsp free(line);
6333 8d0fe45a 2019-08-12 stsp return err;
6334 8d0fe45a 2019-08-12 stsp }
6335 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6336 11b20872 2019-11-08 stsp if (tc)
6337 11b20872 2019-11-08 stsp wattr_on(view->window,
6338 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6339 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6340 11b20872 2019-11-08 stsp if (tc)
6341 11b20872 2019-11-08 stsp wattr_off(view->window,
6342 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6343 8d0fe45a 2019-08-12 stsp free(id_str);
6344 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6345 8d0fe45a 2019-08-12 stsp } else {
6346 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6347 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6348 84451b3e 2018-07-10 stsp }
6349 ee41ec32 2018-07-10 stsp } else {
6350 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6351 ee41ec32 2018-07-10 stsp prev_id = NULL;
6352 ee41ec32 2018-07-10 stsp }
6353 84451b3e 2018-07-10 stsp
6354 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6355 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6356 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6357 27a741e5 2019-09-11 stsp
6358 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6359 41605754 2020-11-12 stsp width = 9;
6360 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6361 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6362 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6363 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6364 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6365 41605754 2020-11-12 stsp if (err) {
6366 41605754 2020-11-12 stsp free(line);
6367 41605754 2020-11-12 stsp return err;
6368 41605754 2020-11-12 stsp }
6369 41605754 2020-11-12 stsp width += 9;
6370 41605754 2020-11-12 stsp } else {
6371 923086fd 2022-06-23 thomas int skip;
6372 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6373 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6374 923086fd 2022-06-23 thomas if (err) {
6375 923086fd 2022-06-23 thomas free(line);
6376 923086fd 2022-06-23 thomas return err;
6377 05171be4 2022-06-23 thomas }
6378 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6379 02bce7e2 2022-06-23 thomas width += 9;
6380 41605754 2020-11-12 stsp free(wline);
6381 41605754 2020-11-12 stsp wline = NULL;
6382 41605754 2020-11-12 stsp }
6383 41605754 2020-11-12 stsp
6384 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6385 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6386 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6387 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6388 84451b3e 2018-07-10 stsp }
6389 826082fe 2020-12-10 stsp free(line);
6390 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6391 84451b3e 2018-07-10 stsp
6392 a5d43cac 2022-07-01 thomas view_border(view);
6393 84451b3e 2018-07-10 stsp
6394 84451b3e 2018-07-10 stsp return NULL;
6395 84451b3e 2018-07-10 stsp }
6396 84451b3e 2018-07-10 stsp
6397 84451b3e 2018-07-10 stsp static const struct got_error *
6398 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6399 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6400 84451b3e 2018-07-10 stsp {
6401 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6402 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6403 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6404 1a76625f 2018-10-22 stsp int errcode;
6405 84451b3e 2018-07-10 stsp
6406 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6407 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6408 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6409 84451b3e 2018-07-10 stsp
6410 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6411 1a76625f 2018-10-22 stsp if (errcode)
6412 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6413 84451b3e 2018-07-10 stsp
6414 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6415 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6416 d68a0a7d 2018-07-10 stsp goto done;
6417 d68a0a7d 2018-07-10 stsp }
6418 d68a0a7d 2018-07-10 stsp
6419 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6420 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6421 d68a0a7d 2018-07-10 stsp
6422 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6423 d68a0a7d 2018-07-10 stsp if (line->annotated)
6424 d68a0a7d 2018-07-10 stsp goto done;
6425 d68a0a7d 2018-07-10 stsp
6426 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6427 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6428 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6429 84451b3e 2018-07-10 stsp goto done;
6430 84451b3e 2018-07-10 stsp }
6431 84451b3e 2018-07-10 stsp line->annotated = 1;
6432 84451b3e 2018-07-10 stsp done:
6433 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6434 1a76625f 2018-10-22 stsp if (errcode)
6435 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6436 84451b3e 2018-07-10 stsp return err;
6437 84451b3e 2018-07-10 stsp }
6438 84451b3e 2018-07-10 stsp
6439 84451b3e 2018-07-10 stsp static void *
6440 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6441 84451b3e 2018-07-10 stsp {
6442 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6443 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6444 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6445 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6446 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6447 00a8878e 2022-07-01 thomas
6448 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6449 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6450 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6451 9117a7b7 2022-07-01 thomas
6452 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6453 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6454 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6455 9117a7b7 2022-07-01 thomas goto done;
6456 9117a7b7 2022-07-01 thomas }
6457 18430de3 2018-07-10 stsp
6458 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6459 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6460 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6461 9117a7b7 2022-07-01 thomas goto done;
6462 9117a7b7 2022-07-01 thomas }
6463 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6464 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6465 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6466 9117a7b7 2022-07-01 thomas goto done;
6467 9117a7b7 2022-07-01 thomas }
6468 9117a7b7 2022-07-01 thomas
6469 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6470 61266923 2020-01-14 stsp if (err)
6471 9117a7b7 2022-07-01 thomas goto done;
6472 61266923 2020-01-14 stsp
6473 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6474 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6475 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6476 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6477 fc06ba56 2019-08-22 stsp err = NULL;
6478 18430de3 2018-07-10 stsp
6479 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6480 9117a7b7 2022-07-01 thomas if (errcode) {
6481 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6482 9117a7b7 2022-07-01 thomas goto done;
6483 9117a7b7 2022-07-01 thomas }
6484 18430de3 2018-07-10 stsp
6485 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6486 1d0f4054 2021-06-17 stsp if (err == NULL)
6487 1d0f4054 2021-06-17 stsp err = close_err;
6488 c9beca56 2018-07-22 stsp ta->repo = NULL;
6489 c9beca56 2018-07-22 stsp *ta->complete = 1;
6490 1134ebde 2023-04-22 thomas
6491 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6492 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6493 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6494 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6495 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6496 1134ebde 2023-04-22 thomas }
6497 18430de3 2018-07-10 stsp
6498 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6499 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6500 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6501 18430de3 2018-07-10 stsp
6502 9117a7b7 2022-07-01 thomas done:
6503 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6504 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6505 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6506 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6507 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6508 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6509 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6510 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6511 00a8878e 2022-07-01 thomas
6512 18430de3 2018-07-10 stsp return (void *)err;
6513 84451b3e 2018-07-10 stsp }
6514 84451b3e 2018-07-10 stsp
6515 245d91c1 2018-07-12 stsp static struct got_object_id *
6516 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6517 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6518 245d91c1 2018-07-12 stsp {
6519 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6520 8d0fe45a 2019-08-12 stsp
6521 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6522 8d0fe45a 2019-08-12 stsp return NULL;
6523 b880a918 2018-07-10 stsp
6524 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6525 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6526 4fc71f3b 2022-07-12 thomas return NULL;
6527 4fc71f3b 2022-07-12 thomas
6528 4fc71f3b 2022-07-12 thomas return line->id;
6529 4fc71f3b 2022-07-12 thomas }
6530 4fc71f3b 2022-07-12 thomas
6531 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6532 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6533 4fc71f3b 2022-07-12 thomas int lineno)
6534 4fc71f3b 2022-07-12 thomas {
6535 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6536 4fc71f3b 2022-07-12 thomas
6537 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6538 4fc71f3b 2022-07-12 thomas return NULL;
6539 4fc71f3b 2022-07-12 thomas
6540 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6541 245d91c1 2018-07-12 stsp if (!line->annotated)
6542 245d91c1 2018-07-12 stsp return NULL;
6543 245d91c1 2018-07-12 stsp
6544 245d91c1 2018-07-12 stsp return line->id;
6545 b880a918 2018-07-10 stsp }
6546 245d91c1 2018-07-12 stsp
6547 b880a918 2018-07-10 stsp static const struct got_error *
6548 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6549 a70480e0 2018-06-23 stsp {
6550 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6551 245d91c1 2018-07-12 stsp int i;
6552 245d91c1 2018-07-12 stsp
6553 245d91c1 2018-07-12 stsp if (blame->thread) {
6554 1a76625f 2018-10-22 stsp int errcode;
6555 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6556 1a76625f 2018-10-22 stsp if (errcode)
6557 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6558 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6559 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6560 1a76625f 2018-10-22 stsp if (errcode)
6561 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6562 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6563 1a76625f 2018-10-22 stsp if (errcode)
6564 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6565 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6566 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6567 245d91c1 2018-07-12 stsp err = NULL;
6568 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6569 245d91c1 2018-07-12 stsp }
6570 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6571 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6572 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6573 1d0f4054 2021-06-17 stsp if (err == NULL)
6574 1d0f4054 2021-06-17 stsp err = close_err;
6575 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6576 245d91c1 2018-07-12 stsp }
6577 245d91c1 2018-07-12 stsp if (blame->f) {
6578 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6579 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6580 245d91c1 2018-07-12 stsp blame->f = NULL;
6581 245d91c1 2018-07-12 stsp }
6582 57670559 2018-12-23 stsp if (blame->lines) {
6583 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6584 57670559 2018-12-23 stsp free(blame->lines[i].id);
6585 57670559 2018-12-23 stsp free(blame->lines);
6586 57670559 2018-12-23 stsp blame->lines = NULL;
6587 57670559 2018-12-23 stsp }
6588 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6589 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6590 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6591 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6592 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6593 7cd52833 2022-06-23 thomas if (err == NULL)
6594 7cd52833 2022-06-23 thomas err = pack_err;
6595 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6596 7cd52833 2022-06-23 thomas }
6597 7477a2d2 2023-08-23 thomas free(blame->line_offsets);
6598 7477a2d2 2023-08-23 thomas blame->line_offsets = NULL;
6599 245d91c1 2018-07-12 stsp return err;
6600 245d91c1 2018-07-12 stsp }
6601 245d91c1 2018-07-12 stsp
6602 245d91c1 2018-07-12 stsp static const struct got_error *
6603 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6604 fc06ba56 2019-08-22 stsp {
6605 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6606 fc06ba56 2019-08-22 stsp int *done = arg;
6607 fc06ba56 2019-08-22 stsp int errcode;
6608 fc06ba56 2019-08-22 stsp
6609 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6610 fc06ba56 2019-08-22 stsp if (errcode)
6611 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6612 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6613 fc06ba56 2019-08-22 stsp
6614 fc06ba56 2019-08-22 stsp if (*done)
6615 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6616 fc06ba56 2019-08-22 stsp
6617 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6618 fc06ba56 2019-08-22 stsp if (errcode)
6619 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6620 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6621 fc06ba56 2019-08-22 stsp
6622 fc06ba56 2019-08-22 stsp return err;
6623 fc06ba56 2019-08-22 stsp }
6624 fc06ba56 2019-08-22 stsp
6625 fc06ba56 2019-08-22 stsp static const struct got_error *
6626 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6627 245d91c1 2018-07-12 stsp {
6628 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6629 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6630 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6631 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6632 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6633 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6634 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6635 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6636 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6637 a70480e0 2018-06-23 stsp
6638 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6639 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6640 27d434c2 2018-09-15 stsp if (err)
6641 15a94983 2018-12-23 stsp return err;
6642 f4ae6ddb 2022-07-01 thomas
6643 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6644 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6645 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6646 f4ae6ddb 2022-07-01 thomas goto done;
6647 f4ae6ddb 2022-07-01 thomas }
6648 945f9229 2022-04-16 thomas
6649 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6650 945f9229 2022-04-16 thomas if (err)
6651 945f9229 2022-04-16 thomas goto done;
6652 27d434c2 2018-09-15 stsp
6653 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6654 84451b3e 2018-07-10 stsp if (err)
6655 84451b3e 2018-07-10 stsp goto done;
6656 27d434c2 2018-09-15 stsp
6657 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6658 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6659 84451b3e 2018-07-10 stsp goto done;
6660 84451b3e 2018-07-10 stsp }
6661 a70480e0 2018-06-23 stsp
6662 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6663 a70480e0 2018-06-23 stsp if (err)
6664 a70480e0 2018-06-23 stsp goto done;
6665 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6666 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6667 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6668 84451b3e 2018-07-10 stsp goto done;
6669 84451b3e 2018-07-10 stsp }
6670 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6671 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6672 1fddf795 2021-01-20 stsp if (err)
6673 1fddf795 2021-01-20 stsp goto done;
6674 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6675 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6676 84451b3e 2018-07-10 stsp goto done;
6677 1fddf795 2021-01-20 stsp }
6678 b02560ec 2019-08-19 stsp
6679 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6680 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6681 b02560ec 2019-08-19 stsp blame->nlines--;
6682 a70480e0 2018-06-23 stsp
6683 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6684 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6685 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6686 84451b3e 2018-07-10 stsp goto done;
6687 84451b3e 2018-07-10 stsp }
6688 a70480e0 2018-06-23 stsp
6689 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6690 bd24772e 2018-07-11 stsp if (err)
6691 bd24772e 2018-07-11 stsp goto done;
6692 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6693 7cd52833 2022-06-23 thomas pack_fds);
6694 7cd52833 2022-06-23 thomas if (err)
6695 7cd52833 2022-06-23 thomas goto done;
6696 bd24772e 2018-07-11 stsp
6697 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6698 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6699 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6700 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6701 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6702 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6703 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6704 245d91c1 2018-07-12 stsp goto done;
6705 245d91c1 2018-07-12 stsp }
6706 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6707 245d91c1 2018-07-12 stsp
6708 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6709 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6710 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6711 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6712 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6713 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6714 a5388363 2020-12-01 naddy s->blame_complete = 0;
6715 f5a09613 2020-12-13 naddy
6716 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6717 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6718 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6719 f5a09613 2020-12-13 naddy s->selected_line = 1;
6720 f5a09613 2020-12-13 naddy }
6721 aa61903a 2021-12-31 thomas s->matched_line = 0;
6722 245d91c1 2018-07-12 stsp
6723 245d91c1 2018-07-12 stsp done:
6724 945f9229 2022-04-16 thomas if (commit)
6725 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6726 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6727 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6728 245d91c1 2018-07-12 stsp if (blob)
6729 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6730 27d434c2 2018-09-15 stsp free(obj_id);
6731 245d91c1 2018-07-12 stsp if (err)
6732 245d91c1 2018-07-12 stsp stop_blame(blame);
6733 245d91c1 2018-07-12 stsp return err;
6734 245d91c1 2018-07-12 stsp }
6735 245d91c1 2018-07-12 stsp
6736 245d91c1 2018-07-12 stsp static const struct got_error *
6737 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6738 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6739 245d91c1 2018-07-12 stsp {
6740 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6741 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6742 dbc6a6b6 2018-07-12 stsp
6743 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6744 245d91c1 2018-07-12 stsp
6745 c4843652 2019-08-12 stsp s->path = strdup(path);
6746 c4843652 2019-08-12 stsp if (s->path == NULL)
6747 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6748 c4843652 2019-08-12 stsp
6749 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6750 c4843652 2019-08-12 stsp if (err) {
6751 c4843652 2019-08-12 stsp free(s->path);
6752 7cbe629d 2018-08-04 stsp return err;
6753 c4843652 2019-08-12 stsp }
6754 245d91c1 2018-07-12 stsp
6755 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6756 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6757 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6758 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6759 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6760 fb2756b9 2018-08-04 stsp s->repo = repo;
6761 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6762 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6763 7cbe629d 2018-08-04 stsp
6764 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6765 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6766 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6767 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6768 11b20872 2019-11-08 stsp if (err)
6769 11b20872 2019-11-08 stsp return err;
6770 11b20872 2019-11-08 stsp }
6771 11b20872 2019-11-08 stsp
6772 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6773 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6774 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6775 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6776 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6777 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6778 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6779 1134ebde 2023-04-22 thomas
6780 1134ebde 2023-04-22 thomas if (using_mock_io) {
6781 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6782 1134ebde 2023-04-22 thomas int rc;
6783 1134ebde 2023-04-22 thomas
6784 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6785 1134ebde 2023-04-22 thomas if (rc)
6786 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6787 1134ebde 2023-04-22 thomas }
6788 e5a0f69f 2018-08-18 stsp
6789 a5388363 2020-12-01 naddy return run_blame(view);
6790 7cbe629d 2018-08-04 stsp }
6791 7cbe629d 2018-08-04 stsp
6792 e5a0f69f 2018-08-18 stsp static const struct got_error *
6793 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6794 7cbe629d 2018-08-04 stsp {
6795 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6796 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6797 7cbe629d 2018-08-04 stsp
6798 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6799 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6800 e5a0f69f 2018-08-18 stsp
6801 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6802 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6803 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6804 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6805 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6806 1134ebde 2023-04-22 thomas }
6807 1134ebde 2023-04-22 thomas
6808 1134ebde 2023-04-22 thomas if (using_mock_io) {
6809 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6810 1134ebde 2023-04-22 thomas int rc;
6811 1134ebde 2023-04-22 thomas
6812 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6813 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6814 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6815 7cbe629d 2018-08-04 stsp }
6816 e5a0f69f 2018-08-18 stsp
6817 e5a0f69f 2018-08-18 stsp free(s->path);
6818 11b20872 2019-11-08 stsp free_colors(&s->colors);
6819 e5a0f69f 2018-08-18 stsp return err;
6820 7cbe629d 2018-08-04 stsp }
6821 7cbe629d 2018-08-04 stsp
6822 7cbe629d 2018-08-04 stsp static const struct got_error *
6823 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6824 6c4c42e0 2019-06-24 stsp {
6825 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6826 6c4c42e0 2019-06-24 stsp
6827 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6828 6c4c42e0 2019-06-24 stsp return NULL;
6829 2a7d3cd7 2022-09-17 thomas }
6830 2a7d3cd7 2022-09-17 thomas
6831 2a7d3cd7 2022-09-17 thomas static void
6832 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6833 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6834 2a7d3cd7 2022-09-17 thomas {
6835 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6836 2a7d3cd7 2022-09-17 thomas
6837 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6838 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6839 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6840 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6841 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6842 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6843 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6844 6c4c42e0 2019-06-24 stsp }
6845 6c4c42e0 2019-06-24 stsp
6846 6c4c42e0 2019-06-24 stsp static const struct got_error *
6847 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6848 7cbe629d 2018-08-04 stsp {
6849 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6850 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6851 2b380cc8 2018-10-24 stsp int errcode;
6852 2b380cc8 2018-10-24 stsp
6853 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6854 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6855 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6856 2b380cc8 2018-10-24 stsp if (errcode)
6857 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6858 51fe7530 2019-08-19 stsp
6859 557d3365 2023-04-14 thomas if (!using_mock_io)
6860 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6861 2b380cc8 2018-10-24 stsp }
6862 e5a0f69f 2018-08-18 stsp
6863 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6864 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6865 51fe7530 2019-08-19 stsp
6866 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6867 e5a0f69f 2018-08-18 stsp
6868 a5d43cac 2022-07-01 thomas view_border(view);
6869 e5a0f69f 2018-08-18 stsp return err;
6870 e5a0f69f 2018-08-18 stsp }
6871 e5a0f69f 2018-08-18 stsp
6872 e5a0f69f 2018-08-18 stsp static const struct got_error *
6873 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6874 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6875 eaeaa612 2022-07-20 thomas {
6876 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6877 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6878 eaeaa612 2022-07-20 thomas
6879 eaeaa612 2022-07-20 thomas *new_view = NULL;
6880 eaeaa612 2022-07-20 thomas
6881 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6882 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6883 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6884 eaeaa612 2022-07-20 thomas
6885 92845f09 2023-07-26 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6886 eaeaa612 2022-07-20 thomas if (err)
6887 eaeaa612 2022-07-20 thomas view_close(log_view);
6888 eaeaa612 2022-07-20 thomas else
6889 eaeaa612 2022-07-20 thomas *new_view = log_view;
6890 eaeaa612 2022-07-20 thomas
6891 eaeaa612 2022-07-20 thomas return err;
6892 eaeaa612 2022-07-20 thomas }
6893 eaeaa612 2022-07-20 thomas
6894 eaeaa612 2022-07-20 thomas static const struct got_error *
6895 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6896 e5a0f69f 2018-08-18 stsp {
6897 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6898 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6899 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6900 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6901 a5d43cac 2022-07-01 thomas
6902 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6903 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6904 a5d43cac 2022-07-01 thomas --eos; /* border */
6905 7cbe629d 2018-08-04 stsp
6906 e5a0f69f 2018-08-18 stsp switch (ch) {
6907 05171be4 2022-06-23 thomas case '0':
6908 05171be4 2022-06-23 thomas case '$':
6909 05171be4 2022-06-23 thomas case KEY_RIGHT:
6910 05171be4 2022-06-23 thomas case 'l':
6911 05171be4 2022-06-23 thomas case KEY_LEFT:
6912 05171be4 2022-06-23 thomas case 'h':
6913 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6914 05171be4 2022-06-23 thomas break;
6915 1e37a5c2 2019-05-12 jcs case 'q':
6916 1e37a5c2 2019-05-12 jcs s->done = 1;
6917 4deef56f 2021-09-02 naddy break;
6918 4deef56f 2021-09-02 naddy case 'g':
6919 4deef56f 2021-09-02 naddy case KEY_HOME:
6920 4deef56f 2021-09-02 naddy s->selected_line = 1;
6921 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6922 07b0611c 2022-06-23 thomas view->count = 0;
6923 4deef56f 2021-09-02 naddy break;
6924 4deef56f 2021-09-02 naddy case 'G':
6925 4deef56f 2021-09-02 naddy case KEY_END:
6926 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6927 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6928 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6929 4deef56f 2021-09-02 naddy } else {
6930 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6931 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6932 4deef56f 2021-09-02 naddy }
6933 07b0611c 2022-06-23 thomas view->count = 0;
6934 1e37a5c2 2019-05-12 jcs break;
6935 1e37a5c2 2019-05-12 jcs case 'k':
6936 1e37a5c2 2019-05-12 jcs case KEY_UP:
6937 f7140bf5 2021-10-17 thomas case CTRL('p'):
6938 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6939 1e37a5c2 2019-05-12 jcs s->selected_line--;
6940 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6941 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6942 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6943 07b0611c 2022-06-23 thomas else
6944 07b0611c 2022-06-23 thomas view->count = 0;
6945 1e37a5c2 2019-05-12 jcs break;
6946 70f17a53 2022-06-13 thomas case CTRL('u'):
6947 23427b14 2022-06-23 thomas case 'u':
6948 70f17a53 2022-06-13 thomas nscroll /= 2;
6949 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6950 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6951 ea025d1d 2020-02-22 naddy case CTRL('b'):
6952 1c5e5faa 2022-06-23 thomas case 'b':
6953 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6954 07b0611c 2022-06-23 thomas if (view->count > 1)
6955 07b0611c 2022-06-23 thomas nscroll += nscroll;
6956 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6957 07b0611c 2022-06-23 thomas view->count = 0;
6958 e5a0f69f 2018-08-18 stsp break;
6959 1e37a5c2 2019-05-12 jcs }
6960 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6961 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6962 1e37a5c2 2019-05-12 jcs else
6963 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6964 1e37a5c2 2019-05-12 jcs break;
6965 1e37a5c2 2019-05-12 jcs case 'j':
6966 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6967 f7140bf5 2021-10-17 thomas case CTRL('n'):
6968 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6969 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6970 1e37a5c2 2019-05-12 jcs s->selected_line++;
6971 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6972 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6973 07b0611c 2022-06-23 thomas else
6974 07b0611c 2022-06-23 thomas view->count = 0;
6975 1e37a5c2 2019-05-12 jcs break;
6976 1c5e5faa 2022-06-23 thomas case 'c':
6977 1e37a5c2 2019-05-12 jcs case 'p': {
6978 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6979 07b0611c 2022-06-23 thomas
6980 07b0611c 2022-06-23 thomas view->count = 0;
6981 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6982 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6983 1e37a5c2 2019-05-12 jcs if (id == NULL)
6984 e5a0f69f 2018-08-18 stsp break;
6985 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6986 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6987 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6988 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6989 1e37a5c2 2019-05-12 jcs int obj_type;
6990 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6991 1e37a5c2 2019-05-12 jcs s->repo, id);
6992 e5a0f69f 2018-08-18 stsp if (err)
6993 e5a0f69f 2018-08-18 stsp break;
6994 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6995 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6996 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6997 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6998 e5a0f69f 2018-08-18 stsp break;
6999 e5a0f69f 2018-08-18 stsp }
7000 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
7001 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
7002 ec242592 2022-04-22 thomas s->repo, &pid->id);
7003 945f9229 2022-04-16 thomas if (err)
7004 945f9229 2022-04-16 thomas break;
7005 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
7006 945f9229 2022-04-16 thomas pcommit, s->path);
7007 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
7008 e5a0f69f 2018-08-18 stsp if (err) {
7009 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
7010 1e37a5c2 2019-05-12 jcs err = NULL;
7011 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7012 e5a0f69f 2018-08-18 stsp break;
7013 e5a0f69f 2018-08-18 stsp }
7014 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
7015 1e37a5c2 2019-05-12 jcs blob_id);
7016 1e37a5c2 2019-05-12 jcs free(blob_id);
7017 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
7018 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
7019 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7020 e5a0f69f 2018-08-18 stsp break;
7021 1e37a5c2 2019-05-12 jcs }
7022 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7023 ec242592 2022-04-22 thomas &pid->id);
7024 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7025 1e37a5c2 2019-05-12 jcs } else {
7026 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
7027 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
7028 1e37a5c2 2019-05-12 jcs break;
7029 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7030 1e37a5c2 2019-05-12 jcs id);
7031 1e37a5c2 2019-05-12 jcs }
7032 1e37a5c2 2019-05-12 jcs if (err)
7033 e5a0f69f 2018-08-18 stsp break;
7034 1e37a5c2 2019-05-12 jcs s->done = 1;
7035 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7036 1e37a5c2 2019-05-12 jcs s->done = 0;
7037 1e37a5c2 2019-05-12 jcs if (thread_err)
7038 1e37a5c2 2019-05-12 jcs break;
7039 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
7040 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
7041 a5388363 2020-12-01 naddy err = run_blame(view);
7042 1e37a5c2 2019-05-12 jcs if (err)
7043 1e37a5c2 2019-05-12 jcs break;
7044 1e37a5c2 2019-05-12 jcs break;
7045 1e37a5c2 2019-05-12 jcs }
7046 1c5e5faa 2022-06-23 thomas case 'C': {
7047 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
7048 07b0611c 2022-06-23 thomas
7049 07b0611c 2022-06-23 thomas view->count = 0;
7050 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
7051 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
7052 1e37a5c2 2019-05-12 jcs break;
7053 1e37a5c2 2019-05-12 jcs s->done = 1;
7054 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7055 1e37a5c2 2019-05-12 jcs s->done = 0;
7056 1e37a5c2 2019-05-12 jcs if (thread_err)
7057 1e37a5c2 2019-05-12 jcs break;
7058 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7059 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
7060 1e37a5c2 2019-05-12 jcs s->blamed_commit =
7061 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
7062 a5388363 2020-12-01 naddy err = run_blame(view);
7063 1e37a5c2 2019-05-12 jcs if (err)
7064 1e37a5c2 2019-05-12 jcs break;
7065 1e37a5c2 2019-05-12 jcs break;
7066 1e37a5c2 2019-05-12 jcs }
7067 2a31b33b 2022-07-23 thomas case 'L':
7068 eaeaa612 2022-07-20 thomas view->count = 0;
7069 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
7070 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
7071 2a31b33b 2022-07-23 thomas if (s->id_to_log)
7072 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7073 eaeaa612 2022-07-20 thomas break;
7074 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7075 1e37a5c2 2019-05-12 jcs case '\r': {
7076 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
7077 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
7078 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
7079 07b0611c 2022-06-23 thomas
7080 07b0611c 2022-06-23 thomas view->count = 0;
7081 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7082 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
7083 1e37a5c2 2019-05-12 jcs if (id == NULL)
7084 1e37a5c2 2019-05-12 jcs break;
7085 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
7086 1e37a5c2 2019-05-12 jcs if (err)
7087 1e37a5c2 2019-05-12 jcs break;
7088 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7089 4fc71f3b 2022-07-12 thomas if (*new_view) {
7090 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
7091 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
7092 4fc71f3b 2022-07-12 thomas if (err)
7093 4fc71f3b 2022-07-12 thomas break;
7094 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
7095 4fc71f3b 2022-07-12 thomas } else {
7096 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
7097 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
7098 a5d43cac 2022-07-01 thomas
7099 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
7100 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
7101 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
7102 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
7103 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
7104 4fc71f3b 2022-07-12 thomas break;
7105 4fc71f3b 2022-07-12 thomas }
7106 15a94983 2018-12-23 stsp }
7107 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7108 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
7109 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7110 c68571e2 2023-07-17 thomas if (err)
7111 1e37a5c2 2019-05-12 jcs break;
7112 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
7113 4fc71f3b 2022-07-12 thomas s->selected_line;
7114 4fc71f3b 2022-07-12 thomas if (*new_view)
7115 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
7116 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7117 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7118 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7119 a5d43cac 2022-07-01 thomas if (err)
7120 a5d43cac 2022-07-01 thomas break;
7121 a5d43cac 2022-07-01 thomas }
7122 a5d43cac 2022-07-01 thomas
7123 e78dc838 2020-12-04 stsp view->focussed = 0;
7124 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
7125 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
7126 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
7127 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
7128 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
7129 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
7130 1e37a5c2 2019-05-12 jcs if (err)
7131 34bc9ec9 2019-02-22 stsp break;
7132 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
7133 40236d76 2022-06-23 thomas if (err)
7134 40236d76 2022-06-23 thomas break;
7135 e78dc838 2020-12-04 stsp view->focus_child = 1;
7136 1e37a5c2 2019-05-12 jcs } else
7137 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
7138 1e37a5c2 2019-05-12 jcs if (err)
7139 e5a0f69f 2018-08-18 stsp break;
7140 1e37a5c2 2019-05-12 jcs break;
7141 1e37a5c2 2019-05-12 jcs }
7142 70f17a53 2022-06-13 thomas case CTRL('d'):
7143 23427b14 2022-06-23 thomas case 'd':
7144 70f17a53 2022-06-13 thomas nscroll /= 2;
7145 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7146 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7147 ea025d1d 2020-02-22 naddy case CTRL('f'):
7148 1c5e5faa 2022-06-23 thomas case 'f':
7149 1e37a5c2 2019-05-12 jcs case ' ':
7150 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7151 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
7152 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
7153 07b0611c 2022-06-23 thomas view->count = 0;
7154 e5a0f69f 2018-08-18 stsp break;
7155 1e37a5c2 2019-05-12 jcs }
7156 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7157 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
7158 70f17a53 2022-06-13 thomas s->selected_line +=
7159 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
7160 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
7161 1e37a5c2 2019-05-12 jcs }
7162 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
7163 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
7164 1e37a5c2 2019-05-12 jcs else
7165 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
7166 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
7167 1e37a5c2 2019-05-12 jcs break;
7168 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7169 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
7170 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
7171 1e37a5c2 2019-05-12 jcs view->nlines - 2);
7172 1e37a5c2 2019-05-12 jcs }
7173 1e37a5c2 2019-05-12 jcs break;
7174 1e37a5c2 2019-05-12 jcs default:
7175 07b0611c 2022-06-23 thomas view->count = 0;
7176 1e37a5c2 2019-05-12 jcs break;
7177 a70480e0 2018-06-23 stsp }
7178 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7179 adf4c9e0 2022-07-03 thomas }
7180 adf4c9e0 2022-07-03 thomas
7181 adf4c9e0 2022-07-03 thomas static const struct got_error *
7182 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7183 adf4c9e0 2022-07-03 thomas {
7184 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7185 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7186 adf4c9e0 2022-07-03 thomas
7187 adf4c9e0 2022-07-03 thomas view->count = 0;
7188 adf4c9e0 2022-07-03 thomas s->done = 1;
7189 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7190 adf4c9e0 2022-07-03 thomas s->done = 0;
7191 adf4c9e0 2022-07-03 thomas if (err)
7192 adf4c9e0 2022-07-03 thomas return err;
7193 adf4c9e0 2022-07-03 thomas return run_blame(view);
7194 a70480e0 2018-06-23 stsp }
7195 a70480e0 2018-06-23 stsp
7196 a70480e0 2018-06-23 stsp static const struct got_error *
7197 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7198 9f7d7167 2018-04-29 stsp {
7199 1d98034b 2023-04-14 thomas const struct got_error *error;
7200 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7201 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7202 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7203 0587e10c 2020-07-23 stsp char *link_target = NULL;
7204 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7205 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7206 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *commit_id_str = NULL;
7207 a70480e0 2018-06-23 stsp int ch;
7208 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7209 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7210 a70480e0 2018-06-23 stsp
7211 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7212 a70480e0 2018-06-23 stsp switch (ch) {
7213 a70480e0 2018-06-23 stsp case 'c':
7214 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7215 a70480e0 2018-06-23 stsp break;
7216 69069811 2018-08-02 stsp case 'r':
7217 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7218 69069811 2018-08-02 stsp if (repo_path == NULL)
7219 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7220 9ba1d308 2019-10-21 stsp optarg);
7221 69069811 2018-08-02 stsp break;
7222 a70480e0 2018-06-23 stsp default:
7223 17020d27 2019-03-07 stsp usage_blame();
7224 a70480e0 2018-06-23 stsp /* NOTREACHED */
7225 a70480e0 2018-06-23 stsp }
7226 a70480e0 2018-06-23 stsp }
7227 a70480e0 2018-06-23 stsp
7228 a70480e0 2018-06-23 stsp argc -= optind;
7229 a70480e0 2018-06-23 stsp argv += optind;
7230 a70480e0 2018-06-23 stsp
7231 f135c941 2020-02-20 stsp if (argc != 1)
7232 a70480e0 2018-06-23 stsp usage_blame();
7233 6962eb72 2020-02-20 stsp
7234 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7235 7cd52833 2022-06-23 thomas if (error != NULL)
7236 7cd52833 2022-06-23 thomas goto done;
7237 7cd52833 2022-06-23 thomas
7238 69069811 2018-08-02 stsp if (repo_path == NULL) {
7239 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7240 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7241 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7242 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
7243 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7244 c156c7a4 2020-12-18 stsp goto done;
7245 f135c941 2020-02-20 stsp if (worktree)
7246 eb41ed75 2019-02-05 stsp repo_path =
7247 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7248 f135c941 2020-02-20 stsp else
7249 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7250 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7251 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7252 c156c7a4 2020-12-18 stsp goto done;
7253 c156c7a4 2020-12-18 stsp }
7254 f135c941 2020-02-20 stsp }
7255 a915003a 2019-02-05 stsp
7256 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7257 c02c541e 2019-03-29 stsp if (error != NULL)
7258 8e94dd5b 2019-01-04 stsp goto done;
7259 69069811 2018-08-02 stsp
7260 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7261 f135c941 2020-02-20 stsp worktree);
7262 c02c541e 2019-03-29 stsp if (error)
7263 92205607 2019-01-04 stsp goto done;
7264 69069811 2018-08-02 stsp
7265 557d3365 2023-04-14 thomas init_curses();
7266 f135c941 2020-02-20 stsp
7267 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7268 51a10b52 2020-12-26 stsp if (error)
7269 51a10b52 2020-12-26 stsp goto done;
7270 51a10b52 2020-12-26 stsp
7271 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7272 eb41ed75 2019-02-05 stsp if (error)
7273 69069811 2018-08-02 stsp goto done;
7274 a70480e0 2018-06-23 stsp
7275 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7276 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7277 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7278 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7279 a70480e0 2018-06-23 stsp if (error != NULL)
7280 66b4983c 2018-06-23 stsp goto done;
7281 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7282 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7283 a70480e0 2018-06-23 stsp } else {
7284 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7285 66b04f8f 2023-07-19 thomas repo, worktree);
7286 66b04f8f 2023-07-19 thomas if (error != NULL)
7287 66b04f8f 2023-07-19 thomas goto done;
7288 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
7289 66b04f8f 2023-07-19 thomas commit_id_str = keyword_idstr;
7290 66b04f8f 2023-07-19 thomas
7291 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7292 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7293 a70480e0 2018-06-23 stsp }
7294 a19e88aa 2018-06-23 stsp if (error != NULL)
7295 e1cd8fed 2018-08-01 stsp goto done;
7296 0587e10c 2020-07-23 stsp
7297 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7298 945f9229 2022-04-16 thomas if (error)
7299 945f9229 2022-04-16 thomas goto done;
7300 945f9229 2022-04-16 thomas
7301 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7302 945f9229 2022-04-16 thomas commit, repo);
7303 7cbe629d 2018-08-04 stsp if (error)
7304 7cbe629d 2018-08-04 stsp goto done;
7305 0587e10c 2020-07-23 stsp
7306 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7307 4334634c 2023-04-22 thomas if (view == NULL) {
7308 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7309 4334634c 2023-04-22 thomas goto done;
7310 4334634c 2023-04-22 thomas }
7311 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7312 78756c87 2020-11-24 stsp commit_id, repo);
7313 4334634c 2023-04-22 thomas if (error != NULL) {
7314 4334634c 2023-04-22 thomas if (view->close == NULL)
7315 4334634c 2023-04-22 thomas close_blame_view(view);
7316 4334634c 2023-04-22 thomas view_close(view);
7317 0587e10c 2020-07-23 stsp goto done;
7318 4334634c 2023-04-22 thomas }
7319 349dfd1e 2023-07-23 thomas
7320 12314ad4 2019-08-31 stsp if (worktree) {
7321 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
7322 349dfd1e 2023-07-23 thomas if (error != NULL)
7323 349dfd1e 2023-07-23 thomas goto done;
7324 349dfd1e 2023-07-23 thomas
7325 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7326 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7327 12314ad4 2019-08-31 stsp worktree = NULL;
7328 12314ad4 2019-08-31 stsp }
7329 349dfd1e 2023-07-23 thomas
7330 557d3365 2023-04-14 thomas error = view_loop(view);
7331 349dfd1e 2023-07-23 thomas
7332 a70480e0 2018-06-23 stsp done:
7333 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
7334 69069811 2018-08-02 stsp free(repo_path);
7335 f135c941 2020-02-20 stsp free(in_repo_path);
7336 0587e10c 2020-07-23 stsp free(link_target);
7337 69069811 2018-08-02 stsp free(cwd);
7338 a70480e0 2018-06-23 stsp free(commit_id);
7339 66b04f8f 2023-07-19 thomas free(keyword_idstr);
7340 945f9229 2022-04-16 thomas if (commit)
7341 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7342 eb41ed75 2019-02-05 stsp if (worktree)
7343 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7344 1d0f4054 2021-06-17 stsp if (repo) {
7345 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7346 1d0f4054 2021-06-17 stsp if (error == NULL)
7347 1d0f4054 2021-06-17 stsp error = close_err;
7348 1d0f4054 2021-06-17 stsp }
7349 7cd52833 2022-06-23 thomas if (pack_fds) {
7350 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7351 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7352 7cd52833 2022-06-23 thomas if (error == NULL)
7353 7cd52833 2022-06-23 thomas error = pack_err;
7354 7cd52833 2022-06-23 thomas }
7355 51a10b52 2020-12-26 stsp tog_free_refs();
7356 a70480e0 2018-06-23 stsp return error;
7357 ffd1d5e5 2018-06-23 stsp }
7358 ffd1d5e5 2018-06-23 stsp
7359 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7360 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7361 ffd1d5e5 2018-06-23 stsp {
7362 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7363 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7364 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7365 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7366 86f4aab9 2022-09-09 thomas char *index = NULL;
7367 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7368 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7369 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7370 ffd1d5e5 2018-06-23 stsp
7371 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7372 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7373 a5d43cac 2022-07-01 thomas --limit; /* border */
7374 ffd1d5e5 2018-06-23 stsp
7375 f7d12f7e 2018-08-01 stsp werase(view->window);
7376 ffd1d5e5 2018-06-23 stsp
7377 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7378 ffd1d5e5 2018-06-23 stsp return NULL;
7379 ffd1d5e5 2018-06-23 stsp
7380 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7381 f91a2b48 2022-06-23 thomas 0, 0);
7382 ffd1d5e5 2018-06-23 stsp if (err)
7383 ffd1d5e5 2018-06-23 stsp return err;
7384 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7385 a3404814 2018-09-02 stsp wstandout(view->window);
7386 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7387 11b20872 2019-11-08 stsp if (tc)
7388 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7389 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7390 86f4aab9 2022-09-09 thomas free(wline);
7391 86f4aab9 2022-09-09 thomas wline = NULL;
7392 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7393 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7394 11b20872 2019-11-08 stsp if (tc)
7395 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7396 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7397 a3404814 2018-09-02 stsp wstandend(view->window);
7398 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7399 86f4aab9 2022-09-09 thomas return NULL;
7400 07dd3ed3 2022-08-06 thomas
7401 6f5f393a 2022-08-12 thomas i += s->selected;
7402 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7403 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7404 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7405 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7406 07dd3ed3 2022-08-06 thomas }
7407 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7408 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7409 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7410 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7411 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7412 86f4aab9 2022-09-09 thomas free(index);
7413 ce52c690 2018-06-23 stsp if (err)
7414 ce52c690 2018-06-23 stsp return err;
7415 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7416 2550e4c3 2018-07-13 stsp free(wline);
7417 2550e4c3 2018-07-13 stsp wline = NULL;
7418 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7419 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7420 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7421 ffd1d5e5 2018-06-23 stsp return NULL;
7422 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7423 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7424 a1eca9bb 2018-06-23 stsp return NULL;
7425 ffd1d5e5 2018-06-23 stsp
7426 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7427 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7428 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7429 0cf4efb1 2018-09-29 stsp if (view->focussed)
7430 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7431 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7432 ffd1d5e5 2018-06-23 stsp }
7433 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7434 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7435 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7436 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7437 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7438 ffd1d5e5 2018-06-23 stsp return NULL;
7439 ffd1d5e5 2018-06-23 stsp n = 1;
7440 ffd1d5e5 2018-06-23 stsp } else {
7441 ffd1d5e5 2018-06-23 stsp n = 0;
7442 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7443 ffd1d5e5 2018-06-23 stsp }
7444 ffd1d5e5 2018-06-23 stsp
7445 c72de8ab 2023-02-03 thomas view->maxx = 0;
7446 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7447 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7448 848d6979 2019-08-12 stsp const char *modestr = "";
7449 56e0773d 2019-11-28 stsp mode_t mode;
7450 1d13200f 2018-07-12 stsp
7451 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7452 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7453 56e0773d 2019-11-28 stsp
7454 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7455 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7456 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7457 1d13200f 2018-07-12 stsp if (err)
7458 638f9024 2019-05-13 stsp return got_error_from_errno(
7459 230a42bd 2019-05-11 jcs "got_object_id_str");
7460 1d13200f 2018-07-12 stsp }
7461 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7462 63c5ca5d 2019-08-24 stsp modestr = "$";
7463 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7464 0d6c6ee3 2020-05-20 stsp int i;
7465 0d6c6ee3 2020-05-20 stsp
7466 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7467 d86d3b18 2020-12-01 naddy te, s->repo);
7468 0d6c6ee3 2020-05-20 stsp if (err) {
7469 0d6c6ee3 2020-05-20 stsp free(id_str);
7470 0d6c6ee3 2020-05-20 stsp return err;
7471 0d6c6ee3 2020-05-20 stsp }
7472 cbb35fac 2023-06-25 thomas for (i = 0; link_target[i] != '\0'; i++) {
7473 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7474 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7475 0d6c6ee3 2020-05-20 stsp }
7476 848d6979 2019-08-12 stsp modestr = "@";
7477 0d6c6ee3 2020-05-20 stsp }
7478 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7479 848d6979 2019-08-12 stsp modestr = "/";
7480 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7481 848d6979 2019-08-12 stsp modestr = "*";
7482 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7483 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7484 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7485 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7486 1d13200f 2018-07-12 stsp free(id_str);
7487 0d6c6ee3 2020-05-20 stsp free(link_target);
7488 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7489 1d13200f 2018-07-12 stsp }
7490 1d13200f 2018-07-12 stsp free(id_str);
7491 0d6c6ee3 2020-05-20 stsp free(link_target);
7492 c72de8ab 2023-02-03 thomas
7493 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7494 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7495 ffd1d5e5 2018-06-23 stsp if (err) {
7496 ffd1d5e5 2018-06-23 stsp free(line);
7497 ffd1d5e5 2018-06-23 stsp break;
7498 ffd1d5e5 2018-06-23 stsp }
7499 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7500 c72de8ab 2023-02-03 thomas free(wline);
7501 c72de8ab 2023-02-03 thomas wline = NULL;
7502 c72de8ab 2023-02-03 thomas
7503 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7504 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7505 c72de8ab 2023-02-03 thomas if (err) {
7506 c72de8ab 2023-02-03 thomas free(line);
7507 c72de8ab 2023-02-03 thomas break;
7508 c72de8ab 2023-02-03 thomas }
7509 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7510 0cf4efb1 2018-09-29 stsp if (view->focussed)
7511 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7512 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7513 ffd1d5e5 2018-06-23 stsp }
7514 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7515 f26dddb7 2019-11-08 stsp if (tc)
7516 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7517 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7518 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7519 f26dddb7 2019-11-08 stsp if (tc)
7520 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7521 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7522 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7523 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7524 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7525 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7526 ffd1d5e5 2018-06-23 stsp free(line);
7527 2550e4c3 2018-07-13 stsp free(wline);
7528 2550e4c3 2018-07-13 stsp wline = NULL;
7529 ffd1d5e5 2018-06-23 stsp n++;
7530 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7531 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7532 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7533 ffd1d5e5 2018-06-23 stsp break;
7534 ffd1d5e5 2018-06-23 stsp }
7535 ffd1d5e5 2018-06-23 stsp
7536 ffd1d5e5 2018-06-23 stsp return err;
7537 ffd1d5e5 2018-06-23 stsp }
7538 ffd1d5e5 2018-06-23 stsp
7539 ffd1d5e5 2018-06-23 stsp static void
7540 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7541 ffd1d5e5 2018-06-23 stsp {
7542 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7543 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7544 fa86c4bf 2020-11-29 stsp int i = 0;
7545 ffd1d5e5 2018-06-23 stsp
7546 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7547 ffd1d5e5 2018-06-23 stsp return;
7548 ffd1d5e5 2018-06-23 stsp
7549 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7550 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7551 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7552 fa86c4bf 2020-11-29 stsp if (!isroot)
7553 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7554 fa86c4bf 2020-11-29 stsp break;
7555 fa86c4bf 2020-11-29 stsp }
7556 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7557 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7558 ffd1d5e5 2018-06-23 stsp }
7559 ffd1d5e5 2018-06-23 stsp }
7560 ffd1d5e5 2018-06-23 stsp
7561 a5d43cac 2022-07-01 thomas static const struct got_error *
7562 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7563 ffd1d5e5 2018-06-23 stsp {
7564 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7565 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7566 ffd1d5e5 2018-06-23 stsp int n = 0;
7567 ffd1d5e5 2018-06-23 stsp
7568 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7569 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7570 694d3271 2020-12-01 naddy s->first_displayed_entry);
7571 694d3271 2020-12-01 naddy else
7572 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7573 56e0773d 2019-11-28 stsp
7574 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7575 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7576 07dd3ed3 2022-08-06 thomas if (last) {
7577 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7578 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7579 07dd3ed3 2022-08-06 thomas }
7580 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7581 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7582 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7583 768394f3 2019-01-24 stsp }
7584 ffd1d5e5 2018-06-23 stsp }
7585 a5d43cac 2022-07-01 thomas
7586 a5d43cac 2022-07-01 thomas return NULL;
7587 ffd1d5e5 2018-06-23 stsp }
7588 ffd1d5e5 2018-06-23 stsp
7589 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7590 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7591 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7592 ffd1d5e5 2018-06-23 stsp {
7593 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7594 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7595 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7596 ffd1d5e5 2018-06-23 stsp
7597 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7598 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7599 56e0773d 2019-11-28 stsp + 1 /* slash */;
7600 ce52c690 2018-06-23 stsp if (te)
7601 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7602 ce52c690 2018-06-23 stsp
7603 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7604 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7605 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7606 ffd1d5e5 2018-06-23 stsp
7607 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7608 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7609 d9765a41 2018-06-23 stsp while (pt) {
7610 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7611 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7612 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7613 cb2ebc8a 2018-06-23 stsp goto done;
7614 cb2ebc8a 2018-06-23 stsp }
7615 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7616 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7617 cb2ebc8a 2018-06-23 stsp goto done;
7618 cb2ebc8a 2018-06-23 stsp }
7619 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7620 ffd1d5e5 2018-06-23 stsp }
7621 ce52c690 2018-06-23 stsp if (te) {
7622 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7623 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7624 ce52c690 2018-06-23 stsp goto done;
7625 ce52c690 2018-06-23 stsp }
7626 cb2ebc8a 2018-06-23 stsp }
7627 ce52c690 2018-06-23 stsp done:
7628 ce52c690 2018-06-23 stsp if (err) {
7629 ce52c690 2018-06-23 stsp free(*path);
7630 ce52c690 2018-06-23 stsp *path = NULL;
7631 ce52c690 2018-06-23 stsp }
7632 ce52c690 2018-06-23 stsp return err;
7633 ce52c690 2018-06-23 stsp }
7634 ce52c690 2018-06-23 stsp
7635 ce52c690 2018-06-23 stsp static const struct got_error *
7636 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7637 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7638 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7639 ce52c690 2018-06-23 stsp {
7640 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7641 ce52c690 2018-06-23 stsp char *path;
7642 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7643 a0de39f3 2019-08-09 stsp
7644 a0de39f3 2019-08-09 stsp *new_view = NULL;
7645 69efd4c4 2018-07-18 stsp
7646 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7647 ce52c690 2018-06-23 stsp if (err)
7648 ce52c690 2018-06-23 stsp return err;
7649 ffd1d5e5 2018-06-23 stsp
7650 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7651 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7652 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7653 83ce39e3 2019-08-12 stsp goto done;
7654 83ce39e3 2019-08-12 stsp }
7655 cdf1ee82 2018-08-01 stsp
7656 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7657 e5a0f69f 2018-08-18 stsp if (err) {
7658 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7659 fc06ba56 2019-08-22 stsp err = NULL;
7660 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7661 e5a0f69f 2018-08-18 stsp } else
7662 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7663 83ce39e3 2019-08-12 stsp done:
7664 83ce39e3 2019-08-12 stsp free(path);
7665 69efd4c4 2018-07-18 stsp return err;
7666 69efd4c4 2018-07-18 stsp }
7667 69efd4c4 2018-07-18 stsp
7668 69efd4c4 2018-07-18 stsp static const struct got_error *
7669 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7670 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7671 69efd4c4 2018-07-18 stsp {
7672 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7673 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7674 69efd4c4 2018-07-18 stsp char *path;
7675 a0de39f3 2019-08-09 stsp
7676 a0de39f3 2019-08-09 stsp *new_view = NULL;
7677 69efd4c4 2018-07-18 stsp
7678 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7679 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7680 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7681 e5a0f69f 2018-08-18 stsp
7682 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7683 69efd4c4 2018-07-18 stsp if (err)
7684 69efd4c4 2018-07-18 stsp return err;
7685 69efd4c4 2018-07-18 stsp
7686 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7687 92845f09 2023-07-26 thomas path, 0, NULL);
7688 ba4f502b 2018-08-04 stsp if (err)
7689 e5a0f69f 2018-08-18 stsp view_close(log_view);
7690 e5a0f69f 2018-08-18 stsp else
7691 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7692 cb2ebc8a 2018-06-23 stsp free(path);
7693 cb2ebc8a 2018-06-23 stsp return err;
7694 ffd1d5e5 2018-06-23 stsp }
7695 ffd1d5e5 2018-06-23 stsp
7696 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7697 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7698 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7699 ffd1d5e5 2018-06-23 stsp {
7700 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7701 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7702 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7703 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7704 ffd1d5e5 2018-06-23 stsp
7705 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7706 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7707 bc573f3b 2021-07-10 stsp
7708 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7709 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7710 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7711 51b7e1c3 2023-04-14 thomas goto done;
7712 51b7e1c3 2023-04-14 thomas }
7713 ffd1d5e5 2018-06-23 stsp
7714 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7715 bc573f3b 2021-07-10 stsp if (err)
7716 bc573f3b 2021-07-10 stsp goto done;
7717 bc573f3b 2021-07-10 stsp
7718 bc573f3b 2021-07-10 stsp /*
7719 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7720 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7721 bc573f3b 2021-07-10 stsp * closed on demand.
7722 bc573f3b 2021-07-10 stsp */
7723 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7724 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7725 bc573f3b 2021-07-10 stsp if (err)
7726 bc573f3b 2021-07-10 stsp goto done;
7727 bc573f3b 2021-07-10 stsp s->tree = s->root;
7728 bc573f3b 2021-07-10 stsp
7729 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7730 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7731 ffd1d5e5 2018-06-23 stsp goto done;
7732 ffd1d5e5 2018-06-23 stsp
7733 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7734 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7735 ffd1d5e5 2018-06-23 stsp goto done;
7736 ffd1d5e5 2018-06-23 stsp }
7737 ffd1d5e5 2018-06-23 stsp
7738 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7739 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7740 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7741 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7742 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7743 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7744 9cd7cbd1 2020-12-07 stsp goto done;
7745 9cd7cbd1 2020-12-07 stsp }
7746 9cd7cbd1 2020-12-07 stsp }
7747 fb2756b9 2018-08-04 stsp s->repo = repo;
7748 c0b01bdb 2019-11-08 stsp
7749 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7750 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7751 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7752 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7753 c0b01bdb 2019-11-08 stsp if (err)
7754 c0b01bdb 2019-11-08 stsp goto done;
7755 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7756 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7757 bc573f3b 2021-07-10 stsp if (err)
7758 c0b01bdb 2019-11-08 stsp goto done;
7759 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7760 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7761 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7762 bc573f3b 2021-07-10 stsp if (err)
7763 c0b01bdb 2019-11-08 stsp goto done;
7764 e5a0f69f 2018-08-18 stsp
7765 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7766 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7767 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7768 bc573f3b 2021-07-10 stsp if (err)
7769 11b20872 2019-11-08 stsp goto done;
7770 11b20872 2019-11-08 stsp
7771 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7772 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7773 bc573f3b 2021-07-10 stsp if (err)
7774 c0b01bdb 2019-11-08 stsp goto done;
7775 c0b01bdb 2019-11-08 stsp }
7776 c0b01bdb 2019-11-08 stsp
7777 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7778 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7779 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7780 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7781 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7782 ad80ab7b 2018-08-04 stsp done:
7783 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7784 bc573f3b 2021-07-10 stsp if (commit)
7785 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7786 51b7e1c3 2023-04-14 thomas if (err) {
7787 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7788 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7789 51b7e1c3 2023-04-14 thomas view_close(view);
7790 51b7e1c3 2023-04-14 thomas }
7791 ad80ab7b 2018-08-04 stsp return err;
7792 ad80ab7b 2018-08-04 stsp }
7793 ad80ab7b 2018-08-04 stsp
7794 e5a0f69f 2018-08-18 stsp static const struct got_error *
7795 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7796 ad80ab7b 2018-08-04 stsp {
7797 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7798 ad80ab7b 2018-08-04 stsp
7799 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7800 fb2756b9 2018-08-04 stsp free(s->tree_label);
7801 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7802 6484ec90 2018-09-29 stsp free(s->commit_id);
7803 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7804 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7805 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7806 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7807 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7808 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7809 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7810 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7811 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7812 ad80ab7b 2018-08-04 stsp free(parent);
7813 ad80ab7b 2018-08-04 stsp
7814 ad80ab7b 2018-08-04 stsp }
7815 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7816 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7817 bc573f3b 2021-07-10 stsp if (s->root)
7818 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7819 7c32bd05 2019-06-22 stsp return NULL;
7820 7c32bd05 2019-06-22 stsp }
7821 7c32bd05 2019-06-22 stsp
7822 7c32bd05 2019-06-22 stsp static const struct got_error *
7823 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7824 7c32bd05 2019-06-22 stsp {
7825 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7826 7c32bd05 2019-06-22 stsp
7827 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7828 7c32bd05 2019-06-22 stsp return NULL;
7829 7c32bd05 2019-06-22 stsp }
7830 7c32bd05 2019-06-22 stsp
7831 7c32bd05 2019-06-22 stsp static int
7832 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7833 7c32bd05 2019-06-22 stsp {
7834 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7835 7c32bd05 2019-06-22 stsp
7836 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7837 56e0773d 2019-11-28 stsp 0) == 0;
7838 7c32bd05 2019-06-22 stsp }
7839 7c32bd05 2019-06-22 stsp
7840 7c32bd05 2019-06-22 stsp static const struct got_error *
7841 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7842 7c32bd05 2019-06-22 stsp {
7843 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7844 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7845 7c32bd05 2019-06-22 stsp
7846 7c32bd05 2019-06-22 stsp if (!view->searching) {
7847 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7848 7c32bd05 2019-06-22 stsp return NULL;
7849 7c32bd05 2019-06-22 stsp }
7850 7c32bd05 2019-06-22 stsp
7851 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7852 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7853 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7854 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7855 56e0773d 2019-11-28 stsp s->selected_entry);
7856 7c32bd05 2019-06-22 stsp else
7857 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7858 56e0773d 2019-11-28 stsp } else {
7859 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7860 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7861 56e0773d 2019-11-28 stsp else
7862 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7863 56e0773d 2019-11-28 stsp s->selected_entry);
7864 7c32bd05 2019-06-22 stsp }
7865 7c32bd05 2019-06-22 stsp } else {
7866 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7867 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7868 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7869 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7870 56e0773d 2019-11-28 stsp else
7871 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7872 7c32bd05 2019-06-22 stsp }
7873 7c32bd05 2019-06-22 stsp
7874 7c32bd05 2019-06-22 stsp while (1) {
7875 56e0773d 2019-11-28 stsp if (te == NULL) {
7876 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7877 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7878 ac66afb8 2019-06-24 stsp return NULL;
7879 ac66afb8 2019-06-24 stsp }
7880 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7881 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7882 56e0773d 2019-11-28 stsp else
7883 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7884 7c32bd05 2019-06-22 stsp }
7885 7c32bd05 2019-06-22 stsp
7886 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7887 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7888 56e0773d 2019-11-28 stsp s->matched_entry = te;
7889 7c32bd05 2019-06-22 stsp break;
7890 7c32bd05 2019-06-22 stsp }
7891 7c32bd05 2019-06-22 stsp
7892 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7893 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7894 56e0773d 2019-11-28 stsp else
7895 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7896 7c32bd05 2019-06-22 stsp }
7897 e5a0f69f 2018-08-18 stsp
7898 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7899 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7900 7c32bd05 2019-06-22 stsp s->selected = 0;
7901 7c32bd05 2019-06-22 stsp }
7902 7c32bd05 2019-06-22 stsp
7903 e5a0f69f 2018-08-18 stsp return NULL;
7904 ad80ab7b 2018-08-04 stsp }
7905 ad80ab7b 2018-08-04 stsp
7906 ad80ab7b 2018-08-04 stsp static const struct got_error *
7907 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7908 ad80ab7b 2018-08-04 stsp {
7909 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7910 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7911 e5a0f69f 2018-08-18 stsp char *parent_path;
7912 ad80ab7b 2018-08-04 stsp
7913 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7914 e5a0f69f 2018-08-18 stsp if (err)
7915 e5a0f69f 2018-08-18 stsp return err;
7916 ffd1d5e5 2018-06-23 stsp
7917 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7918 e5a0f69f 2018-08-18 stsp free(parent_path);
7919 669b5ffa 2018-10-07 stsp
7920 a5d43cac 2022-07-01 thomas view_border(view);
7921 e5a0f69f 2018-08-18 stsp return err;
7922 07dd3ed3 2022-08-06 thomas }
7923 07dd3ed3 2022-08-06 thomas
7924 07dd3ed3 2022-08-06 thomas static const struct got_error *
7925 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7926 07dd3ed3 2022-08-06 thomas {
7927 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7928 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7929 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7930 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7931 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7932 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7933 07dd3ed3 2022-08-06 thomas
7934 07dd3ed3 2022-08-06 thomas g = view->gline;
7935 07dd3ed3 2022-08-06 thomas view->gline = 0;
7936 07dd3ed3 2022-08-06 thomas
7937 07dd3ed3 2022-08-06 thomas if (g == 0)
7938 07dd3ed3 2022-08-06 thomas g = 1;
7939 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7940 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7941 07dd3ed3 2022-08-06 thomas
7942 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7943 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7944 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7945 07dd3ed3 2022-08-06 thomas
7946 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7947 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7948 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7949 07dd3ed3 2022-08-06 thomas }
7950 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7951 07dd3ed3 2022-08-06 thomas last += off;
7952 07dd3ed3 2022-08-06 thomas
7953 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7954 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7955 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7956 07dd3ed3 2022-08-06 thomas }
7957 07dd3ed3 2022-08-06 thomas
7958 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7959 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7960 07dd3ed3 2022-08-06 thomas i += off;
7961 07dd3ed3 2022-08-06 thomas }
7962 07dd3ed3 2022-08-06 thomas
7963 07dd3ed3 2022-08-06 thomas if (i < g) {
7964 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7965 07dd3ed3 2022-08-06 thomas if (err)
7966 07dd3ed3 2022-08-06 thomas return err;
7967 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7968 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7969 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7970 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7971 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7972 07dd3ed3 2022-08-06 thomas first += off;
7973 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7974 07dd3ed3 2022-08-06 thomas }
7975 07dd3ed3 2022-08-06 thomas } else if (i > g)
7976 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7977 07dd3ed3 2022-08-06 thomas
7978 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7979 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7980 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7981 07dd3ed3 2022-08-06 thomas
7982 07dd3ed3 2022-08-06 thomas return NULL;
7983 e5a0f69f 2018-08-18 stsp }
7984 ce52c690 2018-06-23 stsp
7985 e5a0f69f 2018-08-18 stsp static const struct got_error *
7986 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7987 e5a0f69f 2018-08-18 stsp {
7988 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7989 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7990 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7991 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7992 ffd1d5e5 2018-06-23 stsp
7993 07dd3ed3 2022-08-06 thomas if (view->gline)
7994 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7995 07dd3ed3 2022-08-06 thomas
7996 e5a0f69f 2018-08-18 stsp switch (ch) {
7997 c72de8ab 2023-02-03 thomas case '0':
7998 c72de8ab 2023-02-03 thomas case '$':
7999 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
8000 c72de8ab 2023-02-03 thomas case 'l':
8001 c72de8ab 2023-02-03 thomas case KEY_LEFT:
8002 c72de8ab 2023-02-03 thomas case 'h':
8003 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8004 c72de8ab 2023-02-03 thomas break;
8005 1e37a5c2 2019-05-12 jcs case 'i':
8006 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
8007 07b0611c 2022-06-23 thomas view->count = 0;
8008 1e37a5c2 2019-05-12 jcs break;
8009 1be4947a 2022-07-22 thomas case 'L':
8010 07b0611c 2022-06-23 thomas view->count = 0;
8011 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
8012 ffd1d5e5 2018-06-23 stsp break;
8013 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8014 152c1c93 2020-11-29 stsp break;
8015 1be4947a 2022-07-22 thomas case 'R':
8016 07b0611c 2022-06-23 thomas view->count = 0;
8017 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
8018 e4526bf5 2021-09-03 naddy break;
8019 e4526bf5 2021-09-03 naddy case 'g':
8020 aa7a1117 2023-01-09 thomas case '=':
8021 e4526bf5 2021-09-03 naddy case KEY_HOME:
8022 e4526bf5 2021-09-03 naddy s->selected = 0;
8023 07b0611c 2022-06-23 thomas view->count = 0;
8024 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
8025 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
8026 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
8027 e4526bf5 2021-09-03 naddy else
8028 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8029 1e37a5c2 2019-05-12 jcs break;
8030 e4526bf5 2021-09-03 naddy case 'G':
8031 aa7a1117 2023-01-09 thomas case '*':
8032 a5d43cac 2022-07-01 thomas case KEY_END: {
8033 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
8034 a5d43cac 2022-07-01 thomas
8035 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8036 a5d43cac 2022-07-01 thomas --eos; /* border */
8037 e4526bf5 2021-09-03 naddy s->selected = 0;
8038 07b0611c 2022-06-23 thomas view->count = 0;
8039 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
8040 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8041 e4526bf5 2021-09-03 naddy if (te == NULL) {
8042 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
8043 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8044 e4526bf5 2021-09-03 naddy n++;
8045 e4526bf5 2021-09-03 naddy }
8046 e4526bf5 2021-09-03 naddy break;
8047 e4526bf5 2021-09-03 naddy }
8048 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
8049 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
8050 e4526bf5 2021-09-03 naddy }
8051 e4526bf5 2021-09-03 naddy if (n > 0)
8052 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8053 e4526bf5 2021-09-03 naddy break;
8054 a5d43cac 2022-07-01 thomas }
8055 1e37a5c2 2019-05-12 jcs case 'k':
8056 1e37a5c2 2019-05-12 jcs case KEY_UP:
8057 f7140bf5 2021-10-17 thomas case CTRL('p'):
8058 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
8059 1e37a5c2 2019-05-12 jcs s->selected--;
8060 fa86c4bf 2020-11-29 stsp break;
8061 1e37a5c2 2019-05-12 jcs }
8062 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
8063 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8064 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8065 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8066 07b0611c 2022-06-23 thomas view->count = 0;
8067 1e37a5c2 2019-05-12 jcs break;
8068 70f17a53 2022-06-13 thomas case CTRL('u'):
8069 23427b14 2022-06-23 thomas case 'u':
8070 70f17a53 2022-06-13 thomas nscroll /= 2;
8071 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8072 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
8073 ea025d1d 2020-02-22 naddy case CTRL('b'):
8074 1c5e5faa 2022-06-23 thomas case 'b':
8075 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
8076 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
8077 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
8078 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8079 fa86c4bf 2020-11-29 stsp } else {
8080 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
8081 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8082 fa86c4bf 2020-11-29 stsp }
8083 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
8084 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8085 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8086 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8087 07b0611c 2022-06-23 thomas view->count = 0;
8088 1e37a5c2 2019-05-12 jcs break;
8089 1e37a5c2 2019-05-12 jcs case 'j':
8090 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
8091 f7140bf5 2021-10-17 thomas case CTRL('n'):
8092 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
8093 1e37a5c2 2019-05-12 jcs s->selected++;
8094 1e37a5c2 2019-05-12 jcs break;
8095 1e37a5c2 2019-05-12 jcs }
8096 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8097 07b0611c 2022-06-23 thomas == NULL) {
8098 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
8099 07b0611c 2022-06-23 thomas view->count = 0;
8100 1e37a5c2 2019-05-12 jcs break;
8101 07b0611c 2022-06-23 thomas }
8102 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
8103 1e37a5c2 2019-05-12 jcs break;
8104 70f17a53 2022-06-13 thomas case CTRL('d'):
8105 23427b14 2022-06-23 thomas case 'd':
8106 70f17a53 2022-06-13 thomas nscroll /= 2;
8107 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8108 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
8109 ea025d1d 2020-02-22 naddy case CTRL('f'):
8110 1c5e5faa 2022-06-23 thomas case 'f':
8111 4c2d69cb 2022-06-23 thomas case ' ':
8112 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8113 1e37a5c2 2019-05-12 jcs == NULL) {
8114 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
8115 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
8116 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8117 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8118 07b0611c 2022-06-23 thomas else
8119 07b0611c 2022-06-23 thomas view->count = 0;
8120 1e37a5c2 2019-05-12 jcs break;
8121 1e37a5c2 2019-05-12 jcs }
8122 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
8123 1e37a5c2 2019-05-12 jcs break;
8124 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
8125 1e37a5c2 2019-05-12 jcs case '\r':
8126 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
8127 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8128 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
8129 1e37a5c2 2019-05-12 jcs /* user selected '..' */
8130 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
8131 07b0611c 2022-06-23 thomas view->count = 0;
8132 1e37a5c2 2019-05-12 jcs break;
8133 07b0611c 2022-06-23 thomas }
8134 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
8135 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
8136 1e37a5c2 2019-05-12 jcs entry);
8137 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
8138 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
8139 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
8140 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
8141 1e37a5c2 2019-05-12 jcs s->selected_entry =
8142 1e37a5c2 2019-05-12 jcs parent->selected_entry;
8143 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
8144 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
8145 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
8146 a5d43cac 2022-07-01 thomas if (err)
8147 a5d43cac 2022-07-01 thomas break;
8148 a5d43cac 2022-07-01 thomas }
8149 1e37a5c2 2019-05-12 jcs free(parent);
8150 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
8151 56e0773d 2019-11-28 stsp s->selected_entry))) {
8152 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
8153 07b0611c 2022-06-23 thomas view->count = 0;
8154 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
8155 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
8156 1e37a5c2 2019-05-12 jcs if (err)
8157 1e37a5c2 2019-05-12 jcs break;
8158 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
8159 941e9f74 2019-05-21 stsp if (err) {
8160 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
8161 1e37a5c2 2019-05-12 jcs break;
8162 1e37a5c2 2019-05-12 jcs }
8163 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8164 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8165 1e37a5c2 2019-05-12 jcs break;
8166 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
8167 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8168 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
8169 07b0611c 2022-06-23 thomas view->count = 0;
8170 1e37a5c2 2019-05-12 jcs break;
8171 1e37a5c2 2019-05-12 jcs default:
8172 07b0611c 2022-06-23 thomas view->count = 0;
8173 1e37a5c2 2019-05-12 jcs break;
8174 ffd1d5e5 2018-06-23 stsp }
8175 e5a0f69f 2018-08-18 stsp
8176 ffd1d5e5 2018-06-23 stsp return err;
8177 9f7d7167 2018-04-29 stsp }
8178 9f7d7167 2018-04-29 stsp
8179 ffd1d5e5 2018-06-23 stsp __dead static void
8180 ffd1d5e5 2018-06-23 stsp usage_tree(void)
8181 ffd1d5e5 2018-06-23 stsp {
8182 ffd1d5e5 2018-06-23 stsp endwin();
8183 91198554 2022-06-23 thomas fprintf(stderr,
8184 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8185 ffd1d5e5 2018-06-23 stsp getprogname());
8186 ffd1d5e5 2018-06-23 stsp exit(1);
8187 ffd1d5e5 2018-06-23 stsp }
8188 ffd1d5e5 2018-06-23 stsp
8189 ffd1d5e5 2018-06-23 stsp static const struct got_error *
8190 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
8191 ffd1d5e5 2018-06-23 stsp {
8192 1d98034b 2023-04-14 thomas const struct got_error *error;
8193 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8194 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8195 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8196 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8197 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8198 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8199 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *label = NULL;
8200 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8201 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8202 ffd1d5e5 2018-06-23 stsp int ch;
8203 5221c383 2018-08-01 stsp struct tog_view *view;
8204 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8205 70ac5f84 2019-03-28 stsp
8206 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8207 ffd1d5e5 2018-06-23 stsp switch (ch) {
8208 ffd1d5e5 2018-06-23 stsp case 'c':
8209 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8210 74283ab8 2020-02-07 stsp break;
8211 74283ab8 2020-02-07 stsp case 'r':
8212 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8213 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8214 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8215 74283ab8 2020-02-07 stsp optarg);
8216 ffd1d5e5 2018-06-23 stsp break;
8217 ffd1d5e5 2018-06-23 stsp default:
8218 e99e2d15 2020-11-24 naddy usage_tree();
8219 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8220 ffd1d5e5 2018-06-23 stsp }
8221 ffd1d5e5 2018-06-23 stsp }
8222 ffd1d5e5 2018-06-23 stsp
8223 ffd1d5e5 2018-06-23 stsp argc -= optind;
8224 ffd1d5e5 2018-06-23 stsp argv += optind;
8225 ffd1d5e5 2018-06-23 stsp
8226 55cccc34 2020-02-20 stsp if (argc > 1)
8227 e99e2d15 2020-11-24 naddy usage_tree();
8228 7cd52833 2022-06-23 thomas
8229 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8230 7cd52833 2022-06-23 thomas if (error != NULL)
8231 7cd52833 2022-06-23 thomas goto done;
8232 74283ab8 2020-02-07 stsp
8233 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8234 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8235 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8236 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8237 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
8238 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8239 c156c7a4 2020-12-18 stsp goto done;
8240 55cccc34 2020-02-20 stsp if (worktree)
8241 52185f70 2019-02-05 stsp repo_path =
8242 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8243 55cccc34 2020-02-20 stsp else
8244 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8245 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8246 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8247 c156c7a4 2020-12-18 stsp goto done;
8248 c156c7a4 2020-12-18 stsp }
8249 55cccc34 2020-02-20 stsp }
8250 a915003a 2019-02-05 stsp
8251 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8252 c02c541e 2019-03-29 stsp if (error != NULL)
8253 52185f70 2019-02-05 stsp goto done;
8254 d188b9a6 2019-01-04 stsp
8255 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8256 55cccc34 2020-02-20 stsp repo, worktree);
8257 55cccc34 2020-02-20 stsp if (error)
8258 55cccc34 2020-02-20 stsp goto done;
8259 55cccc34 2020-02-20 stsp
8260 557d3365 2023-04-14 thomas init_curses();
8261 55cccc34 2020-02-20 stsp
8262 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8263 c02c541e 2019-03-29 stsp if (error)
8264 52185f70 2019-02-05 stsp goto done;
8265 ffd1d5e5 2018-06-23 stsp
8266 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8267 51a10b52 2020-12-26 stsp if (error)
8268 51a10b52 2020-12-26 stsp goto done;
8269 51a10b52 2020-12-26 stsp
8270 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8271 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8272 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8273 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8274 4e97c21c 2020-12-06 stsp if (error)
8275 4e97c21c 2020-12-06 stsp goto done;
8276 4e97c21c 2020-12-06 stsp head_ref_name = label;
8277 4e97c21c 2020-12-06 stsp } else {
8278 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8279 66b04f8f 2023-07-19 thomas repo, worktree);
8280 66b04f8f 2023-07-19 thomas if (error != NULL)
8281 66b04f8f 2023-07-19 thomas goto done;
8282 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
8283 66b04f8f 2023-07-19 thomas commit_id_arg = keyword_idstr;
8284 66b04f8f 2023-07-19 thomas
8285 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8286 4e97c21c 2020-12-06 stsp if (error == NULL)
8287 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8288 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8289 4e97c21c 2020-12-06 stsp goto done;
8290 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8291 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8292 4e97c21c 2020-12-06 stsp if (error)
8293 4e97c21c 2020-12-06 stsp goto done;
8294 4e97c21c 2020-12-06 stsp }
8295 ffd1d5e5 2018-06-23 stsp
8296 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8297 945f9229 2022-04-16 thomas if (error)
8298 945f9229 2022-04-16 thomas goto done;
8299 945f9229 2022-04-16 thomas
8300 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8301 5221c383 2018-08-01 stsp if (view == NULL) {
8302 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8303 5221c383 2018-08-01 stsp goto done;
8304 5221c383 2018-08-01 stsp }
8305 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8306 ad80ab7b 2018-08-04 stsp if (error)
8307 ad80ab7b 2018-08-04 stsp goto done;
8308 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8309 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8310 d91faf3b 2020-12-01 naddy in_repo_path);
8311 55cccc34 2020-02-20 stsp if (error)
8312 55cccc34 2020-02-20 stsp goto done;
8313 55cccc34 2020-02-20 stsp }
8314 55cccc34 2020-02-20 stsp
8315 55cccc34 2020-02-20 stsp if (worktree) {
8316 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
8317 349dfd1e 2023-07-23 thomas if (error != NULL)
8318 349dfd1e 2023-07-23 thomas goto done;
8319 349dfd1e 2023-07-23 thomas
8320 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8321 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8322 55cccc34 2020-02-20 stsp worktree = NULL;
8323 55cccc34 2020-02-20 stsp }
8324 349dfd1e 2023-07-23 thomas
8325 557d3365 2023-04-14 thomas error = view_loop(view);
8326 349dfd1e 2023-07-23 thomas
8327 ffd1d5e5 2018-06-23 stsp done:
8328 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
8329 66b04f8f 2023-07-19 thomas free(keyword_idstr);
8330 52185f70 2019-02-05 stsp free(repo_path);
8331 e4a0e26d 2020-02-20 stsp free(cwd);
8332 ffd1d5e5 2018-06-23 stsp free(commit_id);
8333 4e97c21c 2020-12-06 stsp free(label);
8334 486cd271 2020-12-06 stsp if (ref)
8335 486cd271 2020-12-06 stsp got_ref_close(ref);
8336 349dfd1e 2023-07-23 thomas if (worktree != NULL)
8337 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
8338 1d0f4054 2021-06-17 stsp if (repo) {
8339 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8340 1d0f4054 2021-06-17 stsp if (error == NULL)
8341 1d0f4054 2021-06-17 stsp error = close_err;
8342 1d0f4054 2021-06-17 stsp }
8343 7cd52833 2022-06-23 thomas if (pack_fds) {
8344 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8345 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8346 7cd52833 2022-06-23 thomas if (error == NULL)
8347 7cd52833 2022-06-23 thomas error = pack_err;
8348 b85a3496 2023-04-14 thomas }
8349 51a10b52 2020-12-26 stsp tog_free_refs();
8350 ffd1d5e5 2018-06-23 stsp return error;
8351 6458efa5 2020-11-24 stsp }
8352 6458efa5 2020-11-24 stsp
8353 6458efa5 2020-11-24 stsp static const struct got_error *
8354 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8355 6458efa5 2020-11-24 stsp {
8356 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8357 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8358 6458efa5 2020-11-24 stsp
8359 6458efa5 2020-11-24 stsp s->nrefs = 0;
8360 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8361 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8362 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8363 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8364 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8365 6458efa5 2020-11-24 stsp continue;
8366 6458efa5 2020-11-24 stsp
8367 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8368 6458efa5 2020-11-24 stsp if (re == NULL)
8369 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8370 6458efa5 2020-11-24 stsp
8371 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8372 8924d611 2020-12-26 stsp if (re->ref == NULL)
8373 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8374 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8375 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8376 6458efa5 2020-11-24 stsp }
8377 6458efa5 2020-11-24 stsp
8378 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8379 6458efa5 2020-11-24 stsp return NULL;
8380 6458efa5 2020-11-24 stsp }
8381 6458efa5 2020-11-24 stsp
8382 ef20f542 2022-06-26 thomas static void
8383 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8384 6458efa5 2020-11-24 stsp {
8385 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8386 6458efa5 2020-11-24 stsp
8387 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8388 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8389 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8390 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8391 6458efa5 2020-11-24 stsp free(re);
8392 6458efa5 2020-11-24 stsp }
8393 6458efa5 2020-11-24 stsp }
8394 6458efa5 2020-11-24 stsp
8395 6458efa5 2020-11-24 stsp static const struct got_error *
8396 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8397 6458efa5 2020-11-24 stsp {
8398 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8399 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8400 6458efa5 2020-11-24 stsp
8401 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8402 6458efa5 2020-11-24 stsp s->repo = repo;
8403 6458efa5 2020-11-24 stsp
8404 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8405 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8406 6458efa5 2020-11-24 stsp
8407 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8408 6458efa5 2020-11-24 stsp if (err)
8409 51b7e1c3 2023-04-14 thomas goto done;
8410 34ba6917 2020-11-30 stsp
8411 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8412 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8413 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8414 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8415 6458efa5 2020-11-24 stsp if (err)
8416 6458efa5 2020-11-24 stsp goto done;
8417 6458efa5 2020-11-24 stsp
8418 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8419 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8420 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8421 6458efa5 2020-11-24 stsp if (err)
8422 6458efa5 2020-11-24 stsp goto done;
8423 6458efa5 2020-11-24 stsp
8424 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8425 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8426 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8427 2183bbf6 2022-01-23 thomas if (err)
8428 2183bbf6 2022-01-23 thomas goto done;
8429 2183bbf6 2022-01-23 thomas
8430 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8431 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8432 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8433 6458efa5 2020-11-24 stsp if (err)
8434 6458efa5 2020-11-24 stsp goto done;
8435 6458efa5 2020-11-24 stsp }
8436 6458efa5 2020-11-24 stsp
8437 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8438 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8439 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8440 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8441 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8442 6458efa5 2020-11-24 stsp done:
8443 51b7e1c3 2023-04-14 thomas if (err) {
8444 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8445 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8446 51b7e1c3 2023-04-14 thomas view_close(view);
8447 51b7e1c3 2023-04-14 thomas }
8448 6458efa5 2020-11-24 stsp return err;
8449 6458efa5 2020-11-24 stsp }
8450 6458efa5 2020-11-24 stsp
8451 6458efa5 2020-11-24 stsp static const struct got_error *
8452 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8453 6458efa5 2020-11-24 stsp {
8454 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8455 6458efa5 2020-11-24 stsp
8456 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8457 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8458 6458efa5 2020-11-24 stsp
8459 6458efa5 2020-11-24 stsp return NULL;
8460 9f7d7167 2018-04-29 stsp }
8461 ce5b7c56 2019-07-09 stsp
8462 6458efa5 2020-11-24 stsp static const struct got_error *
8463 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8464 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8465 6458efa5 2020-11-24 stsp {
8466 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8467 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8468 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8469 6458efa5 2020-11-24 stsp int obj_type;
8470 6458efa5 2020-11-24 stsp
8471 c42c9805 2020-11-24 stsp *commit_id = NULL;
8472 6458efa5 2020-11-24 stsp
8473 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8474 6458efa5 2020-11-24 stsp if (err)
8475 6458efa5 2020-11-24 stsp return err;
8476 6458efa5 2020-11-24 stsp
8477 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8478 6458efa5 2020-11-24 stsp if (err)
8479 6458efa5 2020-11-24 stsp goto done;
8480 6458efa5 2020-11-24 stsp
8481 6458efa5 2020-11-24 stsp switch (obj_type) {
8482 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8483 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8484 6458efa5 2020-11-24 stsp break;
8485 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8486 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8487 6458efa5 2020-11-24 stsp if (err)
8488 6458efa5 2020-11-24 stsp goto done;
8489 c42c9805 2020-11-24 stsp free(obj_id);
8490 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8491 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8492 c42c9805 2020-11-24 stsp if (err)
8493 6458efa5 2020-11-24 stsp goto done;
8494 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8495 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8496 c42c9805 2020-11-24 stsp goto done;
8497 c42c9805 2020-11-24 stsp }
8498 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8499 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8500 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8501 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8502 c42c9805 2020-11-24 stsp goto done;
8503 c42c9805 2020-11-24 stsp }
8504 6458efa5 2020-11-24 stsp break;
8505 6458efa5 2020-11-24 stsp default:
8506 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8507 c42c9805 2020-11-24 stsp break;
8508 6458efa5 2020-11-24 stsp }
8509 6458efa5 2020-11-24 stsp
8510 c42c9805 2020-11-24 stsp done:
8511 c42c9805 2020-11-24 stsp if (tag)
8512 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8513 c42c9805 2020-11-24 stsp if (err) {
8514 c42c9805 2020-11-24 stsp free(*commit_id);
8515 c42c9805 2020-11-24 stsp *commit_id = NULL;
8516 c42c9805 2020-11-24 stsp }
8517 c42c9805 2020-11-24 stsp return err;
8518 c42c9805 2020-11-24 stsp }
8519 c42c9805 2020-11-24 stsp
8520 c42c9805 2020-11-24 stsp static const struct got_error *
8521 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8522 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8523 c42c9805 2020-11-24 stsp {
8524 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8525 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8526 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8527 c42c9805 2020-11-24 stsp
8528 c42c9805 2020-11-24 stsp *new_view = NULL;
8529 c42c9805 2020-11-24 stsp
8530 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8531 c42c9805 2020-11-24 stsp if (err) {
8532 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8533 c42c9805 2020-11-24 stsp return err;
8534 c42c9805 2020-11-24 stsp else
8535 c42c9805 2020-11-24 stsp return NULL;
8536 c42c9805 2020-11-24 stsp }
8537 c42c9805 2020-11-24 stsp
8538 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8539 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8540 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8541 6458efa5 2020-11-24 stsp goto done;
8542 6458efa5 2020-11-24 stsp }
8543 6458efa5 2020-11-24 stsp
8544 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8545 92845f09 2023-07-26 thomas got_ref_get_name(re->ref), "", 0, NULL);
8546 6458efa5 2020-11-24 stsp done:
8547 6458efa5 2020-11-24 stsp if (err)
8548 6458efa5 2020-11-24 stsp view_close(log_view);
8549 6458efa5 2020-11-24 stsp else
8550 6458efa5 2020-11-24 stsp *new_view = log_view;
8551 c42c9805 2020-11-24 stsp free(commit_id);
8552 6458efa5 2020-11-24 stsp return err;
8553 6458efa5 2020-11-24 stsp }
8554 6458efa5 2020-11-24 stsp
8555 ce5b7c56 2019-07-09 stsp static void
8556 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8557 6458efa5 2020-11-24 stsp {
8558 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8559 34ba6917 2020-11-30 stsp int i = 0;
8560 6458efa5 2020-11-24 stsp
8561 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8562 6458efa5 2020-11-24 stsp return;
8563 6458efa5 2020-11-24 stsp
8564 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8565 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8566 34ba6917 2020-11-30 stsp if (re == NULL)
8567 34ba6917 2020-11-30 stsp break;
8568 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8569 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8570 6458efa5 2020-11-24 stsp }
8571 6458efa5 2020-11-24 stsp }
8572 6458efa5 2020-11-24 stsp
8573 a5d43cac 2022-07-01 thomas static const struct got_error *
8574 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8575 6458efa5 2020-11-24 stsp {
8576 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8577 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8578 6458efa5 2020-11-24 stsp int n = 0;
8579 6458efa5 2020-11-24 stsp
8580 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8581 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8582 6458efa5 2020-11-24 stsp else
8583 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8584 6458efa5 2020-11-24 stsp
8585 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8586 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8587 07dd3ed3 2022-08-06 thomas if (last) {
8588 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8589 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8590 07dd3ed3 2022-08-06 thomas }
8591 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8592 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8593 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8594 6458efa5 2020-11-24 stsp }
8595 6458efa5 2020-11-24 stsp }
8596 a5d43cac 2022-07-01 thomas
8597 a5d43cac 2022-07-01 thomas return NULL;
8598 6458efa5 2020-11-24 stsp }
8599 6458efa5 2020-11-24 stsp
8600 6458efa5 2020-11-24 stsp static const struct got_error *
8601 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8602 6458efa5 2020-11-24 stsp {
8603 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8604 6458efa5 2020-11-24 stsp
8605 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8606 6458efa5 2020-11-24 stsp return NULL;
8607 6458efa5 2020-11-24 stsp }
8608 6458efa5 2020-11-24 stsp
8609 6458efa5 2020-11-24 stsp static int
8610 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8611 6458efa5 2020-11-24 stsp {
8612 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8613 6458efa5 2020-11-24 stsp
8614 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8615 6458efa5 2020-11-24 stsp 0) == 0;
8616 6458efa5 2020-11-24 stsp }
8617 6458efa5 2020-11-24 stsp
8618 6458efa5 2020-11-24 stsp static const struct got_error *
8619 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8620 6458efa5 2020-11-24 stsp {
8621 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8622 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8623 6458efa5 2020-11-24 stsp
8624 6458efa5 2020-11-24 stsp if (!view->searching) {
8625 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8626 6458efa5 2020-11-24 stsp return NULL;
8627 6458efa5 2020-11-24 stsp }
8628 6458efa5 2020-11-24 stsp
8629 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8630 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8631 6458efa5 2020-11-24 stsp if (s->selected_entry)
8632 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8633 6458efa5 2020-11-24 stsp else
8634 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8635 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8636 6458efa5 2020-11-24 stsp } else {
8637 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8638 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8639 6458efa5 2020-11-24 stsp else
8640 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8641 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8642 6458efa5 2020-11-24 stsp }
8643 6458efa5 2020-11-24 stsp } else {
8644 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8645 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8646 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8647 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8648 6458efa5 2020-11-24 stsp else
8649 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8650 6458efa5 2020-11-24 stsp }
8651 6458efa5 2020-11-24 stsp
8652 6458efa5 2020-11-24 stsp while (1) {
8653 6458efa5 2020-11-24 stsp if (re == NULL) {
8654 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8655 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8656 6458efa5 2020-11-24 stsp return NULL;
8657 6458efa5 2020-11-24 stsp }
8658 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8659 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8660 6458efa5 2020-11-24 stsp else
8661 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8662 6458efa5 2020-11-24 stsp }
8663 6458efa5 2020-11-24 stsp
8664 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8665 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8666 6458efa5 2020-11-24 stsp s->matched_entry = re;
8667 6458efa5 2020-11-24 stsp break;
8668 6458efa5 2020-11-24 stsp }
8669 6458efa5 2020-11-24 stsp
8670 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8671 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8672 6458efa5 2020-11-24 stsp else
8673 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8674 6458efa5 2020-11-24 stsp }
8675 6458efa5 2020-11-24 stsp
8676 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8677 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8678 6458efa5 2020-11-24 stsp s->selected = 0;
8679 6458efa5 2020-11-24 stsp }
8680 6458efa5 2020-11-24 stsp
8681 6458efa5 2020-11-24 stsp return NULL;
8682 6458efa5 2020-11-24 stsp }
8683 6458efa5 2020-11-24 stsp
8684 6458efa5 2020-11-24 stsp static const struct got_error *
8685 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8686 6458efa5 2020-11-24 stsp {
8687 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8688 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8689 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8690 6458efa5 2020-11-24 stsp char *line = NULL;
8691 6458efa5 2020-11-24 stsp wchar_t *wline;
8692 6458efa5 2020-11-24 stsp struct tog_color *tc;
8693 66a70b97 2023-02-03 thomas int width, n, scrollx;
8694 6458efa5 2020-11-24 stsp int limit = view->nlines;
8695 6458efa5 2020-11-24 stsp
8696 6458efa5 2020-11-24 stsp werase(view->window);
8697 6458efa5 2020-11-24 stsp
8698 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8699 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8700 a5d43cac 2022-07-01 thomas --limit; /* border */
8701 6458efa5 2020-11-24 stsp
8702 6458efa5 2020-11-24 stsp if (limit == 0)
8703 6458efa5 2020-11-24 stsp return NULL;
8704 6458efa5 2020-11-24 stsp
8705 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8706 6458efa5 2020-11-24 stsp
8707 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8708 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8709 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8710 6458efa5 2020-11-24 stsp
8711 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8712 6458efa5 2020-11-24 stsp if (err) {
8713 6458efa5 2020-11-24 stsp free(line);
8714 6458efa5 2020-11-24 stsp return err;
8715 6458efa5 2020-11-24 stsp }
8716 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8717 6458efa5 2020-11-24 stsp wstandout(view->window);
8718 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8719 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8720 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8721 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8722 6458efa5 2020-11-24 stsp wstandend(view->window);
8723 6458efa5 2020-11-24 stsp free(wline);
8724 6458efa5 2020-11-24 stsp wline = NULL;
8725 6458efa5 2020-11-24 stsp free(line);
8726 6458efa5 2020-11-24 stsp line = NULL;
8727 6458efa5 2020-11-24 stsp if (--limit <= 0)
8728 6458efa5 2020-11-24 stsp return NULL;
8729 6458efa5 2020-11-24 stsp
8730 6458efa5 2020-11-24 stsp n = 0;
8731 66a70b97 2023-02-03 thomas view->maxx = 0;
8732 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8733 6458efa5 2020-11-24 stsp char *line = NULL;
8734 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8735 6458efa5 2020-11-24 stsp
8736 84227eb1 2022-06-23 thomas if (s->show_date) {
8737 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8738 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8739 84227eb1 2022-06-23 thomas struct got_object_id *id;
8740 84227eb1 2022-06-23 thomas struct tm tm;
8741 84227eb1 2022-06-23 thomas time_t t;
8742 84227eb1 2022-06-23 thomas
8743 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8744 84227eb1 2022-06-23 thomas if (err)
8745 84227eb1 2022-06-23 thomas return err;
8746 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8747 84227eb1 2022-06-23 thomas if (err) {
8748 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8749 84227eb1 2022-06-23 thomas free(id);
8750 84227eb1 2022-06-23 thomas return err;
8751 84227eb1 2022-06-23 thomas }
8752 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8753 84227eb1 2022-06-23 thomas id);
8754 84227eb1 2022-06-23 thomas if (err) {
8755 84227eb1 2022-06-23 thomas free(id);
8756 84227eb1 2022-06-23 thomas return err;
8757 84227eb1 2022-06-23 thomas }
8758 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8759 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8760 84227eb1 2022-06-23 thomas } else {
8761 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8762 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8763 84227eb1 2022-06-23 thomas }
8764 84227eb1 2022-06-23 thomas free(id);
8765 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8766 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8767 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8768 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8769 84227eb1 2022-06-23 thomas }
8770 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8771 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8772 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8773 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8774 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8775 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8776 6458efa5 2020-11-24 stsp struct got_object_id *id;
8777 6458efa5 2020-11-24 stsp char *id_str;
8778 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8779 6458efa5 2020-11-24 stsp if (err)
8780 6458efa5 2020-11-24 stsp return err;
8781 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8782 6458efa5 2020-11-24 stsp if (err) {
8783 6458efa5 2020-11-24 stsp free(id);
8784 6458efa5 2020-11-24 stsp return err;
8785 6458efa5 2020-11-24 stsp }
8786 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8787 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8788 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8789 6458efa5 2020-11-24 stsp free(id);
8790 6458efa5 2020-11-24 stsp free(id_str);
8791 6458efa5 2020-11-24 stsp return err;
8792 6458efa5 2020-11-24 stsp }
8793 6458efa5 2020-11-24 stsp free(id);
8794 6458efa5 2020-11-24 stsp free(id_str);
8795 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8796 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8797 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8798 6458efa5 2020-11-24 stsp
8799 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8800 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8801 6458efa5 2020-11-24 stsp if (err) {
8802 6458efa5 2020-11-24 stsp free(line);
8803 6458efa5 2020-11-24 stsp return err;
8804 6458efa5 2020-11-24 stsp }
8805 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8806 66a70b97 2023-02-03 thomas free(wline);
8807 66a70b97 2023-02-03 thomas wline = NULL;
8808 66a70b97 2023-02-03 thomas
8809 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8810 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8811 66a70b97 2023-02-03 thomas if (err) {
8812 66a70b97 2023-02-03 thomas free(line);
8813 66a70b97 2023-02-03 thomas return err;
8814 66a70b97 2023-02-03 thomas }
8815 6458efa5 2020-11-24 stsp if (n == s->selected) {
8816 6458efa5 2020-11-24 stsp if (view->focussed)
8817 6458efa5 2020-11-24 stsp wstandout(view->window);
8818 6458efa5 2020-11-24 stsp s->selected_entry = re;
8819 6458efa5 2020-11-24 stsp }
8820 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8821 6458efa5 2020-11-24 stsp if (tc)
8822 6458efa5 2020-11-24 stsp wattr_on(view->window,
8823 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8824 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8825 6458efa5 2020-11-24 stsp if (tc)
8826 6458efa5 2020-11-24 stsp wattr_off(view->window,
8827 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8828 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8829 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8830 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8831 6458efa5 2020-11-24 stsp wstandend(view->window);
8832 6458efa5 2020-11-24 stsp free(line);
8833 6458efa5 2020-11-24 stsp free(wline);
8834 6458efa5 2020-11-24 stsp wline = NULL;
8835 6458efa5 2020-11-24 stsp n++;
8836 6458efa5 2020-11-24 stsp s->ndisplayed++;
8837 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8838 6458efa5 2020-11-24 stsp
8839 6458efa5 2020-11-24 stsp limit--;
8840 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8841 6458efa5 2020-11-24 stsp }
8842 6458efa5 2020-11-24 stsp
8843 a5d43cac 2022-07-01 thomas view_border(view);
8844 6458efa5 2020-11-24 stsp return err;
8845 6458efa5 2020-11-24 stsp }
8846 6458efa5 2020-11-24 stsp
8847 6458efa5 2020-11-24 stsp static const struct got_error *
8848 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8849 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8850 c42c9805 2020-11-24 stsp {
8851 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8852 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8853 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8854 c42c9805 2020-11-24 stsp
8855 c42c9805 2020-11-24 stsp *new_view = NULL;
8856 c42c9805 2020-11-24 stsp
8857 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8858 c42c9805 2020-11-24 stsp if (err) {
8859 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8860 c42c9805 2020-11-24 stsp return err;
8861 c42c9805 2020-11-24 stsp else
8862 c42c9805 2020-11-24 stsp return NULL;
8863 c42c9805 2020-11-24 stsp }
8864 c42c9805 2020-11-24 stsp
8865 c42c9805 2020-11-24 stsp
8866 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8867 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8868 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8869 c42c9805 2020-11-24 stsp goto done;
8870 c42c9805 2020-11-24 stsp }
8871 c42c9805 2020-11-24 stsp
8872 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8873 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8874 c42c9805 2020-11-24 stsp if (err)
8875 c42c9805 2020-11-24 stsp goto done;
8876 c42c9805 2020-11-24 stsp
8877 c42c9805 2020-11-24 stsp *new_view = tree_view;
8878 c42c9805 2020-11-24 stsp done:
8879 c42c9805 2020-11-24 stsp free(commit_id);
8880 c42c9805 2020-11-24 stsp return err;
8881 07dd3ed3 2022-08-06 thomas }
8882 07dd3ed3 2022-08-06 thomas
8883 07dd3ed3 2022-08-06 thomas static const struct got_error *
8884 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8885 07dd3ed3 2022-08-06 thomas {
8886 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8887 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8888 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8889 07dd3ed3 2022-08-06 thomas
8890 07dd3ed3 2022-08-06 thomas g = view->gline;
8891 07dd3ed3 2022-08-06 thomas view->gline = 0;
8892 07dd3ed3 2022-08-06 thomas
8893 07dd3ed3 2022-08-06 thomas if (g == 0)
8894 07dd3ed3 2022-08-06 thomas g = 1;
8895 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8896 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8897 07dd3ed3 2022-08-06 thomas
8898 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8899 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8900 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8901 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8902 07dd3ed3 2022-08-06 thomas return NULL;
8903 07dd3ed3 2022-08-06 thomas }
8904 07dd3ed3 2022-08-06 thomas
8905 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8906 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8907 07dd3ed3 2022-08-06 thomas if (err)
8908 07dd3ed3 2022-08-06 thomas return err;
8909 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8910 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8911 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8912 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8913 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8914 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8915 07dd3ed3 2022-08-06 thomas
8916 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8917 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8918 07dd3ed3 2022-08-06 thomas
8919 07dd3ed3 2022-08-06 thomas return NULL;
8920 07dd3ed3 2022-08-06 thomas
8921 c42c9805 2020-11-24 stsp }
8922 07dd3ed3 2022-08-06 thomas
8923 c42c9805 2020-11-24 stsp static const struct got_error *
8924 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8925 6458efa5 2020-11-24 stsp {
8926 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8927 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8928 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8929 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8930 6458efa5 2020-11-24 stsp
8931 07dd3ed3 2022-08-06 thomas if (view->gline)
8932 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8933 07dd3ed3 2022-08-06 thomas
8934 6458efa5 2020-11-24 stsp switch (ch) {
8935 66a70b97 2023-02-03 thomas case '0':
8936 66a70b97 2023-02-03 thomas case '$':
8937 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8938 66a70b97 2023-02-03 thomas case 'l':
8939 66a70b97 2023-02-03 thomas case KEY_LEFT:
8940 66a70b97 2023-02-03 thomas case 'h':
8941 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8942 66a70b97 2023-02-03 thomas break;
8943 6458efa5 2020-11-24 stsp case 'i':
8944 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8945 07b0611c 2022-06-23 thomas view->count = 0;
8946 3bfadbd4 2021-11-20 thomas break;
8947 84227eb1 2022-06-23 thomas case 'm':
8948 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8949 07b0611c 2022-06-23 thomas view->count = 0;
8950 84227eb1 2022-06-23 thomas break;
8951 98182bd0 2021-11-20 thomas case 'o':
8952 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8953 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8954 07b0611c 2022-06-23 thomas view->count = 0;
8955 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8956 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8957 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8958 c591440f 2021-11-20 thomas if (err)
8959 c591440f 2021-11-20 thomas break;
8960 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8961 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8962 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8963 3bfadbd4 2021-11-20 thomas if (err)
8964 3bfadbd4 2021-11-20 thomas break;
8965 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8966 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8967 6458efa5 2020-11-24 stsp break;
8968 6458efa5 2020-11-24 stsp case KEY_ENTER:
8969 6458efa5 2020-11-24 stsp case '\r':
8970 07b0611c 2022-06-23 thomas view->count = 0;
8971 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8972 a5d43cac 2022-07-01 thomas break;
8973 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8974 6458efa5 2020-11-24 stsp break;
8975 1be4947a 2022-07-22 thomas case 'T':
8976 07b0611c 2022-06-23 thomas view->count = 0;
8977 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8978 c42c9805 2020-11-24 stsp break;
8979 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8980 c42c9805 2020-11-24 stsp break;
8981 e4526bf5 2021-09-03 naddy case 'g':
8982 aa7a1117 2023-01-09 thomas case '=':
8983 e4526bf5 2021-09-03 naddy case KEY_HOME:
8984 e4526bf5 2021-09-03 naddy s->selected = 0;
8985 07b0611c 2022-06-23 thomas view->count = 0;
8986 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8987 e4526bf5 2021-09-03 naddy break;
8988 e4526bf5 2021-09-03 naddy case 'G':
8989 aa7a1117 2023-01-09 thomas case '*':
8990 a5d43cac 2022-07-01 thomas case KEY_END: {
8991 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8992 a5d43cac 2022-07-01 thomas
8993 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8994 a5d43cac 2022-07-01 thomas --eos; /* border */
8995 e4526bf5 2021-09-03 naddy s->selected = 0;
8996 07b0611c 2022-06-23 thomas view->count = 0;
8997 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8998 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8999 e4526bf5 2021-09-03 naddy if (re == NULL)
9000 e4526bf5 2021-09-03 naddy break;
9001 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
9002 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
9003 e4526bf5 2021-09-03 naddy }
9004 e4526bf5 2021-09-03 naddy if (n > 0)
9005 e4526bf5 2021-09-03 naddy s->selected = n - 1;
9006 e4526bf5 2021-09-03 naddy break;
9007 a5d43cac 2022-07-01 thomas }
9008 6458efa5 2020-11-24 stsp case 'k':
9009 6458efa5 2020-11-24 stsp case KEY_UP:
9010 f7140bf5 2021-10-17 thomas case CTRL('p'):
9011 6458efa5 2020-11-24 stsp if (s->selected > 0) {
9012 6458efa5 2020-11-24 stsp s->selected--;
9013 6458efa5 2020-11-24 stsp break;
9014 34ba6917 2020-11-30 stsp }
9015 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
9016 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9017 07b0611c 2022-06-23 thomas view->count = 0;
9018 6458efa5 2020-11-24 stsp break;
9019 70f17a53 2022-06-13 thomas case CTRL('u'):
9020 23427b14 2022-06-23 thomas case 'u':
9021 70f17a53 2022-06-13 thomas nscroll /= 2;
9022 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9023 6458efa5 2020-11-24 stsp case KEY_PPAGE:
9024 6458efa5 2020-11-24 stsp case CTRL('b'):
9025 1c5e5faa 2022-06-23 thomas case 'b':
9026 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
9027 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
9028 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
9029 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9030 07b0611c 2022-06-23 thomas view->count = 0;
9031 6458efa5 2020-11-24 stsp break;
9032 6458efa5 2020-11-24 stsp case 'j':
9033 6458efa5 2020-11-24 stsp case KEY_DOWN:
9034 f7140bf5 2021-10-17 thomas case CTRL('n'):
9035 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
9036 6458efa5 2020-11-24 stsp s->selected++;
9037 6458efa5 2020-11-24 stsp break;
9038 6458efa5 2020-11-24 stsp }
9039 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9040 6458efa5 2020-11-24 stsp /* can't scroll any further */
9041 07b0611c 2022-06-23 thomas view->count = 0;
9042 6458efa5 2020-11-24 stsp break;
9043 07b0611c 2022-06-23 thomas }
9044 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
9045 6458efa5 2020-11-24 stsp break;
9046 70f17a53 2022-06-13 thomas case CTRL('d'):
9047 23427b14 2022-06-23 thomas case 'd':
9048 70f17a53 2022-06-13 thomas nscroll /= 2;
9049 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9050 6458efa5 2020-11-24 stsp case KEY_NPAGE:
9051 6458efa5 2020-11-24 stsp case CTRL('f'):
9052 1c5e5faa 2022-06-23 thomas case 'f':
9053 4c2d69cb 2022-06-23 thomas case ' ':
9054 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9055 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
9056 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
9057 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
9058 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
9059 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
9060 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
9061 07b0611c 2022-06-23 thomas view->count = 0;
9062 6458efa5 2020-11-24 stsp break;
9063 6458efa5 2020-11-24 stsp }
9064 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
9065 6458efa5 2020-11-24 stsp break;
9066 6458efa5 2020-11-24 stsp case CTRL('l'):
9067 07b0611c 2022-06-23 thomas view->count = 0;
9068 8924d611 2020-12-26 stsp tog_free_refs();
9069 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
9070 8924d611 2020-12-26 stsp if (err)
9071 8924d611 2020-12-26 stsp break;
9072 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
9073 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
9074 6458efa5 2020-11-24 stsp break;
9075 6458efa5 2020-11-24 stsp case KEY_RESIZE:
9076 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9077 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
9078 6458efa5 2020-11-24 stsp break;
9079 6458efa5 2020-11-24 stsp default:
9080 07b0611c 2022-06-23 thomas view->count = 0;
9081 6458efa5 2020-11-24 stsp break;
9082 6458efa5 2020-11-24 stsp }
9083 6458efa5 2020-11-24 stsp
9084 6458efa5 2020-11-24 stsp return err;
9085 6458efa5 2020-11-24 stsp }
9086 6458efa5 2020-11-24 stsp
9087 6458efa5 2020-11-24 stsp __dead static void
9088 6458efa5 2020-11-24 stsp usage_ref(void)
9089 6458efa5 2020-11-24 stsp {
9090 6458efa5 2020-11-24 stsp endwin();
9091 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9092 6458efa5 2020-11-24 stsp getprogname());
9093 6458efa5 2020-11-24 stsp exit(1);
9094 6458efa5 2020-11-24 stsp }
9095 6458efa5 2020-11-24 stsp
9096 6458efa5 2020-11-24 stsp static const struct got_error *
9097 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
9098 6458efa5 2020-11-24 stsp {
9099 1d98034b 2023-04-14 thomas const struct got_error *error;
9100 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
9101 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
9102 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
9103 6458efa5 2020-11-24 stsp int ch;
9104 6458efa5 2020-11-24 stsp struct tog_view *view;
9105 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9106 6458efa5 2020-11-24 stsp
9107 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
9108 6458efa5 2020-11-24 stsp switch (ch) {
9109 6458efa5 2020-11-24 stsp case 'r':
9110 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
9111 6458efa5 2020-11-24 stsp if (repo_path == NULL)
9112 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
9113 6458efa5 2020-11-24 stsp optarg);
9114 6458efa5 2020-11-24 stsp break;
9115 6458efa5 2020-11-24 stsp default:
9116 e99e2d15 2020-11-24 naddy usage_ref();
9117 6458efa5 2020-11-24 stsp /* NOTREACHED */
9118 6458efa5 2020-11-24 stsp }
9119 6458efa5 2020-11-24 stsp }
9120 6458efa5 2020-11-24 stsp
9121 6458efa5 2020-11-24 stsp argc -= optind;
9122 6458efa5 2020-11-24 stsp argv += optind;
9123 6458efa5 2020-11-24 stsp
9124 6458efa5 2020-11-24 stsp if (argc > 1)
9125 e99e2d15 2020-11-24 naddy usage_ref();
9126 7cd52833 2022-06-23 thomas
9127 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9128 7cd52833 2022-06-23 thomas if (error != NULL)
9129 7cd52833 2022-06-23 thomas goto done;
9130 6458efa5 2020-11-24 stsp
9131 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
9132 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
9133 c156c7a4 2020-12-18 stsp if (cwd == NULL)
9134 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
9135 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
9136 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9137 c156c7a4 2020-12-18 stsp goto done;
9138 6458efa5 2020-11-24 stsp if (worktree)
9139 6458efa5 2020-11-24 stsp repo_path =
9140 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
9141 6458efa5 2020-11-24 stsp else
9142 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
9143 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
9144 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
9145 c156c7a4 2020-12-18 stsp goto done;
9146 c156c7a4 2020-12-18 stsp }
9147 6458efa5 2020-11-24 stsp }
9148 6458efa5 2020-11-24 stsp
9149 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9150 6458efa5 2020-11-24 stsp if (error != NULL)
9151 6458efa5 2020-11-24 stsp goto done;
9152 6458efa5 2020-11-24 stsp
9153 557d3365 2023-04-14 thomas init_curses();
9154 6458efa5 2020-11-24 stsp
9155 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
9156 51a10b52 2020-12-26 stsp if (error)
9157 51a10b52 2020-12-26 stsp goto done;
9158 51a10b52 2020-12-26 stsp
9159 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9160 6458efa5 2020-11-24 stsp if (error)
9161 6458efa5 2020-11-24 stsp goto done;
9162 6458efa5 2020-11-24 stsp
9163 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9164 6458efa5 2020-11-24 stsp if (view == NULL) {
9165 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
9166 6458efa5 2020-11-24 stsp goto done;
9167 6458efa5 2020-11-24 stsp }
9168 6458efa5 2020-11-24 stsp
9169 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
9170 6458efa5 2020-11-24 stsp if (error)
9171 6458efa5 2020-11-24 stsp goto done;
9172 6458efa5 2020-11-24 stsp
9173 6458efa5 2020-11-24 stsp if (worktree) {
9174 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
9175 349dfd1e 2023-07-23 thomas if (error != NULL)
9176 349dfd1e 2023-07-23 thomas goto done;
9177 349dfd1e 2023-07-23 thomas
9178 6458efa5 2020-11-24 stsp /* Release work tree lock. */
9179 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
9180 6458efa5 2020-11-24 stsp worktree = NULL;
9181 6458efa5 2020-11-24 stsp }
9182 349dfd1e 2023-07-23 thomas
9183 557d3365 2023-04-14 thomas error = view_loop(view);
9184 349dfd1e 2023-07-23 thomas
9185 6458efa5 2020-11-24 stsp done:
9186 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
9187 6458efa5 2020-11-24 stsp free(repo_path);
9188 6458efa5 2020-11-24 stsp free(cwd);
9189 349dfd1e 2023-07-23 thomas if (worktree != NULL)
9190 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
9191 1d0f4054 2021-06-17 stsp if (repo) {
9192 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9193 1d0f4054 2021-06-17 stsp if (close_err)
9194 1d0f4054 2021-06-17 stsp error = close_err;
9195 1d0f4054 2021-06-17 stsp }
9196 7cd52833 2022-06-23 thomas if (pack_fds) {
9197 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9198 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9199 7cd52833 2022-06-23 thomas if (error == NULL)
9200 7cd52833 2022-06-23 thomas error = pack_err;
9201 b85a3496 2023-04-14 thomas }
9202 51a10b52 2020-12-26 stsp tog_free_refs();
9203 6458efa5 2020-11-24 stsp return error;
9204 6458efa5 2020-11-24 stsp }
9205 6458efa5 2020-11-24 stsp
9206 fc2737d5 2022-09-15 thomas static const struct got_error*
9207 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9208 fc2737d5 2022-09-15 thomas const char *str)
9209 fc2737d5 2022-09-15 thomas {
9210 fc2737d5 2022-09-15 thomas size_t len;
9211 fc2737d5 2022-09-15 thomas
9212 fc2737d5 2022-09-15 thomas if (win == NULL)
9213 fc2737d5 2022-09-15 thomas win = stdscr;
9214 fc2737d5 2022-09-15 thomas
9215 fc2737d5 2022-09-15 thomas len = strlen(str);
9216 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9217 fc2737d5 2022-09-15 thomas
9218 fc2737d5 2022-09-15 thomas if (focus)
9219 fc2737d5 2022-09-15 thomas wstandout(win);
9220 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9221 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9222 fc2737d5 2022-09-15 thomas if (focus)
9223 fc2737d5 2022-09-15 thomas wstandend(win);
9224 fc2737d5 2022-09-15 thomas
9225 fc2737d5 2022-09-15 thomas return NULL;
9226 fc2737d5 2022-09-15 thomas }
9227 fc2737d5 2022-09-15 thomas
9228 2a31b33b 2022-07-23 thomas static const struct got_error *
9229 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9230 fc2737d5 2022-09-15 thomas {
9231 fc2737d5 2022-09-15 thomas off_t *p;
9232 fc2737d5 2022-09-15 thomas
9233 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9234 fc2737d5 2022-09-15 thomas if (p == NULL) {
9235 fc2737d5 2022-09-15 thomas free(*line_offsets);
9236 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9237 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9238 fc2737d5 2022-09-15 thomas }
9239 fc2737d5 2022-09-15 thomas
9240 fc2737d5 2022-09-15 thomas *line_offsets = p;
9241 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9242 fc2737d5 2022-09-15 thomas ++(*nlines);
9243 fc2737d5 2022-09-15 thomas return NULL;
9244 fc2737d5 2022-09-15 thomas }
9245 fc2737d5 2022-09-15 thomas
9246 fc2737d5 2022-09-15 thomas static const struct got_error *
9247 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9248 fc2737d5 2022-09-15 thomas {
9249 fc2737d5 2022-09-15 thomas *ret = 0;
9250 fc2737d5 2022-09-15 thomas
9251 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9252 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9253 fc2737d5 2022-09-15 thomas size_t len = 1;
9254 fc2737d5 2022-09-15 thomas
9255 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9256 fc2737d5 2022-09-15 thomas continue;
9257 fc2737d5 2022-09-15 thomas
9258 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9259 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9260 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9261 fc2737d5 2022-09-15 thomas
9262 fc2737d5 2022-09-15 thomas len += strlen(t);
9263 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9264 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9265 fc2737d5 2022-09-15 thomas free(t0);
9266 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9267 fc2737d5 2022-09-15 thomas }
9268 fc2737d5 2022-09-15 thomas
9269 fc2737d5 2022-09-15 thomas return NULL;
9270 fc2737d5 2022-09-15 thomas }
9271 fc2737d5 2022-09-15 thomas
9272 fc2737d5 2022-09-15 thomas /*
9273 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9274 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9275 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9276 fc2737d5 2022-09-15 thomas */
9277 fc2737d5 2022-09-15 thomas static const struct got_error *
9278 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9279 fc2737d5 2022-09-15 thomas {
9280 fc2737d5 2022-09-15 thomas int n, len = width;
9281 fc2737d5 2022-09-15 thomas
9282 fc2737d5 2022-09-15 thomas if (km->keys) {
9283 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9284 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9285 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9286 30e77f6a 2022-09-18 thomas };
9287 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9288 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9289 fc2737d5 2022-09-15 thomas
9290 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9291 fc2737d5 2022-09-15 thomas
9292 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9293 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9294 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9295 fc2737d5 2022-09-15 thomas
9296 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9297 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9298 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9299 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9300 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9301 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9302 fc2737d5 2022-09-15 thomas if (n < 0) {
9303 fc2737d5 2022-09-15 thomas free(t0);
9304 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9305 fc2737d5 2022-09-15 thomas }
9306 fc2737d5 2022-09-15 thomas first = 0;
9307 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9308 fc2737d5 2022-09-15 thomas *off += n;
9309 fc2737d5 2022-09-15 thomas }
9310 fc2737d5 2022-09-15 thomas free(t0);
9311 fc2737d5 2022-09-15 thomas }
9312 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9313 fc2737d5 2022-09-15 thomas if (n < 0)
9314 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9315 fc2737d5 2022-09-15 thomas *off += n;
9316 fc2737d5 2022-09-15 thomas
9317 fc2737d5 2022-09-15 thomas return NULL;
9318 fc2737d5 2022-09-15 thomas }
9319 fc2737d5 2022-09-15 thomas
9320 fc2737d5 2022-09-15 thomas static const struct got_error *
9321 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9322 fc2737d5 2022-09-15 thomas {
9323 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9324 fc2737d5 2022-09-15 thomas off_t off = 0;
9325 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9326 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9327 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9328 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9329 fc2737d5 2022-09-15 thomas GENERATE_HELP
9330 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9331 fc2737d5 2022-09-15 thomas #undef KEY_
9332 fc2737d5 2022-09-15 thomas };
9333 fc2737d5 2022-09-15 thomas
9334 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9335 fc2737d5 2022-09-15 thomas if (err)
9336 fc2737d5 2022-09-15 thomas return err;
9337 fc2737d5 2022-09-15 thomas
9338 fc2737d5 2022-09-15 thomas n = nitems(km);
9339 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9340 fc2737d5 2022-09-15 thomas if (err)
9341 fc2737d5 2022-09-15 thomas return err;
9342 fc2737d5 2022-09-15 thomas
9343 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9344 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9345 fc2737d5 2022-09-15 thomas show = s->all;
9346 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9347 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9348 fc2737d5 2022-09-15 thomas show = 1;
9349 fc2737d5 2022-09-15 thomas }
9350 fc2737d5 2022-09-15 thomas if (show) {
9351 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9352 fc2737d5 2022-09-15 thomas if (err)
9353 fc2737d5 2022-09-15 thomas return err;
9354 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9355 fc2737d5 2022-09-15 thomas if (err)
9356 fc2737d5 2022-09-15 thomas return err;
9357 fc2737d5 2022-09-15 thomas }
9358 fc2737d5 2022-09-15 thomas }
9359 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9360 fc2737d5 2022-09-15 thomas ++off;
9361 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9362 fc2737d5 2022-09-15 thomas return err;
9363 fc2737d5 2022-09-15 thomas }
9364 fc2737d5 2022-09-15 thomas
9365 fc2737d5 2022-09-15 thomas static const struct got_error *
9366 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9367 fc2737d5 2022-09-15 thomas {
9368 fc2737d5 2022-09-15 thomas FILE *f;
9369 fc2737d5 2022-09-15 thomas const struct got_error *err;
9370 fc2737d5 2022-09-15 thomas
9371 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9372 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9373 fc2737d5 2022-09-15 thomas s->nlines = 0;
9374 fc2737d5 2022-09-15 thomas
9375 fc2737d5 2022-09-15 thomas f = got_opentemp();
9376 fc2737d5 2022-09-15 thomas if (f == NULL)
9377 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9378 fc2737d5 2022-09-15 thomas s->f = f;
9379 fc2737d5 2022-09-15 thomas
9380 fc2737d5 2022-09-15 thomas err = format_help(s);
9381 fc2737d5 2022-09-15 thomas if (err)
9382 fc2737d5 2022-09-15 thomas return err;
9383 fc2737d5 2022-09-15 thomas
9384 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9385 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9386 fc2737d5 2022-09-15 thomas
9387 fc2737d5 2022-09-15 thomas return NULL;
9388 fc2737d5 2022-09-15 thomas }
9389 fc2737d5 2022-09-15 thomas
9390 fc2737d5 2022-09-15 thomas static const struct got_error *
9391 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9392 fc2737d5 2022-09-15 thomas {
9393 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9394 fc2737d5 2022-09-15 thomas return NULL;
9395 fc2737d5 2022-09-15 thomas }
9396 fc2737d5 2022-09-15 thomas
9397 2a7d3cd7 2022-09-17 thomas static void
9398 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9399 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9400 2a7d3cd7 2022-09-17 thomas {
9401 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9402 2a7d3cd7 2022-09-17 thomas
9403 2a7d3cd7 2022-09-17 thomas *f = s->f;
9404 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9405 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9406 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9407 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9408 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9409 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9410 2a7d3cd7 2022-09-17 thomas }
9411 2a7d3cd7 2022-09-17 thomas
9412 fc2737d5 2022-09-15 thomas static const struct got_error *
9413 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9414 fc2737d5 2022-09-15 thomas {
9415 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9416 fc2737d5 2022-09-15 thomas const struct got_error *err;
9417 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9418 fc2737d5 2022-09-15 thomas wchar_t *wline;
9419 fc2737d5 2022-09-15 thomas char *line;
9420 fc2737d5 2022-09-15 thomas ssize_t linelen;
9421 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9422 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9423 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9424 fc2737d5 2022-09-15 thomas
9425 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9426 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9427 fc2737d5 2022-09-15 thomas
9428 fc2737d5 2022-09-15 thomas s->lineno = 0;
9429 fc2737d5 2022-09-15 thomas rewind(s->f);
9430 fc2737d5 2022-09-15 thomas werase(view->window);
9431 fc2737d5 2022-09-15 thomas
9432 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9433 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9434 fc2737d5 2022-09-15 thomas
9435 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9436 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9437 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9438 fc2737d5 2022-09-15 thomas if (err)
9439 fc2737d5 2022-09-15 thomas return err;
9440 fc2737d5 2022-09-15 thomas if (eos <= 1)
9441 fc2737d5 2022-09-15 thomas return NULL;
9442 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9443 fc2737d5 2022-09-15 thomas eos -= 2;
9444 fc2737d5 2022-09-15 thomas
9445 fc2737d5 2022-09-15 thomas s->eof = 0;
9446 fc2737d5 2022-09-15 thomas view->maxx = 0;
9447 fc2737d5 2022-09-15 thomas line = NULL;
9448 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9449 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9450 fc2737d5 2022-09-15 thomas
9451 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9452 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9453 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9454 fc2737d5 2022-09-15 thomas free(line);
9455 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9456 fc2737d5 2022-09-15 thomas }
9457 fc2737d5 2022-09-15 thomas s->eof = 1;
9458 fc2737d5 2022-09-15 thomas break;
9459 fc2737d5 2022-09-15 thomas }
9460 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9461 fc2737d5 2022-09-15 thomas continue;
9462 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9463 fc2737d5 2022-09-15 thomas continue;
9464 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9465 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9466 fc2737d5 2022-09-15 thomas
9467 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9468 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9469 fc2737d5 2022-09-15 thomas if (err) {
9470 fc2737d5 2022-09-15 thomas free(line);
9471 fc2737d5 2022-09-15 thomas return err;
9472 fc2737d5 2022-09-15 thomas }
9473 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9474 fc2737d5 2022-09-15 thomas free(wline);
9475 fc2737d5 2022-09-15 thomas wline = NULL;
9476 fc2737d5 2022-09-15 thomas
9477 fc2737d5 2022-09-15 thomas if (attr)
9478 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9479 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9480 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9481 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9482 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9483 fc2737d5 2022-09-15 thomas if (err) {
9484 fc2737d5 2022-09-15 thomas free(line);
9485 fc2737d5 2022-09-15 thomas return err;
9486 fc2737d5 2022-09-15 thomas }
9487 fc2737d5 2022-09-15 thomas } else {
9488 fc2737d5 2022-09-15 thomas int skip;
9489 fc2737d5 2022-09-15 thomas
9490 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9491 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9492 fc2737d5 2022-09-15 thomas if (err) {
9493 fc2737d5 2022-09-15 thomas free(line);
9494 fc2737d5 2022-09-15 thomas return err;
9495 fc2737d5 2022-09-15 thomas }
9496 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9497 fc2737d5 2022-09-15 thomas free(wline);
9498 fc2737d5 2022-09-15 thomas wline = NULL;
9499 fc2737d5 2022-09-15 thomas }
9500 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9501 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9502 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9503 fc2737d5 2022-09-15 thomas } else {
9504 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9505 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9506 fc2737d5 2022-09-15 thomas }
9507 fc2737d5 2022-09-15 thomas if (attr)
9508 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9509 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9510 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9511 fc2737d5 2022-09-15 thomas }
9512 fc2737d5 2022-09-15 thomas free(line);
9513 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9514 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9515 fc2737d5 2022-09-15 thomas else
9516 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9517 fc2737d5 2022-09-15 thomas
9518 fc2737d5 2022-09-15 thomas view_border(view);
9519 fc2737d5 2022-09-15 thomas
9520 fc2737d5 2022-09-15 thomas if (s->eof) {
9521 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9522 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9523 fc2737d5 2022-09-15 thomas view->ncols - 1);
9524 fc2737d5 2022-09-15 thomas if (rc == ERR)
9525 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9526 fc2737d5 2022-09-15 thomas } else {
9527 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9528 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9529 fc2737d5 2022-09-15 thomas wstandout(view->window);
9530 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9531 fc2737d5 2022-09-15 thomas view->ncols - 1);
9532 fc2737d5 2022-09-15 thomas if (rc == ERR)
9533 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9534 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9535 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9536 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9537 fc2737d5 2022-09-15 thomas if (rc == ERR)
9538 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9539 fc2737d5 2022-09-15 thomas }
9540 fc2737d5 2022-09-15 thomas wstandend(view->window);
9541 fc2737d5 2022-09-15 thomas }
9542 fc2737d5 2022-09-15 thomas
9543 fc2737d5 2022-09-15 thomas return NULL;
9544 fc2737d5 2022-09-15 thomas }
9545 fc2737d5 2022-09-15 thomas
9546 fc2737d5 2022-09-15 thomas static const struct got_error *
9547 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9548 fc2737d5 2022-09-15 thomas {
9549 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9550 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9551 fc2737d5 2022-09-15 thomas char *line = NULL;
9552 fc2737d5 2022-09-15 thomas ssize_t linelen;
9553 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9554 fc2737d5 2022-09-15 thomas int eos, nscroll;
9555 fc2737d5 2022-09-15 thomas
9556 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9557 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9558 fc2737d5 2022-09-15 thomas --eos; /* border */
9559 fc2737d5 2022-09-15 thomas
9560 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9561 fc2737d5 2022-09-15 thomas
9562 fc2737d5 2022-09-15 thomas switch (ch) {
9563 fc2737d5 2022-09-15 thomas case '0':
9564 fc2737d5 2022-09-15 thomas case '$':
9565 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9566 fc2737d5 2022-09-15 thomas case 'l':
9567 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9568 fc2737d5 2022-09-15 thomas case 'h':
9569 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9570 fc2737d5 2022-09-15 thomas break;
9571 fc2737d5 2022-09-15 thomas case 'g':
9572 fc2737d5 2022-09-15 thomas case KEY_HOME:
9573 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9574 fc2737d5 2022-09-15 thomas view->count = 0;
9575 fc2737d5 2022-09-15 thomas break;
9576 fc2737d5 2022-09-15 thomas case 'G':
9577 fc2737d5 2022-09-15 thomas case KEY_END:
9578 fc2737d5 2022-09-15 thomas view->count = 0;
9579 fc2737d5 2022-09-15 thomas if (s->eof)
9580 fc2737d5 2022-09-15 thomas break;
9581 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9582 fc2737d5 2022-09-15 thomas s->eof = 1;
9583 fc2737d5 2022-09-15 thomas break;
9584 fc2737d5 2022-09-15 thomas case 'k':
9585 fc2737d5 2022-09-15 thomas case KEY_UP:
9586 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9587 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9588 fc2737d5 2022-09-15 thomas else
9589 fc2737d5 2022-09-15 thomas view->count = 0;
9590 fc2737d5 2022-09-15 thomas break;
9591 fc2737d5 2022-09-15 thomas case CTRL('u'):
9592 fc2737d5 2022-09-15 thomas case 'u':
9593 fc2737d5 2022-09-15 thomas nscroll /= 2;
9594 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9595 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9596 fc2737d5 2022-09-15 thomas case CTRL('b'):
9597 fc2737d5 2022-09-15 thomas case 'b':
9598 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9599 fc2737d5 2022-09-15 thomas view->count = 0;
9600 fc2737d5 2022-09-15 thomas break;
9601 fc2737d5 2022-09-15 thomas }
9602 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9603 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9604 fc2737d5 2022-09-15 thomas break;
9605 fc2737d5 2022-09-15 thomas case 'j':
9606 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9607 fc2737d5 2022-09-15 thomas case CTRL('n'):
9608 fc2737d5 2022-09-15 thomas if (!s->eof)
9609 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9610 fc2737d5 2022-09-15 thomas else
9611 fc2737d5 2022-09-15 thomas view->count = 0;
9612 fc2737d5 2022-09-15 thomas break;
9613 fc2737d5 2022-09-15 thomas case CTRL('d'):
9614 fc2737d5 2022-09-15 thomas case 'd':
9615 fc2737d5 2022-09-15 thomas nscroll /= 2;
9616 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9617 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9618 fc2737d5 2022-09-15 thomas case CTRL('f'):
9619 fc2737d5 2022-09-15 thomas case 'f':
9620 fc2737d5 2022-09-15 thomas case ' ':
9621 fc2737d5 2022-09-15 thomas if (s->eof) {
9622 fc2737d5 2022-09-15 thomas view->count = 0;
9623 fc2737d5 2022-09-15 thomas break;
9624 fc2737d5 2022-09-15 thomas }
9625 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9626 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9627 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9628 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9629 fc2737d5 2022-09-15 thomas if (feof(s->f))
9630 fc2737d5 2022-09-15 thomas s->eof = 1;
9631 fc2737d5 2022-09-15 thomas else
9632 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9633 fc2737d5 2022-09-15 thomas break;
9634 fc2737d5 2022-09-15 thomas }
9635 fc2737d5 2022-09-15 thomas }
9636 fc2737d5 2022-09-15 thomas free(line);
9637 fc2737d5 2022-09-15 thomas break;
9638 fc2737d5 2022-09-15 thomas default:
9639 fc2737d5 2022-09-15 thomas view->count = 0;
9640 fc2737d5 2022-09-15 thomas break;
9641 fc2737d5 2022-09-15 thomas }
9642 fc2737d5 2022-09-15 thomas
9643 fc2737d5 2022-09-15 thomas return err;
9644 fc2737d5 2022-09-15 thomas }
9645 fc2737d5 2022-09-15 thomas
9646 fc2737d5 2022-09-15 thomas static const struct got_error *
9647 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9648 fc2737d5 2022-09-15 thomas {
9649 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9650 fc2737d5 2022-09-15 thomas
9651 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9652 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9653 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9654 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9655 fc2737d5 2022-09-15 thomas
9656 fc2737d5 2022-09-15 thomas return NULL;
9657 fc2737d5 2022-09-15 thomas }
9658 fc2737d5 2022-09-15 thomas
9659 fc2737d5 2022-09-15 thomas static const struct got_error *
9660 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9661 fc2737d5 2022-09-15 thomas {
9662 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9663 fc2737d5 2022-09-15 thomas
9664 fc2737d5 2022-09-15 thomas
9665 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9666 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9667 fc2737d5 2022-09-15 thomas
9668 fc2737d5 2022-09-15 thomas wclear(view->window);
9669 fc2737d5 2022-09-15 thomas view->count = 0;
9670 fc2737d5 2022-09-15 thomas view->x = 0;
9671 fc2737d5 2022-09-15 thomas s->all = !s->all;
9672 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9673 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9674 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9675 fc2737d5 2022-09-15 thomas
9676 fc2737d5 2022-09-15 thomas return create_help(s);
9677 fc2737d5 2022-09-15 thomas }
9678 fc2737d5 2022-09-15 thomas
9679 fc2737d5 2022-09-15 thomas static const struct got_error *
9680 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9681 fc2737d5 2022-09-15 thomas {
9682 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9683 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9684 fc2737d5 2022-09-15 thomas
9685 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9686 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9687 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9688 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9689 fc2737d5 2022-09-15 thomas
9690 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9691 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9692 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9693 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9694 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9695 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9696 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9697 fc2737d5 2022-09-15 thomas
9698 fc2737d5 2022-09-15 thomas err = create_help(s);
9699 fc2737d5 2022-09-15 thomas return err;
9700 fc2737d5 2022-09-15 thomas }
9701 fc2737d5 2022-09-15 thomas
9702 fc2737d5 2022-09-15 thomas static const struct got_error *
9703 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9704 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9705 2a31b33b 2022-07-23 thomas {
9706 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9707 2a31b33b 2022-07-23 thomas
9708 2a31b33b 2022-07-23 thomas *new_view = NULL;
9709 2a31b33b 2022-07-23 thomas
9710 2a31b33b 2022-07-23 thomas switch (request) {
9711 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9712 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9713 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9714 2a31b33b 2022-07-23 thomas
9715 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9716 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9717 2a31b33b 2022-07-23 thomas view, s->repo);
9718 2a31b33b 2022-07-23 thomas } else
9719 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9720 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9721 2a31b33b 2022-07-23 thomas break;
9722 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9723 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9724 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9725 2a31b33b 2022-07-23 thomas
9726 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9727 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9728 2a31b33b 2022-07-23 thomas s->repo);
9729 2a31b33b 2022-07-23 thomas } else
9730 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9731 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9732 2a31b33b 2022-07-23 thomas break;
9733 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9734 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9735 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9736 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9737 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9738 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9739 2a31b33b 2022-07-23 thomas &view->state.tree);
9740 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9741 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9742 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9743 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9744 2a31b33b 2022-07-23 thomas else
9745 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9746 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9747 2a31b33b 2022-07-23 thomas break;
9748 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9749 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9750 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9751 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9752 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9753 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9754 2a31b33b 2022-07-23 thomas view->state.log.repo);
9755 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9756 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9757 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9758 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9759 2a31b33b 2022-07-23 thomas else
9760 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9761 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9762 2a31b33b 2022-07-23 thomas break;
9763 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9764 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9765 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9766 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9767 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9768 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9769 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9770 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9771 2a31b33b 2022-07-23 thomas else
9772 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9773 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9774 2a31b33b 2022-07-23 thomas if (err)
9775 2a31b33b 2022-07-23 thomas view_close(*new_view);
9776 2a31b33b 2022-07-23 thomas break;
9777 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9778 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9779 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9780 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9781 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9782 fc2737d5 2022-09-15 thomas if (err)
9783 fc2737d5 2022-09-15 thomas view_close(*new_view);
9784 fc2737d5 2022-09-15 thomas break;
9785 2a31b33b 2022-07-23 thomas default:
9786 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9787 2a31b33b 2022-07-23 thomas }
9788 2a31b33b 2022-07-23 thomas
9789 2a31b33b 2022-07-23 thomas return err;
9790 2a31b33b 2022-07-23 thomas }
9791 2a31b33b 2022-07-23 thomas
9792 a5d43cac 2022-07-01 thomas /*
9793 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9794 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9795 a5d43cac 2022-07-01 thomas */
9796 6458efa5 2020-11-24 stsp static void
9797 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9798 a5d43cac 2022-07-01 thomas {
9799 a5d43cac 2022-07-01 thomas switch (view->type) {
9800 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9801 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9802 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9803 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9804 a5d43cac 2022-07-01 thomas 1);
9805 a5d43cac 2022-07-01 thomas break;
9806 a5d43cac 2022-07-01 thomas }
9807 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9808 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9809 a5d43cac 2022-07-01 thomas else
9810 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9811 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9812 a5d43cac 2022-07-01 thomas break;
9813 a5d43cac 2022-07-01 thomas }
9814 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9815 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9816 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9817 a5d43cac 2022-07-01 thomas break;
9818 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9819 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9820 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9821 a5d43cac 2022-07-01 thomas break;
9822 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9823 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9824 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9825 a5d43cac 2022-07-01 thomas break;
9826 a5d43cac 2022-07-01 thomas default:
9827 a5d43cac 2022-07-01 thomas break;
9828 a5d43cac 2022-07-01 thomas }
9829 a5d43cac 2022-07-01 thomas
9830 a5d43cac 2022-07-01 thomas view->offset = 0;
9831 a5d43cac 2022-07-01 thomas }
9832 a5d43cac 2022-07-01 thomas
9833 a5d43cac 2022-07-01 thomas /*
9834 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9835 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9836 a5d43cac 2022-07-01 thomas */
9837 a5d43cac 2022-07-01 thomas static const struct got_error *
9838 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9839 a5d43cac 2022-07-01 thomas {
9840 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9841 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9842 a5d43cac 2022-07-01 thomas int *selected = NULL;
9843 a5d43cac 2022-07-01 thomas int header, offset;
9844 a5d43cac 2022-07-01 thomas
9845 a5d43cac 2022-07-01 thomas switch (view->type) {
9846 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9847 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9848 a5d43cac 2022-07-01 thomas header = 3;
9849 a5d43cac 2022-07-01 thomas scrolld = NULL;
9850 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9851 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9852 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9853 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9854 a5d43cac 2022-07-01 thomas view->offset = offset;
9855 a5d43cac 2022-07-01 thomas }
9856 a5d43cac 2022-07-01 thomas break;
9857 a5d43cac 2022-07-01 thomas }
9858 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9859 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9860 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9861 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9862 a5d43cac 2022-07-01 thomas selected = &s->selected;
9863 a5d43cac 2022-07-01 thomas break;
9864 a5d43cac 2022-07-01 thomas }
9865 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9866 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9867 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9868 a5d43cac 2022-07-01 thomas header = 3;
9869 a5d43cac 2022-07-01 thomas selected = &s->selected;
9870 a5d43cac 2022-07-01 thomas break;
9871 a5d43cac 2022-07-01 thomas }
9872 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9873 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9874 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9875 a5d43cac 2022-07-01 thomas header = 5;
9876 a5d43cac 2022-07-01 thomas selected = &s->selected;
9877 a5d43cac 2022-07-01 thomas break;
9878 a5d43cac 2022-07-01 thomas }
9879 a5d43cac 2022-07-01 thomas default:
9880 a5d43cac 2022-07-01 thomas selected = NULL;
9881 a5d43cac 2022-07-01 thomas scrolld = NULL;
9882 a5d43cac 2022-07-01 thomas header = 0;
9883 a5d43cac 2022-07-01 thomas break;
9884 a5d43cac 2022-07-01 thomas }
9885 a5d43cac 2022-07-01 thomas
9886 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9887 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9888 a5d43cac 2022-07-01 thomas view->offset = offset;
9889 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9890 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9891 a5d43cac 2022-07-01 thomas *selected -= offset;
9892 a5d43cac 2022-07-01 thomas }
9893 a5d43cac 2022-07-01 thomas }
9894 a5d43cac 2022-07-01 thomas
9895 a5d43cac 2022-07-01 thomas return err;
9896 a5d43cac 2022-07-01 thomas }
9897 a5d43cac 2022-07-01 thomas
9898 a5d43cac 2022-07-01 thomas static void
9899 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9900 ce5b7c56 2019-07-09 stsp {
9901 6059809a 2020-12-17 stsp size_t i;
9902 9f7d7167 2018-04-29 stsp
9903 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9904 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9905 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9906 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9907 ce5b7c56 2019-07-09 stsp }
9908 6879ba42 2020-10-01 naddy fputc('\n', fp);
9909 ce5b7c56 2019-07-09 stsp }
9910 ce5b7c56 2019-07-09 stsp
9911 4ed7e80c 2018-05-20 stsp __dead static void
9912 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9913 9f7d7167 2018-04-29 stsp {
9914 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9915 6879ba42 2020-10-01 naddy
9916 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9917 6879ba42 2020-10-01 naddy getprogname());
9918 6879ba42 2020-10-01 naddy if (hflag) {
9919 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9920 6879ba42 2020-10-01 naddy list_commands(fp);
9921 ee85c5e8 2020-02-29 stsp }
9922 6879ba42 2020-10-01 naddy exit(status);
9923 9f7d7167 2018-04-29 stsp }
9924 9f7d7167 2018-04-29 stsp
9925 c2301be8 2018-04-30 stsp static char **
9926 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9927 c2301be8 2018-04-30 stsp {
9928 ee85c5e8 2020-02-29 stsp va_list ap;
9929 c2301be8 2018-04-30 stsp char **argv;
9930 ee85c5e8 2020-02-29 stsp int i;
9931 c2301be8 2018-04-30 stsp
9932 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9933 ee85c5e8 2020-02-29 stsp
9934 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9935 c2301be8 2018-04-30 stsp if (argv == NULL)
9936 c2301be8 2018-04-30 stsp err(1, "calloc");
9937 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9938 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9939 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9940 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9941 c2301be8 2018-04-30 stsp }
9942 c2301be8 2018-04-30 stsp
9943 ee85c5e8 2020-02-29 stsp va_end(ap);
9944 c2301be8 2018-04-30 stsp return argv;
9945 ee85c5e8 2020-02-29 stsp }
9946 ee85c5e8 2020-02-29 stsp
9947 ee85c5e8 2020-02-29 stsp /*
9948 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9949 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9950 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9951 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9952 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9953 ee85c5e8 2020-02-29 stsp */
9954 ee85c5e8 2020-02-29 stsp static const struct got_error *
9955 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9956 ee85c5e8 2020-02-29 stsp {
9957 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9958 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9959 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9960 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9961 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9962 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9963 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9964 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9965 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9966 84de9106 2020-12-26 stsp
9967 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9968 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9969 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9970 ee85c5e8 2020-02-29 stsp
9971 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9972 7cd52833 2022-06-23 thomas if (error != NULL)
9973 7cd52833 2022-06-23 thomas goto done;
9974 7cd52833 2022-06-23 thomas
9975 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
9976 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9977 ee85c5e8 2020-02-29 stsp goto done;
9978 ee85c5e8 2020-02-29 stsp
9979 ee85c5e8 2020-02-29 stsp if (worktree)
9980 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9981 ee85c5e8 2020-02-29 stsp else
9982 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9983 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9984 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9985 ee85c5e8 2020-02-29 stsp goto done;
9986 ee85c5e8 2020-02-29 stsp }
9987 ee85c5e8 2020-02-29 stsp
9988 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9989 ee85c5e8 2020-02-29 stsp if (error != NULL)
9990 ee85c5e8 2020-02-29 stsp goto done;
9991 ee85c5e8 2020-02-29 stsp
9992 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9993 ee85c5e8 2020-02-29 stsp repo, worktree);
9994 ee85c5e8 2020-02-29 stsp if (error)
9995 ee85c5e8 2020-02-29 stsp goto done;
9996 ee85c5e8 2020-02-29 stsp
9997 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9998 84de9106 2020-12-26 stsp if (error)
9999 84de9106 2020-12-26 stsp goto done;
10000 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
10001 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
10002 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
10003 ee85c5e8 2020-02-29 stsp if (error)
10004 ee85c5e8 2020-02-29 stsp goto done;
10005 ee85c5e8 2020-02-29 stsp
10006 ee85c5e8 2020-02-29 stsp if (worktree) {
10007 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10008 ee85c5e8 2020-02-29 stsp worktree = NULL;
10009 ee85c5e8 2020-02-29 stsp }
10010 ee85c5e8 2020-02-29 stsp
10011 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10012 945f9229 2022-04-16 thomas if (error)
10013 945f9229 2022-04-16 thomas goto done;
10014 945f9229 2022-04-16 thomas
10015 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
10016 ee85c5e8 2020-02-29 stsp if (error) {
10017 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
10018 ee85c5e8 2020-02-29 stsp goto done;
10019 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
10020 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
10021 6879ba42 2020-10-01 naddy usage(1, 1);
10022 ee85c5e8 2020-02-29 stsp /* not reached */
10023 ee85c5e8 2020-02-29 stsp }
10024 ee85c5e8 2020-02-29 stsp
10025 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
10026 ee85c5e8 2020-02-29 stsp if (error)
10027 ee85c5e8 2020-02-29 stsp goto done;
10028 ee85c5e8 2020-02-29 stsp
10029 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
10030 ee85c5e8 2020-02-29 stsp argc = 4;
10031 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
10032 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
10033 ee85c5e8 2020-02-29 stsp done:
10034 1d0f4054 2021-06-17 stsp if (repo) {
10035 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
10036 1d0f4054 2021-06-17 stsp if (error == NULL)
10037 1d0f4054 2021-06-17 stsp error = close_err;
10038 1d0f4054 2021-06-17 stsp }
10039 945f9229 2022-04-16 thomas if (commit)
10040 945f9229 2022-04-16 thomas got_object_commit_close(commit);
10041 ee85c5e8 2020-02-29 stsp if (worktree)
10042 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10043 7cd52833 2022-06-23 thomas if (pack_fds) {
10044 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
10045 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
10046 7cd52833 2022-06-23 thomas if (error == NULL)
10047 7cd52833 2022-06-23 thomas error = pack_err;
10048 7cd52833 2022-06-23 thomas }
10049 ee85c5e8 2020-02-29 stsp free(id);
10050 ee85c5e8 2020-02-29 stsp free(commit_id_str);
10051 ee85c5e8 2020-02-29 stsp free(commit_id);
10052 ee85c5e8 2020-02-29 stsp free(cwd);
10053 ee85c5e8 2020-02-29 stsp free(repo_path);
10054 ee85c5e8 2020-02-29 stsp free(in_repo_path);
10055 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
10056 ee85c5e8 2020-02-29 stsp int i;
10057 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
10058 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
10059 ee85c5e8 2020-02-29 stsp free(cmd_argv);
10060 ee85c5e8 2020-02-29 stsp }
10061 87670572 2020-12-26 naddy tog_free_refs();
10062 ee85c5e8 2020-02-29 stsp return error;
10063 c2301be8 2018-04-30 stsp }
10064 c2301be8 2018-04-30 stsp
10065 9f7d7167 2018-04-29 stsp int
10066 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
10067 9f7d7167 2018-04-29 stsp {
10068 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
10069 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
10070 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
10071 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
10072 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
10073 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
10074 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
10075 83cd27f8 2020-01-13 stsp };
10076 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
10077 557d3365 2023-04-14 thomas const char *test_script_path;
10078 a0abeae5 2023-02-17 thomas
10079 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
10080 a0abeae5 2023-02-17 thomas
10081 557d3365 2023-04-14 thomas /*
10082 e2a79cf9 2023-07-26 thomas * Override default signal handlers before starting ncurses.
10083 e2a79cf9 2023-07-26 thomas * This should prevent ncurses from installing its own
10084 e2a79cf9 2023-07-26 thomas * broken cleanup() signal handler.
10085 e2a79cf9 2023-07-26 thomas */
10086 e2a79cf9 2023-07-26 thomas signal(SIGWINCH, tog_sigwinch);
10087 e2a79cf9 2023-07-26 thomas signal(SIGPIPE, tog_sigpipe);
10088 e2a79cf9 2023-07-26 thomas signal(SIGCONT, tog_sigcont);
10089 e2a79cf9 2023-07-26 thomas signal(SIGINT, tog_sigint);
10090 e2a79cf9 2023-07-26 thomas signal(SIGTERM, tog_sigterm);
10091 e2a79cf9 2023-07-26 thomas
10092 e2a79cf9 2023-07-26 thomas /*
10093 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
10094 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
10095 557d3365 2023-04-14 thomas */
10096 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
10097 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
10098 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
10099 557d3365 2023-04-14 thomas if (error) {
10100 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10101 557d3365 2023-04-14 thomas return 1;
10102 557d3365 2023-04-14 thomas }
10103 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
10104 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
10105 557d3365 2023-04-14 thomas
10106 557d3365 2023-04-14 thomas #if !defined(PROFILE)
10107 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10108 a0abeae5 2023-02-17 thomas NULL) == -1)
10109 a0abeae5 2023-02-17 thomas err(1, "pledge");
10110 a0abeae5 2023-02-17 thomas #endif
10111 9f7d7167 2018-04-29 stsp
10112 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10113 9f7d7167 2018-04-29 stsp switch (ch) {
10114 9f7d7167 2018-04-29 stsp case 'h':
10115 9f7d7167 2018-04-29 stsp hflag = 1;
10116 9f7d7167 2018-04-29 stsp break;
10117 53ccebc2 2019-07-30 stsp case 'V':
10118 53ccebc2 2019-07-30 stsp Vflag = 1;
10119 53ccebc2 2019-07-30 stsp break;
10120 9f7d7167 2018-04-29 stsp default:
10121 6879ba42 2020-10-01 naddy usage(hflag, 1);
10122 9f7d7167 2018-04-29 stsp /* NOTREACHED */
10123 9f7d7167 2018-04-29 stsp }
10124 9f7d7167 2018-04-29 stsp }
10125 9f7d7167 2018-04-29 stsp
10126 9f7d7167 2018-04-29 stsp argc -= optind;
10127 9f7d7167 2018-04-29 stsp argv += optind;
10128 9814e6a3 2020-09-27 naddy optind = 1;
10129 c2301be8 2018-04-30 stsp optreset = 1;
10130 9f7d7167 2018-04-29 stsp
10131 53ccebc2 2019-07-30 stsp if (Vflag) {
10132 53ccebc2 2019-07-30 stsp got_version_print_str();
10133 6879ba42 2020-10-01 naddy return 0;
10134 53ccebc2 2019-07-30 stsp }
10135 4010e238 2020-12-04 stsp
10136 c2301be8 2018-04-30 stsp if (argc == 0) {
10137 f29d3e89 2018-06-23 stsp if (hflag)
10138 6879ba42 2020-10-01 naddy usage(hflag, 0);
10139 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
10140 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
10141 c2301be8 2018-04-30 stsp argc = 1;
10142 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
10143 c2301be8 2018-04-30 stsp } else {
10144 6059809a 2020-12-17 stsp size_t i;
10145 9f7d7167 2018-04-29 stsp
10146 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
10147 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
10148 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
10149 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
10150 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
10151 9f7d7167 2018-04-29 stsp break;
10152 9f7d7167 2018-04-29 stsp }
10153 9f7d7167 2018-04-29 stsp }
10154 ee85c5e8 2020-02-29 stsp }
10155 3642c4c6 2019-07-09 stsp
10156 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10157 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
10158 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
10159 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10160 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
10161 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10162 adf4c9e0 2022-07-03 thomas }
10163 adf4c9e0 2022-07-03 thomas
10164 92845f09 2023-07-26 thomas tog_base_commit.idx = -1;
10165 92845f09 2023-07-26 thomas tog_base_commit.marker = GOT_WORKTREE_STATE_UNKNOWN;
10166 92845f09 2023-07-26 thomas
10167 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
10168 ee85c5e8 2020-02-29 stsp if (argc != 1)
10169 6879ba42 2020-10-01 naddy usage(0, 1);
10170 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
10171 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
10172 ee85c5e8 2020-02-29 stsp } else {
10173 ee85c5e8 2020-02-29 stsp if (hflag)
10174 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
10175 ee85c5e8 2020-02-29 stsp else
10176 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10177 9f7d7167 2018-04-29 stsp }
10178 9f7d7167 2018-04-29 stsp
10179 1d98034b 2023-04-14 thomas if (using_mock_io) {
10180 1d98034b 2023-04-14 thomas io_err = tog_io_close();
10181 1d98034b 2023-04-14 thomas if (error == NULL)
10182 1d98034b 2023-04-14 thomas error = io_err;
10183 1d98034b 2023-04-14 thomas }
10184 9f7d7167 2018-04-29 stsp endwin();
10185 a2f4a359 2020-02-28 stsp if (cmd_argv) {
10186 a2f4a359 2020-02-28 stsp int i;
10187 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
10188 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
10189 a2f4a359 2020-02-28 stsp free(cmd_argv);
10190 a2f4a359 2020-02-28 stsp }
10191 a2f4a359 2020-02-28 stsp
10192 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
10193 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
10194 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
10195 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
10196 66b04f8f 2023-07-19 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10197 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10198 66b04f8f 2023-07-19 thomas return 1;
10199 66b04f8f 2023-07-19 thomas }
10200 9f7d7167 2018-04-29 stsp return 0;
10201 9f7d7167 2018-04-29 stsp }