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 f517f81a 2023-07-23 thomas entry->idx == tog_base_commit.idx) {
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 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3729 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3730 60493ae3 2019-06-20 stsp }
3731 60493ae3 2019-06-20 stsp
3732 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3733 13add988 2019-10-15 stsp &view->regex);
3734 5943eee2 2019-08-13 stsp if (err)
3735 13add988 2019-10-15 stsp break;
3736 13add988 2019-10-15 stsp if (have_match) {
3737 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3738 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3739 60493ae3 2019-06-20 stsp break;
3740 60493ae3 2019-06-20 stsp }
3741 13add988 2019-10-15 stsp
3742 96e2b566 2019-07-08 stsp s->search_entry = entry;
3743 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3744 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3745 b1bf1435 2019-06-21 stsp else
3746 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3747 60493ae3 2019-06-20 stsp }
3748 60493ae3 2019-06-20 stsp
3749 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3750 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3751 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3752 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3753 60493ae3 2019-06-20 stsp if (err)
3754 60493ae3 2019-06-20 stsp return err;
3755 ead14cbe 2019-06-21 stsp cur++;
3756 ead14cbe 2019-06-21 stsp }
3757 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3758 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3759 60493ae3 2019-06-20 stsp if (err)
3760 60493ae3 2019-06-20 stsp return err;
3761 ead14cbe 2019-06-21 stsp cur--;
3762 60493ae3 2019-06-20 stsp }
3763 60493ae3 2019-06-20 stsp }
3764 60493ae3 2019-06-20 stsp
3765 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3766 96e2b566 2019-07-08 stsp
3767 60493ae3 2019-06-20 stsp return NULL;
3768 60493ae3 2019-06-20 stsp }
3769 60493ae3 2019-06-20 stsp
3770 60493ae3 2019-06-20 stsp static const struct got_error *
3771 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3772 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3773 92845f09 2023-07-26 thomas const char *in_repo_path, int log_branches,
3774 92845f09 2023-07-26 thomas struct got_worktree *worktree)
3775 80ddbec8 2018-04-29 stsp {
3776 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3777 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3778 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3779 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3780 1a76625f 2018-10-22 stsp int errcode;
3781 80ddbec8 2018-04-29 stsp
3782 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3783 f135c941 2020-02-20 stsp free(s->in_repo_path);
3784 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3785 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3786 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3787 51b7e1c3 2023-04-14 thomas goto done;
3788 51b7e1c3 2023-04-14 thomas }
3789 f135c941 2020-02-20 stsp }
3790 ecb28ae0 2018-07-16 stsp
3791 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3792 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3793 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3794 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3795 78756c87 2020-11-24 stsp
3796 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3797 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3798 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3799 7e8004ba 2022-09-11 thomas
3800 fb2756b9 2018-08-04 stsp s->repo = repo;
3801 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3802 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3803 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3804 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3805 9cd7cbd1 2020-12-07 stsp goto done;
3806 9cd7cbd1 2020-12-07 stsp }
3807 9cd7cbd1 2020-12-07 stsp }
3808 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3809 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3810 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3811 5036bf37 2018-09-24 stsp goto done;
3812 5036bf37 2018-09-24 stsp }
3813 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3814 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3815 e5a0f69f 2018-08-18 stsp
3816 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3817 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3818 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3819 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3820 11b20872 2019-11-08 stsp if (err)
3821 11b20872 2019-11-08 stsp goto done;
3822 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3823 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3824 11b20872 2019-11-08 stsp if (err) {
3825 11b20872 2019-11-08 stsp free_colors(&s->colors);
3826 11b20872 2019-11-08 stsp goto done;
3827 11b20872 2019-11-08 stsp }
3828 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3829 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3830 11b20872 2019-11-08 stsp if (err) {
3831 11b20872 2019-11-08 stsp free_colors(&s->colors);
3832 11b20872 2019-11-08 stsp goto done;
3833 11b20872 2019-11-08 stsp }
3834 11b20872 2019-11-08 stsp }
3835 11b20872 2019-11-08 stsp
3836 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3837 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3838 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3839 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3840 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3841 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3842 1a76625f 2018-10-22 stsp
3843 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3844 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3845 1af5eddf 2022-06-23 thomas if (err)
3846 1af5eddf 2022-06-23 thomas goto done;
3847 1af5eddf 2022-06-23 thomas }
3848 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3849 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3850 7cd52833 2022-06-23 thomas if (err)
3851 7cd52833 2022-06-23 thomas goto done;
3852 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3853 b672a97a 2020-01-27 stsp !s->log_branches);
3854 1a76625f 2018-10-22 stsp if (err)
3855 1a76625f 2018-10-22 stsp goto done;
3856 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3857 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3858 c5b78334 2020-01-12 stsp if (err)
3859 c5b78334 2020-01-12 stsp goto done;
3860 1a76625f 2018-10-22 stsp
3861 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3862 1a76625f 2018-10-22 stsp if (errcode) {
3863 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3864 1a76625f 2018-10-22 stsp goto done;
3865 1a76625f 2018-10-22 stsp }
3866 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3867 7c1452c1 2020-03-26 stsp if (errcode) {
3868 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3869 7c1452c1 2020-03-26 stsp goto done;
3870 7c1452c1 2020-03-26 stsp }
3871 1a76625f 2018-10-22 stsp
3872 92845f09 2023-07-26 thomas if (using_mock_io) {
3873 92845f09 2023-07-26 thomas int rc;
3874 92845f09 2023-07-26 thomas
3875 92845f09 2023-07-26 thomas rc = pthread_cond_init(&s->thread_args.log_loaded, NULL);
3876 92845f09 2023-07-26 thomas if (rc)
3877 92845f09 2023-07-26 thomas return got_error_set_errno(rc, "pthread_cond_init");
3878 92845f09 2023-07-26 thomas }
3879 92845f09 2023-07-26 thomas
3880 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3881 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3882 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3883 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3884 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3885 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3886 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3887 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3888 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3889 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3890 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3891 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3892 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3893 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3894 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3895 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3896 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3897 92845f09 2023-07-26 thomas s->thread_args.worktree = worktree;
3898 92845f09 2023-07-26 thomas if (worktree)
3899 92845f09 2023-07-26 thomas s->thread_args.need_commit_marker = 1;
3900 ba4f502b 2018-08-04 stsp done:
3901 51b7e1c3 2023-04-14 thomas if (err) {
3902 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3903 51b7e1c3 2023-04-14 thomas close_log_view(view);
3904 51b7e1c3 2023-04-14 thomas view_close(view);
3905 51b7e1c3 2023-04-14 thomas }
3906 ba4f502b 2018-08-04 stsp return err;
3907 ba4f502b 2018-08-04 stsp }
3908 ba4f502b 2018-08-04 stsp
3909 e5a0f69f 2018-08-18 stsp static const struct got_error *
3910 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3911 ba4f502b 2018-08-04 stsp {
3912 f2f6d207 2020-11-24 stsp const struct got_error *err;
3913 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3914 ba4f502b 2018-08-04 stsp
3915 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3916 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3917 2b380cc8 2018-10-24 stsp &s->thread_args);
3918 2b380cc8 2018-10-24 stsp if (errcode)
3919 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3920 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3921 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3922 f2f6d207 2020-11-24 stsp if (err)
3923 f2f6d207 2020-11-24 stsp return err;
3924 f2f6d207 2020-11-24 stsp }
3925 2b380cc8 2018-10-24 stsp }
3926 2b380cc8 2018-10-24 stsp
3927 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3928 b31f89ff 2022-07-01 thomas }
3929 b31f89ff 2022-07-01 thomas
3930 b31f89ff 2022-07-01 thomas static void
3931 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3932 b31f89ff 2022-07-01 thomas {
3933 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3934 b31f89ff 2022-07-01 thomas
3935 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3936 b31f89ff 2022-07-01 thomas return;
3937 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3938 7e8004ba 2022-09-11 thomas view->count = 0;
3939 b31f89ff 2022-07-01 thomas
3940 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3941 b31f89ff 2022-07-01 thomas || home)
3942 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3943 b31f89ff 2022-07-01 thomas
3944 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3945 b31f89ff 2022-07-01 thomas --s->selected;
3946 b31f89ff 2022-07-01 thomas else
3947 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3948 b31f89ff 2022-07-01 thomas
3949 b31f89ff 2022-07-01 thomas select_commit(s);
3950 b31f89ff 2022-07-01 thomas return;
3951 b31f89ff 2022-07-01 thomas }
3952 b31f89ff 2022-07-01 thomas
3953 b31f89ff 2022-07-01 thomas static const struct got_error *
3954 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3955 b31f89ff 2022-07-01 thomas {
3956 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3957 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3958 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3959 b31f89ff 2022-07-01 thomas
3960 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3961 7e8004ba 2022-09-11 thomas return NULL;
3962 7e8004ba 2022-09-11 thomas
3963 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3964 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3965 b31f89ff 2022-07-01 thomas return NULL;
3966 b31f89ff 2022-07-01 thomas
3967 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3968 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3969 b31f89ff 2022-07-01 thomas
3970 7ed048bd 2022-08-12 thomas if (!page) {
3971 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3972 b31f89ff 2022-07-01 thomas ++s->selected;
3973 b31f89ff 2022-07-01 thomas else
3974 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3975 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3976 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3977 7ed048bd 2022-08-12 thomas int n;
3978 7ed048bd 2022-08-12 thomas
3979 7ed048bd 2022-08-12 thomas s->selected = 0;
3980 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3981 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3982 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3983 7ed048bd 2022-08-12 thomas if (entry == NULL)
3984 7ed048bd 2022-08-12 thomas break;
3985 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3986 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3987 7ed048bd 2022-08-12 thomas }
3988 7ed048bd 2022-08-12 thomas if (n > 0)
3989 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3990 b31f89ff 2022-07-01 thomas } else {
3991 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3992 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3993 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3994 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3995 bd3f8225 2022-08-12 thomas else
3996 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3997 b31f89ff 2022-07-01 thomas }
3998 b31f89ff 2022-07-01 thomas if (err)
3999 b31f89ff 2022-07-01 thomas return err;
4000 b31f89ff 2022-07-01 thomas
4001 a5d43cac 2022-07-01 thomas /*
4002 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
4003 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
4004 a5d43cac 2022-07-01 thomas */
4005 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
4006 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
4007 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
4008 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
4009 25100026 2022-08-13 thomas }
4010 a5d43cac 2022-07-01 thomas
4011 b31f89ff 2022-07-01 thomas select_commit(s);
4012 b31f89ff 2022-07-01 thomas
4013 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
4014 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
4015 b31f89ff 2022-07-01 thomas view->count = 0;
4016 b31f89ff 2022-07-01 thomas
4017 b31f89ff 2022-07-01 thomas return NULL;
4018 e5a0f69f 2018-08-18 stsp }
4019 04cc582a 2018-08-01 stsp
4020 a5d43cac 2022-07-01 thomas static void
4021 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
4022 a5d43cac 2022-07-01 thomas {
4023 24415785 2022-07-03 thomas *x = 0;
4024 24415785 2022-07-03 thomas *y = 0;
4025 24415785 2022-07-03 thomas
4026 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
4027 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
4028 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
4029 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
4030 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
4031 53d2bdd3 2022-07-10 thomas else
4032 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
4033 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
4034 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
4035 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
4036 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
4037 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
4038 53d2bdd3 2022-07-10 thomas else
4039 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
4040 53d2bdd3 2022-07-10 thomas }
4041 a5d43cac 2022-07-01 thomas }
4042 a5d43cac 2022-07-01 thomas
4043 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
4044 e5a0f69f 2018-08-18 stsp static const struct got_error *
4045 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
4046 a5d43cac 2022-07-01 thomas {
4047 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
4048 a5d43cac 2022-07-01 thomas
4049 a5d43cac 2022-07-01 thomas view->nlines = y;
4050 64486692 2022-07-07 thomas view->ncols = COLS;
4051 a5d43cac 2022-07-01 thomas err = view_resize(view);
4052 a5d43cac 2022-07-01 thomas if (err)
4053 a5d43cac 2022-07-01 thomas return err;
4054 a5d43cac 2022-07-01 thomas
4055 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
4056 a5d43cac 2022-07-01 thomas
4057 a5d43cac 2022-07-01 thomas return err;
4058 07dd3ed3 2022-08-06 thomas }
4059 07dd3ed3 2022-08-06 thomas
4060 07dd3ed3 2022-08-06 thomas static const struct got_error *
4061 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
4062 07dd3ed3 2022-08-06 thomas {
4063 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
4064 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
4065 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
4066 25100026 2022-08-13 thomas
4067 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
4068 25100026 2022-08-13 thomas return NULL;
4069 07dd3ed3 2022-08-06 thomas
4070 07dd3ed3 2022-08-06 thomas g = view->gline;
4071 07dd3ed3 2022-08-06 thomas view->gline = 0;
4072 07dd3ed3 2022-08-06 thomas
4073 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
4074 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
4075 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
4076 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
4077 07dd3ed3 2022-08-06 thomas select_commit(s);
4078 07dd3ed3 2022-08-06 thomas return NULL;
4079 07dd3ed3 2022-08-06 thomas }
4080 07dd3ed3 2022-08-06 thomas
4081 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
4082 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
4083 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
4084 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
4085 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
4086 07dd3ed3 2022-08-06 thomas if (err)
4087 07dd3ed3 2022-08-06 thomas return err;
4088 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
4089 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
4090 07dd3ed3 2022-08-06 thomas
4091 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
4092 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
4093 07dd3ed3 2022-08-06 thomas
4094 07dd3ed3 2022-08-06 thomas select_commit(s);
4095 07dd3ed3 2022-08-06 thomas return NULL;
4096 c72de8ab 2023-02-03 thomas
4097 c72de8ab 2023-02-03 thomas }
4098 c72de8ab 2023-02-03 thomas
4099 c72de8ab 2023-02-03 thomas static void
4100 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
4101 c72de8ab 2023-02-03 thomas {
4102 07dd3ed3 2022-08-06 thomas
4103 c72de8ab 2023-02-03 thomas switch (ch) {
4104 c72de8ab 2023-02-03 thomas case KEY_LEFT:
4105 c72de8ab 2023-02-03 thomas case 'h':
4106 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
4107 c72de8ab 2023-02-03 thomas if (view->x <= 0)
4108 c72de8ab 2023-02-03 thomas view->count = 0;
4109 c72de8ab 2023-02-03 thomas break;
4110 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
4111 c72de8ab 2023-02-03 thomas case 'l':
4112 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
4113 c72de8ab 2023-02-03 thomas view->x += 2;
4114 c72de8ab 2023-02-03 thomas else
4115 c72de8ab 2023-02-03 thomas view->count = 0;
4116 c72de8ab 2023-02-03 thomas break;
4117 c72de8ab 2023-02-03 thomas case '0':
4118 c72de8ab 2023-02-03 thomas view->x = 0;
4119 c72de8ab 2023-02-03 thomas break;
4120 c72de8ab 2023-02-03 thomas case '$':
4121 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
4122 c72de8ab 2023-02-03 thomas view->count = 0;
4123 c72de8ab 2023-02-03 thomas break;
4124 c72de8ab 2023-02-03 thomas default:
4125 c72de8ab 2023-02-03 thomas break;
4126 c72de8ab 2023-02-03 thomas }
4127 a5d43cac 2022-07-01 thomas }
4128 a5d43cac 2022-07-01 thomas
4129 a5d43cac 2022-07-01 thomas static const struct got_error *
4130 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4131 e5a0f69f 2018-08-18 stsp {
4132 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4133 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4134 7ed048bd 2022-08-12 thomas int eos, nscroll;
4135 80ddbec8 2018-04-29 stsp
4136 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4137 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4138 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4139 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4140 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4141 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4142 fb280deb 2021-08-30 stsp }
4143 c0be8933 2022-08-12 thomas if (err)
4144 c0be8933 2022-08-12 thomas return err;
4145 528dedf3 2021-08-30 stsp }
4146 068ab281 2022-07-01 thomas
4147 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4148 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4149 068ab281 2022-07-01 thomas --eos; /* border */
4150 07dd3ed3 2022-08-06 thomas
4151 07dd3ed3 2022-08-06 thomas if (view->gline)
4152 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4153 068ab281 2022-07-01 thomas
4154 528dedf3 2021-08-30 stsp switch (ch) {
4155 7e8004ba 2022-09-11 thomas case '&':
4156 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4157 7e8004ba 2022-09-11 thomas break;
4158 1e37a5c2 2019-05-12 jcs case 'q':
4159 1e37a5c2 2019-05-12 jcs s->quit = 1;
4160 05171be4 2022-06-23 thomas break;
4161 05171be4 2022-06-23 thomas case '0':
4162 05171be4 2022-06-23 thomas case '$':
4163 05171be4 2022-06-23 thomas case KEY_RIGHT:
4164 05171be4 2022-06-23 thomas case 'l':
4165 05171be4 2022-06-23 thomas case KEY_LEFT:
4166 05171be4 2022-06-23 thomas case 'h':
4167 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4168 05171be4 2022-06-23 thomas break;
4169 1e37a5c2 2019-05-12 jcs case 'k':
4170 1e37a5c2 2019-05-12 jcs case KEY_UP:
4171 1e37a5c2 2019-05-12 jcs case '<':
4172 1e37a5c2 2019-05-12 jcs case ',':
4173 f7140bf5 2021-10-17 thomas case CTRL('p'):
4174 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4175 912a3f79 2021-08-30 j break;
4176 912a3f79 2021-08-30 j case 'g':
4177 aa7a1117 2023-01-09 thomas case '=':
4178 912a3f79 2021-08-30 j case KEY_HOME:
4179 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4180 07b0611c 2022-06-23 thomas view->count = 0;
4181 1e37a5c2 2019-05-12 jcs break;
4182 70f17a53 2022-06-13 thomas case CTRL('u'):
4183 23427b14 2022-06-23 thomas case 'u':
4184 70f17a53 2022-06-13 thomas nscroll /= 2;
4185 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4186 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4187 a4292ac5 2019-05-12 jcs case CTRL('b'):
4188 1c5e5faa 2022-06-23 thomas case 'b':
4189 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4190 1e37a5c2 2019-05-12 jcs break;
4191 1e37a5c2 2019-05-12 jcs case 'j':
4192 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4193 1e37a5c2 2019-05-12 jcs case '>':
4194 1e37a5c2 2019-05-12 jcs case '.':
4195 f7140bf5 2021-10-17 thomas case CTRL('n'):
4196 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4197 912a3f79 2021-08-30 j break;
4198 f69c5a46 2022-07-19 thomas case '@':
4199 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4200 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4201 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4202 f69c5a46 2022-07-19 thomas break;
4203 912a3f79 2021-08-30 j case 'G':
4204 aa7a1117 2023-01-09 thomas case '*':
4205 912a3f79 2021-08-30 j case KEY_END: {
4206 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4207 912a3f79 2021-08-30 j * traverse them all. */
4208 07b0611c 2022-06-23 thomas view->count = 0;
4209 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4210 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4211 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4212 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4213 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4214 1e37a5c2 2019-05-12 jcs break;
4215 912a3f79 2021-08-30 j }
4216 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4217 23427b14 2022-06-23 thomas case 'd':
4218 70f17a53 2022-06-13 thomas nscroll /= 2;
4219 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4220 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4221 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4222 4c2d69cb 2022-06-23 thomas case 'f':
4223 b31f89ff 2022-07-01 thomas case ' ':
4224 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4225 1e37a5c2 2019-05-12 jcs break;
4226 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4227 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4228 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4229 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4230 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4231 2b779855 2020-12-05 naddy select_commit(s);
4232 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4233 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4234 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4235 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4236 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4237 0bf7f153 2020-12-02 naddy }
4238 1e37a5c2 2019-05-12 jcs break;
4239 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4240 444d5325 2022-07-03 thomas case '\r':
4241 07b0611c 2022-06-23 thomas view->count = 0;
4242 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4243 e5a0f69f 2018-08-18 stsp break;
4244 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4245 1e37a5c2 2019-05-12 jcs break;
4246 1be4947a 2022-07-22 thomas case 'T':
4247 07b0611c 2022-06-23 thomas view->count = 0;
4248 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4249 5036bf37 2018-09-24 stsp break;
4250 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4251 1e37a5c2 2019-05-12 jcs break;
4252 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4253 21355643 2020-12-06 stsp case CTRL('l'):
4254 21355643 2020-12-06 stsp case 'B':
4255 07b0611c 2022-06-23 thomas view->count = 0;
4256 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4257 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4258 1e37a5c2 2019-05-12 jcs break;
4259 21355643 2020-12-06 stsp err = stop_log_thread(s);
4260 74cfe85e 2020-10-20 stsp if (err)
4261 74cfe85e 2020-10-20 stsp return err;
4262 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4263 21355643 2020-12-06 stsp char *parent_path;
4264 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4265 21355643 2020-12-06 stsp if (err)
4266 21355643 2020-12-06 stsp return err;
4267 21355643 2020-12-06 stsp free(s->in_repo_path);
4268 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4269 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4270 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4271 21355643 2020-12-06 stsp struct got_object_id *start_id;
4272 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4273 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4274 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4275 466429a1 2022-11-17 thomas if (err) {
4276 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4277 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4278 466429a1 2022-11-17 thomas return err;
4279 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4280 466429a1 2022-11-17 thomas free(s->head_ref_name);
4281 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4282 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4283 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4284 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4285 466429a1 2022-11-17 thomas if (err)
4286 466429a1 2022-11-17 thomas return err;
4287 466429a1 2022-11-17 thomas }
4288 21355643 2020-12-06 stsp free(s->start_id);
4289 21355643 2020-12-06 stsp s->start_id = start_id;
4290 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4291 21355643 2020-12-06 stsp } else /* 'B' */
4292 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4293 21355643 2020-12-06 stsp
4294 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4295 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4296 bf7e79b3 2022-06-23 thomas if (err)
4297 bf7e79b3 2022-06-23 thomas return err;
4298 bf7e79b3 2022-06-23 thomas }
4299 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4300 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4301 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4302 74cfe85e 2020-10-20 stsp if (err)
4303 21355643 2020-12-06 stsp return err;
4304 51a10b52 2020-12-26 stsp tog_free_refs();
4305 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4306 ca51c541 2020-12-07 stsp if (err)
4307 ca51c541 2020-12-07 stsp return err;
4308 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4309 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4310 d01904d4 2019-06-25 stsp if (err)
4311 d01904d4 2019-06-25 stsp return err;
4312 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4313 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4314 b672a97a 2020-01-27 stsp if (err)
4315 b672a97a 2020-01-27 stsp return err;
4316 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4317 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4318 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4319 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4320 21355643 2020-12-06 stsp s->selected_entry = NULL;
4321 21355643 2020-12-06 stsp s->selected = 0;
4322 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4323 21355643 2020-12-06 stsp s->quit = 0;
4324 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4325 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4326 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4327 94ecf40d 2022-07-22 thomas view->offset = 0;
4328 d01904d4 2019-06-25 stsp break;
4329 1be4947a 2022-07-22 thomas case 'R':
4330 07b0611c 2022-06-23 thomas view->count = 0;
4331 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4332 6458efa5 2020-11-24 stsp break;
4333 1e37a5c2 2019-05-12 jcs default:
4334 07b0611c 2022-06-23 thomas view->count = 0;
4335 1e37a5c2 2019-05-12 jcs break;
4336 899d86c2 2018-05-10 stsp }
4337 e5a0f69f 2018-08-18 stsp
4338 80ddbec8 2018-04-29 stsp return err;
4339 80ddbec8 2018-04-29 stsp }
4340 80ddbec8 2018-04-29 stsp
4341 4ed7e80c 2018-05-20 stsp static const struct got_error *
4342 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4343 c2db6724 2019-01-04 stsp {
4344 c2db6724 2019-01-04 stsp const struct got_error *error;
4345 c2db6724 2019-01-04 stsp
4346 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4347 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4348 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4349 37c06ea4 2019-07-15 stsp #endif
4350 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4351 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4352 c2db6724 2019-01-04 stsp
4353 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4354 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4355 c2db6724 2019-01-04 stsp
4356 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4357 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4358 c2db6724 2019-01-04 stsp
4359 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4360 c2db6724 2019-01-04 stsp if (error != NULL)
4361 c2db6724 2019-01-04 stsp return error;
4362 c2db6724 2019-01-04 stsp
4363 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4364 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4365 c2db6724 2019-01-04 stsp
4366 c2db6724 2019-01-04 stsp return NULL;
4367 c2db6724 2019-01-04 stsp }
4368 c2db6724 2019-01-04 stsp
4369 b85a3496 2023-04-14 thomas static const struct got_error *
4370 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4371 b85a3496 2023-04-14 thomas {
4372 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4373 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4374 f4086c71 2023-04-22 thomas int in;
4375 b85a3496 2023-04-14 thomas
4376 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4377 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4378 b85a3496 2023-04-14 thomas
4379 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4380 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4381 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4382 b85a3496 2023-04-14 thomas test_script_path);
4383 b85a3496 2023-04-14 thomas goto done;
4384 b85a3496 2023-04-14 thomas }
4385 b85a3496 2023-04-14 thomas
4386 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4387 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4388 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4389 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4390 b85a3496 2023-04-14 thomas goto done;
4391 b85a3496 2023-04-14 thomas }
4392 b85a3496 2023-04-14 thomas
4393 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4394 f4086c71 2023-04-22 thomas if (in == -1) {
4395 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4396 f4086c71 2023-04-22 thomas goto done;
4397 f4086c71 2023-04-22 thomas }
4398 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4399 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4400 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4401 f4086c71 2023-04-22 thomas close(in);
4402 b85a3496 2023-04-14 thomas goto done;
4403 b85a3496 2023-04-14 thomas }
4404 b85a3496 2023-04-14 thomas
4405 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4406 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4407 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4408 66b04f8f 2023-07-19 thomas tog_io.sdump = fopen(screen_dump_path, "we");
4409 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4410 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4411 5b3a801d 2023-04-22 thomas goto done;
4412 5b3a801d 2023-04-22 thomas }
4413 5b3a801d 2023-04-22 thomas
4414 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4415 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4416 b85a3496 2023-04-14 thomas goto done;
4417 b85a3496 2023-04-14 thomas }
4418 b85a3496 2023-04-14 thomas
4419 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4420 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4421 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4422 557d3365 2023-04-14 thomas
4423 557d3365 2023-04-14 thomas using_mock_io = 1;
4424 1134ebde 2023-04-22 thomas
4425 b85a3496 2023-04-14 thomas done:
4426 b85a3496 2023-04-14 thomas if (err)
4427 557d3365 2023-04-14 thomas tog_io_close();
4428 b85a3496 2023-04-14 thomas return err;
4429 b85a3496 2023-04-14 thomas }
4430 b85a3496 2023-04-14 thomas
4431 557d3365 2023-04-14 thomas static void
4432 557d3365 2023-04-14 thomas init_curses(void)
4433 a915003a 2019-02-05 stsp {
4434 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4435 557d3365 2023-04-14 thomas return;
4436 b85a3496 2023-04-14 thomas
4437 557d3365 2023-04-14 thomas initscr();
4438 557d3365 2023-04-14 thomas
4439 a915003a 2019-02-05 stsp cbreak();
4440 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4441 a915003a 2019-02-05 stsp noecho();
4442 a915003a 2019-02-05 stsp nonl();
4443 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4444 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4445 a915003a 2019-02-05 stsp curs_set(0);
4446 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4447 6d17833f 2019-11-08 stsp start_color();
4448 6d17833f 2019-11-08 stsp use_default_colors();
4449 6d17833f 2019-11-08 stsp }
4450 b85a3496 2023-04-14 thomas
4451 557d3365 2023-04-14 thomas return;
4452 a915003a 2019-02-05 stsp }
4453 a915003a 2019-02-05 stsp
4454 c2db6724 2019-01-04 stsp static const struct got_error *
4455 349dfd1e 2023-07-23 thomas set_tog_base_commit(struct got_repository *repo, struct got_worktree *worktree)
4456 349dfd1e 2023-07-23 thomas {
4457 349dfd1e 2023-07-23 thomas tog_base_commit.id = got_object_id_dup(
4458 349dfd1e 2023-07-23 thomas got_worktree_get_base_commit_id(worktree));
4459 349dfd1e 2023-07-23 thomas if (tog_base_commit.id == NULL)
4460 349dfd1e 2023-07-23 thomas return got_error_from_errno( "got_object_id_dup");
4461 349dfd1e 2023-07-23 thomas
4462 92845f09 2023-07-26 thomas return NULL;
4463 349dfd1e 2023-07-23 thomas }
4464 349dfd1e 2023-07-23 thomas
4465 349dfd1e 2023-07-23 thomas static const struct got_error *
4466 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4467 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4468 f135c941 2020-02-20 stsp {
4469 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4470 f135c941 2020-02-20 stsp
4471 f135c941 2020-02-20 stsp if (argc == 0) {
4472 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4473 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4474 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4475 f135c941 2020-02-20 stsp return NULL;
4476 f135c941 2020-02-20 stsp }
4477 f135c941 2020-02-20 stsp
4478 f135c941 2020-02-20 stsp if (worktree) {
4479 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4480 bfd61697 2020-11-14 stsp char *p;
4481 f135c941 2020-02-20 stsp
4482 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4483 f135c941 2020-02-20 stsp if (err)
4484 f135c941 2020-02-20 stsp return err;
4485 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4486 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4487 bfd61697 2020-11-14 stsp p) == -1) {
4488 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4489 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4490 f135c941 2020-02-20 stsp }
4491 f135c941 2020-02-20 stsp free(p);
4492 f135c941 2020-02-20 stsp } else
4493 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4494 f135c941 2020-02-20 stsp
4495 f135c941 2020-02-20 stsp return err;
4496 f135c941 2020-02-20 stsp }
4497 f135c941 2020-02-20 stsp
4498 f135c941 2020-02-20 stsp static const struct got_error *
4499 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4500 9f7d7167 2018-04-29 stsp {
4501 1d98034b 2023-04-14 thomas const struct got_error *error;
4502 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4503 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4504 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4505 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4506 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *start_commit = NULL, *label = NULL;
4507 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4508 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4509 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4510 04cc582a 2018-08-01 stsp struct tog_view *view;
4511 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4512 80ddbec8 2018-04-29 stsp
4513 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4514 80ddbec8 2018-04-29 stsp switch (ch) {
4515 b672a97a 2020-01-27 stsp case 'b':
4516 b672a97a 2020-01-27 stsp log_branches = 1;
4517 b672a97a 2020-01-27 stsp break;
4518 80ddbec8 2018-04-29 stsp case 'c':
4519 80ddbec8 2018-04-29 stsp start_commit = optarg;
4520 80ddbec8 2018-04-29 stsp break;
4521 ecb28ae0 2018-07-16 stsp case 'r':
4522 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4523 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4524 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4525 9ba1d308 2019-10-21 stsp optarg);
4526 ecb28ae0 2018-07-16 stsp break;
4527 80ddbec8 2018-04-29 stsp default:
4528 17020d27 2019-03-07 stsp usage_log();
4529 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4530 80ddbec8 2018-04-29 stsp }
4531 80ddbec8 2018-04-29 stsp }
4532 80ddbec8 2018-04-29 stsp
4533 80ddbec8 2018-04-29 stsp argc -= optind;
4534 80ddbec8 2018-04-29 stsp argv += optind;
4535 80ddbec8 2018-04-29 stsp
4536 f135c941 2020-02-20 stsp if (argc > 1)
4537 f135c941 2020-02-20 stsp usage_log();
4538 963f97a1 2019-03-18 stsp
4539 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4540 7cd52833 2022-06-23 thomas if (error != NULL)
4541 7cd52833 2022-06-23 thomas goto done;
4542 7cd52833 2022-06-23 thomas
4543 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4544 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4545 5a5defb7 2023-07-23 thomas if (cwd == NULL) {
4546 5a5defb7 2023-07-23 thomas error = got_error_from_errno("getcwd");
4547 5a5defb7 2023-07-23 thomas goto done;
4548 5a5defb7 2023-07-23 thomas }
4549 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
4550 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4551 c156c7a4 2020-12-18 stsp goto done;
4552 a1fbf39a 2019-08-11 stsp if (worktree)
4553 6962eb72 2020-02-20 stsp repo_path =
4554 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4555 a1fbf39a 2019-08-11 stsp else
4556 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4557 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4558 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4559 c156c7a4 2020-12-18 stsp goto done;
4560 c156c7a4 2020-12-18 stsp }
4561 ecb28ae0 2018-07-16 stsp }
4562 ecb28ae0 2018-07-16 stsp
4563 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4564 80ddbec8 2018-04-29 stsp if (error != NULL)
4565 ecb28ae0 2018-07-16 stsp goto done;
4566 80ddbec8 2018-04-29 stsp
4567 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4568 f135c941 2020-02-20 stsp repo, worktree);
4569 f135c941 2020-02-20 stsp if (error)
4570 f135c941 2020-02-20 stsp goto done;
4571 f135c941 2020-02-20 stsp
4572 557d3365 2023-04-14 thomas init_curses();
4573 f135c941 2020-02-20 stsp
4574 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4575 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4576 c02c541e 2019-03-29 stsp if (error)
4577 c02c541e 2019-03-29 stsp goto done;
4578 c02c541e 2019-03-29 stsp
4579 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4580 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4581 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4582 87670572 2020-12-26 naddy if (error)
4583 87670572 2020-12-26 naddy goto done;
4584 87670572 2020-12-26 naddy }
4585 51a10b52 2020-12-26 stsp
4586 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4587 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4588 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4589 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4590 d8f38dc4 2020-12-05 stsp if (error)
4591 d8f38dc4 2020-12-05 stsp goto done;
4592 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4593 d8f38dc4 2020-12-05 stsp } else {
4594 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4595 66b04f8f 2023-07-19 thomas repo, worktree);
4596 66b04f8f 2023-07-19 thomas if (error != NULL)
4597 66b04f8f 2023-07-19 thomas goto done;
4598 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
4599 66b04f8f 2023-07-19 thomas start_commit = keyword_idstr;
4600 66b04f8f 2023-07-19 thomas
4601 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4602 d8f38dc4 2020-12-05 stsp if (error == NULL)
4603 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4604 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4605 d8f38dc4 2020-12-05 stsp goto done;
4606 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4607 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4608 d8f38dc4 2020-12-05 stsp if (error)
4609 d8f38dc4 2020-12-05 stsp goto done;
4610 d8f38dc4 2020-12-05 stsp }
4611 8b473291 2019-02-21 stsp
4612 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4613 04cc582a 2018-08-01 stsp if (view == NULL) {
4614 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4615 04cc582a 2018-08-01 stsp goto done;
4616 04cc582a 2018-08-01 stsp }
4617 349dfd1e 2023-07-23 thomas
4618 2fc00ff4 2019-08-31 stsp if (worktree) {
4619 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
4620 349dfd1e 2023-07-23 thomas if (error != NULL)
4621 349dfd1e 2023-07-23 thomas goto done;
4622 92845f09 2023-07-26 thomas }
4623 349dfd1e 2023-07-23 thomas
4624 92845f09 2023-07-26 thomas error = open_log_view(view, start_id, repo, head_ref_name,
4625 92845f09 2023-07-26 thomas in_repo_path, log_branches, worktree);
4626 92845f09 2023-07-26 thomas if (error)
4627 92845f09 2023-07-26 thomas goto done;
4628 92845f09 2023-07-26 thomas
4629 92845f09 2023-07-26 thomas if (worktree) {
4630 92845f09 2023-07-26 thomas /* The work tree will be closed by the log thread. */
4631 2fc00ff4 2019-08-31 stsp worktree = NULL;
4632 2fc00ff4 2019-08-31 stsp }
4633 349dfd1e 2023-07-23 thomas
4634 557d3365 2023-04-14 thomas error = view_loop(view);
4635 349dfd1e 2023-07-23 thomas
4636 ecb28ae0 2018-07-16 stsp done:
4637 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
4638 66b04f8f 2023-07-19 thomas free(keyword_idstr);
4639 f135c941 2020-02-20 stsp free(in_repo_path);
4640 ecb28ae0 2018-07-16 stsp free(repo_path);
4641 ecb28ae0 2018-07-16 stsp free(cwd);
4642 899d86c2 2018-05-10 stsp free(start_id);
4643 d8f38dc4 2020-12-05 stsp free(label);
4644 d8f38dc4 2020-12-05 stsp if (ref)
4645 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4646 1d0f4054 2021-06-17 stsp if (repo) {
4647 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4648 1d0f4054 2021-06-17 stsp if (error == NULL)
4649 1d0f4054 2021-06-17 stsp error = close_err;
4650 1d0f4054 2021-06-17 stsp }
4651 ec142235 2019-03-07 stsp if (worktree)
4652 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4653 7cd52833 2022-06-23 thomas if (pack_fds) {
4654 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4655 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4656 7cd52833 2022-06-23 thomas if (error == NULL)
4657 7cd52833 2022-06-23 thomas error = pack_err;
4658 b85a3496 2023-04-14 thomas }
4659 51a10b52 2020-12-26 stsp tog_free_refs();
4660 80ddbec8 2018-04-29 stsp return error;
4661 9f7d7167 2018-04-29 stsp }
4662 9f7d7167 2018-04-29 stsp
4663 4ed7e80c 2018-05-20 stsp __dead static void
4664 9f7d7167 2018-04-29 stsp usage_diff(void)
4665 9f7d7167 2018-04-29 stsp {
4666 80ddbec8 2018-04-29 stsp endwin();
4667 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4668 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4669 9f7d7167 2018-04-29 stsp exit(1);
4670 b304db33 2018-05-20 stsp }
4671 b304db33 2018-05-20 stsp
4672 6d17833f 2019-11-08 stsp static int
4673 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4674 41605754 2020-11-12 stsp regmatch_t *regmatch)
4675 6d17833f 2019-11-08 stsp {
4676 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4677 6d17833f 2019-11-08 stsp }
4678 6d17833f 2019-11-08 stsp
4679 ef20f542 2022-06-26 thomas static struct tog_color *
4680 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4681 6d17833f 2019-11-08 stsp {
4682 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4683 6d17833f 2019-11-08 stsp
4684 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4685 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4686 6d17833f 2019-11-08 stsp return tc;
4687 6d17833f 2019-11-08 stsp }
4688 6d17833f 2019-11-08 stsp
4689 6d17833f 2019-11-08 stsp return NULL;
4690 6d17833f 2019-11-08 stsp }
4691 6d17833f 2019-11-08 stsp
4692 4ed7e80c 2018-05-20 stsp static const struct got_error *
4693 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4694 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4695 41605754 2020-11-12 stsp {
4696 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4697 666f7b10 2022-06-23 thomas char *exstr = NULL;
4698 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4699 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4700 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4701 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4702 41605754 2020-11-12 stsp
4703 41605754 2020-11-12 stsp *wtotal = 0;
4704 cbea3800 2022-06-23 thomas
4705 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4706 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4707 41605754 2020-11-12 stsp
4708 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4709 666f7b10 2022-06-23 thomas if (err)
4710 666f7b10 2022-06-23 thomas return err;
4711 666f7b10 2022-06-23 thomas
4712 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4713 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4714 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4715 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4716 666f7b10 2022-06-23 thomas goto done;
4717 666f7b10 2022-06-23 thomas }
4718 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4719 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4720 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4721 cbea3800 2022-06-23 thomas goto done;
4722 cbea3800 2022-06-23 thomas }
4723 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4724 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4725 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4726 cbea3800 2022-06-23 thomas goto done;
4727 cbea3800 2022-06-23 thomas }
4728 05171be4 2022-06-23 thomas
4729 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4730 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4731 cbea3800 2022-06-23 thomas col_tab_align, 1);
4732 cbea3800 2022-06-23 thomas if (err)
4733 cbea3800 2022-06-23 thomas goto done;
4734 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4735 05171be4 2022-06-23 thomas if (n) {
4736 cbea3800 2022-06-23 thomas free(wline);
4737 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4738 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4739 cbea3800 2022-06-23 thomas if (err)
4740 cbea3800 2022-06-23 thomas goto done;
4741 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4742 cbea3800 2022-06-23 thomas wlimit -= width;
4743 cbea3800 2022-06-23 thomas *wtotal += width;
4744 41605754 2020-11-12 stsp }
4745 41605754 2020-11-12 stsp
4746 41605754 2020-11-12 stsp if (wlimit > 0) {
4747 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4748 cbea3800 2022-06-23 thomas size_t wlen;
4749 cbea3800 2022-06-23 thomas
4750 cbea3800 2022-06-23 thomas free(wline);
4751 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4752 cbea3800 2022-06-23 thomas col_tab_align, 1);
4753 cbea3800 2022-06-23 thomas if (err)
4754 cbea3800 2022-06-23 thomas goto done;
4755 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4756 cbea3800 2022-06-23 thomas while (i < wlen) {
4757 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4758 cbea3800 2022-06-23 thomas if (width == -1) {
4759 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4760 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4761 cbea3800 2022-06-23 thomas goto done;
4762 cbea3800 2022-06-23 thomas }
4763 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4764 cbea3800 2022-06-23 thomas break;
4765 1c5e5faa 2022-06-23 thomas w += width;
4766 cbea3800 2022-06-23 thomas i++;
4767 41605754 2020-11-12 stsp }
4768 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4769 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4770 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4771 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4772 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4773 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4774 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4775 41605754 2020-11-12 stsp }
4776 41605754 2020-11-12 stsp }
4777 41605754 2020-11-12 stsp
4778 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4779 cbea3800 2022-06-23 thomas free(wline);
4780 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4781 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4782 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4783 cbea3800 2022-06-23 thomas col_tab_align, 1);
4784 cbea3800 2022-06-23 thomas if (err)
4785 cbea3800 2022-06-23 thomas goto done;
4786 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4787 cbea3800 2022-06-23 thomas } else {
4788 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4789 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4790 cbea3800 2022-06-23 thomas if (err)
4791 cbea3800 2022-06-23 thomas goto done;
4792 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4793 cbea3800 2022-06-23 thomas }
4794 cbea3800 2022-06-23 thomas *wtotal += width2;
4795 41605754 2020-11-12 stsp }
4796 cbea3800 2022-06-23 thomas done:
4797 05171be4 2022-06-23 thomas free(wline);
4798 666f7b10 2022-06-23 thomas free(exstr);
4799 cbea3800 2022-06-23 thomas free(seg0);
4800 cbea3800 2022-06-23 thomas free(seg1);
4801 cbea3800 2022-06-23 thomas free(seg2);
4802 cbea3800 2022-06-23 thomas return err;
4803 41605754 2020-11-12 stsp }
4804 07dd3ed3 2022-08-06 thomas
4805 07dd3ed3 2022-08-06 thomas static int
4806 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4807 07dd3ed3 2022-08-06 thomas {
4808 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4809 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4810 07dd3ed3 2022-08-06 thomas
4811 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4812 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4813 fc2737d5 2022-09-15 thomas
4814 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4815 fc2737d5 2022-09-15 thomas selected = first;
4816 fc2737d5 2022-09-15 thomas eof = &s->eof;
4817 fc2737d5 2022-09-15 thomas f = s->f;
4818 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4819 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4820 41605754 2020-11-12 stsp
4821 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4822 07dd3ed3 2022-08-06 thomas selected = first;
4823 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4824 07dd3ed3 2022-08-06 thomas f = s->f;
4825 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4826 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4827 07dd3ed3 2022-08-06 thomas
4828 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4829 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4830 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4831 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4832 07dd3ed3 2022-08-06 thomas } else
4833 07dd3ed3 2022-08-06 thomas return 0;
4834 07dd3ed3 2022-08-06 thomas
4835 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4836 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4837 07dd3ed3 2022-08-06 thomas return 0;
4838 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4839 07dd3ed3 2022-08-06 thomas rewind(f);
4840 07dd3ed3 2022-08-06 thomas *eof = 0;
4841 07dd3ed3 2022-08-06 thomas *first = 1;
4842 07dd3ed3 2022-08-06 thomas *lineno = 0;
4843 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4844 07dd3ed3 2022-08-06 thomas return 0;
4845 07dd3ed3 2022-08-06 thomas }
4846 07dd3ed3 2022-08-06 thomas
4847 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4848 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4849 07dd3ed3 2022-08-06 thomas view->gline = 0;
4850 07dd3ed3 2022-08-06 thomas
4851 07dd3ed3 2022-08-06 thomas return 1;
4852 07dd3ed3 2022-08-06 thomas }
4853 07dd3ed3 2022-08-06 thomas
4854 41605754 2020-11-12 stsp static const struct got_error *
4855 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4856 26ed57b2 2018-05-19 stsp {
4857 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4858 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4859 61e69b96 2018-05-20 stsp const struct got_error *err;
4860 82c78e96 2022-08-06 thomas int nprinted = 0;
4861 b304db33 2018-05-20 stsp char *line;
4862 826082fe 2020-12-10 stsp size_t linesize = 0;
4863 826082fe 2020-12-10 stsp ssize_t linelen;
4864 61e69b96 2018-05-20 stsp wchar_t *wline;
4865 e0b650dd 2018-05-20 stsp int width;
4866 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4867 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4868 fe621944 2020-11-10 stsp off_t line_offset;
4869 26ed57b2 2018-05-19 stsp
4870 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4871 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4872 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4873 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4874 fe621944 2020-11-10 stsp
4875 f7d12f7e 2018-08-01 stsp werase(view->window);
4876 a3404814 2018-09-02 stsp
4877 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4878 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4879 07dd3ed3 2022-08-06 thomas
4880 a3404814 2018-09-02 stsp if (header) {
4881 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4882 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4883 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4884 07dd3ed3 2022-08-06 thomas
4885 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4886 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4887 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4888 f91a2b48 2022-06-23 thomas 0, 0);
4889 135a2da0 2020-11-11 stsp free(line);
4890 135a2da0 2020-11-11 stsp if (err)
4891 a3404814 2018-09-02 stsp return err;
4892 a3404814 2018-09-02 stsp
4893 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4894 a3404814 2018-09-02 stsp wstandout(view->window);
4895 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4896 e54cc94a 2020-11-11 stsp free(wline);
4897 e54cc94a 2020-11-11 stsp wline = NULL;
4898 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4899 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4900 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4901 a3404814 2018-09-02 stsp wstandend(view->window);
4902 26ed57b2 2018-05-19 stsp
4903 a3404814 2018-09-02 stsp if (max_lines <= 1)
4904 a3404814 2018-09-02 stsp return NULL;
4905 a3404814 2018-09-02 stsp max_lines--;
4906 a3404814 2018-09-02 stsp }
4907 a3404814 2018-09-02 stsp
4908 89f1a395 2020-12-01 naddy s->eof = 0;
4909 05171be4 2022-06-23 thomas view->maxx = 0;
4910 826082fe 2020-12-10 stsp line = NULL;
4911 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4912 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4913 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4914 6568f0aa 2022-08-06 thomas
4915 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4916 826082fe 2020-12-10 stsp if (linelen == -1) {
4917 826082fe 2020-12-10 stsp if (feof(s->f)) {
4918 826082fe 2020-12-10 stsp s->eof = 1;
4919 826082fe 2020-12-10 stsp break;
4920 826082fe 2020-12-10 stsp }
4921 826082fe 2020-12-10 stsp free(line);
4922 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4923 61e69b96 2018-05-20 stsp }
4924 05171be4 2022-06-23 thomas
4925 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4926 07dd3ed3 2022-08-06 thomas continue;
4927 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4928 07dd3ed3 2022-08-06 thomas continue;
4929 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4930 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4931 07dd3ed3 2022-08-06 thomas
4932 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4933 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4934 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4935 cbea3800 2022-06-23 thomas if (err) {
4936 cbea3800 2022-06-23 thomas free(line);
4937 cbea3800 2022-06-23 thomas return err;
4938 cbea3800 2022-06-23 thomas }
4939 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4940 cbea3800 2022-06-23 thomas free(wline);
4941 cbea3800 2022-06-23 thomas wline = NULL;
4942 cbea3800 2022-06-23 thomas
4943 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4944 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4945 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4946 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4947 07dd3ed3 2022-08-06 thomas if (attr)
4948 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4949 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4950 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4951 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4952 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4953 41605754 2020-11-12 stsp if (err) {
4954 41605754 2020-11-12 stsp free(line);
4955 41605754 2020-11-12 stsp return err;
4956 41605754 2020-11-12 stsp }
4957 41605754 2020-11-12 stsp } else {
4958 f1c0ec19 2022-06-23 thomas int skip;
4959 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4960 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4961 f1c0ec19 2022-06-23 thomas if (err) {
4962 f1c0ec19 2022-06-23 thomas free(line);
4963 f1c0ec19 2022-06-23 thomas return err;
4964 f1c0ec19 2022-06-23 thomas }
4965 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4966 f1c0ec19 2022-06-23 thomas free(wline);
4967 41605754 2020-11-12 stsp wline = NULL;
4968 41605754 2020-11-12 stsp }
4969 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4970 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4971 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4972 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4973 07dd3ed3 2022-08-06 thomas } else {
4974 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4975 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4976 07dd3ed3 2022-08-06 thomas }
4977 07dd3ed3 2022-08-06 thomas if (attr)
4978 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4979 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4980 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4981 826082fe 2020-12-10 stsp }
4982 826082fe 2020-12-10 stsp free(line);
4983 fe621944 2020-11-10 stsp if (nprinted >= 1)
4984 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4985 89f1a395 2020-12-01 naddy (nprinted - 1);
4986 fe621944 2020-11-10 stsp else
4987 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4988 26ed57b2 2018-05-19 stsp
4989 a5d43cac 2022-07-01 thomas view_border(view);
4990 c3e9aa98 2019-05-13 jcs
4991 89f1a395 2020-12-01 naddy if (s->eof) {
4992 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4993 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4994 c3e9aa98 2019-05-13 jcs nprinted++;
4995 c3e9aa98 2019-05-13 jcs }
4996 c3e9aa98 2019-05-13 jcs
4997 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4998 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4999 c3e9aa98 2019-05-13 jcs if (err) {
5000 c3e9aa98 2019-05-13 jcs return err;
5001 c3e9aa98 2019-05-13 jcs }
5002 26ed57b2 2018-05-19 stsp
5003 c3e9aa98 2019-05-13 jcs wstandout(view->window);
5004 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
5005 e54cc94a 2020-11-11 stsp free(wline);
5006 e54cc94a 2020-11-11 stsp wline = NULL;
5007 c3e9aa98 2019-05-13 jcs wstandend(view->window);
5008 c3e9aa98 2019-05-13 jcs }
5009 c3e9aa98 2019-05-13 jcs
5010 26ed57b2 2018-05-19 stsp return NULL;
5011 abd2672a 2018-12-23 stsp }
5012 abd2672a 2018-12-23 stsp
5013 abd2672a 2018-12-23 stsp static char *
5014 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
5015 abd2672a 2018-12-23 stsp {
5016 09867e48 2019-08-13 stsp struct tm mytm, *tm;
5017 09867e48 2019-08-13 stsp char *p, *s;
5018 09867e48 2019-08-13 stsp
5019 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
5020 09867e48 2019-08-13 stsp if (tm == NULL)
5021 09867e48 2019-08-13 stsp return NULL;
5022 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
5023 09867e48 2019-08-13 stsp if (s == NULL)
5024 09867e48 2019-08-13 stsp return NULL;
5025 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
5026 abd2672a 2018-12-23 stsp if (p)
5027 abd2672a 2018-12-23 stsp *p = '\0';
5028 abd2672a 2018-12-23 stsp return s;
5029 9f7d7167 2018-04-29 stsp }
5030 9f7d7167 2018-04-29 stsp
5031 4ed7e80c 2018-05-20 stsp static const struct got_error *
5032 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
5033 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
5034 abd2672a 2018-12-23 stsp {
5035 82c78e96 2022-08-06 thomas struct got_diff_line *p;
5036 fe621944 2020-11-10 stsp
5037 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
5038 fe621944 2020-11-10 stsp if (p == NULL)
5039 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
5040 82c78e96 2022-08-06 thomas *lines = p;
5041 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
5042 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
5043 fe621944 2020-11-10 stsp (*nlines)++;
5044 82c78e96 2022-08-06 thomas
5045 fe621944 2020-11-10 stsp return NULL;
5046 fe621944 2020-11-10 stsp }
5047 fe621944 2020-11-10 stsp
5048 fe621944 2020-11-10 stsp static const struct got_error *
5049 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
5050 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
5051 be97ab03 2023-01-19 thomas {
5052 be97ab03 2023-01-19 thomas struct got_diff_line *p;
5053 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
5054 be97ab03 2023-01-19 thomas size_t i, r;
5055 be97ab03 2023-01-19 thomas
5056 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
5057 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
5058 be97ab03 2023-01-19 thomas
5059 be97ab03 2023-01-19 thomas for (;;) {
5060 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
5061 be97ab03 2023-01-19 thomas if (r == 0) {
5062 be97ab03 2023-01-19 thomas if (ferror(src))
5063 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
5064 be97ab03 2023-01-19 thomas if (feof(src))
5065 be97ab03 2023-01-19 thomas break;
5066 be97ab03 2023-01-19 thomas }
5067 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
5068 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
5069 be97ab03 2023-01-19 thomas }
5070 be97ab03 2023-01-19 thomas
5071 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
5072 9382c10a 2023-02-23 thomas return NULL;
5073 9382c10a 2023-02-23 thomas
5074 be97ab03 2023-01-19 thomas /*
5075 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
5076 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
5077 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
5078 be97ab03 2023-01-19 thomas */
5079 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
5080 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
5081 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
5082 be97ab03 2023-01-19 thomas
5083 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
5084 9382c10a 2023-02-23 thomas --s_nlines;
5085 9382c10a 2023-02-23 thomas ++s_lines;
5086 9382c10a 2023-02-23 thomas }
5087 9382c10a 2023-02-23 thomas }
5088 be97ab03 2023-01-19 thomas
5089 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
5090 be97ab03 2023-01-19 thomas if (p == NULL) {
5091 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
5092 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
5093 be97ab03 2023-01-19 thomas }
5094 be97ab03 2023-01-19 thomas
5095 be97ab03 2023-01-19 thomas *d_lines = p;
5096 be97ab03 2023-01-19 thomas
5097 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
5098 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
5099 be97ab03 2023-01-19 thomas
5100 be97ab03 2023-01-19 thomas return NULL;
5101 be97ab03 2023-01-19 thomas }
5102 be97ab03 2023-01-19 thomas
5103 be97ab03 2023-01-19 thomas static const struct got_error *
5104 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
5105 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
5106 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
5107 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
5108 fe621944 2020-11-10 stsp {
5109 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
5110 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
5111 15a94983 2018-12-23 stsp struct got_commit_object *commit;
5112 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
5113 45d799e2 2018-12-23 stsp time_t committer_time;
5114 45d799e2 2018-12-23 stsp const char *author, *committer;
5115 8b473291 2019-02-21 stsp char *refs_str = NULL;
5116 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
5117 fe621944 2020-11-10 stsp off_t outoff = 0;
5118 fe621944 2020-11-10 stsp int n;
5119 abd2672a 2018-12-23 stsp
5120 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, commit_id, repo);
5121 00ddec2f 2023-05-17 thomas if (err)
5122 00ddec2f 2023-05-17 thomas return err;
5123 8b473291 2019-02-21 stsp
5124 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5125 abd2672a 2018-12-23 stsp if (err)
5126 abd2672a 2018-12-23 stsp return err;
5127 abd2672a 2018-12-23 stsp
5128 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
5129 15a94983 2018-12-23 stsp if (err) {
5130 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
5131 15a94983 2018-12-23 stsp goto done;
5132 15a94983 2018-12-23 stsp }
5133 abd2672a 2018-12-23 stsp
5134 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5135 fe621944 2020-11-10 stsp if (err)
5136 fe621944 2020-11-10 stsp goto done;
5137 fe621944 2020-11-10 stsp
5138 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5139 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
5140 fe621944 2020-11-10 stsp if (n < 0) {
5141 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5142 abd2672a 2018-12-23 stsp goto done;
5143 abd2672a 2018-12-23 stsp }
5144 fe621944 2020-11-10 stsp outoff += n;
5145 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5146 fe621944 2020-11-10 stsp if (err)
5147 fe621944 2020-11-10 stsp goto done;
5148 fe621944 2020-11-10 stsp
5149 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5150 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5151 fe621944 2020-11-10 stsp if (n < 0) {
5152 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5153 abd2672a 2018-12-23 stsp goto done;
5154 abd2672a 2018-12-23 stsp }
5155 fe621944 2020-11-10 stsp outoff += n;
5156 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5157 fe621944 2020-11-10 stsp if (err)
5158 fe621944 2020-11-10 stsp goto done;
5159 fe621944 2020-11-10 stsp
5160 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5161 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5162 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5163 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5164 fe621944 2020-11-10 stsp if (n < 0) {
5165 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5166 fe621944 2020-11-10 stsp goto done;
5167 fe621944 2020-11-10 stsp }
5168 fe621944 2020-11-10 stsp outoff += n;
5169 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5170 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5171 fe621944 2020-11-10 stsp if (err)
5172 fe621944 2020-11-10 stsp goto done;
5173 abd2672a 2018-12-23 stsp }
5174 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5175 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5176 48830929 2023-01-07 thomas if (datestr) {
5177 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5178 fe621944 2020-11-10 stsp if (n < 0) {
5179 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5180 fe621944 2020-11-10 stsp goto done;
5181 fe621944 2020-11-10 stsp }
5182 fe621944 2020-11-10 stsp outoff += n;
5183 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5184 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5185 fe621944 2020-11-10 stsp if (err)
5186 fe621944 2020-11-10 stsp goto done;
5187 abd2672a 2018-12-23 stsp }
5188 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5189 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5190 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5191 ac4dc263 2021-09-24 thomas int pn = 1;
5192 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5193 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5194 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5195 ac4dc263 2021-09-24 thomas if (err)
5196 ac4dc263 2021-09-24 thomas goto done;
5197 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5198 ac4dc263 2021-09-24 thomas if (n < 0) {
5199 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5200 ac4dc263 2021-09-24 thomas goto done;
5201 ac4dc263 2021-09-24 thomas }
5202 ac4dc263 2021-09-24 thomas outoff += n;
5203 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5204 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5205 ac4dc263 2021-09-24 thomas if (err)
5206 ac4dc263 2021-09-24 thomas goto done;
5207 ac4dc263 2021-09-24 thomas free(id_str);
5208 ac4dc263 2021-09-24 thomas id_str = NULL;
5209 ac4dc263 2021-09-24 thomas }
5210 ac4dc263 2021-09-24 thomas }
5211 ac4dc263 2021-09-24 thomas
5212 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5213 5943eee2 2019-08-13 stsp if (err)
5214 5943eee2 2019-08-13 stsp goto done;
5215 fe621944 2020-11-10 stsp s = logmsg;
5216 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5217 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5218 fe621944 2020-11-10 stsp if (n < 0) {
5219 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5220 fe621944 2020-11-10 stsp goto done;
5221 fe621944 2020-11-10 stsp }
5222 fe621944 2020-11-10 stsp outoff += n;
5223 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5224 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5225 fe621944 2020-11-10 stsp if (err)
5226 fe621944 2020-11-10 stsp goto done;
5227 abd2672a 2018-12-23 stsp }
5228 fe621944 2020-11-10 stsp
5229 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5230 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5231 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5232 772fcad5 2023-01-07 thomas
5233 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5234 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5235 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5236 fe621944 2020-11-10 stsp if (n < 0) {
5237 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5238 fe621944 2020-11-10 stsp goto done;
5239 fe621944 2020-11-10 stsp }
5240 fe621944 2020-11-10 stsp outoff += n;
5241 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5242 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5243 fe621944 2020-11-10 stsp if (err)
5244 fe621944 2020-11-10 stsp goto done;
5245 0208f208 2020-05-05 stsp }
5246 fe621944 2020-11-10 stsp
5247 0208f208 2020-05-05 stsp fputc('\n', outfile);
5248 fe621944 2020-11-10 stsp outoff++;
5249 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5250 772fcad5 2023-01-07 thomas if (err)
5251 772fcad5 2023-01-07 thomas goto done;
5252 772fcad5 2023-01-07 thomas
5253 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5254 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5255 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5256 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5257 772fcad5 2023-01-07 thomas if (n < 0) {
5258 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5259 772fcad5 2023-01-07 thomas goto done;
5260 772fcad5 2023-01-07 thomas }
5261 772fcad5 2023-01-07 thomas outoff += n;
5262 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5263 772fcad5 2023-01-07 thomas if (err)
5264 772fcad5 2023-01-07 thomas goto done;
5265 772fcad5 2023-01-07 thomas
5266 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5267 772fcad5 2023-01-07 thomas outoff++;
5268 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5269 abd2672a 2018-12-23 stsp done:
5270 abd2672a 2018-12-23 stsp free(id_str);
5271 5943eee2 2019-08-13 stsp free(logmsg);
5272 8b473291 2019-02-21 stsp free(refs_str);
5273 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5274 7510f233 2020-08-09 stsp if (err) {
5275 82c78e96 2022-08-06 thomas free(*lines);
5276 82c78e96 2022-08-06 thomas *lines = NULL;
5277 ae6a6978 2020-08-09 stsp *nlines = 0;
5278 7510f233 2020-08-09 stsp }
5279 fe621944 2020-11-10 stsp return err;
5280 abd2672a 2018-12-23 stsp }
5281 abd2672a 2018-12-23 stsp
5282 abd2672a 2018-12-23 stsp static const struct got_error *
5283 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5284 26ed57b2 2018-05-19 stsp {
5285 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5286 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5287 15a94983 2018-12-23 stsp int obj_type;
5288 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5289 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5290 fe621944 2020-11-10 stsp
5291 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5292 be97ab03 2023-01-19 thomas
5293 82c78e96 2022-08-06 thomas free(s->lines);
5294 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5295 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5296 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5297 fe621944 2020-11-10 stsp s->nlines = 0;
5298 26ed57b2 2018-05-19 stsp
5299 511a516b 2018-05-19 stsp f = got_opentemp();
5300 48ae06ee 2018-10-18 stsp if (f == NULL) {
5301 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5302 48ae06ee 2018-10-18 stsp goto done;
5303 48ae06ee 2018-10-18 stsp }
5304 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5305 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5306 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5307 be97ab03 2023-01-19 thomas goto done;
5308 be97ab03 2023-01-19 thomas }
5309 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5310 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5311 fb43ecf1 2019-02-11 stsp goto done;
5312 fb43ecf1 2019-02-11 stsp }
5313 48ae06ee 2018-10-18 stsp s->f = f;
5314 26ed57b2 2018-05-19 stsp
5315 15a94983 2018-12-23 stsp if (s->id1)
5316 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5317 15a94983 2018-12-23 stsp else
5318 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5319 15a94983 2018-12-23 stsp if (err)
5320 15a94983 2018-12-23 stsp goto done;
5321 15a94983 2018-12-23 stsp
5322 15a94983 2018-12-23 stsp switch (obj_type) {
5323 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5324 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5325 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5326 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5327 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5328 53d03f97 2023-01-10 thomas s->f);
5329 26ed57b2 2018-05-19 stsp break;
5330 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5331 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5332 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5333 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5334 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5335 26ed57b2 2018-05-19 stsp break;
5336 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5337 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5338 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5339 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5340 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5341 be97ab03 2023-01-19 thomas size_t nlines = 0;
5342 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5343 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5344 be97ab03 2023-01-19 thomas &changed_paths,
5345 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5346 be97ab03 2023-01-19 thomas s->force_text_diff,
5347 be97ab03 2023-01-19 thomas tog_diff_algo
5348 be97ab03 2023-01-19 thomas };
5349 abd2672a 2018-12-23 stsp
5350 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5351 be97ab03 2023-01-19 thomas if (lines == NULL) {
5352 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5353 be97ab03 2023-01-19 thomas goto done;
5354 be97ab03 2023-01-19 thomas }
5355 be97ab03 2023-01-19 thomas
5356 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5357 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5358 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5359 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5360 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5361 be97ab03 2023-01-19 thomas if (err)
5362 be97ab03 2023-01-19 thomas break;
5363 be97ab03 2023-01-19 thomas
5364 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5365 abd2672a 2018-12-23 stsp if (err)
5366 3ffacbe1 2020-02-02 tracey goto done;
5367 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5368 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5369 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5370 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5371 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5372 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5373 f44b1f58 2020-02-02 tracey if (err)
5374 f44b1f58 2020-02-02 tracey goto done;
5375 f44b1f58 2020-02-02 tracey } else {
5376 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5377 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5378 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5379 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5380 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5381 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5382 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5383 f44b1f58 2020-02-02 tracey if (err)
5384 f44b1f58 2020-02-02 tracey goto done;
5385 f5404e4e 2020-02-02 tracey break;
5386 15a087fe 2019-02-21 stsp }
5387 abd2672a 2018-12-23 stsp }
5388 abd2672a 2018-12-23 stsp }
5389 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5390 abd2672a 2018-12-23 stsp
5391 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5392 be97ab03 2023-01-19 thomas lines, nlines);
5393 26ed57b2 2018-05-19 stsp break;
5394 abd2672a 2018-12-23 stsp }
5395 26ed57b2 2018-05-19 stsp default:
5396 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5397 48ae06ee 2018-10-18 stsp break;
5398 26ed57b2 2018-05-19 stsp }
5399 48ae06ee 2018-10-18 stsp done:
5400 be97ab03 2023-01-19 thomas free(lines);
5401 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5402 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5403 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5404 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5405 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5406 48ae06ee 2018-10-18 stsp return err;
5407 48ae06ee 2018-10-18 stsp }
5408 26ed57b2 2018-05-19 stsp
5409 f5215bb9 2019-02-22 stsp static void
5410 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5411 f5215bb9 2019-02-22 stsp {
5412 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5413 f5215bb9 2019-02-22 stsp update_panels();
5414 f5215bb9 2019-02-22 stsp doupdate();
5415 f44b1f58 2020-02-02 tracey }
5416 f44b1f58 2020-02-02 tracey
5417 f44b1f58 2020-02-02 tracey static const struct got_error *
5418 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5419 f44b1f58 2020-02-02 tracey {
5420 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5421 f44b1f58 2020-02-02 tracey
5422 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5423 f44b1f58 2020-02-02 tracey return NULL;
5424 f44b1f58 2020-02-02 tracey }
5425 f44b1f58 2020-02-02 tracey
5426 2a7d3cd7 2022-09-17 thomas static void
5427 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5428 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5429 f44b1f58 2020-02-02 tracey {
5430 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5431 fc2737d5 2022-09-15 thomas
5432 2a7d3cd7 2022-09-17 thomas *f = s->f;
5433 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5434 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5435 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5436 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5437 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5438 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5439 fc2737d5 2022-09-15 thomas }
5440 fc2737d5 2022-09-15 thomas
5441 fc2737d5 2022-09-15 thomas static const struct got_error *
5442 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5443 fc2737d5 2022-09-15 thomas {
5444 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5445 fc2737d5 2022-09-15 thomas FILE *f;
5446 f44b1f58 2020-02-02 tracey int lineno;
5447 78eecffc 2022-06-23 thomas char *line = NULL;
5448 826082fe 2020-12-10 stsp size_t linesize = 0;
5449 826082fe 2020-12-10 stsp ssize_t linelen;
5450 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5451 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5452 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5453 f44b1f58 2020-02-02 tracey
5454 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5455 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5456 2a7d3cd7 2022-09-17 thomas "view search not supported");
5457 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5458 fc2737d5 2022-09-15 thomas &match, &selected);
5459 fc2737d5 2022-09-15 thomas
5460 f44b1f58 2020-02-02 tracey if (!view->searching) {
5461 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5462 f44b1f58 2020-02-02 tracey return NULL;
5463 f44b1f58 2020-02-02 tracey }
5464 f44b1f58 2020-02-02 tracey
5465 fc2737d5 2022-09-15 thomas if (*match) {
5466 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5467 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5468 f44b1f58 2020-02-02 tracey else
5469 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5470 4b3f9dac 2021-12-17 thomas } else
5471 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5472 f44b1f58 2020-02-02 tracey
5473 f44b1f58 2020-02-02 tracey while (1) {
5474 f44b1f58 2020-02-02 tracey off_t offset;
5475 f44b1f58 2020-02-02 tracey
5476 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5477 fc2737d5 2022-09-15 thomas if (*match == 0) {
5478 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5479 f44b1f58 2020-02-02 tracey break;
5480 f44b1f58 2020-02-02 tracey }
5481 f44b1f58 2020-02-02 tracey
5482 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5483 f44b1f58 2020-02-02 tracey lineno = 1;
5484 f44b1f58 2020-02-02 tracey else
5485 fc2737d5 2022-09-15 thomas lineno = nlines;
5486 f44b1f58 2020-02-02 tracey }
5487 f44b1f58 2020-02-02 tracey
5488 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5489 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5490 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5491 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5492 f44b1f58 2020-02-02 tracey free(line);
5493 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5494 f44b1f58 2020-02-02 tracey }
5495 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5496 78eecffc 2022-06-23 thomas if (linelen != -1) {
5497 78eecffc 2022-06-23 thomas char *exstr;
5498 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5499 78eecffc 2022-06-23 thomas if (err)
5500 78eecffc 2022-06-23 thomas break;
5501 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5502 78eecffc 2022-06-23 thomas &view->regmatch)) {
5503 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5504 fc2737d5 2022-09-15 thomas *match = lineno;
5505 78eecffc 2022-06-23 thomas free(exstr);
5506 78eecffc 2022-06-23 thomas break;
5507 78eecffc 2022-06-23 thomas }
5508 78eecffc 2022-06-23 thomas free(exstr);
5509 f44b1f58 2020-02-02 tracey }
5510 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5511 f44b1f58 2020-02-02 tracey lineno++;
5512 f44b1f58 2020-02-02 tracey else
5513 f44b1f58 2020-02-02 tracey lineno--;
5514 f44b1f58 2020-02-02 tracey }
5515 826082fe 2020-12-10 stsp free(line);
5516 f44b1f58 2020-02-02 tracey
5517 fc2737d5 2022-09-15 thomas if (*match) {
5518 fc2737d5 2022-09-15 thomas *first = *match;
5519 fc2737d5 2022-09-15 thomas *selected = 1;
5520 f44b1f58 2020-02-02 tracey }
5521 f44b1f58 2020-02-02 tracey
5522 05171be4 2022-06-23 thomas return err;
5523 f5215bb9 2019-02-22 stsp }
5524 f5215bb9 2019-02-22 stsp
5525 48ae06ee 2018-10-18 stsp static const struct got_error *
5526 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5527 a0f32f33 2022-06-13 thomas {
5528 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5529 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5530 a0f32f33 2022-06-13 thomas
5531 a0f32f33 2022-06-13 thomas free(s->id1);
5532 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5533 a0f32f33 2022-06-13 thomas free(s->id2);
5534 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5535 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5536 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5537 a0f32f33 2022-06-13 thomas s->f = NULL;
5538 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5539 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5540 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5541 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5542 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5543 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5544 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5545 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5546 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5547 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5548 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5549 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5550 82c78e96 2022-08-06 thomas free(s->lines);
5551 82c78e96 2022-08-06 thomas s->lines = NULL;
5552 a0f32f33 2022-06-13 thomas s->nlines = 0;
5553 a0f32f33 2022-06-13 thomas return err;
5554 a0f32f33 2022-06-13 thomas }
5555 a0f32f33 2022-06-13 thomas
5556 a0f32f33 2022-06-13 thomas static const struct got_error *
5557 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5558 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5559 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5560 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5561 48ae06ee 2018-10-18 stsp {
5562 48ae06ee 2018-10-18 stsp const struct got_error *err;
5563 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5564 5dc9f4bc 2018-08-04 stsp
5565 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5566 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5567 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5568 a0f32f33 2022-06-13 thomas
5569 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5570 51b7e1c3 2023-04-14 thomas int type1, type2;
5571 15a94983 2018-12-23 stsp
5572 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5573 51b7e1c3 2023-04-14 thomas if (err)
5574 51b7e1c3 2023-04-14 thomas goto done;
5575 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5576 51b7e1c3 2023-04-14 thomas if (err)
5577 51b7e1c3 2023-04-14 thomas goto done;
5578 51b7e1c3 2023-04-14 thomas
5579 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5580 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5581 51b7e1c3 2023-04-14 thomas goto done;
5582 51b7e1c3 2023-04-14 thomas }
5583 15a94983 2018-12-23 stsp }
5584 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5585 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5586 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5587 f44b1f58 2020-02-02 tracey s->repo = repo;
5588 f44b1f58 2020-02-02 tracey s->id1 = id1;
5589 f44b1f58 2020-02-02 tracey s->id2 = id2;
5590 3dbaef42 2020-11-24 stsp s->label1 = label1;
5591 3dbaef42 2020-11-24 stsp s->label2 = label2;
5592 48ae06ee 2018-10-18 stsp
5593 15a94983 2018-12-23 stsp if (id1) {
5594 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5595 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5596 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5597 51b7e1c3 2023-04-14 thomas goto done;
5598 51b7e1c3 2023-04-14 thomas }
5599 48ae06ee 2018-10-18 stsp } else
5600 5465d566 2020-02-01 tracey s->id1 = NULL;
5601 48ae06ee 2018-10-18 stsp
5602 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5603 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5604 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5605 a0f32f33 2022-06-13 thomas goto done;
5606 48ae06ee 2018-10-18 stsp }
5607 a0f32f33 2022-06-13 thomas
5608 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5609 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5610 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5611 9a1d7435 2022-06-23 thomas goto done;
5612 9a1d7435 2022-06-23 thomas }
5613 9a1d7435 2022-06-23 thomas
5614 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5615 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5616 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5617 a0f32f33 2022-06-13 thomas goto done;
5618 a0f32f33 2022-06-13 thomas }
5619 a0f32f33 2022-06-13 thomas
5620 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5621 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5622 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5623 19a6a6b5 2022-07-01 thomas goto done;
5624 19a6a6b5 2022-07-01 thomas }
5625 19a6a6b5 2022-07-01 thomas
5626 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5627 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5628 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5629 19a6a6b5 2022-07-01 thomas goto done;
5630 19a6a6b5 2022-07-01 thomas }
5631 19a6a6b5 2022-07-01 thomas
5632 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5633 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5634 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5635 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5636 5465d566 2020-02-01 tracey s->repo = repo;
5637 6d17833f 2019-11-08 stsp
5638 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5639 6568f0aa 2022-08-06 thomas int rc;
5640 6d17833f 2019-11-08 stsp
5641 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5642 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5643 6568f0aa 2022-08-06 thomas if (rc != ERR)
5644 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5645 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5646 6568f0aa 2022-08-06 thomas if (rc != ERR)
5647 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5648 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5649 6568f0aa 2022-08-06 thomas if (rc != ERR)
5650 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5651 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5652 6568f0aa 2022-08-06 thomas if (rc != ERR)
5653 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5654 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5655 6568f0aa 2022-08-06 thomas if (rc != ERR)
5656 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5657 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5658 6568f0aa 2022-08-06 thomas if (rc != ERR)
5659 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5660 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5661 6568f0aa 2022-08-06 thomas if (rc != ERR)
5662 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5663 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5664 6568f0aa 2022-08-06 thomas if (rc != ERR)
5665 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5666 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5667 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5668 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5669 a0f32f33 2022-06-13 thomas goto done;
5670 6568f0aa 2022-08-06 thomas }
5671 6d17833f 2019-11-08 stsp }
5672 5dc9f4bc 2018-08-04 stsp
5673 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5674 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5675 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5676 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5677 f5215bb9 2019-02-22 stsp
5678 5465d566 2020-02-01 tracey err = create_diff(s);
5679 48ae06ee 2018-10-18 stsp
5680 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5681 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5682 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5683 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5684 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5685 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5686 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5687 a0f32f33 2022-06-13 thomas done:
5688 51b7e1c3 2023-04-14 thomas if (err) {
5689 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5690 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5691 51b7e1c3 2023-04-14 thomas view_close(view);
5692 51b7e1c3 2023-04-14 thomas }
5693 e5a0f69f 2018-08-18 stsp return err;
5694 5dc9f4bc 2018-08-04 stsp }
5695 5dc9f4bc 2018-08-04 stsp
5696 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5697 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5698 5dc9f4bc 2018-08-04 stsp {
5699 a3404814 2018-09-02 stsp const struct got_error *err;
5700 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5701 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5702 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5703 a3404814 2018-09-02 stsp
5704 a3404814 2018-09-02 stsp if (s->id1) {
5705 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5706 a3404814 2018-09-02 stsp if (err)
5707 a3404814 2018-09-02 stsp return err;
5708 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5709 3dbaef42 2020-11-24 stsp } else
5710 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5711 3dbaef42 2020-11-24 stsp
5712 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5713 a3404814 2018-09-02 stsp if (err)
5714 a3404814 2018-09-02 stsp return err;
5715 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5716 26ed57b2 2018-05-19 stsp
5717 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5718 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5719 a3404814 2018-09-02 stsp free(id_str1);
5720 a3404814 2018-09-02 stsp free(id_str2);
5721 a3404814 2018-09-02 stsp return err;
5722 a3404814 2018-09-02 stsp }
5723 a3404814 2018-09-02 stsp free(id_str1);
5724 a3404814 2018-09-02 stsp free(id_str2);
5725 a3404814 2018-09-02 stsp
5726 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5727 267bb3b8 2021-08-01 stsp free(header);
5728 267bb3b8 2021-08-01 stsp return err;
5729 15a087fe 2019-02-21 stsp }
5730 15a087fe 2019-02-21 stsp
5731 15a087fe 2019-02-21 stsp static const struct got_error *
5732 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5733 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5734 15a087fe 2019-02-21 stsp {
5735 d7a04538 2019-02-21 stsp const struct got_error *err;
5736 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5737 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5738 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5739 15a087fe 2019-02-21 stsp
5740 15a087fe 2019-02-21 stsp free(s->id2);
5741 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5742 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5743 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5744 15a087fe 2019-02-21 stsp
5745 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5746 d7a04538 2019-02-21 stsp if (err)
5747 d7a04538 2019-02-21 stsp return err;
5748 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5749 15a087fe 2019-02-21 stsp free(s->id1);
5750 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5751 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5752 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5753 15a087fe 2019-02-21 stsp return NULL;
5754 0cf4efb1 2018-09-29 stsp }
5755 0cf4efb1 2018-09-29 stsp
5756 0cf4efb1 2018-09-29 stsp static const struct got_error *
5757 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5758 adf4c9e0 2022-07-03 thomas {
5759 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5760 adf4c9e0 2022-07-03 thomas
5761 adf4c9e0 2022-07-03 thomas view->count = 0;
5762 adf4c9e0 2022-07-03 thomas wclear(view->window);
5763 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5764 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5765 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5766 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5767 adf4c9e0 2022-07-03 thomas return create_diff(s);
5768 82c78e96 2022-08-06 thomas }
5769 82c78e96 2022-08-06 thomas
5770 82c78e96 2022-08-06 thomas static void
5771 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5772 82c78e96 2022-08-06 thomas {
5773 82c78e96 2022-08-06 thomas int start, i;
5774 82c78e96 2022-08-06 thomas
5775 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5776 82c78e96 2022-08-06 thomas
5777 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5778 82c78e96 2022-08-06 thomas if (i == 0)
5779 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5780 82c78e96 2022-08-06 thomas if (--i == start)
5781 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5782 82c78e96 2022-08-06 thomas }
5783 82c78e96 2022-08-06 thomas
5784 82c78e96 2022-08-06 thomas s->selected_line = 1;
5785 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5786 adf4c9e0 2022-07-03 thomas }
5787 adf4c9e0 2022-07-03 thomas
5788 82c78e96 2022-08-06 thomas static void
5789 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5790 82c78e96 2022-08-06 thomas {
5791 82c78e96 2022-08-06 thomas int start, i;
5792 82c78e96 2022-08-06 thomas
5793 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5794 82c78e96 2022-08-06 thomas
5795 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5796 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5797 82c78e96 2022-08-06 thomas i = 0;
5798 82c78e96 2022-08-06 thomas if (++i == start)
5799 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5800 82c78e96 2022-08-06 thomas }
5801 82c78e96 2022-08-06 thomas
5802 82c78e96 2022-08-06 thomas s->selected_line = 1;
5803 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5804 82c78e96 2022-08-06 thomas }
5805 82c78e96 2022-08-06 thomas
5806 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5807 4fc71f3b 2022-07-12 thomas int, int, int);
5808 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5809 4fc71f3b 2022-07-12 thomas int, int);
5810 4fc71f3b 2022-07-12 thomas
5811 adf4c9e0 2022-07-03 thomas static const struct got_error *
5812 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5813 e5a0f69f 2018-08-18 stsp {
5814 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5815 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5816 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5817 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5818 826082fe 2020-12-10 stsp char *line = NULL;
5819 826082fe 2020-12-10 stsp size_t linesize = 0;
5820 826082fe 2020-12-10 stsp ssize_t linelen;
5821 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5822 e5a0f69f 2018-08-18 stsp
5823 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5824 82c78e96 2022-08-06 thomas
5825 e5a0f69f 2018-08-18 stsp switch (ch) {
5826 05171be4 2022-06-23 thomas case '0':
5827 05171be4 2022-06-23 thomas case '$':
5828 05171be4 2022-06-23 thomas case KEY_RIGHT:
5829 05171be4 2022-06-23 thomas case 'l':
5830 05171be4 2022-06-23 thomas case KEY_LEFT:
5831 05171be4 2022-06-23 thomas case 'h':
5832 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5833 05171be4 2022-06-23 thomas break;
5834 64453f7e 2020-11-21 stsp case 'a':
5835 3dbaef42 2020-11-24 stsp case 'w':
5836 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5837 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5838 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5839 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5840 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5841 f2d06bef 2023-01-23 thomas }
5842 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5843 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5844 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5845 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5846 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5847 f2d06bef 2023-01-23 thomas }
5848 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5849 912a3f79 2021-08-30 j break;
5850 912a3f79 2021-08-30 j case 'g':
5851 912a3f79 2021-08-30 j case KEY_HOME:
5852 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5853 07b0611c 2022-06-23 thomas view->count = 0;
5854 64453f7e 2020-11-21 stsp break;
5855 912a3f79 2021-08-30 j case 'G':
5856 912a3f79 2021-08-30 j case KEY_END:
5857 07b0611c 2022-06-23 thomas view->count = 0;
5858 912a3f79 2021-08-30 j if (s->eof)
5859 912a3f79 2021-08-30 j break;
5860 912a3f79 2021-08-30 j
5861 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5862 912a3f79 2021-08-30 j s->eof = 1;
5863 912a3f79 2021-08-30 j break;
5864 1e37a5c2 2019-05-12 jcs case 'k':
5865 1e37a5c2 2019-05-12 jcs case KEY_UP:
5866 f7140bf5 2021-10-17 thomas case CTRL('p'):
5867 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5868 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5869 07b0611c 2022-06-23 thomas else
5870 07b0611c 2022-06-23 thomas view->count = 0;
5871 1e37a5c2 2019-05-12 jcs break;
5872 70f17a53 2022-06-13 thomas case CTRL('u'):
5873 23427b14 2022-06-23 thomas case 'u':
5874 70f17a53 2022-06-13 thomas nscroll /= 2;
5875 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5876 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5877 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5878 1c5e5faa 2022-06-23 thomas case 'b':
5879 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5880 07b0611c 2022-06-23 thomas view->count = 0;
5881 26ed57b2 2018-05-19 stsp break;
5882 07b0611c 2022-06-23 thomas }
5883 1e37a5c2 2019-05-12 jcs i = 0;
5884 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5885 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5886 1e37a5c2 2019-05-12 jcs break;
5887 1e37a5c2 2019-05-12 jcs case 'j':
5888 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5889 f7140bf5 2021-10-17 thomas case CTRL('n'):
5890 1e37a5c2 2019-05-12 jcs if (!s->eof)
5891 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5892 07b0611c 2022-06-23 thomas else
5893 07b0611c 2022-06-23 thomas view->count = 0;
5894 1e37a5c2 2019-05-12 jcs break;
5895 70f17a53 2022-06-13 thomas case CTRL('d'):
5896 23427b14 2022-06-23 thomas case 'd':
5897 70f17a53 2022-06-13 thomas nscroll /= 2;
5898 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5899 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5900 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5901 1c5e5faa 2022-06-23 thomas case 'f':
5902 1e37a5c2 2019-05-12 jcs case ' ':
5903 07b0611c 2022-06-23 thomas if (s->eof) {
5904 07b0611c 2022-06-23 thomas view->count = 0;
5905 1e37a5c2 2019-05-12 jcs break;
5906 07b0611c 2022-06-23 thomas }
5907 1e37a5c2 2019-05-12 jcs i = 0;
5908 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5909 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5910 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5911 826082fe 2020-12-10 stsp if (linelen == -1) {
5912 826082fe 2020-12-10 stsp if (feof(s->f)) {
5913 826082fe 2020-12-10 stsp s->eof = 1;
5914 826082fe 2020-12-10 stsp } else
5915 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5916 34bc9ec9 2019-02-22 stsp break;
5917 826082fe 2020-12-10 stsp }
5918 1e37a5c2 2019-05-12 jcs }
5919 826082fe 2020-12-10 stsp free(line);
5920 1e37a5c2 2019-05-12 jcs break;
5921 82c78e96 2022-08-06 thomas case '(':
5922 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5923 82c78e96 2022-08-06 thomas break;
5924 82c78e96 2022-08-06 thomas case ')':
5925 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5926 82c78e96 2022-08-06 thomas break;
5927 82c78e96 2022-08-06 thomas case '{':
5928 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5929 82c78e96 2022-08-06 thomas break;
5930 82c78e96 2022-08-06 thomas case '}':
5931 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5932 82c78e96 2022-08-06 thomas break;
5933 1e37a5c2 2019-05-12 jcs case '[':
5934 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5935 1e37a5c2 2019-05-12 jcs s->diff_context--;
5936 aa61903a 2021-12-31 thomas s->matched_line = 0;
5937 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5938 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5939 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5940 27829c9e 2020-11-21 stsp s->nlines) {
5941 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5942 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5943 27829c9e 2020-11-21 stsp }
5944 07b0611c 2022-06-23 thomas } else
5945 07b0611c 2022-06-23 thomas view->count = 0;
5946 1e37a5c2 2019-05-12 jcs break;
5947 1e37a5c2 2019-05-12 jcs case ']':
5948 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5949 1e37a5c2 2019-05-12 jcs s->diff_context++;
5950 aa61903a 2021-12-31 thomas s->matched_line = 0;
5951 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5952 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5953 07b0611c 2022-06-23 thomas } else
5954 07b0611c 2022-06-23 thomas view->count = 0;
5955 1e37a5c2 2019-05-12 jcs break;
5956 1e37a5c2 2019-05-12 jcs case '<':
5957 1e37a5c2 2019-05-12 jcs case ',':
5958 777aae21 2022-07-20 thomas case 'K':
5959 4fc71f3b 2022-07-12 thomas up = 1;
5960 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5961 4fc71f3b 2022-07-12 thomas case '>':
5962 4fc71f3b 2022-07-12 thomas case '.':
5963 777aae21 2022-07-20 thomas case 'J':
5964 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5965 07b0611c 2022-06-23 thomas view->count = 0;
5966 48ae06ee 2018-10-18 stsp break;
5967 07b0611c 2022-06-23 thomas }
5968 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5969 6524637e 2019-02-21 stsp
5970 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5971 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5972 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5973 15a087fe 2019-02-21 stsp
5974 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5975 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5976 4fc71f3b 2022-07-12 thomas if (err)
5977 4fc71f3b 2022-07-12 thomas break;
5978 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5979 15a087fe 2019-02-21 stsp
5980 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5981 4fc71f3b 2022-07-12 thomas break;
5982 15a087fe 2019-02-21 stsp
5983 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5984 4fc71f3b 2022-07-12 thomas if (err)
5985 4fc71f3b 2022-07-12 thomas break;
5986 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5987 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5988 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5989 5e224a3e 2019-02-22 stsp
5990 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5991 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5992 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5993 4fc71f3b 2022-07-12 thomas
5994 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5995 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5996 4fc71f3b 2022-07-12 thomas if (err)
5997 4fc71f3b 2022-07-12 thomas break;
5998 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5999 5a8b5076 2020-12-05 stsp
6000 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
6001 4fc71f3b 2022-07-12 thomas break;
6002 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
6003 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
6004 4fc71f3b 2022-07-12 thomas bs->selected_line);
6005 4fc71f3b 2022-07-12 thomas if (id == NULL)
6006 4fc71f3b 2022-07-12 thomas break;
6007 15a087fe 2019-02-21 stsp
6008 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
6009 4fc71f3b 2022-07-12 thomas break;
6010 15a087fe 2019-02-21 stsp
6011 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
6012 4fc71f3b 2022-07-12 thomas if (err)
6013 4fc71f3b 2022-07-12 thomas break;
6014 4fc71f3b 2022-07-12 thomas }
6015 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6016 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
6017 aa61903a 2021-12-31 thomas s->matched_line = 0;
6018 05171be4 2022-06-23 thomas view->x = 0;
6019 1e37a5c2 2019-05-12 jcs
6020 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
6021 1e37a5c2 2019-05-12 jcs err = create_diff(s);
6022 1e37a5c2 2019-05-12 jcs break;
6023 1e37a5c2 2019-05-12 jcs default:
6024 07b0611c 2022-06-23 thomas view->count = 0;
6025 1e37a5c2 2019-05-12 jcs break;
6026 26ed57b2 2018-05-19 stsp }
6027 e5a0f69f 2018-08-18 stsp
6028 bcbd79e2 2018-08-19 stsp return err;
6029 26ed57b2 2018-05-19 stsp }
6030 26ed57b2 2018-05-19 stsp
6031 4ed7e80c 2018-05-20 stsp static const struct got_error *
6032 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
6033 9f7d7167 2018-04-29 stsp {
6034 1d98034b 2023-04-14 thomas const struct got_error *error;
6035 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
6036 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
6037 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
6038 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
6039 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
6040 66b04f8f 2023-07-19 thomas char *keyword_idstr1 = NULL, *keyword_idstr2 = NULL;
6041 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
6042 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
6043 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
6044 3dbaef42 2020-11-24 stsp const char *errstr;
6045 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
6046 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6047 70ac5f84 2019-03-28 stsp
6048 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
6049 26ed57b2 2018-05-19 stsp switch (ch) {
6050 64453f7e 2020-11-21 stsp case 'a':
6051 64453f7e 2020-11-21 stsp force_text_diff = 1;
6052 3dbaef42 2020-11-24 stsp break;
6053 3dbaef42 2020-11-24 stsp case 'C':
6054 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
6055 3dbaef42 2020-11-24 stsp &errstr);
6056 3dbaef42 2020-11-24 stsp if (errstr != NULL)
6057 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
6058 8f666e67 2022-02-12 thomas errstr, errstr);
6059 64453f7e 2020-11-21 stsp break;
6060 09b5bff8 2020-02-23 naddy case 'r':
6061 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
6062 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
6063 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
6064 09b5bff8 2020-02-23 naddy optarg);
6065 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
6066 09b5bff8 2020-02-23 naddy break;
6067 3dbaef42 2020-11-24 stsp case 'w':
6068 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
6069 3dbaef42 2020-11-24 stsp break;
6070 26ed57b2 2018-05-19 stsp default:
6071 17020d27 2019-03-07 stsp usage_diff();
6072 26ed57b2 2018-05-19 stsp /* NOTREACHED */
6073 26ed57b2 2018-05-19 stsp }
6074 26ed57b2 2018-05-19 stsp }
6075 26ed57b2 2018-05-19 stsp
6076 26ed57b2 2018-05-19 stsp argc -= optind;
6077 26ed57b2 2018-05-19 stsp argv += optind;
6078 26ed57b2 2018-05-19 stsp
6079 26ed57b2 2018-05-19 stsp if (argc == 0) {
6080 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
6081 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
6082 15a94983 2018-12-23 stsp id_str1 = argv[0];
6083 15a94983 2018-12-23 stsp id_str2 = argv[1];
6084 26ed57b2 2018-05-19 stsp } else
6085 26ed57b2 2018-05-19 stsp usage_diff();
6086 eb6600df 2019-01-04 stsp
6087 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6088 7cd52833 2022-06-23 thomas if (error)
6089 7cd52833 2022-06-23 thomas goto done;
6090 7cd52833 2022-06-23 thomas
6091 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
6092 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6093 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6094 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6095 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
6096 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6097 c156c7a4 2020-12-18 stsp goto done;
6098 a273ac94 2020-02-23 naddy if (worktree)
6099 a273ac94 2020-02-23 naddy repo_path =
6100 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
6101 a273ac94 2020-02-23 naddy else
6102 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
6103 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6104 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6105 c156c7a4 2020-12-18 stsp goto done;
6106 c156c7a4 2020-12-18 stsp }
6107 a273ac94 2020-02-23 naddy }
6108 a273ac94 2020-02-23 naddy
6109 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6110 eb6600df 2019-01-04 stsp if (error)
6111 eb6600df 2019-01-04 stsp goto done;
6112 26ed57b2 2018-05-19 stsp
6113 557d3365 2023-04-14 thomas init_curses();
6114 a273ac94 2020-02-23 naddy
6115 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6116 51a10b52 2020-12-26 stsp if (error)
6117 51a10b52 2020-12-26 stsp goto done;
6118 51a10b52 2020-12-26 stsp
6119 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6120 26ed57b2 2018-05-19 stsp if (error)
6121 26ed57b2 2018-05-19 stsp goto done;
6122 26ed57b2 2018-05-19 stsp
6123 66b04f8f 2023-07-19 thomas if (id_str1 != NULL) {
6124 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr1, id_str1,
6125 66b04f8f 2023-07-19 thomas repo, worktree);
6126 66b04f8f 2023-07-19 thomas if (error != NULL)
6127 66b04f8f 2023-07-19 thomas goto done;
6128 66b04f8f 2023-07-19 thomas if (keyword_idstr1 != NULL)
6129 66b04f8f 2023-07-19 thomas id_str1 = keyword_idstr1;
6130 66b04f8f 2023-07-19 thomas }
6131 66b04f8f 2023-07-19 thomas if (id_str2 != NULL) {
6132 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr2, id_str2,
6133 66b04f8f 2023-07-19 thomas repo, worktree);
6134 66b04f8f 2023-07-19 thomas if (error != NULL)
6135 66b04f8f 2023-07-19 thomas goto done;
6136 66b04f8f 2023-07-19 thomas if (keyword_idstr2 != NULL)
6137 66b04f8f 2023-07-19 thomas id_str2 = keyword_idstr2;
6138 66b04f8f 2023-07-19 thomas }
6139 66b04f8f 2023-07-19 thomas
6140 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
6141 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6142 26ed57b2 2018-05-19 stsp if (error)
6143 26ed57b2 2018-05-19 stsp goto done;
6144 26ed57b2 2018-05-19 stsp
6145 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
6146 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6147 26ed57b2 2018-05-19 stsp if (error)
6148 26ed57b2 2018-05-19 stsp goto done;
6149 26ed57b2 2018-05-19 stsp
6150 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6151 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
6152 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6153 ea5e7bb5 2018-08-01 stsp goto done;
6154 ea5e7bb5 2018-08-01 stsp }
6155 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6156 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
6157 5dc9f4bc 2018-08-04 stsp if (error)
6158 5dc9f4bc 2018-08-04 stsp goto done;
6159 349dfd1e 2023-07-23 thomas
6160 349dfd1e 2023-07-23 thomas if (worktree) {
6161 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
6162 349dfd1e 2023-07-23 thomas if (error != NULL)
6163 349dfd1e 2023-07-23 thomas goto done;
6164 349dfd1e 2023-07-23 thomas }
6165 349dfd1e 2023-07-23 thomas
6166 557d3365 2023-04-14 thomas error = view_loop(view);
6167 349dfd1e 2023-07-23 thomas
6168 26ed57b2 2018-05-19 stsp done:
6169 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
6170 66b04f8f 2023-07-19 thomas free(keyword_idstr1);
6171 66b04f8f 2023-07-19 thomas free(keyword_idstr2);
6172 3dbaef42 2020-11-24 stsp free(label1);
6173 3dbaef42 2020-11-24 stsp free(label2);
6174 c02c541e 2019-03-29 stsp free(repo_path);
6175 a273ac94 2020-02-23 naddy free(cwd);
6176 1d0f4054 2021-06-17 stsp if (repo) {
6177 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6178 1d0f4054 2021-06-17 stsp if (error == NULL)
6179 1d0f4054 2021-06-17 stsp error = close_err;
6180 1d0f4054 2021-06-17 stsp }
6181 a273ac94 2020-02-23 naddy if (worktree)
6182 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6183 7cd52833 2022-06-23 thomas if (pack_fds) {
6184 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6185 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6186 7cd52833 2022-06-23 thomas if (error == NULL)
6187 7cd52833 2022-06-23 thomas error = pack_err;
6188 b85a3496 2023-04-14 thomas }
6189 51a10b52 2020-12-26 stsp tog_free_refs();
6190 26ed57b2 2018-05-19 stsp return error;
6191 9f7d7167 2018-04-29 stsp }
6192 9f7d7167 2018-04-29 stsp
6193 4ed7e80c 2018-05-20 stsp __dead static void
6194 9f7d7167 2018-04-29 stsp usage_blame(void)
6195 9f7d7167 2018-04-29 stsp {
6196 80ddbec8 2018-04-29 stsp endwin();
6197 91198554 2022-06-23 thomas fprintf(stderr,
6198 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6199 9f7d7167 2018-04-29 stsp getprogname());
6200 9f7d7167 2018-04-29 stsp exit(1);
6201 9f7d7167 2018-04-29 stsp }
6202 84451b3e 2018-07-10 stsp
6203 84451b3e 2018-07-10 stsp struct tog_blame_line {
6204 84451b3e 2018-07-10 stsp int annotated;
6205 84451b3e 2018-07-10 stsp struct got_object_id *id;
6206 84451b3e 2018-07-10 stsp };
6207 9f7d7167 2018-04-29 stsp
6208 4ed7e80c 2018-05-20 stsp static const struct got_error *
6209 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6210 84451b3e 2018-07-10 stsp {
6211 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6212 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6213 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6214 84451b3e 2018-07-10 stsp const struct got_error *err;
6215 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6216 826082fe 2020-12-10 stsp char *line = NULL;
6217 826082fe 2020-12-10 stsp size_t linesize = 0;
6218 826082fe 2020-12-10 stsp ssize_t linelen;
6219 84451b3e 2018-07-10 stsp wchar_t *wline;
6220 27a741e5 2019-09-11 stsp int width;
6221 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6222 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6223 ab089a2a 2018-07-12 stsp char *id_str;
6224 11b20872 2019-11-08 stsp struct tog_color *tc;
6225 ab089a2a 2018-07-12 stsp
6226 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6227 ab089a2a 2018-07-12 stsp if (err)
6228 ab089a2a 2018-07-12 stsp return err;
6229 84451b3e 2018-07-10 stsp
6230 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6231 f7d12f7e 2018-08-01 stsp werase(view->window);
6232 84451b3e 2018-07-10 stsp
6233 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6234 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6235 ab089a2a 2018-07-12 stsp free(id_str);
6236 ab089a2a 2018-07-12 stsp return err;
6237 ab089a2a 2018-07-12 stsp }
6238 ab089a2a 2018-07-12 stsp
6239 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6240 ab089a2a 2018-07-12 stsp free(line);
6241 2550e4c3 2018-07-13 stsp line = NULL;
6242 1cae65b4 2019-09-22 stsp if (err)
6243 1cae65b4 2019-09-22 stsp return err;
6244 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6245 a3404814 2018-09-02 stsp wstandout(view->window);
6246 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6247 11b20872 2019-11-08 stsp if (tc)
6248 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6249 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6250 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6251 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6252 11b20872 2019-11-08 stsp if (tc)
6253 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6254 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6255 a3404814 2018-09-02 stsp wstandend(view->window);
6256 2550e4c3 2018-07-13 stsp free(wline);
6257 2550e4c3 2018-07-13 stsp wline = NULL;
6258 ab089a2a 2018-07-12 stsp
6259 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6260 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6261 07dd3ed3 2022-08-06 thomas
6262 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6263 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6264 1134ebde 2023-04-22 thomas int rc;
6265 1134ebde 2023-04-22 thomas
6266 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6267 1134ebde 2023-04-22 thomas if (rc)
6268 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6269 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6270 1134ebde 2023-04-22 thomas }
6271 1134ebde 2023-04-22 thomas
6272 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6273 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6274 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6275 ab089a2a 2018-07-12 stsp free(id_str);
6276 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6277 ab089a2a 2018-07-12 stsp }
6278 ab089a2a 2018-07-12 stsp free(id_str);
6279 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6280 3f60a8ef 2018-07-10 stsp free(line);
6281 2550e4c3 2018-07-13 stsp line = NULL;
6282 3f60a8ef 2018-07-10 stsp if (err)
6283 3f60a8ef 2018-07-10 stsp return err;
6284 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6285 2550e4c3 2018-07-13 stsp free(wline);
6286 2550e4c3 2018-07-13 stsp wline = NULL;
6287 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6288 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6289 3f60a8ef 2018-07-10 stsp
6290 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6291 05171be4 2022-06-23 thomas view->maxx = 0;
6292 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6293 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6294 826082fe 2020-12-10 stsp if (linelen == -1) {
6295 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6296 826082fe 2020-12-10 stsp s->eof = 1;
6297 826082fe 2020-12-10 stsp break;
6298 826082fe 2020-12-10 stsp }
6299 84451b3e 2018-07-10 stsp free(line);
6300 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6301 84451b3e 2018-07-10 stsp }
6302 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6303 07dd3ed3 2022-08-06 thomas continue;
6304 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6305 826082fe 2020-12-10 stsp continue;
6306 cbea3800 2022-06-23 thomas
6307 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6308 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6309 cbea3800 2022-06-23 thomas if (err) {
6310 cbea3800 2022-06-23 thomas free(line);
6311 cbea3800 2022-06-23 thomas return err;
6312 cbea3800 2022-06-23 thomas }
6313 cbea3800 2022-06-23 thomas free(wline);
6314 cbea3800 2022-06-23 thomas wline = NULL;
6315 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6316 84451b3e 2018-07-10 stsp
6317 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6318 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6319 b700b5d6 2018-07-10 stsp
6320 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6321 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6322 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6323 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6324 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6325 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6326 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6327 8d0fe45a 2019-08-12 stsp char *id_str;
6328 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6329 91198554 2022-06-23 thomas blame_line->id);
6330 8d0fe45a 2019-08-12 stsp if (err) {
6331 8d0fe45a 2019-08-12 stsp free(line);
6332 8d0fe45a 2019-08-12 stsp return err;
6333 8d0fe45a 2019-08-12 stsp }
6334 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6335 11b20872 2019-11-08 stsp if (tc)
6336 11b20872 2019-11-08 stsp wattr_on(view->window,
6337 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6338 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6339 11b20872 2019-11-08 stsp if (tc)
6340 11b20872 2019-11-08 stsp wattr_off(view->window,
6341 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6342 8d0fe45a 2019-08-12 stsp free(id_str);
6343 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6344 8d0fe45a 2019-08-12 stsp } else {
6345 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6346 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6347 84451b3e 2018-07-10 stsp }
6348 ee41ec32 2018-07-10 stsp } else {
6349 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6350 ee41ec32 2018-07-10 stsp prev_id = NULL;
6351 ee41ec32 2018-07-10 stsp }
6352 84451b3e 2018-07-10 stsp
6353 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6354 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6355 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6356 27a741e5 2019-09-11 stsp
6357 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6358 41605754 2020-11-12 stsp width = 9;
6359 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6360 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6361 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6362 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6363 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6364 41605754 2020-11-12 stsp if (err) {
6365 41605754 2020-11-12 stsp free(line);
6366 41605754 2020-11-12 stsp return err;
6367 41605754 2020-11-12 stsp }
6368 41605754 2020-11-12 stsp width += 9;
6369 41605754 2020-11-12 stsp } else {
6370 923086fd 2022-06-23 thomas int skip;
6371 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6372 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6373 923086fd 2022-06-23 thomas if (err) {
6374 923086fd 2022-06-23 thomas free(line);
6375 923086fd 2022-06-23 thomas return err;
6376 05171be4 2022-06-23 thomas }
6377 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6378 02bce7e2 2022-06-23 thomas width += 9;
6379 41605754 2020-11-12 stsp free(wline);
6380 41605754 2020-11-12 stsp wline = NULL;
6381 41605754 2020-11-12 stsp }
6382 41605754 2020-11-12 stsp
6383 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6384 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6385 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6386 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6387 84451b3e 2018-07-10 stsp }
6388 826082fe 2020-12-10 stsp free(line);
6389 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6390 84451b3e 2018-07-10 stsp
6391 a5d43cac 2022-07-01 thomas view_border(view);
6392 84451b3e 2018-07-10 stsp
6393 84451b3e 2018-07-10 stsp return NULL;
6394 84451b3e 2018-07-10 stsp }
6395 84451b3e 2018-07-10 stsp
6396 84451b3e 2018-07-10 stsp static const struct got_error *
6397 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6398 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6399 84451b3e 2018-07-10 stsp {
6400 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6401 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6402 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6403 1a76625f 2018-10-22 stsp int errcode;
6404 84451b3e 2018-07-10 stsp
6405 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6406 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6407 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6408 84451b3e 2018-07-10 stsp
6409 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6410 1a76625f 2018-10-22 stsp if (errcode)
6411 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6412 84451b3e 2018-07-10 stsp
6413 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6414 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6415 d68a0a7d 2018-07-10 stsp goto done;
6416 d68a0a7d 2018-07-10 stsp }
6417 d68a0a7d 2018-07-10 stsp
6418 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6419 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6420 d68a0a7d 2018-07-10 stsp
6421 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6422 d68a0a7d 2018-07-10 stsp if (line->annotated)
6423 d68a0a7d 2018-07-10 stsp goto done;
6424 d68a0a7d 2018-07-10 stsp
6425 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6426 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6427 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6428 84451b3e 2018-07-10 stsp goto done;
6429 84451b3e 2018-07-10 stsp }
6430 84451b3e 2018-07-10 stsp line->annotated = 1;
6431 84451b3e 2018-07-10 stsp done:
6432 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6433 1a76625f 2018-10-22 stsp if (errcode)
6434 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6435 84451b3e 2018-07-10 stsp return err;
6436 84451b3e 2018-07-10 stsp }
6437 84451b3e 2018-07-10 stsp
6438 84451b3e 2018-07-10 stsp static void *
6439 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6440 84451b3e 2018-07-10 stsp {
6441 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6442 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6443 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6444 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6445 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6446 00a8878e 2022-07-01 thomas
6447 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6448 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6449 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6450 9117a7b7 2022-07-01 thomas
6451 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6452 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6453 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6454 9117a7b7 2022-07-01 thomas goto done;
6455 9117a7b7 2022-07-01 thomas }
6456 18430de3 2018-07-10 stsp
6457 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6458 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6459 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6460 9117a7b7 2022-07-01 thomas goto done;
6461 9117a7b7 2022-07-01 thomas }
6462 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6463 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6464 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6465 9117a7b7 2022-07-01 thomas goto done;
6466 9117a7b7 2022-07-01 thomas }
6467 9117a7b7 2022-07-01 thomas
6468 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6469 61266923 2020-01-14 stsp if (err)
6470 9117a7b7 2022-07-01 thomas goto done;
6471 61266923 2020-01-14 stsp
6472 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6473 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6474 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6475 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6476 fc06ba56 2019-08-22 stsp err = NULL;
6477 18430de3 2018-07-10 stsp
6478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6479 9117a7b7 2022-07-01 thomas if (errcode) {
6480 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6481 9117a7b7 2022-07-01 thomas goto done;
6482 9117a7b7 2022-07-01 thomas }
6483 18430de3 2018-07-10 stsp
6484 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6485 1d0f4054 2021-06-17 stsp if (err == NULL)
6486 1d0f4054 2021-06-17 stsp err = close_err;
6487 c9beca56 2018-07-22 stsp ta->repo = NULL;
6488 c9beca56 2018-07-22 stsp *ta->complete = 1;
6489 1134ebde 2023-04-22 thomas
6490 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6491 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6492 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6493 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6494 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6495 1134ebde 2023-04-22 thomas }
6496 18430de3 2018-07-10 stsp
6497 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6498 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6499 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6500 18430de3 2018-07-10 stsp
6501 9117a7b7 2022-07-01 thomas done:
6502 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6503 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6504 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6505 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6506 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6507 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6508 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6509 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6510 00a8878e 2022-07-01 thomas
6511 18430de3 2018-07-10 stsp return (void *)err;
6512 84451b3e 2018-07-10 stsp }
6513 84451b3e 2018-07-10 stsp
6514 245d91c1 2018-07-12 stsp static struct got_object_id *
6515 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6516 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6517 245d91c1 2018-07-12 stsp {
6518 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6519 8d0fe45a 2019-08-12 stsp
6520 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6521 8d0fe45a 2019-08-12 stsp return NULL;
6522 b880a918 2018-07-10 stsp
6523 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6524 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6525 4fc71f3b 2022-07-12 thomas return NULL;
6526 4fc71f3b 2022-07-12 thomas
6527 4fc71f3b 2022-07-12 thomas return line->id;
6528 4fc71f3b 2022-07-12 thomas }
6529 4fc71f3b 2022-07-12 thomas
6530 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6531 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6532 4fc71f3b 2022-07-12 thomas int lineno)
6533 4fc71f3b 2022-07-12 thomas {
6534 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6535 4fc71f3b 2022-07-12 thomas
6536 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6537 4fc71f3b 2022-07-12 thomas return NULL;
6538 4fc71f3b 2022-07-12 thomas
6539 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6540 245d91c1 2018-07-12 stsp if (!line->annotated)
6541 245d91c1 2018-07-12 stsp return NULL;
6542 245d91c1 2018-07-12 stsp
6543 245d91c1 2018-07-12 stsp return line->id;
6544 b880a918 2018-07-10 stsp }
6545 245d91c1 2018-07-12 stsp
6546 b880a918 2018-07-10 stsp static const struct got_error *
6547 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6548 a70480e0 2018-06-23 stsp {
6549 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6550 245d91c1 2018-07-12 stsp int i;
6551 245d91c1 2018-07-12 stsp
6552 245d91c1 2018-07-12 stsp if (blame->thread) {
6553 1a76625f 2018-10-22 stsp int errcode;
6554 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6555 1a76625f 2018-10-22 stsp if (errcode)
6556 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6557 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6558 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6559 1a76625f 2018-10-22 stsp if (errcode)
6560 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6561 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6562 1a76625f 2018-10-22 stsp if (errcode)
6563 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6564 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6565 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6566 245d91c1 2018-07-12 stsp err = NULL;
6567 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6568 245d91c1 2018-07-12 stsp }
6569 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6570 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6571 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6572 1d0f4054 2021-06-17 stsp if (err == NULL)
6573 1d0f4054 2021-06-17 stsp err = close_err;
6574 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6575 245d91c1 2018-07-12 stsp }
6576 245d91c1 2018-07-12 stsp if (blame->f) {
6577 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6578 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6579 245d91c1 2018-07-12 stsp blame->f = NULL;
6580 245d91c1 2018-07-12 stsp }
6581 57670559 2018-12-23 stsp if (blame->lines) {
6582 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6583 57670559 2018-12-23 stsp free(blame->lines[i].id);
6584 57670559 2018-12-23 stsp free(blame->lines);
6585 57670559 2018-12-23 stsp blame->lines = NULL;
6586 57670559 2018-12-23 stsp }
6587 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6588 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6589 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6590 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6591 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6592 7cd52833 2022-06-23 thomas if (err == NULL)
6593 7cd52833 2022-06-23 thomas err = pack_err;
6594 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6595 7cd52833 2022-06-23 thomas }
6596 245d91c1 2018-07-12 stsp return err;
6597 245d91c1 2018-07-12 stsp }
6598 245d91c1 2018-07-12 stsp
6599 245d91c1 2018-07-12 stsp static const struct got_error *
6600 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6601 fc06ba56 2019-08-22 stsp {
6602 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6603 fc06ba56 2019-08-22 stsp int *done = arg;
6604 fc06ba56 2019-08-22 stsp int errcode;
6605 fc06ba56 2019-08-22 stsp
6606 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6607 fc06ba56 2019-08-22 stsp if (errcode)
6608 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6609 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6610 fc06ba56 2019-08-22 stsp
6611 fc06ba56 2019-08-22 stsp if (*done)
6612 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6613 fc06ba56 2019-08-22 stsp
6614 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6615 fc06ba56 2019-08-22 stsp if (errcode)
6616 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6617 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6618 fc06ba56 2019-08-22 stsp
6619 fc06ba56 2019-08-22 stsp return err;
6620 fc06ba56 2019-08-22 stsp }
6621 fc06ba56 2019-08-22 stsp
6622 fc06ba56 2019-08-22 stsp static const struct got_error *
6623 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6624 245d91c1 2018-07-12 stsp {
6625 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6626 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6627 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6628 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6629 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6630 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6631 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6632 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6633 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6634 a70480e0 2018-06-23 stsp
6635 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6636 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6637 27d434c2 2018-09-15 stsp if (err)
6638 15a94983 2018-12-23 stsp return err;
6639 f4ae6ddb 2022-07-01 thomas
6640 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6641 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6642 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6643 f4ae6ddb 2022-07-01 thomas goto done;
6644 f4ae6ddb 2022-07-01 thomas }
6645 945f9229 2022-04-16 thomas
6646 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6647 945f9229 2022-04-16 thomas if (err)
6648 945f9229 2022-04-16 thomas goto done;
6649 27d434c2 2018-09-15 stsp
6650 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6651 84451b3e 2018-07-10 stsp if (err)
6652 84451b3e 2018-07-10 stsp goto done;
6653 27d434c2 2018-09-15 stsp
6654 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6655 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6656 84451b3e 2018-07-10 stsp goto done;
6657 84451b3e 2018-07-10 stsp }
6658 a70480e0 2018-06-23 stsp
6659 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6660 a70480e0 2018-06-23 stsp if (err)
6661 a70480e0 2018-06-23 stsp goto done;
6662 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6663 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6664 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6665 84451b3e 2018-07-10 stsp goto done;
6666 84451b3e 2018-07-10 stsp }
6667 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6668 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6669 1fddf795 2021-01-20 stsp if (err)
6670 1fddf795 2021-01-20 stsp goto done;
6671 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6672 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6673 84451b3e 2018-07-10 stsp goto done;
6674 1fddf795 2021-01-20 stsp }
6675 b02560ec 2019-08-19 stsp
6676 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6677 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6678 b02560ec 2019-08-19 stsp blame->nlines--;
6679 a70480e0 2018-06-23 stsp
6680 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6681 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6682 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6683 84451b3e 2018-07-10 stsp goto done;
6684 84451b3e 2018-07-10 stsp }
6685 a70480e0 2018-06-23 stsp
6686 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6687 bd24772e 2018-07-11 stsp if (err)
6688 bd24772e 2018-07-11 stsp goto done;
6689 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6690 7cd52833 2022-06-23 thomas pack_fds);
6691 7cd52833 2022-06-23 thomas if (err)
6692 7cd52833 2022-06-23 thomas goto done;
6693 bd24772e 2018-07-11 stsp
6694 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6695 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6696 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6697 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6698 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6699 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6700 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6701 245d91c1 2018-07-12 stsp goto done;
6702 245d91c1 2018-07-12 stsp }
6703 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6704 245d91c1 2018-07-12 stsp
6705 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6706 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6707 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6708 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6709 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6710 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6711 a5388363 2020-12-01 naddy s->blame_complete = 0;
6712 f5a09613 2020-12-13 naddy
6713 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6714 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6715 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6716 f5a09613 2020-12-13 naddy s->selected_line = 1;
6717 f5a09613 2020-12-13 naddy }
6718 aa61903a 2021-12-31 thomas s->matched_line = 0;
6719 245d91c1 2018-07-12 stsp
6720 245d91c1 2018-07-12 stsp done:
6721 945f9229 2022-04-16 thomas if (commit)
6722 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6723 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6724 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6725 245d91c1 2018-07-12 stsp if (blob)
6726 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6727 27d434c2 2018-09-15 stsp free(obj_id);
6728 245d91c1 2018-07-12 stsp if (err)
6729 245d91c1 2018-07-12 stsp stop_blame(blame);
6730 245d91c1 2018-07-12 stsp return err;
6731 245d91c1 2018-07-12 stsp }
6732 245d91c1 2018-07-12 stsp
6733 245d91c1 2018-07-12 stsp static const struct got_error *
6734 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6735 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6736 245d91c1 2018-07-12 stsp {
6737 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6738 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6739 dbc6a6b6 2018-07-12 stsp
6740 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6741 245d91c1 2018-07-12 stsp
6742 c4843652 2019-08-12 stsp s->path = strdup(path);
6743 c4843652 2019-08-12 stsp if (s->path == NULL)
6744 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6745 c4843652 2019-08-12 stsp
6746 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6747 c4843652 2019-08-12 stsp if (err) {
6748 c4843652 2019-08-12 stsp free(s->path);
6749 7cbe629d 2018-08-04 stsp return err;
6750 c4843652 2019-08-12 stsp }
6751 245d91c1 2018-07-12 stsp
6752 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6753 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6754 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6755 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6756 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6757 fb2756b9 2018-08-04 stsp s->repo = repo;
6758 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6759 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6760 7cbe629d 2018-08-04 stsp
6761 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6762 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6763 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6764 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6765 11b20872 2019-11-08 stsp if (err)
6766 11b20872 2019-11-08 stsp return err;
6767 11b20872 2019-11-08 stsp }
6768 11b20872 2019-11-08 stsp
6769 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6770 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6771 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6772 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6773 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6774 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6775 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6776 1134ebde 2023-04-22 thomas
6777 1134ebde 2023-04-22 thomas if (using_mock_io) {
6778 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6779 1134ebde 2023-04-22 thomas int rc;
6780 1134ebde 2023-04-22 thomas
6781 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6782 1134ebde 2023-04-22 thomas if (rc)
6783 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6784 1134ebde 2023-04-22 thomas }
6785 e5a0f69f 2018-08-18 stsp
6786 a5388363 2020-12-01 naddy return run_blame(view);
6787 7cbe629d 2018-08-04 stsp }
6788 7cbe629d 2018-08-04 stsp
6789 e5a0f69f 2018-08-18 stsp static const struct got_error *
6790 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6791 7cbe629d 2018-08-04 stsp {
6792 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6793 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6794 7cbe629d 2018-08-04 stsp
6795 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6796 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6797 e5a0f69f 2018-08-18 stsp
6798 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6799 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6800 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6801 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6802 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6803 1134ebde 2023-04-22 thomas }
6804 1134ebde 2023-04-22 thomas
6805 1134ebde 2023-04-22 thomas if (using_mock_io) {
6806 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6807 1134ebde 2023-04-22 thomas int rc;
6808 1134ebde 2023-04-22 thomas
6809 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6810 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6811 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6812 7cbe629d 2018-08-04 stsp }
6813 e5a0f69f 2018-08-18 stsp
6814 e5a0f69f 2018-08-18 stsp free(s->path);
6815 11b20872 2019-11-08 stsp free_colors(&s->colors);
6816 e5a0f69f 2018-08-18 stsp return err;
6817 7cbe629d 2018-08-04 stsp }
6818 7cbe629d 2018-08-04 stsp
6819 7cbe629d 2018-08-04 stsp static const struct got_error *
6820 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6821 6c4c42e0 2019-06-24 stsp {
6822 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6823 6c4c42e0 2019-06-24 stsp
6824 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6825 6c4c42e0 2019-06-24 stsp return NULL;
6826 2a7d3cd7 2022-09-17 thomas }
6827 2a7d3cd7 2022-09-17 thomas
6828 2a7d3cd7 2022-09-17 thomas static void
6829 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6830 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6831 2a7d3cd7 2022-09-17 thomas {
6832 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6833 2a7d3cd7 2022-09-17 thomas
6834 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6835 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6836 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6837 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6838 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6839 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6840 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6841 6c4c42e0 2019-06-24 stsp }
6842 6c4c42e0 2019-06-24 stsp
6843 6c4c42e0 2019-06-24 stsp static const struct got_error *
6844 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6845 7cbe629d 2018-08-04 stsp {
6846 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6847 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6848 2b380cc8 2018-10-24 stsp int errcode;
6849 2b380cc8 2018-10-24 stsp
6850 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6851 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6852 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6853 2b380cc8 2018-10-24 stsp if (errcode)
6854 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6855 51fe7530 2019-08-19 stsp
6856 557d3365 2023-04-14 thomas if (!using_mock_io)
6857 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6858 2b380cc8 2018-10-24 stsp }
6859 e5a0f69f 2018-08-18 stsp
6860 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6861 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6862 51fe7530 2019-08-19 stsp
6863 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6864 e5a0f69f 2018-08-18 stsp
6865 a5d43cac 2022-07-01 thomas view_border(view);
6866 e5a0f69f 2018-08-18 stsp return err;
6867 e5a0f69f 2018-08-18 stsp }
6868 e5a0f69f 2018-08-18 stsp
6869 e5a0f69f 2018-08-18 stsp static const struct got_error *
6870 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6871 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6872 eaeaa612 2022-07-20 thomas {
6873 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6874 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6875 eaeaa612 2022-07-20 thomas
6876 eaeaa612 2022-07-20 thomas *new_view = NULL;
6877 eaeaa612 2022-07-20 thomas
6878 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6879 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6880 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6881 eaeaa612 2022-07-20 thomas
6882 92845f09 2023-07-26 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0, NULL);
6883 eaeaa612 2022-07-20 thomas if (err)
6884 eaeaa612 2022-07-20 thomas view_close(log_view);
6885 eaeaa612 2022-07-20 thomas else
6886 eaeaa612 2022-07-20 thomas *new_view = log_view;
6887 eaeaa612 2022-07-20 thomas
6888 eaeaa612 2022-07-20 thomas return err;
6889 eaeaa612 2022-07-20 thomas }
6890 eaeaa612 2022-07-20 thomas
6891 eaeaa612 2022-07-20 thomas static const struct got_error *
6892 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6893 e5a0f69f 2018-08-18 stsp {
6894 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6895 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6896 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6897 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6898 a5d43cac 2022-07-01 thomas
6899 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6900 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6901 a5d43cac 2022-07-01 thomas --eos; /* border */
6902 7cbe629d 2018-08-04 stsp
6903 e5a0f69f 2018-08-18 stsp switch (ch) {
6904 05171be4 2022-06-23 thomas case '0':
6905 05171be4 2022-06-23 thomas case '$':
6906 05171be4 2022-06-23 thomas case KEY_RIGHT:
6907 05171be4 2022-06-23 thomas case 'l':
6908 05171be4 2022-06-23 thomas case KEY_LEFT:
6909 05171be4 2022-06-23 thomas case 'h':
6910 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6911 05171be4 2022-06-23 thomas break;
6912 1e37a5c2 2019-05-12 jcs case 'q':
6913 1e37a5c2 2019-05-12 jcs s->done = 1;
6914 4deef56f 2021-09-02 naddy break;
6915 4deef56f 2021-09-02 naddy case 'g':
6916 4deef56f 2021-09-02 naddy case KEY_HOME:
6917 4deef56f 2021-09-02 naddy s->selected_line = 1;
6918 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6919 07b0611c 2022-06-23 thomas view->count = 0;
6920 4deef56f 2021-09-02 naddy break;
6921 4deef56f 2021-09-02 naddy case 'G':
6922 4deef56f 2021-09-02 naddy case KEY_END:
6923 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6924 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6925 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6926 4deef56f 2021-09-02 naddy } else {
6927 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6928 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6929 4deef56f 2021-09-02 naddy }
6930 07b0611c 2022-06-23 thomas view->count = 0;
6931 1e37a5c2 2019-05-12 jcs break;
6932 1e37a5c2 2019-05-12 jcs case 'k':
6933 1e37a5c2 2019-05-12 jcs case KEY_UP:
6934 f7140bf5 2021-10-17 thomas case CTRL('p'):
6935 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6936 1e37a5c2 2019-05-12 jcs s->selected_line--;
6937 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6938 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6939 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6940 07b0611c 2022-06-23 thomas else
6941 07b0611c 2022-06-23 thomas view->count = 0;
6942 1e37a5c2 2019-05-12 jcs break;
6943 70f17a53 2022-06-13 thomas case CTRL('u'):
6944 23427b14 2022-06-23 thomas case 'u':
6945 70f17a53 2022-06-13 thomas nscroll /= 2;
6946 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6947 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6948 ea025d1d 2020-02-22 naddy case CTRL('b'):
6949 1c5e5faa 2022-06-23 thomas case 'b':
6950 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6951 07b0611c 2022-06-23 thomas if (view->count > 1)
6952 07b0611c 2022-06-23 thomas nscroll += nscroll;
6953 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6954 07b0611c 2022-06-23 thomas view->count = 0;
6955 e5a0f69f 2018-08-18 stsp break;
6956 1e37a5c2 2019-05-12 jcs }
6957 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6958 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6959 1e37a5c2 2019-05-12 jcs else
6960 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6961 1e37a5c2 2019-05-12 jcs break;
6962 1e37a5c2 2019-05-12 jcs case 'j':
6963 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6964 f7140bf5 2021-10-17 thomas case CTRL('n'):
6965 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6966 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6967 1e37a5c2 2019-05-12 jcs s->selected_line++;
6968 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6969 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6970 07b0611c 2022-06-23 thomas else
6971 07b0611c 2022-06-23 thomas view->count = 0;
6972 1e37a5c2 2019-05-12 jcs break;
6973 1c5e5faa 2022-06-23 thomas case 'c':
6974 1e37a5c2 2019-05-12 jcs case 'p': {
6975 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6976 07b0611c 2022-06-23 thomas
6977 07b0611c 2022-06-23 thomas view->count = 0;
6978 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6979 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6980 1e37a5c2 2019-05-12 jcs if (id == NULL)
6981 e5a0f69f 2018-08-18 stsp break;
6982 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6983 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6984 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6985 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6986 1e37a5c2 2019-05-12 jcs int obj_type;
6987 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6988 1e37a5c2 2019-05-12 jcs s->repo, id);
6989 e5a0f69f 2018-08-18 stsp if (err)
6990 e5a0f69f 2018-08-18 stsp break;
6991 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6992 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6993 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6994 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6995 e5a0f69f 2018-08-18 stsp break;
6996 e5a0f69f 2018-08-18 stsp }
6997 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6998 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6999 ec242592 2022-04-22 thomas s->repo, &pid->id);
7000 945f9229 2022-04-16 thomas if (err)
7001 945f9229 2022-04-16 thomas break;
7002 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
7003 945f9229 2022-04-16 thomas pcommit, s->path);
7004 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
7005 e5a0f69f 2018-08-18 stsp if (err) {
7006 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
7007 1e37a5c2 2019-05-12 jcs err = NULL;
7008 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7009 e5a0f69f 2018-08-18 stsp break;
7010 e5a0f69f 2018-08-18 stsp }
7011 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
7012 1e37a5c2 2019-05-12 jcs blob_id);
7013 1e37a5c2 2019-05-12 jcs free(blob_id);
7014 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
7015 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
7016 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7017 e5a0f69f 2018-08-18 stsp break;
7018 1e37a5c2 2019-05-12 jcs }
7019 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7020 ec242592 2022-04-22 thomas &pid->id);
7021 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7022 1e37a5c2 2019-05-12 jcs } else {
7023 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
7024 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
7025 1e37a5c2 2019-05-12 jcs break;
7026 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
7027 1e37a5c2 2019-05-12 jcs id);
7028 1e37a5c2 2019-05-12 jcs }
7029 1e37a5c2 2019-05-12 jcs if (err)
7030 e5a0f69f 2018-08-18 stsp break;
7031 1e37a5c2 2019-05-12 jcs s->done = 1;
7032 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7033 1e37a5c2 2019-05-12 jcs s->done = 0;
7034 1e37a5c2 2019-05-12 jcs if (thread_err)
7035 1e37a5c2 2019-05-12 jcs break;
7036 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
7037 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
7038 a5388363 2020-12-01 naddy err = run_blame(view);
7039 1e37a5c2 2019-05-12 jcs if (err)
7040 1e37a5c2 2019-05-12 jcs break;
7041 1e37a5c2 2019-05-12 jcs break;
7042 1e37a5c2 2019-05-12 jcs }
7043 1c5e5faa 2022-06-23 thomas case 'C': {
7044 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
7045 07b0611c 2022-06-23 thomas
7046 07b0611c 2022-06-23 thomas view->count = 0;
7047 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
7048 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
7049 1e37a5c2 2019-05-12 jcs break;
7050 1e37a5c2 2019-05-12 jcs s->done = 1;
7051 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
7052 1e37a5c2 2019-05-12 jcs s->done = 0;
7053 1e37a5c2 2019-05-12 jcs if (thread_err)
7054 1e37a5c2 2019-05-12 jcs break;
7055 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
7056 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
7057 1e37a5c2 2019-05-12 jcs s->blamed_commit =
7058 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
7059 a5388363 2020-12-01 naddy err = run_blame(view);
7060 1e37a5c2 2019-05-12 jcs if (err)
7061 1e37a5c2 2019-05-12 jcs break;
7062 1e37a5c2 2019-05-12 jcs break;
7063 1e37a5c2 2019-05-12 jcs }
7064 2a31b33b 2022-07-23 thomas case 'L':
7065 eaeaa612 2022-07-20 thomas view->count = 0;
7066 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
7067 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
7068 2a31b33b 2022-07-23 thomas if (s->id_to_log)
7069 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7070 eaeaa612 2022-07-20 thomas break;
7071 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7072 1e37a5c2 2019-05-12 jcs case '\r': {
7073 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
7074 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
7075 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
7076 07b0611c 2022-06-23 thomas
7077 07b0611c 2022-06-23 thomas view->count = 0;
7078 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
7079 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
7080 1e37a5c2 2019-05-12 jcs if (id == NULL)
7081 1e37a5c2 2019-05-12 jcs break;
7082 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
7083 1e37a5c2 2019-05-12 jcs if (err)
7084 1e37a5c2 2019-05-12 jcs break;
7085 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7086 4fc71f3b 2022-07-12 thomas if (*new_view) {
7087 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
7088 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
7089 4fc71f3b 2022-07-12 thomas if (err)
7090 4fc71f3b 2022-07-12 thomas break;
7091 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
7092 4fc71f3b 2022-07-12 thomas } else {
7093 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
7094 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
7095 a5d43cac 2022-07-01 thomas
7096 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
7097 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
7098 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
7099 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
7100 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
7101 4fc71f3b 2022-07-12 thomas break;
7102 4fc71f3b 2022-07-12 thomas }
7103 15a94983 2018-12-23 stsp }
7104 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
7105 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
7106 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
7107 c68571e2 2023-07-17 thomas if (err)
7108 1e37a5c2 2019-05-12 jcs break;
7109 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
7110 4fc71f3b 2022-07-12 thomas s->selected_line;
7111 4fc71f3b 2022-07-12 thomas if (*new_view)
7112 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
7113 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7114 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7115 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7116 a5d43cac 2022-07-01 thomas if (err)
7117 a5d43cac 2022-07-01 thomas break;
7118 a5d43cac 2022-07-01 thomas }
7119 a5d43cac 2022-07-01 thomas
7120 e78dc838 2020-12-04 stsp view->focussed = 0;
7121 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
7122 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
7123 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
7124 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
7125 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
7126 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
7127 1e37a5c2 2019-05-12 jcs if (err)
7128 34bc9ec9 2019-02-22 stsp break;
7129 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
7130 40236d76 2022-06-23 thomas if (err)
7131 40236d76 2022-06-23 thomas break;
7132 e78dc838 2020-12-04 stsp view->focus_child = 1;
7133 1e37a5c2 2019-05-12 jcs } else
7134 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
7135 1e37a5c2 2019-05-12 jcs if (err)
7136 e5a0f69f 2018-08-18 stsp break;
7137 1e37a5c2 2019-05-12 jcs break;
7138 1e37a5c2 2019-05-12 jcs }
7139 70f17a53 2022-06-13 thomas case CTRL('d'):
7140 23427b14 2022-06-23 thomas case 'd':
7141 70f17a53 2022-06-13 thomas nscroll /= 2;
7142 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7143 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7144 ea025d1d 2020-02-22 naddy case CTRL('f'):
7145 1c5e5faa 2022-06-23 thomas case 'f':
7146 1e37a5c2 2019-05-12 jcs case ' ':
7147 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7148 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
7149 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
7150 07b0611c 2022-06-23 thomas view->count = 0;
7151 e5a0f69f 2018-08-18 stsp break;
7152 1e37a5c2 2019-05-12 jcs }
7153 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7154 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
7155 70f17a53 2022-06-13 thomas s->selected_line +=
7156 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
7157 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
7158 1e37a5c2 2019-05-12 jcs }
7159 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
7160 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
7161 1e37a5c2 2019-05-12 jcs else
7162 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
7163 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
7164 1e37a5c2 2019-05-12 jcs break;
7165 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7166 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
7167 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
7168 1e37a5c2 2019-05-12 jcs view->nlines - 2);
7169 1e37a5c2 2019-05-12 jcs }
7170 1e37a5c2 2019-05-12 jcs break;
7171 1e37a5c2 2019-05-12 jcs default:
7172 07b0611c 2022-06-23 thomas view->count = 0;
7173 1e37a5c2 2019-05-12 jcs break;
7174 a70480e0 2018-06-23 stsp }
7175 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7176 adf4c9e0 2022-07-03 thomas }
7177 adf4c9e0 2022-07-03 thomas
7178 adf4c9e0 2022-07-03 thomas static const struct got_error *
7179 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7180 adf4c9e0 2022-07-03 thomas {
7181 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7182 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7183 adf4c9e0 2022-07-03 thomas
7184 adf4c9e0 2022-07-03 thomas view->count = 0;
7185 adf4c9e0 2022-07-03 thomas s->done = 1;
7186 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7187 adf4c9e0 2022-07-03 thomas s->done = 0;
7188 adf4c9e0 2022-07-03 thomas if (err)
7189 adf4c9e0 2022-07-03 thomas return err;
7190 adf4c9e0 2022-07-03 thomas return run_blame(view);
7191 a70480e0 2018-06-23 stsp }
7192 a70480e0 2018-06-23 stsp
7193 a70480e0 2018-06-23 stsp static const struct got_error *
7194 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7195 9f7d7167 2018-04-29 stsp {
7196 1d98034b 2023-04-14 thomas const struct got_error *error;
7197 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7198 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7199 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7200 0587e10c 2020-07-23 stsp char *link_target = NULL;
7201 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7202 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7203 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *commit_id_str = NULL;
7204 a70480e0 2018-06-23 stsp int ch;
7205 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7206 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7207 a70480e0 2018-06-23 stsp
7208 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7209 a70480e0 2018-06-23 stsp switch (ch) {
7210 a70480e0 2018-06-23 stsp case 'c':
7211 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7212 a70480e0 2018-06-23 stsp break;
7213 69069811 2018-08-02 stsp case 'r':
7214 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7215 69069811 2018-08-02 stsp if (repo_path == NULL)
7216 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7217 9ba1d308 2019-10-21 stsp optarg);
7218 69069811 2018-08-02 stsp break;
7219 a70480e0 2018-06-23 stsp default:
7220 17020d27 2019-03-07 stsp usage_blame();
7221 a70480e0 2018-06-23 stsp /* NOTREACHED */
7222 a70480e0 2018-06-23 stsp }
7223 a70480e0 2018-06-23 stsp }
7224 a70480e0 2018-06-23 stsp
7225 a70480e0 2018-06-23 stsp argc -= optind;
7226 a70480e0 2018-06-23 stsp argv += optind;
7227 a70480e0 2018-06-23 stsp
7228 f135c941 2020-02-20 stsp if (argc != 1)
7229 a70480e0 2018-06-23 stsp usage_blame();
7230 6962eb72 2020-02-20 stsp
7231 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7232 7cd52833 2022-06-23 thomas if (error != NULL)
7233 7cd52833 2022-06-23 thomas goto done;
7234 7cd52833 2022-06-23 thomas
7235 69069811 2018-08-02 stsp if (repo_path == NULL) {
7236 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7237 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7238 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7239 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
7240 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7241 c156c7a4 2020-12-18 stsp goto done;
7242 f135c941 2020-02-20 stsp if (worktree)
7243 eb41ed75 2019-02-05 stsp repo_path =
7244 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7245 f135c941 2020-02-20 stsp else
7246 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7247 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7248 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7249 c156c7a4 2020-12-18 stsp goto done;
7250 c156c7a4 2020-12-18 stsp }
7251 f135c941 2020-02-20 stsp }
7252 a915003a 2019-02-05 stsp
7253 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7254 c02c541e 2019-03-29 stsp if (error != NULL)
7255 8e94dd5b 2019-01-04 stsp goto done;
7256 69069811 2018-08-02 stsp
7257 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7258 f135c941 2020-02-20 stsp worktree);
7259 c02c541e 2019-03-29 stsp if (error)
7260 92205607 2019-01-04 stsp goto done;
7261 69069811 2018-08-02 stsp
7262 557d3365 2023-04-14 thomas init_curses();
7263 f135c941 2020-02-20 stsp
7264 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7265 51a10b52 2020-12-26 stsp if (error)
7266 51a10b52 2020-12-26 stsp goto done;
7267 51a10b52 2020-12-26 stsp
7268 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7269 eb41ed75 2019-02-05 stsp if (error)
7270 69069811 2018-08-02 stsp goto done;
7271 a70480e0 2018-06-23 stsp
7272 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7273 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7274 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7275 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7276 a70480e0 2018-06-23 stsp if (error != NULL)
7277 66b4983c 2018-06-23 stsp goto done;
7278 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7279 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7280 a70480e0 2018-06-23 stsp } else {
7281 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
7282 66b04f8f 2023-07-19 thomas repo, worktree);
7283 66b04f8f 2023-07-19 thomas if (error != NULL)
7284 66b04f8f 2023-07-19 thomas goto done;
7285 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
7286 66b04f8f 2023-07-19 thomas commit_id_str = keyword_idstr;
7287 66b04f8f 2023-07-19 thomas
7288 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7289 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7290 a70480e0 2018-06-23 stsp }
7291 a19e88aa 2018-06-23 stsp if (error != NULL)
7292 e1cd8fed 2018-08-01 stsp goto done;
7293 0587e10c 2020-07-23 stsp
7294 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7295 945f9229 2022-04-16 thomas if (error)
7296 945f9229 2022-04-16 thomas goto done;
7297 945f9229 2022-04-16 thomas
7298 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7299 945f9229 2022-04-16 thomas commit, repo);
7300 7cbe629d 2018-08-04 stsp if (error)
7301 7cbe629d 2018-08-04 stsp goto done;
7302 0587e10c 2020-07-23 stsp
7303 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7304 4334634c 2023-04-22 thomas if (view == NULL) {
7305 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7306 4334634c 2023-04-22 thomas goto done;
7307 4334634c 2023-04-22 thomas }
7308 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7309 78756c87 2020-11-24 stsp commit_id, repo);
7310 4334634c 2023-04-22 thomas if (error != NULL) {
7311 4334634c 2023-04-22 thomas if (view->close == NULL)
7312 4334634c 2023-04-22 thomas close_blame_view(view);
7313 4334634c 2023-04-22 thomas view_close(view);
7314 0587e10c 2020-07-23 stsp goto done;
7315 4334634c 2023-04-22 thomas }
7316 349dfd1e 2023-07-23 thomas
7317 12314ad4 2019-08-31 stsp if (worktree) {
7318 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
7319 349dfd1e 2023-07-23 thomas if (error != NULL)
7320 349dfd1e 2023-07-23 thomas goto done;
7321 349dfd1e 2023-07-23 thomas
7322 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7323 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7324 12314ad4 2019-08-31 stsp worktree = NULL;
7325 12314ad4 2019-08-31 stsp }
7326 349dfd1e 2023-07-23 thomas
7327 557d3365 2023-04-14 thomas error = view_loop(view);
7328 349dfd1e 2023-07-23 thomas
7329 a70480e0 2018-06-23 stsp done:
7330 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
7331 69069811 2018-08-02 stsp free(repo_path);
7332 f135c941 2020-02-20 stsp free(in_repo_path);
7333 0587e10c 2020-07-23 stsp free(link_target);
7334 69069811 2018-08-02 stsp free(cwd);
7335 a70480e0 2018-06-23 stsp free(commit_id);
7336 66b04f8f 2023-07-19 thomas free(keyword_idstr);
7337 945f9229 2022-04-16 thomas if (commit)
7338 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7339 eb41ed75 2019-02-05 stsp if (worktree)
7340 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7341 1d0f4054 2021-06-17 stsp if (repo) {
7342 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7343 1d0f4054 2021-06-17 stsp if (error == NULL)
7344 1d0f4054 2021-06-17 stsp error = close_err;
7345 1d0f4054 2021-06-17 stsp }
7346 7cd52833 2022-06-23 thomas if (pack_fds) {
7347 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7348 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7349 7cd52833 2022-06-23 thomas if (error == NULL)
7350 7cd52833 2022-06-23 thomas error = pack_err;
7351 7cd52833 2022-06-23 thomas }
7352 51a10b52 2020-12-26 stsp tog_free_refs();
7353 a70480e0 2018-06-23 stsp return error;
7354 ffd1d5e5 2018-06-23 stsp }
7355 ffd1d5e5 2018-06-23 stsp
7356 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7357 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7358 ffd1d5e5 2018-06-23 stsp {
7359 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7360 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7361 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7362 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7363 86f4aab9 2022-09-09 thomas char *index = NULL;
7364 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7365 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7366 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7367 ffd1d5e5 2018-06-23 stsp
7368 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7369 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7370 a5d43cac 2022-07-01 thomas --limit; /* border */
7371 ffd1d5e5 2018-06-23 stsp
7372 f7d12f7e 2018-08-01 stsp werase(view->window);
7373 ffd1d5e5 2018-06-23 stsp
7374 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7375 ffd1d5e5 2018-06-23 stsp return NULL;
7376 ffd1d5e5 2018-06-23 stsp
7377 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7378 f91a2b48 2022-06-23 thomas 0, 0);
7379 ffd1d5e5 2018-06-23 stsp if (err)
7380 ffd1d5e5 2018-06-23 stsp return err;
7381 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7382 a3404814 2018-09-02 stsp wstandout(view->window);
7383 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7384 11b20872 2019-11-08 stsp if (tc)
7385 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7386 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7387 86f4aab9 2022-09-09 thomas free(wline);
7388 86f4aab9 2022-09-09 thomas wline = NULL;
7389 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7390 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7391 11b20872 2019-11-08 stsp if (tc)
7392 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7393 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7394 a3404814 2018-09-02 stsp wstandend(view->window);
7395 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7396 86f4aab9 2022-09-09 thomas return NULL;
7397 07dd3ed3 2022-08-06 thomas
7398 6f5f393a 2022-08-12 thomas i += s->selected;
7399 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7400 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7401 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7402 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7403 07dd3ed3 2022-08-06 thomas }
7404 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7405 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7406 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7407 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7408 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7409 86f4aab9 2022-09-09 thomas free(index);
7410 ce52c690 2018-06-23 stsp if (err)
7411 ce52c690 2018-06-23 stsp return err;
7412 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7413 2550e4c3 2018-07-13 stsp free(wline);
7414 2550e4c3 2018-07-13 stsp wline = NULL;
7415 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7416 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7417 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7418 ffd1d5e5 2018-06-23 stsp return NULL;
7419 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7420 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7421 a1eca9bb 2018-06-23 stsp return NULL;
7422 ffd1d5e5 2018-06-23 stsp
7423 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7424 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7425 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7426 0cf4efb1 2018-09-29 stsp if (view->focussed)
7427 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7428 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7429 ffd1d5e5 2018-06-23 stsp }
7430 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7431 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7432 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7433 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7434 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7435 ffd1d5e5 2018-06-23 stsp return NULL;
7436 ffd1d5e5 2018-06-23 stsp n = 1;
7437 ffd1d5e5 2018-06-23 stsp } else {
7438 ffd1d5e5 2018-06-23 stsp n = 0;
7439 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7440 ffd1d5e5 2018-06-23 stsp }
7441 ffd1d5e5 2018-06-23 stsp
7442 c72de8ab 2023-02-03 thomas view->maxx = 0;
7443 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7444 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7445 848d6979 2019-08-12 stsp const char *modestr = "";
7446 56e0773d 2019-11-28 stsp mode_t mode;
7447 1d13200f 2018-07-12 stsp
7448 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7449 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7450 56e0773d 2019-11-28 stsp
7451 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7452 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7453 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7454 1d13200f 2018-07-12 stsp if (err)
7455 638f9024 2019-05-13 stsp return got_error_from_errno(
7456 230a42bd 2019-05-11 jcs "got_object_id_str");
7457 1d13200f 2018-07-12 stsp }
7458 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7459 63c5ca5d 2019-08-24 stsp modestr = "$";
7460 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7461 0d6c6ee3 2020-05-20 stsp int i;
7462 0d6c6ee3 2020-05-20 stsp
7463 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7464 d86d3b18 2020-12-01 naddy te, s->repo);
7465 0d6c6ee3 2020-05-20 stsp if (err) {
7466 0d6c6ee3 2020-05-20 stsp free(id_str);
7467 0d6c6ee3 2020-05-20 stsp return err;
7468 0d6c6ee3 2020-05-20 stsp }
7469 cbb35fac 2023-06-25 thomas for (i = 0; link_target[i] != '\0'; i++) {
7470 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7471 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7472 0d6c6ee3 2020-05-20 stsp }
7473 848d6979 2019-08-12 stsp modestr = "@";
7474 0d6c6ee3 2020-05-20 stsp }
7475 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7476 848d6979 2019-08-12 stsp modestr = "/";
7477 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7478 848d6979 2019-08-12 stsp modestr = "*";
7479 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7480 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7481 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7482 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7483 1d13200f 2018-07-12 stsp free(id_str);
7484 0d6c6ee3 2020-05-20 stsp free(link_target);
7485 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7486 1d13200f 2018-07-12 stsp }
7487 1d13200f 2018-07-12 stsp free(id_str);
7488 0d6c6ee3 2020-05-20 stsp free(link_target);
7489 c72de8ab 2023-02-03 thomas
7490 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7491 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7492 ffd1d5e5 2018-06-23 stsp if (err) {
7493 ffd1d5e5 2018-06-23 stsp free(line);
7494 ffd1d5e5 2018-06-23 stsp break;
7495 ffd1d5e5 2018-06-23 stsp }
7496 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7497 c72de8ab 2023-02-03 thomas free(wline);
7498 c72de8ab 2023-02-03 thomas wline = NULL;
7499 c72de8ab 2023-02-03 thomas
7500 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7501 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7502 c72de8ab 2023-02-03 thomas if (err) {
7503 c72de8ab 2023-02-03 thomas free(line);
7504 c72de8ab 2023-02-03 thomas break;
7505 c72de8ab 2023-02-03 thomas }
7506 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7507 0cf4efb1 2018-09-29 stsp if (view->focussed)
7508 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7509 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7510 ffd1d5e5 2018-06-23 stsp }
7511 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7512 f26dddb7 2019-11-08 stsp if (tc)
7513 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7514 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7515 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7516 f26dddb7 2019-11-08 stsp if (tc)
7517 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7518 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7519 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7520 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7521 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7522 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7523 ffd1d5e5 2018-06-23 stsp free(line);
7524 2550e4c3 2018-07-13 stsp free(wline);
7525 2550e4c3 2018-07-13 stsp wline = NULL;
7526 ffd1d5e5 2018-06-23 stsp n++;
7527 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7528 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7529 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7530 ffd1d5e5 2018-06-23 stsp break;
7531 ffd1d5e5 2018-06-23 stsp }
7532 ffd1d5e5 2018-06-23 stsp
7533 ffd1d5e5 2018-06-23 stsp return err;
7534 ffd1d5e5 2018-06-23 stsp }
7535 ffd1d5e5 2018-06-23 stsp
7536 ffd1d5e5 2018-06-23 stsp static void
7537 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7538 ffd1d5e5 2018-06-23 stsp {
7539 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7540 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7541 fa86c4bf 2020-11-29 stsp int i = 0;
7542 ffd1d5e5 2018-06-23 stsp
7543 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7544 ffd1d5e5 2018-06-23 stsp return;
7545 ffd1d5e5 2018-06-23 stsp
7546 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7547 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7548 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7549 fa86c4bf 2020-11-29 stsp if (!isroot)
7550 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7551 fa86c4bf 2020-11-29 stsp break;
7552 fa86c4bf 2020-11-29 stsp }
7553 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7554 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7555 ffd1d5e5 2018-06-23 stsp }
7556 ffd1d5e5 2018-06-23 stsp }
7557 ffd1d5e5 2018-06-23 stsp
7558 a5d43cac 2022-07-01 thomas static const struct got_error *
7559 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7560 ffd1d5e5 2018-06-23 stsp {
7561 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7562 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7563 ffd1d5e5 2018-06-23 stsp int n = 0;
7564 ffd1d5e5 2018-06-23 stsp
7565 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7566 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7567 694d3271 2020-12-01 naddy s->first_displayed_entry);
7568 694d3271 2020-12-01 naddy else
7569 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7570 56e0773d 2019-11-28 stsp
7571 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7572 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7573 07dd3ed3 2022-08-06 thomas if (last) {
7574 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7575 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7576 07dd3ed3 2022-08-06 thomas }
7577 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7578 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7579 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7580 768394f3 2019-01-24 stsp }
7581 ffd1d5e5 2018-06-23 stsp }
7582 a5d43cac 2022-07-01 thomas
7583 a5d43cac 2022-07-01 thomas return NULL;
7584 ffd1d5e5 2018-06-23 stsp }
7585 ffd1d5e5 2018-06-23 stsp
7586 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7587 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7588 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7589 ffd1d5e5 2018-06-23 stsp {
7590 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7591 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7592 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7593 ffd1d5e5 2018-06-23 stsp
7594 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7595 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7596 56e0773d 2019-11-28 stsp + 1 /* slash */;
7597 ce52c690 2018-06-23 stsp if (te)
7598 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7599 ce52c690 2018-06-23 stsp
7600 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7601 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7602 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7603 ffd1d5e5 2018-06-23 stsp
7604 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7605 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7606 d9765a41 2018-06-23 stsp while (pt) {
7607 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7608 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7609 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7610 cb2ebc8a 2018-06-23 stsp goto done;
7611 cb2ebc8a 2018-06-23 stsp }
7612 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7613 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7614 cb2ebc8a 2018-06-23 stsp goto done;
7615 cb2ebc8a 2018-06-23 stsp }
7616 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7617 ffd1d5e5 2018-06-23 stsp }
7618 ce52c690 2018-06-23 stsp if (te) {
7619 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7620 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7621 ce52c690 2018-06-23 stsp goto done;
7622 ce52c690 2018-06-23 stsp }
7623 cb2ebc8a 2018-06-23 stsp }
7624 ce52c690 2018-06-23 stsp done:
7625 ce52c690 2018-06-23 stsp if (err) {
7626 ce52c690 2018-06-23 stsp free(*path);
7627 ce52c690 2018-06-23 stsp *path = NULL;
7628 ce52c690 2018-06-23 stsp }
7629 ce52c690 2018-06-23 stsp return err;
7630 ce52c690 2018-06-23 stsp }
7631 ce52c690 2018-06-23 stsp
7632 ce52c690 2018-06-23 stsp static const struct got_error *
7633 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7634 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7635 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7636 ce52c690 2018-06-23 stsp {
7637 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7638 ce52c690 2018-06-23 stsp char *path;
7639 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7640 a0de39f3 2019-08-09 stsp
7641 a0de39f3 2019-08-09 stsp *new_view = NULL;
7642 69efd4c4 2018-07-18 stsp
7643 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7644 ce52c690 2018-06-23 stsp if (err)
7645 ce52c690 2018-06-23 stsp return err;
7646 ffd1d5e5 2018-06-23 stsp
7647 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7648 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7649 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7650 83ce39e3 2019-08-12 stsp goto done;
7651 83ce39e3 2019-08-12 stsp }
7652 cdf1ee82 2018-08-01 stsp
7653 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7654 e5a0f69f 2018-08-18 stsp if (err) {
7655 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7656 fc06ba56 2019-08-22 stsp err = NULL;
7657 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7658 e5a0f69f 2018-08-18 stsp } else
7659 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7660 83ce39e3 2019-08-12 stsp done:
7661 83ce39e3 2019-08-12 stsp free(path);
7662 69efd4c4 2018-07-18 stsp return err;
7663 69efd4c4 2018-07-18 stsp }
7664 69efd4c4 2018-07-18 stsp
7665 69efd4c4 2018-07-18 stsp static const struct got_error *
7666 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7667 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7668 69efd4c4 2018-07-18 stsp {
7669 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7670 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7671 69efd4c4 2018-07-18 stsp char *path;
7672 a0de39f3 2019-08-09 stsp
7673 a0de39f3 2019-08-09 stsp *new_view = NULL;
7674 69efd4c4 2018-07-18 stsp
7675 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7676 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7677 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7678 e5a0f69f 2018-08-18 stsp
7679 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7680 69efd4c4 2018-07-18 stsp if (err)
7681 69efd4c4 2018-07-18 stsp return err;
7682 69efd4c4 2018-07-18 stsp
7683 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7684 92845f09 2023-07-26 thomas path, 0, NULL);
7685 ba4f502b 2018-08-04 stsp if (err)
7686 e5a0f69f 2018-08-18 stsp view_close(log_view);
7687 e5a0f69f 2018-08-18 stsp else
7688 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7689 cb2ebc8a 2018-06-23 stsp free(path);
7690 cb2ebc8a 2018-06-23 stsp return err;
7691 ffd1d5e5 2018-06-23 stsp }
7692 ffd1d5e5 2018-06-23 stsp
7693 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7694 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7695 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7696 ffd1d5e5 2018-06-23 stsp {
7697 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7698 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7699 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7700 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7701 ffd1d5e5 2018-06-23 stsp
7702 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7703 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7704 bc573f3b 2021-07-10 stsp
7705 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7706 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7707 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7708 51b7e1c3 2023-04-14 thomas goto done;
7709 51b7e1c3 2023-04-14 thomas }
7710 ffd1d5e5 2018-06-23 stsp
7711 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7712 bc573f3b 2021-07-10 stsp if (err)
7713 bc573f3b 2021-07-10 stsp goto done;
7714 bc573f3b 2021-07-10 stsp
7715 bc573f3b 2021-07-10 stsp /*
7716 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7717 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7718 bc573f3b 2021-07-10 stsp * closed on demand.
7719 bc573f3b 2021-07-10 stsp */
7720 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7721 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7722 bc573f3b 2021-07-10 stsp if (err)
7723 bc573f3b 2021-07-10 stsp goto done;
7724 bc573f3b 2021-07-10 stsp s->tree = s->root;
7725 bc573f3b 2021-07-10 stsp
7726 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7727 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7728 ffd1d5e5 2018-06-23 stsp goto done;
7729 ffd1d5e5 2018-06-23 stsp
7730 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7731 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7732 ffd1d5e5 2018-06-23 stsp goto done;
7733 ffd1d5e5 2018-06-23 stsp }
7734 ffd1d5e5 2018-06-23 stsp
7735 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7736 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7737 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7738 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7739 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7740 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7741 9cd7cbd1 2020-12-07 stsp goto done;
7742 9cd7cbd1 2020-12-07 stsp }
7743 9cd7cbd1 2020-12-07 stsp }
7744 fb2756b9 2018-08-04 stsp s->repo = repo;
7745 c0b01bdb 2019-11-08 stsp
7746 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7747 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7748 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7749 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7750 c0b01bdb 2019-11-08 stsp if (err)
7751 c0b01bdb 2019-11-08 stsp goto done;
7752 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7753 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7754 bc573f3b 2021-07-10 stsp if (err)
7755 c0b01bdb 2019-11-08 stsp goto done;
7756 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7757 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7758 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7759 bc573f3b 2021-07-10 stsp if (err)
7760 c0b01bdb 2019-11-08 stsp goto done;
7761 e5a0f69f 2018-08-18 stsp
7762 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7763 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7764 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7765 bc573f3b 2021-07-10 stsp if (err)
7766 11b20872 2019-11-08 stsp goto done;
7767 11b20872 2019-11-08 stsp
7768 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7769 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7770 bc573f3b 2021-07-10 stsp if (err)
7771 c0b01bdb 2019-11-08 stsp goto done;
7772 c0b01bdb 2019-11-08 stsp }
7773 c0b01bdb 2019-11-08 stsp
7774 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7775 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7776 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7777 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7778 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7779 ad80ab7b 2018-08-04 stsp done:
7780 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7781 bc573f3b 2021-07-10 stsp if (commit)
7782 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7783 51b7e1c3 2023-04-14 thomas if (err) {
7784 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7785 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7786 51b7e1c3 2023-04-14 thomas view_close(view);
7787 51b7e1c3 2023-04-14 thomas }
7788 ad80ab7b 2018-08-04 stsp return err;
7789 ad80ab7b 2018-08-04 stsp }
7790 ad80ab7b 2018-08-04 stsp
7791 e5a0f69f 2018-08-18 stsp static const struct got_error *
7792 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7793 ad80ab7b 2018-08-04 stsp {
7794 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7795 ad80ab7b 2018-08-04 stsp
7796 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7797 fb2756b9 2018-08-04 stsp free(s->tree_label);
7798 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7799 6484ec90 2018-09-29 stsp free(s->commit_id);
7800 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7801 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7802 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7803 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7804 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7805 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7806 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7807 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7808 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7809 ad80ab7b 2018-08-04 stsp free(parent);
7810 ad80ab7b 2018-08-04 stsp
7811 ad80ab7b 2018-08-04 stsp }
7812 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7813 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7814 bc573f3b 2021-07-10 stsp if (s->root)
7815 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7816 7c32bd05 2019-06-22 stsp return NULL;
7817 7c32bd05 2019-06-22 stsp }
7818 7c32bd05 2019-06-22 stsp
7819 7c32bd05 2019-06-22 stsp static const struct got_error *
7820 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7821 7c32bd05 2019-06-22 stsp {
7822 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7823 7c32bd05 2019-06-22 stsp
7824 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7825 7c32bd05 2019-06-22 stsp return NULL;
7826 7c32bd05 2019-06-22 stsp }
7827 7c32bd05 2019-06-22 stsp
7828 7c32bd05 2019-06-22 stsp static int
7829 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7830 7c32bd05 2019-06-22 stsp {
7831 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7832 7c32bd05 2019-06-22 stsp
7833 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7834 56e0773d 2019-11-28 stsp 0) == 0;
7835 7c32bd05 2019-06-22 stsp }
7836 7c32bd05 2019-06-22 stsp
7837 7c32bd05 2019-06-22 stsp static const struct got_error *
7838 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7839 7c32bd05 2019-06-22 stsp {
7840 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7841 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7842 7c32bd05 2019-06-22 stsp
7843 7c32bd05 2019-06-22 stsp if (!view->searching) {
7844 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7845 7c32bd05 2019-06-22 stsp return NULL;
7846 7c32bd05 2019-06-22 stsp }
7847 7c32bd05 2019-06-22 stsp
7848 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7849 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7850 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7851 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7852 56e0773d 2019-11-28 stsp s->selected_entry);
7853 7c32bd05 2019-06-22 stsp else
7854 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7855 56e0773d 2019-11-28 stsp } else {
7856 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7857 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7858 56e0773d 2019-11-28 stsp else
7859 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7860 56e0773d 2019-11-28 stsp s->selected_entry);
7861 7c32bd05 2019-06-22 stsp }
7862 7c32bd05 2019-06-22 stsp } else {
7863 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7864 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7865 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7866 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7867 56e0773d 2019-11-28 stsp else
7868 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7869 7c32bd05 2019-06-22 stsp }
7870 7c32bd05 2019-06-22 stsp
7871 7c32bd05 2019-06-22 stsp while (1) {
7872 56e0773d 2019-11-28 stsp if (te == NULL) {
7873 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7874 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7875 ac66afb8 2019-06-24 stsp return NULL;
7876 ac66afb8 2019-06-24 stsp }
7877 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7878 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7879 56e0773d 2019-11-28 stsp else
7880 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7881 7c32bd05 2019-06-22 stsp }
7882 7c32bd05 2019-06-22 stsp
7883 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7884 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7885 56e0773d 2019-11-28 stsp s->matched_entry = te;
7886 7c32bd05 2019-06-22 stsp break;
7887 7c32bd05 2019-06-22 stsp }
7888 7c32bd05 2019-06-22 stsp
7889 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7890 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7891 56e0773d 2019-11-28 stsp else
7892 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7893 7c32bd05 2019-06-22 stsp }
7894 e5a0f69f 2018-08-18 stsp
7895 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7896 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7897 7c32bd05 2019-06-22 stsp s->selected = 0;
7898 7c32bd05 2019-06-22 stsp }
7899 7c32bd05 2019-06-22 stsp
7900 e5a0f69f 2018-08-18 stsp return NULL;
7901 ad80ab7b 2018-08-04 stsp }
7902 ad80ab7b 2018-08-04 stsp
7903 ad80ab7b 2018-08-04 stsp static const struct got_error *
7904 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7905 ad80ab7b 2018-08-04 stsp {
7906 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7907 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7908 e5a0f69f 2018-08-18 stsp char *parent_path;
7909 ad80ab7b 2018-08-04 stsp
7910 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7911 e5a0f69f 2018-08-18 stsp if (err)
7912 e5a0f69f 2018-08-18 stsp return err;
7913 ffd1d5e5 2018-06-23 stsp
7914 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7915 e5a0f69f 2018-08-18 stsp free(parent_path);
7916 669b5ffa 2018-10-07 stsp
7917 a5d43cac 2022-07-01 thomas view_border(view);
7918 e5a0f69f 2018-08-18 stsp return err;
7919 07dd3ed3 2022-08-06 thomas }
7920 07dd3ed3 2022-08-06 thomas
7921 07dd3ed3 2022-08-06 thomas static const struct got_error *
7922 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7923 07dd3ed3 2022-08-06 thomas {
7924 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7925 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7926 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7927 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7928 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7929 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7930 07dd3ed3 2022-08-06 thomas
7931 07dd3ed3 2022-08-06 thomas g = view->gline;
7932 07dd3ed3 2022-08-06 thomas view->gline = 0;
7933 07dd3ed3 2022-08-06 thomas
7934 07dd3ed3 2022-08-06 thomas if (g == 0)
7935 07dd3ed3 2022-08-06 thomas g = 1;
7936 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7937 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7938 07dd3ed3 2022-08-06 thomas
7939 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7940 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7941 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7942 07dd3ed3 2022-08-06 thomas
7943 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7944 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7945 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7946 07dd3ed3 2022-08-06 thomas }
7947 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7948 07dd3ed3 2022-08-06 thomas last += off;
7949 07dd3ed3 2022-08-06 thomas
7950 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7951 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7952 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7953 07dd3ed3 2022-08-06 thomas }
7954 07dd3ed3 2022-08-06 thomas
7955 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7956 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7957 07dd3ed3 2022-08-06 thomas i += off;
7958 07dd3ed3 2022-08-06 thomas }
7959 07dd3ed3 2022-08-06 thomas
7960 07dd3ed3 2022-08-06 thomas if (i < g) {
7961 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7962 07dd3ed3 2022-08-06 thomas if (err)
7963 07dd3ed3 2022-08-06 thomas return err;
7964 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7965 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7966 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7967 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7968 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7969 07dd3ed3 2022-08-06 thomas first += off;
7970 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7971 07dd3ed3 2022-08-06 thomas }
7972 07dd3ed3 2022-08-06 thomas } else if (i > g)
7973 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7974 07dd3ed3 2022-08-06 thomas
7975 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7976 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7977 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7978 07dd3ed3 2022-08-06 thomas
7979 07dd3ed3 2022-08-06 thomas return NULL;
7980 e5a0f69f 2018-08-18 stsp }
7981 ce52c690 2018-06-23 stsp
7982 e5a0f69f 2018-08-18 stsp static const struct got_error *
7983 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7984 e5a0f69f 2018-08-18 stsp {
7985 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7986 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7987 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7988 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7989 ffd1d5e5 2018-06-23 stsp
7990 07dd3ed3 2022-08-06 thomas if (view->gline)
7991 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7992 07dd3ed3 2022-08-06 thomas
7993 e5a0f69f 2018-08-18 stsp switch (ch) {
7994 c72de8ab 2023-02-03 thomas case '0':
7995 c72de8ab 2023-02-03 thomas case '$':
7996 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7997 c72de8ab 2023-02-03 thomas case 'l':
7998 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7999 c72de8ab 2023-02-03 thomas case 'h':
8000 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8001 c72de8ab 2023-02-03 thomas break;
8002 1e37a5c2 2019-05-12 jcs case 'i':
8003 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
8004 07b0611c 2022-06-23 thomas view->count = 0;
8005 1e37a5c2 2019-05-12 jcs break;
8006 1be4947a 2022-07-22 thomas case 'L':
8007 07b0611c 2022-06-23 thomas view->count = 0;
8008 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
8009 ffd1d5e5 2018-06-23 stsp break;
8010 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8011 152c1c93 2020-11-29 stsp break;
8012 1be4947a 2022-07-22 thomas case 'R':
8013 07b0611c 2022-06-23 thomas view->count = 0;
8014 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
8015 e4526bf5 2021-09-03 naddy break;
8016 e4526bf5 2021-09-03 naddy case 'g':
8017 aa7a1117 2023-01-09 thomas case '=':
8018 e4526bf5 2021-09-03 naddy case KEY_HOME:
8019 e4526bf5 2021-09-03 naddy s->selected = 0;
8020 07b0611c 2022-06-23 thomas view->count = 0;
8021 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
8022 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
8023 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
8024 e4526bf5 2021-09-03 naddy else
8025 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8026 1e37a5c2 2019-05-12 jcs break;
8027 e4526bf5 2021-09-03 naddy case 'G':
8028 aa7a1117 2023-01-09 thomas case '*':
8029 a5d43cac 2022-07-01 thomas case KEY_END: {
8030 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
8031 a5d43cac 2022-07-01 thomas
8032 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8033 a5d43cac 2022-07-01 thomas --eos; /* border */
8034 e4526bf5 2021-09-03 naddy s->selected = 0;
8035 07b0611c 2022-06-23 thomas view->count = 0;
8036 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
8037 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8038 e4526bf5 2021-09-03 naddy if (te == NULL) {
8039 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
8040 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
8041 e4526bf5 2021-09-03 naddy n++;
8042 e4526bf5 2021-09-03 naddy }
8043 e4526bf5 2021-09-03 naddy break;
8044 e4526bf5 2021-09-03 naddy }
8045 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
8046 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
8047 e4526bf5 2021-09-03 naddy }
8048 e4526bf5 2021-09-03 naddy if (n > 0)
8049 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8050 e4526bf5 2021-09-03 naddy break;
8051 a5d43cac 2022-07-01 thomas }
8052 1e37a5c2 2019-05-12 jcs case 'k':
8053 1e37a5c2 2019-05-12 jcs case KEY_UP:
8054 f7140bf5 2021-10-17 thomas case CTRL('p'):
8055 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
8056 1e37a5c2 2019-05-12 jcs s->selected--;
8057 fa86c4bf 2020-11-29 stsp break;
8058 1e37a5c2 2019-05-12 jcs }
8059 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
8060 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8061 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8062 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8063 07b0611c 2022-06-23 thomas view->count = 0;
8064 1e37a5c2 2019-05-12 jcs break;
8065 70f17a53 2022-06-13 thomas case CTRL('u'):
8066 23427b14 2022-06-23 thomas case 'u':
8067 70f17a53 2022-06-13 thomas nscroll /= 2;
8068 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8069 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
8070 ea025d1d 2020-02-22 naddy case CTRL('b'):
8071 1c5e5faa 2022-06-23 thomas case 'b':
8072 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
8073 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
8074 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
8075 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8076 fa86c4bf 2020-11-29 stsp } else {
8077 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
8078 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
8079 fa86c4bf 2020-11-29 stsp }
8080 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
8081 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
8082 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
8083 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
8084 07b0611c 2022-06-23 thomas view->count = 0;
8085 1e37a5c2 2019-05-12 jcs break;
8086 1e37a5c2 2019-05-12 jcs case 'j':
8087 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
8088 f7140bf5 2021-10-17 thomas case CTRL('n'):
8089 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
8090 1e37a5c2 2019-05-12 jcs s->selected++;
8091 1e37a5c2 2019-05-12 jcs break;
8092 1e37a5c2 2019-05-12 jcs }
8093 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8094 07b0611c 2022-06-23 thomas == NULL) {
8095 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
8096 07b0611c 2022-06-23 thomas view->count = 0;
8097 1e37a5c2 2019-05-12 jcs break;
8098 07b0611c 2022-06-23 thomas }
8099 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
8100 1e37a5c2 2019-05-12 jcs break;
8101 70f17a53 2022-06-13 thomas case CTRL('d'):
8102 23427b14 2022-06-23 thomas case 'd':
8103 70f17a53 2022-06-13 thomas nscroll /= 2;
8104 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8105 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
8106 ea025d1d 2020-02-22 naddy case CTRL('f'):
8107 1c5e5faa 2022-06-23 thomas case 'f':
8108 4c2d69cb 2022-06-23 thomas case ' ':
8109 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
8110 1e37a5c2 2019-05-12 jcs == NULL) {
8111 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
8112 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
8113 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8114 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8115 07b0611c 2022-06-23 thomas else
8116 07b0611c 2022-06-23 thomas view->count = 0;
8117 1e37a5c2 2019-05-12 jcs break;
8118 1e37a5c2 2019-05-12 jcs }
8119 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
8120 1e37a5c2 2019-05-12 jcs break;
8121 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
8122 1e37a5c2 2019-05-12 jcs case '\r':
8123 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
8124 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
8125 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
8126 1e37a5c2 2019-05-12 jcs /* user selected '..' */
8127 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
8128 07b0611c 2022-06-23 thomas view->count = 0;
8129 1e37a5c2 2019-05-12 jcs break;
8130 07b0611c 2022-06-23 thomas }
8131 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
8132 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
8133 1e37a5c2 2019-05-12 jcs entry);
8134 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
8135 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
8136 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
8137 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
8138 1e37a5c2 2019-05-12 jcs s->selected_entry =
8139 1e37a5c2 2019-05-12 jcs parent->selected_entry;
8140 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
8141 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
8142 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
8143 a5d43cac 2022-07-01 thomas if (err)
8144 a5d43cac 2022-07-01 thomas break;
8145 a5d43cac 2022-07-01 thomas }
8146 1e37a5c2 2019-05-12 jcs free(parent);
8147 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
8148 56e0773d 2019-11-28 stsp s->selected_entry))) {
8149 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
8150 07b0611c 2022-06-23 thomas view->count = 0;
8151 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
8152 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
8153 1e37a5c2 2019-05-12 jcs if (err)
8154 1e37a5c2 2019-05-12 jcs break;
8155 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
8156 941e9f74 2019-05-21 stsp if (err) {
8157 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
8158 1e37a5c2 2019-05-12 jcs break;
8159 1e37a5c2 2019-05-12 jcs }
8160 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8161 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8162 1e37a5c2 2019-05-12 jcs break;
8163 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
8164 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8165 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
8166 07b0611c 2022-06-23 thomas view->count = 0;
8167 1e37a5c2 2019-05-12 jcs break;
8168 1e37a5c2 2019-05-12 jcs default:
8169 07b0611c 2022-06-23 thomas view->count = 0;
8170 1e37a5c2 2019-05-12 jcs break;
8171 ffd1d5e5 2018-06-23 stsp }
8172 e5a0f69f 2018-08-18 stsp
8173 ffd1d5e5 2018-06-23 stsp return err;
8174 9f7d7167 2018-04-29 stsp }
8175 9f7d7167 2018-04-29 stsp
8176 ffd1d5e5 2018-06-23 stsp __dead static void
8177 ffd1d5e5 2018-06-23 stsp usage_tree(void)
8178 ffd1d5e5 2018-06-23 stsp {
8179 ffd1d5e5 2018-06-23 stsp endwin();
8180 91198554 2022-06-23 thomas fprintf(stderr,
8181 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8182 ffd1d5e5 2018-06-23 stsp getprogname());
8183 ffd1d5e5 2018-06-23 stsp exit(1);
8184 ffd1d5e5 2018-06-23 stsp }
8185 ffd1d5e5 2018-06-23 stsp
8186 ffd1d5e5 2018-06-23 stsp static const struct got_error *
8187 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
8188 ffd1d5e5 2018-06-23 stsp {
8189 1d98034b 2023-04-14 thomas const struct got_error *error;
8190 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8191 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8192 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8193 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8194 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8195 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8196 66b04f8f 2023-07-19 thomas char *keyword_idstr = NULL, *label = NULL;
8197 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8198 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8199 ffd1d5e5 2018-06-23 stsp int ch;
8200 5221c383 2018-08-01 stsp struct tog_view *view;
8201 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8202 70ac5f84 2019-03-28 stsp
8203 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8204 ffd1d5e5 2018-06-23 stsp switch (ch) {
8205 ffd1d5e5 2018-06-23 stsp case 'c':
8206 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8207 74283ab8 2020-02-07 stsp break;
8208 74283ab8 2020-02-07 stsp case 'r':
8209 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8210 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8211 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8212 74283ab8 2020-02-07 stsp optarg);
8213 ffd1d5e5 2018-06-23 stsp break;
8214 ffd1d5e5 2018-06-23 stsp default:
8215 e99e2d15 2020-11-24 naddy usage_tree();
8216 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8217 ffd1d5e5 2018-06-23 stsp }
8218 ffd1d5e5 2018-06-23 stsp }
8219 ffd1d5e5 2018-06-23 stsp
8220 ffd1d5e5 2018-06-23 stsp argc -= optind;
8221 ffd1d5e5 2018-06-23 stsp argv += optind;
8222 ffd1d5e5 2018-06-23 stsp
8223 55cccc34 2020-02-20 stsp if (argc > 1)
8224 e99e2d15 2020-11-24 naddy usage_tree();
8225 7cd52833 2022-06-23 thomas
8226 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8227 7cd52833 2022-06-23 thomas if (error != NULL)
8228 7cd52833 2022-06-23 thomas goto done;
8229 74283ab8 2020-02-07 stsp
8230 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8231 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8232 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8233 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8234 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
8235 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8236 c156c7a4 2020-12-18 stsp goto done;
8237 55cccc34 2020-02-20 stsp if (worktree)
8238 52185f70 2019-02-05 stsp repo_path =
8239 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8240 55cccc34 2020-02-20 stsp else
8241 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8242 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8243 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8244 c156c7a4 2020-12-18 stsp goto done;
8245 c156c7a4 2020-12-18 stsp }
8246 55cccc34 2020-02-20 stsp }
8247 a915003a 2019-02-05 stsp
8248 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8249 c02c541e 2019-03-29 stsp if (error != NULL)
8250 52185f70 2019-02-05 stsp goto done;
8251 d188b9a6 2019-01-04 stsp
8252 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8253 55cccc34 2020-02-20 stsp repo, worktree);
8254 55cccc34 2020-02-20 stsp if (error)
8255 55cccc34 2020-02-20 stsp goto done;
8256 55cccc34 2020-02-20 stsp
8257 557d3365 2023-04-14 thomas init_curses();
8258 55cccc34 2020-02-20 stsp
8259 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8260 c02c541e 2019-03-29 stsp if (error)
8261 52185f70 2019-02-05 stsp goto done;
8262 ffd1d5e5 2018-06-23 stsp
8263 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8264 51a10b52 2020-12-26 stsp if (error)
8265 51a10b52 2020-12-26 stsp goto done;
8266 51a10b52 2020-12-26 stsp
8267 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8268 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8269 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8270 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8271 4e97c21c 2020-12-06 stsp if (error)
8272 4e97c21c 2020-12-06 stsp goto done;
8273 4e97c21c 2020-12-06 stsp head_ref_name = label;
8274 4e97c21c 2020-12-06 stsp } else {
8275 66b04f8f 2023-07-19 thomas error = got_keyword_to_idstr(&keyword_idstr, commit_id_arg,
8276 66b04f8f 2023-07-19 thomas repo, worktree);
8277 66b04f8f 2023-07-19 thomas if (error != NULL)
8278 66b04f8f 2023-07-19 thomas goto done;
8279 66b04f8f 2023-07-19 thomas if (keyword_idstr != NULL)
8280 66b04f8f 2023-07-19 thomas commit_id_arg = keyword_idstr;
8281 66b04f8f 2023-07-19 thomas
8282 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8283 4e97c21c 2020-12-06 stsp if (error == NULL)
8284 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8285 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8286 4e97c21c 2020-12-06 stsp goto done;
8287 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8288 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8289 4e97c21c 2020-12-06 stsp if (error)
8290 4e97c21c 2020-12-06 stsp goto done;
8291 4e97c21c 2020-12-06 stsp }
8292 ffd1d5e5 2018-06-23 stsp
8293 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8294 945f9229 2022-04-16 thomas if (error)
8295 945f9229 2022-04-16 thomas goto done;
8296 945f9229 2022-04-16 thomas
8297 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8298 5221c383 2018-08-01 stsp if (view == NULL) {
8299 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8300 5221c383 2018-08-01 stsp goto done;
8301 5221c383 2018-08-01 stsp }
8302 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8303 ad80ab7b 2018-08-04 stsp if (error)
8304 ad80ab7b 2018-08-04 stsp goto done;
8305 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8306 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8307 d91faf3b 2020-12-01 naddy in_repo_path);
8308 55cccc34 2020-02-20 stsp if (error)
8309 55cccc34 2020-02-20 stsp goto done;
8310 55cccc34 2020-02-20 stsp }
8311 55cccc34 2020-02-20 stsp
8312 55cccc34 2020-02-20 stsp if (worktree) {
8313 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
8314 349dfd1e 2023-07-23 thomas if (error != NULL)
8315 349dfd1e 2023-07-23 thomas goto done;
8316 349dfd1e 2023-07-23 thomas
8317 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8318 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8319 55cccc34 2020-02-20 stsp worktree = NULL;
8320 55cccc34 2020-02-20 stsp }
8321 349dfd1e 2023-07-23 thomas
8322 557d3365 2023-04-14 thomas error = view_loop(view);
8323 349dfd1e 2023-07-23 thomas
8324 ffd1d5e5 2018-06-23 stsp done:
8325 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
8326 66b04f8f 2023-07-19 thomas free(keyword_idstr);
8327 52185f70 2019-02-05 stsp free(repo_path);
8328 e4a0e26d 2020-02-20 stsp free(cwd);
8329 ffd1d5e5 2018-06-23 stsp free(commit_id);
8330 4e97c21c 2020-12-06 stsp free(label);
8331 486cd271 2020-12-06 stsp if (ref)
8332 486cd271 2020-12-06 stsp got_ref_close(ref);
8333 349dfd1e 2023-07-23 thomas if (worktree != NULL)
8334 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
8335 1d0f4054 2021-06-17 stsp if (repo) {
8336 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8337 1d0f4054 2021-06-17 stsp if (error == NULL)
8338 1d0f4054 2021-06-17 stsp error = close_err;
8339 1d0f4054 2021-06-17 stsp }
8340 7cd52833 2022-06-23 thomas if (pack_fds) {
8341 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8342 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8343 7cd52833 2022-06-23 thomas if (error == NULL)
8344 7cd52833 2022-06-23 thomas error = pack_err;
8345 b85a3496 2023-04-14 thomas }
8346 51a10b52 2020-12-26 stsp tog_free_refs();
8347 ffd1d5e5 2018-06-23 stsp return error;
8348 6458efa5 2020-11-24 stsp }
8349 6458efa5 2020-11-24 stsp
8350 6458efa5 2020-11-24 stsp static const struct got_error *
8351 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8352 6458efa5 2020-11-24 stsp {
8353 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8354 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8355 6458efa5 2020-11-24 stsp
8356 6458efa5 2020-11-24 stsp s->nrefs = 0;
8357 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8358 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8359 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8360 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8361 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8362 6458efa5 2020-11-24 stsp continue;
8363 6458efa5 2020-11-24 stsp
8364 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8365 6458efa5 2020-11-24 stsp if (re == NULL)
8366 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8367 6458efa5 2020-11-24 stsp
8368 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8369 8924d611 2020-12-26 stsp if (re->ref == NULL)
8370 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8371 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8372 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8373 6458efa5 2020-11-24 stsp }
8374 6458efa5 2020-11-24 stsp
8375 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8376 6458efa5 2020-11-24 stsp return NULL;
8377 6458efa5 2020-11-24 stsp }
8378 6458efa5 2020-11-24 stsp
8379 ef20f542 2022-06-26 thomas static void
8380 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8381 6458efa5 2020-11-24 stsp {
8382 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8383 6458efa5 2020-11-24 stsp
8384 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8385 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8386 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8387 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8388 6458efa5 2020-11-24 stsp free(re);
8389 6458efa5 2020-11-24 stsp }
8390 6458efa5 2020-11-24 stsp }
8391 6458efa5 2020-11-24 stsp
8392 6458efa5 2020-11-24 stsp static const struct got_error *
8393 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8394 6458efa5 2020-11-24 stsp {
8395 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8396 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8397 6458efa5 2020-11-24 stsp
8398 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8399 6458efa5 2020-11-24 stsp s->repo = repo;
8400 6458efa5 2020-11-24 stsp
8401 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8402 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8403 6458efa5 2020-11-24 stsp
8404 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8405 6458efa5 2020-11-24 stsp if (err)
8406 51b7e1c3 2023-04-14 thomas goto done;
8407 34ba6917 2020-11-30 stsp
8408 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8409 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8410 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8411 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8412 6458efa5 2020-11-24 stsp if (err)
8413 6458efa5 2020-11-24 stsp goto done;
8414 6458efa5 2020-11-24 stsp
8415 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8416 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8417 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8418 6458efa5 2020-11-24 stsp if (err)
8419 6458efa5 2020-11-24 stsp goto done;
8420 6458efa5 2020-11-24 stsp
8421 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8422 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8423 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8424 2183bbf6 2022-01-23 thomas if (err)
8425 2183bbf6 2022-01-23 thomas goto done;
8426 2183bbf6 2022-01-23 thomas
8427 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8428 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8429 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8430 6458efa5 2020-11-24 stsp if (err)
8431 6458efa5 2020-11-24 stsp goto done;
8432 6458efa5 2020-11-24 stsp }
8433 6458efa5 2020-11-24 stsp
8434 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8435 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8436 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8437 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8438 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8439 6458efa5 2020-11-24 stsp done:
8440 51b7e1c3 2023-04-14 thomas if (err) {
8441 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8442 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8443 51b7e1c3 2023-04-14 thomas view_close(view);
8444 51b7e1c3 2023-04-14 thomas }
8445 6458efa5 2020-11-24 stsp return err;
8446 6458efa5 2020-11-24 stsp }
8447 6458efa5 2020-11-24 stsp
8448 6458efa5 2020-11-24 stsp static const struct got_error *
8449 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8450 6458efa5 2020-11-24 stsp {
8451 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8452 6458efa5 2020-11-24 stsp
8453 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8454 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8455 6458efa5 2020-11-24 stsp
8456 6458efa5 2020-11-24 stsp return NULL;
8457 9f7d7167 2018-04-29 stsp }
8458 ce5b7c56 2019-07-09 stsp
8459 6458efa5 2020-11-24 stsp static const struct got_error *
8460 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8461 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8462 6458efa5 2020-11-24 stsp {
8463 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8464 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8465 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8466 6458efa5 2020-11-24 stsp int obj_type;
8467 6458efa5 2020-11-24 stsp
8468 c42c9805 2020-11-24 stsp *commit_id = NULL;
8469 6458efa5 2020-11-24 stsp
8470 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8471 6458efa5 2020-11-24 stsp if (err)
8472 6458efa5 2020-11-24 stsp return err;
8473 6458efa5 2020-11-24 stsp
8474 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8475 6458efa5 2020-11-24 stsp if (err)
8476 6458efa5 2020-11-24 stsp goto done;
8477 6458efa5 2020-11-24 stsp
8478 6458efa5 2020-11-24 stsp switch (obj_type) {
8479 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8480 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8481 6458efa5 2020-11-24 stsp break;
8482 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8483 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8484 6458efa5 2020-11-24 stsp if (err)
8485 6458efa5 2020-11-24 stsp goto done;
8486 c42c9805 2020-11-24 stsp free(obj_id);
8487 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8488 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8489 c42c9805 2020-11-24 stsp if (err)
8490 6458efa5 2020-11-24 stsp goto done;
8491 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8492 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8493 c42c9805 2020-11-24 stsp goto done;
8494 c42c9805 2020-11-24 stsp }
8495 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8496 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8497 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8498 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8499 c42c9805 2020-11-24 stsp goto done;
8500 c42c9805 2020-11-24 stsp }
8501 6458efa5 2020-11-24 stsp break;
8502 6458efa5 2020-11-24 stsp default:
8503 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8504 c42c9805 2020-11-24 stsp break;
8505 6458efa5 2020-11-24 stsp }
8506 6458efa5 2020-11-24 stsp
8507 c42c9805 2020-11-24 stsp done:
8508 c42c9805 2020-11-24 stsp if (tag)
8509 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8510 c42c9805 2020-11-24 stsp if (err) {
8511 c42c9805 2020-11-24 stsp free(*commit_id);
8512 c42c9805 2020-11-24 stsp *commit_id = NULL;
8513 c42c9805 2020-11-24 stsp }
8514 c42c9805 2020-11-24 stsp return err;
8515 c42c9805 2020-11-24 stsp }
8516 c42c9805 2020-11-24 stsp
8517 c42c9805 2020-11-24 stsp static const struct got_error *
8518 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8519 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8520 c42c9805 2020-11-24 stsp {
8521 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8522 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8523 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8524 c42c9805 2020-11-24 stsp
8525 c42c9805 2020-11-24 stsp *new_view = NULL;
8526 c42c9805 2020-11-24 stsp
8527 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8528 c42c9805 2020-11-24 stsp if (err) {
8529 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8530 c42c9805 2020-11-24 stsp return err;
8531 c42c9805 2020-11-24 stsp else
8532 c42c9805 2020-11-24 stsp return NULL;
8533 c42c9805 2020-11-24 stsp }
8534 c42c9805 2020-11-24 stsp
8535 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8536 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8537 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8538 6458efa5 2020-11-24 stsp goto done;
8539 6458efa5 2020-11-24 stsp }
8540 6458efa5 2020-11-24 stsp
8541 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8542 92845f09 2023-07-26 thomas got_ref_get_name(re->ref), "", 0, NULL);
8543 6458efa5 2020-11-24 stsp done:
8544 6458efa5 2020-11-24 stsp if (err)
8545 6458efa5 2020-11-24 stsp view_close(log_view);
8546 6458efa5 2020-11-24 stsp else
8547 6458efa5 2020-11-24 stsp *new_view = log_view;
8548 c42c9805 2020-11-24 stsp free(commit_id);
8549 6458efa5 2020-11-24 stsp return err;
8550 6458efa5 2020-11-24 stsp }
8551 6458efa5 2020-11-24 stsp
8552 ce5b7c56 2019-07-09 stsp static void
8553 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8554 6458efa5 2020-11-24 stsp {
8555 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8556 34ba6917 2020-11-30 stsp int i = 0;
8557 6458efa5 2020-11-24 stsp
8558 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8559 6458efa5 2020-11-24 stsp return;
8560 6458efa5 2020-11-24 stsp
8561 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8562 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8563 34ba6917 2020-11-30 stsp if (re == NULL)
8564 34ba6917 2020-11-30 stsp break;
8565 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8566 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8567 6458efa5 2020-11-24 stsp }
8568 6458efa5 2020-11-24 stsp }
8569 6458efa5 2020-11-24 stsp
8570 a5d43cac 2022-07-01 thomas static const struct got_error *
8571 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8572 6458efa5 2020-11-24 stsp {
8573 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8574 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8575 6458efa5 2020-11-24 stsp int n = 0;
8576 6458efa5 2020-11-24 stsp
8577 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8578 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8579 6458efa5 2020-11-24 stsp else
8580 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8581 6458efa5 2020-11-24 stsp
8582 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8583 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8584 07dd3ed3 2022-08-06 thomas if (last) {
8585 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8586 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8587 07dd3ed3 2022-08-06 thomas }
8588 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8589 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8590 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8591 6458efa5 2020-11-24 stsp }
8592 6458efa5 2020-11-24 stsp }
8593 a5d43cac 2022-07-01 thomas
8594 a5d43cac 2022-07-01 thomas return NULL;
8595 6458efa5 2020-11-24 stsp }
8596 6458efa5 2020-11-24 stsp
8597 6458efa5 2020-11-24 stsp static const struct got_error *
8598 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8599 6458efa5 2020-11-24 stsp {
8600 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8601 6458efa5 2020-11-24 stsp
8602 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8603 6458efa5 2020-11-24 stsp return NULL;
8604 6458efa5 2020-11-24 stsp }
8605 6458efa5 2020-11-24 stsp
8606 6458efa5 2020-11-24 stsp static int
8607 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8608 6458efa5 2020-11-24 stsp {
8609 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8610 6458efa5 2020-11-24 stsp
8611 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8612 6458efa5 2020-11-24 stsp 0) == 0;
8613 6458efa5 2020-11-24 stsp }
8614 6458efa5 2020-11-24 stsp
8615 6458efa5 2020-11-24 stsp static const struct got_error *
8616 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8617 6458efa5 2020-11-24 stsp {
8618 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8619 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8620 6458efa5 2020-11-24 stsp
8621 6458efa5 2020-11-24 stsp if (!view->searching) {
8622 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8623 6458efa5 2020-11-24 stsp return NULL;
8624 6458efa5 2020-11-24 stsp }
8625 6458efa5 2020-11-24 stsp
8626 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8627 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8628 6458efa5 2020-11-24 stsp if (s->selected_entry)
8629 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8630 6458efa5 2020-11-24 stsp else
8631 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8632 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8633 6458efa5 2020-11-24 stsp } else {
8634 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8635 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8636 6458efa5 2020-11-24 stsp else
8637 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8638 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8639 6458efa5 2020-11-24 stsp }
8640 6458efa5 2020-11-24 stsp } else {
8641 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8642 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8643 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8644 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8645 6458efa5 2020-11-24 stsp else
8646 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8647 6458efa5 2020-11-24 stsp }
8648 6458efa5 2020-11-24 stsp
8649 6458efa5 2020-11-24 stsp while (1) {
8650 6458efa5 2020-11-24 stsp if (re == NULL) {
8651 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8652 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8653 6458efa5 2020-11-24 stsp return NULL;
8654 6458efa5 2020-11-24 stsp }
8655 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8656 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8657 6458efa5 2020-11-24 stsp else
8658 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8659 6458efa5 2020-11-24 stsp }
8660 6458efa5 2020-11-24 stsp
8661 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8662 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8663 6458efa5 2020-11-24 stsp s->matched_entry = re;
8664 6458efa5 2020-11-24 stsp break;
8665 6458efa5 2020-11-24 stsp }
8666 6458efa5 2020-11-24 stsp
8667 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8668 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8669 6458efa5 2020-11-24 stsp else
8670 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8671 6458efa5 2020-11-24 stsp }
8672 6458efa5 2020-11-24 stsp
8673 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8674 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8675 6458efa5 2020-11-24 stsp s->selected = 0;
8676 6458efa5 2020-11-24 stsp }
8677 6458efa5 2020-11-24 stsp
8678 6458efa5 2020-11-24 stsp return NULL;
8679 6458efa5 2020-11-24 stsp }
8680 6458efa5 2020-11-24 stsp
8681 6458efa5 2020-11-24 stsp static const struct got_error *
8682 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8683 6458efa5 2020-11-24 stsp {
8684 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8685 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8686 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8687 6458efa5 2020-11-24 stsp char *line = NULL;
8688 6458efa5 2020-11-24 stsp wchar_t *wline;
8689 6458efa5 2020-11-24 stsp struct tog_color *tc;
8690 66a70b97 2023-02-03 thomas int width, n, scrollx;
8691 6458efa5 2020-11-24 stsp int limit = view->nlines;
8692 6458efa5 2020-11-24 stsp
8693 6458efa5 2020-11-24 stsp werase(view->window);
8694 6458efa5 2020-11-24 stsp
8695 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8696 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8697 a5d43cac 2022-07-01 thomas --limit; /* border */
8698 6458efa5 2020-11-24 stsp
8699 6458efa5 2020-11-24 stsp if (limit == 0)
8700 6458efa5 2020-11-24 stsp return NULL;
8701 6458efa5 2020-11-24 stsp
8702 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8703 6458efa5 2020-11-24 stsp
8704 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8705 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8706 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8707 6458efa5 2020-11-24 stsp
8708 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8709 6458efa5 2020-11-24 stsp if (err) {
8710 6458efa5 2020-11-24 stsp free(line);
8711 6458efa5 2020-11-24 stsp return err;
8712 6458efa5 2020-11-24 stsp }
8713 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8714 6458efa5 2020-11-24 stsp wstandout(view->window);
8715 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8716 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8717 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8718 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8719 6458efa5 2020-11-24 stsp wstandend(view->window);
8720 6458efa5 2020-11-24 stsp free(wline);
8721 6458efa5 2020-11-24 stsp wline = NULL;
8722 6458efa5 2020-11-24 stsp free(line);
8723 6458efa5 2020-11-24 stsp line = NULL;
8724 6458efa5 2020-11-24 stsp if (--limit <= 0)
8725 6458efa5 2020-11-24 stsp return NULL;
8726 6458efa5 2020-11-24 stsp
8727 6458efa5 2020-11-24 stsp n = 0;
8728 66a70b97 2023-02-03 thomas view->maxx = 0;
8729 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8730 6458efa5 2020-11-24 stsp char *line = NULL;
8731 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8732 6458efa5 2020-11-24 stsp
8733 84227eb1 2022-06-23 thomas if (s->show_date) {
8734 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8735 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8736 84227eb1 2022-06-23 thomas struct got_object_id *id;
8737 84227eb1 2022-06-23 thomas struct tm tm;
8738 84227eb1 2022-06-23 thomas time_t t;
8739 84227eb1 2022-06-23 thomas
8740 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8741 84227eb1 2022-06-23 thomas if (err)
8742 84227eb1 2022-06-23 thomas return err;
8743 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8744 84227eb1 2022-06-23 thomas if (err) {
8745 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8746 84227eb1 2022-06-23 thomas free(id);
8747 84227eb1 2022-06-23 thomas return err;
8748 84227eb1 2022-06-23 thomas }
8749 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8750 84227eb1 2022-06-23 thomas id);
8751 84227eb1 2022-06-23 thomas if (err) {
8752 84227eb1 2022-06-23 thomas free(id);
8753 84227eb1 2022-06-23 thomas return err;
8754 84227eb1 2022-06-23 thomas }
8755 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8756 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8757 84227eb1 2022-06-23 thomas } else {
8758 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8759 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8760 84227eb1 2022-06-23 thomas }
8761 84227eb1 2022-06-23 thomas free(id);
8762 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8763 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8764 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8765 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8766 84227eb1 2022-06-23 thomas }
8767 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8768 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8769 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8770 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8771 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8772 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8773 6458efa5 2020-11-24 stsp struct got_object_id *id;
8774 6458efa5 2020-11-24 stsp char *id_str;
8775 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8776 6458efa5 2020-11-24 stsp if (err)
8777 6458efa5 2020-11-24 stsp return err;
8778 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8779 6458efa5 2020-11-24 stsp if (err) {
8780 6458efa5 2020-11-24 stsp free(id);
8781 6458efa5 2020-11-24 stsp return err;
8782 6458efa5 2020-11-24 stsp }
8783 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8784 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8785 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8786 6458efa5 2020-11-24 stsp free(id);
8787 6458efa5 2020-11-24 stsp free(id_str);
8788 6458efa5 2020-11-24 stsp return err;
8789 6458efa5 2020-11-24 stsp }
8790 6458efa5 2020-11-24 stsp free(id);
8791 6458efa5 2020-11-24 stsp free(id_str);
8792 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8793 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8794 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8795 6458efa5 2020-11-24 stsp
8796 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8797 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8798 6458efa5 2020-11-24 stsp if (err) {
8799 6458efa5 2020-11-24 stsp free(line);
8800 6458efa5 2020-11-24 stsp return err;
8801 6458efa5 2020-11-24 stsp }
8802 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8803 66a70b97 2023-02-03 thomas free(wline);
8804 66a70b97 2023-02-03 thomas wline = NULL;
8805 66a70b97 2023-02-03 thomas
8806 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8807 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8808 66a70b97 2023-02-03 thomas if (err) {
8809 66a70b97 2023-02-03 thomas free(line);
8810 66a70b97 2023-02-03 thomas return err;
8811 66a70b97 2023-02-03 thomas }
8812 6458efa5 2020-11-24 stsp if (n == s->selected) {
8813 6458efa5 2020-11-24 stsp if (view->focussed)
8814 6458efa5 2020-11-24 stsp wstandout(view->window);
8815 6458efa5 2020-11-24 stsp s->selected_entry = re;
8816 6458efa5 2020-11-24 stsp }
8817 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8818 6458efa5 2020-11-24 stsp if (tc)
8819 6458efa5 2020-11-24 stsp wattr_on(view->window,
8820 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8821 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8822 6458efa5 2020-11-24 stsp if (tc)
8823 6458efa5 2020-11-24 stsp wattr_off(view->window,
8824 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8825 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8826 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8827 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8828 6458efa5 2020-11-24 stsp wstandend(view->window);
8829 6458efa5 2020-11-24 stsp free(line);
8830 6458efa5 2020-11-24 stsp free(wline);
8831 6458efa5 2020-11-24 stsp wline = NULL;
8832 6458efa5 2020-11-24 stsp n++;
8833 6458efa5 2020-11-24 stsp s->ndisplayed++;
8834 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8835 6458efa5 2020-11-24 stsp
8836 6458efa5 2020-11-24 stsp limit--;
8837 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8838 6458efa5 2020-11-24 stsp }
8839 6458efa5 2020-11-24 stsp
8840 a5d43cac 2022-07-01 thomas view_border(view);
8841 6458efa5 2020-11-24 stsp return err;
8842 6458efa5 2020-11-24 stsp }
8843 6458efa5 2020-11-24 stsp
8844 6458efa5 2020-11-24 stsp static const struct got_error *
8845 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8846 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8847 c42c9805 2020-11-24 stsp {
8848 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8849 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8850 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8851 c42c9805 2020-11-24 stsp
8852 c42c9805 2020-11-24 stsp *new_view = NULL;
8853 c42c9805 2020-11-24 stsp
8854 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8855 c42c9805 2020-11-24 stsp if (err) {
8856 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8857 c42c9805 2020-11-24 stsp return err;
8858 c42c9805 2020-11-24 stsp else
8859 c42c9805 2020-11-24 stsp return NULL;
8860 c42c9805 2020-11-24 stsp }
8861 c42c9805 2020-11-24 stsp
8862 c42c9805 2020-11-24 stsp
8863 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8864 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8865 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8866 c42c9805 2020-11-24 stsp goto done;
8867 c42c9805 2020-11-24 stsp }
8868 c42c9805 2020-11-24 stsp
8869 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8870 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8871 c42c9805 2020-11-24 stsp if (err)
8872 c42c9805 2020-11-24 stsp goto done;
8873 c42c9805 2020-11-24 stsp
8874 c42c9805 2020-11-24 stsp *new_view = tree_view;
8875 c42c9805 2020-11-24 stsp done:
8876 c42c9805 2020-11-24 stsp free(commit_id);
8877 c42c9805 2020-11-24 stsp return err;
8878 07dd3ed3 2022-08-06 thomas }
8879 07dd3ed3 2022-08-06 thomas
8880 07dd3ed3 2022-08-06 thomas static const struct got_error *
8881 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8882 07dd3ed3 2022-08-06 thomas {
8883 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8884 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8885 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8886 07dd3ed3 2022-08-06 thomas
8887 07dd3ed3 2022-08-06 thomas g = view->gline;
8888 07dd3ed3 2022-08-06 thomas view->gline = 0;
8889 07dd3ed3 2022-08-06 thomas
8890 07dd3ed3 2022-08-06 thomas if (g == 0)
8891 07dd3ed3 2022-08-06 thomas g = 1;
8892 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8893 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8894 07dd3ed3 2022-08-06 thomas
8895 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8896 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8897 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8898 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8899 07dd3ed3 2022-08-06 thomas return NULL;
8900 07dd3ed3 2022-08-06 thomas }
8901 07dd3ed3 2022-08-06 thomas
8902 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8903 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8904 07dd3ed3 2022-08-06 thomas if (err)
8905 07dd3ed3 2022-08-06 thomas return err;
8906 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8907 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8908 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8909 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8910 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8911 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8912 07dd3ed3 2022-08-06 thomas
8913 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8914 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8915 07dd3ed3 2022-08-06 thomas
8916 07dd3ed3 2022-08-06 thomas return NULL;
8917 07dd3ed3 2022-08-06 thomas
8918 c42c9805 2020-11-24 stsp }
8919 07dd3ed3 2022-08-06 thomas
8920 c42c9805 2020-11-24 stsp static const struct got_error *
8921 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8922 6458efa5 2020-11-24 stsp {
8923 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8924 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8925 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8926 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8927 6458efa5 2020-11-24 stsp
8928 07dd3ed3 2022-08-06 thomas if (view->gline)
8929 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8930 07dd3ed3 2022-08-06 thomas
8931 6458efa5 2020-11-24 stsp switch (ch) {
8932 66a70b97 2023-02-03 thomas case '0':
8933 66a70b97 2023-02-03 thomas case '$':
8934 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8935 66a70b97 2023-02-03 thomas case 'l':
8936 66a70b97 2023-02-03 thomas case KEY_LEFT:
8937 66a70b97 2023-02-03 thomas case 'h':
8938 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8939 66a70b97 2023-02-03 thomas break;
8940 6458efa5 2020-11-24 stsp case 'i':
8941 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8942 07b0611c 2022-06-23 thomas view->count = 0;
8943 3bfadbd4 2021-11-20 thomas break;
8944 84227eb1 2022-06-23 thomas case 'm':
8945 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8946 07b0611c 2022-06-23 thomas view->count = 0;
8947 84227eb1 2022-06-23 thomas break;
8948 98182bd0 2021-11-20 thomas case 'o':
8949 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8950 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8951 07b0611c 2022-06-23 thomas view->count = 0;
8952 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8953 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8954 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8955 c591440f 2021-11-20 thomas if (err)
8956 c591440f 2021-11-20 thomas break;
8957 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8958 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8959 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8960 3bfadbd4 2021-11-20 thomas if (err)
8961 3bfadbd4 2021-11-20 thomas break;
8962 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8963 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8964 6458efa5 2020-11-24 stsp break;
8965 6458efa5 2020-11-24 stsp case KEY_ENTER:
8966 6458efa5 2020-11-24 stsp case '\r':
8967 07b0611c 2022-06-23 thomas view->count = 0;
8968 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8969 a5d43cac 2022-07-01 thomas break;
8970 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8971 6458efa5 2020-11-24 stsp break;
8972 1be4947a 2022-07-22 thomas case 'T':
8973 07b0611c 2022-06-23 thomas view->count = 0;
8974 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8975 c42c9805 2020-11-24 stsp break;
8976 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8977 c42c9805 2020-11-24 stsp break;
8978 e4526bf5 2021-09-03 naddy case 'g':
8979 aa7a1117 2023-01-09 thomas case '=':
8980 e4526bf5 2021-09-03 naddy case KEY_HOME:
8981 e4526bf5 2021-09-03 naddy s->selected = 0;
8982 07b0611c 2022-06-23 thomas view->count = 0;
8983 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8984 e4526bf5 2021-09-03 naddy break;
8985 e4526bf5 2021-09-03 naddy case 'G':
8986 aa7a1117 2023-01-09 thomas case '*':
8987 a5d43cac 2022-07-01 thomas case KEY_END: {
8988 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8989 a5d43cac 2022-07-01 thomas
8990 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8991 a5d43cac 2022-07-01 thomas --eos; /* border */
8992 e4526bf5 2021-09-03 naddy s->selected = 0;
8993 07b0611c 2022-06-23 thomas view->count = 0;
8994 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8995 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8996 e4526bf5 2021-09-03 naddy if (re == NULL)
8997 e4526bf5 2021-09-03 naddy break;
8998 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8999 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
9000 e4526bf5 2021-09-03 naddy }
9001 e4526bf5 2021-09-03 naddy if (n > 0)
9002 e4526bf5 2021-09-03 naddy s->selected = n - 1;
9003 e4526bf5 2021-09-03 naddy break;
9004 a5d43cac 2022-07-01 thomas }
9005 6458efa5 2020-11-24 stsp case 'k':
9006 6458efa5 2020-11-24 stsp case KEY_UP:
9007 f7140bf5 2021-10-17 thomas case CTRL('p'):
9008 6458efa5 2020-11-24 stsp if (s->selected > 0) {
9009 6458efa5 2020-11-24 stsp s->selected--;
9010 6458efa5 2020-11-24 stsp break;
9011 34ba6917 2020-11-30 stsp }
9012 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
9013 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9014 07b0611c 2022-06-23 thomas view->count = 0;
9015 6458efa5 2020-11-24 stsp break;
9016 70f17a53 2022-06-13 thomas case CTRL('u'):
9017 23427b14 2022-06-23 thomas case 'u':
9018 70f17a53 2022-06-13 thomas nscroll /= 2;
9019 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9020 6458efa5 2020-11-24 stsp case KEY_PPAGE:
9021 6458efa5 2020-11-24 stsp case CTRL('b'):
9022 1c5e5faa 2022-06-23 thomas case 'b':
9023 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
9024 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
9025 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
9026 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
9027 07b0611c 2022-06-23 thomas view->count = 0;
9028 6458efa5 2020-11-24 stsp break;
9029 6458efa5 2020-11-24 stsp case 'j':
9030 6458efa5 2020-11-24 stsp case KEY_DOWN:
9031 f7140bf5 2021-10-17 thomas case CTRL('n'):
9032 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
9033 6458efa5 2020-11-24 stsp s->selected++;
9034 6458efa5 2020-11-24 stsp break;
9035 6458efa5 2020-11-24 stsp }
9036 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9037 6458efa5 2020-11-24 stsp /* can't scroll any further */
9038 07b0611c 2022-06-23 thomas view->count = 0;
9039 6458efa5 2020-11-24 stsp break;
9040 07b0611c 2022-06-23 thomas }
9041 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
9042 6458efa5 2020-11-24 stsp break;
9043 70f17a53 2022-06-13 thomas case CTRL('d'):
9044 23427b14 2022-06-23 thomas case 'd':
9045 70f17a53 2022-06-13 thomas nscroll /= 2;
9046 70f17a53 2022-06-13 thomas /* FALL THROUGH */
9047 6458efa5 2020-11-24 stsp case KEY_NPAGE:
9048 6458efa5 2020-11-24 stsp case CTRL('f'):
9049 1c5e5faa 2022-06-23 thomas case 'f':
9050 4c2d69cb 2022-06-23 thomas case ' ':
9051 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
9052 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
9053 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
9054 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
9055 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
9056 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
9057 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
9058 07b0611c 2022-06-23 thomas view->count = 0;
9059 6458efa5 2020-11-24 stsp break;
9060 6458efa5 2020-11-24 stsp }
9061 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
9062 6458efa5 2020-11-24 stsp break;
9063 6458efa5 2020-11-24 stsp case CTRL('l'):
9064 07b0611c 2022-06-23 thomas view->count = 0;
9065 8924d611 2020-12-26 stsp tog_free_refs();
9066 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
9067 8924d611 2020-12-26 stsp if (err)
9068 8924d611 2020-12-26 stsp break;
9069 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
9070 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
9071 6458efa5 2020-11-24 stsp break;
9072 6458efa5 2020-11-24 stsp case KEY_RESIZE:
9073 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
9074 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
9075 6458efa5 2020-11-24 stsp break;
9076 6458efa5 2020-11-24 stsp default:
9077 07b0611c 2022-06-23 thomas view->count = 0;
9078 6458efa5 2020-11-24 stsp break;
9079 6458efa5 2020-11-24 stsp }
9080 6458efa5 2020-11-24 stsp
9081 6458efa5 2020-11-24 stsp return err;
9082 6458efa5 2020-11-24 stsp }
9083 6458efa5 2020-11-24 stsp
9084 6458efa5 2020-11-24 stsp __dead static void
9085 6458efa5 2020-11-24 stsp usage_ref(void)
9086 6458efa5 2020-11-24 stsp {
9087 6458efa5 2020-11-24 stsp endwin();
9088 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
9089 6458efa5 2020-11-24 stsp getprogname());
9090 6458efa5 2020-11-24 stsp exit(1);
9091 6458efa5 2020-11-24 stsp }
9092 6458efa5 2020-11-24 stsp
9093 6458efa5 2020-11-24 stsp static const struct got_error *
9094 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
9095 6458efa5 2020-11-24 stsp {
9096 1d98034b 2023-04-14 thomas const struct got_error *error;
9097 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
9098 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
9099 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
9100 6458efa5 2020-11-24 stsp int ch;
9101 6458efa5 2020-11-24 stsp struct tog_view *view;
9102 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9103 6458efa5 2020-11-24 stsp
9104 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
9105 6458efa5 2020-11-24 stsp switch (ch) {
9106 6458efa5 2020-11-24 stsp case 'r':
9107 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
9108 6458efa5 2020-11-24 stsp if (repo_path == NULL)
9109 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
9110 6458efa5 2020-11-24 stsp optarg);
9111 6458efa5 2020-11-24 stsp break;
9112 6458efa5 2020-11-24 stsp default:
9113 e99e2d15 2020-11-24 naddy usage_ref();
9114 6458efa5 2020-11-24 stsp /* NOTREACHED */
9115 6458efa5 2020-11-24 stsp }
9116 6458efa5 2020-11-24 stsp }
9117 6458efa5 2020-11-24 stsp
9118 6458efa5 2020-11-24 stsp argc -= optind;
9119 6458efa5 2020-11-24 stsp argv += optind;
9120 6458efa5 2020-11-24 stsp
9121 6458efa5 2020-11-24 stsp if (argc > 1)
9122 e99e2d15 2020-11-24 naddy usage_ref();
9123 7cd52833 2022-06-23 thomas
9124 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9125 7cd52833 2022-06-23 thomas if (error != NULL)
9126 7cd52833 2022-06-23 thomas goto done;
9127 6458efa5 2020-11-24 stsp
9128 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
9129 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
9130 c156c7a4 2020-12-18 stsp if (cwd == NULL)
9131 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
9132 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
9133 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9134 c156c7a4 2020-12-18 stsp goto done;
9135 6458efa5 2020-11-24 stsp if (worktree)
9136 6458efa5 2020-11-24 stsp repo_path =
9137 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
9138 6458efa5 2020-11-24 stsp else
9139 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
9140 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
9141 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
9142 c156c7a4 2020-12-18 stsp goto done;
9143 c156c7a4 2020-12-18 stsp }
9144 6458efa5 2020-11-24 stsp }
9145 6458efa5 2020-11-24 stsp
9146 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9147 6458efa5 2020-11-24 stsp if (error != NULL)
9148 6458efa5 2020-11-24 stsp goto done;
9149 6458efa5 2020-11-24 stsp
9150 557d3365 2023-04-14 thomas init_curses();
9151 6458efa5 2020-11-24 stsp
9152 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
9153 51a10b52 2020-12-26 stsp if (error)
9154 51a10b52 2020-12-26 stsp goto done;
9155 51a10b52 2020-12-26 stsp
9156 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9157 6458efa5 2020-11-24 stsp if (error)
9158 6458efa5 2020-11-24 stsp goto done;
9159 6458efa5 2020-11-24 stsp
9160 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
9161 6458efa5 2020-11-24 stsp if (view == NULL) {
9162 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
9163 6458efa5 2020-11-24 stsp goto done;
9164 6458efa5 2020-11-24 stsp }
9165 6458efa5 2020-11-24 stsp
9166 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
9167 6458efa5 2020-11-24 stsp if (error)
9168 6458efa5 2020-11-24 stsp goto done;
9169 6458efa5 2020-11-24 stsp
9170 6458efa5 2020-11-24 stsp if (worktree) {
9171 349dfd1e 2023-07-23 thomas error = set_tog_base_commit(repo, worktree);
9172 349dfd1e 2023-07-23 thomas if (error != NULL)
9173 349dfd1e 2023-07-23 thomas goto done;
9174 349dfd1e 2023-07-23 thomas
9175 6458efa5 2020-11-24 stsp /* Release work tree lock. */
9176 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
9177 6458efa5 2020-11-24 stsp worktree = NULL;
9178 6458efa5 2020-11-24 stsp }
9179 349dfd1e 2023-07-23 thomas
9180 557d3365 2023-04-14 thomas error = view_loop(view);
9181 349dfd1e 2023-07-23 thomas
9182 6458efa5 2020-11-24 stsp done:
9183 349dfd1e 2023-07-23 thomas free(tog_base_commit.id);
9184 6458efa5 2020-11-24 stsp free(repo_path);
9185 6458efa5 2020-11-24 stsp free(cwd);
9186 349dfd1e 2023-07-23 thomas if (worktree != NULL)
9187 349dfd1e 2023-07-23 thomas got_worktree_close(worktree);
9188 1d0f4054 2021-06-17 stsp if (repo) {
9189 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9190 1d0f4054 2021-06-17 stsp if (close_err)
9191 1d0f4054 2021-06-17 stsp error = close_err;
9192 1d0f4054 2021-06-17 stsp }
9193 7cd52833 2022-06-23 thomas if (pack_fds) {
9194 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9195 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9196 7cd52833 2022-06-23 thomas if (error == NULL)
9197 7cd52833 2022-06-23 thomas error = pack_err;
9198 b85a3496 2023-04-14 thomas }
9199 51a10b52 2020-12-26 stsp tog_free_refs();
9200 6458efa5 2020-11-24 stsp return error;
9201 6458efa5 2020-11-24 stsp }
9202 6458efa5 2020-11-24 stsp
9203 fc2737d5 2022-09-15 thomas static const struct got_error*
9204 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9205 fc2737d5 2022-09-15 thomas const char *str)
9206 fc2737d5 2022-09-15 thomas {
9207 fc2737d5 2022-09-15 thomas size_t len;
9208 fc2737d5 2022-09-15 thomas
9209 fc2737d5 2022-09-15 thomas if (win == NULL)
9210 fc2737d5 2022-09-15 thomas win = stdscr;
9211 fc2737d5 2022-09-15 thomas
9212 fc2737d5 2022-09-15 thomas len = strlen(str);
9213 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9214 fc2737d5 2022-09-15 thomas
9215 fc2737d5 2022-09-15 thomas if (focus)
9216 fc2737d5 2022-09-15 thomas wstandout(win);
9217 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9218 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9219 fc2737d5 2022-09-15 thomas if (focus)
9220 fc2737d5 2022-09-15 thomas wstandend(win);
9221 fc2737d5 2022-09-15 thomas
9222 fc2737d5 2022-09-15 thomas return NULL;
9223 fc2737d5 2022-09-15 thomas }
9224 fc2737d5 2022-09-15 thomas
9225 2a31b33b 2022-07-23 thomas static const struct got_error *
9226 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9227 fc2737d5 2022-09-15 thomas {
9228 fc2737d5 2022-09-15 thomas off_t *p;
9229 fc2737d5 2022-09-15 thomas
9230 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9231 fc2737d5 2022-09-15 thomas if (p == NULL) {
9232 fc2737d5 2022-09-15 thomas free(*line_offsets);
9233 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9234 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9235 fc2737d5 2022-09-15 thomas }
9236 fc2737d5 2022-09-15 thomas
9237 fc2737d5 2022-09-15 thomas *line_offsets = p;
9238 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9239 fc2737d5 2022-09-15 thomas ++(*nlines);
9240 fc2737d5 2022-09-15 thomas return NULL;
9241 fc2737d5 2022-09-15 thomas }
9242 fc2737d5 2022-09-15 thomas
9243 fc2737d5 2022-09-15 thomas static const struct got_error *
9244 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9245 fc2737d5 2022-09-15 thomas {
9246 fc2737d5 2022-09-15 thomas *ret = 0;
9247 fc2737d5 2022-09-15 thomas
9248 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9249 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9250 fc2737d5 2022-09-15 thomas size_t len = 1;
9251 fc2737d5 2022-09-15 thomas
9252 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9253 fc2737d5 2022-09-15 thomas continue;
9254 fc2737d5 2022-09-15 thomas
9255 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9256 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9257 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9258 fc2737d5 2022-09-15 thomas
9259 fc2737d5 2022-09-15 thomas len += strlen(t);
9260 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9261 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9262 fc2737d5 2022-09-15 thomas free(t0);
9263 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9264 fc2737d5 2022-09-15 thomas }
9265 fc2737d5 2022-09-15 thomas
9266 fc2737d5 2022-09-15 thomas return NULL;
9267 fc2737d5 2022-09-15 thomas }
9268 fc2737d5 2022-09-15 thomas
9269 fc2737d5 2022-09-15 thomas /*
9270 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9271 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9272 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9273 fc2737d5 2022-09-15 thomas */
9274 fc2737d5 2022-09-15 thomas static const struct got_error *
9275 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9276 fc2737d5 2022-09-15 thomas {
9277 fc2737d5 2022-09-15 thomas int n, len = width;
9278 fc2737d5 2022-09-15 thomas
9279 fc2737d5 2022-09-15 thomas if (km->keys) {
9280 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9281 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9282 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9283 30e77f6a 2022-09-18 thomas };
9284 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9285 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9286 fc2737d5 2022-09-15 thomas
9287 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9288 fc2737d5 2022-09-15 thomas
9289 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9290 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9291 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9292 fc2737d5 2022-09-15 thomas
9293 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9294 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9295 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9296 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9297 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9298 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9299 fc2737d5 2022-09-15 thomas if (n < 0) {
9300 fc2737d5 2022-09-15 thomas free(t0);
9301 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9302 fc2737d5 2022-09-15 thomas }
9303 fc2737d5 2022-09-15 thomas first = 0;
9304 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9305 fc2737d5 2022-09-15 thomas *off += n;
9306 fc2737d5 2022-09-15 thomas }
9307 fc2737d5 2022-09-15 thomas free(t0);
9308 fc2737d5 2022-09-15 thomas }
9309 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9310 fc2737d5 2022-09-15 thomas if (n < 0)
9311 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9312 fc2737d5 2022-09-15 thomas *off += n;
9313 fc2737d5 2022-09-15 thomas
9314 fc2737d5 2022-09-15 thomas return NULL;
9315 fc2737d5 2022-09-15 thomas }
9316 fc2737d5 2022-09-15 thomas
9317 fc2737d5 2022-09-15 thomas static const struct got_error *
9318 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9319 fc2737d5 2022-09-15 thomas {
9320 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9321 fc2737d5 2022-09-15 thomas off_t off = 0;
9322 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9323 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9324 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9325 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9326 fc2737d5 2022-09-15 thomas GENERATE_HELP
9327 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9328 fc2737d5 2022-09-15 thomas #undef KEY_
9329 fc2737d5 2022-09-15 thomas };
9330 fc2737d5 2022-09-15 thomas
9331 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9332 fc2737d5 2022-09-15 thomas if (err)
9333 fc2737d5 2022-09-15 thomas return err;
9334 fc2737d5 2022-09-15 thomas
9335 fc2737d5 2022-09-15 thomas n = nitems(km);
9336 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9337 fc2737d5 2022-09-15 thomas if (err)
9338 fc2737d5 2022-09-15 thomas return err;
9339 fc2737d5 2022-09-15 thomas
9340 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9341 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9342 fc2737d5 2022-09-15 thomas show = s->all;
9343 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9344 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9345 fc2737d5 2022-09-15 thomas show = 1;
9346 fc2737d5 2022-09-15 thomas }
9347 fc2737d5 2022-09-15 thomas if (show) {
9348 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9349 fc2737d5 2022-09-15 thomas if (err)
9350 fc2737d5 2022-09-15 thomas return err;
9351 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9352 fc2737d5 2022-09-15 thomas if (err)
9353 fc2737d5 2022-09-15 thomas return err;
9354 fc2737d5 2022-09-15 thomas }
9355 fc2737d5 2022-09-15 thomas }
9356 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9357 fc2737d5 2022-09-15 thomas ++off;
9358 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9359 fc2737d5 2022-09-15 thomas return err;
9360 fc2737d5 2022-09-15 thomas }
9361 fc2737d5 2022-09-15 thomas
9362 fc2737d5 2022-09-15 thomas static const struct got_error *
9363 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9364 fc2737d5 2022-09-15 thomas {
9365 fc2737d5 2022-09-15 thomas FILE *f;
9366 fc2737d5 2022-09-15 thomas const struct got_error *err;
9367 fc2737d5 2022-09-15 thomas
9368 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9369 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9370 fc2737d5 2022-09-15 thomas s->nlines = 0;
9371 fc2737d5 2022-09-15 thomas
9372 fc2737d5 2022-09-15 thomas f = got_opentemp();
9373 fc2737d5 2022-09-15 thomas if (f == NULL)
9374 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9375 fc2737d5 2022-09-15 thomas s->f = f;
9376 fc2737d5 2022-09-15 thomas
9377 fc2737d5 2022-09-15 thomas err = format_help(s);
9378 fc2737d5 2022-09-15 thomas if (err)
9379 fc2737d5 2022-09-15 thomas return err;
9380 fc2737d5 2022-09-15 thomas
9381 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9382 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9383 fc2737d5 2022-09-15 thomas
9384 fc2737d5 2022-09-15 thomas return NULL;
9385 fc2737d5 2022-09-15 thomas }
9386 fc2737d5 2022-09-15 thomas
9387 fc2737d5 2022-09-15 thomas static const struct got_error *
9388 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9389 fc2737d5 2022-09-15 thomas {
9390 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9391 fc2737d5 2022-09-15 thomas return NULL;
9392 fc2737d5 2022-09-15 thomas }
9393 fc2737d5 2022-09-15 thomas
9394 2a7d3cd7 2022-09-17 thomas static void
9395 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9396 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9397 2a7d3cd7 2022-09-17 thomas {
9398 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9399 2a7d3cd7 2022-09-17 thomas
9400 2a7d3cd7 2022-09-17 thomas *f = s->f;
9401 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9402 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9403 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9404 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9405 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9406 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9407 2a7d3cd7 2022-09-17 thomas }
9408 2a7d3cd7 2022-09-17 thomas
9409 fc2737d5 2022-09-15 thomas static const struct got_error *
9410 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9411 fc2737d5 2022-09-15 thomas {
9412 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9413 fc2737d5 2022-09-15 thomas const struct got_error *err;
9414 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9415 fc2737d5 2022-09-15 thomas wchar_t *wline;
9416 fc2737d5 2022-09-15 thomas char *line;
9417 fc2737d5 2022-09-15 thomas ssize_t linelen;
9418 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9419 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9420 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9421 fc2737d5 2022-09-15 thomas
9422 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9423 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9424 fc2737d5 2022-09-15 thomas
9425 fc2737d5 2022-09-15 thomas s->lineno = 0;
9426 fc2737d5 2022-09-15 thomas rewind(s->f);
9427 fc2737d5 2022-09-15 thomas werase(view->window);
9428 fc2737d5 2022-09-15 thomas
9429 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9430 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9431 fc2737d5 2022-09-15 thomas
9432 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9433 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9434 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9435 fc2737d5 2022-09-15 thomas if (err)
9436 fc2737d5 2022-09-15 thomas return err;
9437 fc2737d5 2022-09-15 thomas if (eos <= 1)
9438 fc2737d5 2022-09-15 thomas return NULL;
9439 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9440 fc2737d5 2022-09-15 thomas eos -= 2;
9441 fc2737d5 2022-09-15 thomas
9442 fc2737d5 2022-09-15 thomas s->eof = 0;
9443 fc2737d5 2022-09-15 thomas view->maxx = 0;
9444 fc2737d5 2022-09-15 thomas line = NULL;
9445 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9446 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9447 fc2737d5 2022-09-15 thomas
9448 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9449 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9450 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9451 fc2737d5 2022-09-15 thomas free(line);
9452 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9453 fc2737d5 2022-09-15 thomas }
9454 fc2737d5 2022-09-15 thomas s->eof = 1;
9455 fc2737d5 2022-09-15 thomas break;
9456 fc2737d5 2022-09-15 thomas }
9457 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9458 fc2737d5 2022-09-15 thomas continue;
9459 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9460 fc2737d5 2022-09-15 thomas continue;
9461 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9462 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9463 fc2737d5 2022-09-15 thomas
9464 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9465 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9466 fc2737d5 2022-09-15 thomas if (err) {
9467 fc2737d5 2022-09-15 thomas free(line);
9468 fc2737d5 2022-09-15 thomas return err;
9469 fc2737d5 2022-09-15 thomas }
9470 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9471 fc2737d5 2022-09-15 thomas free(wline);
9472 fc2737d5 2022-09-15 thomas wline = NULL;
9473 fc2737d5 2022-09-15 thomas
9474 fc2737d5 2022-09-15 thomas if (attr)
9475 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9476 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9477 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9478 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9479 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9480 fc2737d5 2022-09-15 thomas if (err) {
9481 fc2737d5 2022-09-15 thomas free(line);
9482 fc2737d5 2022-09-15 thomas return err;
9483 fc2737d5 2022-09-15 thomas }
9484 fc2737d5 2022-09-15 thomas } else {
9485 fc2737d5 2022-09-15 thomas int skip;
9486 fc2737d5 2022-09-15 thomas
9487 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9488 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9489 fc2737d5 2022-09-15 thomas if (err) {
9490 fc2737d5 2022-09-15 thomas free(line);
9491 fc2737d5 2022-09-15 thomas return err;
9492 fc2737d5 2022-09-15 thomas }
9493 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9494 fc2737d5 2022-09-15 thomas free(wline);
9495 fc2737d5 2022-09-15 thomas wline = NULL;
9496 fc2737d5 2022-09-15 thomas }
9497 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9498 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9499 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9500 fc2737d5 2022-09-15 thomas } else {
9501 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9502 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9503 fc2737d5 2022-09-15 thomas }
9504 fc2737d5 2022-09-15 thomas if (attr)
9505 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9506 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9507 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9508 fc2737d5 2022-09-15 thomas }
9509 fc2737d5 2022-09-15 thomas free(line);
9510 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9511 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9512 fc2737d5 2022-09-15 thomas else
9513 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9514 fc2737d5 2022-09-15 thomas
9515 fc2737d5 2022-09-15 thomas view_border(view);
9516 fc2737d5 2022-09-15 thomas
9517 fc2737d5 2022-09-15 thomas if (s->eof) {
9518 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9519 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9520 fc2737d5 2022-09-15 thomas view->ncols - 1);
9521 fc2737d5 2022-09-15 thomas if (rc == ERR)
9522 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9523 fc2737d5 2022-09-15 thomas } else {
9524 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9525 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9526 fc2737d5 2022-09-15 thomas wstandout(view->window);
9527 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9528 fc2737d5 2022-09-15 thomas view->ncols - 1);
9529 fc2737d5 2022-09-15 thomas if (rc == ERR)
9530 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9531 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9532 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9533 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9534 fc2737d5 2022-09-15 thomas if (rc == ERR)
9535 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9536 fc2737d5 2022-09-15 thomas }
9537 fc2737d5 2022-09-15 thomas wstandend(view->window);
9538 fc2737d5 2022-09-15 thomas }
9539 fc2737d5 2022-09-15 thomas
9540 fc2737d5 2022-09-15 thomas return NULL;
9541 fc2737d5 2022-09-15 thomas }
9542 fc2737d5 2022-09-15 thomas
9543 fc2737d5 2022-09-15 thomas static const struct got_error *
9544 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9545 fc2737d5 2022-09-15 thomas {
9546 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9547 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9548 fc2737d5 2022-09-15 thomas char *line = NULL;
9549 fc2737d5 2022-09-15 thomas ssize_t linelen;
9550 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9551 fc2737d5 2022-09-15 thomas int eos, nscroll;
9552 fc2737d5 2022-09-15 thomas
9553 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9554 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9555 fc2737d5 2022-09-15 thomas --eos; /* border */
9556 fc2737d5 2022-09-15 thomas
9557 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9558 fc2737d5 2022-09-15 thomas
9559 fc2737d5 2022-09-15 thomas switch (ch) {
9560 fc2737d5 2022-09-15 thomas case '0':
9561 fc2737d5 2022-09-15 thomas case '$':
9562 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9563 fc2737d5 2022-09-15 thomas case 'l':
9564 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9565 fc2737d5 2022-09-15 thomas case 'h':
9566 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9567 fc2737d5 2022-09-15 thomas break;
9568 fc2737d5 2022-09-15 thomas case 'g':
9569 fc2737d5 2022-09-15 thomas case KEY_HOME:
9570 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9571 fc2737d5 2022-09-15 thomas view->count = 0;
9572 fc2737d5 2022-09-15 thomas break;
9573 fc2737d5 2022-09-15 thomas case 'G':
9574 fc2737d5 2022-09-15 thomas case KEY_END:
9575 fc2737d5 2022-09-15 thomas view->count = 0;
9576 fc2737d5 2022-09-15 thomas if (s->eof)
9577 fc2737d5 2022-09-15 thomas break;
9578 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9579 fc2737d5 2022-09-15 thomas s->eof = 1;
9580 fc2737d5 2022-09-15 thomas break;
9581 fc2737d5 2022-09-15 thomas case 'k':
9582 fc2737d5 2022-09-15 thomas case KEY_UP:
9583 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9584 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9585 fc2737d5 2022-09-15 thomas else
9586 fc2737d5 2022-09-15 thomas view->count = 0;
9587 fc2737d5 2022-09-15 thomas break;
9588 fc2737d5 2022-09-15 thomas case CTRL('u'):
9589 fc2737d5 2022-09-15 thomas case 'u':
9590 fc2737d5 2022-09-15 thomas nscroll /= 2;
9591 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9592 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9593 fc2737d5 2022-09-15 thomas case CTRL('b'):
9594 fc2737d5 2022-09-15 thomas case 'b':
9595 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9596 fc2737d5 2022-09-15 thomas view->count = 0;
9597 fc2737d5 2022-09-15 thomas break;
9598 fc2737d5 2022-09-15 thomas }
9599 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9600 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9601 fc2737d5 2022-09-15 thomas break;
9602 fc2737d5 2022-09-15 thomas case 'j':
9603 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9604 fc2737d5 2022-09-15 thomas case CTRL('n'):
9605 fc2737d5 2022-09-15 thomas if (!s->eof)
9606 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9607 fc2737d5 2022-09-15 thomas else
9608 fc2737d5 2022-09-15 thomas view->count = 0;
9609 fc2737d5 2022-09-15 thomas break;
9610 fc2737d5 2022-09-15 thomas case CTRL('d'):
9611 fc2737d5 2022-09-15 thomas case 'd':
9612 fc2737d5 2022-09-15 thomas nscroll /= 2;
9613 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9614 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9615 fc2737d5 2022-09-15 thomas case CTRL('f'):
9616 fc2737d5 2022-09-15 thomas case 'f':
9617 fc2737d5 2022-09-15 thomas case ' ':
9618 fc2737d5 2022-09-15 thomas if (s->eof) {
9619 fc2737d5 2022-09-15 thomas view->count = 0;
9620 fc2737d5 2022-09-15 thomas break;
9621 fc2737d5 2022-09-15 thomas }
9622 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9623 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9624 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9625 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9626 fc2737d5 2022-09-15 thomas if (feof(s->f))
9627 fc2737d5 2022-09-15 thomas s->eof = 1;
9628 fc2737d5 2022-09-15 thomas else
9629 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9630 fc2737d5 2022-09-15 thomas break;
9631 fc2737d5 2022-09-15 thomas }
9632 fc2737d5 2022-09-15 thomas }
9633 fc2737d5 2022-09-15 thomas free(line);
9634 fc2737d5 2022-09-15 thomas break;
9635 fc2737d5 2022-09-15 thomas default:
9636 fc2737d5 2022-09-15 thomas view->count = 0;
9637 fc2737d5 2022-09-15 thomas break;
9638 fc2737d5 2022-09-15 thomas }
9639 fc2737d5 2022-09-15 thomas
9640 fc2737d5 2022-09-15 thomas return err;
9641 fc2737d5 2022-09-15 thomas }
9642 fc2737d5 2022-09-15 thomas
9643 fc2737d5 2022-09-15 thomas static const struct got_error *
9644 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9645 fc2737d5 2022-09-15 thomas {
9646 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9647 fc2737d5 2022-09-15 thomas
9648 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9649 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9650 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9651 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9652 fc2737d5 2022-09-15 thomas
9653 fc2737d5 2022-09-15 thomas return NULL;
9654 fc2737d5 2022-09-15 thomas }
9655 fc2737d5 2022-09-15 thomas
9656 fc2737d5 2022-09-15 thomas static const struct got_error *
9657 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9658 fc2737d5 2022-09-15 thomas {
9659 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9660 fc2737d5 2022-09-15 thomas
9661 fc2737d5 2022-09-15 thomas
9662 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9663 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9664 fc2737d5 2022-09-15 thomas
9665 fc2737d5 2022-09-15 thomas wclear(view->window);
9666 fc2737d5 2022-09-15 thomas view->count = 0;
9667 fc2737d5 2022-09-15 thomas view->x = 0;
9668 fc2737d5 2022-09-15 thomas s->all = !s->all;
9669 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9670 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9671 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9672 fc2737d5 2022-09-15 thomas
9673 fc2737d5 2022-09-15 thomas return create_help(s);
9674 fc2737d5 2022-09-15 thomas }
9675 fc2737d5 2022-09-15 thomas
9676 fc2737d5 2022-09-15 thomas static const struct got_error *
9677 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9678 fc2737d5 2022-09-15 thomas {
9679 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9680 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9681 fc2737d5 2022-09-15 thomas
9682 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9683 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9684 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9685 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9686 fc2737d5 2022-09-15 thomas
9687 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9688 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9689 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9690 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9691 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9692 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9693 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9694 fc2737d5 2022-09-15 thomas
9695 fc2737d5 2022-09-15 thomas err = create_help(s);
9696 fc2737d5 2022-09-15 thomas return err;
9697 fc2737d5 2022-09-15 thomas }
9698 fc2737d5 2022-09-15 thomas
9699 fc2737d5 2022-09-15 thomas static const struct got_error *
9700 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9701 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9702 2a31b33b 2022-07-23 thomas {
9703 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9704 2a31b33b 2022-07-23 thomas
9705 2a31b33b 2022-07-23 thomas *new_view = NULL;
9706 2a31b33b 2022-07-23 thomas
9707 2a31b33b 2022-07-23 thomas switch (request) {
9708 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9709 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9710 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9711 2a31b33b 2022-07-23 thomas
9712 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9713 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9714 2a31b33b 2022-07-23 thomas view, s->repo);
9715 2a31b33b 2022-07-23 thomas } else
9716 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9717 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9718 2a31b33b 2022-07-23 thomas break;
9719 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9720 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9721 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9722 2a31b33b 2022-07-23 thomas
9723 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9724 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9725 2a31b33b 2022-07-23 thomas s->repo);
9726 2a31b33b 2022-07-23 thomas } else
9727 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9728 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9729 2a31b33b 2022-07-23 thomas break;
9730 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9731 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9732 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9733 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9734 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9735 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9736 2a31b33b 2022-07-23 thomas &view->state.tree);
9737 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9738 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9739 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9740 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9741 2a31b33b 2022-07-23 thomas else
9742 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9743 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9744 2a31b33b 2022-07-23 thomas break;
9745 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9746 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9747 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9748 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9749 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9750 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9751 2a31b33b 2022-07-23 thomas view->state.log.repo);
9752 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9753 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9754 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9755 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9756 2a31b33b 2022-07-23 thomas else
9757 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9758 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9759 2a31b33b 2022-07-23 thomas break;
9760 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9761 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9762 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9763 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9764 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9765 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9766 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9767 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9768 2a31b33b 2022-07-23 thomas else
9769 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9770 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9771 2a31b33b 2022-07-23 thomas if (err)
9772 2a31b33b 2022-07-23 thomas view_close(*new_view);
9773 2a31b33b 2022-07-23 thomas break;
9774 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9775 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9776 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9777 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9778 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9779 fc2737d5 2022-09-15 thomas if (err)
9780 fc2737d5 2022-09-15 thomas view_close(*new_view);
9781 fc2737d5 2022-09-15 thomas break;
9782 2a31b33b 2022-07-23 thomas default:
9783 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9784 2a31b33b 2022-07-23 thomas }
9785 2a31b33b 2022-07-23 thomas
9786 2a31b33b 2022-07-23 thomas return err;
9787 2a31b33b 2022-07-23 thomas }
9788 2a31b33b 2022-07-23 thomas
9789 a5d43cac 2022-07-01 thomas /*
9790 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9791 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9792 a5d43cac 2022-07-01 thomas */
9793 6458efa5 2020-11-24 stsp static void
9794 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9795 a5d43cac 2022-07-01 thomas {
9796 a5d43cac 2022-07-01 thomas switch (view->type) {
9797 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9798 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9799 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9800 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9801 a5d43cac 2022-07-01 thomas 1);
9802 a5d43cac 2022-07-01 thomas break;
9803 a5d43cac 2022-07-01 thomas }
9804 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9805 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9806 a5d43cac 2022-07-01 thomas else
9807 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9808 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9809 a5d43cac 2022-07-01 thomas break;
9810 a5d43cac 2022-07-01 thomas }
9811 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9812 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9813 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9814 a5d43cac 2022-07-01 thomas break;
9815 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9816 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9817 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9818 a5d43cac 2022-07-01 thomas break;
9819 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9820 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9821 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9822 a5d43cac 2022-07-01 thomas break;
9823 a5d43cac 2022-07-01 thomas default:
9824 a5d43cac 2022-07-01 thomas break;
9825 a5d43cac 2022-07-01 thomas }
9826 a5d43cac 2022-07-01 thomas
9827 a5d43cac 2022-07-01 thomas view->offset = 0;
9828 a5d43cac 2022-07-01 thomas }
9829 a5d43cac 2022-07-01 thomas
9830 a5d43cac 2022-07-01 thomas /*
9831 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9832 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9833 a5d43cac 2022-07-01 thomas */
9834 a5d43cac 2022-07-01 thomas static const struct got_error *
9835 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9836 a5d43cac 2022-07-01 thomas {
9837 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9838 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9839 a5d43cac 2022-07-01 thomas int *selected = NULL;
9840 a5d43cac 2022-07-01 thomas int header, offset;
9841 a5d43cac 2022-07-01 thomas
9842 a5d43cac 2022-07-01 thomas switch (view->type) {
9843 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9844 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9845 a5d43cac 2022-07-01 thomas header = 3;
9846 a5d43cac 2022-07-01 thomas scrolld = NULL;
9847 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9848 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9849 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9850 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9851 a5d43cac 2022-07-01 thomas view->offset = offset;
9852 a5d43cac 2022-07-01 thomas }
9853 a5d43cac 2022-07-01 thomas break;
9854 a5d43cac 2022-07-01 thomas }
9855 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9856 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9857 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9858 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9859 a5d43cac 2022-07-01 thomas selected = &s->selected;
9860 a5d43cac 2022-07-01 thomas break;
9861 a5d43cac 2022-07-01 thomas }
9862 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9863 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9864 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9865 a5d43cac 2022-07-01 thomas header = 3;
9866 a5d43cac 2022-07-01 thomas selected = &s->selected;
9867 a5d43cac 2022-07-01 thomas break;
9868 a5d43cac 2022-07-01 thomas }
9869 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9870 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9871 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9872 a5d43cac 2022-07-01 thomas header = 5;
9873 a5d43cac 2022-07-01 thomas selected = &s->selected;
9874 a5d43cac 2022-07-01 thomas break;
9875 a5d43cac 2022-07-01 thomas }
9876 a5d43cac 2022-07-01 thomas default:
9877 a5d43cac 2022-07-01 thomas selected = NULL;
9878 a5d43cac 2022-07-01 thomas scrolld = NULL;
9879 a5d43cac 2022-07-01 thomas header = 0;
9880 a5d43cac 2022-07-01 thomas break;
9881 a5d43cac 2022-07-01 thomas }
9882 a5d43cac 2022-07-01 thomas
9883 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9884 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9885 a5d43cac 2022-07-01 thomas view->offset = offset;
9886 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9887 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9888 a5d43cac 2022-07-01 thomas *selected -= offset;
9889 a5d43cac 2022-07-01 thomas }
9890 a5d43cac 2022-07-01 thomas }
9891 a5d43cac 2022-07-01 thomas
9892 a5d43cac 2022-07-01 thomas return err;
9893 a5d43cac 2022-07-01 thomas }
9894 a5d43cac 2022-07-01 thomas
9895 a5d43cac 2022-07-01 thomas static void
9896 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9897 ce5b7c56 2019-07-09 stsp {
9898 6059809a 2020-12-17 stsp size_t i;
9899 9f7d7167 2018-04-29 stsp
9900 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9901 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9902 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9903 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9904 ce5b7c56 2019-07-09 stsp }
9905 6879ba42 2020-10-01 naddy fputc('\n', fp);
9906 ce5b7c56 2019-07-09 stsp }
9907 ce5b7c56 2019-07-09 stsp
9908 4ed7e80c 2018-05-20 stsp __dead static void
9909 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9910 9f7d7167 2018-04-29 stsp {
9911 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9912 6879ba42 2020-10-01 naddy
9913 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9914 6879ba42 2020-10-01 naddy getprogname());
9915 6879ba42 2020-10-01 naddy if (hflag) {
9916 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9917 6879ba42 2020-10-01 naddy list_commands(fp);
9918 ee85c5e8 2020-02-29 stsp }
9919 6879ba42 2020-10-01 naddy exit(status);
9920 9f7d7167 2018-04-29 stsp }
9921 9f7d7167 2018-04-29 stsp
9922 c2301be8 2018-04-30 stsp static char **
9923 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9924 c2301be8 2018-04-30 stsp {
9925 ee85c5e8 2020-02-29 stsp va_list ap;
9926 c2301be8 2018-04-30 stsp char **argv;
9927 ee85c5e8 2020-02-29 stsp int i;
9928 c2301be8 2018-04-30 stsp
9929 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9930 ee85c5e8 2020-02-29 stsp
9931 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9932 c2301be8 2018-04-30 stsp if (argv == NULL)
9933 c2301be8 2018-04-30 stsp err(1, "calloc");
9934 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9935 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9936 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9937 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9938 c2301be8 2018-04-30 stsp }
9939 c2301be8 2018-04-30 stsp
9940 ee85c5e8 2020-02-29 stsp va_end(ap);
9941 c2301be8 2018-04-30 stsp return argv;
9942 ee85c5e8 2020-02-29 stsp }
9943 ee85c5e8 2020-02-29 stsp
9944 ee85c5e8 2020-02-29 stsp /*
9945 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9946 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9947 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9948 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9949 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9950 ee85c5e8 2020-02-29 stsp */
9951 ee85c5e8 2020-02-29 stsp static const struct got_error *
9952 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9953 ee85c5e8 2020-02-29 stsp {
9954 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9955 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9956 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9957 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9958 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9959 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9960 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9961 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9962 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9963 84de9106 2020-12-26 stsp
9964 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9965 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9966 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9967 ee85c5e8 2020-02-29 stsp
9968 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9969 7cd52833 2022-06-23 thomas if (error != NULL)
9970 7cd52833 2022-06-23 thomas goto done;
9971 7cd52833 2022-06-23 thomas
9972 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, NULL);
9973 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9974 ee85c5e8 2020-02-29 stsp goto done;
9975 ee85c5e8 2020-02-29 stsp
9976 ee85c5e8 2020-02-29 stsp if (worktree)
9977 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9978 ee85c5e8 2020-02-29 stsp else
9979 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9980 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9981 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9982 ee85c5e8 2020-02-29 stsp goto done;
9983 ee85c5e8 2020-02-29 stsp }
9984 ee85c5e8 2020-02-29 stsp
9985 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9986 ee85c5e8 2020-02-29 stsp if (error != NULL)
9987 ee85c5e8 2020-02-29 stsp goto done;
9988 ee85c5e8 2020-02-29 stsp
9989 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9990 ee85c5e8 2020-02-29 stsp repo, worktree);
9991 ee85c5e8 2020-02-29 stsp if (error)
9992 ee85c5e8 2020-02-29 stsp goto done;
9993 ee85c5e8 2020-02-29 stsp
9994 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9995 84de9106 2020-12-26 stsp if (error)
9996 84de9106 2020-12-26 stsp goto done;
9997 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9998 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9999 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
10000 ee85c5e8 2020-02-29 stsp if (error)
10001 ee85c5e8 2020-02-29 stsp goto done;
10002 ee85c5e8 2020-02-29 stsp
10003 ee85c5e8 2020-02-29 stsp if (worktree) {
10004 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10005 ee85c5e8 2020-02-29 stsp worktree = NULL;
10006 ee85c5e8 2020-02-29 stsp }
10007 ee85c5e8 2020-02-29 stsp
10008 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
10009 945f9229 2022-04-16 thomas if (error)
10010 945f9229 2022-04-16 thomas goto done;
10011 945f9229 2022-04-16 thomas
10012 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
10013 ee85c5e8 2020-02-29 stsp if (error) {
10014 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
10015 ee85c5e8 2020-02-29 stsp goto done;
10016 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
10017 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
10018 6879ba42 2020-10-01 naddy usage(1, 1);
10019 ee85c5e8 2020-02-29 stsp /* not reached */
10020 ee85c5e8 2020-02-29 stsp }
10021 ee85c5e8 2020-02-29 stsp
10022 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
10023 ee85c5e8 2020-02-29 stsp if (error)
10024 ee85c5e8 2020-02-29 stsp goto done;
10025 ee85c5e8 2020-02-29 stsp
10026 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
10027 ee85c5e8 2020-02-29 stsp argc = 4;
10028 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
10029 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
10030 ee85c5e8 2020-02-29 stsp done:
10031 1d0f4054 2021-06-17 stsp if (repo) {
10032 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
10033 1d0f4054 2021-06-17 stsp if (error == NULL)
10034 1d0f4054 2021-06-17 stsp error = close_err;
10035 1d0f4054 2021-06-17 stsp }
10036 945f9229 2022-04-16 thomas if (commit)
10037 945f9229 2022-04-16 thomas got_object_commit_close(commit);
10038 ee85c5e8 2020-02-29 stsp if (worktree)
10039 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
10040 7cd52833 2022-06-23 thomas if (pack_fds) {
10041 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
10042 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
10043 7cd52833 2022-06-23 thomas if (error == NULL)
10044 7cd52833 2022-06-23 thomas error = pack_err;
10045 7cd52833 2022-06-23 thomas }
10046 ee85c5e8 2020-02-29 stsp free(id);
10047 ee85c5e8 2020-02-29 stsp free(commit_id_str);
10048 ee85c5e8 2020-02-29 stsp free(commit_id);
10049 ee85c5e8 2020-02-29 stsp free(cwd);
10050 ee85c5e8 2020-02-29 stsp free(repo_path);
10051 ee85c5e8 2020-02-29 stsp free(in_repo_path);
10052 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
10053 ee85c5e8 2020-02-29 stsp int i;
10054 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
10055 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
10056 ee85c5e8 2020-02-29 stsp free(cmd_argv);
10057 ee85c5e8 2020-02-29 stsp }
10058 87670572 2020-12-26 naddy tog_free_refs();
10059 ee85c5e8 2020-02-29 stsp return error;
10060 c2301be8 2018-04-30 stsp }
10061 c2301be8 2018-04-30 stsp
10062 9f7d7167 2018-04-29 stsp int
10063 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
10064 9f7d7167 2018-04-29 stsp {
10065 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
10066 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
10067 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
10068 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
10069 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
10070 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
10071 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
10072 83cd27f8 2020-01-13 stsp };
10073 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
10074 557d3365 2023-04-14 thomas const char *test_script_path;
10075 a0abeae5 2023-02-17 thomas
10076 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
10077 a0abeae5 2023-02-17 thomas
10078 557d3365 2023-04-14 thomas /*
10079 e2a79cf9 2023-07-26 thomas * Override default signal handlers before starting ncurses.
10080 e2a79cf9 2023-07-26 thomas * This should prevent ncurses from installing its own
10081 e2a79cf9 2023-07-26 thomas * broken cleanup() signal handler.
10082 e2a79cf9 2023-07-26 thomas */
10083 e2a79cf9 2023-07-26 thomas signal(SIGWINCH, tog_sigwinch);
10084 e2a79cf9 2023-07-26 thomas signal(SIGPIPE, tog_sigpipe);
10085 e2a79cf9 2023-07-26 thomas signal(SIGCONT, tog_sigcont);
10086 e2a79cf9 2023-07-26 thomas signal(SIGINT, tog_sigint);
10087 e2a79cf9 2023-07-26 thomas signal(SIGTERM, tog_sigterm);
10088 e2a79cf9 2023-07-26 thomas
10089 e2a79cf9 2023-07-26 thomas /*
10090 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
10091 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
10092 557d3365 2023-04-14 thomas */
10093 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
10094 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
10095 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
10096 557d3365 2023-04-14 thomas if (error) {
10097 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10098 557d3365 2023-04-14 thomas return 1;
10099 557d3365 2023-04-14 thomas }
10100 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
10101 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
10102 557d3365 2023-04-14 thomas
10103 557d3365 2023-04-14 thomas #if !defined(PROFILE)
10104 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
10105 a0abeae5 2023-02-17 thomas NULL) == -1)
10106 a0abeae5 2023-02-17 thomas err(1, "pledge");
10107 a0abeae5 2023-02-17 thomas #endif
10108 9f7d7167 2018-04-29 stsp
10109 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
10110 9f7d7167 2018-04-29 stsp switch (ch) {
10111 9f7d7167 2018-04-29 stsp case 'h':
10112 9f7d7167 2018-04-29 stsp hflag = 1;
10113 9f7d7167 2018-04-29 stsp break;
10114 53ccebc2 2019-07-30 stsp case 'V':
10115 53ccebc2 2019-07-30 stsp Vflag = 1;
10116 53ccebc2 2019-07-30 stsp break;
10117 9f7d7167 2018-04-29 stsp default:
10118 6879ba42 2020-10-01 naddy usage(hflag, 1);
10119 9f7d7167 2018-04-29 stsp /* NOTREACHED */
10120 9f7d7167 2018-04-29 stsp }
10121 9f7d7167 2018-04-29 stsp }
10122 9f7d7167 2018-04-29 stsp
10123 9f7d7167 2018-04-29 stsp argc -= optind;
10124 9f7d7167 2018-04-29 stsp argv += optind;
10125 9814e6a3 2020-09-27 naddy optind = 1;
10126 c2301be8 2018-04-30 stsp optreset = 1;
10127 9f7d7167 2018-04-29 stsp
10128 53ccebc2 2019-07-30 stsp if (Vflag) {
10129 53ccebc2 2019-07-30 stsp got_version_print_str();
10130 6879ba42 2020-10-01 naddy return 0;
10131 53ccebc2 2019-07-30 stsp }
10132 4010e238 2020-12-04 stsp
10133 c2301be8 2018-04-30 stsp if (argc == 0) {
10134 f29d3e89 2018-06-23 stsp if (hflag)
10135 6879ba42 2020-10-01 naddy usage(hflag, 0);
10136 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
10137 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
10138 c2301be8 2018-04-30 stsp argc = 1;
10139 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
10140 c2301be8 2018-04-30 stsp } else {
10141 6059809a 2020-12-17 stsp size_t i;
10142 9f7d7167 2018-04-29 stsp
10143 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
10144 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
10145 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
10146 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
10147 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
10148 9f7d7167 2018-04-29 stsp break;
10149 9f7d7167 2018-04-29 stsp }
10150 9f7d7167 2018-04-29 stsp }
10151 ee85c5e8 2020-02-29 stsp }
10152 3642c4c6 2019-07-09 stsp
10153 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
10154 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
10155 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
10156 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
10157 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
10158 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
10159 adf4c9e0 2022-07-03 thomas }
10160 adf4c9e0 2022-07-03 thomas
10161 92845f09 2023-07-26 thomas tog_base_commit.idx = -1;
10162 92845f09 2023-07-26 thomas tog_base_commit.marker = GOT_WORKTREE_STATE_UNKNOWN;
10163 92845f09 2023-07-26 thomas
10164 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
10165 ee85c5e8 2020-02-29 stsp if (argc != 1)
10166 6879ba42 2020-10-01 naddy usage(0, 1);
10167 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
10168 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
10169 ee85c5e8 2020-02-29 stsp } else {
10170 ee85c5e8 2020-02-29 stsp if (hflag)
10171 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
10172 ee85c5e8 2020-02-29 stsp else
10173 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
10174 9f7d7167 2018-04-29 stsp }
10175 9f7d7167 2018-04-29 stsp
10176 1d98034b 2023-04-14 thomas if (using_mock_io) {
10177 1d98034b 2023-04-14 thomas io_err = tog_io_close();
10178 1d98034b 2023-04-14 thomas if (error == NULL)
10179 1d98034b 2023-04-14 thomas error = io_err;
10180 1d98034b 2023-04-14 thomas }
10181 9f7d7167 2018-04-29 stsp endwin();
10182 a2f4a359 2020-02-28 stsp if (cmd_argv) {
10183 a2f4a359 2020-02-28 stsp int i;
10184 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
10185 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
10186 a2f4a359 2020-02-28 stsp free(cmd_argv);
10187 a2f4a359 2020-02-28 stsp }
10188 a2f4a359 2020-02-28 stsp
10189 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
10190 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
10191 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
10192 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
10193 66b04f8f 2023-07-19 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR)) {
10194 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
10195 66b04f8f 2023-07-19 thomas return 1;
10196 66b04f8f 2023-07-19 thomas }
10197 9f7d7167 2018-04-29 stsp return 0;
10198 9f7d7167 2018-04-29 stsp }